| 1092 | } |
| 1093 | |
| 1094 | int Histories::sendPreparedMessage( |
| 1095 | not_null<History*> history, |
| 1096 | FullReplyTo replyTo, |
| 1097 | uint64 randomId, |
| 1098 | Fn<PreparedMessage(not_null<History*>, FullReplyTo)> message, |
| 1099 | Fn<void(const MTPUpdates&, const MTP::Response&)> done, |
| 1100 | Fn<void(const MTP::Error&, const MTP::Response&)> fail) { |
| 1101 | if (isCreatingTopic(history, replyTo.topicRootId)) { |
| 1102 | const auto id = ++_requestAutoincrement; |
| 1103 | const auto creatingId = FullMsgId( |
| 1104 | history->peer->id, |
| 1105 | replyTo.topicRootId); |
| 1106 | auto i = _creatingTopics.find(creatingId); |
| 1107 | if (i == end(_creatingTopics)) { |
| 1108 | sendCreateTopicRequest(history, replyTo.topicRootId); |
| 1109 | i = _creatingTopics.emplace(creatingId).first; |
| 1110 | } |
| 1111 | i->second.push_back({ |
| 1112 | .randomId = randomId, |
| 1113 | .replyTo = replyTo.messageId, |
| 1114 | .message = std::move(message), |
| 1115 | .done = std::move(done), |
| 1116 | .fail = std::move(fail), |
| 1117 | .requestId = id, |
| 1118 | }); |
| 1119 | _creatingTopicRequests.emplace(id); |
| 1120 | return id; |
| 1121 | } |
| 1122 | auto realReplyTo = replyTo; |
| 1123 | const auto topicReplyToId = [&](const auto &id) { |
| 1124 | return convertTopicReplyToId(history, id); |
| 1125 | }; |
| 1126 | realReplyTo.messageId = topicReplyToId(replyTo.messageId); |
| 1127 | realReplyTo.topicRootId = topicReplyToId(replyTo.topicRootId); |
| 1128 | return v::match(message(history, realReplyTo), [&](const auto &request) { |
| 1129 | const auto type = RequestType::Send; |
| 1130 | return sendRequest(history, type, [=](Fn<void()> finish) { |
| 1131 | const auto session = &_owner->session(); |
| 1132 | const auto api = &session->api(); |
| 1133 | history->sendRequestId = api->request( |
| 1134 | base::duplicate(request) |
| 1135 | ).done([=]( |
| 1136 | const MTPUpdates &result, |
| 1137 | const MTP::Response &response) { |
| 1138 | api->applyUpdates(result, randomId); |
| 1139 | done(result, response); |
| 1140 | finish(); |
| 1141 | }).fail([=]( |
| 1142 | const MTP::Error &error, |
| 1143 | const MTP::Response &response) { |
| 1144 | fail(error, response); |
| 1145 | finish(); |
| 1146 | }).afterRequest( |
| 1147 | history->sendRequestId |
| 1148 | ).send(); |
| 1149 | return history->sendRequestId; |
| 1150 | }); |
| 1151 | }); |
no test coverage detected