2020-05-28 11:43:11 +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_editing.h"
|
|
|
|
|
|
|
|
#include "apiwrap.h"
|
|
|
|
#include "api/api_media.h"
|
|
|
|
#include "api/api_text_entities.h"
|
2021-10-18 21:36:55 +00:00
|
|
|
#include "ui/boxes/confirm_box.h"
|
2020-05-28 11:43:11 +00:00
|
|
|
#include "data/data_scheduled_messages.h"
|
|
|
|
#include "data/data_session.h"
|
|
|
|
#include "history/history.h"
|
|
|
|
#include "history/history_item.h"
|
|
|
|
#include "lang/lang_keys.h"
|
|
|
|
#include "main/main_session.h"
|
2021-03-12 12:48:00 +00:00
|
|
|
#include "mtproto/mtproto_response.h"
|
2022-02-28 03:59:36 +00:00
|
|
|
#include "boxes/abstract_box.h" // Ui::show().
|
2020-05-28 11:43:11 +00:00
|
|
|
|
|
|
|
namespace Api {
|
|
|
|
namespace {
|
|
|
|
|
2020-05-29 21:21:25 +00:00
|
|
|
using namespace rpl::details;
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
constexpr auto WithId =
|
2022-08-05 10:37:30 +00:00
|
|
|
is_callable_plain_v<T, Fn<void()>, mtpRequestId>;
|
2020-05-29 21:21:25 +00:00
|
|
|
template <typename T>
|
|
|
|
constexpr auto WithoutId =
|
2022-08-05 10:37:30 +00:00
|
|
|
is_callable_plain_v<T, Fn<void()>>;
|
2020-05-29 21:21:25 +00:00
|
|
|
template <typename T>
|
|
|
|
constexpr auto WithoutCallback =
|
2022-08-05 10:37:30 +00:00
|
|
|
is_callable_plain_v<T>;
|
|
|
|
template <typename T>
|
|
|
|
constexpr auto ErrorWithId =
|
|
|
|
is_callable_plain_v<T, QString, mtpRequestId>;
|
|
|
|
template <typename T>
|
|
|
|
constexpr auto ErrorWithoutId =
|
|
|
|
is_callable_plain_v<T, QString>;
|
2020-05-29 21:21:25 +00:00
|
|
|
|
|
|
|
template <typename DoneCallback, typename FailCallback>
|
2020-05-28 14:57:21 +00:00
|
|
|
mtpRequestId EditMessage(
|
2020-05-28 11:43:11 +00:00
|
|
|
not_null<HistoryItem*> item,
|
2020-05-28 14:57:21 +00:00
|
|
|
const TextWithEntities &textWithEntities,
|
2020-05-28 11:43:11 +00:00
|
|
|
SendOptions options,
|
2020-05-29 21:21:25 +00:00
|
|
|
DoneCallback &&done,
|
|
|
|
FailCallback &&fail,
|
2020-05-28 13:42:50 +00:00
|
|
|
std::optional<MTPInputMedia> inputMedia = std::nullopt) {
|
2020-05-28 11:43:11 +00:00
|
|
|
const auto session = &item->history()->session();
|
|
|
|
const auto api = &session->api();
|
|
|
|
|
2020-05-28 14:57:21 +00:00
|
|
|
const auto text = textWithEntities.text;
|
2020-05-28 11:43:11 +00:00
|
|
|
const auto sentEntities = EntitiesToMTP(
|
|
|
|
session,
|
2020-05-28 14:57:21 +00:00
|
|
|
textWithEntities.entities,
|
2020-05-28 11:43:11 +00:00
|
|
|
ConvertOption::SkipLocal);
|
|
|
|
const auto media = item->media();
|
|
|
|
|
2021-03-30 21:00:48 +00:00
|
|
|
const auto updateRecentStickers = inputMedia.has_value()
|
|
|
|
? Api::HasAttachedStickers(*inputMedia)
|
|
|
|
: false;
|
|
|
|
|
2020-05-28 11:43:11 +00:00
|
|
|
const auto emptyFlag = MTPmessages_EditMessage::Flag(0);
|
|
|
|
const auto flags = emptyFlag
|
2020-05-28 14:57:21 +00:00
|
|
|
| (!text.isEmpty() || media
|
2020-05-28 11:43:11 +00:00
|
|
|
? MTPmessages_EditMessage::Flag::f_message
|
|
|
|
: emptyFlag)
|
|
|
|
| ((media && inputMedia.has_value())
|
|
|
|
? MTPmessages_EditMessage::Flag::f_media
|
|
|
|
: emptyFlag)
|
2020-05-29 23:23:27 +00:00
|
|
|
| (options.removeWebPageId
|
2020-05-28 11:43:11 +00:00
|
|
|
? MTPmessages_EditMessage::Flag::f_no_webpage
|
|
|
|
: emptyFlag)
|
|
|
|
| (!sentEntities.v.isEmpty()
|
|
|
|
? MTPmessages_EditMessage::Flag::f_entities
|
|
|
|
: emptyFlag)
|
|
|
|
| (options.scheduled
|
|
|
|
? MTPmessages_EditMessage::Flag::f_schedule_date
|
|
|
|
: emptyFlag);
|
|
|
|
|
|
|
|
const auto id = item->isScheduled()
|
|
|
|
? session->data().scheduledMessages().lookupId(item)
|
|
|
|
: item->id;
|
2020-05-28 14:57:21 +00:00
|
|
|
return api->request(MTPmessages_EditMessage(
|
2020-05-28 11:43:11 +00:00
|
|
|
MTP_flags(flags),
|
|
|
|
item->history()->peer->input,
|
|
|
|
MTP_int(id),
|
|
|
|
MTP_string(text),
|
|
|
|
inputMedia.value_or(MTPInputMedia()),
|
|
|
|
MTPReplyMarkup(),
|
|
|
|
sentEntities,
|
|
|
|
MTP_int(options.scheduled)
|
2020-05-29 21:21:25 +00:00
|
|
|
)).done([=](
|
|
|
|
const MTPUpdates &result,
|
|
|
|
[[maybe_unused]] mtpRequestId requestId) {
|
|
|
|
const auto apply = [=] { api->applyUpdates(result); };
|
|
|
|
|
|
|
|
if constexpr (WithId<DoneCallback>) {
|
2022-08-05 10:37:30 +00:00
|
|
|
done(apply, requestId);
|
2020-05-29 21:21:25 +00:00
|
|
|
} else if constexpr (WithoutId<DoneCallback>) {
|
2022-08-05 10:37:30 +00:00
|
|
|
done(apply);
|
2020-05-29 21:21:25 +00:00
|
|
|
} else if constexpr (WithoutCallback<DoneCallback>) {
|
2022-08-05 10:37:30 +00:00
|
|
|
done();
|
2020-05-29 21:21:25 +00:00
|
|
|
apply();
|
|
|
|
} else {
|
2022-08-05 10:37:30 +00:00
|
|
|
t_bad_callback(done);
|
2020-05-29 21:21:25 +00:00
|
|
|
}
|
2021-03-30 21:00:48 +00:00
|
|
|
|
|
|
|
if (updateRecentStickers) {
|
|
|
|
api->requestRecentStickersForce(true);
|
|
|
|
}
|
2022-08-05 10:37:30 +00:00
|
|
|
}).fail([=](const MTP::Error &error, mtpRequestId requestId) {
|
|
|
|
if constexpr (ErrorWithId<FailCallback>) {
|
|
|
|
fail(error.type(), requestId);
|
|
|
|
} else if constexpr (ErrorWithoutId<FailCallback>) {
|
|
|
|
fail(error.type());
|
|
|
|
} else if constexpr (WithoutCallback<FailCallback>) {
|
|
|
|
fail();
|
|
|
|
} else {
|
|
|
|
t_bad_callback(fail);
|
|
|
|
}
|
|
|
|
}).send();
|
2020-05-28 11:43:11 +00:00
|
|
|
}
|
|
|
|
|
2020-05-29 21:21:25 +00:00
|
|
|
template <typename DoneCallback, typename FailCallback>
|
2020-05-28 14:57:21 +00:00
|
|
|
mtpRequestId EditMessage(
|
|
|
|
not_null<HistoryItem*> item,
|
|
|
|
SendOptions options,
|
2020-05-29 21:21:25 +00:00
|
|
|
DoneCallback &&done,
|
|
|
|
FailCallback &&fail,
|
2020-05-28 14:57:21 +00:00
|
|
|
std::optional<MTPInputMedia> inputMedia = std::nullopt) {
|
|
|
|
const auto &text = item->originalText();
|
2020-05-29 21:21:25 +00:00
|
|
|
return EditMessage(
|
|
|
|
item,
|
|
|
|
text,
|
|
|
|
options,
|
|
|
|
std::forward<DoneCallback>(done),
|
|
|
|
std::forward<FailCallback>(fail),
|
|
|
|
inputMedia);
|
2020-05-28 14:57:21 +00:00
|
|
|
}
|
|
|
|
|
2020-05-28 13:42:50 +00:00
|
|
|
void EditMessageWithUploadedMedia(
|
|
|
|
not_null<HistoryItem*> item,
|
|
|
|
SendOptions options,
|
|
|
|
MTPInputMedia media) {
|
2022-08-05 10:37:30 +00:00
|
|
|
const auto done = [=](Fn<void()> applyUpdates) {
|
2020-05-28 13:42:50 +00:00
|
|
|
if (item) {
|
2023-01-09 07:48:33 +00:00
|
|
|
item->removeFromSharedMediaIndex();
|
2020-05-28 13:42:50 +00:00
|
|
|
item->clearSavedMedia();
|
|
|
|
item->setIsLocalUpdateMedia(true);
|
|
|
|
applyUpdates();
|
|
|
|
item->setIsLocalUpdateMedia(false);
|
|
|
|
}
|
|
|
|
};
|
2022-08-05 10:37:30 +00:00
|
|
|
const auto fail = [=](const QString &error) {
|
2020-05-28 13:42:50 +00:00
|
|
|
const auto session = &item->history()->session();
|
2022-08-05 10:37:30 +00:00
|
|
|
const auto notModified = (error == u"MESSAGE_NOT_MODIFIED"_q);
|
|
|
|
const auto mediaInvalid = (error == u"MEDIA_NEW_INVALID"_q);
|
2020-05-28 13:42:50 +00:00
|
|
|
if (notModified || mediaInvalid) {
|
|
|
|
item->returnSavedMedia();
|
|
|
|
session->data().sendHistoryChangeNotifications();
|
|
|
|
if (mediaInvalid) {
|
|
|
|
Ui::show(
|
2022-02-27 08:23:20 +00:00
|
|
|
Ui::MakeInformBox(tr::lng_edit_media_invalid_file()),
|
2020-05-28 13:42:50 +00:00
|
|
|
Ui::LayerOption::KeepOther);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
session->api().sendMessageFail(error, item->history()->peer);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
EditMessage(item, options, done, fail, media);
|
|
|
|
}
|
|
|
|
|
2020-05-28 11:43:11 +00:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
void RescheduleMessage(
|
|
|
|
not_null<HistoryItem*> item,
|
|
|
|
SendOptions options) {
|
2021-03-12 10:45:13 +00:00
|
|
|
const auto empty = [] {};
|
2020-05-29 21:21:25 +00:00
|
|
|
EditMessage(item, options, empty, empty);
|
2020-05-28 13:42:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void EditMessageWithUploadedDocument(
|
|
|
|
HistoryItem *item,
|
2021-11-16 12:38:31 +00:00
|
|
|
RemoteFileInfo info,
|
|
|
|
SendOptions options) {
|
2020-05-28 13:42:50 +00:00
|
|
|
if (!item || !item->media() || !item->media()->document()) {
|
|
|
|
return;
|
|
|
|
}
|
2021-11-16 12:38:31 +00:00
|
|
|
EditMessageWithUploadedMedia(
|
2021-03-14 09:46:17 +00:00
|
|
|
item,
|
2021-11-16 12:38:31 +00:00
|
|
|
options,
|
|
|
|
PrepareUploadedDocument(item, std::move(info)));
|
2020-05-28 13:42:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void EditMessageWithUploadedPhoto(
|
|
|
|
HistoryItem *item,
|
2021-11-16 12:38:31 +00:00
|
|
|
RemoteFileInfo info,
|
|
|
|
SendOptions options) {
|
2020-05-28 13:42:50 +00:00
|
|
|
if (!item || !item->media() || !item->media()->photo()) {
|
|
|
|
return;
|
|
|
|
}
|
2021-11-16 12:38:31 +00:00
|
|
|
EditMessageWithUploadedMedia(
|
|
|
|
item,
|
|
|
|
options,
|
2022-12-13 12:11:52 +00:00
|
|
|
PrepareUploadedPhoto(item, std::move(info)));
|
2020-05-28 11:43:11 +00:00
|
|
|
}
|
|
|
|
|
2020-05-28 14:57:21 +00:00
|
|
|
mtpRequestId EditCaption(
|
|
|
|
not_null<HistoryItem*> item,
|
|
|
|
const TextWithEntities &caption,
|
2020-05-30 08:47:32 +00:00
|
|
|
SendOptions options,
|
2022-08-05 10:37:30 +00:00
|
|
|
Fn<void()> done,
|
|
|
|
Fn<void(const QString &)> fail) {
|
2020-05-30 08:47:32 +00:00
|
|
|
return EditMessage(item, caption, options, done, fail);
|
2020-05-28 14:57:21 +00:00
|
|
|
}
|
|
|
|
|
2020-05-29 23:08:31 +00:00
|
|
|
mtpRequestId EditTextMessage(
|
|
|
|
not_null<HistoryItem*> item,
|
|
|
|
const TextWithEntities &caption,
|
|
|
|
SendOptions options,
|
2022-08-05 10:37:30 +00:00
|
|
|
Fn<void(mtpRequestId requestId)> done,
|
|
|
|
Fn<void(const QString &, mtpRequestId requestId)> fail) {
|
|
|
|
const auto callback = [=](Fn<void()> applyUpdates, mtpRequestId id) {
|
2020-05-29 23:08:31 +00:00
|
|
|
applyUpdates();
|
2022-08-05 10:37:30 +00:00
|
|
|
done(id);
|
2020-05-29 23:08:31 +00:00
|
|
|
};
|
|
|
|
return EditMessage(item, caption, options, callback, fail);
|
|
|
|
}
|
|
|
|
|
2020-05-28 11:43:11 +00:00
|
|
|
} // namespace Api
|