Refresh file references in saveGif/faveSticker.

This commit is contained in:
John Preston 2018-07-16 22:41:43 +03:00
parent c913c77fef
commit 5a487bc30c
11 changed files with 116 additions and 54 deletions

View File

@ -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<not_null<DocumentData*>> *ApiWrap::stickersByEmoji(
return nullptr;
}
void ApiWrap::toggleFavedSticker(
not_null<DocumentData*> document,
Data::FileOrigin origin,
bool faved) {
if (faved && !document->sticker()) {
return;
}
auto failHandler = std::make_shared<Fn<void(const RPCError&)>>();
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<DocumentData*> document,
Data::FileOrigin origin,
bool saved) {
if (saved && !document->isGifv()) {
return;
}
auto failHandler = std::make_shared<Fn<void(const RPCError&)>>();
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<Fn<void(const RPCError&)>>();
const auto replyTo = options.replyTo;
const auto captionText = caption.text;
auto failHandler = std::make_shared<Fn<void(const RPCError&)>>();
auto performRequest = [=] {
history->sendRequestId = request(MTPmessages_SendMedia(
MTP_flags(sendFlags),

View File

@ -156,6 +156,14 @@ public:
const MTPInputStickerSet &set);
std::vector<not_null<DocumentData*>> *stickersByEmoji(
not_null<EmojiPtr> emoji);
void toggleFavedSticker(
not_null<DocumentData*> document,
Data::FileOrigin origin,
bool faved);
void toggleSavedGif(
not_null<DocumentData*> document,
Data::FileOrigin origin,
bool saved);
void joinChannel(not_null<ChannelData*> channel);
void leaveChannel(not_null<ChannelData*> channel);

View File

@ -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<DocumentData*>,FieldAutocomplete::ChooseMethod)), this, SIGNAL(stickerChosen(not_null<DocumentData*>,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()) {

View File

@ -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<DocumentData*> 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<DocumentData*> sticker, FieldAutocomplete::ChooseMethod method) const;
void mustScrollTo(int scrollToTop, int scrollToBottom);
public slots:

View File

@ -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) {

View File

@ -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<DocumentData*> document) {
}
}
void HistoryInner::toggleFavedSticker(not_null<DocumentData*> 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<DocumentData*> 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);
}
}
}

View File

@ -214,7 +214,6 @@ private:
not_null<DocumentData*> document);
void copyContextImage(not_null<PhotoData*> photo);
void showStickerPackInfo(not_null<DocumentData*> document);
void toggleFavedSticker(not_null<DocumentData*> document);
void itemRemoved(not_null<const HistoryItem*> item);
void viewRemoved(not_null<const Element*> view);

View File

@ -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<DocumentData*>,FieldAutocomplete::ChooseMethod)), this, SLOT(onStickerOrGifSend(not_null<DocumentData*>)));
connect(_fieldAutocomplete, SIGNAL(moderateKeyActivate(int,bool*)), this, SLOT(onModerateKeyActivate(int,bool*)));
_fieldLinksParser = std::make_unique<MessageLinksParser>(_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;

View File

@ -322,8 +322,6 @@ public:
void updateNotifyControls();
void saveGif(DocumentData *doc);
bool contentOverlapped(const QRect &globalRect);
QPixmap grabForShowAnimation(const Window::SectionSlideParams &params);
@ -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);

View File

@ -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<DocumentData*> document) {
}
}
void ToggleFavedSticker(not_null<DocumentData*> 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<DocumentData*> 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()) {

View File

@ -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();
}