2020-08-17 12:29:27 +00:00
|
|
|
/*
|
|
|
|
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"
|
2020-08-19 14:04:05 +00:00
|
|
|
#include "data/stickers/data_stickers.h"
|
2022-05-09 13:09:46 +00:00
|
|
|
#include "window/window_session_controller.h"
|
2020-08-17 12:29:27 +00:00
|
|
|
#include "main/main_session.h"
|
|
|
|
|
|
|
|
namespace Api {
|
|
|
|
namespace {
|
|
|
|
|
2022-04-07 16:12:58 +00:00
|
|
|
template <typename ToggleRequestCallback, typename DoneCallback>
|
2020-08-17 12:29:27 +00:00
|
|
|
void ToggleExistingMedia(
|
|
|
|
not_null<DocumentData*> document,
|
|
|
|
Data::FileOrigin origin,
|
2022-04-07 16:12:58 +00:00
|
|
|
ToggleRequestCallback toggleRequest,
|
2020-08-17 12:29:27 +00:00
|
|
|
DoneCallback &&done) {
|
|
|
|
const auto api = &document->owner().session().api();
|
|
|
|
|
|
|
|
auto performRequest = [=](const auto &repeatRequest) -> void {
|
|
|
|
const auto usedFileReference = document->fileReference();
|
2022-04-07 16:12:58 +00:00
|
|
|
api->request(
|
|
|
|
toggleRequest()
|
|
|
|
).done(done).fail([=](const MTP::Error &error) {
|
2020-08-17 12:29:27 +00:00
|
|
|
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(
|
2023-05-02 09:33:19 +00:00
|
|
|
std::shared_ptr<ChatHelpers::Show> show,
|
2020-08-17 12:29:27 +00:00
|
|
|
not_null<DocumentData*> document,
|
|
|
|
Data::FileOrigin origin) {
|
|
|
|
ToggleFavedSticker(
|
2023-05-02 09:33:19 +00:00
|
|
|
std::move(show),
|
2020-08-17 12:29:27 +00:00
|
|
|
document,
|
|
|
|
std::move(origin),
|
|
|
|
!document->owner().stickers().isFaved(document));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ToggleFavedSticker(
|
2023-05-02 09:33:19 +00:00
|
|
|
std::shared_ptr<ChatHelpers::Show> show,
|
2020-08-17 12:29:27 +00:00
|
|
|
not_null<DocumentData*> document,
|
|
|
|
Data::FileOrigin origin,
|
|
|
|
bool faved) {
|
|
|
|
if (faved && !document->sticker()) {
|
|
|
|
return;
|
|
|
|
}
|
2022-05-09 13:09:46 +00:00
|
|
|
auto done = [=] {
|
2023-05-02 09:33:19 +00:00
|
|
|
document->owner().stickers().setFaved(show, document, faved);
|
2022-05-09 13:09:46 +00:00
|
|
|
};
|
2020-08-19 14:04:05 +00:00
|
|
|
ToggleExistingMedia(
|
2020-08-17 12:29:27 +00:00
|
|
|
document,
|
|
|
|
std::move(origin),
|
2022-04-07 16:12:58 +00:00
|
|
|
[=, d = document] {
|
|
|
|
return MTPmessages_FaveSticker(d->mtpInput(), MTP_bool(!faved));
|
|
|
|
},
|
2022-05-09 13:09:46 +00:00
|
|
|
std::move(done));
|
2020-08-17 12:29:27 +00:00
|
|
|
}
|
|
|
|
|
2020-08-19 14:04:05 +00:00
|
|
|
void ToggleRecentSticker(
|
|
|
|
not_null<DocumentData*> document,
|
|
|
|
Data::FileOrigin origin,
|
|
|
|
bool saved) {
|
|
|
|
if (!document->sticker()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto done = [=] {
|
|
|
|
if (!saved) {
|
|
|
|
document->owner().stickers().removeFromRecentSet(document);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
ToggleExistingMedia(
|
|
|
|
document,
|
|
|
|
std::move(origin),
|
2022-04-07 16:12:58 +00:00
|
|
|
[=] {
|
|
|
|
return MTPmessages_SaveRecentSticker(
|
|
|
|
MTP_flags(MTPmessages_SaveRecentSticker::Flag(0)),
|
|
|
|
document->mtpInput(),
|
|
|
|
MTP_bool(!saved));
|
|
|
|
},
|
2020-08-19 14:04:05 +00:00
|
|
|
std::move(done));
|
|
|
|
}
|
|
|
|
|
2020-08-17 12:29:27 +00:00
|
|
|
void ToggleSavedGif(
|
2023-05-02 09:33:19 +00:00
|
|
|
std::shared_ptr<ChatHelpers::Show> show,
|
2020-08-17 12:29:27 +00:00
|
|
|
not_null<DocumentData*> document,
|
|
|
|
Data::FileOrigin origin,
|
|
|
|
bool saved) {
|
|
|
|
if (saved && !document->isGifv()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto done = [=] {
|
|
|
|
if (saved) {
|
2023-05-02 09:33:19 +00:00
|
|
|
document->owner().stickers().addSavedGif(show, document);
|
2020-08-17 12:29:27 +00:00
|
|
|
}
|
|
|
|
};
|
2020-08-19 14:04:05 +00:00
|
|
|
ToggleExistingMedia(
|
2020-08-17 12:29:27 +00:00
|
|
|
document,
|
|
|
|
std::move(origin),
|
2022-04-07 16:12:58 +00:00
|
|
|
[=, d = document] {
|
|
|
|
return MTPmessages_SaveGif(d->mtpInput(), MTP_bool(!saved));
|
|
|
|
},
|
2020-08-17 12:29:27 +00:00
|
|
|
std::move(done));
|
|
|
|
}
|
|
|
|
|
2022-04-07 14:57:07 +00:00
|
|
|
void ToggleSavedRingtone(
|
|
|
|
not_null<DocumentData*> document,
|
|
|
|
Data::FileOrigin origin,
|
|
|
|
Fn<void()> &&done,
|
|
|
|
bool saved) {
|
|
|
|
ToggleExistingMedia(
|
|
|
|
document,
|
|
|
|
std::move(origin),
|
2022-04-07 16:12:58 +00:00
|
|
|
[=, d = document] {
|
|
|
|
return MTPaccount_SaveRingtone(d->mtpInput(), MTP_bool(!saved));
|
|
|
|
},
|
2022-04-07 14:57:07 +00:00
|
|
|
std::move(done));
|
|
|
|
}
|
|
|
|
|
2020-08-17 12:29:27 +00:00
|
|
|
} // namespace Api
|