aboutsummaryrefslogtreecommitdiffhomepage
path: root/td/telegram/BusinessRecipients.cpp
blob: 5b98a842b19b9fb4dc8505971277fea42593a733 (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
//
// 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/BusinessRecipients.h"

#include "td/telegram/Dependencies.h"
#include "td/telegram/DialogId.h"
#include "td/telegram/DialogManager.h"
#include "td/telegram/Td.h"
#include "td/telegram/UserManager.h"

#include "td/utils/algorithm.h"

namespace td {

BusinessRecipients::BusinessRecipients(telegram_api::object_ptr<telegram_api::businessRecipients> recipients)
    : user_ids_(UserId::get_user_ids(recipients->users_, true))
    , existing_chats_(recipients->existing_chats_)
    , new_chats_(recipients->new_chats_)
    , contacts_(recipients->contacts_)
    , non_contacts_(recipients->non_contacts_)
    , exclude_selected_(recipients->exclude_selected_) {
}

BusinessRecipients::BusinessRecipients(telegram_api::object_ptr<telegram_api::businessBotRecipients> recipients)
    : user_ids_(UserId::get_user_ids(recipients->users_, true))
    , excluded_user_ids_(UserId::get_user_ids(recipients->exclude_users_, true))
    , existing_chats_(recipients->existing_chats_)
    , new_chats_(recipients->new_chats_)
    , contacts_(recipients->contacts_)
    , non_contacts_(recipients->non_contacts_)
    , exclude_selected_(recipients->exclude_selected_) {
}

BusinessRecipients::BusinessRecipients(td_api::object_ptr<td_api::businessRecipients> recipients, bool allow_excluded) {
  if (recipients == nullptr) {
    return;
  }
  for (auto chat_id : recipients->chat_ids_) {
    DialogId dialog_id(chat_id);
    if (dialog_id.get_type() == DialogType::User) {
      user_ids_.push_back(dialog_id.get_user_id());
    }
  }
  if (allow_excluded) {
    for (auto chat_id : recipients->excluded_chat_ids_) {
      DialogId dialog_id(chat_id);
      if (dialog_id.get_type() == DialogType::User) {
        excluded_user_ids_.push_back(dialog_id.get_user_id());
      }
    }
    if (recipients->exclude_selected_) {
      append(user_ids_, std::move(excluded_user_ids_));
      reset_to_empty(excluded_user_ids_);
    }
  }
  existing_chats_ = recipients->select_existing_chats_;
  new_chats_ = recipients->select_new_chats_;
  contacts_ = recipients->select_contacts_;
  non_contacts_ = recipients->select_non_contacts_;
  exclude_selected_ = recipients->exclude_selected_;
}

td_api::object_ptr<td_api::businessRecipients> BusinessRecipients::get_business_recipients_object(Td *td) const {
  vector<int64> chat_ids;
  for (auto user_id : user_ids_) {
    DialogId dialog_id(user_id);
    td->dialog_manager_->force_create_dialog(dialog_id, "get_business_recipients_object 1", true);
    CHECK(td->dialog_manager_->have_dialog_force(dialog_id, "get_business_recipients_object 1"));
    chat_ids.push_back(td->dialog_manager_->get_chat_id_object(dialog_id, "businessRecipients 1"));
  }
  vector<int64> excluded_chat_ids;
  for (auto user_id : excluded_user_ids_) {
    DialogId dialog_id(user_id);
    td->dialog_manager_->force_create_dialog(dialog_id, "get_business_recipients_object 2", true);
    CHECK(td->dialog_manager_->have_dialog_force(dialog_id, "get_business_recipients_object 2"));
    excluded_chat_ids.push_back(td->dialog_manager_->get_chat_id_object(dialog_id, "businessRecipients 2"));
  }
  return td_api::make_object<td_api::businessRecipients>(std::move(chat_ids), std::move(excluded_chat_ids),
                                                         existing_chats_, new_chats_, contacts_, non_contacts_,
                                                         exclude_selected_);
}

telegram_api::object_ptr<telegram_api::inputBusinessRecipients> BusinessRecipients::get_input_business_recipients(
    Td *td) const {
  int32 flags = 0;
  auto input_users = td->user_manager_->get_input_users_force(user_ids_);
  if (!input_users.empty()) {
    flags |= telegram_api::inputBusinessRecipients::USERS_MASK;
  }
  return telegram_api::make_object<telegram_api::inputBusinessRecipients>(
      flags, existing_chats_, new_chats_, contacts_, non_contacts_, exclude_selected_, std::move(input_users));
}

telegram_api::object_ptr<telegram_api::inputBusinessBotRecipients>
BusinessRecipients::get_input_business_bot_recipients(Td *td) const {
  int32 flags = 0;
  auto input_users = td->user_manager_->get_input_users_force(user_ids_);
  if (!input_users.empty()) {
    flags |= telegram_api::inputBusinessBotRecipients::USERS_MASK;
  }
  auto excluded_input_users = td->user_manager_->get_input_users_force(excluded_user_ids_);
  if (!excluded_input_users.empty()) {
    flags |= telegram_api::inputBusinessBotRecipients::EXCLUDE_USERS_MASK;
  }
  return telegram_api::make_object<telegram_api::inputBusinessBotRecipients>(
      flags, existing_chats_, new_chats_, contacts_, non_contacts_, exclude_selected_, std::move(input_users),
      std::move(excluded_input_users));
}

void BusinessRecipients::add_dependencies(Dependencies &dependencies) const {
  for (auto user_id : user_ids_) {
    dependencies.add(user_id);
  }
  for (auto user_id : excluded_user_ids_) {
    dependencies.add(user_id);
  }
}

bool operator==(const BusinessRecipients &lhs, const BusinessRecipients &rhs) {
  return lhs.user_ids_ == rhs.user_ids_ && lhs.excluded_user_ids_ == rhs.excluded_user_ids_ &&
         lhs.existing_chats_ == rhs.existing_chats_ && lhs.new_chats_ == rhs.new_chats_ &&
         lhs.contacts_ == rhs.contacts_ && lhs.non_contacts_ == rhs.non_contacts_ &&
         lhs.exclude_selected_ == rhs.exclude_selected_;
}

StringBuilder &operator<<(StringBuilder &string_builder, const BusinessRecipients &recipients) {
  return string_builder << "received by " << (recipients.exclude_selected_ ? "all private chats except " : "")
                        << recipients.user_ids_ << (recipients.contacts_ ? ", contacts " : "")
                        << (recipients.non_contacts_ ? ", non-contacts " : "")
                        << (recipients.existing_chats_ ? ", existing chats " : "")
                        << (recipients.new_chats_ ? ", new chats " : "");
}

}  // namespace td