1 //
2 // Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 #pragma once
8 
9 #include "td/telegram/ChannelId.h"
10 #include "td/telegram/ChatId.h"
11 #include "td/telegram/SecretChatId.h"
12 #include "td/telegram/telegram_api.h"
13 #include "td/telegram/UserId.h"
14 
15 #include "td/utils/common.h"
16 #include "td/utils/StringBuilder.h"
17 
18 #include <functional>
19 #include <type_traits>
20 
21 namespace td {
22 
23 enum class DialogType : int32 { None, User, Chat, Channel, SecretChat };
24 
25 class DialogId {
26   static constexpr int64 ZERO_SECRET_CHAT_ID = -2000000000000ll;
27   static constexpr int64 ZERO_CHANNEL_ID = -1000000000000ll;
28 
29   int64 id = 0;
30 
31   static int64 get_peer_id(const tl_object_ptr<telegram_api::Peer> &peer);
32 
33  public:
34   DialogId() = default;
35 
DialogId(int64 dialog_id)36   explicit DialogId(int64 dialog_id) : id(dialog_id) {
37   }
38   template <class T, typename = std::enable_if_t<std::is_convertible<T, int64>::value>>
39   DialogId(T dialog_id) = delete;
40 
41   explicit DialogId(const tl_object_ptr<telegram_api::DialogPeer> &dialog_peer);
42   explicit DialogId(const tl_object_ptr<telegram_api::Peer> &peer);
43   explicit DialogId(UserId user_id);
44   explicit DialogId(ChatId chat_id);
45   explicit DialogId(ChannelId channel_id);
46   explicit DialogId(SecretChatId secret_chat_id);
47 
get()48   int64 get() const {
49     return id;
50   }
51 
52   bool operator==(const DialogId &other) const {
53     return id == other.id;
54   }
55 
56   bool operator!=(const DialogId &other) const {
57     return id != other.id;
58   }
59 
60   bool is_valid() const;
61 
62   DialogType get_type() const;
63 
64   UserId get_user_id() const;
65   ChatId get_chat_id() const;
66   ChannelId get_channel_id() const;
67   SecretChatId get_secret_chat_id() const;
68 
69   template <class StorerT>
store(StorerT & storer)70   void store(StorerT &storer) const {
71     storer.store_long(id);
72   }
73 
74   template <class ParserT>
parse(ParserT & parser)75   void parse(ParserT &parser) {
76     id = parser.fetch_long();
77   }
78 };
79 
80 struct DialogIdHash {
operatorDialogIdHash81   std::size_t operator()(DialogId dialog_id) const {
82     return std::hash<int64>()(dialog_id.get());
83   }
84 };
85 
86 inline StringBuilder &operator<<(StringBuilder &string_builder, DialogId dialog_id) {
87   return string_builder << "chat " << dialog_id.get();
88 }
89 
90 }  // namespace td
91