| 328 | } |
| 329 | |
| 330 | void Polls::sendVotes( |
| 331 | FullMsgId itemId, |
| 332 | const std::vector<QByteArray> &options) { |
| 333 | if (_pollVotesRequestIds.contains(itemId)) { |
| 334 | return; |
| 335 | } |
| 336 | const auto item = _session->data().message(itemId); |
| 337 | const auto media = item ? item->media() : nullptr; |
| 338 | const auto poll = media ? media->poll() : nullptr; |
| 339 | if (!item) { |
| 340 | return; |
| 341 | } |
| 342 | const auto peer = item->history()->peer; |
| 343 | |
| 344 | const auto showSending = poll && !options.empty(); |
| 345 | const auto hideSending = [=] { |
| 346 | if (showSending) { |
| 347 | if (const auto item = _session->data().message(itemId)) { |
| 348 | poll->sendingVotes.clear(); |
| 349 | _session->data().requestItemRepaint(item); |
| 350 | } |
| 351 | } |
| 352 | }; |
| 353 | if (showSending) { |
| 354 | poll->sendingVotes = options; |
| 355 | _session->data().requestItemRepaint(item); |
| 356 | } else if (poll && options.empty() && poll->voted()) { |
| 357 | for (auto &answer : poll->answers) { |
| 358 | answer.chosen = false; |
| 359 | } |
| 360 | ++poll->version; |
| 361 | _session->data().notifyPollUpdateDelayed(poll); |
| 362 | } |
| 363 | |
| 364 | auto prepared = QVector<MTPbytes>(); |
| 365 | prepared.reserve(options.size()); |
| 366 | ranges::transform( |
| 367 | options, |
| 368 | ranges::back_inserter(prepared), |
| 369 | [](const QByteArray &option) { return MTP_bytes(option); }); |
| 370 | const auto requestId = _api.request(MTPmessages_SendVote( |
| 371 | peer->input(), |
| 372 | MTP_int(item->id), |
| 373 | MTP_vector<MTPbytes>(prepared) |
| 374 | )).done([=](const MTPUpdates &result) { |
| 375 | _pollVotesRequestIds.erase(itemId); |
| 376 | hideSending(); |
| 377 | if (poll) { |
| 378 | if (poll->voteRestriction() != PollData::VoteRestriction::None) { |
| 379 | poll->setVoteRestriction(PollData::VoteRestriction::None); |
| 380 | _session->data().notifyPollUpdateDelayed(poll); |
| 381 | } |
| 382 | } |
| 383 | _session->updates().applyUpdates(result); |
| 384 | }).fail([=](const MTP::Error &error) { |
| 385 | _pollVotesRequestIds.erase(itemId); |
| 386 | hideSending(); |
| 387 | if (poll) { |
no test coverage detected