mirror of
https://github.com/AyuGram/AyuGramDesktop.git
synced 2025-06-05 06:33:57 +02:00
Moved edit captions from EditCaptionBox to api_editing.
This commit is contained in:
parent
6c89f60679
commit
c46b96f252
4 changed files with 74 additions and 77 deletions
|
@ -17,12 +17,14 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
||||||
#include "history/history_item.h"
|
#include "history/history_item.h"
|
||||||
#include "lang/lang_keys.h"
|
#include "lang/lang_keys.h"
|
||||||
#include "main/main_session.h"
|
#include "main/main_session.h"
|
||||||
|
#include "mtproto/mtproto_rpc_sender.h"
|
||||||
|
|
||||||
namespace Api {
|
namespace Api {
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
void EditMessage(
|
mtpRequestId EditMessage(
|
||||||
not_null<HistoryItem*> item,
|
not_null<HistoryItem*> item,
|
||||||
|
const TextWithEntities &textWithEntities,
|
||||||
SendOptions options,
|
SendOptions options,
|
||||||
Fn<void(const MTPUpdates &, Fn<void()>)> done,
|
Fn<void(const MTPUpdates &, Fn<void()>)> done,
|
||||||
Fn<void(const RPCError &)> fail,
|
Fn<void(const RPCError &)> fail,
|
||||||
|
@ -30,16 +32,16 @@ void EditMessage(
|
||||||
const auto session = &item->history()->session();
|
const auto session = &item->history()->session();
|
||||||
const auto api = &session->api();
|
const auto api = &session->api();
|
||||||
|
|
||||||
const auto text = item->originalText().text;
|
const auto text = textWithEntities.text;
|
||||||
const auto sentEntities = EntitiesToMTP(
|
const auto sentEntities = EntitiesToMTP(
|
||||||
session,
|
session,
|
||||||
item->originalText().entities,
|
textWithEntities.entities,
|
||||||
ConvertOption::SkipLocal);
|
ConvertOption::SkipLocal);
|
||||||
const auto media = item->media();
|
const auto media = item->media();
|
||||||
|
|
||||||
const auto emptyFlag = MTPmessages_EditMessage::Flag(0);
|
const auto emptyFlag = MTPmessages_EditMessage::Flag(0);
|
||||||
const auto flags = emptyFlag
|
const auto flags = emptyFlag
|
||||||
| (!text.isEmpty()
|
| (!text.isEmpty() || media
|
||||||
? MTPmessages_EditMessage::Flag::f_message
|
? MTPmessages_EditMessage::Flag::f_message
|
||||||
: emptyFlag)
|
: emptyFlag)
|
||||||
| ((media && inputMedia.has_value())
|
| ((media && inputMedia.has_value())
|
||||||
|
@ -58,7 +60,7 @@ void EditMessage(
|
||||||
const auto id = item->isScheduled()
|
const auto id = item->isScheduled()
|
||||||
? session->data().scheduledMessages().lookupId(item)
|
? session->data().scheduledMessages().lookupId(item)
|
||||||
: item->id;
|
: item->id;
|
||||||
api->request(MTPmessages_EditMessage(
|
return api->request(MTPmessages_EditMessage(
|
||||||
MTP_flags(flags),
|
MTP_flags(flags),
|
||||||
item->history()->peer->input,
|
item->history()->peer->input,
|
||||||
MTP_int(id),
|
MTP_int(id),
|
||||||
|
@ -74,6 +76,16 @@ void EditMessage(
|
||||||
).send();
|
).send();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mtpRequestId EditMessage(
|
||||||
|
not_null<HistoryItem*> item,
|
||||||
|
SendOptions options,
|
||||||
|
Fn<void(const MTPUpdates &, Fn<void()>)> done,
|
||||||
|
Fn<void(const RPCError &)> fail,
|
||||||
|
std::optional<MTPInputMedia> inputMedia = std::nullopt) {
|
||||||
|
const auto &text = item->originalText();
|
||||||
|
return EditMessage(item, text, options, done, fail, inputMedia);
|
||||||
|
}
|
||||||
|
|
||||||
void EditMessageWithUploadedMedia(
|
void EditMessageWithUploadedMedia(
|
||||||
not_null<HistoryItem*> item,
|
not_null<HistoryItem*> item,
|
||||||
SendOptions options,
|
SendOptions options,
|
||||||
|
@ -143,5 +155,17 @@ void EditMessageWithUploadedPhoto(
|
||||||
EditMessageWithUploadedMedia(item, options, media);
|
EditMessageWithUploadedMedia(item, options, media);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mtpRequestId EditCaption(
|
||||||
|
not_null<HistoryItem*> item,
|
||||||
|
const TextWithEntities &caption,
|
||||||
|
Fn<void(const MTPUpdates &)> done,
|
||||||
|
Fn<void(const RPCError &)> fail) {
|
||||||
|
const auto callback = [=](const auto &result, Fn<void()> applyUpdates) {
|
||||||
|
done(result);
|
||||||
|
applyUpdates();
|
||||||
|
};
|
||||||
|
return EditMessage(item, caption, SendOptions(), callback, fail);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
} // namespace Api
|
} // namespace Api
|
||||||
|
|
|
@ -8,6 +8,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
class HistoryItem;
|
class HistoryItem;
|
||||||
|
class RPCError;
|
||||||
|
|
||||||
namespace Api {
|
namespace Api {
|
||||||
|
|
||||||
|
@ -28,4 +29,10 @@ void EditMessageWithUploadedPhoto(
|
||||||
const MTPInputFile &file,
|
const MTPInputFile &file,
|
||||||
SendOptions options);
|
SendOptions options);
|
||||||
|
|
||||||
|
mtpRequestId EditCaption(
|
||||||
|
not_null<HistoryItem*> item,
|
||||||
|
const TextWithEntities &caption,
|
||||||
|
Fn<void(const MTPUpdates &)> done,
|
||||||
|
Fn<void(const RPCError &)> fail);
|
||||||
|
|
||||||
} // namespace Api
|
} // namespace Api
|
||||||
|
|
|
@ -8,6 +8,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
||||||
#include "boxes/edit_caption_box.h"
|
#include "boxes/edit_caption_box.h"
|
||||||
|
|
||||||
#include "apiwrap.h"
|
#include "apiwrap.h"
|
||||||
|
#include "api/api_editing.h"
|
||||||
#include "api/api_text_entities.h"
|
#include "api/api_text_entities.h"
|
||||||
#include "main/main_session.h"
|
#include "main/main_session.h"
|
||||||
#include "chat_helpers/emoji_suggestions_widget.h"
|
#include "chat_helpers/emoji_suggestions_widget.h"
|
||||||
|
@ -70,7 +71,6 @@ EditCaptionBox::EditCaptionBox(
|
||||||
not_null<Window::SessionController*> controller,
|
not_null<Window::SessionController*> controller,
|
||||||
not_null<HistoryItem*> item)
|
not_null<HistoryItem*> item)
|
||||||
: _controller(controller)
|
: _controller(controller)
|
||||||
, _api(&controller->session().mtp())
|
|
||||||
, _msgId(item->fullId()) {
|
, _msgId(item->fullId()) {
|
||||||
Expects(item->media() != nullptr);
|
Expects(item->media() != nullptr);
|
||||||
Expects(item->media()->allowsEditCaption());
|
Expects(item->media()->allowsEditCaption());
|
||||||
|
@ -930,37 +930,13 @@ void EditCaptionBox::save() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto flags = MTPmessages_EditMessage::Flag::f_message | 0;
|
|
||||||
if (_previewCancelled) {
|
|
||||||
flags |= MTPmessages_EditMessage::Flag::f_no_webpage;
|
|
||||||
}
|
|
||||||
const auto textWithTags = _field->getTextWithAppliedMarkdown();
|
const auto textWithTags = _field->getTextWithAppliedMarkdown();
|
||||||
auto sending = TextWithEntities{
|
const auto sending = TextWithEntities{
|
||||||
textWithTags.text,
|
textWithTags.text,
|
||||||
TextUtilities::ConvertTextTagsToEntities(textWithTags.tags)
|
TextUtilities::ConvertTextTagsToEntities(textWithTags.tags)
|
||||||
};
|
};
|
||||||
const auto prepareFlags = Ui::ItemTextOptions(
|
|
||||||
item->history(),
|
|
||||||
_controller->session().user()).flags;
|
|
||||||
TextUtilities::PrepareForSending(sending, prepareFlags);
|
|
||||||
TextUtilities::Trim(sending);
|
|
||||||
|
|
||||||
const auto sentEntities = Api::EntitiesToMTP(
|
|
||||||
&item->history()->session(),
|
|
||||||
sending.entities,
|
|
||||||
Api::ConvertOption::SkipLocal);
|
|
||||||
if (!sentEntities.v.isEmpty()) {
|
|
||||||
flags |= MTPmessages_EditMessage::Flag::f_entities;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!_preparedList.files.empty()) {
|
if (!_preparedList.files.empty()) {
|
||||||
const auto textWithTags = _field->getTextWithAppliedMarkdown();
|
|
||||||
auto sending = TextWithEntities{
|
|
||||||
textWithTags.text,
|
|
||||||
TextUtilities::ConvertTextTagsToEntities(textWithTags.tags)
|
|
||||||
};
|
|
||||||
item->setText(sending);
|
|
||||||
|
|
||||||
_controller->session().api().editMedia(
|
_controller->session().api().editMedia(
|
||||||
std::move(_preparedList),
|
std::move(_preparedList),
|
||||||
(!_asFile && _photo) ? SendMediaType::Photo : SendMediaType::File,
|
(!_asFile && _photo) ? SendMediaType::Photo : SendMediaType::File,
|
||||||
|
@ -971,47 +947,43 @@ void EditCaptionBox::save() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
_saveRequestId = _api.request(MTPmessages_EditMessage(
|
const auto done = crl::guard(this, [=](const MTPUpdates &updates) {
|
||||||
MTP_flags(flags),
|
_saveRequestId = 0;
|
||||||
item->history()->peer->input,
|
|
||||||
MTP_int(item->id),
|
|
||||||
MTP_string(sending.text),
|
|
||||||
MTPInputMedia(),
|
|
||||||
MTPReplyMarkup(),
|
|
||||||
sentEntities,
|
|
||||||
MTP_int(0)
|
|
||||||
)).done([=](const MTPUpdates &result) {
|
|
||||||
saveDone(result);
|
|
||||||
}).fail([=](const RPCError &error) {
|
|
||||||
saveFail(error);
|
|
||||||
}).send();
|
|
||||||
}
|
|
||||||
|
|
||||||
void EditCaptionBox::saveDone(const MTPUpdates &updates) {
|
|
||||||
_saveRequestId = 0;
|
|
||||||
const auto controller = _controller;
|
|
||||||
closeBox();
|
|
||||||
controller->session().api().applyUpdates(updates);
|
|
||||||
}
|
|
||||||
|
|
||||||
void EditCaptionBox::saveFail(const RPCError &error) {
|
|
||||||
_saveRequestId = 0;
|
|
||||||
const auto &type = error.type();
|
|
||||||
if (type == qstr("MESSAGE_ID_INVALID")
|
|
||||||
|| type == qstr("CHAT_ADMIN_REQUIRED")
|
|
||||||
|| type == qstr("MESSAGE_EDIT_TIME_EXPIRED")) {
|
|
||||||
_error = tr::lng_edit_error(tr::now);
|
|
||||||
update();
|
|
||||||
} else if (type == qstr("MESSAGE_NOT_MODIFIED")) {
|
|
||||||
closeBox();
|
closeBox();
|
||||||
} else if (type == qstr("MESSAGE_EMPTY")) {
|
});
|
||||||
_field->setFocus();
|
|
||||||
_field->showError();
|
const auto fail = crl::guard(this, [=](const RPCError &error) {
|
||||||
update();
|
const auto defaultErrors = {
|
||||||
} else {
|
u"MESSAGE_ID_INVALID"_q,
|
||||||
_error = tr::lng_edit_error(tr::now);
|
u"CHAT_ADMIN_REQUIRED"_q,
|
||||||
update();
|
u"MESSAGE_EDIT_TIME_EXPIRED"_q,
|
||||||
}
|
};
|
||||||
|
|
||||||
|
_saveRequestId = 0;
|
||||||
|
const auto &type = error.type();
|
||||||
|
if (ranges::contains(defaultErrors, type)) {
|
||||||
|
_error = tr::lng_edit_error(tr::now);
|
||||||
|
update();
|
||||||
|
} else if (type == u"MESSAGE_NOT_MODIFIED"_q) {
|
||||||
|
closeBox();
|
||||||
|
} else if (type == u"MESSAGE_EMPTY"_q) {
|
||||||
|
_field->setFocus();
|
||||||
|
_field->showError();
|
||||||
|
update();
|
||||||
|
} else {
|
||||||
|
_error = tr::lng_edit_error(tr::now);
|
||||||
|
update();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
lifetime().add([=] {
|
||||||
|
if (_saveRequestId) {
|
||||||
|
auto &session = _controller->session();
|
||||||
|
session.api().request(base::take(_saveRequestId)).cancel();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
_saveRequestId = Api::EditCaption(item, sending, done, fail);
|
||||||
}
|
}
|
||||||
|
|
||||||
void EditCaptionBox::setName(QString nameString, qint64 size) {
|
void EditCaptionBox::setName(QString nameString, qint64 size) {
|
||||||
|
|
|
@ -10,7 +10,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
||||||
#include "boxes/abstract_box.h"
|
#include "boxes/abstract_box.h"
|
||||||
#include "storage/storage_media_prepare.h"
|
#include "storage/storage_media_prepare.h"
|
||||||
#include "ui/wrap/slide_wrap.h"
|
#include "ui/wrap/slide_wrap.h"
|
||||||
#include "mtproto/sender.h"
|
|
||||||
|
|
||||||
class Image;
|
class Image;
|
||||||
|
|
||||||
|
@ -83,9 +82,6 @@ private:
|
||||||
void save();
|
void save();
|
||||||
void captionResized();
|
void captionResized();
|
||||||
|
|
||||||
void saveDone(const MTPUpdates &updates);
|
|
||||||
void saveFail(const RPCError &error);
|
|
||||||
|
|
||||||
void setName(QString nameString, qint64 size);
|
void setName(QString nameString, qint64 size);
|
||||||
bool fileFromClipboard(not_null<const QMimeData*> data);
|
bool fileFromClipboard(not_null<const QMimeData*> data);
|
||||||
void updateEditPreview();
|
void updateEditPreview();
|
||||||
|
@ -102,7 +98,6 @@ private:
|
||||||
}
|
}
|
||||||
|
|
||||||
const not_null<Window::SessionController*> _controller;
|
const not_null<Window::SessionController*> _controller;
|
||||||
MTP::Sender _api;
|
|
||||||
|
|
||||||
FullMsgId _msgId;
|
FullMsgId _msgId;
|
||||||
std::shared_ptr<Data::PhotoMedia> _photoMedia;
|
std::shared_ptr<Data::PhotoMedia> _photoMedia;
|
||||||
|
@ -135,7 +130,6 @@ private:
|
||||||
|
|
||||||
Storage::PreparedList _preparedList;
|
Storage::PreparedList _preparedList;
|
||||||
|
|
||||||
bool _previewCancelled = false;
|
|
||||||
mtpRequestId _saveRequestId = 0;
|
mtpRequestId _saveRequestId = 0;
|
||||||
|
|
||||||
object_ptr<Ui::IconButton> _editMedia = nullptr;
|
object_ptr<Ui::IconButton> _editMedia = nullptr;
|
||||||
|
|
Loading…
Add table
Reference in a new issue