-
Notifications
You must be signed in to change notification settings - Fork 7.3k
Expand file tree
/
Copy pathexport_data_types.cpp
More file actions
3575 lines (3403 loc) · 111 KB
/
Copy pathexport_data_types.cpp
File metadata and controls
3575 lines (3403 loc) · 111 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
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
/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#include "export/data/export_data_types.h"
#include "export/export_settings.h"
#include "export/output/export_output_file.h"
#include "base/base_file_utilities.h"
#include "ui/text/format_values.h"
#include "core/mime_type.h"
#include "core/utils.h"
#include <QtCore/QDateTime>
#include <QtCore/QTimeZone>
#include <QtCore/QRegularExpression>
#include <QtGui/QImageReader>
#include <range/v3/algorithm/max_element.hpp>
#include <range/v3/view/all.hpp>
#include <range/v3/view/transform.hpp>
#include <range/v3/range/conversion.hpp>
namespace Export {
namespace Data {
namespace {
constexpr auto kMaxImageSize = 10000;
constexpr auto kMigratedMessagesIdShift = -1'000'000'000;
QString PrepareFileNameDatePart(TimeId date) {
return date
? ('@' + QString::fromUtf8(FormatDateTime(date, 0, '-', '-', '_')))
: QString();
}
QString PreparePhotoFileName(int index, TimeId date) {
return "photo_"
+ QString::number(index)
+ PrepareFileNameDatePart(date)
+ ".jpg";
}
QString PrepareStoryFileName(
int index,
TimeId date,
const Utf8String &extension) {
return "story_"
+ QString::number(index)
+ PrepareFileNameDatePart(date)
+ extension;
}
std::vector<std::vector<HistoryMessageMarkupButton>> ButtonRowsFromTL(
const MTPDreplyInlineMarkup &data) {
const auto &list = data.vrows().v;
if (list.isEmpty()) {
return {};
}
using Type = HistoryMessageMarkupButton::Type;
auto rows = std::vector<std::vector<HistoryMessageMarkupButton>>();
rows.reserve(list.size());
for (const auto &tlRow : list) {
auto row = std::vector<HistoryMessageMarkupButton>();
row.reserve(tlRow.data().vbuttons().v.size());
for (const auto &button : tlRow.data().vbuttons().v) {
const auto &fields = button.data();
const auto text = qs(fields.vtext());
fields.vtype().match([&](const MTPDinlineButtonTypeUrl &data) {
row.push_back({
Type::Url,
text,
qba(data.vurl())
});
}, [&](const MTPDinlineButtonTypeUrlAuth &data) {
row.push_back({
Type::Auth,
text,
qba(data.vurl()),
qs(data.vfwd_text().value_or_empty()),
data.vbutton_id().v,
});
}, [&](const MTPDinputInlineButtonTypeUrlAuth &) {
}, [&](const MTPDinlineButtonTypeWebView &data) {
row.push_back({
Type::WebView,
text,
data.vurl().v
});
}, [&](const MTPDinlineButtonTypeCallback &data) {
row.push_back({
(data.is_requires_password()
? Type::CallbackWithPassword
: Type::Callback),
text,
qba(data.vdata())
});
}, [&](const MTPDinlineButtonTypeGame &) {
row.push_back({ Type::Game, text });
}, [&](const MTPDinlineButtonTypeBuy &) {
row.push_back({ Type::Buy, text });
}, [&](const MTPDinlineButtonTypeSwitchInline &data) {
const auto type = data.is_same_peer()
? Type::SwitchInlineSame
: Type::SwitchInline;
row.push_back({ type, text, qba(data.vquery()) });
}, [&](const MTPDinlineButtonTypeUserProfile &data) {
row.push_back({
Type::UserProfile,
text,
QByteArray::number(data.vuser_id().v)
});
}, [&](const MTPDinputInlineButtonTypeUserProfile &) {
}, [&](const MTPDinlineButtonTypeCopy &data) {
row.push_back({
Type::CopyText,
text,
data.vcopy_text().v,
});
}, [&](const MTPDinlineButtonTypeDisabled &) {
row.push_back({ Type::Disabled, text });
});
}
if (!row.empty()) {
rows.push_back(std::move(row));
}
}
return rows;
}
} // namespace
Utf8String InlineButtonAction::TypeToString(
const InlineButtonAction &action) {
using Type = InlineButtonAction::Type;
switch (action.type) {
case Type::Url: return "url";
case Type::Auth: return "auth";
case Type::WebView: return "web_view";
case Type::Callback: return "callback";
case Type::CallbackWithPassword: return "callback_with_password";
case Type::Game: return "game";
case Type::Buy: return "buy";
case Type::SwitchInline: return "switch_inline";
case Type::SwitchInlineSame: return "switch_inline_same";
case Type::UserProfile: return "user_profile";
case Type::CopyText: return "copy_text";
case Type::Disabled: return "disabled";
}
Unexpected("Type in InlineButtonAction::Type.");
}
Utf8String InlineButtonPeerTypeToString(InlineButtonPeerType type) {
using Type = InlineButtonPeerType;
switch (type) {
case Type::SameBotPM: return "same_bot_pm";
case Type::PM: return "pm";
case Type::Chat: return "chat";
case Type::Megagroup: return "megagroup";
case Type::Broadcast: return "broadcast";
case Type::BotPM: return "bot_pm";
}
Unexpected("Type in InlineButtonPeerType.");
}
Utf8String RichButtonStyleToString(RichButtonStyle style) {
using Style = RichButtonStyle;
switch (style) {
case Style::Default: return "default";
case Style::Primary: return "primary";
case Style::Success: return "success";
case Style::Danger: return "danger";
case Style::Link: return "link";
}
Unexpected("Style in RichButtonStyle.");
}
Utf8String RichButtonAlignmentToString(RichButtonAlignment alignment) {
using Alignment = RichButtonAlignment;
switch (alignment) {
case Alignment::Stretch: return "stretch";
case Alignment::Left: return "left";
case Alignment::Center: return "center";
case Alignment::Right: return "right";
}
Unexpected("Alignment in RichButtonAlignment.");
}
QByteArray HistoryMessageMarkupButton::TypeToString(
const HistoryMessageMarkupButton &button) {
using Type = HistoryMessageMarkupButton::Type;
switch (button.type) {
case Type::Default: return "default";
case Type::Url: return "url";
case Type::Callback: return "callback";
case Type::CallbackWithPassword: return "callback_with_password";
case Type::RequestPhone: return "request_phone";
case Type::RequestLocation: return "request_location";
case Type::RequestPoll: return "request_poll";
case Type::RequestPeer: return "request_peer";
case Type::SwitchInline: return "switch_inline";
case Type::SwitchInlineSame: return "switch_inline_same";
case Type::Game: return "game";
case Type::Buy: return "buy";
case Type::Auth: return "auth";
case Type::UserProfile: return "user_profile";
case Type::WebView: return "web_view";
case Type::SimpleWebView: return "simple_web_view";
case Type::CopyText: return "copy_text";
case Type::Disabled: return "disabled";
}
Unexpected("Type in HistoryMessageMarkupButton::Type.");
}
uint8 PeerColorIndex(BareId bareId) {
const uint8 map[] = { 0, 7, 4, 1, 6, 3, 5 };
return map[bareId % base::array_size(map)];
}
BareId PeerToBareId(PeerId peerId) {
return (peerId.value & PeerId::kChatTypeMask);
}
uint8 PeerColorIndex(PeerId peerId) {
return PeerColorIndex(PeerToBareId(peerId));
}
BareId StringBarePeerId(const Utf8String &data) {
auto result = BareId(0xFF);
for (const auto ch : data) {
result *= 239;
result += ch;
result &= 0xFF;
}
return result;
}
uint8 ApplicationColorIndex(int applicationId) {
static const auto official = std::map<int, int> {
{ 1, 0 }, // iOS
{ 7, 0 }, // iOS X
{ 6, 1 }, // Android
{ 21724, 1 }, // Android X
{ 2834, 2 }, // macOS
{ 2496, 3 }, // Webogram
{ 2040, 4 }, // Desktop
{ 1429, 5 }, // Windows Phone
};
if (const auto i = official.find(applicationId); i != end(official)) {
return i->second;
}
return PeerColorIndex(applicationId);
}
int DomainApplicationId(const Utf8String &data) {
return 0x1000 + StringBarePeerId(data);
}
PeerId ParsePeerId(const MTPPeer &data) {
return peerFromMTP(data);
}
Utf8String ParseString(const MTPstring &data) {
return data.v;
}
std::vector<TextPart> ParseText(
const MTPstring &data,
const QVector<MTPMessageEntity> &entities) {
using Type = TextPart::Type;
const auto text = QString::fromUtf8(data.v);
const auto size = data.v.size();
const auto mid = [&](int offset, int length) {
return text.mid(offset, length).toUtf8();
};
auto result = std::vector<TextPart>();
auto offset = 0;
auto addTextPart = [&](int till) {
if (till > offset) {
auto part = TextPart();
part.text = mid(offset, till - offset);
result.push_back(std::move(part));
offset = till;
}
};
for (const auto &entity : entities) {
const auto start = entity.match([](const auto &data) {
return data.voffset().v;
});
const auto length = entity.match([](const auto &data) {
return data.vlength().v;
});
if (start < offset || length <= 0 || start + length > size) {
continue;
}
addTextPart(start);
auto part = TextPart();
part.type = entity.match(
[](const MTPDmessageEntityUnknown&) { return Type::Unknown; },
[](const MTPDmessageEntityMention&) { return Type::Mention; },
[](const MTPDmessageEntityHashtag&) { return Type::Hashtag; },
[](const MTPDmessageEntityBotCommand&) {
return Type::BotCommand; },
[](const MTPDmessageEntityUrl&) { return Type::Url; },
[](const MTPDmessageEntityEmail&) { return Type::Email; },
[](const MTPDmessageEntityBold&) { return Type::Bold; },
[](const MTPDmessageEntityItalic&) { return Type::Italic; },
[](const MTPDmessageEntityCode&) { return Type::Code; },
[](const MTPDmessageEntityPre&) { return Type::Pre; },
[](const MTPDmessageEntityTextUrl&) { return Type::TextUrl; },
[](const MTPDmessageEntityMentionName&) {
return Type::MentionName; },
[](const MTPDinputMessageEntityMentionName&) {
return Type::MentionName; },
[](const MTPDmessageEntityPhone&) { return Type::Phone; },
[](const MTPDmessageEntityCashtag&) { return Type::Cashtag; },
[](const MTPDmessageEntityUnderline&) { return Type::Underline; },
[](const MTPDmessageEntityStrike&) { return Type::Strike; },
[](const MTPDmessageEntityBlockquote&) {
return Type::Blockquote; },
[](const MTPDmessageEntityBankCard&) { return Type::BankCard; },
[](const MTPDmessageEntitySpoiler&) { return Type::Spoiler; },
[](const MTPDmessageEntityCustomEmoji&) { return Type::CustomEmoji; },
[](const MTPDmessageEntityFormattedDate&) { return Type::Unknown; },
[](const MTPDmessageEntityDiffInsert&) { return Type::Unknown; },
[](const MTPDmessageEntityDiffReplace&) { return Type::Unknown; },
[](const MTPDmessageEntityDiffDelete&) { return Type::Unknown; });
part.text = mid(start, length);
part.additional = entity.match(
[](const MTPDmessageEntityPre &data) {
return ParseString(data.vlanguage());
}, [](const MTPDmessageEntityTextUrl &data) {
return ParseString(data.vurl());
}, [](const MTPDmessageEntityMentionName &data) {
return NumberToString(data.vuser_id().v);
}, [](const MTPDmessageEntityCustomEmoji &data) {
return NumberToString(data.vdocument_id().v);
}, [](const MTPDmessageEntityBlockquote &data) {
return data.is_collapsed() ? Utf8String("1") : Utf8String();
}, [](const auto &) { return Utf8String(); });
result.push_back(std::move(part));
offset = start + length;
}
addTextPart(size);
return result;
}
Utf8String Reaction::TypeToString(const Reaction &reaction) {
switch (reaction.type) {
case Reaction::Type::Empty: return "empty";
case Reaction::Type::Emoji: return "emoji";
case Reaction::Type::CustomEmoji: return "custom_emoji";
case Reaction::Type::Paid: return "paid";
}
Unexpected("Type in Reaction::Type.");
}
std::vector<TextPart> ParseText(const MTPTextWithEntities &text) {
return ParseText(text.data().vtext(), text.data().ventities().v);
}
Photo ParsePhoto(const MTPPhoto &data, const QString &suggestedPath);
namespace {
RichText ParseRichText(const MTPRichText &text);
RichBlock ParseRichBlock(const MTPPageBlock &block);
std::vector<RichBlock> ParseRichBlocks(
const QVector<MTPPageBlock> &blocks);
InlineButtonPeerType ParseInlineButtonPeerType(
const MTPInlineQueryPeerType &type) {
using Type = InlineButtonPeerType;
return type.match(
[](const MTPDinlineQueryPeerTypeSameBotPM &) {
return Type::SameBotPM;
}, [](const MTPDinlineQueryPeerTypePM &) {
return Type::PM;
}, [](const MTPDinlineQueryPeerTypeChat &) {
return Type::Chat;
}, [](const MTPDinlineQueryPeerTypeMegagroup &) {
return Type::Megagroup;
}, [](const MTPDinlineQueryPeerTypeBroadcast &) {
return Type::Broadcast;
}, [](const MTPDinlineQueryPeerTypeBotPM &) {
return Type::BotPM;
});
}
std::optional<InlineButtonAction> ParseInlineButtonAction(
const MTPInlineButtonType &type) {
using Type = InlineButtonAction::Type;
auto result = std::optional<InlineButtonAction>();
type.match([&](const MTPDinlineButtonTypeUrl &data) {
auto &action = result.emplace();
action.type = Type::Url;
action.url = ParseString(data.vurl());
}, [&](const MTPDinlineButtonTypeUrlAuth &data) {
auto &action = result.emplace();
action.type = Type::Auth;
action.url = ParseString(data.vurl());
action.buttonId = data.vbutton_id().v;
if (const auto forwardText = data.vfwd_text()) {
action.forwardText = ParseString(*forwardText);
}
}, [&](const MTPDinputInlineButtonTypeUrlAuth &) {
}, [&](const MTPDinlineButtonTypeWebView &data) {
auto &action = result.emplace();
action.type = Type::WebView;
action.url = ParseString(data.vurl());
}, [&](const MTPDinlineButtonTypeCallback &data) {
auto &action = result.emplace();
action.requiresPassword = data.is_requires_password();
action.type = action.requiresPassword
? Type::CallbackWithPassword
: Type::Callback;
action.callbackData = qba(data.vdata());
}, [&](const MTPDinlineButtonTypeGame &) {
auto &action = result.emplace();
action.type = Type::Game;
}, [&](const MTPDinlineButtonTypeBuy &) {
auto &action = result.emplace();
action.type = Type::Buy;
}, [&](const MTPDinlineButtonTypeSwitchInline &data) {
auto &action = result.emplace();
action.samePeer = data.is_same_peer();
action.type = action.samePeer
? Type::SwitchInlineSame
: Type::SwitchInline;
action.query = ParseString(data.vquery());
if (const auto peerTypes = data.vpeer_types()) {
auto &parsed = action.peerTypes.emplace();
parsed.reserve(peerTypes->v.size());
for (const auto &peerType : peerTypes->v) {
parsed.push_back(ParseInlineButtonPeerType(peerType));
}
}
}, [&](const MTPDinlineButtonTypeUserProfile &data) {
auto &action = result.emplace();
action.type = Type::UserProfile;
action.userId = uint64(data.vuser_id().v);
}, [&](const MTPDinputInlineButtonTypeUserProfile &) {
}, [&](const MTPDinlineButtonTypeCopy &data) {
auto &action = result.emplace();
action.type = Type::CopyText;
action.copyText = ParseString(data.vcopy_text());
}, [&](const MTPDinlineButtonTypeDisabled &) {
auto &action = result.emplace();
action.type = Type::Disabled;
});
return result;
}
bool RichButtonLinkStyleAllowed(InlineButtonAction::Type type) {
using Type = InlineButtonAction::Type;
return (type == Type::Callback)
|| (type == Type::CallbackWithPassword)
|| (type == Type::Disabled);
}
std::optional<RichButtonStyle> ParseRichButtonStyle(
const tl::conditional<MTPRichButtonStyle> &style,
InlineButtonAction::Type actionType,
bool inlineButton) {
if (!style) {
return std::nullopt;
}
const auto &data = style->data();
if (inlineButton
&& data.is_link()
&& RichButtonLinkStyleAllowed(actionType)) {
return RichButtonStyle::Link;
}
return data.is_bg_danger()
? RichButtonStyle::Danger
: data.is_bg_primary()
? RichButtonStyle::Primary
: data.is_bg_success()
? RichButtonStyle::Success
: RichButtonStyle::Default;
}
std::optional<RichText> ParseRichButton(
const MTPRichText &text,
const MTPInlineButtonType &type,
const tl::conditional<MTPRichButtonStyle> &style,
bool inlineButton) {
auto action = ParseInlineButtonAction(type);
if (!action) {
return std::nullopt;
}
auto result = RichText();
result.type = RichText::Type::Button;
result.children.reserve(1);
result.children.push_back(ParseRichText(text));
auto buttonStyle = ParseRichButtonStyle(
style,
action->type,
inlineButton);
result.button = std::make_unique<RichButtonPayload>(RichButtonPayload{
.action = std::move(*action),
.style = std::move(buttonStyle),
});
return result;
}
RichText ParseRichTextWrapper(
RichText::Type type,
const MTPRichText &child) {
auto result = RichText();
result.type = type;
result.children.reserve(1);
result.children.push_back(ParseRichText(child));
return result;
}
RichText ParseRichText(const MTPRichText &text) {
using Type = RichText::Type;
return text.match(
[](const MTPDtextEmpty &) {
auto result = RichText();
result.type = Type::Empty;
return result;
}, [](const MTPDtextPlain &data) {
auto result = RichText();
result.type = Type::Plain;
result.text = ParseString(data.vtext());
return result;
}, [](const MTPDtextBold &data) {
return ParseRichTextWrapper(Type::Bold, data.vtext());
}, [](const MTPDtextItalic &data) {
return ParseRichTextWrapper(Type::Italic, data.vtext());
}, [](const MTPDtextUnderline &data) {
return ParseRichTextWrapper(Type::Underline, data.vtext());
}, [](const MTPDtextStrike &data) {
return ParseRichTextWrapper(Type::Strike, data.vtext());
}, [](const MTPDtextFixed &data) {
return ParseRichTextWrapper(Type::Fixed, data.vtext());
}, [](const MTPDtextUrl &data) {
auto result = ParseRichTextWrapper(Type::Url, data.vtext());
result.data = ParseString(data.vurl());
result.id = uint64(data.vwebpage_id().v);
return result;
}, [](const MTPDtextEmail &data) {
auto result = ParseRichTextWrapper(Type::Email, data.vtext());
result.data = ParseString(data.vemail());
return result;
}, [](const MTPDtextConcat &data) {
auto result = RichText();
result.type = Type::Concat;
const auto &texts = data.vtexts().v;
result.children.reserve(texts.size());
for (const auto &text : texts) {
result.children.push_back(ParseRichText(text));
}
return result;
}, [](const MTPDtextSubscript &data) {
return ParseRichTextWrapper(Type::Subscript, data.vtext());
}, [](const MTPDtextSuperscript &data) {
return ParseRichTextWrapper(Type::Superscript, data.vtext());
}, [](const MTPDtextMarked &data) {
return ParseRichTextWrapper(Type::Marked, data.vtext());
}, [](const MTPDtextPhone &data) {
auto result = ParseRichTextWrapper(Type::Phone, data.vtext());
result.data = ParseString(data.vphone());
return result;
}, [](const MTPDtextImage &data) {
auto result = RichText();
result.type = Type::InlineImage;
result.id = uint64(data.vdocument_id().v);
result.width = data.vw().v;
result.height = data.vh().v;
return result;
}, [](const MTPDtextAnchor &data) {
auto result = ParseRichTextWrapper(Type::Anchor, data.vtext());
result.data = ParseString(data.vname());
return result;
}, [](const MTPDtextMath &data) {
auto result = RichText();
result.type = Type::Math;
result.data = ParseString(data.vsource());
return result;
}, [](const MTPDtextCustomEmoji &data) {
auto result = RichText();
result.type = Type::CustomEmoji;
result.text = ParseString(data.valt());
result.id = uint64(data.vdocument_id().v);
result.customEmojiData = NumberToString(result.id);
return result;
}, [](const MTPDtextSpoiler &data) {
return ParseRichTextWrapper(Type::Spoiler, data.vtext());
}, [](const MTPDtextMention &data) {
return ParseRichTextWrapper(Type::Mention, data.vtext());
}, [](const MTPDtextHashtag &data) {
return ParseRichTextWrapper(Type::Hashtag, data.vtext());
}, [](const MTPDtextBotCommand &data) {
return ParseRichTextWrapper(Type::BotCommand, data.vtext());
}, [](const MTPDtextCashtag &data) {
return ParseRichTextWrapper(Type::Cashtag, data.vtext());
}, [](const MTPDtextAutoUrl &data) {
return ParseRichTextWrapper(Type::AutoUrl, data.vtext());
}, [](const MTPDtextAutoEmail &data) {
return ParseRichTextWrapper(Type::AutoEmail, data.vtext());
}, [](const MTPDtextAutoPhone &data) {
return ParseRichTextWrapper(Type::AutoPhone, data.vtext());
}, [](const MTPDtextBankCard &data) {
return ParseRichTextWrapper(Type::BankCard, data.vtext());
}, [](const MTPDtextMentionName &data) {
auto result = ParseRichTextWrapper(Type::MentionName, data.vtext());
result.id = uint64(data.vuser_id().v);
return result;
}, [](const MTPDtextDate &data) {
auto result = ParseRichTextWrapper(Type::FormattedDate, data.vtext());
result.date = data.vdate().v;
result.relative = data.is_relative();
result.shortTime = data.is_short_time();
result.longTime = data.is_long_time();
result.shortDate = data.is_short_date();
result.longDate = data.is_long_date();
result.dayOfWeek = data.is_day_of_week();
return result;
}, [](const MTPDtextDiff &data) {
auto result = RichText();
result.type = Type::Diff;
result.unsupported = true;
result.children.reserve(1);
result.children.push_back(ParseRichText(data.vtext()));
result.oldChildren.reserve(1);
result.oldChildren.push_back(ParseRichText(data.vold_text()));
return result;
}, [](const MTPDtextButton &data) {
if (auto button = ParseRichButton(
data.vtext(),
data.vtype(),
data.vstyle(),
true)) {
return std::move(*button);
}
return ParseRichText(data.vtext());
});
}
RichCaption ParseRichCaption(const MTPPageCaption &caption) {
const auto &data = caption.data();
return {
.text = ParseRichText(data.vtext()),
.credit = ParseRichText(data.vcredit()),
};
}
RichTaskState ParseRichTaskState(bool checkbox, bool checked) {
if (!checkbox) {
return RichTaskState::None;
}
return checked ? RichTaskState::Checked : RichTaskState::Unchecked;
}
RichListItem ParseRichListItem(const MTPPageListItem &item) {
return item.match([](const MTPDpageListItemText &data) {
auto result = RichListItem();
result.content = RichListItemContent::Text;
result.taskState = ParseRichTaskState(
data.is_checkbox(),
data.is_checked());
result.text = ParseRichText(data.vtext());
return result;
}, [](const MTPDpageListItemBlocks &data) {
auto result = RichListItem();
result.content = RichListItemContent::Blocks;
result.taskState = ParseRichTaskState(
data.is_checkbox(),
data.is_checked());
result.blocks = ParseRichBlocks(data.vblocks().v);
return result;
});
}
template <typename Data>
void FillRichOrderedListItem(
RichListItem &result,
const Data &data) {
result.taskState = ParseRichTaskState(
data.is_checkbox(),
data.is_checked());
if (const auto num = data.vnum()) {
result.num = ParseString(*num);
}
if (const auto value = data.vvalue()) {
result.value = value->v;
}
if (const auto type = data.vtype()) {
result.type = ParseString(*type);
}
}
RichListItem ParseRichOrderedListItem(
const MTPPageListOrderedItem &item) {
return item.match([](const MTPDpageListOrderedItemText &data) {
auto result = RichListItem();
result.content = RichListItemContent::Text;
FillRichOrderedListItem(result, data);
result.text = ParseRichText(data.vtext());
return result;
}, [](const MTPDpageListOrderedItemBlocks &data) {
auto result = RichListItem();
result.content = RichListItemContent::Blocks;
FillRichOrderedListItem(result, data);
result.blocks = ParseRichBlocks(data.vblocks().v);
return result;
});
}
RichTableAlignment ParseRichTableAlignment(
const MTPDpageTableCell &data) {
if (data.is_align_right()) {
return RichTableAlignment::Right;
} else if (data.is_align_center()) {
return RichTableAlignment::Center;
}
return RichTableAlignment::Left;
}
RichTableVerticalAlignment ParseRichTableVerticalAlignment(
const MTPDpageTableCell &data) {
if (data.is_valign_bottom()) {
return RichTableVerticalAlignment::Bottom;
} else if (data.is_valign_middle()) {
return RichTableVerticalAlignment::Middle;
}
return RichTableVerticalAlignment::Top;
}
RichTableCell ParseRichTableCell(const MTPPageTableCell &cell) {
const auto &data = cell.data();
auto result = RichTableCell();
result.header = data.is_header();
result.alignment = ParseRichTableAlignment(data);
result.verticalAlignment = ParseRichTableVerticalAlignment(data);
if (const auto text = data.vtext()) {
result.text = ParseRichText(*text);
}
if (const auto colspan = data.vcolspan()) {
result.colspan = colspan->v;
}
if (const auto rowspan = data.vrowspan()) {
result.rowspan = rowspan->v;
}
return result;
}
RichTableRow ParseRichTableRow(const MTPPageTableRow &row) {
const auto &cells = row.data().vcells().v;
auto result = RichTableRow();
result.cells.reserve(cells.size());
for (const auto &cell : cells) {
result.cells.push_back(ParseRichTableCell(cell));
}
return result;
}
RichRelatedArticle ParseRichRelatedArticle(
const MTPPageRelatedArticle &article) {
const auto &data = article.data();
auto result = RichRelatedArticle();
result.url = ParseString(data.vurl());
result.webpageId = uint64(data.vwebpage_id().v);
if (const auto title = data.vtitle()) {
result.title = ParseString(*title);
}
if (const auto description = data.vdescription()) {
result.description = ParseString(*description);
}
if (const auto photoId = data.vphoto_id()) {
result.photoId = uint64(photoId->v);
}
if (const auto author = data.vauthor()) {
result.author = ParseString(*author);
}
if (const auto publishedDate = data.vpublished_date()) {
result.publishedDate = publishedDate->v;
}
return result;
}
RichChannel ParseRichChannel(const MTPChat &chat) {
using Source = RichChannel::Source;
return chat.match([](const MTPDchatEmpty &data) {
auto result = RichChannel();
result.source = Source::ChatEmpty;
result.id = uint64(data.vid().v);
return result;
}, [](const MTPDchat &data) {
auto result = RichChannel();
result.source = Source::Chat;
result.id = uint64(data.vid().v);
result.title = ParseString(data.vtitle());
return result;
}, [](const MTPDchatForbidden &data) {
auto result = RichChannel();
result.source = Source::ChatForbidden;
result.id = uint64(data.vid().v);
result.title = ParseString(data.vtitle());
return result;
}, [](const MTPDchannel &data) {
auto result = RichChannel();
result.source = Source::Channel;
result.id = uint64(data.vid().v);
result.title = ParseString(data.vtitle());
result.broadcast = data.is_broadcast();
result.megagroup = data.is_megagroup();
result.monoforum = data.is_monoforum();
if (const auto accessHash = data.vaccess_hash()) {
result.accessHash = int64(accessHash->v);
}
if (const auto username = data.vusername()) {
result.username = ParseString(*username);
}
return result;
}, [](const MTPDchannelForbidden &data) {
auto result = RichChannel();
result.source = Source::ChannelForbidden;
result.id = uint64(data.vid().v);
result.title = ParseString(data.vtitle());
result.accessHash = int64(data.vaccess_hash().v);
result.broadcast = data.is_broadcast();
result.megagroup = data.is_megagroup();
result.monoforum = data.is_monoforum();
return result;
}, [](const MTPDcommunity &data) {
auto result = RichChannel();
result.source = Source::Community;
result.id = uint64(data.vid().v);
result.title = ParseString(data.vtitle());
if (const auto accessHash = data.vaccess_hash()) {
result.accessHash = int64(accessHash->v);
}
return result;
}, [](const MTPDcommunityForbidden &data) {
auto result = RichChannel();
result.source = Source::CommunityForbidden;
result.id = uint64(data.vid().v);
result.title = ParseString(data.vtitle());
if (const auto accessHash = data.vaccess_hash()) {
result.accessHash = int64(accessHash->v);
}
return result;
});
}
RichMapPoint ParseRichMapPoint(const MTPGeoPoint &point) {
using Source = RichMapPoint::Source;
return point.match([](const MTPDgeoPointEmpty &) {
auto result = RichMapPoint();
result.source = Source::GeoPointEmpty;
return result;
}, [](const MTPDgeoPoint &data) {
auto result = RichMapPoint();
result.source = Source::GeoPoint;
result.latitude = data.vlat().v;
result.longitude = data.vlong().v;
result.accessHash = int64(data.vaccess_hash().v);
if (const auto accuracyRadius = data.vaccuracy_radius()) {
result.accuracyRadius = accuracyRadius->v;
}
return result;
});
}
RichMapPoint ParseRichMapPoint(const MTPInputGeoPoint &point) {
using Source = RichMapPoint::Source;
return point.match([](const MTPDinputGeoPointEmpty &) {
auto result = RichMapPoint();
result.source = Source::InputGeoPointEmpty;
return result;
}, [](const MTPDinputGeoPoint &data) {
auto result = RichMapPoint();
result.source = Source::InputGeoPoint;
result.latitude = data.vlat().v;
result.longitude = data.vlong().v;
if (const auto accuracyRadius = data.vaccuracy_radius()) {
result.accuracyRadius = accuracyRadius->v;
}
return result;
});
}
RichBlock ParseRichSupportedBlock(RichBlock::Kind kind) {
auto result = RichBlock();
result.kind = kind;
return result;
}
RichBlock ParseRichTextBlock(
RichBlock::Kind kind,
const MTPRichText &text) {
auto result = RichBlock();
result.kind = kind;
result.text = ParseRichText(text);
return result;
}
RichBlock ParseRichHeadingBlock(
int level,
const MTPRichText &text) {
auto result = ParseRichTextBlock(RichBlock::Kind::Heading, text);
result.headingLevel = level;
return result;
}
RichBlock ParseRichUnsupportedBlock(RichBlock::Kind kind) {
auto result = ParseRichSupportedBlock(kind);
result.unsupported = true;
return result;
}
RichBlock ParseRichBlock(const MTPPageBlock &block) {
using Kind = RichBlock::Kind;
return block.match(
[](const MTPDpageBlockUnsupported &) {
return ParseRichUnsupportedBlock(Kind::Unsupported);
}, [](const MTPDpageBlockTitle &data) {
return ParseRichHeadingBlock(1, data.vtext());
}, [](const MTPDpageBlockSubtitle &data) {
return ParseRichHeadingBlock(2, data.vtext());
}, [](const MTPDpageBlockAuthorDate &data) {
auto result = ParseRichTextBlock(Kind::AuthorDate, data.vauthor());
result.date = data.vpublished_date().v;
return result;
}, [](const MTPDpageBlockHeader &data) {
return ParseRichHeadingBlock(3, data.vtext());
}, [](const MTPDpageBlockSubheader &data) {
return ParseRichHeadingBlock(4, data.vtext());
}, [](const MTPDpageBlockParagraph &data) {
return ParseRichTextBlock(Kind::Paragraph, data.vtext());
}, [](const MTPDpageBlockPreformatted &data) {
auto result = ParseRichTextBlock(Kind::Code, data.vtext());
result.language = ParseString(data.vlanguage());
return result;
}, [](const MTPDpageBlockFooter &data) {
return ParseRichTextBlock(Kind::Footer, data.vtext());
}, [](const MTPDpageBlockDivider &) {
auto result = RichBlock();
result.kind = Kind::Divider;
return result;
}, [](const MTPDpageBlockAnchor &data) {
auto result = RichBlock();
result.kind = Kind::Anchor;
result.name = ParseString(data.vname());
return result;
}, [](const MTPDpageBlockList &data) {
auto result = RichBlock();
result.kind = Kind::List;
result.listKind = RichListKind::Bullet;
const auto &items = data.vitems().v;
result.listItems.reserve(items.size());
for (const auto &item : items) {
result.listItems.push_back(ParseRichListItem(item));
}
return result;
}, [](const MTPDpageBlockBlockquote &data) {
auto result = ParseRichTextBlock(Kind::Quote, data.vtext());
result.quoteContent = RichQuoteContent::Text;
result.quoteCaption = ParseRichText(data.vcaption());
return result;
}, [](const MTPDpageBlockPullquote &data) {
auto result = ParseRichTextBlock(Kind::Quote, data.vtext());
result.quoteContent = RichQuoteContent::Text;
result.quoteCaption = ParseRichText(data.vcaption());
result.pullquote = true;
return result;
}, [](const MTPDpageBlockPhoto &data) {
auto result = ParseRichSupportedBlock(Kind::Photo);
result.photoId = uint64(data.vphoto_id().v);
result.caption = ParseRichCaption(data.vcaption());
result.spoiler = data.is_spoiler();
if (const auto url = data.vurl()) {
result.optionalUrl = ParseString(*url);
}
if (const auto webpageId = data.vwebpage_id()) {
result.optionalWebpageId = uint64(webpageId->v);
}
return result;
}, [](const MTPDpageBlockVideo &data) {
auto result = ParseRichSupportedBlock(Kind::Video);
result.documentId = uint64(data.vvideo_id().v);
result.caption = ParseRichCaption(data.vcaption());
result.autoplay = data.is_autoplay();
result.loop = data.is_loop();
result.spoiler = data.is_spoiler();
return result;
}, [](const MTPDpageBlockCover &data) {
auto result = ParseRichSupportedBlock(Kind::Cover);