diff --git a/Telegram/SourceFiles/apiwrap.cpp b/Telegram/SourceFiles/apiwrap.cpp index 688035e8eb..3301b6f042 100644 --- a/Telegram/SourceFiles/apiwrap.cpp +++ b/Telegram/SourceFiles/apiwrap.cpp @@ -2515,7 +2515,9 @@ void ApiWrap::refreshFileReference( }); }); } }, [&](Data::FileOriginSavedGifs data) { - request(MTPmessages_GetSavedGifs(MTP_int(0))); + request( + MTPmessages_GetSavedGifs(MTP_int(0)), + [] { crl::on_main([] { Local::writeSavedGifs(); }); }); }, [&](base::none_type) { fail(); }); @@ -2672,6 +2674,80 @@ std::vector> *ApiWrap::stickersByEmoji( return nullptr; } +void ApiWrap::toggleFavedSticker( + not_null document, + Data::FileOrigin origin, + bool faved) { + if (faved && !document->sticker()) { + return; + } + + auto failHandler = std::make_shared>(); + auto performRequest = [=] { + request(MTPmessages_FaveSticker( + document->mtpInput(), + MTP_bool(!faved) + )).done([=](const MTPBool &result) { + if (mtpIsTrue(result)) { + Stickers::SetFaved(document, faved); + } + }).fail( + base::duplicate(*failHandler) + ).send(); + }; + *failHandler = [=](const RPCError &error) { + if (error.code() == 400 + && error.type().startsWith(qstr("FILE_REFERENCE_"))) { + const auto current = document->fileReference(); + auto refreshed = [=](const Data::UpdatedFileReferences &data) { + if (document->fileReference() != current) { + performRequest(); + } + }; + refreshFileReference(origin, std::move(refreshed)); + } + }; + performRequest(); +} + +void ApiWrap::toggleSavedGif( + not_null document, + Data::FileOrigin origin, + bool saved) { + if (saved && !document->isGifv()) { + return; + } + + auto failHandler = std::make_shared>(); + auto performRequest = [=] { + request(MTPmessages_SaveGif( + document->mtpInput(), + MTP_bool(!saved) + )).done([=](const MTPBool &result) { + if (mtpIsTrue(result)) { + if (saved) { + App::addSavedGif(document); + } + } + }).fail( + base::duplicate(*failHandler) + ).send(); + }; + *failHandler = [=](const RPCError &error) { + if (error.code() == 400 + && error.type().startsWith(qstr("FILE_REFERENCE_"))) { + const auto current = document->fileReference(); + auto refreshed = [=](const Data::UpdatedFileReferences &data) { + if (document->fileReference() != current) { + performRequest(); + } + }; + refreshFileReference(origin, std::move(refreshed)); + } + }; + performRequest(); +} + void ApiWrap::requestStickers(TimeId now) { if (!_session->data().stickersUpdateNeeded(now) || _stickersUpdateRequest) { @@ -4383,6 +4459,8 @@ void ApiWrap::sendExistingDocument( if (!sentEntities.v.isEmpty()) { sendFlags |= MTPmessages_SendMedia::Flag::f_entities; } + const auto replyTo = options.replyTo; + const auto captionText = caption.text; App::historyRegRandom(randomId, newId); @@ -4390,17 +4468,15 @@ void ApiWrap::sendExistingDocument( newId.msg, flags, 0, - options.replyTo, + replyTo, unixtime(), messageFromId, messagePostAuthor, document, caption, MTPnullMarkup); - auto failHandler = std::make_shared>(); - const auto replyTo = options.replyTo; - const auto captionText = caption.text; + auto failHandler = std::make_shared>(); auto performRequest = [=] { history->sendRequestId = request(MTPmessages_SendMedia( MTP_flags(sendFlags), diff --git a/Telegram/SourceFiles/apiwrap.h b/Telegram/SourceFiles/apiwrap.h index 861356ebdd..43843d94ed 100644 --- a/Telegram/SourceFiles/apiwrap.h +++ b/Telegram/SourceFiles/apiwrap.h @@ -156,6 +156,14 @@ public: const MTPInputStickerSet &set); std::vector> *stickersByEmoji( not_null emoji); + void toggleFavedSticker( + not_null document, + Data::FileOrigin origin, + bool faved); + void toggleSavedGif( + not_null document, + Data::FileOrigin origin, + bool saved); void joinChannel(not_null channel); void leaveChannel(not_null channel); diff --git a/Telegram/SourceFiles/chat_helpers/field_autocomplete.cpp b/Telegram/SourceFiles/chat_helpers/field_autocomplete.cpp index 8e4d56280c..d085a66bfa 100644 --- a/Telegram/SourceFiles/chat_helpers/field_autocomplete.cpp +++ b/Telegram/SourceFiles/chat_helpers/field_autocomplete.cpp @@ -29,7 +29,7 @@ FieldAutocomplete::FieldAutocomplete(QWidget *parent) : TWidget(parent) connect(_inner, SIGNAL(mentionChosen(UserData*, FieldAutocomplete::ChooseMethod)), this, SIGNAL(mentionChosen(UserData*, FieldAutocomplete::ChooseMethod))); connect(_inner, SIGNAL(hashtagChosen(QString, FieldAutocomplete::ChooseMethod)), this, SIGNAL(hashtagChosen(QString, FieldAutocomplete::ChooseMethod))); connect(_inner, SIGNAL(botCommandChosen(QString, FieldAutocomplete::ChooseMethod)), this, SIGNAL(botCommandChosen(QString, FieldAutocomplete::ChooseMethod))); - connect(_inner, SIGNAL(stickerChosen(DocumentData*, FieldAutocomplete::ChooseMethod)), this, SIGNAL(stickerChosen(DocumentData*, FieldAutocomplete::ChooseMethod))); + connect(_inner, SIGNAL(stickerChosen(not_null,FieldAutocomplete::ChooseMethod)), this, SIGNAL(stickerChosen(not_null,FieldAutocomplete::ChooseMethod))); connect(_inner, SIGNAL(mustScrollTo(int, int)), _scroll, SLOT(scrollToY(int, int))); _scroll->show(); @@ -729,7 +729,7 @@ bool FieldAutocompleteInner::moveSel(int key) { bool FieldAutocompleteInner::chooseSelected(FieldAutocomplete::ChooseMethod method) const { if (!_srows->empty()) { if (_sel >= 0 && _sel < _srows->size()) { - emit stickerChosen(_srows->at(_sel), method); + emit stickerChosen((*_srows)[_sel], method); return true; } } else if (!_mrows->isEmpty()) { diff --git a/Telegram/SourceFiles/chat_helpers/field_autocomplete.h b/Telegram/SourceFiles/chat_helpers/field_autocomplete.h index fe4d5d43db..4585c41787 100644 --- a/Telegram/SourceFiles/chat_helpers/field_autocomplete.h +++ b/Telegram/SourceFiles/chat_helpers/field_autocomplete.h @@ -74,7 +74,7 @@ signals: void mentionChosen(UserData *user, FieldAutocomplete::ChooseMethod method) const; void hashtagChosen(QString hashtag, FieldAutocomplete::ChooseMethod method) const; void botCommandChosen(QString command, FieldAutocomplete::ChooseMethod method) const; - void stickerChosen(DocumentData *sticker, FieldAutocomplete::ChooseMethod method) const; + void stickerChosen(not_null sticker, FieldAutocomplete::ChooseMethod method) const; void moderateKeyActivate(int key, bool *outHandled) const; @@ -146,7 +146,7 @@ signals: void mentionChosen(UserData *user, FieldAutocomplete::ChooseMethod method) const; void hashtagChosen(QString hashtag, FieldAutocomplete::ChooseMethod method) const; void botCommandChosen(QString command, FieldAutocomplete::ChooseMethod method) const; - void stickerChosen(DocumentData *sticker, FieldAutocomplete::ChooseMethod method) const; + void stickerChosen(not_null sticker, FieldAutocomplete::ChooseMethod method) const; void mustScrollTo(int scrollToTop, int scrollToBottom); public slots: diff --git a/Telegram/SourceFiles/chat_helpers/stickers_list_widget.cpp b/Telegram/SourceFiles/chat_helpers/stickers_list_widget.cpp index 9154dad4ec..fe7337933e 100644 --- a/Telegram/SourceFiles/chat_helpers/stickers_list_widget.cpp +++ b/Telegram/SourceFiles/chat_helpers/stickers_list_widget.cpp @@ -1606,8 +1606,10 @@ void StickersListWidget::removeFavedSticker(int section, int index) { clearSelection(); auto sticker = _mySets[section].pack[index]; Stickers::SetFaved(sticker, false); - auto unfave = true; - MTP::send(MTPmessages_FaveSticker(sticker->mtpInput(), MTP_bool(unfave))); + Auth().api().toggleFavedSticker( + sticker, + Data::FileOriginStickerSet(Stickers::FavedSetId, 0), + false); } void StickersListWidget::setColumnCount(int count) { diff --git a/Telegram/SourceFiles/history/history_inner_widget.cpp b/Telegram/SourceFiles/history/history_inner_widget.cpp index 5f1e794dd0..06014c6e59 100644 --- a/Telegram/SourceFiles/history/history_inner_widget.cpp +++ b/Telegram/SourceFiles/history/history_inner_widget.cpp @@ -1636,7 +1636,10 @@ void HistoryInner::showContextMenu(QContextMenuEvent *e, bool showFromTouch) { showStickerPackInfo(document); }); _menu->addAction(lang(Stickers::IsFaved(document) ? lng_faved_stickers_remove : lng_faved_stickers_add), [=] { - toggleFavedSticker(document); + Auth().api().toggleFavedSticker( + document, + itemId, + !Stickers::IsFaved(document)); }); } _menu->addAction(lang(lng_context_save_image), App::LambdaDelayed(st::defaultDropdownMenu.menu.ripple.hideDuration, this, [=] { @@ -1772,13 +1775,6 @@ void HistoryInner::showStickerPackInfo(not_null document) { } } -void HistoryInner::toggleFavedSticker(not_null document) { - auto unfave = Stickers::IsFaved(document); - MTP::send(MTPmessages_FaveSticker(document->mtpInput(), MTP_bool(unfave)), rpcDone([document, unfave](const MTPBool &result) { - Stickers::SetFaved(document, !unfave); - })); -} - void HistoryInner::cancelContextDownload(not_null document) { document->cancel(); } @@ -1811,7 +1807,7 @@ void HistoryInner::saveContextGif(FullMsgId itemId) { if (const auto item = App::histItemById(itemId)) { if (const auto media = item->media()) { if (const auto document = media->document()) { - _widget->saveGif(document); + Auth().api().toggleSavedGif(document, item->fullId(), true); } } } diff --git a/Telegram/SourceFiles/history/history_inner_widget.h b/Telegram/SourceFiles/history/history_inner_widget.h index 81457f412e..042885a730 100644 --- a/Telegram/SourceFiles/history/history_inner_widget.h +++ b/Telegram/SourceFiles/history/history_inner_widget.h @@ -214,7 +214,6 @@ private: not_null document); void copyContextImage(not_null photo); void showStickerPackInfo(not_null document); - void toggleFavedSticker(not_null document); void itemRemoved(not_null item); void viewRemoved(not_null view); diff --git a/Telegram/SourceFiles/history/history_widget.cpp b/Telegram/SourceFiles/history/history_widget.cpp index 2c3415cf5e..e5b26d69cd 100644 --- a/Telegram/SourceFiles/history/history_widget.cpp +++ b/Telegram/SourceFiles/history/history_widget.cpp @@ -520,7 +520,7 @@ HistoryWidget::HistoryWidget( connect(_fieldAutocomplete, SIGNAL(mentionChosen(UserData*,FieldAutocomplete::ChooseMethod)), this, SLOT(onMentionInsert(UserData*))); connect(_fieldAutocomplete, SIGNAL(hashtagChosen(QString,FieldAutocomplete::ChooseMethod)), this, SLOT(onHashtagOrBotCommandInsert(QString,FieldAutocomplete::ChooseMethod))); connect(_fieldAutocomplete, SIGNAL(botCommandChosen(QString,FieldAutocomplete::ChooseMethod)), this, SLOT(onHashtagOrBotCommandInsert(QString,FieldAutocomplete::ChooseMethod))); - connect(_fieldAutocomplete, SIGNAL(stickerChosen(DocumentData*,FieldAutocomplete::ChooseMethod)), this, SLOT(onStickerSend(DocumentData*))); + connect(_fieldAutocomplete, SIGNAL(stickerChosen(not_null,FieldAutocomplete::ChooseMethod)), this, SLOT(onStickerOrGifSend(not_null))); connect(_fieldAutocomplete, SIGNAL(moderateKeyActivate(int,bool*)), this, SLOT(onModerateKeyActivate(int,bool*))); _fieldLinksParser = std::make_unique(_field); _fieldLinksParser->list().changes( @@ -1558,21 +1558,6 @@ bool HistoryWidget::cmd_previous_chat() { return false; } -void HistoryWidget::saveGif(DocumentData *doc) { - if (doc->isGifv() && Auth().data().savedGifs().indexOf(doc) != 0) { - MTPInputDocument mtpInput = doc->mtpInput(); - if (mtpInput.type() != mtpc_inputDocumentEmpty) { - MTP::send(MTPmessages_SaveGif(mtpInput, MTP_bool(false)), rpcDone(&HistoryWidget::saveGifDone, doc)); - } - } -} - -void HistoryWidget::saveGifDone(DocumentData *doc, const MTPBool &result) { - if (mtpIsTrue(result)) { - App::addSavedGif(doc); - } -} - void HistoryWidget::clearReplyReturns() { _replyReturns.clear(); _replyReturn = 0; diff --git a/Telegram/SourceFiles/history/history_widget.h b/Telegram/SourceFiles/history/history_widget.h index 30244973f3..8b8446b6f1 100644 --- a/Telegram/SourceFiles/history/history_widget.h +++ b/Telegram/SourceFiles/history/history_widget.h @@ -322,8 +322,6 @@ public: void updateNotifyControls(); - void saveGif(DocumentData *doc); - bool contentOverlapped(const QRect &globalRect); QPixmap grabForShowAnimation(const Window::SectionSlideParams ¶ms); @@ -696,8 +694,6 @@ private: // This one is syntetic. void synteticScrollToY(int y); - void saveGifDone(DocumentData *doc, const MTPBool &result); - void reportSpamDone(PeerData *peer, const MTPBool &result, mtpRequestId request); bool reportSpamFail(const RPCError &error, mtpRequestId request); diff --git a/Telegram/SourceFiles/history/view/history_view_context_menu.cpp b/Telegram/SourceFiles/history/view/history_view_context_menu.cpp index 5a7834d3ba..2b68f4e4e7 100644 --- a/Telegram/SourceFiles/history/view/history_view_context_menu.cpp +++ b/Telegram/SourceFiles/history/view/history_view_context_menu.cpp @@ -28,6 +28,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "messenger.h" #include "mainwidget.h" #include "auth_session.h" +#include "apiwrap.h" namespace HistoryView { namespace { @@ -76,15 +77,13 @@ void ShowStickerPackInfo(not_null document) { } } -void ToggleFavedSticker(not_null document) { - const auto unfave = Stickers::IsFaved(document); - MTP::send( - MTPmessages_FaveSticker( - document->mtpInput(), - MTP_bool(unfave)), - rpcDone([=](const MTPBool &result) { - Stickers::SetFaved(document, !unfave); - })); +void ToggleFavedSticker( + not_null document, + FullMsgId contextId) { + Auth().api().toggleFavedSticker( + document, + contextId, + !Stickers::IsFaved(document)); } void AddPhotoActions( @@ -167,7 +166,7 @@ void AddDocumentActions( lang(Stickers::IsFaved(document) ? lng_faved_stickers_remove : lng_faved_stickers_add), - [=] { ToggleFavedSticker(document); }); + [=] { ToggleFavedSticker(document, contextId); }); } if (!document->filepath( DocumentData::FilePathResolveChecked).isEmpty()) { diff --git a/Telegram/SourceFiles/inline_bots/inline_bot_layout_internal.cpp b/Telegram/SourceFiles/inline_bots/inline_bot_layout_internal.cpp index aa7ef14591..4eb8d529a1 100644 --- a/Telegram/SourceFiles/inline_bots/inline_bot_layout_internal.cpp +++ b/Telegram/SourceFiles/inline_bots/inline_bot_layout_internal.cpp @@ -22,6 +22,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "history/view/history_view_cursor_state.h" #include "storage/localstorage.h" #include "auth_session.h" +#include "apiwrap.h" #include "lang/lang_keys.h" namespace InlineBots { @@ -117,12 +118,12 @@ void Gif::setPosition(int32 position) { } void DeleteSavedGifClickHandler::onClickImpl() const { - auto index = Auth().data().savedGifs().indexOf(_data); + Auth().api().toggleSavedGif(_data, Data::FileOriginSavedGifs(), false); + + const auto index = Auth().data().savedGifs().indexOf(_data); if (index >= 0) { Auth().data().savedGifsRef().remove(index); Local::writeSavedGifs(); - - MTP::send(MTPmessages_SaveGif(_data->mtpInput(), MTP_bool(true))); } Auth().data().notifySavedGifsUpdated(); }