Refactored code.

- Refactored passing message id to edit media.
 - Removed get/setEditMedia from mainwidget.
 - Combined onEditMedia and onSendFileConfirm in single method.
 - Added argument in FileLoadTask to pass message id to edit media.
 - Renamed flags in apiwrap.
 - Added check for allowing edit media when use clipboard.
 - Removed unused fileIsValidForAlbum.
 - Removed LOGs.
 - Replaced _isNotAlbum with _isAlbum.
 - Removed _viaRemoteContent.
 - Removed _newMediaPath.
 - Added empty() to MessageGroupId.
This commit is contained in:
23rd 2019-04-02 13:38:53 +03:00 committed by John Preston
parent 4988d21819
commit 5d8888bb8b
14 changed files with 94 additions and 118 deletions

View File

@ -4471,7 +4471,8 @@ void ApiWrap::editMedia(
Storage::PreparedList &&list,
SendMediaType type,
TextWithTags &&caption,
const SendOptions &options) {
const SendOptions &options,
MsgId msgIdToEdit) {
if (list.files.empty()) return;
auto &file = list.files.front();
@ -4484,7 +4485,7 @@ void ApiWrap::editMedia(
to,
caption,
nullptr,
true));
msgIdToEdit));
}
void ApiWrap::sendFiles(
@ -4620,17 +4621,17 @@ void ApiWrap::editUploadedPhoto(
MTPVector<MTPInputDocument>(),
MTP_int(0));
auto flags2 = MTPmessages_EditMessage::Flag::f_message | 0;
flags2 |= MTPmessages_EditMessage::Flag::f_no_webpage;
flags2 |= MTPmessages_EditMessage::Flag::f_entities;
flags2 |= MTPmessages_EditMessage::Flag::f_media;
auto flagsEditMsg = MTPmessages_EditMessage::Flag::f_message | 0;
flagsEditMsg |= MTPmessages_EditMessage::Flag::f_no_webpage;
flagsEditMsg |= MTPmessages_EditMessage::Flag::f_entities;
flagsEditMsg |= MTPmessages_EditMessage::Flag::f_media;
auto sentEntities = TextUtilities::EntitiesToMTP(
item->originalText().entities,
TextUtilities::ConvertOption::SkipLocal);
request(MTPmessages_EditMessage(
MTP_flags(flags2),
MTP_flags(flagsEditMsg),
item->history()->peer->input,
MTP_int(item->id),
MTP_string(item->originalText().text),
@ -4683,18 +4684,17 @@ void ApiWrap::editUploadedDocument(
MTPVector<MTPInputDocument>(),
MTP_int(0));
auto flags2 = MTPmessages_EditMessage::Flag::f_message | 0;
flags2 |= MTPmessages_EditMessage::Flag::f_no_webpage;
flags2 |= MTPmessages_EditMessage::Flag::f_entities;
flags2 |= MTPmessages_EditMessage::Flag::f_media;
auto flagsEditMsg = MTPmessages_EditMessage::Flag::f_message | 0;
flagsEditMsg |= MTPmessages_EditMessage::Flag::f_no_webpage;
flagsEditMsg |= MTPmessages_EditMessage::Flag::f_entities;
flagsEditMsg |= MTPmessages_EditMessage::Flag::f_media;
auto sentEntities = TextUtilities::EntitiesToMTP(
item->originalText().entities,
TextUtilities::ConvertOption::SkipLocal);
request(MTPmessages_EditMessage(
MTP_flags(flags2),
MTP_flags(flagsEditMsg),
item->history()->peer->input,
MTP_int(item->id),
MTP_string(item->originalText().text),

View File

@ -332,7 +332,8 @@ public:
Storage::PreparedList &&list,
SendMediaType type,
TextWithTags &&caption,
const SendOptions &options);
const SendOptions &options,
MsgId msgIdToEdit);
void sendUploadedPhoto(
FullMsgId localId,

View File

@ -24,7 +24,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "history/history_item.h"
#include "lang/lang_keys.h"
#include "layout.h"
#include "mainwidget.h"
#include "media/clip/media_clip_reader.h"
#include "storage/storage_media_prepare.h"
#include "styles/style_boxes.h"
@ -45,8 +44,9 @@ EditCaptionBox::EditCaptionBox(
, _msgId(item->fullId()) {
Expects(item->media() != nullptr);
Expects(item->media()->allowsEditCaption());
_isAllowedEditMedia = item->media()->allowsEditMedia();
_isNotAlbum = !item->groupId();
_isAlbum = !item->groupId().empty();
QSize dimensions;
auto image = (Image*)nullptr;
@ -288,9 +288,10 @@ void EditCaptionBox::updateEmojiPanelGeometry() {
}
void EditCaptionBox::prepareGifPreview(DocumentData* document) {
const auto newPath = getNewMediaPath();
if (_gifPreview) {
return;
} else if (!document && _newMediaPath.isEmpty()) {
} else if (!document && newPath.isEmpty()) {
return;
}
const auto callback = [=](Media::Clip::Notification notification) {
@ -301,9 +302,9 @@ void EditCaptionBox::prepareGifPreview(DocumentData* document) {
document,
_msgId,
callback);
} else if (!_newMediaPath.isEmpty()) {
} else if (!newPath.isEmpty()) {
_gifPreview = Media::Clip::MakeReader(
_newMediaPath,
newPath,
callback);
}
if (_gifPreview) _gifPreview->setAutoplay();
@ -339,23 +340,11 @@ void EditCaptionBox::updateEditPreview() {
const auto file = &_preparedList.files.front();
const auto fileMedia = &file->information->media;
const auto fileinfo = QFileInfo(_newMediaPath);
const auto fileinfo = QFileInfo(file->path);
const auto filename = fileinfo.fileName();
const auto mimeType = Core::MimeTypeForFile(fileinfo).name();
if (!_isNotAlbum) {
// This check only for users, who chose not valid file with absolute path.
if ((!_newMediaPath.isEmpty()
&& !fileIsValidForAlbum(filename, mimeType))
// And for users, who send file via remoteContent.
|| _viaRemoteContent) {
_newMediaPath = QString();
_preparedList.files.clear();
return;
}
}
if (!_newMediaPath.isEmpty()) {
if (!file->path.isEmpty()) {
_isImage = fileIsImage(filename, mimeType);
}
_isAudio = false;
@ -392,7 +381,7 @@ void EditCaptionBox::updateEditPreview() {
_doc = true;
}
_wayWrap->toggle(_isImage && _isNotAlbum, anim::type::instant);
_wayWrap->toggle(_isImage && !_isAlbum, anim::type::instant);
if (!_doc) {
_thumb = App::pixmapFromImageInPlace(
@ -411,18 +400,35 @@ void EditCaptionBox::createEditMediaButton() {
if (result.paths.isEmpty() && result.remoteContent.isEmpty()) {
return;
}
_viaRemoteContent = !result.remoteContent.isEmpty();
if (!result.remoteContent.isEmpty()) {
_newMediaPath = QString();
// Don't use remoteContent to edit album item.
if (_isAlbum) {
return;
}
auto image = Media::Clip::PrepareForSending(
QString(),
result.remoteContent
).thumbnail;
_preparedList = Storage::PrepareMediaFromImage(
Media::Clip::PrepareForSending(QString(), result.remoteContent).thumbnail,
std::move(image),
std::move(result.remoteContent),
st::sendMediaPreviewSize);
} else if (!result.paths.isEmpty()) {
_newMediaPath = result.paths.front();
_preparedList = Storage::PrepareMediaList(
QStringList(_newMediaPath),
auto list = Storage::PrepareMediaList(
QStringList(result.paths.front()),
st::sendMediaPreviewSize);
// Don't rewrite _preparedList if new list is not valid for album.
if (_isAlbum) {
const auto fileMedia = &list.files.front().information->media;
if (!base::get_if<FileMediaInformation::Image>(fileMedia)
&& !base::get_if<FileMediaInformation::Video>(fileMedia)) {
return;
}
}
_preparedList = std::move(list);
} else {
return;
}
@ -431,9 +437,9 @@ void EditCaptionBox::createEditMediaButton() {
};
const auto buttonCallback = [=] {
const auto filters = _isNotAlbum
? QStringList(FileDialog::AllFilesFilter())
: QStringList(qsl("Image and Video Files (*.png *.jpg *.mp4)"));
const auto filters = _isAlbum
? QStringList(qsl("Image and Video Files (*.png *.jpg *.mp4)"))
: QStringList(FileDialog::AllFilesFilter());
FileDialog::GetOpenPath(
this,
lang(lng_choose_file),
@ -453,7 +459,7 @@ void EditCaptionBox::prepare() {
if (_isAllowedEditMedia) {
createEditMediaButton();
} else {
_newMediaPath = QString();
_preparedList.files.clear();
}
addButton(langFactory(lng_cancel), [this] { closeBox(); });
@ -465,6 +471,9 @@ void EditCaptionBox::prepare() {
not_null<const QMimeData*> data,
Ui::InputField::MimeAction action) {
if (action == Ui::InputField::MimeAction::Check) {
if (!data->hasText() && !_isAllowedEditMedia) {
return false;
}
if (data->hasImage()) {
const auto image = qvariant_cast<QImage>(data->imageData());
if (!image.isNull()) {
@ -497,6 +506,10 @@ void EditCaptionBox::prepare() {
}
bool EditCaptionBox::fileFromClipboard(not_null<const QMimeData*> data) {
if (!_isAllowedEditMedia) {
return false;
}
auto list = [&] {
auto url = QList<QUrl>();
auto canAddUrl = false;
@ -527,13 +540,9 @@ bool EditCaptionBox::fileFromClipboard(not_null<const QMimeData*> data) {
return result;
}();
_preparedList = std::move(list);
_newMediaPath = _preparedList.files.empty()
? QString()
: _preparedList.files.front().path;
if (_preparedList.files.empty()) {
return false;
}
_viaRemoteContent = false;
updateEditPreview();
return true;
}
@ -748,7 +757,6 @@ void EditCaptionBox::save() {
}
if (!_preparedList.files.empty()) {
App::main()->setEditMedia(item->fullId());
const auto textWithTags = _field->getTextWithAppliedMarkdown();
auto sending = TextWithEntities{
textWithTags.text,
@ -760,7 +768,8 @@ void EditCaptionBox::save() {
std::move(_preparedList),
(!_asFile && _isImage) ? SendMediaType::Photo : SendMediaType::File,
_field->getTextWithAppliedMarkdown(),
ApiWrap::SendOptions(item->history()));
ApiWrap::SendOptions(item->history()),
item->fullId().msg);
closeBox();
return;
}

View File

@ -72,6 +72,12 @@ private:
void createEditMediaButton();
inline QString getNewMediaPath() {
return _preparedList.files.empty()
? QString()
: _preparedList.files.front().path;
}
not_null<Window::Controller*> _controller;
FullMsgId _msgId;
Image *_thumbnailImage = nullptr;
@ -111,8 +117,7 @@ private:
Ui::SlideWrap<Ui::RpWidget> *_wayWrap = nullptr;
QString _newMediaPath;
bool _isAllowedEditMedia = false;
bool _isNotAlbum = true;
bool _viaRemoteContent = false;
bool _isAlbum = false;
rpl::event_stream<> _editMediaClicks;
QString _error;

View File

@ -122,22 +122,6 @@ bool fileIsImage(const QString &name, const QString &mime) {
return false;
}
bool fileIsValidForAlbum(const QString &name, const QString &mime) {
QString lowermime = mime.toLower(), namelower = name.toLower();
if (lowermime.startsWith(qstr("video/mp4"))
|| lowermime.startsWith(qstr("image/jpeg"))
|| lowermime.startsWith(qstr("image/jpg"))
|| lowermime.startsWith(qstr("image/png"))) {
return true;
} else if (namelower.endsWith(qstr(".mp4"))
|| namelower.endsWith(qstr(".jpg"))
|| namelower.endsWith(qstr(".jpeg"))
|| namelower.endsWith(qstr(".png"))) {
return true;
}
return false;
}
QString FileNameUnsafe(
const QString &title,
const QString &filter,

View File

@ -72,7 +72,6 @@ struct VoiceData : public DocumentAdditionalData {
};
bool fileIsImage(const QString &name, const QString &mime);
bool fileIsValidForAlbum(const QString &name, const QString &mime);
namespace Serialize {
class Document;

View File

@ -64,7 +64,9 @@ void Groups::unregisterMessage(not_null<const HistoryItem*> item) {
}
}
void Groups::refreshMessage(not_null<HistoryItem*> item, bool forceRefresh) {
void Groups::refreshMessage(
not_null<HistoryItem*> item,
bool justRefreshViews) {
if (!isGrouped(item)) {
unregisterMessage(item);
return;
@ -80,7 +82,7 @@ void Groups::refreshMessage(not_null<HistoryItem*> item, bool forceRefresh) {
}
auto &items = i->second.items;
if (forceRefresh) {
if (justRefreshViews) {
refreshViews(items);
return;
}

View File

@ -27,7 +27,7 @@ public:
void unregisterMessage(not_null<const HistoryItem*> item);
void refreshMessage(
not_null<HistoryItem*> item,
bool forceRefresh = false);
bool justRefreshViews = false);
const Group *find(not_null<HistoryItem*> item) const;

View File

@ -98,8 +98,11 @@ struct MessageGroupId {
return static_cast<Type>(value);
}
bool empty() const {
return value == None;
}
explicit operator bool() const {
return value != None;
return !empty();
}
Underlying raw() const {
return static_cast<Underlying>(value);

View File

@ -3054,7 +3054,6 @@ void HistoryWidget::chooseAttach() {
uploadFile(result.remoteContent, SendMediaType::File);
}
} else {
LOG((result.paths[0]));
auto list = Storage::PrepareMediaList(
result.paths,
st::sendMediaPreviewSize);
@ -4422,7 +4421,6 @@ void HistoryWidget::documentEdited(
const FullMsgId &newId,
bool silent,
const MTPInputFile &file) {
LOG(("DOCUMENT EDITED %1").arg(newId.msg));
Auth().api().editUploadedDocument(newId, file, std::nullopt, silent);
}
@ -4430,7 +4428,6 @@ void HistoryWidget::photoEdited(
const FullMsgId &newId,
bool silent,
const MTPInputFile &file) {
LOG(("PHOTO EDITED %1").arg(newId.msg));
Auth().api().editUploadedPhoto(newId, file, silent);
}
@ -4466,7 +4463,6 @@ void HistoryWidget::documentProgress(const FullMsgId &newId) {
? document->uploadingData->offset
: 0;
LOG(("ITEM EXISTS %1 TYPE: %2 PROGRESS: %3").arg(newId.msg).arg(sendAction == SendAction::Type::UploadFile).arg(progress));
updateSendAction(
item->history(),
sendAction,

View File

@ -842,11 +842,9 @@ void MainWidget::cancelUploadLayer(not_null<HistoryItem*> item) {
Ui::hideLayer();
if (const auto item = App::histItemById(itemId)) {
const auto history = item->history();
if (!item->isEditingMedia()) {
if (!IsServerMsgId(itemId.msg)) {
item->destroy();
history->requestChatListMessage();
}
if (!IsServerMsgId(itemId.msg)) {
item->destroy();
history->requestChatListMessage();
} else {
item->returnSavedMedia();
session().uploader().cancel(item->fullId());
@ -1318,15 +1316,8 @@ void MainWidget::inlineResultLoadFailed(FileLoader *loader, bool started) {
}
void MainWidget::onSendFileConfirm(
const std::shared_ptr<FileLoadResult> &file) {
_history->sendFileConfirmed(file);
}
void MainWidget::onEditMedia(
const std::shared_ptr<FileLoadResult> &file,
const FullMsgId &oldId) {
LOG(("ON EDIT MEDIA"));
App::main()->setEditMedia(FullMsgId());
const std::optional<FullMsgId> &oldId) {
_history->sendFileConfirmed(file, oldId);
}
@ -3721,7 +3712,6 @@ void MainWidget::updateReceived(const mtpPrime *from, const mtpPrime *end) {
void MainWidget::feedUpdates(const MTPUpdates &updates, uint64 randomId) {
switch (updates.type()) {
case mtpc_updates: {
LOG(("TYPE 1"));
auto &d = updates.c_updates();
if (d.vseq.v) {
if (d.vseq.v <= updSeq) {
@ -3742,7 +3732,6 @@ void MainWidget::feedUpdates(const MTPUpdates &updates, uint64 randomId) {
} break;
case mtpc_updatesCombined: {
LOG(("TYPE 2"));
auto &d = updates.c_updatesCombined();
if (d.vseq_start.v) {
if (d.vseq_start.v <= updSeq) {
@ -3763,7 +3752,6 @@ void MainWidget::feedUpdates(const MTPUpdates &updates, uint64 randomId) {
} break;
case mtpc_updateShort: {
LOG(("TYPE 3"));
auto &d = updates.c_updateShort();
feedUpdate(d.vupdate);
@ -3771,7 +3759,6 @@ void MainWidget::feedUpdates(const MTPUpdates &updates, uint64 randomId) {
} break;
case mtpc_updateShortMessage: {
LOG(("TYPE 4"));
auto &d = updates.c_updateShortMessage();
if (!session().data().userLoaded(d.vuser_id.v)
|| (d.has_via_bot_id() && !session().data().userLoaded(d.vvia_bot_id.v))
@ -3787,7 +3774,6 @@ void MainWidget::feedUpdates(const MTPUpdates &updates, uint64 randomId) {
} break;
case mtpc_updateShortChatMessage: {
LOG(("TYPE 5"));
auto &d = updates.c_updateShortChatMessage();
const auto noFrom = !session().data().userLoaded(d.vfrom_id.v);
const auto chat = session().data().chatLoaded(d.vchat_id.v);
@ -3809,7 +3795,6 @@ void MainWidget::feedUpdates(const MTPUpdates &updates, uint64 randomId) {
} break;
case mtpc_updateShortSentMessage: {
LOG(("TYPE 6"));
auto &d = updates.c_updateShortSentMessage();
if (!IsServerMsgId(d.vid.v)) {
LOG(("API Error: Bad msgId got from server: %1").arg(d.vid.v));
@ -3849,7 +3834,6 @@ void MainWidget::feedUpdates(const MTPUpdates &updates, uint64 randomId) {
} break;
case mtpc_updatesTooLong: {
LOG(("TYPE 7"));
MTP_LOG(0, ("getDifference { good - updatesTooLong received }%1").arg(cTestMode() ? " TESTMODE" : ""));
return getDifference();
} break;

View File

@ -111,13 +111,6 @@ public:
void start();
void setEditMedia(FullMsgId flag) {
_editMedia = flag;
}
FullMsgId getEditMedia() const {
return _editMedia;
}
void openPeerByName(
const QString &name,
MsgId msgId = ShowAtUnreadMsgId,
@ -165,9 +158,9 @@ public:
QPixmap grabForShowAnimation(const Window::SectionSlideParams &params);
void checkMainSectionToLayer();
void onSendFileConfirm(const std::shared_ptr<FileLoadResult> &file);
void onEditMedia(const std::shared_ptr<FileLoadResult> &file,
const FullMsgId &oldId);
void onSendFileConfirm(
const std::shared_ptr<FileLoadResult> &file,
const std::optional<FullMsgId> &oldId);
bool onSendSticker(DocumentData *sticker);
void destroyData();
@ -553,6 +546,4 @@ private:
bool _firstColumnResizing = false;
int _firstColumnResizingShift = 0;
FullMsgId _editMedia;
};

View File

@ -529,7 +529,7 @@ FileLoadTask::FileLoadTask(
const FileLoadTo &to,
const TextWithTags &caption,
std::shared_ptr<SendingAlbum> album,
std::optional<bool> edit)
MsgId msgIdToEdit)
: _id(rand_value<uint64>())
, _to(to)
, _album(std::move(album))
@ -538,7 +538,7 @@ FileLoadTask::FileLoadTask(
, _information(std::move(information))
, _type(type)
, _caption(caption)
, _edit(edit) {
, _msgIdToEdit(msgIdToEdit) {
}
FileLoadTask::FileLoadTask(
@ -704,7 +704,7 @@ void FileLoadTask::process() {
_caption,
_album);
_result->edit = _edit.value_or(false);
_result->edit = (_msgIdToEdit > 0);
QString filename, filemime;
qint64 filesize = 0;
@ -984,11 +984,13 @@ void FileLoadTask::finish() {
lng_send_image_too_large(lt_name, _filepath)),
LayerOption::KeepOther);
removeFromAlbum();
} else if (App::main()->getEditMedia()) {
LOG(("FINISH UPLOAD EDIT"));
App::main()->onEditMedia(_result, App::main()->getEditMedia());
} else if (App::main()) {
App::main()->onSendFileConfirm(_result);
const auto fullId = _msgIdToEdit
? std::make_optional(FullMsgId(
peerToChannel(_to.peer),
_msgIdToEdit))
: std::nullopt;
App::main()->onSendFileConfirm(_result, fullId);
}
}

View File

@ -276,7 +276,7 @@ public:
const FileLoadTo &to,
const TextWithTags &caption,
std::shared_ptr<SendingAlbum> album = nullptr,
std::optional<bool> edit = false);
MsgId msgIdToEdit = 0);
FileLoadTask(
const QByteArray &voice,
int32 duration,
@ -323,7 +323,7 @@ private:
VoiceWaveform _waveform;
SendMediaType _type;
TextWithTags _caption;
std::optional<bool> _edit;
MsgId _msgIdToEdit;
std::shared_ptr<FileLoadResult> _result;