aboutsummaryrefslogtreecommitdiffhomepage
path: root/td/telegram/MessageTopic.h
blob: bed7ef41b2e7eb2434394264f225236f02cd934a (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
//
// 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)
//
#pragma once

#include "td/telegram/DialogId.h"
#include "td/telegram/ForumTopicId.h"
#include "td/telegram/MessageId.h"
#include "td/telegram/SavedMessagesTopicId.h"
#include "td/telegram/td_api.h"
#include "td/telegram/telegram_api.h"

#include "td/utils/common.h"
#include "td/utils/Status.h"
#include "td/utils/StringBuilder.h"

namespace td {

class Td;

class MessageTopic {
  enum class Type : int32 { None, Thread, Forum, Monoforum, SavedMessages };
  Type type_ = Type::None;
  DialogId dialog_id_;
  MessageId top_thread_message_id_;
  ForumTopicId forum_topic_id_;
  SavedMessagesTopicId saved_messages_topic_id_;

  friend bool operator==(const MessageTopic &lhs, const MessageTopic &rhs);

  friend StringBuilder &operator<<(StringBuilder &string_builder, const MessageTopic &message_topic);

  bool is_general_forum() const {
    return type_ == Type::Forum && forum_topic_id_ == ForumTopicId::general();
  }

 public:
  MessageTopic() = default;

  MessageTopic(Td *td, DialogId dialog_id, bool is_topic_message, MessageId top_thread_message_id,
               SavedMessagesTopicId saved_messages_topic_id);

  static MessageTopic autodetect(Td *td, DialogId dialog_id, MessageId top_thread_message_id);

  static MessageTopic thread(DialogId dialog_id, MessageId top_thread_message_id);

  static MessageTopic forum(DialogId dialog_id, ForumTopicId forum_topic_id);

  static MessageTopic monoforum(DialogId dialog_id, SavedMessagesTopicId saved_messages_topic_id);

  static MessageTopic saved_messages(DialogId dialog_id, SavedMessagesTopicId saved_messages_topic_id);

  static Result<MessageTopic> get_message_topic(Td *td, DialogId dialog_id,
                                                const td_api::object_ptr<td_api::MessageTopic> &topic);

  static Result<MessageTopic> get_send_message_topic(Td *td, DialogId dialog_id, MessageTopic &&message_topic);

  static Result<MessageTopic> get_send_message_topic(Td *td, DialogId dialog_id,
                                                     const td_api::object_ptr<td_api::MessageTopic> &topic);

  td_api::object_ptr<td_api::MessageTopic> get_message_topic_object(Td *td) const;

  bool is_empty() const {
    return type_ == Type::None;
  }

  bool is_thread() const {
    return type_ == Type::Thread;
  }

  bool is_forum() const {
    return type_ == Type::Forum;
  }

  bool is_forum_general() const {
    return type_ == Type::Forum && forum_topic_id_ == ForumTopicId::general();
  }

  bool is_monoforum() const {
    return type_ == Type::Monoforum;
  }

  bool is_saved_messages() const {
    return type_ == Type::SavedMessages;
  }

  MessageId get_top_thread_message_id() const {
    CHECK(type_ == Type::Thread);
    return top_thread_message_id_;
  }

  ForumTopicId get_forum_topic_id() const {
    CHECK(type_ == Type::Forum);
    return forum_topic_id_;
  }

  SavedMessagesTopicId get_monoforum_saved_messages_topic_id() const {
    CHECK(type_ == Type::Monoforum);
    return saved_messages_topic_id_;
  }

  MessageId get_implicit_reply_to_message_id(const Td *td) const;

  int32 get_input_top_msg_id() const {
    switch (type_) {
      case Type::Thread:
        return top_thread_message_id_.get_server_message_id().get();
      case Type::Forum:
        return forum_topic_id_.get();
      default:
        return 0;
    }
  }

  telegram_api::object_ptr<telegram_api::InputPeer> get_saved_input_peer(const Td *td) const {
    if (type_ != Type::SavedMessages && type_ != Type::Monoforum) {
      return nullptr;
    }
    auto saved_input_peer = saved_messages_topic_id_.get_input_peer(td);
    CHECK(saved_input_peer != nullptr);
    return saved_input_peer;
  }

  template <class StorerT>
  void store(StorerT &storer) const;

  template <class ParserT>
  void parse(ParserT &parser);
};

StringBuilder &operator<<(StringBuilder &string_builder, const MessageTopic &message_topic);

bool operator==(const MessageTopic &lhs, const MessageTopic &rhs);

inline bool operator!=(const MessageTopic &lhs, const MessageTopic &rhs) {
  return !(lhs == rhs);
}

}  // namespace td