Send dice emoji as Dice media.

This commit is contained in:
John Preston 2020-03-06 16:12:03 +04:00
parent c279986493
commit f8cc134bd6
6 changed files with 124 additions and 10 deletions

View File

@ -198,4 +198,109 @@ void SendExistingPhoto(
Data::FileOrigin());
}
bool SendDice(Api::MessageToSend &message) {
static const auto kDiceString = QString::fromUtf8("\xF0\x9F\x8E\xB2");
if (message.textWithTags.text != kDiceString) {
return false;
}
const auto history = message.action.history;
const auto peer = history->peer;
const auto session = &history->session();
const auto api = &session->api();
message.textWithTags = TextWithTags();
message.action.clearDraft = false;
message.action.generateLocal = true;
api->sendAction(message.action);
const auto newId = FullMsgId(
peerToChannel(peer->id),
session->data().nextLocalMessageId());
const auto randomId = rand_value<uint64>();
auto &histories = history->owner().histories();
auto flags = NewMessageFlags(peer) | MTPDmessage::Flag::f_media;
auto clientFlags = NewMessageClientFlags();
auto sendFlags = MTPmessages_SendMedia::Flags(0);
if (message.action.replyTo) {
flags |= MTPDmessage::Flag::f_reply_to_msg_id;
sendFlags |= MTPmessages_SendMedia::Flag::f_reply_to_msg_id;
}
const auto channelPost = peer->isChannel() && !peer->isMegagroup();
const auto silentPost = message.action.options.silent
|| (channelPost && session->data().notifySilentPosts(peer));
if (channelPost) {
flags |= MTPDmessage::Flag::f_views;
flags |= MTPDmessage::Flag::f_post;
}
if (!channelPost) {
flags |= MTPDmessage::Flag::f_from_id;
} else if (peer->asChannel()->addsSignature()) {
flags |= MTPDmessage::Flag::f_post_author;
}
if (silentPost) {
sendFlags |= MTPmessages_SendMedia::Flag::f_silent;
}
auto messageFromId = channelPost ? 0 : session->userId();
auto messagePostAuthor = channelPost ? session->user()->name : QString();
const auto replyTo = message.action.replyTo;
if (message.action.options.scheduled) {
flags |= MTPDmessage::Flag::f_from_scheduled;
sendFlags |= MTPmessages_SendMedia::Flag::f_schedule_date;
} else {
clientFlags |= MTPDmessage_ClientFlag::f_local_history_entry;
}
session->data().registerMessageRandomId(randomId, newId);
history->addNewMessage(
MTP_message(
MTP_flags(flags),
MTP_int(newId.msg),
MTP_int(messageFromId),
peerToMTP(history->peer->id),
MTPMessageFwdHeader(),
MTP_int(0),
MTP_int(replyTo),
MTP_int(HistoryItem::NewMessageDate(
message.action.options.scheduled)),
MTP_string(),
MTP_messageMediaDice(MTP_int(0)),
MTPReplyMarkup(),
MTP_vector<MTPMessageEntity>(),
MTP_int(1),
MTPint(),
MTP_string(messagePostAuthor),
MTPlong(),
//MTPMessageReactions(),
MTPVector<MTPRestrictionReason>()),
clientFlags,
NewMessageType::Unread);
const auto requestType = Data::Histories::RequestType::Send;
histories.sendRequest(history, requestType, [=](Fn<void()> finish) {
history->sendRequestId = api->request(MTPmessages_SendMedia(
MTP_flags(sendFlags),
peer->input,
MTP_int(replyTo),
MTP_inputMediaDice(),
MTP_string(),
MTP_long(randomId),
MTPReplyMarkup(),
MTP_vector<MTPMessageEntity>(),
MTP_int(message.action.options.scheduled)
)).done([=](const MTPUpdates &result) {
api->applyUpdates(result, randomId);
finish();
}).fail([=](const RPCError &error) {
api->sendMessageFail(error, peer, randomId, newId);
finish();
}).afterRequest(history->sendRequestId
).send();
return history->sendRequestId;
});
return true;
}
} // namespace Api

View File

@ -8,6 +8,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#pragma once
class History;
class PhotoData;
class DocumentData;
namespace Api {
@ -22,4 +23,6 @@ void SendExistingPhoto(
Api::MessageToSend &&message,
not_null<PhotoData*> photo);
[[nodiscard]] bool SendDice(Api::MessageToSend &message);
} // namespace Api

View File

@ -7,6 +7,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#include "apiwrap.h"
#include "api/api_sending.h"
#include "api/api_text_entities.h"
#include "api/api_self_destruct.h"
#include "api/api_sensitive_content.h"
@ -4793,7 +4794,7 @@ void ApiWrap::sendMessage(MessageToSend &&message) {
action.generateLocal = true;
sendAction(action);
if (!peer->canWrite()) {
if (!peer->canWrite() || Api::SendDice(message)) {
return;
}
Local::saveRecentSentHashtags(textWithTags.text);

View File

@ -1360,6 +1360,7 @@ bool MediaDice::updateSentMedia(const MTPMessageMedia &media) {
return false;
}
_value = media.c_messageMediaDice().vvalue().v;
parent()->history()->owner().requestItemRepaint(parent());
return true;
}
@ -1368,7 +1369,7 @@ std::unique_ptr<HistoryView::Media> MediaDice::createView(
not_null<HistoryItem*> realParent) {
return std::make_unique<HistoryView::UnwrappedMedia>(
message,
std::make_unique<HistoryView::Dice>(message, _value));
std::make_unique<HistoryView::Dice>(message, this));
}
} // namespace Data

View File

@ -25,10 +25,10 @@ DocumentData *Lookup(not_null<Element*> view, int value) {
} // namespace
Dice::Dice(not_null<Element*> parent, int value)
Dice::Dice(not_null<Element*> parent, not_null<Data::MediaDice*> dice)
: _parent(parent)
, _start(parent, Lookup(parent, 0))
, _value(value) {
, _dice(dice)
, _start(parent, Lookup(parent, 0)) {
_showLastFrame = _parent->data()->Has<HistoryMessageForwarded>();
if (_showLastFrame) {
_drawingEnd = true;
@ -44,10 +44,10 @@ QSize Dice::size() {
}
void Dice::draw(Painter &p, const QRect &r, bool selected) {
if (!_end && _value) {
if (const auto document = Lookup(_parent, _value)) {
if (const auto value = _end ? 0 : _dice->diceValue()) {
if (const auto document = Lookup(_parent, value)) {
_end.emplace(_parent, document);
_end->setDiceIndex(_value);
_end->setDiceIndex(value);
_end->initSize();
}
}

View File

@ -10,11 +10,15 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "history/view/media/history_view_media_unwrapped.h"
#include "history/view/media/history_view_sticker.h"
namespace Data {
class MediaDice;
} // namespace Data
namespace HistoryView {
class Dice final : public UnwrappedMedia::Content {
public:
Dice(not_null<Element*> parent, int value);
Dice(not_null<Element*> parent, not_null<Data::MediaDice*> dice);
~Dice();
QSize size() override;
@ -34,9 +38,9 @@ public:
private:
const not_null<Element*> _parent;
const not_null<Data::MediaDice*> _dice;
std::optional<Sticker> _end;
Sticker _start;
int _value = 0;
mutable bool _showLastFrame = false;
mutable bool _drawingEnd = false;