aboutsummaryrefslogtreecommitdiffhomepage
path: root/td/telegram/NotificationType.cpp
blob: ff9d6ac8e17d6c10d647392c78f28c0aa7e7c9ed (plain)
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
//
// 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/NotificationType.h"

#include "td/telegram/AnimationsManager.h"
#include "td/telegram/AudiosManager.h"
#include "td/telegram/DocumentsManager.h"
#include "td/telegram/MessageSender.h"
#include "td/telegram/MessagesManager.h"
#include "td/telegram/PhotoFormat.h"
#include "td/telegram/StickersManager.h"
#include "td/telegram/Td.h"
#include "td/telegram/VideoNotesManager.h"
#include "td/telegram/VideosManager.h"
#include "td/telegram/VoiceNotesManager.h"

#include "td/utils/logging.h"
#include "td/utils/misc.h"
#include "td/utils/Slice.h"

#include <tuple>

namespace td {

class NotificationTypeMessage final : public NotificationType {
  bool can_be_delayed() const final {
    return message_id_.is_server();
  }

  bool is_temporary() const final {
    return false;
  }

  NotificationObjectId get_object_id() const final {
    return message_id_;
  }

  vector<FileId> get_file_ids(const Td *td) const final {
    return {};
  }

  td_api::object_ptr<td_api::NotificationType> get_notification_type_object(Td *td, DialogId dialog_id) const final {
    auto message_object =
        td->messages_manager_->get_message_object({dialog_id, message_id_}, "get_notification_type_object");
    if (message_object == nullptr) {
      return nullptr;
    }
    return td_api::make_object<td_api::notificationTypeNewMessage>(std::move(message_object), show_preview_);
  }

  StringBuilder &to_string_builder(StringBuilder &string_builder) const final {
    return string_builder << "NewMessageNotification[" << message_id_ << ']';
  }

  MessageId message_id_;
  bool show_preview_;

 public:
  NotificationTypeMessage(MessageId message_id, bool show_preview)
      : message_id_(message_id), show_preview_(show_preview) {
  }
};

class NotificationTypeSecretChat final : public NotificationType {
  bool can_be_delayed() const final {
    return false;
  }

  bool is_temporary() const final {
    return false;
  }

  NotificationObjectId get_object_id() const final {
    return NotificationObjectId();
  }

  vector<FileId> get_file_ids(const Td *td) const final {
    return {};
  }

  td_api::object_ptr<td_api::NotificationType> get_notification_type_object(Td *, DialogId) const final {
    return td_api::make_object<td_api::notificationTypeNewSecretChat>();
  }

  StringBuilder &to_string_builder(StringBuilder &string_builder) const final {
    return string_builder << "NewSecretChatNotification[]";
  }

 public:
  NotificationTypeSecretChat() {
  }
};

class NotificationTypeCall final : public NotificationType {
  bool can_be_delayed() const final {
    return false;
  }

  bool is_temporary() const final {
    return false;
  }

  NotificationObjectId get_object_id() const final {
    return NotificationObjectId::max();
  }

  vector<FileId> get_file_ids(const Td *td) const final {
    return {};
  }

  td_api::object_ptr<td_api::NotificationType> get_notification_type_object(Td *, DialogId) const final {
    return td_api::make_object<td_api::notificationTypeNewCall>(call_id_.get());
  }

  StringBuilder &to_string_builder(StringBuilder &string_builder) const final {
    return string_builder << "NewCallNotification[" << call_id_ << ']';
  }

  CallId call_id_;

 public:
  explicit NotificationTypeCall(CallId call_id) : call_id_(call_id) {
  }
};

class NotificationTypePushMessage final : public NotificationType {
  bool can_be_delayed() const final {
    return false;
  }

  bool is_temporary() const final {
    return true;
  }

  NotificationObjectId get_object_id() const final {
    return message_id_;
  }

  vector<FileId> get_file_ids(const Td *td) const final {
    if (!document_.is_empty()) {
      return document_.get_file_ids(td);
    }

    return photo_get_file_ids(photo_);
  }

  static td_api::object_ptr<td_api::PushMessageContent> get_push_message_content_object(Td *td, Slice key,
                                                                                        const string &arg,
                                                                                        const Photo &photo,
                                                                                        const Document &document) {
    bool is_pinned = false;
    if (begins_with(key, "PINNED_")) {
      is_pinned = true;
      key = key.substr(7);
    }
    if (key == "MESSAGE") {
      return td_api::make_object<td_api::pushMessageContentHidden>(is_pinned);
    }
    if (key == "MESSAGES") {
      return td_api::make_object<td_api::pushMessageContentMediaAlbum>(to_integer<int32>(arg), true, true, false,
                                                                       false);
    }
    CHECK(key.size() > 8);
    switch (key[8]) {
      case 'A':
        if (key == "MESSAGE_ANIMATION") {
          return td_api::make_object<td_api::pushMessageContentAnimation>(
              td->animations_manager_->get_animation_object(document.file_id), arg, is_pinned);
        }
        if (key == "MESSAGE_AUDIO") {
          return td_api::make_object<td_api::pushMessageContentAudio>(
              td->audios_manager_->get_audio_object(document.file_id), is_pinned);
        }
        if (key == "MESSAGE_AUDIOS") {
          return td_api::make_object<td_api::pushMessageContentMediaAlbum>(to_integer<int32>(arg), false, false, true,
                                                                           false);
        }
        break;
      case 'B':
        if (key == "MESSAGE_BASIC_GROUP_CHAT_CREATE") {
          return td_api::make_object<td_api::pushMessageContentBasicGroupChatCreate>();
        }
        break;
      case 'C':
        if (key == "MESSAGE_CHAT_ADD_MEMBERS") {
          return td_api::make_object<td_api::pushMessageContentChatAddMembers>(arg, false, false);
        }
        if (key == "MESSAGE_CHAT_ADD_MEMBERS_RETURNED") {
          return td_api::make_object<td_api::pushMessageContentChatAddMembers>(arg, false, true);
        }
        if (key == "MESSAGE_CHAT_ADD_MEMBERS_YOU") {
          return td_api::make_object<td_api::pushMessageContentChatAddMembers>(arg, true, false);
        }
        if (key == "MESSAGE_CHAT_CHANGE_PHOTO") {
          return td_api::make_object<td_api::pushMessageContentChatChangePhoto>();
        }
        if (key == "MESSAGE_CHAT_CHANGE_THEME") {
          return td_api::make_object<td_api::pushMessageContentChatSetTheme>(arg);
        }
        if (key == "MESSAGE_CHAT_CHANGE_TITLE") {
          return td_api::make_object<td_api::pushMessageContentChatChangeTitle>(arg);
        }
        if (key == "MESSAGE_CHAT_DELETE_MEMBER") {
          return td_api::make_object<td_api::pushMessageContentChatDeleteMember>(arg, false, false);
        }
        if (key == "MESSAGE_CHAT_DELETE_MEMBER_LEFT") {
          return td_api::make_object<td_api::pushMessageContentChatDeleteMember>(arg, false, true);
        }
        if (key == "MESSAGE_CHAT_DELETE_MEMBER_YOU") {
          return td_api::make_object<td_api::pushMessageContentChatDeleteMember>(arg, true, false);
        }
        if (key == "MESSAGE_CHAT_JOIN_BY_LINK") {
          return td_api::make_object<td_api::pushMessageContentChatJoinByLink>();
        }
        if (key == "MESSAGE_CHAT_JOIN_BY_REQUEST") {
          return td_api::make_object<td_api::pushMessageContentChatJoinByRequest>();
        }
        if (key == "MESSAGE_CHAT_LIVESTREAM_END") {
          return td_api::make_object<td_api::pushMessageContentVideoChatEnded>();
        }
        if (key == "MESSAGE_CHAT_LIVESTREAM_START") {
          return td_api::make_object<td_api::pushMessageContentVideoChatStarted>();
        }
        if (key == "MESSAGE_CHAT_VOICECHAT_END") {
          return td_api::make_object<td_api::pushMessageContentVideoChatEnded>();
        }
        if (key == "MESSAGE_CHAT_VOICECHAT_INVITE") {
          return td_api::make_object<td_api::pushMessageContentInviteVideoChatParticipants>(false);
        }
        if (key == "MESSAGE_CHAT_VOICECHAT_INVITE_YOU") {
          return td_api::make_object<td_api::pushMessageContentInviteVideoChatParticipants>(true);
        }
        if (key == "MESSAGE_CHAT_VOICECHAT_START") {
          return td_api::make_object<td_api::pushMessageContentVideoChatStarted>();
        }
        if (key == "MESSAGE_CONTACT") {
          return td_api::make_object<td_api::pushMessageContentContact>(arg, is_pinned);
        }
        if (key == "MESSAGE_CONTACT_REGISTERED") {
          return td_api::make_object<td_api::pushMessageContentContactRegistered>(false);
        }
        if (key == "MESSAGE_CONTACT_REGISTERED_PREMIUM") {
          return td_api::make_object<td_api::pushMessageContentContactRegistered>(true);
        }
        break;
      case 'D':
        if (key == "MESSAGE_DOCUMENT") {
          return td_api::make_object<td_api::pushMessageContentDocument>(
              td->documents_manager_->get_document_object(document.file_id, PhotoFormat::Jpeg), is_pinned);
        }
        if (key == "MESSAGE_DOCUMENTS") {
          return td_api::make_object<td_api::pushMessageContentMediaAlbum>(to_integer<int32>(arg), false, false, false,
                                                                           true);
        }
        break;
      case 'F':
        if (key == "MESSAGE_FORWARDS") {
          return td_api::make_object<td_api::pushMessageContentMessageForwards>(to_integer<int32>(arg));
        }
        break;
      case 'G':
        if (key == "MESSAGE_GAME") {
          return td_api::make_object<td_api::pushMessageContentGame>(arg, is_pinned);
        }
        if (key == "MESSAGE_GAME_SCORE") {
          int32 score = 0;
          string title;
          if (!is_pinned) {
            string score_str;
            std::tie(score_str, title) = split(arg);
            score = to_integer<int32>(score_str);
          }
          return td_api::make_object<td_api::pushMessageContentGameScore>(title, score, is_pinned);
        }
        if (key == "MESSAGE_GIFTCODE") {
          auto month_count = to_integer<int32>(arg);
          return td_api::make_object<td_api::pushMessageContentPremiumGiftCode>(month_count);
        }
        if (key == "MESSAGE_GIVEAWAY") {
          int32 user_count = 0;
          int32 month_count = 0;
          if (!is_pinned) {
            string user_count_str;
            string month_count_str;
            std::tie(user_count_str, month_count_str) = split(arg);
            user_count = to_integer<int32>(user_count_str);
            month_count = to_integer<int32>(month_count_str);
          }
          return td_api::make_object<td_api::pushMessageContentGiveaway>(
              user_count, is_pinned ? nullptr : td_api::make_object<td_api::giveawayPrizePremium>(month_count),
              is_pinned);
        }
        if (key == "MESSAGE_GIVEAWAY_STARS") {
          int32 user_count = 0;
          int64 star_count = 0;
          if (!is_pinned) {
            string user_count_str;
            string star_count_str;
            std::tie(user_count_str, star_count_str) = split(arg);
            user_count = to_integer<int32>(user_count_str);
            star_count = to_integer<int64>(star_count_str);
          }
          return td_api::make_object<td_api::pushMessageContentGiveaway>(
              user_count, is_pinned ? nullptr : td_api::make_object<td_api::giveawayPrizeStars>(star_count), is_pinned);
        }
        break;
      case 'I':
        if (key == "MESSAGE_INVOICE") {
          return td_api::make_object<td_api::pushMessageContentInvoice>(arg, is_pinned);
        }
        break;
      case 'L':
        if (key == "MESSAGE_LIVE_LOCATION") {
          return td_api::make_object<td_api::pushMessageContentLocation>(false, is_pinned);
        }
        if (key == "MESSAGE_LOCATION") {
          return td_api::make_object<td_api::pushMessageContentLocation>(true, is_pinned);
        }
        break;
      case 'P':
        if (key == "MESSAGE_PAID_MEDIA") {
          int64 star_count = 0;
          if (!is_pinned) {
            star_count = to_integer<int64>(arg);
          }
          return td_api::make_object<td_api::pushMessageContentPaidMedia>(star_count, is_pinned);
        }
        if (key == "MESSAGE_PHOTO") {
          return td_api::make_object<td_api::pushMessageContentPhoto>(get_photo_object(td->file_manager_.get(), photo),
                                                                      arg, false, is_pinned);
        }
        if (key == "MESSAGE_PHOTOS") {
          return td_api::make_object<td_api::pushMessageContentMediaAlbum>(to_integer<int32>(arg), true, false, false,
                                                                           false);
        }
        if (key == "MESSAGE_POLL") {
          return td_api::make_object<td_api::pushMessageContentPoll>(arg, true, is_pinned);
        }
        if (key == "MESSAGE_POLL_APPEND") {
          return td_api::make_object<td_api::pushMessageContentPollOptionAdded>(arg);
        }
        if (key == "MESSAGE_PROXIMITY") {
          return td_api::make_object<td_api::pushMessageContentProximityAlertTriggered>(to_integer<int32>(arg));
        }
        break;
      case 'Q':
        if (key == "MESSAGE_QUIZ") {
          return td_api::make_object<td_api::pushMessageContentPoll>(arg, false, is_pinned);
        }
        break;
      case 'R':
        if (key == "MESSAGE_RECURRING_PAYMENT") {
          return td_api::make_object<td_api::pushMessageContentRecurringPayment>(arg);
        }
        break;
      case 'S':
        if (key == "MESSAGE_SAME_WALLPAPER") {
          return td_api::make_object<td_api::pushMessageContentChatSetBackground>(true);
        }
        if (key == "MESSAGE_SCREENSHOT_TAKEN") {
          return td_api::make_object<td_api::pushMessageContentScreenshotTaken>();
        }
        if (key == "MESSAGE_SECRET_PHOTO") {
          return td_api::make_object<td_api::pushMessageContentPhoto>(nullptr, arg, true, false);
        }
        if (key == "MESSAGE_SECRET_VIDEO") {
          return td_api::make_object<td_api::pushMessageContentVideo>(nullptr, arg, true, false);
        }
        if (key == "MESSAGE_STARGIFT") {
          auto star_count = to_integer<int64>(arg);
          return td_api::make_object<td_api::pushMessageContentGift>(star_count, false);
        }
        if (key == "MESSAGE_STARGIFT_PREPAID_UPGRADE") {
          auto star_count = to_integer<int64>(arg);
          return td_api::make_object<td_api::pushMessageContentGift>(star_count, true);
        }
        if (key == "MESSAGE_STARGIFT_TRANSFER") {
          return td_api::make_object<td_api::pushMessageContentUpgradedGift>(false, false);
        }
        if (key == "MESSAGE_STARGIFT_UPGRADE") {
          return td_api::make_object<td_api::pushMessageContentUpgradedGift>(true, false);
        }
        if (key == "MESSAGE_STARGIFT_UNPACK_UPGRADE") {
          return td_api::make_object<td_api::pushMessageContentUpgradedGift>(false, true);
        }
        if (key == "MESSAGE_STICKER") {
          return td_api::make_object<td_api::pushMessageContentSticker>(
              td->stickers_manager_->get_sticker_object(document.file_id), trim(arg), is_pinned);
        }
        if (key == "MESSAGE_STORY") {
          return td_api::make_object<td_api::pushMessageContentStory>(false, is_pinned);
        }
        if (key == "MESSAGE_STORY_MENTION") {
          return td_api::make_object<td_api::pushMessageContentStory>(true, is_pinned);
        }
        if (key == "MESSAGE_SUGGEST_BIRTHDAY") {
          return td_api::make_object<td_api::pushMessageContentSuggestBirthdate>();
        }
        if (key == "MESSAGE_SUGGEST_PHOTO") {
          return td_api::make_object<td_api::pushMessageContentSuggestProfilePhoto>();
        }
        break;
      case 'T':
        if (key == "MESSAGE_TEXT") {
          return td_api::make_object<td_api::pushMessageContentText>(arg, is_pinned);
        }
        if (key == "MESSAGE_TODO") {
          return td_api::make_object<td_api::pushMessageContentChecklist>(arg, is_pinned);
        }
        if (key == "MESSAGE_TODO_APPEND") {
          return td_api::make_object<td_api::pushMessageContentChecklistTasksAdded>(to_integer<int32>(arg));
        }
        if (key == "MESSAGE_TODO_DONE") {
          return td_api::make_object<td_api::pushMessageContentChecklistTasksDone>(to_integer<int32>(arg));
        }
        break;
      case 'V':
        if (key == "MESSAGE_VIDEO") {
          return td_api::make_object<td_api::pushMessageContentVideo>(
              td->videos_manager_->get_video_object(document.file_id), arg, false, is_pinned);
        }
        if (key == "MESSAGE_VIDEO_NOTE") {
          return td_api::make_object<td_api::pushMessageContentVideoNote>(
              td->video_notes_manager_->get_video_note_object(document.file_id), is_pinned);
        }
        if (key == "MESSAGE_VIDEOS") {
          return td_api::make_object<td_api::pushMessageContentMediaAlbum>(to_integer<int32>(arg), false, true, false,
                                                                           false);
        }
        if (key == "MESSAGE_VOICE_NOTE") {
          return td_api::make_object<td_api::pushMessageContentVoiceNote>(
              td->voice_notes_manager_->get_voice_note_object(document.file_id), is_pinned);
        }
        break;
      case 'W':
        if (key == "MESSAGE_WALLPAPER") {
          return td_api::make_object<td_api::pushMessageContentChatSetBackground>(false);
        }
        break;
      default:
        break;
    }
    LOG(FATAL) << "Have unsupported push notification key " << key;
    return nullptr;
  }

  td_api::object_ptr<td_api::NotificationType> get_notification_type_object(Td *td, DialogId) const final {
    auto sender = get_message_sender_object(td, sender_user_id_, sender_dialog_id_, "get_notification_type_object");
    return td_api::make_object<td_api::notificationTypeNewPushMessage>(
        message_id_.get(), std::move(sender), sender_name_, is_outgoing_,
        get_push_message_content_object(td, key_, arg_, photo_, document_));
  }

  StringBuilder &to_string_builder(StringBuilder &string_builder) const final {
    return string_builder << "NewPushMessageNotification[" << sender_user_id_ << "/" << sender_dialog_id_ << "/\""
                          << sender_name_ << "\", " << message_id_ << ", " << key_ << ", " << arg_ << ", " << photo_
                          << ", " << document_ << ']';
  }

  UserId sender_user_id_;
  DialogId sender_dialog_id_;
  MessageId message_id_;
  string sender_name_;
  string key_;
  string arg_;
  Photo photo_;
  Document document_;
  bool is_outgoing_;

 public:
  NotificationTypePushMessage(UserId sender_user_id, DialogId sender_dialog_id, string sender_name, bool is_outgoing,
                              MessageId message_id, string key, string arg, Photo photo, Document document)
      : sender_user_id_(sender_user_id)
      , sender_dialog_id_(sender_dialog_id)
      , message_id_(message_id)
      , sender_name_(std::move(sender_name))
      , key_(std::move(key))
      , arg_(std::move(arg))
      , photo_(std::move(photo))
      , document_(std::move(document))
      , is_outgoing_(is_outgoing) {
  }
};

unique_ptr<NotificationType> create_new_message_notification(MessageId message_id, bool show_preview) {
  return make_unique<NotificationTypeMessage>(message_id, show_preview);
}

unique_ptr<NotificationType> create_new_secret_chat_notification() {
  return make_unique<NotificationTypeSecretChat>();
}

unique_ptr<NotificationType> create_new_call_notification(CallId call_id) {
  return make_unique<NotificationTypeCall>(call_id);
}

unique_ptr<NotificationType> create_new_push_message_notification(UserId sender_user_id, DialogId sender_dialog_id,
                                                                  string sender_name, bool is_outgoing,
                                                                  MessageId message_id, string key, string arg,
                                                                  Photo photo, Document document) {
  return td::make_unique<NotificationTypePushMessage>(sender_user_id, sender_dialog_id, std::move(sender_name),
                                                      is_outgoing, message_id, std::move(key), std::move(arg),
                                                      std::move(photo), std::move(document));
}

}  // namespace td