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;
bool clearDraft = true;
bool generateLocal = true;
MsgId replaceMediaOf = 0;
};
struct MessageToSend {

View file

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

View file

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

View file

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

View file

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

View file

@ -1045,13 +1045,13 @@ void EditCaptionBox::save() {
if (!_preparedList.files.empty()) {
auto action = Api::SendAction(item->history());
action.options = options;
action.replaceMediaOf = item->fullId().msg;
_controller->session().api().editMedia(
std::move(_preparedList),
(!_asFile && _photo) ? SendMediaType::Photo : SendMediaType::File,
_field->getTextWithAppliedMarkdown(),
action,
item->fullId().msg);
action);
closeBox();
return;
}

View file

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

View file

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

View file

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

View file

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

View file

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