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
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
|
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2026
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#include "td/telegram/ForumTopicManager.h"
#include "td/telegram/AccessRights.h"
#include "td/telegram/AuthManager.h"
#include "td/telegram/ChannelId.h"
#include "td/telegram/ChatManager.h"
#include "td/telegram/CustomEmojiId.h"
#include "td/telegram/DialogManager.h"
#include "td/telegram/DraftMessageManager.h"
#include "td/telegram/ForumTopic.h"
#include "td/telegram/ForumTopic.hpp"
#include "td/telegram/ForumTopicIcon.h"
#include "td/telegram/ForumTopicInfo.hpp"
#include "td/telegram/Global.h"
#include "td/telegram/LinkManager.h"
#include "td/telegram/logevent/LogEvent.h"
#include "td/telegram/MessageQueryManager.h"
#include "td/telegram/MessagesManager.h"
#include "td/telegram/MessageThreadDb.h"
#include "td/telegram/MessageTopic.h"
#include "td/telegram/misc.h"
#include "td/telegram/NotificationManager.h"
#include "td/telegram/NotificationSettingsManager.h"
#include "td/telegram/Td.h"
#include "td/telegram/TdDb.h"
#include "td/telegram/telegram_api.h"
#include "td/telegram/UpdatesManager.h"
#include "td/telegram/UserManager.h"
#include "td/utils/algorithm.h"
#include "td/utils/buffer.h"
#include "td/utils/logging.h"
#include "td/utils/Random.h"
#include "td/utils/Slice.h"
#include "td/utils/tl_helpers.h"
namespace td {
class CreateForumTopicQuery final : public Td::ResultHandler {
Promise<td_api::object_ptr<td_api::forumTopicInfo>> promise_;
DialogId dialog_id_;
DialogId creator_dialog_id_;
int64 random_id_;
public:
explicit CreateForumTopicQuery(Promise<td_api::object_ptr<td_api::forumTopicInfo>> &&promise)
: promise_(std::move(promise)) {
}
void send(DialogId dialog_id, const string &title, bool title_missing, int32 icon_color,
CustomEmojiId icon_custom_emoji_id, DialogId as_dialog_id) {
dialog_id_ = dialog_id;
creator_dialog_id_ = td_->dialog_manager_->get_my_dialog_id();
int32 flags = 0;
if (icon_color != -1) {
flags |= telegram_api::messages_createForumTopic::ICON_COLOR_MASK;
}
if (icon_custom_emoji_id.is_valid()) {
flags |= telegram_api::messages_createForumTopic::ICON_EMOJI_ID_MASK;
}
telegram_api::object_ptr<telegram_api::InputPeer> as_input_peer;
if (as_dialog_id.is_valid()) {
as_input_peer = td_->dialog_manager_->get_input_peer(as_dialog_id, AccessRights::Write);
if (as_input_peer != nullptr) {
flags |= telegram_api::messages_createForumTopic::SEND_AS_MASK;
creator_dialog_id_ = as_dialog_id;
}
}
do {
random_id_ = Random::secure_int64();
} while (random_id_ == 0);
auto input_peer = td_->dialog_manager_->get_input_peer(dialog_id, AccessRights::Write);
if (input_peer == nullptr) {
return on_error(Status::Error(400, "Can't access the chat"));
}
send_query(G()->net_query_creator().create(
telegram_api::messages_createForumTopic(flags, title_missing, std::move(input_peer), title, icon_color,
icon_custom_emoji_id.get(), random_id_, std::move(as_input_peer)),
{{dialog_id}}));
}
void on_result(BufferSlice packet) final {
auto result_ptr = fetch_result<telegram_api::messages_createForumTopic>(packet);
if (result_ptr.is_error()) {
return on_error(result_ptr.move_as_error());
}
auto ptr = result_ptr.move_as_ok();
LOG(INFO) << "Receive result for CreateForumTopicQuery: " << to_string(ptr);
auto message = UpdatesManager::get_message_by_random_id(ptr.get(), dialog_id_, random_id_);
if (message == nullptr || message->get_id() != telegram_api::messageService::ID) {
LOG(ERROR) << "Receive invalid result for CreateForumTopicQuery: " << to_string(ptr);
return promise_.set_error(400, "Invalid result received");
}
auto service_message = static_cast<const telegram_api::messageService *>(message);
if (service_message->action_->get_id() != telegram_api::messageActionTopicCreate::ID) {
LOG(ERROR) << "Receive invalid result for CreateForumTopicQuery: " << to_string(ptr);
return promise_.set_error(400, "Invalid result received");
}
auto action = static_cast<const telegram_api::messageActionTopicCreate *>(service_message->action_.get());
auto forum_topic_id = ForumTopicId(service_message->id_);
if (service_message->reply_to_ != nullptr &&
service_message->reply_to_->get_id() == telegram_api::messageReplyHeader::ID) {
auto reply_header = static_cast<const telegram_api::messageReplyHeader *>(service_message->reply_to_.get());
if (reply_header->forum_topic_ && reply_header->reply_to_top_id_ > 0) {
forum_topic_id = ForumTopicId(reply_header->reply_to_top_id_);
}
}
auto forum_topic_info = td::make_unique<ForumTopicInfo>(
dialog_id_, forum_topic_id, action->title_, ForumTopicIcon(action->icon_color_, action->icon_emoji_id_),
service_message->date_, creator_dialog_id_, true, false, false, action->title_missing_);
td_->updates_manager_->on_get_updates(
std::move(ptr), PromiseCreator::lambda([dialog_id = dialog_id_, forum_topic_info = std::move(forum_topic_info),
promise = std::move(promise_)](Unit result) mutable {
send_closure(G()->forum_topic_manager(), &ForumTopicManager::on_forum_topic_created, dialog_id,
std::move(forum_topic_info), std::move(promise));
}));
}
void on_error(Status status) final {
td_->dialog_manager_->on_get_dialog_error(dialog_id_, status, "CreateForumTopicQuery");
promise_.set_error(std::move(status));
}
};
class EditForumTopicQuery final : public Td::ResultHandler {
Promise<Unit> promise_;
DialogId dialog_id_;
ForumTopicId forum_topic_id_;
public:
explicit EditForumTopicQuery(Promise<Unit> &&promise) : promise_(std::move(promise)) {
}
void send(DialogId dialog_id, ForumTopicId forum_topic_id, bool edit_title, const string &title,
bool edit_custom_emoji_id, CustomEmojiId icon_custom_emoji_id) {
dialog_id_ = dialog_id;
forum_topic_id_ = forum_topic_id;
auto input_peer = td_->dialog_manager_->get_input_peer(dialog_id, AccessRights::Write);
if (input_peer == nullptr) {
return on_error(Status::Error(400, "Can't access the chat"));
}
int32 flags = 0;
if (edit_title) {
flags |= telegram_api::messages_editForumTopic::TITLE_MASK;
}
if (edit_custom_emoji_id) {
flags |= telegram_api::messages_editForumTopic::ICON_EMOJI_ID_MASK;
}
send_query(G()->net_query_creator().create(
telegram_api::messages_editForumTopic(flags, std::move(input_peer), forum_topic_id.get(), title,
icon_custom_emoji_id.get(), false, false),
{{dialog_id}}));
}
void send(ChannelId channel_id, ForumTopicId forum_topic_id, bool is_closed) {
dialog_id_ = DialogId(channel_id);
forum_topic_id_ = forum_topic_id;
auto input_peer = td_->dialog_manager_->get_input_peer(DialogId(channel_id), AccessRights::Write);
if (input_peer == nullptr) {
return on_error(Status::Error(400, "Can't access the chat"));
}
int32 flags = telegram_api::messages_editForumTopic::CLOSED_MASK;
send_query(G()->net_query_creator().create(
telegram_api::messages_editForumTopic(flags, std::move(input_peer), forum_topic_id.get(), string(), 0,
is_closed, false),
{{channel_id}}));
}
void send(ChannelId channel_id, bool is_hidden) {
dialog_id_ = DialogId(channel_id);
forum_topic_id_ = ForumTopicId::general();
auto input_peer = td_->dialog_manager_->get_input_peer(DialogId(channel_id), AccessRights::Write);
if (input_peer == nullptr) {
return on_error(Status::Error(400, "Can't access the chat"));
}
int32 flags = telegram_api::messages_editForumTopic::HIDDEN_MASK;
send_query(G()->net_query_creator().create(
telegram_api::messages_editForumTopic(flags, std::move(input_peer), forum_topic_id_.get(), string(), 0, false,
is_hidden),
{{channel_id}}));
}
void on_result(BufferSlice packet) final {
auto result_ptr = fetch_result<telegram_api::messages_editForumTopic>(packet);
if (result_ptr.is_error()) {
return on_error(result_ptr.move_as_error());
}
auto ptr = result_ptr.move_as_ok();
LOG(INFO) << "Receive result for EditForumTopicQuery: " << to_string(ptr);
td_->updates_manager_->on_get_updates(std::move(ptr), std::move(promise_));
}
void on_error(Status status) final {
if (status.message() == "TOPIC_NOT_MODIFIED" && !td_->auth_manager_->is_bot()) {
return promise_.set_value(Unit());
}
td_->dialog_manager_->on_get_dialog_error(dialog_id_, status, "EditForumTopicQuery");
promise_.set_error(std::move(status));
}
};
class UpdatePinnedForumTopicQuery final : public Td::ResultHandler {
Promise<Unit> promise_;
DialogId dialog_id_;
public:
explicit UpdatePinnedForumTopicQuery(Promise<Unit> &&promise) : promise_(std::move(promise)) {
}
void send(DialogId dialog_id, ForumTopicId forum_topic_id, bool is_pinned) {
dialog_id_ = dialog_id;
auto input_peer = td_->dialog_manager_->get_input_peer(dialog_id, AccessRights::Write);
if (input_peer == nullptr) {
return on_error(Status::Error(400, "Can't access the chat"));
}
send_query(G()->net_query_creator().create(
telegram_api::messages_updatePinnedForumTopic(std::move(input_peer), forum_topic_id.get(), is_pinned),
{{dialog_id}}));
}
void on_result(BufferSlice packet) final {
auto result_ptr = fetch_result<telegram_api::messages_updatePinnedForumTopic>(packet);
if (result_ptr.is_error()) {
return on_error(result_ptr.move_as_error());
}
auto ptr = result_ptr.move_as_ok();
LOG(INFO) << "Receive result for UpdatePinnedForumTopicQuery: " << to_string(ptr);
td_->updates_manager_->on_get_updates(std::move(ptr), std::move(promise_));
}
void on_error(Status status) final {
if (status.message() == "PINNED_TOPIC_NOT_MODIFIED" && !td_->auth_manager_->is_bot()) {
return promise_.set_value(Unit());
}
td_->dialog_manager_->on_get_dialog_error(dialog_id_, status, "UpdatePinnedForumTopicQuery");
promise_.set_error(std::move(status));
}
};
class ReorderPinnedForumTopicsQuery final : public Td::ResultHandler {
Promise<Unit> promise_;
DialogId dialog_id_;
public:
explicit ReorderPinnedForumTopicsQuery(Promise<Unit> &&promise) : promise_(std::move(promise)) {
}
void send(DialogId dialog_id, const vector<ForumTopicId> &forum_topic_ids) {
dialog_id_ = dialog_id;
auto input_peer = td_->dialog_manager_->get_input_peer(dialog_id, AccessRights::Write);
if (input_peer == nullptr) {
return on_error(Status::Error(400, "Can't access the chat"));
}
send_query(G()->net_query_creator().create(
telegram_api::messages_reorderPinnedForumTopics(0, true, std::move(input_peer),
ForumTopicId::get_top_msg_ids(forum_topic_ids)),
{{dialog_id}}));
}
void on_result(BufferSlice packet) final {
auto result_ptr = fetch_result<telegram_api::messages_reorderPinnedForumTopics>(packet);
if (result_ptr.is_error()) {
return on_error(result_ptr.move_as_error());
}
auto ptr = result_ptr.move_as_ok();
LOG(INFO) << "Receive result for ReorderPinnedForumTopicsQuery: " << to_string(ptr);
td_->updates_manager_->on_get_updates(std::move(ptr), std::move(promise_));
}
void on_error(Status status) final {
if (status.message() == "PINNED_TOPICS_NOT_MODIFIED" && !td_->auth_manager_->is_bot()) {
return promise_.set_value(Unit());
}
td_->dialog_manager_->on_get_dialog_error(dialog_id_, status, "ReorderPinnedForumTopicsQuery");
promise_.set_error(std::move(status));
}
};
class GetForumTopicQuery final : public Td::ResultHandler {
Promise<td_api::object_ptr<td_api::forumTopic>> promise_;
DialogId dialog_id_;
ForumTopicId forum_topic_id_;
public:
explicit GetForumTopicQuery(Promise<td_api::object_ptr<td_api::forumTopic>> &&promise)
: promise_(std::move(promise)) {
}
void send(DialogId dialog_id, ForumTopicId forum_topic_id) {
dialog_id_ = dialog_id;
forum_topic_id_ = forum_topic_id;
auto input_peer = td_->dialog_manager_->get_input_peer(dialog_id, AccessRights::Read);
if (input_peer == nullptr) {
return on_error(Status::Error(400, "Can't access the chat"));
}
send_query(G()->net_query_creator().create(
telegram_api::messages_getForumTopicsByID(std::move(input_peer), {forum_topic_id.get()}), {{dialog_id}}));
}
void on_result(BufferSlice packet) final {
auto result_ptr = fetch_result<telegram_api::messages_getForumTopicsByID>(packet);
if (result_ptr.is_error()) {
return on_error(result_ptr.move_as_error());
}
auto ptr = result_ptr.move_as_ok();
LOG(INFO) << "Receive result for GetForumTopicQuery: " << to_string(ptr);
td_->user_manager_->on_get_users(std::move(ptr->users_), "GetForumTopicQuery");
td_->chat_manager_->on_get_chats(std::move(ptr->chats_), "GetForumTopicQuery");
if (ptr->topics_.size() != 1u) {
return promise_.set_value(nullptr);
}
MessagesInfo messages_info;
messages_info.messages = std::move(ptr->messages_);
messages_info.total_count = ptr->count_;
messages_info.is_channel_messages = dialog_id_.get_type() == DialogType::Channel;
td_->messages_manager_->get_channel_difference_if_needed(
dialog_id_, std::move(messages_info),
PromiseCreator::lambda([actor_id = td_->forum_topic_manager_actor_.get(), dialog_id = dialog_id_,
forum_topic_id = forum_topic_id_, topic = std::move(ptr->topics_[0]),
promise = std::move(promise_)](Result<MessagesInfo> &&result) mutable {
if (result.is_error()) {
promise.set_error(result.move_as_error());
} else {
auto info = result.move_as_ok();
send_closure(actor_id, &ForumTopicManager::on_get_forum_topic, dialog_id, forum_topic_id, std::move(info),
std::move(topic), std::move(promise));
}
}),
"GetForumTopicQuery");
}
void on_error(Status status) final {
td_->dialog_manager_->on_get_dialog_error(dialog_id_, status, "GetForumTopicQuery");
promise_.set_error(std::move(status));
}
};
class GetForumTopicsQuery final : public Td::ResultHandler {
Promise<td_api::object_ptr<td_api::forumTopics>> promise_;
DialogId dialog_id_;
public:
explicit GetForumTopicsQuery(Promise<td_api::object_ptr<td_api::forumTopics>> &&promise)
: promise_(std::move(promise)) {
}
void send(DialogId dialog_id, const string &query, int32 offset_date, MessageId offset_message_id,
ForumTopicId offset_forum_topic_id, int32 limit) {
dialog_id_ = dialog_id;
auto input_peer = td_->dialog_manager_->get_input_peer(dialog_id, AccessRights::Read);
if (input_peer == nullptr) {
return on_error(Status::Error(400, "Can't access the chat"));
}
int32 flags = 0;
if (!query.empty()) {
flags |= telegram_api::messages_getForumTopics::Q_MASK;
}
send_query(G()->net_query_creator().create(
telegram_api::messages_getForumTopics(flags, std::move(input_peer), query, offset_date,
offset_message_id.get_server_message_id().get(),
offset_forum_topic_id.get(), limit),
{{dialog_id}}));
}
void on_result(BufferSlice packet) final {
auto result_ptr = fetch_result<telegram_api::messages_getForumTopics>(packet);
if (result_ptr.is_error()) {
return on_error(result_ptr.move_as_error());
}
auto ptr = result_ptr.move_as_ok();
LOG(INFO) << "Receive result for GetForumTopicsQuery: " << to_string(ptr);
td_->user_manager_->on_get_users(std::move(ptr->users_), "GetForumTopicsQuery");
td_->chat_manager_->on_get_chats(std::move(ptr->chats_), "GetForumTopicsQuery");
MessagesInfo messages_info;
messages_info.messages = std::move(ptr->messages_);
messages_info.total_count = ptr->count_;
messages_info.is_channel_messages = dialog_id_.get_type() == DialogType::Channel;
// ignore ptr->pts_
td_->messages_manager_->get_channel_difference_if_needed(
dialog_id_, std::move(messages_info),
PromiseCreator::lambda([actor_id = td_->forum_topic_manager_actor_.get(), dialog_id = dialog_id_,
order_by_creation_date = ptr->order_by_create_date_, topics = std::move(ptr->topics_),
promise = std::move(promise_)](Result<MessagesInfo> &&result) mutable {
if (result.is_error()) {
promise.set_error(result.move_as_error());
} else {
auto info = result.move_as_ok();
send_closure(actor_id, &ForumTopicManager::on_get_forum_topics, dialog_id, order_by_creation_date,
std::move(info), std::move(topics), std::move(promise));
}
}),
"GetForumTopicsQuery");
}
void on_error(Status status) final {
td_->dialog_manager_->on_get_dialog_error(dialog_id_, status, "GetForumTopicsQuery");
promise_.set_error(std::move(status));
}
};
class ReadForumTopicQuery final : public Td::ResultHandler {
DialogId dialog_id_;
public:
void send(DialogId dialog_id, ForumTopicId forum_topic_id, MessageId max_message_id) {
dialog_id_ = dialog_id;
auto input_peer = td_->dialog_manager_->get_input_peer(dialog_id, AccessRights::Read);
if (input_peer == nullptr) {
return on_error(Status::Error(400, "Can't access the chat"));
}
send_query(G()->net_query_creator().create(
telegram_api::messages_readDiscussion(std::move(input_peer), forum_topic_id.get(),
max_message_id.get_server_message_id().get()),
{{dialog_id}}));
}
void on_result(BufferSlice packet) final {
auto result_ptr = fetch_result<telegram_api::messages_readDiscussion>(packet);
if (result_ptr.is_error()) {
return on_error(result_ptr.move_as_error());
}
}
void on_error(Status status) final {
td_->dialog_manager_->on_get_dialog_error(dialog_id_, status, "ReadForumTopicQuery");
}
};
template <class StorerT>
void ForumTopicManager::Topic::store(StorerT &storer) const {
CHECK(info_ != nullptr);
using td::store;
store(MAGIC, storer);
bool has_topic = topic_ != nullptr;
BEGIN_STORE_FLAGS();
STORE_FLAG(has_topic);
END_STORE_FLAGS();
store(info_, storer);
if (has_topic) {
store(topic_, storer);
}
}
template <class ParserT>
void ForumTopicManager::Topic::parse(ParserT &parser) {
CHECK(info_ != nullptr);
using td::parse;
int32 magic;
parse(magic, parser);
if (magic != MAGIC) {
return parser.set_error("Invalid magic");
}
bool has_topic;
BEGIN_PARSE_FLAGS();
PARSE_FLAG(has_topic);
END_PARSE_FLAGS();
parse(info_, parser);
if (has_topic) {
parse(topic_, parser);
}
}
ForumTopicManager::ForumTopicManager(Td *td, ActorShared<> parent) : td_(td), parent_(std::move(parent)) {
}
ForumTopicManager::~ForumTopicManager() {
Scheduler::instance()->destroy_on_scheduler(G()->get_gc_scheduler_id(), dialog_topics_);
}
void ForumTopicManager::tear_down() {
parent_.reset();
}
void ForumTopicManager::create_forum_topic(DialogId dialog_id, string &&title, bool title_missing,
td_api::object_ptr<td_api::forumTopicIcon> &&icon,
Promise<td_api::object_ptr<td_api::forumTopicInfo>> &&promise) {
TRY_STATUS_PROMISE(promise, is_forum(dialog_id, true));
if (dialog_id.get_type() == DialogType::Channel) {
auto channel_id = dialog_id.get_channel_id();
if (!td_->chat_manager_->get_channel_permissions(channel_id).can_create_topics()) {
return promise.set_error(400, "Not enough rights to create a topic");
}
}
auto new_title = clean_name(std::move(title), MAX_FORUM_TOPIC_TITLE_LENGTH);
if (new_title.empty()) {
return promise.set_error(400, "Name must be non-empty");
}
int32 icon_color = -1;
CustomEmojiId icon_custom_emoji_id;
if (icon != nullptr) {
icon_color = icon->color_;
if (icon_color < 0 || icon_color > 0xFFFFFF) {
return promise.set_error(400, "Invalid icon color specified");
}
icon_custom_emoji_id = CustomEmojiId(icon->custom_emoji_id_);
}
DialogId as_dialog_id = td_->messages_manager_->get_dialog_default_send_message_as_dialog_id(dialog_id);
td_->create_handler<CreateForumTopicQuery>(std::move(promise))
->send(dialog_id, new_title, title_missing, icon_color, icon_custom_emoji_id, as_dialog_id);
}
void ForumTopicManager::on_forum_topic_created(DialogId dialog_id, unique_ptr<ForumTopicInfo> &&forum_topic_info,
Promise<td_api::object_ptr<td_api::forumTopicInfo>> &&promise) {
TRY_STATUS_PROMISE(promise, G()->close_status());
CHECK(forum_topic_info != nullptr);
ForumTopicId forum_topic_id = forum_topic_info->get_forum_topic_id();
auto topic = add_topic(dialog_id, forum_topic_id);
if (topic == nullptr) {
return promise.set_value(forum_topic_info->get_forum_topic_info_object(td_));
}
if (topic->info_ == nullptr) {
set_topic_info(dialog_id, topic, std::move(forum_topic_info));
}
save_topic_to_database(dialog_id, topic);
promise.set_value(topic->info_->get_forum_topic_info_object(td_));
}
void ForumTopicManager::edit_forum_topic(DialogId dialog_id, ForumTopicId forum_topic_id, string &&title,
bool edit_icon_custom_emoji, CustomEmojiId icon_custom_emoji_id,
Promise<Unit> &&promise) {
TRY_STATUS_PROMISE(promise, is_forum(dialog_id, true));
TRY_STATUS_PROMISE(promise, can_be_forum_topic_id(forum_topic_id));
if (dialog_id.get_type() == DialogType::Channel) {
auto channel_id = dialog_id.get_channel_id();
if (!td_->chat_manager_->get_channel_permissions(channel_id).can_edit_topics()) {
auto topic_info = get_topic_info(dialog_id, forum_topic_id);
if (topic_info != nullptr && !topic_info->is_outgoing()) {
return promise.set_error(400, "Not enough rights to edit the topic");
}
}
}
bool edit_title = !title.empty();
auto new_title = clean_name(std::move(title), MAX_FORUM_TOPIC_TITLE_LENGTH);
if (edit_title && new_title.empty()) {
return promise.set_error(400, "Name must be non-empty");
}
if (!edit_title && !edit_icon_custom_emoji) {
return promise.set_value(Unit());
}
td_->create_handler<EditForumTopicQuery>(std::move(promise))
->send(dialog_id, forum_topic_id, edit_title, new_title, edit_icon_custom_emoji, icon_custom_emoji_id);
}
void ForumTopicManager::read_forum_topic_messages(DialogId dialog_id, ForumTopicId forum_topic_id,
MessageId last_read_inbox_message_id) {
CHECK(!td_->auth_manager_->is_bot());
auto topic = get_topic(dialog_id, forum_topic_id);
if (topic == nullptr || topic->topic_ == nullptr) {
return;
}
bool need_update = false;
if (topic->topic_->update_last_read_inbox_message_id(last_read_inbox_message_id, -1)) {
need_update = true;
auto max_message_id = last_read_inbox_message_id.get_prev_server_message_id();
LOG(INFO) << "Send read topic history request in topic of " << forum_topic_id << " in " << dialog_id << " up to "
<< max_message_id;
td_->create_handler<ReadForumTopicQuery>()->send(dialog_id, forum_topic_id, max_message_id);
}
if (need_update) {
on_forum_topic_changed(dialog_id, topic);
}
}
void ForumTopicManager::on_update_forum_topic_unread(DialogId dialog_id, ForumTopicId forum_topic_id,
MessageId last_message_id, MessageId last_read_inbox_message_id,
MessageId last_read_outbox_message_id, int32 unread_count) {
if (td_->auth_manager_->is_bot()) {
return;
}
auto topic = get_topic(dialog_id, forum_topic_id);
if (topic == nullptr || topic->topic_ == nullptr) {
return;
}
bool need_update = false;
if (topic->topic_->update_last_read_outbox_message_id(last_read_outbox_message_id)) {
need_update = true;
}
if (topic->topic_->update_last_read_inbox_message_id(last_read_inbox_message_id, unread_count)) {
need_update = true;
}
if (need_update) {
on_forum_topic_changed(dialog_id, topic);
}
}
DialogNotificationSettings *ForumTopicManager::get_forum_topic_notification_settings(DialogId dialog_id,
ForumTopicId forum_topic_id) {
auto topic = get_topic(dialog_id, forum_topic_id);
if (topic == nullptr || topic->topic_ == nullptr) {
return nullptr;
}
return topic->topic_->get_notification_settings();
}
const DialogNotificationSettings *ForumTopicManager::get_forum_topic_notification_settings(
DialogId dialog_id, ForumTopicId forum_topic_id) const {
auto topic = get_topic(dialog_id, forum_topic_id);
if (topic == nullptr || topic->topic_ == nullptr) {
return nullptr;
}
return topic->topic_->get_notification_settings();
}
void ForumTopicManager::on_update_forum_topic_notify_settings(
DialogId dialog_id, ForumTopicId forum_topic_id,
tl_object_ptr<telegram_api::peerNotifySettings> &&peer_notify_settings, const char *source) {
if (td_->auth_manager_->is_bot()) {
return;
}
VLOG(notifications) << "Receive notification settings for topic of " << forum_topic_id << " in " << dialog_id
<< " from " << source << ": " << to_string(peer_notify_settings);
DialogNotificationSettings *current_settings = get_forum_topic_notification_settings(dialog_id, forum_topic_id);
if (current_settings == nullptr) {
return;
}
auto notification_settings = get_dialog_notification_settings(std::move(peer_notify_settings), current_settings);
if (!notification_settings.is_synchronized) {
return;
}
update_forum_topic_notification_settings(dialog_id, forum_topic_id, current_settings,
std::move(notification_settings));
}
void ForumTopicManager::on_update_forum_topic_draft_message(DialogId dialog_id, ForumTopicId forum_topic_id,
unique_ptr<DraftMessage> &&draft_message) {
if (td_->auth_manager_->is_bot()) {
return;
}
auto topic = get_topic(dialog_id, forum_topic_id);
if (topic == nullptr || topic->topic_ == nullptr) {
LOG(DEBUG) << "Ignore update about unknown " << forum_topic_id << " in " << dialog_id;
return;
}
auto old_file_ids = get_topic_file_ids(topic);
if (topic->topic_->set_draft_message(std::move(draft_message), true)) {
on_forum_topic_changed(dialog_id, topic);
td_->draft_message_manager_->change_draft_message_files(dialog_id, MessageTopic::forum(dialog_id, forum_topic_id),
old_file_ids, get_topic_file_ids(topic), true);
}
}
void ForumTopicManager::clear_forum_topic_draft_by_sent_message(DialogId dialog_id, ForumTopicId forum_topic_id,
bool message_clear_draft,
MessageContentType message_content_type) {
if (td_->auth_manager_->is_bot()) {
return;
}
LOG(INFO) << "Clear draft in " << forum_topic_id << " of " << dialog_id << " by sent message";
auto topic = get_topic(dialog_id, forum_topic_id);
if (topic == nullptr || topic->topic_ == nullptr) {
return;
}
if (!message_clear_draft) {
const auto *draft_message = topic->topic_->get_draft_message().get();
if (draft_message == nullptr || !draft_message->need_clear_local(message_content_type)) {
return;
}
}
auto old_file_ids = get_topic_file_ids(topic);
if (topic->topic_->set_draft_message(nullptr, false)) {
on_forum_topic_changed(dialog_id, topic);
td_->draft_message_manager_->change_draft_message_files(dialog_id, MessageTopic::forum(dialog_id, forum_topic_id),
old_file_ids, get_topic_file_ids(topic), false);
}
}
void ForumTopicManager::on_update_forum_topic_is_pinned(DialogId dialog_id, ForumTopicId forum_topic_id,
bool is_pinned) {
if (!td_->dialog_manager_->have_dialog_force(dialog_id, "on_update_forum_topic_is_pinned")) {
return;
}
if (!can_be_forum(dialog_id)) {
LOG(ERROR) << "Receive pinned topics in " << dialog_id;
return;
}
if (td_->auth_manager_->is_bot()) {
return;
}
auto topic = get_topic(dialog_id, forum_topic_id);
if (topic == nullptr || topic->topic_ == nullptr) {
return;
}
if (topic->topic_->set_is_pinned(is_pinned)) {
on_forum_topic_changed(dialog_id, topic);
}
}
void ForumTopicManager::on_update_pinned_forum_topics(DialogId dialog_id, vector<ForumTopicId> forum_topic_ids) {
if (!td_->dialog_manager_->have_dialog_force(dialog_id, "on_update_pinned_forum_topics")) {
return;
}
if (!can_be_forum(dialog_id)) {
LOG(ERROR) << "Receive pinned topics in " << dialog_id;
return;
}
if (td_->auth_manager_->is_bot()) {
return;
}
auto dialog_topics = get_dialog_topics(dialog_id);
if (dialog_topics == nullptr) {
return;
}
dialog_topics->topics_.foreach([&](const ForumTopicId &forum_topic_id, unique_ptr<Topic> &topic) {
if (topic->topic_ == nullptr) {
return;
}
if (topic->topic_->set_is_pinned(contains(forum_topic_ids, forum_topic_id))) {
on_forum_topic_changed(dialog_id, topic.get());
}
});
}
Status ForumTopicManager::set_forum_topic_notification_settings(
DialogId dialog_id, ForumTopicId forum_topic_id,
td_api::object_ptr<td_api::chatNotificationSettings> &¬ification_settings) {
CHECK(!td_->auth_manager_->is_bot());
TRY_STATUS(is_forum(dialog_id, true));
TRY_STATUS(can_be_forum_topic_id(forum_topic_id));
auto current_settings = get_forum_topic_notification_settings(dialog_id, forum_topic_id);
if (current_settings == nullptr) {
return Status::Error(400, "Unknown forum topic identifier specified");
}
TRY_RESULT(new_settings, get_dialog_notification_settings(std::move(notification_settings), current_settings));
if (update_forum_topic_notification_settings(dialog_id, forum_topic_id, current_settings, std::move(new_settings))) {
// TODO log event
td_->notification_settings_manager_->update_dialog_notify_settings(dialog_id, forum_topic_id, *current_settings,
Promise<Unit>());
}
return Status::OK();
}
bool ForumTopicManager::update_forum_topic_notification_settings(DialogId dialog_id, ForumTopicId forum_topic_id,
DialogNotificationSettings *current_settings,
DialogNotificationSettings &&new_settings) {
if (td_->auth_manager_->is_bot()) {
// just in case
return false;
}
auto need_update = need_update_dialog_notification_settings(current_settings, new_settings);
if (need_update.are_changed) {
// TODO update unmute timeouts, remove notifications
*current_settings = std::move(new_settings);
auto topic = get_topic(dialog_id, forum_topic_id);
on_forum_topic_changed(dialog_id, topic);
}
return need_update.need_update_server;
}
Status ForumTopicManager::set_forum_topic_draft_message(DialogId dialog_id, ForumTopicId forum_topic_id,
unique_ptr<DraftMessage> &&draft_message) {
TRY_STATUS(is_forum(dialog_id, true));
TRY_STATUS(can_be_forum_topic_id(forum_topic_id));
auto topic = get_topic(dialog_id, forum_topic_id);
if (topic == nullptr || topic->topic_ == nullptr) {
td_->draft_message_manager_->save_draft_message(dialog_id, MessageTopic::forum(dialog_id, forum_topic_id),
std::move(draft_message), Promise<Unit>());
} else if (topic->topic_->set_draft_message(std::move(draft_message), false)) {
td_->draft_message_manager_->save_draft_message(dialog_id, MessageTopic::forum(dialog_id, forum_topic_id),
topic->topic_->get_draft_message(), Promise<Unit>());
on_forum_topic_changed(dialog_id, topic);
}
return Status::OK();
}
void ForumTopicManager::get_forum_topic(DialogId dialog_id, ForumTopicId forum_topic_id,
Promise<td_api::object_ptr<td_api::forumTopic>> &&promise) {
TRY_STATUS_PROMISE(promise, is_forum(dialog_id, true));
TRY_STATUS_PROMISE(promise, can_be_forum_topic_id(forum_topic_id));
if (td_->auth_manager_->is_bot()) {
auto topic = get_topic(dialog_id, forum_topic_id);
if (topic != nullptr && topic->topic_ != nullptr && topic->receive_date_ > G()->unix_time() - 60) {
CHECK(topic->info_ != nullptr);
auto forum_topic = topic->topic_->get_forum_topic_object(td_, dialog_id, *topic->info_);
if (forum_topic != nullptr) {
return promise.set_value(std::move(forum_topic));
}
}
}
td_->create_handler<GetForumTopicQuery>(std::move(promise))->send(dialog_id, forum_topic_id);
}
void ForumTopicManager::reload_forum_topic(DialogId dialog_id, ForumTopicId forum_topic_id, Promise<Unit> &&promise) {
TRY_STATUS_PROMISE(promise, is_forum(dialog_id, true));
TRY_STATUS_PROMISE(promise, can_be_forum_topic_id(forum_topic_id));
auto query_promise = PromiseCreator::lambda(
[promise = std::move(promise)](Result<td_api::object_ptr<td_api::forumTopic>> r_forum_topic) mutable {
if (r_forum_topic.is_error()) {
promise.set_error(r_forum_topic.move_as_error());
} else {
promise.set_value(Unit());
}
});
td_->create_handler<GetForumTopicQuery>(std::move(query_promise))->send(dialog_id, forum_topic_id);
}
void ForumTopicManager::on_get_forum_topic(DialogId dialog_id, ForumTopicId expected_forum_topic_id,
MessagesInfo &&info,
telegram_api::object_ptr<telegram_api::ForumTopic> &&topic,
Promise<td_api::object_ptr<td_api::forumTopic>> &&promise) {
TRY_STATUS_PROMISE(promise, is_forum(dialog_id, true));
td_->messages_manager_->on_get_messages(dialog_id, std::move(info.messages), false, Promise<Unit>(),
"on_get_forum_topic");
auto forum_topic_id = on_get_forum_topic_impl(dialog_id, std::move(topic));
if (!forum_topic_id.is_valid()) {
return promise.set_value(nullptr);
}
if (forum_topic_id != expected_forum_topic_id) {
return promise.set_error(500, "Wrong forum topic received");
}
promise.set_value(get_forum_topic_object(dialog_id, forum_topic_id));
}
void ForumTopicManager::get_forum_topic_link(DialogId dialog_id, ForumTopicId forum_topic_id,
Promise<td_api::object_ptr<td_api::messageLink>> &&promise) {
TRY_STATUS_PROMISE(promise, is_forum(dialog_id));
TRY_STATUS_PROMISE(promise, can_be_forum_topic_id(forum_topic_id));
auto channel_id = dialog_id.get_channel_id();
SliceBuilder sb;
sb << LinkManager::get_t_me_url();
bool is_public = false;
auto dialog_username = td_->chat_manager_->get_channel_first_username(channel_id);
if (!dialog_username.empty()) {
sb << dialog_username;
is_public = true;
} else {
sb << "c/" << channel_id.get();
}
sb << '/' << forum_topic_id.get();
promise.set_value(td_api::make_object<td_api::messageLink>(sb.as_cslice().str(), is_public));
}
void ForumTopicManager::get_forum_topics(DialogId dialog_id, string query, int32 offset_date,
MessageId offset_message_id, ForumTopicId offset_forum_topic_id, int32 limit,
Promise<td_api::object_ptr<td_api::forumTopics>> promise) {
TRY_STATUS_PROMISE(promise, is_forum(dialog_id, true));
if (offset_date < 0) {
return promise.set_error(400, "Invalid offset date specified");
}
if (offset_message_id != MessageId() && !offset_message_id.is_server()) {
return promise.set_error(400, "Invalid offset message identifier specified");
}
if (offset_forum_topic_id != ForumTopicId()) {
TRY_STATUS_PROMISE(promise, can_be_forum_topic_id(offset_forum_topic_id));
}
if (limit <= 0) {
return promise.set_error(400, "Invalid limit specified");
}
td_->create_handler<GetForumTopicsQuery>(std::move(promise))
->send(dialog_id, query, offset_date, offset_message_id, offset_forum_topic_id, limit);
}
void ForumTopicManager::on_get_forum_topics(DialogId dialog_id, bool order_by_creation_date, MessagesInfo &&info,
vector<telegram_api::object_ptr<telegram_api::ForumTopic>> &&topics,
Promise<td_api::object_ptr<td_api::forumTopics>> &&promise) {
TRY_STATUS_PROMISE(promise, is_forum(dialog_id, true));
td_->messages_manager_->on_get_messages(dialog_id, std::move(info.messages), false, Promise<Unit>(),
"on_get_forum_topics");
vector<td_api::object_ptr<td_api::forumTopic>> forum_topics;
int32 next_offset_date = 0;
MessageId next_offset_message_id;
ForumTopicId next_offset_forum_topic_id;
for (auto &topic : topics) {
auto forum_topic_id = on_get_forum_topic_impl(dialog_id, std::move(topic));
if (!forum_topic_id.is_valid()) {
continue;
}
auto forum_topic_object = get_forum_topic_object(dialog_id, forum_topic_id);
CHECK(forum_topic_object != nullptr);
if (order_by_creation_date || forum_topic_object->last_message_ == nullptr) {
next_offset_date = forum_topic_object->info_->creation_date_;
} else {
next_offset_date = forum_topic_object->last_message_->date_;
}
next_offset_message_id =
forum_topic_object->last_message_ != nullptr ? MessageId(forum_topic_object->last_message_->id_) : MessageId();
next_offset_forum_topic_id = forum_topic_id;
forum_topics.push_back(std::move(forum_topic_object));
}
promise.set_value(td_api::make_object<td_api::forumTopics>(info.total_count, std::move(forum_topics),
next_offset_date, next_offset_message_id.get(),
next_offset_forum_topic_id.get()));
}
void ForumTopicManager::toggle_forum_topic_is_closed(DialogId dialog_id, ForumTopicId forum_topic_id, bool is_closed,
Promise<Unit> &&promise) {
TRY_STATUS_PROMISE(promise, is_forum(dialog_id));
TRY_STATUS_PROMISE(promise, can_be_forum_topic_id(forum_topic_id));
auto channel_id = dialog_id.get_channel_id();
if (!td_->chat_manager_->get_channel_permissions(channel_id).can_edit_topics()) {
auto topic_info = get_topic_info(dialog_id, forum_topic_id);
if (topic_info != nullptr && !topic_info->is_outgoing()) {
return promise.set_error(400, "Not enough rights to close or open the topic");
}
}
td_->create_handler<EditForumTopicQuery>(std::move(promise))->send(channel_id, forum_topic_id, is_closed);
}
void ForumTopicManager::toggle_forum_topic_is_hidden(DialogId dialog_id, bool is_hidden, Promise<Unit> &&promise) {
TRY_STATUS_PROMISE(promise, is_forum(dialog_id));
auto channel_id = dialog_id.get_channel_id();
if (!td_->chat_manager_->get_channel_permissions(channel_id).can_edit_topics()) {
return promise.set_error(400, "Not enough rights to close or open the topic");
}
td_->create_handler<EditForumTopicQuery>(std::move(promise))->send(channel_id, is_hidden);
}
void ForumTopicManager::toggle_forum_topic_is_pinned(DialogId dialog_id, ForumTopicId forum_topic_id, bool is_pinned,
Promise<Unit> &&promise) {
TRY_STATUS_PROMISE(promise, is_forum(dialog_id, true));
TRY_STATUS_PROMISE(promise, can_be_forum_topic_id(forum_topic_id));
if (dialog_id.get_type() == DialogType::Channel) {
auto channel_id = dialog_id.get_channel_id();
if (!td_->chat_manager_->get_channel_permissions(channel_id).can_pin_topics()) {
return promise.set_error(400, "Not enough rights to pin or unpin the topic");
}
}
td_->create_handler<UpdatePinnedForumTopicQuery>(std::move(promise))->send(dialog_id, forum_topic_id, is_pinned);
}
void ForumTopicManager::set_pinned_forum_topics(DialogId dialog_id, vector<ForumTopicId> forum_topic_ids,
Promise<Unit> &&promise) {
TRY_STATUS_PROMISE(promise, is_forum(dialog_id, true));
for (auto forum_topic_id : forum_topic_ids) {
TRY_STATUS_PROMISE(promise, can_be_forum_topic_id(forum_topic_id));
}
if (dialog_id.get_type() == DialogType::Channel) {
auto channel_id = dialog_id.get_channel_id();
if (!td_->chat_manager_->get_channel_permissions(channel_id).can_pin_topics()) {
return promise.set_error(400, "Not enough rights to reorder forum topics");
}
}
td_->create_handler<ReorderPinnedForumTopicsQuery>(std::move(promise))->send(dialog_id, forum_topic_ids);
}
void ForumTopicManager::delete_forum_topic(DialogId dialog_id, ForumTopicId forum_topic_id, Promise<Unit> &&promise) {
TRY_STATUS_PROMISE(promise, is_forum(dialog_id, true));
TRY_STATUS_PROMISE(promise, can_be_forum_topic_id(forum_topic_id));
if (dialog_id.get_type() == DialogType::Channel) {
auto channel_id = dialog_id.get_channel_id();
if (!td_->chat_manager_->get_channel_permissions(channel_id).can_delete_messages()) {
auto topic_info = get_topic_info(dialog_id, forum_topic_id);
if (topic_info != nullptr && !topic_info->is_outgoing()) {
return promise.set_error(400, "Not enough rights to delete the topic");
}
}
}
auto delete_promise = PromiseCreator::lambda([actor_id = actor_id(this), dialog_id, forum_topic_id,
promise = std::move(promise)](Result<Unit> result) mutable {
if (result.is_error()) {
return promise.set_error(result.move_as_error());
}
send_closure(actor_id, &ForumTopicManager::on_delete_forum_topic, dialog_id, forum_topic_id, std::move(promise));
});
td_->message_query_manager_->delete_topic_history(dialog_id, forum_topic_id, std::move(delete_promise));
}
void ForumTopicManager::on_delete_forum_topic(DialogId dialog_id, ForumTopicId forum_topic_id,
Promise<Unit> &&promise) {
TRY_STATUS_PROMISE(promise, G()->close_status());
auto *dialog_topics = dialog_topics_.get_pointer(dialog_id);
if (dialog_topics != nullptr) {
CHECK(forum_topic_id.is_valid());
dialog_topics->topics_.erase(forum_topic_id);
dialog_topics->deleted_topic_ids_.insert(forum_topic_id);
}
delete_topic_from_database(dialog_id, forum_topic_id, std::move(promise));
}
void ForumTopicManager::delete_all_dialog_topics(DialogId dialog_id) {
dialog_topics_.erase(dialog_id);
auto message_thread_db = G()->td_db()->get_message_thread_db_async();
if (message_thread_db == nullptr) {
return;
}
LOG(INFO) << "Delete all topics in " << dialog_id << " from database";
message_thread_db->delete_all_dialog_message_threads(dialog_id, Auto());
}
void ForumTopicManager::on_forum_topic_edited(DialogId dialog_id, ForumTopicId forum_topic_id,
const ForumTopicEditedData &edited_data) {
auto topic = get_topic(dialog_id, forum_topic_id);
if (topic == nullptr || topic->info_ == nullptr) {
return;
}
if (topic->info_->apply_edited_data(edited_data)) {
send_update_forum_topic_info(topic->info_.get());
topic->need_save_to_database_ = true;
}
save_topic_to_database(dialog_id, topic);
}
bool ForumTopicManager::on_get_forum_topic_error(DialogId dialog_id, ForumTopicId forum_topic_id, const Status &status,
const char *source) {
return td_->dialog_manager_->on_get_dialog_error(dialog_id, status, source);
}
void ForumTopicManager::on_get_forum_topic_info(DialogId dialog_id, const ForumTopicInfo &topic_info,
const char *source) {
if (!can_be_forum(dialog_id)) {
LOG(ERROR) << "Receive forum topics in " << dialog_id << " from " << source;
return;
}
auto *dialog_topics = add_dialog_topics(dialog_id);
CHECK(dialog_topics != nullptr);
auto forum_topic_info = td::make_unique<ForumTopicInfo>(topic_info);
ForumTopicId forum_topic_id = forum_topic_info->get_forum_topic_id();
CHECK(can_be_forum_topic_id(forum_topic_id).is_ok());
auto topic = add_topic(dialog_topics, forum_topic_id);
if (topic == nullptr) {
return;
}
set_topic_info(dialog_id, topic, std::move(forum_topic_info));
save_topic_to_database(dialog_id, topic);
}
void ForumTopicManager::on_get_forum_topic_infos(DialogId dialog_id,
vector<tl_object_ptr<telegram_api::ForumTopic>> &&forum_topics,
const char *source) {
if (forum_topics.empty()) {
return;
}
if (dialog_id == DialogId()) {
for (auto &forum_topic : forum_topics) {
auto forum_topic_info = td::make_unique<ForumTopicInfo>(td_, forum_topic, DialogId());
ForumTopicId forum_topic_id = forum_topic_info->get_forum_topic_id();
if (can_be_forum_topic_id(forum_topic_id).is_error()) {
continue;
}
dialog_id = forum_topic_info->get_dialog_id();
if (!can_be_forum(dialog_id)) {
LOG(ERROR) << "Receive forum topics in " << dialog_id << " from " << source;
return;
}
auto *dialog_topics = add_dialog_topics(dialog_id);
CHECK(dialog_topics != nullptr);
auto topic = add_topic(dialog_topics, forum_topic_id);
if (topic != nullptr) {
set_topic_info(dialog_id, topic, std::move(forum_topic_info));
save_topic_to_database(dialog_id, topic);
}
}
return;
}
if (!can_be_forum(dialog_id)) {
LOG(ERROR) << "Receive forum topics in " << dialog_id << " from " << source;
return;
}
auto *dialog_topics = add_dialog_topics(dialog_id);
CHECK(dialog_topics != nullptr);
for (auto &forum_topic : forum_topics) {
auto forum_topic_info = td::make_unique<ForumTopicInfo>(td_, forum_topic, dialog_id);
ForumTopicId forum_topic_id = forum_topic_info->get_forum_topic_id();
if (can_be_forum_topic_id(forum_topic_id).is_error()) {
continue;
}
auto topic = add_topic(dialog_topics, forum_topic_id);
if (topic != nullptr) {
set_topic_info(dialog_id, topic, std::move(forum_topic_info));
save_topic_to_database(dialog_id, topic);
}
}
}
ForumTopicId ForumTopicManager::on_get_forum_topic_impl(DialogId dialog_id,
tl_object_ptr<telegram_api::ForumTopic> &&forum_topic) {
CHECK(forum_topic != nullptr);
switch (forum_topic->get_id()) {
case telegram_api::forumTopicDeleted::ID: {
auto forum_topic_id = ForumTopicId(static_cast<const telegram_api::forumTopicDeleted *>(forum_topic.get())->id_);
if (!forum_topic_id.is_valid()) {
LOG(ERROR) << "Receive " << to_string(forum_topic);
return ForumTopicId();
}
on_delete_forum_topic(dialog_id, forum_topic_id, Promise<Unit>());
return ForumTopicId();
}
case telegram_api::forumTopic::ID: {
auto forum_topic_info = td::make_unique<ForumTopicInfo>(td_, forum_topic, dialog_id);
auto forum_topic_id = forum_topic_info->get_forum_topic_id();
Topic *topic = add_topic(dialog_id, forum_topic_id);
if (topic == nullptr) {
return ForumTopicId();
}
auto current_notification_settings =
topic->topic_ == nullptr ? nullptr : topic->topic_->get_notification_settings();
auto forum_topic_full = td::make_unique<ForumTopic>(td_, std::move(forum_topic), current_notification_settings);
if (forum_topic_full->is_short() && !td_->auth_manager_->is_bot()) {
LOG(ERROR) << "Receive short forum topic";
return ForumTopicId();
}
if (topic->topic_ == nullptr || true) {
auto old_file_ids = get_topic_file_ids(topic);
topic->topic_ = std::move(forum_topic_full);
topic->need_save_to_database_ = true; // TODO temporary
td_->draft_message_manager_->change_draft_message_files(
dialog_id, MessageTopic::forum(dialog_id, forum_topic_id), old_file_ids, get_topic_file_ids(topic), true);
}
topic->receive_date_ = G()->unix_time();
set_topic_info(dialog_id, topic, std::move(forum_topic_info));
send_update_forum_topic(dialog_id, topic);
save_topic_to_database(dialog_id, topic);
return forum_topic_id;
}
default:
UNREACHABLE();
return ForumTopicId();
}
}
vector<FileId> ForumTopicManager::get_topic_file_ids(const Topic *topic) const {
if (topic == nullptr || topic->topic_ == nullptr) {
return {};
}
return get_draft_message_file_ids(td_, topic->topic_->get_draft_message());
}
int32 ForumTopicManager::get_forum_topic_id_object(DialogId dialog_id, ForumTopicId forum_topic_id) {
if (forum_topic_id == ForumTopicId()) {
return 0;
}
// TODO load topic from database or get from the server
return forum_topic_id.get();
}
td_api::object_ptr<td_api::forumTopic> ForumTopicManager::get_forum_topic_object(DialogId dialog_id,
ForumTopicId forum_topic_id) const {
auto topic = get_topic(dialog_id, forum_topic_id);
if (topic == nullptr || topic->topic_ == nullptr) {
return nullptr;
}
CHECK(topic->info_ != nullptr);
return topic->topic_->get_forum_topic_object(td_, dialog_id, *topic->info_);
}
Status ForumTopicManager::is_forum(DialogId dialog_id, bool allow_bots) {
if (!td_->dialog_manager_->have_dialog_force(dialog_id, "ForumTopicManager::is_forum")) {
return Status::Error(400, "Chat not found");
}
switch (dialog_id.get_type()) {
case DialogType::User: {
auto bot_user_id = td_->auth_manager_->is_bot() ? td_->user_manager_->get_my_id() : dialog_id.get_user_id();
if (td_->user_manager_->is_user_forum_bot(bot_user_id)) {
if (!allow_bots) {
return Status::Error(400, "The chat is not a supergroup forum");
}
return Status::OK();
}
break;
}
case DialogType::Channel:
if (td_->chat_manager_->is_forum_channel(dialog_id.get_channel_id())) {
return Status::OK();
}
break;
case DialogType::Chat:
case DialogType::SecretChat:
case DialogType::None:
default:
break;
}
return Status::Error(400, "The chat is not a forum");
}
bool ForumTopicManager::can_be_forum(DialogId dialog_id) const {
switch (dialog_id.get_type()) {
case DialogType::User:
return td_->auth_manager_->is_bot() || td_->user_manager_->is_user_bot(dialog_id.get_user_id());
case DialogType::Channel:
return !td_->chat_manager_->is_broadcast_channel(dialog_id.get_channel_id()) &&
!td_->chat_manager_->is_monoforum_channel(dialog_id.get_channel_id());
case DialogType::Chat:
case DialogType::SecretChat:
case DialogType::None:
default:
break;
}
return false;
}
Status ForumTopicManager::can_be_forum_topic_id(ForumTopicId forum_topic_id) {
if (!forum_topic_id.is_valid()) {
return Status::Error(400, "Invalid forum topic identifier specified");
}
return Status::OK();
}
bool ForumTopicManager::can_send_message_to_forum_topic(DialogId dialog_id, ForumTopicId forum_topic_id) const {
auto *topic_info = get_topic_info(dialog_id, forum_topic_id);
if (topic_info == nullptr) {
return true; // allow sending to unknown topic
}
if (topic_info->is_closed() && !topic_info->is_outgoing() && dialog_id.get_type() == DialogType::Channel &&
!td_->chat_manager_->get_channel_status(dialog_id.get_channel_id()).can_edit_topics()) {
return false; // don't allow sending to closed topic
}
return true;
}
ForumTopicManager::DialogTopics *ForumTopicManager::add_dialog_topics(DialogId dialog_id) {
auto *dialog_topics = dialog_topics_.get_pointer(dialog_id);
if (dialog_topics == nullptr) {
auto new_dialog_topics = make_unique<DialogTopics>();
dialog_topics = new_dialog_topics.get();
dialog_topics_.set(dialog_id, std::move(new_dialog_topics));
}
return dialog_topics;
}
ForumTopicManager::DialogTopics *ForumTopicManager::get_dialog_topics(DialogId dialog_id) {
return dialog_topics_.get_pointer(dialog_id);
}
ForumTopicManager::Topic *ForumTopicManager::add_topic(DialogTopics *dialog_topics, ForumTopicId forum_topic_id) {
auto topic = dialog_topics->topics_.get_pointer(forum_topic_id);
if (topic == nullptr) {
if (dialog_topics->deleted_topic_ids_.count(forum_topic_id) > 0) {
return nullptr;
}
auto new_topic = make_unique<Topic>();
topic = new_topic.get();
CHECK(forum_topic_id.is_valid());
dialog_topics->topics_.set(forum_topic_id, std::move(new_topic));
}
return topic;
}
ForumTopicManager::Topic *ForumTopicManager::get_topic(DialogTopics *dialog_topics, ForumTopicId forum_topic_id) {
return dialog_topics->topics_.get_pointer(forum_topic_id);
}
ForumTopicManager::Topic *ForumTopicManager::add_topic(DialogId dialog_id, ForumTopicId forum_topic_id) {
return add_topic(add_dialog_topics(dialog_id), forum_topic_id);
}
ForumTopicManager::Topic *ForumTopicManager::get_topic(DialogId dialog_id, ForumTopicId forum_topic_id) {
auto *dialog_topics = dialog_topics_.get_pointer(dialog_id);
if (dialog_topics == nullptr) {
return nullptr;
}
return dialog_topics->topics_.get_pointer(forum_topic_id);
}
const ForumTopicManager::Topic *ForumTopicManager::get_topic(DialogId dialog_id, ForumTopicId forum_topic_id) const {
auto *dialog_topics = dialog_topics_.get_pointer(dialog_id);
if (dialog_topics == nullptr) {
return nullptr;
}
return dialog_topics->topics_.get_pointer(forum_topic_id);
}
ForumTopicInfo *ForumTopicManager::get_topic_info(DialogId dialog_id, ForumTopicId forum_topic_id) {
auto *topic = get_topic(dialog_id, forum_topic_id);
if (topic == nullptr) {
return nullptr;
}
return topic->info_.get();
}
const ForumTopicInfo *ForumTopicManager::get_topic_info(DialogId dialog_id, ForumTopicId forum_topic_id) const {
auto *topic = get_topic(dialog_id, forum_topic_id);
if (topic == nullptr) {
return nullptr;
}
return topic->info_.get();
}
void ForumTopicManager::set_topic_info(DialogId dialog_id, Topic *topic, unique_ptr<ForumTopicInfo> forum_topic_info) {
if (topic->info_ == nullptr || *topic->info_ != *forum_topic_info) {
topic->info_ = std::move(forum_topic_info);
send_update_forum_topic_info(topic->info_.get());
topic->need_save_to_database_ = true;
}
}
td_api::object_ptr<td_api::updateForumTopicInfo> ForumTopicManager::get_update_forum_topic_info_object(
const ForumTopicInfo *topic_info) const {
return td_api::make_object<td_api::updateForumTopicInfo>(topic_info->get_forum_topic_info_object(td_));
}
void ForumTopicManager::send_update_forum_topic_info(const ForumTopicInfo *topic_info) const {
if (td_->auth_manager_->is_bot()) {
return;
}
send_closure(G()->td(), &Td::send_update, get_update_forum_topic_info_object(topic_info));
}
td_api::object_ptr<td_api::updateForumTopic> ForumTopicManager::get_update_forum_topic_object(
DialogId dialog_id, const Topic *topic) const {
CHECK(topic != nullptr);
return topic->topic_->get_update_forum_topic_object(td_, dialog_id, topic->info_->get_forum_topic_id());
}
void ForumTopicManager::send_update_forum_topic(DialogId dialog_id, const Topic *topic) const {
if (td_->auth_manager_->is_bot()) {
return;
}
send_closure(G()->td(), &Td::send_update, get_update_forum_topic_object(dialog_id, topic));
}
void ForumTopicManager::on_forum_topic_changed(DialogId dialog_id, Topic *topic) {
CHECK(topic != nullptr);
send_update_forum_topic(dialog_id, topic);
topic->need_save_to_database_ = true;
save_topic_to_database(dialog_id, topic);
}
void ForumTopicManager::save_topic_to_database(DialogId dialog_id, const Topic *topic) {
CHECK(topic != nullptr);
if (topic->info_ == nullptr || !topic->need_save_to_database_) {
return;
}
topic->need_save_to_database_ = false;
auto message_thread_db = G()->td_db()->get_message_thread_db_async();
if (message_thread_db == nullptr) {
return;
}
auto forum_topic_id = topic->info_->get_forum_topic_id();
LOG(INFO) << "Save topic of " << forum_topic_id << " in " << dialog_id << " to database";
message_thread_db->add_message_thread(dialog_id, forum_topic_id, 0, log_event_store(*topic), Auto());
}
void ForumTopicManager::delete_topic_from_database(DialogId dialog_id, ForumTopicId forum_topic_id,
Promise<Unit> &&promise) {
auto message_thread_db = G()->td_db()->get_message_thread_db_async();
if (message_thread_db == nullptr) {
return promise.set_value(Unit());
}
LOG(INFO) << "Delete topic of " << forum_topic_id << " in " << dialog_id << " from database";
message_thread_db->delete_message_thread(dialog_id, forum_topic_id, std::move(promise));
}
void ForumTopicManager::on_topic_message_count_changed(DialogId dialog_id, ForumTopicId forum_topic_id, int diff) {
if (!can_be_forum(dialog_id) || can_be_forum_topic_id(forum_topic_id).is_error()) {
LOG(ERROR) << "Change by " << diff << " number of loaded messages in thread of " << forum_topic_id << " in "
<< dialog_id;
return;
}
LOG(INFO) << "Change by " << diff << " number of loaded messages in thread of " << forum_topic_id << " in "
<< dialog_id;
auto *dialog_topics = add_dialog_topics(dialog_id);
auto topic = add_topic(dialog_topics, forum_topic_id);
if (topic == nullptr) {
return;
}
topic->message_count_ += diff;
CHECK(topic->message_count_ >= 0);
if (topic->message_count_ == 0 && !td_->auth_manager_->is_bot()) {
// TODO keep topics in the topic list
dialog_topics->topics_.erase(forum_topic_id);
}
}
void ForumTopicManager::on_topic_mention_count_changed(DialogId dialog_id, ForumTopicId forum_topic_id, int32 count,
bool is_relative) {
LOG(INFO) << "Change " << (is_relative ? "by" : "to") << ' ' << count << " number of mentions in thread of "
<< forum_topic_id << " in " << dialog_id;
auto dialog_topics = get_dialog_topics(dialog_id);
if (dialog_topics == nullptr) {
return;
}
auto topic = get_topic(dialog_topics, forum_topic_id);
if (topic == nullptr || topic->topic_ == nullptr) {
return;
}
if (topic->topic_->update_unread_mention_count(count, is_relative)) {
on_forum_topic_changed(dialog_id, topic);
}
}
void ForumTopicManager::on_topic_reaction_count_changed(DialogId dialog_id, ForumTopicId forum_topic_id, int32 count,
bool is_relative) {
LOG(INFO) << "Change " << (is_relative ? "by" : "to") << ' ' << count << " number of reactions in thread of "
<< forum_topic_id << " in " << dialog_id;
auto dialog_topics = get_dialog_topics(dialog_id);
if (dialog_topics == nullptr) {
return;
}
auto topic = get_topic(dialog_topics, forum_topic_id);
if (topic == nullptr || topic->topic_ == nullptr) {
return;
}
if (topic->topic_->update_unread_reaction_count(count, is_relative)) {
on_forum_topic_changed(dialog_id, topic);
}
}
void ForumTopicManager::on_topic_poll_vote_count_changed(DialogId dialog_id, ForumTopicId forum_topic_id, int32 count,
bool is_relative) {
LOG(INFO) << "Change " << (is_relative ? "by" : "to") << ' ' << count << " number of poll votes in thread of "
<< forum_topic_id << " in " << dialog_id;
auto dialog_topics = get_dialog_topics(dialog_id);
if (dialog_topics == nullptr) {
LOG(INFO) << "Topic list of " << dialog_id << " not found";
return;
}
auto topic = get_topic(dialog_topics, forum_topic_id);
if (topic == nullptr || topic->topic_ == nullptr) {
LOG(INFO) << "Topic " << forum_topic_id << " not found";
return;
}
if (topic->topic_->update_unread_poll_vote_count(count, is_relative)) {
on_forum_topic_changed(dialog_id, topic);
}
}
void ForumTopicManager::repair_topic_unread_mention_count(DialogId dialog_id, ForumTopicId forum_topic_id) {
// no need to repair mention count in private chats with bots
if (is_forum(dialog_id, false).is_error() || can_be_forum_topic_id(forum_topic_id).is_error()) {
return;
}
td_->create_handler<GetForumTopicQuery>(Promise<td_api::object_ptr<td_api::forumTopic>>())
->send(dialog_id, forum_topic_id);
}
void ForumTopicManager::repair_topic_unread_reaction_count(DialogId dialog_id, ForumTopicId forum_topic_id) {
if (is_forum(dialog_id, true).is_error() || can_be_forum_topic_id(forum_topic_id).is_error()) {
return;
}
td_->create_handler<GetForumTopicQuery>(Promise<td_api::object_ptr<td_api::forumTopic>>())
->send(dialog_id, forum_topic_id);
}
void ForumTopicManager::repair_topic_unread_poll_vote_count(DialogId dialog_id, ForumTopicId forum_topic_id) {
// no need to repair unread vote count in private chats with bots
if (is_forum(dialog_id, false).is_error() || can_be_forum_topic_id(forum_topic_id).is_error()) {
return;
}
td_->create_handler<GetForumTopicQuery>(Promise<td_api::object_ptr<td_api::forumTopic>>())
->send(dialog_id, forum_topic_id);
}
} // namespace td
|