init
Some checks failed
Docker. / Ubuntu (push) Has been cancelled
User-agent updater. / User-agent (push) Failing after 15s
Lock Threads / lock (push) Failing after 10s
Waiting for answer. / waiting-for-answer (push) Failing after 22s
Needs user action. / needs-user-action (push) Failing after 8s
Can't reproduce. / cant-reproduce (push) Failing after 8s
Close stale issues and PRs / stale (push) Has been cancelled
Some checks failed
Docker. / Ubuntu (push) Has been cancelled
User-agent updater. / User-agent (push) Failing after 15s
Lock Threads / lock (push) Failing after 10s
Waiting for answer. / waiting-for-answer (push) Failing after 22s
Needs user action. / needs-user-action (push) Failing after 8s
Can't reproduce. / cant-reproduce (push) Failing after 8s
Close stale issues and PRs / stale (push) Has been cancelled
This commit is contained in:
226
Telegram/SourceFiles/api/api_polls.cpp
Normal file
226
Telegram/SourceFiles/api/api_polls.cpp
Normal file
@@ -0,0 +1,226 @@
|
||||
/*
|
||||
This file is part of Telegram Desktop,
|
||||
the official desktop application for the Telegram messaging service.
|
||||
|
||||
For license and copyright information please follow this link:
|
||||
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
||||
*/
|
||||
#include "api/api_polls.h"
|
||||
|
||||
#include "api/api_common.h"
|
||||
#include "api/api_updates.h"
|
||||
#include "apiwrap.h"
|
||||
#include "base/random.h"
|
||||
#include "data/business/data_shortcut_messages.h"
|
||||
#include "data/data_changes.h"
|
||||
#include "data/data_histories.h"
|
||||
#include "data/data_poll.h"
|
||||
#include "data/data_session.h"
|
||||
#include "history/history.h"
|
||||
#include "history/history_item.h"
|
||||
#include "history/history_item_helpers.h" // ShouldSendSilent
|
||||
#include "main/main_session.h"
|
||||
|
||||
namespace Api {
|
||||
|
||||
Polls::Polls(not_null<ApiWrap*> api)
|
||||
: _session(&api->session())
|
||||
, _api(&api->instance()) {
|
||||
}
|
||||
|
||||
void Polls::create(
|
||||
const PollData &data,
|
||||
SendAction action,
|
||||
Fn<void()> done,
|
||||
Fn<void()> fail) {
|
||||
_session->api().sendAction(action);
|
||||
|
||||
const auto history = action.history;
|
||||
const auto peer = history->peer;
|
||||
const auto topicRootId = action.replyTo.messageId
|
||||
? action.replyTo.topicRootId
|
||||
: 0;
|
||||
const auto monoforumPeerId = action.replyTo.monoforumPeerId;
|
||||
auto sendFlags = MTPmessages_SendMedia::Flags(0);
|
||||
if (action.replyTo) {
|
||||
sendFlags |= MTPmessages_SendMedia::Flag::f_reply_to;
|
||||
}
|
||||
const auto clearCloudDraft = action.clearDraft;
|
||||
if (clearCloudDraft) {
|
||||
sendFlags |= MTPmessages_SendMedia::Flag::f_clear_draft;
|
||||
history->clearLocalDraft(topicRootId, monoforumPeerId);
|
||||
history->clearCloudDraft(topicRootId, monoforumPeerId);
|
||||
history->startSavingCloudDraft(topicRootId, monoforumPeerId);
|
||||
}
|
||||
const auto silentPost = ShouldSendSilent(peer, action.options);
|
||||
const auto starsPaid = std::min(
|
||||
peer->starsPerMessageChecked(),
|
||||
action.options.starsApproved);
|
||||
if (silentPost) {
|
||||
sendFlags |= MTPmessages_SendMedia::Flag::f_silent;
|
||||
}
|
||||
if (action.options.scheduled) {
|
||||
sendFlags |= MTPmessages_SendMedia::Flag::f_schedule_date;
|
||||
if (action.options.scheduleRepeatPeriod) {
|
||||
sendFlags |= MTPmessages_SendMedia::Flag::f_schedule_repeat_period;
|
||||
}
|
||||
}
|
||||
if (action.options.shortcutId) {
|
||||
sendFlags |= MTPmessages_SendMedia::Flag::f_quick_reply_shortcut;
|
||||
}
|
||||
if (action.options.effectId) {
|
||||
sendFlags |= MTPmessages_SendMedia::Flag::f_effect;
|
||||
}
|
||||
if (action.options.suggest) {
|
||||
sendFlags |= MTPmessages_SendMedia::Flag::f_suggested_post;
|
||||
}
|
||||
if (starsPaid) {
|
||||
action.options.starsApproved -= starsPaid;
|
||||
sendFlags |= MTPmessages_SendMedia::Flag::f_allow_paid_stars;
|
||||
}
|
||||
const auto sendAs = action.options.sendAs;
|
||||
if (sendAs) {
|
||||
sendFlags |= MTPmessages_SendMedia::Flag::f_send_as;
|
||||
}
|
||||
auto &histories = history->owner().histories();
|
||||
const auto randomId = base::RandomValue<uint64>();
|
||||
histories.sendPreparedMessage(
|
||||
history,
|
||||
action.replyTo,
|
||||
randomId,
|
||||
Data::Histories::PrepareMessage<MTPmessages_SendMedia>(
|
||||
MTP_flags(sendFlags),
|
||||
peer->input(),
|
||||
Data::Histories::ReplyToPlaceholder(),
|
||||
PollDataToInputMedia(&data),
|
||||
MTP_string(),
|
||||
MTP_long(randomId),
|
||||
MTPReplyMarkup(),
|
||||
MTPVector<MTPMessageEntity>(),
|
||||
MTP_int(action.options.scheduled),
|
||||
MTP_int(action.options.scheduleRepeatPeriod),
|
||||
(sendAs ? sendAs->input() : MTP_inputPeerEmpty()),
|
||||
Data::ShortcutIdToMTP(_session, action.options.shortcutId),
|
||||
MTP_long(action.options.effectId),
|
||||
MTP_long(starsPaid),
|
||||
SuggestToMTP(action.options.suggest)
|
||||
), [=](const MTPUpdates &result, const MTP::Response &response) {
|
||||
if (clearCloudDraft) {
|
||||
history->finishSavingCloudDraft(
|
||||
topicRootId,
|
||||
monoforumPeerId,
|
||||
UnixtimeFromMsgId(response.outerMsgId));
|
||||
}
|
||||
_session->changes().historyUpdated(
|
||||
history,
|
||||
(action.options.scheduled
|
||||
? Data::HistoryUpdate::Flag::ScheduledSent
|
||||
: Data::HistoryUpdate::Flag::MessageSent));
|
||||
done();
|
||||
}, [=](const MTP::Error &error, const MTP::Response &response) {
|
||||
if (clearCloudDraft) {
|
||||
history->finishSavingCloudDraft(
|
||||
topicRootId,
|
||||
monoforumPeerId,
|
||||
UnixtimeFromMsgId(response.outerMsgId));
|
||||
}
|
||||
fail();
|
||||
});
|
||||
}
|
||||
|
||||
void Polls::sendVotes(
|
||||
FullMsgId itemId,
|
||||
const std::vector<QByteArray> &options) {
|
||||
if (_pollVotesRequestIds.contains(itemId)) {
|
||||
return;
|
||||
}
|
||||
const auto item = _session->data().message(itemId);
|
||||
const auto media = item ? item->media() : nullptr;
|
||||
const auto poll = media ? media->poll() : nullptr;
|
||||
if (!item) {
|
||||
return;
|
||||
}
|
||||
|
||||
const auto showSending = poll && !options.empty();
|
||||
const auto hideSending = [=] {
|
||||
if (showSending) {
|
||||
if (const auto item = _session->data().message(itemId)) {
|
||||
poll->sendingVotes.clear();
|
||||
_session->data().requestItemRepaint(item);
|
||||
}
|
||||
}
|
||||
};
|
||||
if (showSending) {
|
||||
poll->sendingVotes = options;
|
||||
_session->data().requestItemRepaint(item);
|
||||
}
|
||||
|
||||
auto prepared = QVector<MTPbytes>();
|
||||
prepared.reserve(options.size());
|
||||
ranges::transform(
|
||||
options,
|
||||
ranges::back_inserter(prepared),
|
||||
[](const QByteArray &option) { return MTP_bytes(option); });
|
||||
const auto requestId = _api.request(MTPmessages_SendVote(
|
||||
item->history()->peer->input(),
|
||||
MTP_int(item->id),
|
||||
MTP_vector<MTPbytes>(prepared)
|
||||
)).done([=](const MTPUpdates &result) {
|
||||
_pollVotesRequestIds.erase(itemId);
|
||||
hideSending();
|
||||
_session->updates().applyUpdates(result);
|
||||
}).fail([=] {
|
||||
_pollVotesRequestIds.erase(itemId);
|
||||
hideSending();
|
||||
}).send();
|
||||
_pollVotesRequestIds.emplace(itemId, requestId);
|
||||
}
|
||||
|
||||
void Polls::close(not_null<HistoryItem*> item) {
|
||||
const auto itemId = item->fullId();
|
||||
if (_pollCloseRequestIds.contains(itemId)) {
|
||||
return;
|
||||
}
|
||||
const auto media = item ? item->media() : nullptr;
|
||||
const auto poll = media ? media->poll() : nullptr;
|
||||
if (!poll) {
|
||||
return;
|
||||
}
|
||||
const auto requestId = _api.request(MTPmessages_EditMessage(
|
||||
MTP_flags(MTPmessages_EditMessage::Flag::f_media),
|
||||
item->history()->peer->input(),
|
||||
MTP_int(item->id),
|
||||
MTPstring(),
|
||||
PollDataToInputMedia(poll, true),
|
||||
MTPReplyMarkup(),
|
||||
MTPVector<MTPMessageEntity>(),
|
||||
MTP_int(0), // schedule_date
|
||||
MTP_int(0), // schedule_repeat_period
|
||||
MTPint() // quick_reply_shortcut_id
|
||||
)).done([=](const MTPUpdates &result) {
|
||||
_pollCloseRequestIds.erase(itemId);
|
||||
_session->updates().applyUpdates(result);
|
||||
}).fail([=] {
|
||||
_pollCloseRequestIds.erase(itemId);
|
||||
}).send();
|
||||
_pollCloseRequestIds.emplace(itemId, requestId);
|
||||
}
|
||||
|
||||
void Polls::reloadResults(not_null<HistoryItem*> item) {
|
||||
const auto itemId = item->fullId();
|
||||
if (!item->isRegular() || _pollReloadRequestIds.contains(itemId)) {
|
||||
return;
|
||||
}
|
||||
const auto requestId = _api.request(MTPmessages_GetPollResults(
|
||||
item->history()->peer->input(),
|
||||
MTP_int(item->id)
|
||||
)).done([=](const MTPUpdates &result) {
|
||||
_pollReloadRequestIds.erase(itemId);
|
||||
_session->updates().applyUpdates(result);
|
||||
}).fail([=] {
|
||||
_pollReloadRequestIds.erase(itemId);
|
||||
}).send();
|
||||
_pollReloadRequestIds.emplace(itemId, requestId);
|
||||
}
|
||||
|
||||
} // namespace Api
|
||||
Reference in New Issue
Block a user