Slightly refactored uploading of edit media.

This commit is contained in:
23rd 2021-03-09 12:11:40 +03:00
parent bbeb9d3950
commit d4bb62d055
11 changed files with 35 additions and 41 deletions

View file

@ -33,6 +33,7 @@ struct SendAction {
MsgId replyTo = 0; MsgId replyTo = 0;
bool clearDraft = true; bool clearDraft = true;
bool generateLocal = true; bool generateLocal = true;
MsgId replaceMediaOf = 0;
}; };
struct MessageToSend { struct MessageToSend {

View file

@ -344,13 +344,15 @@ void FillMessagePostFlags(
void SendConfirmedFile( void SendConfirmedFile(
not_null<Main::Session*> session, not_null<Main::Session*> session,
const std::shared_ptr<FileLoadResult> &file, const std::shared_ptr<FileLoadResult> &file) {
const std::optional<FullMsgId> &oldId) { const auto isEditing = file->to.replaceMediaOf != 0;
const auto isEditing = oldId.has_value();
const auto channelId = peerToChannel(file->to.peer); const auto channelId = peerToChannel(file->to.peer);
const auto newId = oldId.value_or( const auto newId = FullMsgId(
FullMsgId(channelId, session->data().nextLocalMessageId())); channelId,
isEditing
? file->to.replaceMediaOf
: session->data().nextLocalMessageId());
auto groupId = file->album ? file->album->groupId : uint64(0); auto groupId = file->album ? file->album->groupId : uint64(0);
if (file->album) { if (file->album) {
const auto proj = [](const SendingAlbum::Item &item) { const auto proj = [](const SendingAlbum::Item &item) {
@ -361,7 +363,6 @@ void SendConfirmedFile(
it->msgId = newId; it->msgId = newId;
} }
file->edit = isEditing;
session->uploader().upload(newId, file); session->uploader().upload(newId, file);
const auto itemToEdit = isEditing const auto itemToEdit = isEditing

View file

@ -34,7 +34,6 @@ void FillMessagePostFlags(
void SendConfirmedFile( void SendConfirmedFile(
not_null<Main::Session*> session, not_null<Main::Session*> session,
const std::shared_ptr<FileLoadResult> &file, const std::shared_ptr<FileLoadResult> &file);
const std::optional<FullMsgId> &oldId);
} // namespace Api } // namespace Api

View file

@ -3834,8 +3834,7 @@ void ApiWrap::editMedia(
Ui::PreparedList &&list, Ui::PreparedList &&list,
SendMediaType type, SendMediaType type,
TextWithTags &&caption, TextWithTags &&caption,
const SendAction &action, const SendAction &action) {
MsgId msgIdToEdit) {
if (list.files.empty()) return; if (list.files.empty()) return;
auto &file = list.files.front(); auto &file = list.files.front();
@ -3847,9 +3846,7 @@ void ApiWrap::editMedia(
std::move(file.information), std::move(file.information),
type, type,
to, to,
caption, caption));
nullptr,
msgIdToEdit));
} }
void ApiWrap::sendFiles( void ApiWrap::sendFiles(
@ -4490,7 +4487,11 @@ void ApiWrap::sendAlbumIfReady(not_null<SendingAlbum*> album) {
FileLoadTo ApiWrap::fileLoadTaskOptions(const SendAction &action) const { FileLoadTo ApiWrap::fileLoadTaskOptions(const SendAction &action) const {
const auto peer = action.history->peer; const auto peer = action.history->peer;
return FileLoadTo(peer->id, action.options, action.replyTo); return FileLoadTo(
peer->id,
action.options,
action.replyTo,
action.replaceMediaOf);
} }
void ApiWrap::uploadPeerPhoto(not_null<PeerData*> peer, QImage &&image) { void ApiWrap::uploadPeerPhoto(not_null<PeerData*> peer, QImage &&image) {

View file

@ -394,8 +394,7 @@ public:
Ui::PreparedList &&list, Ui::PreparedList &&list,
SendMediaType type, SendMediaType type,
TextWithTags &&caption, TextWithTags &&caption,
const SendAction &action, const SendAction &action);
MsgId msgIdToEdit);
void sendUploadedPhoto( void sendUploadedPhoto(
FullMsgId localId, FullMsgId localId,

View file

@ -1045,13 +1045,13 @@ void EditCaptionBox::save() {
if (!_preparedList.files.empty()) { if (!_preparedList.files.empty()) {
auto action = Api::SendAction(item->history()); auto action = Api::SendAction(item->history());
action.options = options; action.options = options;
action.replaceMediaOf = item->fullId().msg;
_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,
_field->getTextWithAppliedMarkdown(), _field->getTextWithAppliedMarkdown(),
action, action);
item->fullId().msg);
closeBox(); closeBox();
return; return;
} }

View file

@ -125,7 +125,7 @@ void DicePack::generateLocal(int index, const QString &name) {
QByteArray(), QByteArray(),
nullptr, nullptr,
SendMediaType::File, SendMediaType::File,
FileLoadTo(0, {}, 0), FileLoadTo(0, {}, 0, 0),
{}); {});
task.process({ .generateGoodThumbnail = false }); task.process({ .generateGoodThumbnail = false });
const auto result = task.peekResult(); const auto result = task.peekResult();

View file

@ -1535,7 +1535,7 @@ void FormController::uploadEncryptedFile(
auto prepared = std::make_shared<FileLoadResult>( auto prepared = std::make_shared<FileLoadResult>(
TaskId(), TaskId(),
file.uploadData->fileId, file.uploadData->fileId,
FileLoadTo(PeerId(0), Api::SendOptions(), MsgId(0)), FileLoadTo(PeerId(0), Api::SendOptions(), MsgId(0), MsgId(0)),
TextWithTags(), TextWithTags(),
std::shared_ptr<SendingAlbum>(nullptr)); std::shared_ptr<SendingAlbum>(nullptr));
prepared->type = SendMediaType::Secure; prepared->type = SendMediaType::Secure;

View file

@ -469,7 +469,7 @@ void Uploader::sendNext() {
? uploadingData.file->to.options ? uploadingData.file->to.options
: Api::SendOptions(); : Api::SendOptions();
const auto edit = uploadingData.file && const auto edit = uploadingData.file &&
uploadingData.file->edit; uploadingData.file->to.replaceMediaOf;
if (uploadingData.type() == SendMediaType::Photo) { if (uploadingData.type() == SendMediaType::Photo) {
auto photoFilename = uploadingData.filename(); auto photoFilename = uploadingData.filename();
if (!photoFilename.endsWith(qstr(".jpg"), Qt::CaseInsensitive)) { if (!photoFilename.endsWith(qstr(".jpg"), Qt::CaseInsensitive)) {

View file

@ -494,8 +494,7 @@ FileLoadTask::FileLoadTask(
SendMediaType type, SendMediaType type,
const FileLoadTo &to, const FileLoadTo &to,
const TextWithTags &caption, const TextWithTags &caption,
std::shared_ptr<SendingAlbum> album, std::shared_ptr<SendingAlbum> album)
MsgId msgIdToEdit)
: _id(openssl::RandomValue<uint64>()) : _id(openssl::RandomValue<uint64>())
, _session(session) , _session(session)
, _dcId(session->mainDcId()) , _dcId(session->mainDcId())
@ -505,10 +504,9 @@ FileLoadTask::FileLoadTask(
, _content(content) , _content(content)
, _information(std::move(information)) , _information(std::move(information))
, _type(type) , _type(type)
, _caption(caption) , _caption(caption) {
, _msgIdToEdit(msgIdToEdit) {
Expects(to.options.scheduled Expects(to.options.scheduled
|| (_msgIdToEdit == 0 || IsServerMsgId(_msgIdToEdit))); || (to.replaceMediaOf == 0 || IsServerMsgId(to.replaceMediaOf)));
} }
FileLoadTask::FileLoadTask( FileLoadTask::FileLoadTask(
@ -690,8 +688,6 @@ void FileLoadTask::process(Args &&args) {
_caption, _caption,
_album); _album);
_result->edit = (_msgIdToEdit > 0);
QString filename, filemime; QString filename, filemime;
qint64 filesize = 0; qint64 filesize = 0;
QByteArray filedata; QByteArray filedata;
@ -993,12 +989,7 @@ void FileLoadTask::finish() {
Ui::LayerOption::KeepOther); Ui::LayerOption::KeepOther);
removeFromAlbum(); removeFromAlbum();
} else if (const auto session = _session.get()) { } else if (const auto session = _session.get()) {
const auto fullId = _msgIdToEdit Api::SendConfirmedFile(session, _result);
? std::make_optional(FullMsgId(
peerToChannel(_to.peer),
_msgIdToEdit))
: std::nullopt;
Api::SendConfirmedFile(session, _result, fullId);
} }
} }

View file

@ -212,14 +212,20 @@ struct SendingAlbum {
}; };
struct FileLoadTo { struct FileLoadTo {
FileLoadTo(const PeerId &peer, Api::SendOptions options, MsgId replyTo) FileLoadTo(
const PeerId &peer,
Api::SendOptions options,
MsgId replyTo,
MsgId replaceMediaOf)
: peer(peer) : peer(peer)
, options(options) , options(options)
, replyTo(replyTo) { , replyTo(replyTo)
, replaceMediaOf(replaceMediaOf) {
} }
PeerId peer; PeerId peer;
Api::SendOptions options; Api::SendOptions options;
MsgId replyTo; MsgId replyTo;
MsgId replaceMediaOf;
}; };
struct FileLoadResult { struct FileLoadResult {
@ -261,8 +267,6 @@ struct FileLoadResult {
PreparedPhotoThumbs photoThumbs; PreparedPhotoThumbs photoThumbs;
TextWithTags caption; TextWithTags caption;
bool edit = false;
void setFileData(const QByteArray &filedata); void setFileData(const QByteArray &filedata);
void setThumbData(const QByteArray &thumbdata); void setThumbData(const QByteArray &thumbdata);
@ -287,8 +291,7 @@ public:
SendMediaType type, SendMediaType type,
const FileLoadTo &to, const FileLoadTo &to,
const TextWithTags &caption, const TextWithTags &caption,
std::shared_ptr<SendingAlbum> album = nullptr, std::shared_ptr<SendingAlbum> album = nullptr);
MsgId msgIdToEdit = 0);
FileLoadTask( FileLoadTask(
not_null<Main::Session*> session, not_null<Main::Session*> session,
const QByteArray &voice, const QByteArray &voice,
@ -346,7 +349,6 @@ private:
VoiceWaveform _waveform; VoiceWaveform _waveform;
SendMediaType _type; SendMediaType _type;
TextWithTags _caption; TextWithTags _caption;
MsgId _msgIdToEdit = 0;
std::shared_ptr<FileLoadResult> _result; std::shared_ptr<FileLoadResult> _result;