-
-
Notifications
You must be signed in to change notification settings - Fork 679
Expand file tree
/
Copy pathChatInviteLinksViewModel.cs
More file actions
405 lines (338 loc) · 14.8 KB
/
Copy pathChatInviteLinksViewModel.cs
File metadata and controls
405 lines (338 loc) · 14.8 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
//
// 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.Threading.Tasks;
using Telegram.Collections;
using Telegram.Common;
using Telegram.Converters;
using Telegram.Navigation;
using Telegram.Navigation.Services;
using Telegram.Services;
using Telegram.Td.Api;
using Telegram.Views.Chats;
using Telegram.Views.Chats.Popups;
using Telegram.Views.Popups;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
namespace Telegram.ViewModels.Chats
{
public partial class ChatInviteLinksViewModel : ViewModelBase, IIncrementalCollectionOwner, IHandle
{
private LinksCollection _inviteLinks;
private LinkCountsCollection _linkCounts;
private LinksCollection _revokedLinks;
public ChatInviteLinksViewModel(IClientService clientService, ISettingsService settingsService, IEventAggregator aggregator)
: base(clientService, settingsService, aggregator)
{
}
public IncrementalCollection<ChatInviteLink> InviteLinks => _inviteLinks?.Items;
public IncrementalCollection<ChatInviteLinkCount> LinkCounts => _linkCounts?.Items;
public IncrementalCollection<ChatInviteLink> RevokedLinks => _revokedLinks?.Items;
private ChatInviteLink _inviteLink;
public ChatInviteLink InviteLink
{
get => _inviteLink;
set => Set(ref _inviteLink, value);
}
public bool IsChannel => _channel;
private long _chatId;
private long _creatorUserId;
private long _supergroupId;
private long _basicGroupId;
private bool _channel;
private bool _canCreateJoinRequests;
protected override Task OnNavigatedToAsync(object parameter, NavigationMode mode, NavigationState state)
{
if (parameter is long chatId)
{
parameter = new ChatInviteLinksArgs(chatId, ClientService.Options.MyId);
}
if (parameter is ChatInviteLinksArgs args)
{
_chatId = args.ChatId;
_creatorUserId = args.CreatorUserId;
_inviteLinks = new LinksCollection(ClientService, _chatId, _creatorUserId, false, this, null);
_revokedLinks = new LinksCollection(ClientService, _chatId, _creatorUserId, true, this, this);
_linkCounts = new LinkCountsCollection(ClientService, _chatId, _creatorUserId == ClientService.Options.MyId);
if (ClientService.TryGetChat(_chatId, out Chat chat))
{
if (ClientService.TryGetSupergroup(chat, out Supergroup supergroup))
{
// Handle(UpdateSupergroupFullInfo) matches on this, and it's the only
// way InviteLink ever arrives when full info isn't cached yet.
_supergroupId = supergroup.Id;
_channel = supergroup.IsChannel;
_canCreateJoinRequests = !supergroup.IsPublic();
if (supergroup.HasActiveUsername(out string username))
{
InviteLink = new ChatInviteLink
{
InviteLink = MeUrlPrefixConverter.Convert(ClientService, username)
};
}
else if (ClientService.TryGetSupergroupFull(chat, out SupergroupFullInfo supergroupFullInfo))
{
InviteLink = supergroupFullInfo.InviteLink;
}
else
{
ClientService.Send(new GetSupergroupFullInfo(supergroup.Id));
}
}
else if (ClientService.TryGetBasicGroup(chat, out BasicGroup basicGroup))
{
_basicGroupId = basicGroup.Id;
_channel = false;
_canCreateJoinRequests = true;
if (ClientService.TryGetBasicGroupFull(chat, out BasicGroupFullInfo basicGroupFullInfo))
{
InviteLink = basicGroupFullInfo.InviteLink;
}
else
{
ClientService.Send(new GetBasicGroupFullInfo(basicGroup.Id));
}
}
}
}
return Task.CompletedTask;
}
public override void Subscribe()
{
Aggregator.Subscribe<UpdateSupergroupFullInfo>(this, Handle)
.Subscribe<UpdateBasicGroupFullInfo>(Handle);
}
private void Handle(UpdateSupergroupFullInfo update)
{
if (update.SupergroupId == _supergroupId)
{
BeginOnUIThread(() => InviteLink = update.SupergroupFullInfo.InviteLink);
}
}
private void Handle(UpdateBasicGroupFullInfo update)
{
if (update.BasicGroupId == _basicGroupId)
{
BeginOnUIThread(() => InviteLink = update.BasicGroupFullInfo.InviteLink);
}
}
// Three lists behind one: active links, per-creator counts, then revoked links. HasMoreItems
// below aggregates all three, so no single phase running out ends the list.
public async Task<IncrementalLoadResult> LoadMoreItemsAsync(uint count)
{
Logger.Info();
uint totalCount;
if (_inviteLinks.HasMoreItems)
{
totalCount = (await _inviteLinks.Items.LoadMoreItemsAsync(count)).Count;
}
else if (_linkCounts != null && _linkCounts.HasMoreItems)
{
totalCount = (await _linkCounts.Items.LoadMoreItemsAsync(count)).Count;
}
else
{
totalCount = (await _revokedLinks.LoadMoreItemsAsyncImpl(count)).Count;
}
return new IncrementalLoadResult(totalCount, HasMoreItems);
}
public async void CreateLink()
{
var popup = new ChatInviteLinkPopup(ClientService, _chatId, _channel, _canCreateJoinRequests, null);
var confirm = await ShowPopupAsync(popup);
if (confirm == ContentDialogResult.Primary)
{
var response = await ClientService.SendAsync(popup.Request);
if (response is ChatInviteLink newLink)
{
InviteLinks.Insert(0, newLink);
}
}
}
public void CopyLink(ChatInviteLink inviteLink)
{
MessageHelper.CopyLink(XamlRoot, inviteLink.InviteLink);
}
public async void ShareLink(ChatInviteLink inviteLink)
{
await ShowPopupAsync(new ChooseChatsPopup(), new ChooseChatsConfigurationPostText(inviteLink.InviteLink));
}
public async void EditLink(ChatInviteLink inviteLink)
{
var popup = new ChatInviteLinkPopup(ClientService, _chatId, _channel, _canCreateJoinRequests, inviteLink);
var confirm = await ShowPopupAsync(popup);
if (confirm == ContentDialogResult.Primary)
{
var response = await ClientService.SendAsync(popup.Request);
if (response is ChatInviteLink newLink)
{
var index = InviteLinks.IndexOf(inviteLink);
InviteLinks.RemoveAt(index);
InviteLinks.Insert(index, newLink);
}
}
}
public async void RevokeLink(ChatInviteLink inviteLink)
{
var confirm = await ShowPopupAsync(Strings.RevokeAlert, Strings.RevokeLink, Strings.RevokeButton, Strings.Cancel, destructive: true);
if (confirm == ContentDialogResult.Primary)
{
var response = await ClientService.SendAsync(new RevokeChatInviteLink(_chatId, inviteLink.InviteLink));
if (response is ChatInviteLinks inviteLinks)
{
InviteLinks.Remove(inviteLink);
for (int i = inviteLinks.InviteLinks.Count - 1; i >= 0; i--)
{
var newLink = inviteLinks.InviteLinks[i];
if (newLink.IsRevoked)
{
RevokedLinks.Insert(0, newLink);
}
else if (newLink.IsPrimary)
{
InviteLink = newLink;
}
}
}
}
}
public async void DeleteLink(ChatInviteLink inviteLink)
{
var confirm = await ShowPopupAsync(Strings.DeleteLinkHelp, Strings.DeleteLink, Strings.Delete, Strings.Cancel, destructive: true);
if (confirm == ContentDialogResult.Primary)
{
var response = await ClientService.SendAsync(new DeleteRevokedChatInviteLink(_chatId, inviteLink.InviteLink));
if (response is Ok)
{
RevokedLinks.Remove(inviteLink);
}
}
}
public async void RevokeAll()
{
var confirm = await ShowPopupAsync(Strings.DeleteAllRevokedLinkHelp, Strings.DeleteAllRevokedLinks, Strings.Delete, Strings.Cancel, destructive: true);
if (confirm == ContentDialogResult.Primary)
{
var response = await ClientService.SendAsync(new DeleteAllRevokedChatInviteLinks(_chatId, _creatorUserId));
if (response is Ok)
{
RevokedLinks.Clear();
}
}
}
public void OpenInviteLink(ChatInviteLink inviteLink)
{
ShowPopup(new ChatInviteLinkInfoPopup(this, _chatId, inviteLink));
}
public void OpenInviteLinkCount(ChatInviteLinkCount inviteLinkCount)
{
NavigationService.Navigate(typeof(ChatInviteLinksPage), new ChatInviteLinksArgs(_chatId, inviteLinkCount.UserId));
}
public bool HasMoreItems => _inviteLinks.HasMoreItems || _revokedLinks.HasMoreItemsImpl || _linkCounts.HasMoreItems;
class LinksCollection : IIncrementalCollectionOwner
{
private readonly IClientService _clientService;
private readonly long _chatId;
private readonly long _creatorUserId;
private readonly bool _isRevoked;
private readonly ChatInviteLinksViewModel _viewModel;
private readonly ChatInviteLinksViewModel _owner;
private int _offsetDate = 0;
private string _offsetInviteLink = string.Empty;
public LinksCollection(IClientService clientService, long chatId, long creatorUserId, bool isRevoked, ChatInviteLinksViewModel viewModel, ChatInviteLinksViewModel owner)
{
_clientService = clientService;
_chatId = chatId;
_creatorUserId = creatorUserId;
_isRevoked = isRevoked;
_viewModel = viewModel;
_owner = owner;
Items = new IncrementalCollection<ChatInviteLink>(this);
}
public IncrementalCollection<ChatInviteLink> Items { get; private set; }
public Task<IncrementalLoadResult> LoadMoreItemsAsync(uint count)
{
if (_owner != null)
{
return _owner.LoadMoreItemsAsync(count);
}
return LoadMoreItemsAsyncImpl(count);
}
public async Task<IncrementalLoadResult> LoadMoreItemsAsyncImpl(uint count)
{
var totalCount = 0u;
var response = await _clientService.SendAsync(new GetChatInviteLinks(_chatId, _creatorUserId, _isRevoked, _offsetDate, _offsetInviteLink, 100));
if (response is ChatInviteLinks inviteLinks)
{
foreach (var item in inviteLinks.InviteLinks)
{
_offsetDate = item.Date;
_offsetInviteLink = item.InviteLink;
if (item.IsPrimary && (_viewModel.InviteLink == null || _viewModel.InviteLink.InviteLink == item.InviteLink))
{
_viewModel.InviteLink = item;
continue;
}
Items.Add(item);
totalCount++;
}
}
HasMoreItemsImpl = totalCount > 0;
return new IncrementalLoadResult(totalCount, HasMoreItems);
}
public bool HasMoreItemsImpl { get; private set; } = true;
public bool HasMoreItems
{
get
{
if (_owner != null)
{
return _owner.HasMoreItems;
}
return HasMoreItemsImpl;
}
}
}
class LinkCountsCollection : IIncrementalCollectionOwner
{
private readonly IClientService _clientService;
private readonly long _chatId;
public LinkCountsCollection(IClientService clientService, long chatId, bool hasMoreItems)
{
_clientService = clientService;
_chatId = chatId;
Items = new IncrementalCollection<ChatInviteLinkCount>(this);
HasMoreItems = hasMoreItems;
}
public IncrementalCollection<ChatInviteLinkCount> Items { get; private set; }
public async Task<IncrementalLoadResult> LoadMoreItemsAsync(uint count)
{
if (!HasMoreItems)
{
return default;
}
var totalCount = 0u;
var response = await _clientService.SendAsync(new GetChatInviteLinkCounts(_chatId));
if (response is ChatInviteLinkCounts inviteLinks)
{
foreach (var item in inviteLinks.InviteLinkCounts)
{
if (item.UserId == _clientService.Options.MyId)
{
continue;
}
Items.Add(item);
totalCount++;
}
}
HasMoreItems = false;
return new IncrementalLoadResult(totalCount, HasMoreItems);
}
public bool HasMoreItems { get; private set; } = true;
}
}
}