-
Notifications
You must be signed in to change notification settings - Fork 10.2k
Expand file tree
/
Copy pathBotForumHelper.java
More file actions
786 lines (623 loc) · 29.6 KB
/
Copy pathBotForumHelper.java
File metadata and controls
786 lines (623 loc) · 29.6 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
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
package org.telegram.messenger;
import static org.telegram.messenger.AndroidUtilities.dp;
import android.app.Activity;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.DynamicDrawableSpan;
import android.util.LongSparseArray;
import android.util.SparseIntArray;
import androidx.annotation.Nullable;
import org.telegram.messenger.utils.tlutils.TlUtils;
import org.telegram.tgnet.TLObject;
import org.telegram.tgnet.TLRPC;
import org.telegram.tgnet.tl.TL_forum;
import org.telegram.tgnet.tl.TL_iv;
import org.telegram.tgnet.tl.TL_update;
import org.telegram.ui.ActionBar.Theme;
import org.telegram.ui.Components.ColoredImageSpan;
import org.telegram.ui.Components.TypingDotsDrawable;
import org.telegram.ui.MultiLayoutTypingAnimator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;
public class BotForumHelper extends BaseController {
private MessageObject createDraftMessage(long userId, int topicId, long randomId, int messageId, TLRPC.TL_textWithEntities text) {
TLRPC.Message message = new TLRPC.TL_message();
message.dialog_id = userId;
message.peer_id = getMessagesController().getPeer(userId);
message.from_id = getMessagesController().getPeer(userId);
message.id = message.local_id = messageId;
message.random_id = randomId;
message.message = text.text;
message.entities = text.entities;
message.flags |= 128;
message.date = getConnectionsManager().getCurrentTime();
message.reply_to = new TLRPC.TL_messageReplyHeader();
message.flags |= 16;
message.reply_to.forum_topic = true;
message.reply_to.reply_to_top_id = topicId;
message.reply_to.flags |= 2;
message.media = new TLRPC.TL_messageMediaEmpty();
message.flags |= 512;
MessageObject messageObject = new MessageObject(currentAccount, message, false, true);
messageObject.isBotPendingDraft = true;
messageObject.resetLayout();
return messageObject;
}
private MessageObject createDraftMessage(long userId, int topicId, long randomId, int messageId, TL_iv.RichMessage rich_message) {
TLRPC.Message message = new TLRPC.TL_message();
message.dialog_id = userId;
message.peer_id = getMessagesController().getPeer(userId);
message.from_id = getMessagesController().getPeer(userId);
message.id = message.local_id = messageId;
message.random_id = randomId;
message.message = "";
message.flags |= TLObject.FLAG_13;
message.rich_message = rich_message;
message.date = getConnectionsManager().getCurrentTime();
message.reply_to = new TLRPC.TL_messageReplyHeader();
message.flags |= 16;
message.reply_to.forum_topic = true;
message.reply_to.reply_to_top_id = topicId;
message.reply_to.flags |= 2;
message.media = new TLRPC.TL_messageMediaEmpty();
message.flags |= 512;
MessageObject messageObject = new MessageObject(currentAccount, message, false, true);
messageObject.isBotPendingDraft = true;
messageObject.resetLayout();
return messageObject;
}
// user_id > topic_id -> random_id - message
private final DialogTopicIdKeyMap<BotDraftMessage> botTextDraftsByRandomIds = new DialogTopicIdKeyMap<>();
private final DialogTopicIdKeyMap<Object> botTextDraftsByRandomIdsBlocklist = new DialogTopicIdKeyMap<>();
public void onBotForumDraftUpdate(long userId, int topicId, TLRPC.TL_sendMessageTextDraftAction action) {
onBotForumDraftUpdate(userId, topicId, action.random_id, action.text, action.can_stop, action.keep_on_stop);
}
public void onBotForumDraftUpdate(long userId, int topicId, TLRPC.TL_sendMessageRichMessageDraftAction action) {
onBotForumDraftUpdate(userId, topicId, action.random_id, action.rich_message, action.can_stop, action.keep_on_stop);
}
private void onBotForumDraftUpdate(long userId, int topicId, long randomId, TLRPC.TL_textWithEntities text, boolean canStop, boolean keepOnStop) {
FileLog.d("[BotForum] onDraftNewDraft " + userId + " " + topicId + " " + randomId);
if (botTextDraftsByRandomIdsBlocklist.get(userId, topicId, randomId) != null) {
FileLog.d("[BotForum] onDraftNewDraft ignore " + userId + " " + topicId + " " + randomId);
return;
}
LongSparseArray<BotDraftMessage> drafts = botTextDraftsByRandomIds.get(userId, topicId);
long[] toRemove = null;
if (drafts != null && drafts.size() > 0) {
toRemove = new long[drafts.size()];
for (int a = 0, N = drafts.size(); a < N; a++) {
toRemove[a] = drafts.keyAt(a);
}
}
BotDraftMessage draftMessage = botTextDraftsByRandomIds.get(userId, topicId, randomId);
if (draftMessage == null) {
draftMessage = new BotDraftMessage(userId, topicId, randomId, getUserConfig().getNewMessageId());
botTextDraftsByRandomIds.put(userId, topicId, randomId, draftMessage);
}
draftMessage.keepOnStop = keepOnStop;
draftMessage.canStop = canStop;
if (toRemove != null) {
for (long id: toRemove) {
if (id == randomId) {
continue;
}
BotDraftMessage deletedMessage = drafts.get(id);
if (deletedMessage.selfDestruct != null) {
AndroidUtilities.cancelRunOnUIThread(deletedMessage.selfDestruct);
}
onBotForumDraftTimeout(userId, topicId, id);
}
}
final boolean isNew = draftMessage.messageObject == null;
if (draftMessage.selfDestruct != null) {
AndroidUtilities.cancelRunOnUIThread(draftMessage.selfDestruct);
}
draftMessage.selfDestruct = () -> onBotForumDraftTimeout(userId, topicId, randomId);
draftMessage.text = text;
draftMessage.messageObject = createDraftMessage(userId, topicId, randomId, draftMessage.localMessageId, text);
AndroidUtilities.runOnUIThread(draftMessage.selfDestruct, getAppGlobalConfig().messageTypingDraftTtl.get(TimeUnit.MILLISECONDS));
getNotificationCenter().postNotificationName(NotificationCenter.botForumDraftUpdate,
new BotForumTextDraftUpdateNotification(userId, topicId, draftMessage.messageObject, isNew));
}
private void onBotForumDraftUpdate(long userId, int topicId, long randomId, TL_iv.RichMessage text, boolean canStop, boolean keepOnStop) {
FileLog.d("[BotForum] onDraftNewDraft (rich_message) " + userId + " " + topicId + " " + randomId);
if (botTextDraftsByRandomIdsBlocklist.get(userId, topicId, randomId) != null) {
FileLog.d("[BotForum] onDraftNewDraft (rich_message) ignore " + userId + " " + topicId + " " + randomId);
return;
}
LongSparseArray<BotDraftMessage> drafts = botTextDraftsByRandomIds.get(userId, topicId);
long[] toRemove = null;
if (drafts != null && drafts.size() > 0) {
toRemove = new long[drafts.size()];
for (int a = 0, N = drafts.size(); a < N; a++) {
toRemove[a] = drafts.keyAt(a);
}
}
BotDraftMessage draftMessage = botTextDraftsByRandomIds.get(userId, topicId, randomId);
if (draftMessage == null) {
draftMessage = new BotDraftMessage(userId, topicId, randomId, getUserConfig().getNewMessageId());
botTextDraftsByRandomIds.put(userId, topicId, randomId, draftMessage);
}
draftMessage.keepOnStop = keepOnStop;
draftMessage.canStop = canStop;
if (toRemove != null) {
for (long id: toRemove) {
if (id == randomId) {
continue;
}
BotDraftMessage deletedMessage = drafts.get(id);
if (deletedMessage.selfDestruct != null) {
AndroidUtilities.cancelRunOnUIThread(deletedMessage.selfDestruct);
}
onBotForumDraftTimeout(userId, topicId, id);
}
}
final boolean isNew = draftMessage.messageObject == null;
if (draftMessage.selfDestruct != null) {
AndroidUtilities.cancelRunOnUIThread(draftMessage.selfDestruct);
}
draftMessage.selfDestruct = () -> onBotForumDraftTimeout(userId, topicId, randomId);
draftMessage.richMessage = text;
draftMessage.messageObject = createDraftMessage(userId, topicId, randomId, draftMessage.localMessageId, text);
AndroidUtilities.runOnUIThread(draftMessage.selfDestruct, getAppGlobalConfig().messageTypingDraftTtl.get(TimeUnit.MILLISECONDS));
getNotificationCenter().postNotificationName(NotificationCenter.botForumDraftUpdate,
new BotForumTextDraftUpdateNotification(userId, topicId, draftMessage.messageObject, isNew));
}
public enum SteamingSendButtonState {
NO_STREAMING, BLOCKING, STOP
}
public SteamingSendButtonState getStreamingSendButtonState(long userId, int topicId) {
LongSparseArray<BotDraftMessage> messages = botTextDraftsByRandomIds.get(userId, topicId);
if (messages != null && messages.size() > 0) {
BotDraftMessage message = null;
for (int a = 0, N = messages.size(); a < N; a++) {
message = messages.valueAt(a);
if (!message.removed) {
break;
}
}
if (message == null || message.removed) {
return SteamingSendButtonState.NO_STREAMING;
}
return message.canStop ?
SteamingSendButtonState.STOP :
SteamingSendButtonState.BLOCKING;
}
return SteamingSendButtonState.NO_STREAMING;
}
public void stopStreaming(long userId, long topicId) {
final LongSparseArray<BotDraftMessage> drafts = botTextDraftsByRandomIds.get(userId, topicId);
if (drafts != null && drafts.size() > 0) {
long randomId = 0;
BotDraftMessage message = null;
for (int a = 0, N = drafts.size(); a < N; a++) {
randomId = drafts.keyAt(a);
message = drafts.valueAt(a);
if (!message.removed) {
break;
}
}
if (message == null || message.removed) {
return;
}
if (message.selfDestruct != null) {
AndroidUtilities.cancelRunOnUIThread(message.selfDestruct);
}
botTextDraftsByRandomIdsBlocklist.put(userId, topicId, randomId, new Object());
if (message.keepOnStop) {
message.removed = true;
} else {
botTextDraftsByRandomIds.remove(userId, topicId, randomId);
getNotificationCenter().postNotificationName(NotificationCenter.botForumDraftDelete,
new BotForumTextDraftDeleteNotification(userId, topicId, message.localMessageId));
}
final TLRPC.TL_sendMessageStopDraftAction action = new TLRPC.TL_sendMessageStopDraftAction();
action.random_id = randomId;
final TLRPC.TL_messages_setTyping req = new TLRPC.TL_messages_setTyping();
req.peer = getMessagesController().getInputPeer(userId);
req.action = action;
if (topicId != 0) {
req.flags |= TLObject.FLAG_0;
req.top_msg_id = (int) topicId;
}
getConnectionsManager().sendRequestTyped(req, (res, err) -> {});
}
}
public boolean hasBotForumDrafts(long userId, int topicId) {
LongSparseArray<BotDraftMessage> messages = botTextDraftsByRandomIds.get(userId, topicId);
if (messages != null && messages.size() > 0) {
for (int a = 0, N = messages.size(); a < N; a++) {
if (!messages.valueAt(a).removed) {
return true;
}
}
}
return false;
}
public void removeAllMarkedAsRemovedMessages(long userId, int topicId) {
LongSparseArray<BotDraftMessage> messages = botTextDraftsByRandomIds.get(userId, topicId);
if (messages == null) {
return;
}
for (int a = 0, N = messages.size(); a < N; a++) {
BotDraftMessage draftMessage = messages.valueAt(a);
if (draftMessage.removed) {
getNotificationCenter().postNotificationName(NotificationCenter.botForumDraftDelete,
new BotForumTextDraftDeleteNotification(userId, topicId, draftMessage.localMessageId));
botTextDraftsByRandomIds.remove(userId, topicId, draftMessage.randomId);
a--;
N--;
}
}
}
public MessageObject onBotForumDraftCheckNewMessages(long userId, int topicId, int messageId, String message) {
removeAllMarkedAsRemovedMessages(userId, topicId);
LongSparseArray<BotDraftMessage> messages = botTextDraftsByRandomIds.get(userId, topicId);
if (messages == null) {
return null;
}
BotDraftMessage bestDraftMessage = null;
for (int i = 0; i < messages.size(); i++) {
final BotDraftMessage draftMessage = messages.valueAt(i);
if (bestDraftMessage == null) {
bestDraftMessage = draftMessage;
}
if (message != null && draftMessage.text != null && message.startsWith(draftMessage.text.text)) {
bestDraftMessage = draftMessage;
break;
}
}
if (bestDraftMessage != null) {
if (bestDraftMessage.selfDestruct != null) {
AndroidUtilities.cancelRunOnUIThread(bestDraftMessage.selfDestruct);
}
botTextDraftsByRandomIds.remove(userId, topicId, bestDraftMessage.randomId);
FileLog.d("[BotForum] onDraftNewMessage " + userId + " " + topicId);
return bestDraftMessage.messageObject;
}
return null;
}
private void onBotForumDraftTimeout(long userId, int topicId, long randomId) {
BotDraftMessage draftMessage = botTextDraftsByRandomIds.remove(userId, topicId, randomId);
if (draftMessage == null) {
return;
}
getNotificationCenter().postNotificationName(NotificationCenter.botForumDraftDelete,
new BotForumTextDraftDeleteNotification(userId, topicId, draftMessage.localMessageId));
}
private static class BotDraftMessage {
public final long userId;
public final int topicId;
public final long randomId;
public final int localMessageId;
private boolean canStop;
private boolean keepOnStop;
private boolean removed;
private Runnable selfDestruct;
private TLRPC.TL_textWithEntities text;
private TL_iv.RichMessage richMessage;
private MessageObject messageObject;
private BotDraftMessage(long userId, int topicId, long randomId, int localMessageId) {
this.userId = userId;
this.topicId = topicId;
this.randomId = randomId;
this.localMessageId = localMessageId;
}
}
/** Send message interceptors **/
public boolean beforeSendingFinalRequest(TLObject req, MessageObject msg, Runnable send) {
return beforeSendingFinalRequest(req, Collections.singletonList(msg), send);
}
public boolean beforeSendingFinalRequest(TLObject req, List<MessageObject> messages, Runnable send) {
if (messages == null || messages.isEmpty()) return true;
final TLRPC.InputPeer inputPeer = TlUtils.getInputPeerFromSendMessageRequest(req);
final long dialogId = DialogObject.getPeerDialogId(inputPeer);
if (inputPeer == null || dialogId <= 0) {
return true;
}
final TLRPC.User user = getMessagesController().getUser(dialogId);
if (!UserObject.isBotForumWithEditableTopics(user)) {
return true;
}
final long[] messageIds = new long[messages.size()];
for (int a = 0; a < messages.size(); a++) {
messageIds[a] = messages.get(a).getId();
}
final long messageRandomId = TlUtils.getOrCalculateRandomIdFromSendMessageRequest(req);
final TLRPC.InputReplyTo inputReplyTo = TlUtils.getInputReplyToFromSendMessageRequest(req);
if (inputReplyTo instanceof TLRPC.TL_inputReplyToMessage) {
return true;
}
if (req instanceof TLRPC.TL_messages_forwardMessages) {
if (((TLRPC.TL_messages_forwardMessages) req).top_msg_id != 0) {
return true;
}
}
final String messageText = TlUtils.getMessageFromSendMessageRequest(req);
final long randomId = messageRandomId != 0 ? (~messageRandomId) : (getSendMessagesHelper().getNextRandomId());
final String topicName;
if (!TextUtils.isEmpty(messageText)) {
if (messageText.length() > 16) {
topicName = messageText.substring(0, 16) + "...";
} else {
topicName = messageText;
}
} else {
topicName = LocaleController.getString(R.string.TopicsTitleMedia);
}
performSendBotTopicCreate(inputPeer, topicName, randomId, topicId -> {
if (req instanceof TLRPC.TL_messages_forwardMessages) {
TLRPC.TL_messages_forwardMessages request = (TLRPC.TL_messages_forwardMessages) req;
request.top_msg_id = topicId;
request.flags |= 512;
} else {
final TLRPC.TL_inputReplyToMessage fixedReplyTo = new TLRPC.TL_inputReplyToMessage();
fixedReplyTo.reply_to_msg_id = topicId;
TlUtils.setInputReplyToFromSendMessageRequest(req, fixedReplyTo);
}
getMessagesStorage().getStorageQueue().postRunnable(() -> {
for (long messageId : messageIds) {
getMessagesStorage().updateMessageTopicId(dialogId, messageId, topicId);
}
AndroidUtilities.runOnUIThread(send);
});
});
return false;
}
// userId -> topicId
private final LongSparseArray<List<MessagesStorage.IntCallback>> pendingBotTopics = new LongSparseArray<>();
private void performSendBotTopicCreate(final TLRPC.InputPeer inputPeer,
final String title,
final long randomId,
final MessagesStorage.IntCallback onCreateTopic
) {
final long dialogId = DialogObject.getPeerDialogId(inputPeer);
List<MessagesStorage.IntCallback> callbacks = pendingBotTopics.get(dialogId);
if (callbacks != null) {
callbacks.add(onCreateTopic);
return;
}
callbacks = new ArrayList<>(1);
callbacks.add(onCreateTopic);
pendingBotTopics.put(dialogId, callbacks);
final TL_forum.TL_messages_createForumTopic req = new TL_forum.TL_messages_createForumTopic();
final boolean titleMissing = TextUtils.isEmpty(title);
req.title = titleMissing ? "#New Chat" : title;
req.title_missing = true;
req.peer = inputPeer;
req.random_id = randomId;
getConnectionsManager().sendRequestTyped(req, AndroidUtilities::runOnUIThread, (updates, err) -> {
if (updates == null) {
performSendBotTopicCreateComplete(dialogId, -1);
return;
}
getMessagesController().processUpdates(updates, false);
TL_update.TL_updateMessageID updateMessageID = null;
for (TLRPC.Update update: updates.updates) {
if (update instanceof TL_update.TL_updateMessageID) {
updateMessageID = (TL_update.TL_updateMessageID) update;
break;
}
}
if (updateMessageID == null) {
performSendBotTopicCreateComplete(dialogId, -1);
return;
}
final TLRPC.TL_forumTopic forumTopic = new TLRPC.TL_forumTopic();
final TLRPC.TL_messageService message = new TLRPC.TL_messageService();
final TLRPC.TL_messageActionTopicCreate actionMessage = new TLRPC.TL_messageActionTopicCreate();
actionMessage.title = title;
message.action = actionMessage;
message.peer_id = getMessagesController().getPeer(dialogId);
message.dialog_id = dialogId;
message.id = updateMessageID.id;
message.date = (int) (System.currentTimeMillis() / 1000);
forumTopic.id = updateMessageID.id;
forumTopic.my = true;
forumTopic.flags |= 2;
forumTopic.topicStartMessage = message;
forumTopic.title = title;
forumTopic.top_message = updateMessageID.id;
forumTopic.topMessage = message;
forumTopic.from_id = getMessagesController().getPeer(getUserConfig().clientUserId);
forumTopic.notify_settings = new TLRPC.TL_peerNotifySettings();
forumTopic.icon_color = 0;
forumTopic.title_missing = true;
getMessagesController().getTopicsController().onTopicCreated(dialogId, forumTopic, true);
performSendBotTopicCreateComplete(dialogId, updateMessageID.id);
getNotificationCenter().postNotificationName(
NotificationCenter.botForumTopicDidCreate,
new BotForumTopicCreateNotification(dialogId, updateMessageID.id)
);
});
}
private void performSendBotTopicCreateComplete(final long dialogId, final int topicId) {
final List<MessagesStorage.IntCallback> doneCallbacks = pendingBotTopics.get(dialogId);
if (doneCallbacks != null) {
pendingBotTopics.remove(dialogId);
for (MessagesStorage.IntCallback onDone : doneCallbacks) {
onDone.run(topicId);
}
}
}
/** Notification classes **/
public static class BotForumTopicCreateNotification {
public final long dialogId;
public final int topicId;
public BotForumTopicCreateNotification(long dialogId, int topicId) {
this.dialogId = dialogId;
this.topicId = topicId;
}
}
public static class BotForumTextDraftUpdateNotification {
public final long botUserId;
public final long botTopicId;
public final MessageObject messageObject;
public final boolean isNew;
public BotForumTextDraftUpdateNotification(long botUserId, long botTopicId, MessageObject messageObject, boolean isNew) {
this.botUserId = botUserId;
this.botTopicId = botTopicId;
this.messageObject = messageObject;
this.isNew = isNew;
}
}
public static class BotForumTextDraftDeleteNotification {
public final long botUserId;
public final long botTopicId;
public final int messageId;
public BotForumTextDraftDeleteNotification(long botUserId, long botTopicId, int messageId) {
this.botUserId = botUserId;
this.botTopicId = botTopicId;
this.messageId = messageId;
}
}
/** Helper Utils **/
public static boolean isBotForum(int currentAccount, long dialogId) {
if (dialogId > 0) {
return UserObject.isBotForum(MessagesController.getInstance(currentAccount).getUser(dialogId));
} else {
MessagesController.getInstance(currentAccount).getChat(-dialogId);
return false;
}
}
private final SharedPreferences preferences;
public void saveIsStreamingTopic(long dialogId, long topicId, boolean isStreaming) {
preferences.edit().putBoolean(dialogId + "_" + topicId, isStreaming).apply();
}
public boolean isStreamingTopic(long dialogId, long topicId) {
return preferences.getBoolean(dialogId + "_" + topicId, false);
}
/** Instance **/
private BotForumHelper(int currentAccount) {
super(currentAccount);
preferences = ApplicationLoader.applicationContext.getSharedPreferences("bot_drafts" + currentAccount, Activity.MODE_PRIVATE);
}
private static volatile BotForumHelper[] Instance = new BotForumHelper[UserConfig.MAX_ACCOUNT_COUNT];
public static BotForumHelper getInstance(final int num) {
BotForumHelper localInstance = Instance[num];
if (localInstance == null) {
synchronized (BotForumHelper.class) {
localInstance = Instance[num];
if (localInstance == null) {
Instance[num] = localInstance = new BotForumHelper(num);
}
}
}
return localInstance;
}
public static class BotDraftAnimationsPool {
private final DialogTopicIdKeyMap<MultiLayoutTypingAnimator> animators = new DialogTopicIdKeyMap<>();
private final SparseIntArray ids = new SparseIntArray(); // messageId -> pendingId;
@Nullable
public MultiLayoutTypingAnimator getAnimator(long dialogId, int messageId, boolean allowCreate) {
final int animatorId = messageId > 0 ? ids.get(messageId, 0) : messageId;
if (animatorId == 0) {
return null;
}
MultiLayoutTypingAnimator animator = animators.get(dialogId, 0, animatorId);
if (animator == null && allowCreate) {
animator = new MultiLayoutTypingAnimator();
animators.put(dialogId, 0, animatorId, animator);
}
return animator;
}
public void bind(int pendingMessageId, int messageId) {
ids.put(messageId, pendingMessageId);
}
public void removeAnimator(long dialogId, int messageId) {
final int animatorId = messageId > 0 ? ids.get(messageId, 0) : messageId;
if (animatorId == 0) {
return;
}
animators.remove(dialogId, 0, animatorId);
}
}
public static class DialogTopicIdKeyMap<T> {
private final LongSparseArray<LongSparseArray<LongSparseArray<T>>> map = new LongSparseArray<>();
public LongSparseArray<T> get(long dialogId, long topicId) {
LongSparseArray<LongSparseArray<T>> topics = map.get(dialogId);
if (topics == null) {
return null;
}
return topics.get(topicId);
}
public T get(long dialogId, long topicId, long messageId) {
LongSparseArray<T> messages = get(dialogId, topicId);
if (messages == null) {
return null;
}
return messages.get(messageId);
}
public T put(long dialogId, long topicId, long messageId, T value) {
LongSparseArray<LongSparseArray<T>> topics = map.get(dialogId);
if (topics == null) {
topics = new LongSparseArray<>();
map.put(dialogId, topics);
}
LongSparseArray<T> messages = topics.get(topicId);
if (messages == null) {
messages = new LongSparseArray<>();
topics.put(topicId, messages);
}
T oldValue = messages.get(messageId);
messages.put(messageId, value);
return oldValue;
}
public LongSparseArray<T> removeAll(long dialogId, long topicId) {
LongSparseArray<LongSparseArray<T>> topics = map.get(dialogId);
if (topics == null) {
return null;
}
LongSparseArray<T> messages = topics.get(topicId);
topics.remove(topicId);
return messages;
}
public T remove(long dialogId, long topicId, long messageId) {
LongSparseArray<LongSparseArray<T>> topics = map.get(dialogId);
if (topics == null) {
return null;
}
LongSparseArray<T> messages = topics.get(topicId);
if (messages == null) {
return null;
}
T oldValue = messages.get(messageId);
messages.remove(messageId);
return oldValue;
}
}
public static CharSequence applyTypingAnimationSpan(CharSequence text) {
if (text instanceof Spannable) {
TypingBotSpan[] spans = ((Spannable) text).getSpans(0, text.length(), TypingBotSpan.class);
if (spans != null && spans.length > 0) {
return text;
}
}
final SpannableStringBuilder ssb;
if (text instanceof SpannableStringBuilder) {
ssb = (SpannableStringBuilder) text;
} else {
ssb = new SpannableStringBuilder(text);
}
final TypingDotsDrawable typingDotsDrawable = new TypingDotsDrawable(true);
typingDotsDrawable.setColor(Color.WHITE);
typingDotsDrawable.start();
final ColoredImageSpan coloredImageSpan = new TypingBotSpan(typingDotsDrawable, DynamicDrawableSpan.ALIGN_BASELINE);
coloredImageSpan.setColorKey(Theme.key_chat_messageTextIn);
coloredImageSpan.setTopOffset(-dp(10));
ssb.append(" _");
ssb.setSpan(coloredImageSpan, ssb.length() - 1, ssb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
return ssb;
}
private static class TypingBotSpan extends ColoredImageSpan {
public TypingBotSpan(TypingDotsDrawable drawable, int align) {
super(drawable, align);
}
}
}