-
-
Notifications
You must be signed in to change notification settings - Fork 679
Expand file tree
/
Copy pathDialogViewModel.Delegate.cs
More file actions
722 lines (625 loc) · 26.3 KB
/
Copy pathDialogViewModel.Delegate.cs
File metadata and controls
722 lines (625 loc) · 26.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
//
// Copyright (c) Fela Ameghino 2015-2026
//
// Distributed under the GNU General Public License v3.0. (See accompanying
// file LICENSE or copy at https://www.gnu.org/licenses/gpl-3.0.txt)
//
using System;
using System.Linq;
using System.Text;
using Telegram.Common;
using Telegram.Controls;
using Telegram.Controls.Messages.Content;
using Telegram.Controls.Stories;
using Telegram.Converters;
using Telegram.Navigation.Services;
using Telegram.Services;
using Telegram.Services.Updates;
using Telegram.Td.Api;
using Telegram.ViewModels.Chats;
using Telegram.ViewModels.Gallery;
using Telegram.ViewModels.Stories;
using Telegram.Views;
using Telegram.Views.Popups;
using Windows.Foundation;
using Windows.UI.Xaml;
namespace Telegram.ViewModels
{
public partial class DialogViewModel
{
public void ViewVisibleMessages()
{
Delegate?.ViewVisibleMessages();
}
public void DoubleTapped(MessageViewModel message, bool alternate)
{
if (AppSettings.Appearance.IsQuickReplySelected || alternate)
{
if (alternate)
{
ReactToMessage(message, ClientService.DefaultReaction);
}
else
{
ReplyToMessage(message);
}
}
else if (alternate)
{
ReplyToMessage(message);
}
else
{
ReactToMessage(message, ClientService.DefaultReaction);
}
}
private void ReactToMessage(MessageViewModel message, ReactionType reaction)
{
if (message.InteractionInfo?.Reactions != null && message.InteractionInfo.Reactions.IsChosen(reaction))
{
ClientService.Send(new RemoveMessageReaction(message.ChatId, message.Id, reaction));
}
else
{
ClientService.Send(new AddMessageReaction(message.ChatId, message.Id, reaction, false, false));
}
}
public async void OpenReply(MessageViewModel message)
{
if (message.ReplyToState == MessageReplyToState.Deleted || message.ReplyTo is not MessageReplyToMessage replyToMessage)
{
return;
}
if (replyToMessage.ChatId != message.ChatId && ClientService.TryGetChat(replyToMessage.ChatId, out Chat replyToChat))
{
if (ClientService.TryGetSupergroup(replyToChat, out Supergroup supergroup))
{
if (supergroup.Status is ChatMemberStatusLeft && !supergroup.IsPublic() && !ClientService.IsChatAccessible(replyToChat))
{
if (supergroup.IsChannel)
{
ShowToast(replyToMessage.Quote != null && replyToMessage.Quote.IsManual
? Strings.QuotePrivateChannel
: Strings.ReplyPrivateChannel, ToastPopupIcon.Info);
}
else
{
ShowToast(replyToMessage.Quote != null && replyToMessage.Quote.IsManual
? Strings.QuotePrivateGroup
: Strings.ReplyPrivateGroup, ToastPopupIcon.Info);
}
return;
}
}
else if (replyToMessage.MessageId == 0)
{
ShowToast(replyToMessage.Quote != null && replyToMessage.Quote.IsManual
? Strings.QuotePrivate
: Strings.ReplyPrivate, ToastPopupIcon.Info);
return;
}
long chatId = replyToChat.Id;
long messageId = replyToMessage.MessageId;
MessageTopic messageTopic = null;
if (message.ChatId == ClientService.Options.RepliesBotChatId)
{
// TODO: 172 is this correct?
if (message.ForwardInfo?.Origin is MessageOriginUser or MessageOriginChat && message.ForwardInfo?.Source != null)
{
chatId = message.ForwardInfo.Source.ChatId;
messageTopic = new MessageTopicThread(message.ForwardInfo.Source.MessageId);
}
else if (message.ForwardInfo?.Origin is MessageOriginChannel fromChannel)
{
chatId = fromChannel.ChatId;
messageTopic = new MessageTopicThread(fromChannel.MessageId);
}
if (messageTopic is MessageTopicThread messageTopicThread)
{
await ClientService.SendAsync(new GetMessage(chatId, messageTopicThread.MessageThreadId));
var response = await ClientService.SendAsync(new GetMessageThread(chatId, messageTopicThread.MessageThreadId));
if (response is not MessageThreadInfo)
{
return;
}
}
}
NavigationService.NavigateToChat(chatId, messageId, topic: messageTopic, state: new NavigationState
{
{ "highlight", replyToMessage.Quote },
{ "checklist_task_id", replyToMessage.ChecklistTaskId },
{ "poll_option_id", replyToMessage.PollOptionId }
});
}
else if (replyToMessage.Origin != null && replyToMessage.MessageId == 0)
{
ShowToast(replyToMessage.Quote != null && replyToMessage.Quote.IsManual
? Strings.QuotePrivate
: Strings.ReplyPrivate, ToastPopupIcon.Info);
}
else if (replyToMessage.ChatId == message.ChatId || replyToMessage.ChatId == 0)
{
await LoadMessageSliceAsync(message.Id, replyToMessage.MessageId, highlight: replyToMessage.Quote, checklistTaskId: replyToMessage.ChecklistTaskId, pollOptionId: replyToMessage.PollOptionId);
}
}
public async void OpenThread(MessageViewModel message)
{
long chatId = message.ChatId;
long threadId = message.Id;
long? messageId = null;
if (message.ChatId == ClientService.Options.RepliesBotChatId)
{
// TODO: 172 is this correct?
if (message.ForwardInfo?.Origin is MessageOriginUser or MessageOriginChat && message.ForwardInfo?.Source != null)
{
chatId = message.ForwardInfo.Source.ChatId;
threadId = message.ForwardInfo.Source.MessageId;
messageId = threadId;
}
else if (message.ForwardInfo?.Origin is MessageOriginChannel fromChannel)
{
chatId = fromChannel.ChatId;
threadId = fromChannel.MessageId;
messageId = threadId;
}
await ClientService.SendAsync(new GetMessage(chatId, threadId));
var properties = await ClientService.SendAsync(new GetMessageProperties(chatId, threadId)) as MessageProperties;
if (properties == null || !properties.CanGetMessageThread)
{
NavigationService.NavigateToChat(chatId, threadId);
return;
}
}
var response = await ClientService.SendAsync(new GetMessageThread(chatId, threadId));
if (response is MessageThreadInfo)
{
NavigationService.NavigateToChat(chatId, messageId, topic: new MessageTopicThread(threadId));
}
}
public bool IsAdministrator(MessageSender memberId) => _messageDelegate.IsAdministrator(memberId);
public void OpenWebPage(MessageViewModel message)
{
if (message.Content is not MessageText text)
{
return;
}
if (text.LinkPreview?.InstantViewVersion != 0)
{
var url = text.LinkPreview.Url;
foreach (var entity in text.Text.Entities)
{
string compare;
if (entity.Type is TextEntityTypeUrl)
{
compare = text.Text.Text.Substring(entity.Offset, entity.Length);
}
else if (entity.Type is TextEntityTypeTextUrl textUrl)
{
compare = textUrl.Url;
}
else
{
continue;
}
if (MessageHelper.AreTheSame(url, compare, out _))
{
url = compare;
break;
}
}
NavigationService.NavigateToInstant(url);
}
else if (text.LinkPreview != null)
{
MessageHelper.OpenUrl(ClientService, NavigationService, text.LinkPreview.Url, !text.LinkPreview.SkipConfirmation, new OpenUrlSourceChat(message.ChatId, message.SenderId));
}
}
public async void OpenSticker(Sticker sticker)
{
if (sticker.SetId != 0)
{
await StickersPopup.ShowAsync(NavigationService, sticker.SetId);
}
}
public async void OpenGame(MessageViewModel message)
{
if (_chat is not Chat chat)
{
return;
}
var game = message.Content as MessageGame;
if (game == null)
{
return;
}
var response = await ClientService.SendAsync(new GetCallbackQueryAnswer(chat.Id, message.Id, new CallbackQueryPayloadGame(game.Game.ShortName)));
if (response is CallbackQueryAnswer answer && !string.IsNullOrEmpty(answer.Url))
{
ChatActionManager.SetTyping(new ChatActionStartPlayingGame());
var viaBot = message.GetViaBotUser();
if (viaBot != null)
{
NavigationService.NavigateToWebApp(viaBot, answer.Url, game.Game.Title, message.ChatId, message.Id);
}
}
}
public void Call(MessageViewModel message, bool video)
{
if (message.Content is MessageGroupCall groupCall)
{
_voipService.JoinGroupCall(NavigationService, new InputGroupCallMessage(message.ChatId, message.Id));
}
else
{
Call(video);
}
}
public async void VotePoll(MessageViewModel message, Vector<int> options)
{
var poll = message.Content as MessagePoll;
if (poll == null || options == null)
{
return;
}
await ClientService.SendAsync(new SetPollAnswer(message.ChatId, message.Id, options));
var updated = message.Content as MessagePoll;
if (updated.Poll.Type is PollTypeQuiz quiz)
{
var ids1 = quiz.CorrectOptionIds.OrderBy(x => x).ToList();
var ids2 = options.OrderBy(x => x).ToList();
if (ids1.SequenceEqual(ids2))
{
Aggregator.Publish(new UpdateConfetti());
}
else
{
Delegate?.UpdateBubbleWithMessageId(message.Id, bubble =>
{
if (bubble.MediaTemplateRoot is PollContent pollContent)
{
pollContent.ShowExplanation();
}
VisualUtilities.ShakeView(bubble);
});
}
}
}
public void OpenUser(long userId)
{
_messageDelegate.OpenUser(userId);
}
public void OpenViaBot(long viaBotUserId)
{
var chat = Chat;
if (chat != null && chat.Type is ChatTypeSupergroup super && super.IsChannel)
{
var supergroup = ClientService.GetSupergroup(super.SupergroupId);
if (supergroup != null && !supergroup.CanPostMessages())
{
return;
}
}
var user = ClientService.GetUser(viaBotUserId);
if (user != null && user.HasActiveUsername(out string username))
{
SetText($"@{username} ", focus: true);
ResolveInlineBot(username);
}
}
public void OpenChat(long chatId, bool profile = false)
{
var chat = ClientService.GetChat(chatId);
if (chat == null)
{
return;
}
if (profile)
{
NavigationService.Navigate(typeof(ProfilePage), chat.Id);
}
else
{
NavigationService.NavigateToChat(chat);
}
}
public void OpenChat(long chatId, long messageId)
{
var chat = ClientService.GetChat(chatId);
if (chat == null)
{
return;
}
NavigationService.NavigateToChat(chat, message: messageId);
}
public void OpenHashtag(string hashtag)
{
Search = new ChatSearchViewModel(ClientService, NavigationService, Settings, Aggregator, this, hashtag, null);
}
public void OpenUrl(string url, bool untrust, OpenUrlSource source = null)
{
_messageDelegate.OpenUrl(url, untrust, source);
}
public void OpenPoll(MessageViewModel message)
{
if (message.Content is MessagePoll poll)
{
ShowPopup(new PollResultsPopup(ClientService, Settings, Aggregator, _messageDelegate, message.ChatId, message.Id, poll.Poll));
}
}
public async void OpenMedia(MessageViewModel message, FrameworkElement target, double timestamp = 0)
{
if (message.Content is MessageAudio or MessageVoiceNote)
{
LifetimeService.Current.Playback.Play(XamlRoot, message, TopicId);
if (timestamp > 0)
{
LifetimeService.Current.Playback.Seek(TimeSpan.FromSeconds(timestamp));
}
}
else if (message.Content is MessageGame game && message.ReplyMarkup is ReplyMarkupInlineKeyboard inline)
{
foreach (var row in inline.Rows)
{
foreach (var button in row)
{
if (button.Type is InlineKeyboardButtonTypeCallbackGame)
{
OpenInlineButton(message, button);
}
}
}
}
else if (message.Content is MessageAsyncStory story && story.Story != null)
{
Rect GetOrigin(ActiveStoriesViewModel activeStories)
{
var transform = target.TransformToVisual(null);
var point = transform.TransformPoint(new Point());
return new Rect(point.X, point.Y, target.ActualWidth, target.ActualHeight);
}
var transform = target.TransformToVisual(null);
var point = transform.TransformPoint(new Point());
var origin = new Rect(point.X, point.Y, target.ActualWidth, target.ActualHeight);
var activeStories = new ActiveStoriesViewModel(ClientService, Settings, Aggregator, story.Story);
var viewModel = StoryListViewModel.Create(NavigationService, activeStories);
var window = new StoriesWindow(XamlRoot);
window.Update(viewModel, activeStories, StoryOpenOrigin.Card, origin, GetOrigin);
_ = window.ShowAsync();
}
else
{
GalleryViewModelBase viewModel = null;
var linkPreview = message.Content is MessageText text ? text.LinkPreview : null;
if (linkPreview != null && linkPreview.Type is LinkPreviewTypeAlbum album)
{
viewModel = InstantGalleryViewModel.Create(ClientService, StorageService, Aggregator, message, album);
}
if (viewModel == null && (message.Content is MessageAnimation || linkPreview?.Type is LinkPreviewTypeAnimation))
{
Delegate?.PlayMessage(message, target);
}
else
{
if (viewModel == null)
{
static bool IsSingle(MessageContent content)
{
return content switch
{
MessagePhoto photo => photo.IsSecret,
MessageVideo video => video.IsSecret,
MessageVideoNote videoNote => videoNote.IsSecret,
MessageDocument => false,
_ => true
};
}
var properties = await ClientService.SendAsync(new GetMessageProperties(message.ChatId, message.Id)) as MessageProperties;
if (properties == null && Type != DialogType.EventLog)
{
return;
}
if (Type == DialogType.EventLog || IsSingle(message.Content))
{
viewModel = new StandaloneGalleryViewModel(ClientService, _storageService, Aggregator, new GalleryMessage(ClientService, message, properties));
}
else
{
viewModel = new ChatGalleryViewModel(ClientService, _storageService, Aggregator, message.ChatId, TopicId, message, properties);
}
}
NavigationService.ShowGallery(viewModel, target, timestamp);
}
//TextField?.Focus(FocusState.Programmatic);
}
}
public void OpenMedia(GalleryMedia media, FrameworkElement target)
{
var viewModel = new StandaloneGalleryViewModel(ClientService, _storageService, Aggregator, media);
NavigationService.ShowGallery(viewModel, target);
}
public void OpenPaidMedia(MessageViewModel message, PaidMedia media, FrameworkElement target, double timestamp = 0)
{
if (message.Content is MessagePaidAlbum album)
{
GalleryMedia item = null;
GalleryMedia Filter(PaidMedia x)
{
GalleryMedia result = null;
if (x is PaidMediaPhoto photo)
{
result = new GalleryPhoto(ClientService, photo.Photo, null, true);
}
else if (x is PaidMediaVideo video)
{
result = new GalleryVideo(ClientService, video.Video, null, true);
}
if (x == media)
{
item = result;
}
return result;
}
var items = album.Media
.Select(Filter)
.Where(x => x is not null)
.ToList();
var viewModel = new StandaloneGalleryViewModel(ClientService, StorageService, Aggregator, items, item);
NavigationService.ShowGallery(viewModel, target, timestamp);
//TextField?.Focus(FocusState.Programmatic);
}
}
public void PlayMessage(MessageViewModel message)
{
LifetimeService.Current.Playback.Play(XamlRoot, message, TopicId);
}
public bool RecognizeSpeech(MessageViewModel message)
{
if (ClientService.IsPremium)
{
_needsUpdateSpeechRecognitionTrial = false;
ClientService.Send(new RecognizeSpeech(message.ChatId, message.Id));
return true;
}
else if (ClientService.SpeechRecognitionTrial.LeftCount > 0)
{
_needsUpdateSpeechRecognitionTrial = true;
ClientService.Send(new RecognizeSpeech(message.ChatId, message.Id));
return true;
}
else if (ClientService.SpeechRecognitionTrial.WeeklyCount > 0)
{
ShowSpeechRecognitionTrial(3);
}
else
{
ShowSpeechRecognitionTrial(0);
}
return false;
}
private void ShowSpeechRecognitionTrial(int type)
{
_needsUpdateSpeechRecognitionTrial = false;
var trial = ClientService.SpeechRecognitionTrial;
var builder = new StringBuilder();
if (type == 0)
{
// TODO: generic error
}
else if (type == 1)
{
if (trial.NextResetDate > 0)
{
builder.Append(Locale.Declension(Strings.R.TranscriptionTrialLeftUntil, trial.LeftCount, Formatter.DateAt(trial.NextResetDate)));
}
else
{
builder.Append(Locale.Declension(Strings.R.TranscriptionTrialLeft, trial.LeftCount));
}
}
else if (type == 2 || type == 3)
{
builder.Append(Locale.Declension(Strings.R.TranscriptionTrialEnd, trial.WeeklyCount));
if (type == 2)
{
builder.Append(" ");
builder.Append(Strings.TranscriptionTrialEndBuy);
}
else if (trial.NextResetDate > 0)
{
builder.Append(" ");
builder.Append(string.Format(Strings.TranscriptionTrialEndWaitOrBuy, Formatter.DateAt(trial.NextResetDate)));
}
var text = builder.ToString();
var markdown = Extensions.ReplacePremiumLink(text, new PremiumFeatureVoiceRecognition());
ToastPopup.Show(XamlRoot, markdown, ToastPopupIcon.Transcribe);
}
}
public void SendBotCommand(string command)
{
_ = SendMessageAsync(null, new InputMessageText(command.AsFormattedText(false), null, false), new MessageSendOptions
{
SendingId = int.MaxValue
});
}
public void Select(MessageViewModel message)
{
if (message.IsService)
{
return;
}
if (message.MediaAlbumId != 0)
{
if (message.Content is MessageAlbum album)
{
foreach (var child in album.Messages)
{
_selectedItems[child.Id] = child;
Delegate?.UpdateMessageSelection(child);
}
Delegate?.UpdateMessageSelection(message);
}
else if (_groupedMessages.TryGetValue(message.MediaAlbumId, out MessageViewModel group))
{
_selectedItems[message.Id] = message;
Delegate?.UpdateMessageSelection(message);
Delegate?.UpdateMessageSelection(group);
}
}
else
{
_selectedItems[message.Id] = message;
Delegate?.UpdateMessageSelection(message);
}
UpdateSelectionState();
}
public void Unselect(MessageViewModel message, bool updateSelection = false)
{
if (message.MediaAlbumId != 0)
{
if (message.Content is MessageAlbum album)
{
foreach (var child in album.Messages)
{
_selectedItems.TryRemove(child.Id, out _);
Delegate?.UpdateMessageSelection(child);
}
Delegate?.UpdateMessageSelection(message);
}
else if (_groupedMessages.TryGetValue(message.MediaAlbumId, out MessageViewModel group))
{
_selectedItems.TryRemove(message.Id, out _);
Delegate?.UpdateMessageSelection(message);
Delegate?.UpdateMessageSelection(group);
}
}
else
{
_selectedItems.TryRemove(message.Id, out _);
Delegate?.UpdateMessageSelection(message);
}
if (updateSelection && SelectedItems.Count < 1 && IsReportingMessages == null)
{
IsSelectionEnabled = false;
}
UpdateSelectionState();
}
private async void UpdateSelectionState()
{
RaisePropertyChanged(nameof(CanCopySelectedMessage));
RaisePropertyChanged(nameof(CanReportSelectedMessages));
RaisePropertyChanged(nameof(SelectedCount));
if (Type is DialogType.BusinessReplies or DialogType.WelcomeMessages)
{
CanDeleteSelectedMessages = true;
CanForwardSelectedMessages = false;
}
else
{
var selectedItems = SelectedItems.Values.ToList();
var properties = await ClientService.GetMessagePropertiesAsync(selectedItems.Select(x => new MessageId(x)));
CanDeleteSelectedMessages = properties.Count > 0 && properties.Values.All(x => x.CanBeDeletedForAllUsers || x.CanBeDeletedOnlyForSelf);
CanForwardSelectedMessages = properties.Count > 0 && properties.Values.All(x => x.CanBeForwarded);
}
}
}
}