2019-07-17 14:34:39 +00:00
|
|
|
/*
|
|
|
|
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_sending.h"
|
|
|
|
|
2019-09-16 11:14:06 +00:00
|
|
|
#include "api/api_text_entities.h"
|
2019-07-17 14:34:39 +00:00
|
|
|
#include "base/unixtime.h"
|
|
|
|
#include "data/data_document.h"
|
|
|
|
#include "data/data_photo.h"
|
|
|
|
#include "data/data_channel.h" // ChannelData::addsSignature.
|
2019-09-13 06:06:02 +00:00
|
|
|
#include "data/data_user.h" // UserData::name
|
2019-07-17 14:34:39 +00:00
|
|
|
#include "data/data_session.h"
|
|
|
|
#include "data/data_file_origin.h"
|
2020-02-21 10:29:48 +00:00
|
|
|
#include "data/data_histories.h"
|
2020-06-12 14:09:04 +00:00
|
|
|
#include "data/data_changes.h"
|
2020-06-08 17:24:36 +00:00
|
|
|
#include "data/stickers/data_stickers.h"
|
2019-07-17 14:34:39 +00:00
|
|
|
#include "history/history.h"
|
|
|
|
#include "history/history_message.h" // NewMessageFlags.
|
2019-08-12 12:11:34 +00:00
|
|
|
#include "chat_helpers/message_field.h" // ConvertTextTagsToEntities.
|
2020-10-14 13:01:07 +00:00
|
|
|
#include "chat_helpers/stickers_dice_pack.h" // DicePacks::kDiceString.
|
2019-07-17 14:34:39 +00:00
|
|
|
#include "ui/text/text_entity.h" // TextWithEntities.
|
2020-10-10 09:15:37 +00:00
|
|
|
#include "ui/item_text_options.h" // Ui::ItemTextOptions.
|
2019-07-24 11:45:24 +00:00
|
|
|
#include "main/main_session.h"
|
2020-04-20 09:45:59 +00:00
|
|
|
#include "main/main_account.h"
|
|
|
|
#include "main/main_app_config.h"
|
2020-06-12 14:09:04 +00:00
|
|
|
#include "storage/localimageloader.h"
|
|
|
|
#include "storage/file_upload.h"
|
2019-07-17 14:34:39 +00:00
|
|
|
#include "mainwidget.h"
|
|
|
|
#include "apiwrap.h"
|
2019-09-13 06:06:02 +00:00
|
|
|
#include "app.h"
|
2019-07-17 14:34:39 +00:00
|
|
|
|
|
|
|
namespace Api {
|
|
|
|
namespace {
|
|
|
|
|
2020-05-17 09:09:20 +00:00
|
|
|
void InnerFillMessagePostFlags(
|
|
|
|
const Api::SendOptions &options,
|
|
|
|
not_null<PeerData*> peer,
|
|
|
|
MTPDmessage::Flags &flags) {
|
2020-09-10 11:19:48 +00:00
|
|
|
const auto anonymousPost = peer->amAnonymous();
|
|
|
|
if (!anonymousPost) {
|
2020-05-17 09:09:20 +00:00
|
|
|
flags |= MTPDmessage::Flag::f_from_id;
|
|
|
|
return;
|
2020-09-10 11:19:48 +00:00
|
|
|
} else if (peer->asMegagroup()) {
|
|
|
|
return;
|
2020-05-17 09:09:20 +00:00
|
|
|
}
|
|
|
|
flags |= MTPDmessage::Flag::f_post;
|
|
|
|
// Don't display views and author of a new post when it's scheduled.
|
|
|
|
if (options.scheduled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
flags |= MTPDmessage::Flag::f_views;
|
|
|
|
if (peer->asChannel()->addsSignature()) {
|
|
|
|
flags |= MTPDmessage::Flag::f_post_author;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-17 14:34:39 +00:00
|
|
|
template <typename MediaData>
|
|
|
|
void SendExistingMedia(
|
2019-08-12 12:11:34 +00:00
|
|
|
Api::MessageToSend &&message,
|
2019-07-17 14:34:39 +00:00
|
|
|
not_null<MediaData*> media,
|
2019-09-30 10:25:09 +00:00
|
|
|
Fn<MTPInputMedia()> inputMedia,
|
2019-08-12 12:11:34 +00:00
|
|
|
Data::FileOrigin origin) {
|
|
|
|
const auto history = message.action.history;
|
2019-07-17 14:34:39 +00:00
|
|
|
const auto peer = history->peer;
|
|
|
|
const auto session = &history->session();
|
|
|
|
const auto api = &session->api();
|
|
|
|
|
2019-08-12 12:11:34 +00:00
|
|
|
message.action.clearDraft = false;
|
|
|
|
message.action.generateLocal = true;
|
|
|
|
api->sendAction(message.action);
|
2019-07-17 14:34:39 +00:00
|
|
|
|
2019-08-12 16:33:36 +00:00
|
|
|
const auto newId = FullMsgId(
|
|
|
|
peerToChannel(peer->id),
|
|
|
|
session->data().nextLocalMessageId());
|
2019-07-17 14:34:39 +00:00
|
|
|
const auto randomId = rand_value<uint64>();
|
|
|
|
|
|
|
|
auto flags = NewMessageFlags(peer) | MTPDmessage::Flag::f_media;
|
2019-08-09 19:19:23 +00:00
|
|
|
auto clientFlags = NewMessageClientFlags();
|
2019-07-17 14:34:39 +00:00
|
|
|
auto sendFlags = MTPmessages_SendMedia::Flags(0);
|
2019-08-12 12:11:34 +00:00
|
|
|
if (message.action.replyTo) {
|
2020-09-01 06:44:18 +00:00
|
|
|
flags |= MTPDmessage::Flag::f_reply_to;
|
2019-07-17 14:34:39 +00:00
|
|
|
sendFlags |= MTPmessages_SendMedia::Flag::f_reply_to_msg_id;
|
|
|
|
}
|
2020-09-10 11:19:48 +00:00
|
|
|
const auto anonymousPost = peer->amAnonymous();
|
2020-11-19 13:23:51 +00:00
|
|
|
const auto silentPost = ShouldSendSilent(peer, message.action.options);
|
2020-05-17 09:09:20 +00:00
|
|
|
InnerFillMessagePostFlags(message.action.options, peer, flags);
|
2019-07-17 14:34:39 +00:00
|
|
|
if (silentPost) {
|
|
|
|
sendFlags |= MTPmessages_SendMedia::Flag::f_silent;
|
|
|
|
}
|
2020-09-10 11:19:48 +00:00
|
|
|
auto messageFromId = anonymousPost ? 0 : session->userPeerId();
|
|
|
|
auto messagePostAuthor = peer->isBroadcast() ? session->user()->name : QString();
|
2019-07-17 14:34:39 +00:00
|
|
|
|
2019-08-12 12:11:34 +00:00
|
|
|
auto caption = TextWithEntities{
|
|
|
|
message.textWithTags.text,
|
2019-09-16 11:14:06 +00:00
|
|
|
TextUtilities::ConvertTextTagsToEntities(message.textWithTags.tags)
|
2019-08-12 12:11:34 +00:00
|
|
|
};
|
2019-07-17 14:34:39 +00:00
|
|
|
TextUtilities::Trim(caption);
|
2019-09-16 11:14:06 +00:00
|
|
|
auto sentEntities = EntitiesToMTP(
|
2020-06-08 08:03:45 +00:00
|
|
|
session,
|
2019-07-17 14:34:39 +00:00
|
|
|
caption.entities,
|
2019-09-16 11:14:06 +00:00
|
|
|
ConvertOption::SkipLocal);
|
2019-07-17 14:34:39 +00:00
|
|
|
if (!sentEntities.v.isEmpty()) {
|
|
|
|
sendFlags |= MTPmessages_SendMedia::Flag::f_entities;
|
|
|
|
}
|
2019-08-12 12:11:34 +00:00
|
|
|
const auto replyTo = message.action.replyTo;
|
2019-07-17 14:34:39 +00:00
|
|
|
const auto captionText = caption.text;
|
|
|
|
|
2019-08-12 16:33:36 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-07-17 14:34:39 +00:00
|
|
|
session->data().registerMessageRandomId(randomId, newId);
|
|
|
|
|
|
|
|
history->addNewLocalMessage(
|
|
|
|
newId.msg,
|
|
|
|
flags,
|
2019-08-09 19:19:23 +00:00
|
|
|
clientFlags,
|
2019-07-17 14:34:39 +00:00
|
|
|
0,
|
|
|
|
replyTo,
|
2019-08-12 16:33:36 +00:00
|
|
|
HistoryItem::NewMessageDate(message.action.options.scheduled),
|
2019-07-17 14:34:39 +00:00
|
|
|
messageFromId,
|
|
|
|
messagePostAuthor,
|
|
|
|
media,
|
|
|
|
caption,
|
|
|
|
MTPReplyMarkup());
|
|
|
|
|
2020-07-06 11:51:58 +00:00
|
|
|
auto performRequest = [=](const auto &repeatRequest) -> void {
|
2020-02-21 10:29:48 +00:00
|
|
|
auto &histories = history->owner().histories();
|
|
|
|
const auto requestType = Data::Histories::RequestType::Send;
|
|
|
|
histories.sendRequest(history, requestType, [=](Fn<void()> finish) {
|
|
|
|
const auto usedFileReference = media->fileReference();
|
|
|
|
history->sendRequestId = api->request(MTPmessages_SendMedia(
|
|
|
|
MTP_flags(sendFlags),
|
|
|
|
peer->input,
|
|
|
|
MTP_int(replyTo),
|
|
|
|
inputMedia(),
|
|
|
|
MTP_string(captionText),
|
|
|
|
MTP_long(randomId),
|
|
|
|
MTPReplyMarkup(),
|
|
|
|
sentEntities,
|
|
|
|
MTP_int(message.action.options.scheduled)
|
|
|
|
)).done([=](const MTPUpdates &result) {
|
|
|
|
api->applyUpdates(result, randomId);
|
|
|
|
finish();
|
|
|
|
}).fail([=](const RPCError &error) {
|
2020-07-06 11:51:58 +00:00
|
|
|
if (error.code() == 400
|
|
|
|
&& error.type().startsWith(qstr("FILE_REFERENCE_"))) {
|
|
|
|
api->refreshFileReference(origin, [=](const auto &result) {
|
|
|
|
if (media->fileReference() != usedFileReference) {
|
|
|
|
repeatRequest(repeatRequest);
|
|
|
|
} else {
|
|
|
|
api->sendMessageFail(error, peer, randomId, newId);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
api->sendMessageFail(error, peer, randomId, newId);
|
|
|
|
}
|
2020-02-21 10:29:48 +00:00
|
|
|
finish();
|
|
|
|
}).afterRequest(history->sendRequestId
|
|
|
|
).send();
|
|
|
|
return history->sendRequestId;
|
|
|
|
});
|
2019-07-17 14:34:39 +00:00
|
|
|
};
|
2020-07-06 11:51:58 +00:00
|
|
|
performRequest(performRequest);
|
2019-07-17 14:34:39 +00:00
|
|
|
|
2020-06-12 12:12:34 +00:00
|
|
|
api->finishForwarding(message.action);
|
2019-07-17 14:34:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
void SendExistingDocument(
|
2019-08-12 12:11:34 +00:00
|
|
|
Api::MessageToSend &&message,
|
|
|
|
not_null<DocumentData*> document) {
|
2019-09-30 10:25:09 +00:00
|
|
|
const auto inputMedia = [=] {
|
|
|
|
return MTP_inputMediaDocument(
|
|
|
|
MTP_flags(0),
|
|
|
|
document->mtpInput(),
|
2020-12-22 13:48:50 +00:00
|
|
|
MTPint(), // ttl_seconds
|
|
|
|
MTPstring()); // query
|
2019-09-30 10:25:09 +00:00
|
|
|
};
|
2019-07-17 14:34:39 +00:00
|
|
|
SendExistingMedia(
|
2019-08-12 12:11:34 +00:00
|
|
|
std::move(message),
|
2019-07-17 14:34:39 +00:00
|
|
|
document,
|
2019-09-30 10:25:09 +00:00
|
|
|
inputMedia,
|
2019-08-12 12:11:34 +00:00
|
|
|
document->stickerOrGifOrigin());
|
2019-07-17 14:34:39 +00:00
|
|
|
|
|
|
|
if (document->sticker()) {
|
2020-06-12 12:12:34 +00:00
|
|
|
document->owner().stickers().incrementSticker(document);
|
2019-07-17 14:34:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SendExistingPhoto(
|
2019-08-12 12:11:34 +00:00
|
|
|
Api::MessageToSend &&message,
|
|
|
|
not_null<PhotoData*> photo) {
|
2019-09-30 10:25:09 +00:00
|
|
|
const auto inputMedia = [=] {
|
|
|
|
return MTP_inputMediaPhoto(
|
|
|
|
MTP_flags(0),
|
|
|
|
photo->mtpInput(),
|
|
|
|
MTPint());
|
|
|
|
};
|
2019-07-17 14:34:39 +00:00
|
|
|
SendExistingMedia(
|
2019-08-12 12:11:34 +00:00
|
|
|
std::move(message),
|
2019-07-17 14:34:39 +00:00
|
|
|
photo,
|
2019-09-30 10:25:09 +00:00
|
|
|
inputMedia,
|
2019-08-12 12:11:34 +00:00
|
|
|
Data::FileOrigin());
|
2019-07-17 14:34:39 +00:00
|
|
|
}
|
|
|
|
|
2020-03-06 12:12:03 +00:00
|
|
|
bool SendDice(Api::MessageToSend &message) {
|
2020-04-06 09:07:49 +00:00
|
|
|
const auto full = message.textWithTags.text.midRef(0).trimmed();
|
2020-04-20 09:45:59 +00:00
|
|
|
auto length = 0;
|
|
|
|
if (!Ui::Emoji::Find(full.data(), full.data() + full.size(), &length)
|
|
|
|
|| length != full.size()) {
|
2020-03-06 12:12:03 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-04-20 09:45:59 +00:00
|
|
|
auto &account = message.action.history->session().account();
|
|
|
|
auto &config = account.appConfig();
|
|
|
|
static const auto hardcoded = std::vector<QString>{
|
2020-10-14 13:01:07 +00:00
|
|
|
Stickers::DicePacks::kDiceString,
|
|
|
|
Stickers::DicePacks::kDartString,
|
|
|
|
Stickers::DicePacks::kSlotString,
|
2020-10-14 15:12:24 +00:00
|
|
|
Stickers::DicePacks::kFballString,
|
|
|
|
Stickers::DicePacks::kFballString + QChar(0xFE0F),
|
|
|
|
Stickers::DicePacks::kBballString,
|
2020-04-20 09:45:59 +00:00
|
|
|
};
|
|
|
|
const auto list = config.get<std::vector<QString>>(
|
|
|
|
"emojies_send_dice",
|
|
|
|
hardcoded);
|
2020-04-06 09:07:49 +00:00
|
|
|
const auto emoji = full.toString();
|
2020-04-20 09:45:59 +00:00
|
|
|
if (!ranges::contains(list, emoji)) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-03-06 12:12:03 +00:00
|
|
|
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) {
|
2020-09-01 06:44:18 +00:00
|
|
|
flags |= MTPDmessage::Flag::f_reply_to;
|
2020-03-06 12:12:03 +00:00
|
|
|
sendFlags |= MTPmessages_SendMedia::Flag::f_reply_to_msg_id;
|
|
|
|
}
|
2020-09-01 06:44:18 +00:00
|
|
|
const auto replyHeader = NewMessageReplyHeader(message.action);
|
2020-09-10 11:19:48 +00:00
|
|
|
const auto anonymousPost = peer->amAnonymous();
|
2020-11-19 13:23:51 +00:00
|
|
|
const auto silentPost = ShouldSendSilent(peer, message.action.options);
|
2020-05-17 09:09:20 +00:00
|
|
|
InnerFillMessagePostFlags(message.action.options, peer, flags);
|
2020-03-06 12:12:03 +00:00
|
|
|
if (silentPost) {
|
|
|
|
sendFlags |= MTPmessages_SendMedia::Flag::f_silent;
|
|
|
|
}
|
2020-09-10 11:19:48 +00:00
|
|
|
auto messageFromId = anonymousPost ? 0 : session->userPeerId();
|
|
|
|
auto messagePostAuthor = peer->isBroadcast() ? session->user()->name : QString();
|
2020-03-06 12:12:03 +00:00
|
|
|
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);
|
|
|
|
|
2020-09-01 06:44:18 +00:00
|
|
|
const auto views = 1;
|
|
|
|
const auto forwards = 0;
|
2020-03-06 12:12:03 +00:00
|
|
|
history->addNewMessage(
|
|
|
|
MTP_message(
|
|
|
|
MTP_flags(flags),
|
|
|
|
MTP_int(newId.msg),
|
2020-09-01 06:44:18 +00:00
|
|
|
peerToMTP(messageFromId),
|
2020-03-06 12:12:03 +00:00
|
|
|
peerToMTP(history->peer->id),
|
|
|
|
MTPMessageFwdHeader(),
|
2020-09-01 06:44:18 +00:00
|
|
|
MTPint(), // via_bot_id
|
|
|
|
replyHeader,
|
2020-03-06 12:12:03 +00:00
|
|
|
MTP_int(HistoryItem::NewMessageDate(
|
|
|
|
message.action.options.scheduled)),
|
|
|
|
MTP_string(),
|
2020-04-06 09:07:49 +00:00
|
|
|
MTP_messageMediaDice(MTP_int(0), MTP_string(emoji)),
|
2020-03-06 12:12:03 +00:00
|
|
|
MTPReplyMarkup(),
|
|
|
|
MTP_vector<MTPMessageEntity>(),
|
2020-09-01 06:44:18 +00:00
|
|
|
MTP_int(views),
|
|
|
|
MTP_int(forwards),
|
|
|
|
MTPMessageReplies(),
|
|
|
|
MTPint(), // edit_date
|
2020-03-06 12:12:03 +00:00
|
|
|
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),
|
2020-04-06 09:07:49 +00:00
|
|
|
MTP_inputMediaDice(MTP_string(emoji)),
|
2020-03-06 12:12:03 +00:00
|
|
|
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;
|
|
|
|
});
|
2020-06-12 12:12:34 +00:00
|
|
|
api->finishForwarding(message.action);
|
2020-03-06 12:12:03 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-05-17 09:09:20 +00:00
|
|
|
void FillMessagePostFlags(
|
|
|
|
const Api::SendAction &action,
|
|
|
|
not_null<PeerData*> peer,
|
|
|
|
MTPDmessage::Flags &flags) {
|
|
|
|
InnerFillMessagePostFlags(action.options, peer, flags);
|
|
|
|
}
|
|
|
|
|
2020-06-12 14:09:04 +00:00
|
|
|
void SendConfirmedFile(
|
|
|
|
not_null<Main::Session*> session,
|
|
|
|
const std::shared_ptr<FileLoadResult> &file,
|
|
|
|
const std::optional<FullMsgId> &oldId) {
|
|
|
|
const auto isEditing = oldId.has_value();
|
|
|
|
const auto channelId = peerToChannel(file->to.peer);
|
|
|
|
|
|
|
|
const auto newId = oldId.value_or(
|
|
|
|
FullMsgId(channelId, session->data().nextLocalMessageId()));
|
|
|
|
auto groupId = file->album ? file->album->groupId : uint64(0);
|
|
|
|
if (file->album) {
|
|
|
|
const auto proj = [](const SendingAlbum::Item &item) {
|
|
|
|
return item.taskId;
|
|
|
|
};
|
|
|
|
const auto it = ranges::find(file->album->items, file->taskId, proj);
|
|
|
|
Assert(it != file->album->items.end());
|
|
|
|
|
|
|
|
it->msgId = newId;
|
|
|
|
}
|
|
|
|
file->edit = isEditing;
|
|
|
|
session->uploader().upload(newId, file);
|
|
|
|
|
|
|
|
const auto itemToEdit = isEditing
|
|
|
|
? session->data().message(newId)
|
|
|
|
: nullptr;
|
|
|
|
|
|
|
|
const auto history = session->data().history(file->to.peer);
|
|
|
|
const auto peer = history->peer;
|
|
|
|
|
|
|
|
auto action = Api::SendAction(history);
|
|
|
|
action.options = file->to.options;
|
|
|
|
action.clearDraft = false;
|
|
|
|
action.replyTo = file->to.replyTo;
|
|
|
|
action.generateLocal = true;
|
|
|
|
session->api().sendAction(action);
|
|
|
|
|
|
|
|
auto caption = TextWithEntities{
|
|
|
|
file->caption.text,
|
|
|
|
TextUtilities::ConvertTextTagsToEntities(file->caption.tags)
|
|
|
|
};
|
|
|
|
const auto prepareFlags = Ui::ItemTextOptions(
|
|
|
|
history,
|
|
|
|
session->user()).flags;
|
|
|
|
TextUtilities::PrepareForSending(caption, prepareFlags);
|
|
|
|
TextUtilities::Trim(caption);
|
|
|
|
auto localEntities = Api::EntitiesToMTP(session, caption.entities);
|
|
|
|
|
|
|
|
if (itemToEdit) {
|
|
|
|
if (const auto id = itemToEdit->groupId()) {
|
|
|
|
groupId = id.value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto flags = (isEditing ? MTPDmessage::Flags() : NewMessageFlags(peer))
|
|
|
|
| MTPDmessage::Flag::f_entities
|
|
|
|
| MTPDmessage::Flag::f_media;
|
|
|
|
auto clientFlags = NewMessageClientFlags();
|
|
|
|
if (file->to.replyTo) {
|
2020-09-01 06:44:18 +00:00
|
|
|
flags |= MTPDmessage::Flag::f_reply_to;
|
2020-06-12 14:09:04 +00:00
|
|
|
}
|
2020-09-01 06:44:18 +00:00
|
|
|
const auto replyHeader = NewMessageReplyHeader(action);
|
2020-09-10 11:19:48 +00:00
|
|
|
const auto anonymousPost = peer->amAnonymous();
|
2020-11-19 13:23:51 +00:00
|
|
|
const auto silentPost = ShouldSendSilent(peer, file->to.options);
|
2020-06-12 14:09:04 +00:00
|
|
|
Api::FillMessagePostFlags(action, peer, flags);
|
|
|
|
if (silentPost) {
|
|
|
|
flags |= MTPDmessage::Flag::f_silent;
|
|
|
|
}
|
|
|
|
if (groupId) {
|
|
|
|
flags |= MTPDmessage::Flag::f_grouped_id;
|
|
|
|
}
|
|
|
|
if (file->to.options.scheduled) {
|
|
|
|
flags |= MTPDmessage::Flag::f_from_scheduled;
|
2020-05-30 08:47:32 +00:00
|
|
|
// Scheduled messages have no the 'edited' badge.
|
|
|
|
flags |= MTPDmessage::Flag::f_edit_hide;
|
2020-06-12 14:09:04 +00:00
|
|
|
} else {
|
|
|
|
clientFlags |= MTPDmessage_ClientFlag::f_local_history_entry;
|
|
|
|
}
|
|
|
|
|
2020-09-10 11:19:48 +00:00
|
|
|
const auto messageFromId = anonymousPost ? 0 : session->userPeerId();
|
|
|
|
const auto messagePostAuthor = peer->isBroadcast()
|
2020-06-12 14:09:04 +00:00
|
|
|
? session->user()->name
|
|
|
|
: QString();
|
|
|
|
|
2020-09-01 06:44:18 +00:00
|
|
|
const auto views = 1;
|
|
|
|
const auto forwards = 0;
|
2020-06-12 14:09:04 +00:00
|
|
|
if (file->type == SendMediaType::Photo) {
|
|
|
|
const auto photoFlags = MTPDmessageMediaPhoto::Flag::f_photo | 0;
|
|
|
|
const auto photo = MTP_messageMediaPhoto(
|
|
|
|
MTP_flags(photoFlags),
|
|
|
|
file->photo,
|
|
|
|
MTPint());
|
|
|
|
|
|
|
|
const auto mtpMessage = MTP_message(
|
|
|
|
MTP_flags(flags),
|
|
|
|
MTP_int(newId.msg),
|
2020-09-01 06:44:18 +00:00
|
|
|
peerToMTP(messageFromId),
|
2020-06-12 14:09:04 +00:00
|
|
|
peerToMTP(file->to.peer),
|
|
|
|
MTPMessageFwdHeader(),
|
|
|
|
MTPint(),
|
2020-09-01 06:44:18 +00:00
|
|
|
replyHeader,
|
2020-06-12 14:09:04 +00:00
|
|
|
MTP_int(HistoryItem::NewMessageDate(file->to.options.scheduled)),
|
|
|
|
MTP_string(caption.text),
|
|
|
|
photo,
|
|
|
|
MTPReplyMarkup(),
|
|
|
|
localEntities,
|
2020-09-01 06:44:18 +00:00
|
|
|
MTP_int(views),
|
|
|
|
MTP_int(forwards),
|
|
|
|
MTPMessageReplies(),
|
|
|
|
MTPint(), // edit_date
|
2020-06-12 14:09:04 +00:00
|
|
|
MTP_string(messagePostAuthor),
|
|
|
|
MTP_long(groupId),
|
|
|
|
//MTPMessageReactions(),
|
|
|
|
MTPVector<MTPRestrictionReason>());
|
|
|
|
|
|
|
|
if (itemToEdit) {
|
|
|
|
itemToEdit->savePreviousMedia();
|
|
|
|
itemToEdit->applyEdition(mtpMessage.c_message());
|
|
|
|
} else {
|
|
|
|
history->addNewMessage(
|
|
|
|
mtpMessage,
|
|
|
|
clientFlags,
|
|
|
|
NewMessageType::Unread);
|
|
|
|
}
|
|
|
|
} else if (file->type == SendMediaType::File) {
|
|
|
|
const auto documentFlags = MTPDmessageMediaDocument::Flag::f_document | 0;
|
|
|
|
const auto document = MTP_messageMediaDocument(
|
|
|
|
MTP_flags(documentFlags),
|
|
|
|
file->document,
|
|
|
|
MTPint());
|
|
|
|
|
|
|
|
const auto mtpMessage = MTP_message(
|
|
|
|
MTP_flags(flags),
|
|
|
|
MTP_int(newId.msg),
|
2020-09-01 06:44:18 +00:00
|
|
|
peerToMTP(messageFromId),
|
2020-06-12 14:09:04 +00:00
|
|
|
peerToMTP(file->to.peer),
|
|
|
|
MTPMessageFwdHeader(),
|
|
|
|
MTPint(),
|
2020-09-01 06:44:18 +00:00
|
|
|
replyHeader,
|
2020-06-12 14:09:04 +00:00
|
|
|
MTP_int(HistoryItem::NewMessageDate(file->to.options.scheduled)),
|
|
|
|
MTP_string(caption.text),
|
|
|
|
document,
|
|
|
|
MTPReplyMarkup(),
|
|
|
|
localEntities,
|
2020-09-01 06:44:18 +00:00
|
|
|
MTP_int(views),
|
|
|
|
MTP_int(forwards),
|
|
|
|
MTPMessageReplies(),
|
|
|
|
MTPint(), // edit_date
|
2020-06-12 14:09:04 +00:00
|
|
|
MTP_string(messagePostAuthor),
|
|
|
|
MTP_long(groupId),
|
|
|
|
//MTPMessageReactions(),
|
|
|
|
MTPVector<MTPRestrictionReason>());
|
|
|
|
|
|
|
|
if (itemToEdit) {
|
|
|
|
itemToEdit->savePreviousMedia();
|
|
|
|
itemToEdit->applyEdition(mtpMessage.c_message());
|
|
|
|
} else {
|
|
|
|
history->addNewMessage(
|
|
|
|
mtpMessage,
|
|
|
|
clientFlags,
|
|
|
|
NewMessageType::Unread);
|
|
|
|
}
|
|
|
|
} else if (file->type == SendMediaType::Audio) {
|
|
|
|
if (!peer->isChannel() || peer->isMegagroup()) {
|
|
|
|
flags |= MTPDmessage::Flag::f_media_unread;
|
|
|
|
}
|
|
|
|
const auto documentFlags = MTPDmessageMediaDocument::Flag::f_document | 0;
|
|
|
|
const auto document = MTP_messageMediaDocument(
|
|
|
|
MTP_flags(documentFlags),
|
|
|
|
file->document,
|
|
|
|
MTPint());
|
|
|
|
history->addNewMessage(
|
|
|
|
MTP_message(
|
|
|
|
MTP_flags(flags),
|
|
|
|
MTP_int(newId.msg),
|
2020-09-01 06:44:18 +00:00
|
|
|
peerToMTP(messageFromId),
|
2020-06-12 14:09:04 +00:00
|
|
|
peerToMTP(file->to.peer),
|
|
|
|
MTPMessageFwdHeader(),
|
|
|
|
MTPint(),
|
2020-09-01 06:44:18 +00:00
|
|
|
replyHeader,
|
2020-06-12 14:09:04 +00:00
|
|
|
MTP_int(
|
|
|
|
HistoryItem::NewMessageDate(file->to.options.scheduled)),
|
|
|
|
MTP_string(caption.text),
|
|
|
|
document,
|
|
|
|
MTPReplyMarkup(),
|
|
|
|
localEntities,
|
2020-09-01 06:44:18 +00:00
|
|
|
MTP_int(views),
|
|
|
|
MTP_int(forwards),
|
|
|
|
MTPMessageReplies(),
|
|
|
|
MTPint(), // edit_date
|
2020-06-12 14:09:04 +00:00
|
|
|
MTP_string(messagePostAuthor),
|
|
|
|
MTP_long(groupId),
|
|
|
|
//MTPMessageReactions(),
|
|
|
|
MTPVector<MTPRestrictionReason>()),
|
|
|
|
clientFlags,
|
|
|
|
NewMessageType::Unread);
|
|
|
|
// Voices can't be edited.
|
|
|
|
} else {
|
|
|
|
Unexpected("Type in sendFilesConfirmed.");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isEditing) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
session->data().sendHistoryChangeNotifications();
|
2020-07-23 13:46:52 +00:00
|
|
|
if (!itemToEdit) {
|
|
|
|
session->changes().historyUpdated(
|
|
|
|
history,
|
|
|
|
(action.options.scheduled
|
|
|
|
? Data::HistoryUpdate::Flag::ScheduledSent
|
|
|
|
: Data::HistoryUpdate::Flag::MessageSent));
|
|
|
|
}
|
2020-06-12 14:09:04 +00:00
|
|
|
}
|
|
|
|
|
2019-07-17 14:34:39 +00:00
|
|
|
} // namespace Api
|