tdesktop/Telegram/SourceFiles/api/api_sending.cpp

503 lines
14 KiB
C++
Raw Normal View History

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"
#include "base/random.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.
#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"
#include "data/data_histories.h"
2020-06-12 14:09:04 +00:00
#include "data/data_changes.h"
#include "data/stickers/data_stickers.h"
2019-07-17 14:34:39 +00:00
#include "history/history.h"
2022-12-14 12:15:46 +00:00
#include "history/history_item.h"
#include "history/history_item_helpers.h" // NewMessageFlags.
#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"
namespace Api {
namespace {
void InnerFillMessagePostFlags(
2021-11-18 07:03:03 +00:00
const SendOptions &options,
not_null<PeerData*> peer,
2021-07-28 11:55:02 +00:00
MessageFlags &flags) {
2020-09-10 11:19:48 +00:00
const auto anonymousPost = peer->amAnonymous();
if (!anonymousPost || options.sendAs) {
2021-07-28 11:55:02 +00:00
flags |= MessageFlag::HasFromId;
return;
2020-09-10 11:19:48 +00:00
} else if (peer->asMegagroup()) {
return;
}
2021-07-28 11:55:02 +00:00
flags |= MessageFlag::Post;
// Don't display views and author of a new post when it's scheduled.
if (options.scheduled) {
return;
}
2021-07-28 11:55:02 +00:00
flags |= MessageFlag::HasViews;
if (peer->asChannel()->addsSignature()) {
2021-07-28 11:55:02 +00:00
flags |= MessageFlag::HasPostAuthor;
}
}
2019-07-17 14:34:39 +00:00
template <typename MediaData>
void SendExistingMedia(
2021-11-18 07:03:03 +00:00
MessageToSend &&message,
2019-07-17 14:34:39 +00:00
not_null<MediaData*> media,
Fn<MTPInputMedia()> inputMedia,
Data::FileOrigin origin,
std::optional<MsgId> localMessageId) {
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();
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(
peer->id,
localMessageId
? (*localMessageId)
: session->data().nextLocalMessageId());
const auto randomId = base::RandomValue<uint64>();
2019-07-17 14:34:39 +00:00
2021-07-28 11:55:02 +00:00
auto flags = NewMessageFlags(peer);
2019-07-17 14:34:39 +00:00
auto sendFlags = MTPmessages_SendMedia::Flags(0);
if (message.action.replyTo) {
2021-07-28 11:55:02 +00:00
flags |= MessageFlag::HasReplyInfo;
sendFlags |= MTPmessages_SendMedia::Flag::f_reply_to;
2019-07-17 14:34:39 +00:00
}
2020-09-10 11:19:48 +00:00
const auto anonymousPost = peer->amAnonymous();
const auto silentPost = ShouldSendSilent(peer, message.action.options);
InnerFillMessagePostFlags(message.action.options, peer, flags);
2019-07-17 14:34:39 +00:00
if (silentPost) {
sendFlags |= MTPmessages_SendMedia::Flag::f_silent;
}
2021-11-09 15:24:13 +00:00
const auto sendAs = message.action.options.sendAs;
const auto messageFromId = sendAs
? sendAs->id
: anonymousPost
? 0
: session->userPeerId();
if (sendAs) {
sendFlags |= MTPmessages_SendMedia::Flag::f_send_as;
}
const auto messagePostAuthor = peer->isBroadcast()
2022-08-09 11:12:19 +00:00
? session->user()->name()
2021-11-09 15:24:13 +00:00
: QString();
2019-07-17 14:34:39 +00:00
auto caption = TextWithEntities{
message.textWithTags.text,
2019-09-16 11:14:06 +00:00
TextUtilities::ConvertTextTagsToEntities(message.textWithTags.tags)
};
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;
}
const auto captionText = caption.text;
2019-08-12 16:33:36 +00:00
if (message.action.options.scheduled) {
2021-07-28 11:55:02 +00:00
flags |= MessageFlag::IsOrWasScheduled;
2019-08-12 16:33:36 +00:00
sendFlags |= MTPmessages_SendMedia::Flag::f_schedule_date;
}
2019-07-17 14:34:39 +00:00
session->data().registerMessageRandomId(randomId, newId);
2021-07-28 11:55:02 +00:00
const auto viaBotId = UserId();
2019-07-17 14:34:39 +00:00
history->addNewLocalMessage(
newId.msg,
flags,
2021-07-28 11:55:02 +00:00
viaBotId,
message.action.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,
2021-10-02 11:28:21 +00:00
HistoryMessageMarkupData());
2019-07-17 14:34:39 +00:00
const auto performRequest = [=](const auto &repeatRequest) -> void {
auto &histories = history->owner().histories();
const auto usedFileReference = media->fileReference();
histories.sendPreparedMessage(
history,
message.action.replyTo,
randomId,
2022-10-04 15:34:45 +00:00
Data::Histories::PrepareMessage<MTPmessages_SendMedia>(
MTP_flags(sendFlags),
peer->input,
2022-10-04 15:34:45 +00:00
Data::Histories::ReplyToPlaceholder(),
inputMedia(),
MTP_string(captionText),
MTP_long(randomId),
MTPReplyMarkup(),
sentEntities,
2021-11-05 12:05:11 +00:00
MTP_int(message.action.options.scheduled),
2021-11-09 15:24:13 +00:00
(sendAs ? sendAs->input : MTP_inputPeerEmpty())
), [=](const MTPUpdates &result, const MTP::Response &response) {
}, [=](const MTP::Error &error, const MTP::Response &response) {
if (error.code() == 400
2022-11-26 21:20:17 +00:00
&& error.type().startsWith(u"FILE_REFERENCE_"_q)) {
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);
}
});
2019-07-17 14:34:39 +00:00
};
performRequest(performRequest);
2019-07-17 14:34:39 +00:00
api->finishForwarding(message.action);
2019-07-17 14:34:39 +00:00
}
} // namespace
void SendExistingDocument(
2021-11-18 07:03:03 +00:00
MessageToSend &&message,
not_null<DocumentData*> document,
std::optional<MsgId> localMessageId) {
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-07-17 14:34:39 +00:00
SendExistingMedia(
std::move(message),
2019-07-17 14:34:39 +00:00
document,
inputMedia,
document->stickerOrGifOrigin(),
std::move(localMessageId));
2019-07-17 14:34:39 +00:00
if (document->sticker()) {
document->owner().stickers().incrementSticker(document);
2019-07-17 14:34:39 +00:00
}
}
void SendExistingPhoto(
2021-11-18 07:03:03 +00:00
MessageToSend &&message,
not_null<PhotoData*> photo,
std::optional<MsgId> localMessageId) {
const auto inputMedia = [=] {
return MTP_inputMediaPhoto(
MTP_flags(0),
photo->mtpInput(),
MTPint());
};
2019-07-17 14:34:39 +00:00
SendExistingMedia(
std::move(message),
2019-07-17 14:34:39 +00:00
photo,
inputMedia,
Data::FileOrigin(),
std::move(localMessageId));
2019-07-17 14:34:39 +00:00
}
2021-11-18 07:03:03 +00:00
bool SendDice(MessageToSend &message) {
const auto full = QStringView(message.textWithTags.text).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()
|| !message.textWithTags.tags.isEmpty()) {
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,
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);
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(
peer->id,
2020-03-06 12:12:03 +00:00
session->data().nextLocalMessageId());
const auto randomId = base::RandomValue<uint64>();
2020-03-06 12:12:03 +00:00
auto &histories = history->owner().histories();
2021-07-28 11:55:02 +00:00
auto flags = NewMessageFlags(peer);
2020-03-06 12:12:03 +00:00
auto sendFlags = MTPmessages_SendMedia::Flags(0);
if (message.action.replyTo) {
2021-07-28 11:55:02 +00:00
flags |= MessageFlag::HasReplyInfo;
sendFlags |= MTPmessages_SendMedia::Flag::f_reply_to;
2020-03-06 12:12:03 +00:00
}
const auto replyHeader = NewMessageReplyHeader(message.action);
2020-09-10 11:19:48 +00:00
const auto anonymousPost = peer->amAnonymous();
const auto silentPost = ShouldSendSilent(peer, message.action.options);
InnerFillMessagePostFlags(message.action.options, peer, flags);
2020-03-06 12:12:03 +00:00
if (silentPost) {
sendFlags |= MTPmessages_SendMedia::Flag::f_silent;
}
2021-11-09 15:24:13 +00:00
const auto sendAs = message.action.options.sendAs;
const auto messageFromId = sendAs
? sendAs->id
: anonymousPost
? 0
: session->userPeerId();
if (sendAs) {
sendFlags |= MTPmessages_SendMedia::Flag::f_send_as;
}
const auto messagePostAuthor = peer->isBroadcast()
2022-08-09 11:12:19 +00:00
? session->user()->name()
2021-11-09 15:24:13 +00:00
: QString();
2020-03-06 12:12:03 +00:00
if (message.action.options.scheduled) {
2021-07-28 11:55:02 +00:00
flags |= MessageFlag::IsOrWasScheduled;
2020-03-06 12:12:03 +00:00
sendFlags |= MTPmessages_SendMedia::Flag::f_schedule_date;
}
session->data().registerMessageRandomId(randomId, newId);
2021-07-28 11:55:02 +00:00
const auto viaBotId = UserId();
history->addNewLocalMessage(
newId.msg,
flags,
viaBotId,
message.action.replyTo,
HistoryItem::NewMessageDate(message.action.options.scheduled),
messageFromId,
messagePostAuthor,
TextWithEntities(),
MTP_messageMediaDice(MTP_int(0), MTP_string(emoji)),
2021-10-02 11:28:21 +00:00
HistoryMessageMarkupData());
histories.sendPreparedMessage(
history,
message.action.replyTo,
randomId,
2022-10-04 15:34:45 +00:00
Data::Histories::PrepareMessage<MTPmessages_SendMedia>(
2020-03-06 12:12:03 +00:00
MTP_flags(sendFlags),
peer->input,
2022-10-04 15:34:45 +00:00
Data::Histories::ReplyToPlaceholder(),
MTP_inputMediaDice(MTP_string(emoji)),
2020-03-06 12:12:03 +00:00
MTP_string(),
MTP_long(randomId),
MTPReplyMarkup(),
MTP_vector<MTPMessageEntity>(),
2021-11-05 12:05:11 +00:00
MTP_int(message.action.options.scheduled),
2021-11-09 15:24:13 +00:00
(sendAs ? sendAs->input : MTP_inputPeerEmpty())
), [=](const MTPUpdates &result, const MTP::Response &response) {
}, [=](const MTP::Error &error, const MTP::Response &response) {
api->sendMessageFail(error, peer, randomId, newId);
2020-03-06 12:12:03 +00:00
});
api->finishForwarding(message.action);
2020-03-06 12:12:03 +00:00
return true;
}
void FillMessagePostFlags(
2021-11-18 07:03:03 +00:00
const SendAction &action,
not_null<PeerData*> peer,
2021-07-28 11:55:02 +00:00
MessageFlags &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) {
2021-07-28 11:55:02 +00:00
const auto isEditing = (file->type != SendMediaType::Audio)
&& (file->to.replaceMediaOf != 0);
const auto newId = FullMsgId(
file->to.peer,
2022-10-04 15:34:45 +00:00
(isEditing
? file->to.replaceMediaOf
2022-10-04 15:34:45 +00:00
: session->data().nextLocalMessageId()));
const auto groupId = file->album ? file->album->groupId : uint64(0);
2020-06-12 14:09:04 +00:00
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;
}
const auto itemToEdit = isEditing
? session->data().message(newId)
: nullptr;
const auto history = session->data().history(file->to.peer);
const auto peer = history->peer;
2022-10-04 15:34:45 +00:00
if (!isEditing) {
const auto histories = &session->data().histories();
file->to.replyTo.msgId = histories->convertTopicReplyToId(
2022-10-04 15:34:45 +00:00
history,
file->to.replyTo.msgId);
file->to.replyTo.topicRootId = histories->convertTopicReplyToId(
history,
file->to.replyTo.topicRootId);
2022-10-04 15:34:45 +00:00
}
session->uploader().upload(newId, file);
2021-11-09 15:24:13 +00:00
auto action = SendAction(history, file->to.options);
2020-06-12 14:09:04 +00:00
action.clearDraft = false;
action.replyTo = file->to.replyTo;
action.generateLocal = true;
action.replaceMediaOf = file->to.replaceMediaOf;
2020-06-12 14:09:04 +00:00
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);
2021-07-28 11:55:02 +00:00
auto flags = isEditing ? MessageFlags() : NewMessageFlags(peer);
2020-06-12 14:09:04 +00:00
if (file->to.replyTo) {
2021-07-28 11:55:02 +00:00
flags |= MessageFlag::HasReplyInfo;
2020-06-12 14:09:04 +00:00
}
const auto replyHeader = NewMessageReplyHeader(action);
2020-09-10 11:19:48 +00:00
const auto anonymousPost = peer->amAnonymous();
const auto silentPost = ShouldSendSilent(peer, file->to.options);
2021-07-28 11:55:02 +00:00
FillMessagePostFlags(action, peer, flags);
2020-06-12 14:09:04 +00:00
if (silentPost) {
2021-07-28 11:55:02 +00:00
flags |= MessageFlag::Silent;
2020-06-12 14:09:04 +00:00
}
if (file->to.options.scheduled) {
2021-07-28 11:55:02 +00:00
flags |= MessageFlag::IsOrWasScheduled;
// Scheduled messages have no the 'edited' badge.
2021-07-28 11:55:02 +00:00
flags |= MessageFlag::HideEdited;
2020-06-12 14:09:04 +00:00
}
if (file->type == SendMediaType::Audio) {
if (!peer->isChannel() || peer->isMegagroup()) {
flags |= MessageFlag::MediaIsUnread;
}
}
2020-06-12 14:09:04 +00:00
2021-11-09 15:24:13 +00:00
const auto messageFromId =
file->to.options.sendAs
? file->to.options.sendAs->id
: anonymousPost
? PeerId()
: session->userPeerId();
2020-09-10 11:19:48 +00:00
const auto messagePostAuthor = peer->isBroadcast()
2022-08-09 11:12:19 +00:00
? session->user()->name()
2020-06-12 14:09:04 +00:00
: QString();
const auto media = MTPMessageMedia([&] {
2021-07-28 11:55:02 +00:00
if (file->type == SendMediaType::Photo) {
2022-12-13 12:11:52 +00:00
using Flag = MTPDmessageMediaPhoto::Flag;
2021-07-28 11:55:02 +00:00
return MTP_messageMediaPhoto(
2022-12-13 12:11:52 +00:00
MTP_flags(Flag::f_photo
| (file->spoiler ? Flag::f_spoiler : Flag())),
2021-07-28 11:55:02 +00:00
file->photo,
MTPint());
} else if (file->type == SendMediaType::File) {
2022-12-13 12:11:52 +00:00
using Flag = MTPDmessageMediaDocument::Flag;
2021-07-28 11:55:02 +00:00
return MTP_messageMediaDocument(
2022-12-13 12:11:52 +00:00
MTP_flags(Flag::f_document
| (file->spoiler ? Flag::f_spoiler : Flag())),
2021-07-28 11:55:02 +00:00
file->document,
2023-05-01 06:44:57 +00:00
MTPDocument(), // alt_document
2021-07-28 11:55:02 +00:00
MTPint());
} else if (file->type == SendMediaType::Audio) {
return MTP_messageMediaDocument(
MTP_flags(MTPDmessageMediaDocument::Flag::f_document),
file->document,
2023-05-01 06:44:57 +00:00
MTPDocument(), // alt_document
2021-07-28 11:55:02 +00:00
MTPint());
2020-06-12 14:09:04 +00:00
} else {
2021-07-28 11:55:02 +00:00
Unexpected("Type in sendFilesConfirmed.");
2020-06-12 14:09:04 +00:00
}
}());
2020-06-12 14:09:04 +00:00
2021-07-28 11:55:02 +00:00
if (itemToEdit) {
itemToEdit->savePreviousMedia();
auto edition = HistoryMessageEdition();
edition.isEditHide = (flags & MessageFlag::HideEdited);
edition.editDate = 0;
edition.ttl = 0;
edition.mtpMedia = &media;
edition.textWithEntities = caption;
edition.useSameViews = true;
edition.useSameForwards = true;
edition.useSameMarkup = true;
edition.useSameReplies = true;
edition.useSameReactions = true;
itemToEdit->applyEdition(std::move(edition));
2020-06-12 14:09:04 +00:00
} else {
2021-07-28 11:55:02 +00:00
const auto viaBotId = UserId();
history->addNewLocalMessage(
newId.msg,
flags,
viaBotId,
file->to.replyTo,
HistoryItem::NewMessageDate(file->to.options.scheduled),
messageFromId,
messagePostAuthor,
caption,
media,
2021-10-02 11:28:21 +00:00
HistoryMessageMarkupData(),
2021-07-28 11:55:02 +00:00
groupId);
2020-06-12 14:09:04 +00:00
}
if (isEditing) {
return;
}
session->data().sendHistoryChangeNotifications();
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