Moved toggling of existing media to separate file.

This commit is contained in:
23rd 2020-08-17 15:29:27 +03:00
parent 2574389129
commit 8a34f29329
9 changed files with 135 additions and 89 deletions

View File

@ -217,6 +217,8 @@ PRIVATE
api/api_single_message_search.h
api/api_text_entities.cpp
api/api_text_entities.h
api/api_toggling_media.cpp
api/api_toggling_media.h
api/api_updates.cpp
api/api_updates.h
boxes/filters/edit_filter_box.cpp

View File

@ -0,0 +1,96 @@
/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#include "api/api_toggling_media.h"
#include "apiwrap.h"
#include "data/data_document.h"
#include "data/data_file_origin.h"
#include "data/data_session.h"
#include "data/stickers/data_stickers.h" // Stickers::addSavedGif
#include "main/main_session.h"
namespace Api {
namespace {
template <typename MTPToggleRequest, typename DoneCallback>
void ToggleExistingMedia(
not_null<DocumentData*> document,
Data::FileOrigin origin,
bool saved,
DoneCallback &&done) {
const auto api = &document->owner().session().api();
auto performRequest = [=](const auto &repeatRequest) -> void {
const auto usedFileReference = document->fileReference();
api->request(MTPToggleRequest(
document->mtpInput(),
MTP_bool(!saved)
)).done([=](const MTPBool &result) {
if (mtpIsTrue(result)) {
done();
}
}).fail([=](const RPCError &error) {
if (error.code() == 400
&& error.type().startsWith(u"FILE_REFERENCE_"_q)) {
auto refreshed = [=](const Data::UpdatedFileReferences &d) {
if (document->fileReference() != usedFileReference) {
repeatRequest(repeatRequest);
}
};
api->refreshFileReference(origin, std::move(refreshed));
}
}).send();
};
performRequest(performRequest);
}
} // namespace
void ToggleFavedSticker(
not_null<DocumentData*> document,
Data::FileOrigin origin) {
ToggleFavedSticker(
document,
std::move(origin),
!document->owner().stickers().isFaved(document));
}
void ToggleFavedSticker(
not_null<DocumentData*> document,
Data::FileOrigin origin,
bool faved) {
if (faved && !document->sticker()) {
return;
}
ToggleExistingMedia<MTPmessages_FaveSticker>(
document,
std::move(origin),
faved,
[=] { document->owner().stickers().setFaved(document, faved); });
}
void ToggleSavedGif(
not_null<DocumentData*> document,
Data::FileOrigin origin,
bool saved) {
if (saved && !document->isGifv()) {
return;
}
auto done = [=] {
if (saved) {
document->owner().stickers().addSavedGif(document);
}
};
ToggleExistingMedia<MTPmessages_SaveGif>(
document,
std::move(origin),
saved,
std::move(done));
}
} // namespace Api

View File

@ -0,0 +1,26 @@
/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#pragma once
namespace Api {
void ToggleFavedSticker(
not_null<DocumentData*> document,
Data::FileOrigin origin);
void ToggleFavedSticker(
not_null<DocumentData*> document,
Data::FileOrigin origin,
bool faved);
void ToggleSavedGif(
not_null<DocumentData*> document,
Data::FileOrigin origin,
bool saved);
} // namespace Api

View File

@ -2936,72 +2936,6 @@ 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 performRequest = [=](const auto &repeatRequest) -> void {
const auto usedFileReference = document->fileReference();
request(MTPmessages_FaveSticker(
document->mtpInput(),
MTP_bool(!faved)
)).done([=](const MTPBool &result) {
if (mtpIsTrue(result)) {
_session->data().stickers().setFaved(document, faved);
}
}).fail([=](const RPCError &error) {
if (error.code() == 400
&& error.type().startsWith(qstr("FILE_REFERENCE_"))) {
auto refreshed = [=](const UpdatedFileReferences &data) {
if (document->fileReference() != usedFileReference) {
repeatRequest(repeatRequest);
}
};
refreshFileReference(origin, std::move(refreshed));
}
}).send();
};
performRequest(performRequest);
}
void ApiWrap::toggleSavedGif(
not_null<DocumentData*> document,
Data::FileOrigin origin,
bool saved) {
if (saved && !document->isGifv()) {
return;
}
auto performRequest = [=](const auto &repeatRequest) -> void {
const auto usedFileReference = document->fileReference();
request(MTPmessages_SaveGif(
document->mtpInput(),
MTP_bool(!saved)
)).done([=](const MTPBool &result) {
if (mtpIsTrue(result)) {
if (saved) {
_session->data().stickers().addSavedGif(document);
}
}
}).fail([=](const RPCError &error) {
if (error.code() == 400
&& error.type().startsWith(qstr("FILE_REFERENCE_"))) {
auto refreshed = [=](const UpdatedFileReferences &data) {
if (document->fileReference() != usedFileReference) {
repeatRequest(repeatRequest);
}
};
refreshFileReference(origin, std::move(refreshed));
}
}).send();
};
performRequest(performRequest);
}
void ApiWrap::requestStickers(TimeId now) {
if (!_session->data().stickers().updateNeeded(now)
|| _stickersUpdateRequest) {

View File

@ -275,14 +275,6 @@ 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

@ -7,7 +7,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#include "chat_helpers/gifs_list_widget.h"
#include "apiwrap.h" // ApiWrap::toggleSavedGif
#include "api/api_toggling_media.h" // Api::ToggleSavedGif
#include "base/const_string.h"
#include "data/data_photo.h"
#include "data/data_document.h"
@ -48,7 +48,7 @@ constexpr auto kSearchBotUsername = "gif"_cs;
void DeleteSavedGif(not_null<DocumentData*> document) {
auto &data = document->owner();
document->session().api().toggleSavedGif(
Api::ToggleSavedGif(
document,
Data::FileOriginSavedGifs(),
false);

View File

@ -37,6 +37,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "main/main_session.h"
#include "main/main_session_settings.h"
#include "apiwrap.h"
#include "api/api_toggling_media.h" // Api::ToggleFavedSticker
#include "app.h"
#include "styles/style_chat_helpers.h"
#include "styles/style_window.h"
@ -2079,10 +2080,9 @@ void StickersListWidget::fillContextMenu(
SendMenu::DefaultScheduleCallback(this, type, send));
const auto toggleFavedSticker = [=] {
document->session().api().toggleFavedSticker(
Api::ToggleFavedSticker(
document,
Data::FileOriginStickerSet(Data::Stickers::FavedSetId, 0),
!document->owner().stickers().isFaved(document));
Data::FileOriginStickerSet(Data::Stickers::FavedSetId, 0));
};
menu->addAction(
(document->owner().stickers().isFaved(document)
@ -2219,7 +2219,7 @@ void StickersListWidget::removeFavedSticker(int section, int index) {
const auto &sticker = _mySets[section].stickers[index];
const auto document = sticker.document;
session().data().stickers().setFaved(document, false);
session().api().toggleFavedSticker(
Api::ToggleFavedSticker(
document,
Data::FileOriginStickerSet(Data::Stickers::FavedSetId, 0),
false);

View File

@ -45,6 +45,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "main/main_session_settings.h"
#include "core/application.h"
#include "apiwrap.h"
#include "api/api_toggling_media.h"
#include "lang/lang_keys.h"
#include "data/data_session.h"
#include "data/data_media_types.h"
@ -1718,10 +1719,7 @@ void HistoryInner::showContextMenu(QContextMenuEvent *e, bool showFromTouch) {
showStickerPackInfo(document);
});
_menu->addAction(session->data().stickers().isFaved(document) ? tr::lng_faved_stickers_remove(tr::now) : tr::lng_faved_stickers_add(tr::now), [=] {
session->api().toggleFavedSticker(
document,
itemId,
!session->data().stickers().isFaved(document));
Api::ToggleFavedSticker(document, itemId);
});
}
_menu->addAction(tr::lng_context_save_image(tr::now), App::LambdaDelayed(st::defaultDropdownMenu.menu.ripple.hideDuration, this, [=] {
@ -1915,7 +1913,7 @@ void HistoryInner::saveContextGif(FullMsgId itemId) {
if (const auto item = session().data().message(itemId)) {
if (const auto media = item->media()) {
if (const auto document = media->document()) {
session().api().toggleSavedGif(document, item->fullId(), true);
Api::ToggleSavedGif(document, item->fullId(), true);
}
}
}

View File

@ -8,6 +8,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "history/view/history_view_context_menu.h"
#include "api/api_editing.h"
#include "api/api_toggling_media.h" // Api::ToggleFavedSticker
#include "base/unixtime.h"
#include "history/view/history_view_list_widget.h"
#include "history/view/history_view_cursor_state.h"
@ -128,10 +129,7 @@ void ShowStickerPackInfo(not_null<DocumentData*> document) {
void ToggleFavedSticker(
not_null<DocumentData*> document,
FullMsgId contextId) {
document->session().api().toggleFavedSticker(
document,
contextId,
!document->owner().stickers().isFaved(document));
Api::ToggleFavedSticker(document, contextId);
}
void AddPhotoActions(