2018-12-31 20:04:05 +01:00
|
|
|
//
|
2022-01-01 01:35:39 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#include "td/telegram/StickersManager.h"
|
|
|
|
|
|
|
|
#include "td/telegram/AccessRights.h"
|
|
|
|
#include "td/telegram/AuthManager.h"
|
2020-05-17 00:06:23 +02:00
|
|
|
#include "td/telegram/ConfigManager.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/ContactsManager.h"
|
|
|
|
#include "td/telegram/DialogId.h"
|
2019-04-09 17:38:57 +02:00
|
|
|
#include "td/telegram/Document.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/DocumentsManager.h"
|
2019-01-24 20:41:05 +01:00
|
|
|
#include "td/telegram/FileReferenceManager.h"
|
2019-01-19 22:54:29 +01:00
|
|
|
#include "td/telegram/files/FileLocation.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/files/FileManager.h"
|
2019-01-19 22:54:29 +01:00
|
|
|
#include "td/telegram/files/FileType.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/Global.h"
|
2019-05-21 17:48:35 +02:00
|
|
|
#include "td/telegram/LanguagePackManager.h"
|
2019-07-23 00:50:12 +02:00
|
|
|
#include "td/telegram/logevent/LogEvent.h"
|
2022-08-12 17:39:36 +02:00
|
|
|
#include "td/telegram/logevent/LogEventHelper.h"
|
2022-09-12 16:28:58 +02:00
|
|
|
#include "td/telegram/MessageReaction.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/MessagesManager.h"
|
|
|
|
#include "td/telegram/misc.h"
|
2018-06-26 01:43:11 +02:00
|
|
|
#include "td/telegram/net/DcId.h"
|
2019-06-12 15:05:22 +02:00
|
|
|
#include "td/telegram/net/MtprotoHeader.h"
|
2021-10-05 23:55:22 +02:00
|
|
|
#include "td/telegram/net/NetQueryDispatcher.h"
|
2022-08-18 16:00:18 +02:00
|
|
|
#include "td/telegram/OptionManager.h"
|
2021-11-04 10:46:08 +01:00
|
|
|
#include "td/telegram/PhotoSizeSource.h"
|
2021-05-27 20:15:30 +02:00
|
|
|
#include "td/telegram/secret_api.h"
|
2022-08-05 13:06:06 +02:00
|
|
|
#include "td/telegram/SecretChatLayer.h"
|
2019-09-18 05:55:43 +02:00
|
|
|
#include "td/telegram/StickerSetId.hpp"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/StickersManager.hpp"
|
|
|
|
#include "td/telegram/Td.h"
|
2021-05-27 20:15:30 +02:00
|
|
|
#include "td/telegram/td_api.h"
|
2021-06-02 14:43:56 +02:00
|
|
|
#include "td/telegram/TdDb.h"
|
2021-09-18 23:47:05 +02:00
|
|
|
#include "td/telegram/TdParameters.h"
|
2021-05-27 20:15:30 +02:00
|
|
|
#include "td/telegram/telegram_api.h"
|
2022-06-21 01:55:53 +02:00
|
|
|
#include "td/telegram/Version.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-09-18 23:47:05 +02:00
|
|
|
#include "td/db/SqliteKeyValue.h"
|
|
|
|
#include "td/db/SqliteKeyValueAsync.h"
|
|
|
|
|
2020-04-19 23:41:26 +02:00
|
|
|
#include "td/actor/SleepActor.h"
|
2018-07-18 03:11:48 +02:00
|
|
|
|
2021-01-01 13:59:53 +01:00
|
|
|
#include "td/utils/algorithm.h"
|
2021-11-04 10:46:08 +01:00
|
|
|
#include "td/utils/base64.h"
|
2021-09-21 05:32:34 +02:00
|
|
|
#include "td/utils/emoji.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/format.h"
|
2021-09-16 22:22:34 +02:00
|
|
|
#include "td/utils/JsonBuilder.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/logging.h"
|
2021-10-05 22:20:49 +02:00
|
|
|
#include "td/utils/MimeType.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/misc.h"
|
2020-05-31 00:49:16 +02:00
|
|
|
#include "td/utils/PathView.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/Random.h"
|
|
|
|
#include "td/utils/Slice.h"
|
2021-05-17 14:21:11 +02:00
|
|
|
#include "td/utils/SliceBuilder.h"
|
2020-12-22 13:51:57 +01:00
|
|
|
#include "td/utils/StackAllocator.h"
|
|
|
|
#include "td/utils/StringBuilder.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/Time.h"
|
|
|
|
#include "td/utils/tl_helpers.h"
|
2019-06-12 15:05:22 +02:00
|
|
|
#include "td/utils/utf8.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
#include <algorithm>
|
2021-09-17 14:51:42 +02:00
|
|
|
#include <cmath>
|
2020-04-30 21:21:42 +02:00
|
|
|
#include <limits>
|
2018-12-31 20:04:05 +01:00
|
|
|
#include <type_traits>
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
2022-01-19 12:14:21 +01:00
|
|
|
class GetAvailableReactionsQuery final : public Td::ResultHandler {
|
|
|
|
public:
|
|
|
|
void send(int32 hash) {
|
|
|
|
send_query(G()->net_query_creator().create(telegram_api::messages_getAvailableReactions(hash)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_result(BufferSlice packet) final {
|
|
|
|
auto result_ptr = fetch_result<telegram_api::messages_getAvailableReactions>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
|
|
|
return on_error(result_ptr.move_as_error());
|
|
|
|
}
|
|
|
|
|
|
|
|
auto ptr = result_ptr.move_as_ok();
|
|
|
|
LOG(INFO) << "Receive result for GetAvailableReactionsQuery: " << to_string(ptr);
|
|
|
|
td_->stickers_manager_->on_get_available_reactions(std::move(ptr));
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_error(Status status) final {
|
|
|
|
LOG(INFO) << "Receive error for GetAvailableReactionsQuery: " << status;
|
|
|
|
td_->stickers_manager_->on_get_available_reactions(nullptr);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-09-12 18:14:02 +02:00
|
|
|
class GetRecentReactionsQuery final : public Td::ResultHandler {
|
|
|
|
public:
|
2022-09-12 19:17:14 +02:00
|
|
|
void send(int32 limit, int64 hash) {
|
|
|
|
send_query(G()->net_query_creator().create(telegram_api::messages_getRecentReactions(limit, hash)));
|
2022-09-12 18:14:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void on_result(BufferSlice packet) final {
|
|
|
|
auto result_ptr = fetch_result<telegram_api::messages_getRecentReactions>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
|
|
|
return on_error(result_ptr.move_as_error());
|
|
|
|
}
|
|
|
|
|
|
|
|
auto ptr = result_ptr.move_as_ok();
|
|
|
|
LOG(INFO) << "Receive result for GetRecentReactionsQuery: " << to_string(ptr);
|
|
|
|
td_->stickers_manager_->on_get_recent_reactions(std::move(ptr));
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_error(Status status) final {
|
|
|
|
LOG(INFO) << "Receive error for GetRecentReactionsQuery: " << status;
|
|
|
|
td_->stickers_manager_->on_get_recent_reactions(nullptr);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-09-12 16:28:58 +02:00
|
|
|
class GetTopReactionsQuery final : public Td::ResultHandler {
|
|
|
|
public:
|
|
|
|
void send(int64 hash) {
|
|
|
|
send_query(G()->net_query_creator().create(telegram_api::messages_getTopReactions(50, hash)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_result(BufferSlice packet) final {
|
|
|
|
auto result_ptr = fetch_result<telegram_api::messages_getTopReactions>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
|
|
|
return on_error(result_ptr.move_as_error());
|
|
|
|
}
|
|
|
|
|
|
|
|
auto ptr = result_ptr.move_as_ok();
|
|
|
|
LOG(INFO) << "Receive result for GetTopReactionsQuery: " << to_string(ptr);
|
|
|
|
td_->stickers_manager_->on_get_top_reactions(std::move(ptr));
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_error(Status status) final {
|
|
|
|
LOG(INFO) << "Receive error for GetTopReactionsQuery: " << status;
|
|
|
|
td_->stickers_manager_->on_get_top_reactions(nullptr);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-09-12 18:37:45 +02:00
|
|
|
class ClearRecentReactionsQuery final : public Td::ResultHandler {
|
|
|
|
Promise<Unit> promise_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit ClearRecentReactionsQuery(Promise<Unit> &&promise) : promise_(std::move(promise)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void send() {
|
|
|
|
send_query(G()->net_query_creator().create(telegram_api::messages_clearRecentReactions()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_result(BufferSlice packet) final {
|
|
|
|
auto result_ptr = fetch_result<telegram_api::messages_clearRecentReactions>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
|
|
|
return on_error(result_ptr.move_as_error());
|
|
|
|
}
|
|
|
|
|
|
|
|
td_->stickers_manager_->reload_recent_reactions();
|
|
|
|
promise_.set_value(Unit());
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_error(Status status) final {
|
|
|
|
if (!G()->is_expected_error(status)) {
|
|
|
|
LOG(ERROR) << "Receive error for clear recent reactions: " << status;
|
|
|
|
}
|
|
|
|
td_->stickers_manager_->reload_recent_reactions();
|
|
|
|
promise_.set_error(std::move(status));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class GetAllStickersQuery final : public Td::ResultHandler {
|
2022-07-14 15:17:19 +02:00
|
|
|
StickerType sticker_type_;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
public:
|
2022-07-14 15:17:19 +02:00
|
|
|
void send(StickerType sticker_type, int64 hash) {
|
|
|
|
sticker_type_ = sticker_type;
|
2022-07-14 15:42:27 +02:00
|
|
|
switch (sticker_type) {
|
|
|
|
case StickerType::Regular:
|
|
|
|
return send_query(G()->net_query_creator().create(telegram_api::messages_getAllStickers(hash)));
|
|
|
|
case StickerType::Mask:
|
|
|
|
return send_query(G()->net_query_creator().create(telegram_api::messages_getMaskStickers(hash)));
|
2022-07-19 13:51:29 +02:00
|
|
|
case StickerType::CustomEmoji:
|
2022-07-14 15:42:27 +02:00
|
|
|
return send_query(G()->net_query_creator().create(telegram_api::messages_getEmojiStickers(hash)));
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_result(BufferSlice packet) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
static_assert(std::is_same<telegram_api::messages_getMaskStickers::ReturnType,
|
|
|
|
telegram_api::messages_getAllStickers::ReturnType>::value,
|
|
|
|
"");
|
|
|
|
auto result_ptr = fetch_result<telegram_api::messages_getAllStickers>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
2021-11-08 12:19:57 +01:00
|
|
|
return on_error(result_ptr.move_as_error());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
auto ptr = result_ptr.move_as_ok();
|
2022-07-14 15:17:19 +02:00
|
|
|
LOG(DEBUG) << "Receive result for get all " << sticker_type_ << " stickers: " << to_string(ptr);
|
|
|
|
td_->stickers_manager_->on_get_installed_sticker_sets(sticker_type_, std::move(ptr));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_error(Status status) final {
|
2020-03-30 23:35:58 +02:00
|
|
|
if (!G()->is_expected_error(status)) {
|
2019-02-14 16:36:16 +01:00
|
|
|
LOG(ERROR) << "Receive error for get all stickers: " << status;
|
|
|
|
}
|
2022-07-14 15:17:19 +02:00
|
|
|
td_->stickers_manager_->on_get_installed_sticker_sets_failed(sticker_type_, std::move(status));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class SearchStickersQuery final : public Td::ResultHandler {
|
2018-03-09 18:04:44 +01:00
|
|
|
string emoji_;
|
|
|
|
|
|
|
|
public:
|
2021-09-03 11:27:59 +02:00
|
|
|
void send(string emoji, int64 hash) {
|
2018-03-09 18:04:44 +01:00
|
|
|
emoji_ = std::move(emoji);
|
2021-01-15 12:53:00 +01:00
|
|
|
send_query(G()->net_query_creator().create(telegram_api::messages_getStickers(emoji_, hash)));
|
2018-03-09 18:04:44 +01:00
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_result(BufferSlice packet) final {
|
2018-03-09 18:04:44 +01:00
|
|
|
auto result_ptr = fetch_result<telegram_api::messages_getStickers>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
2021-11-08 12:19:57 +01:00
|
|
|
return on_error(result_ptr.move_as_error());
|
2018-03-09 18:04:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
auto ptr = result_ptr.move_as_ok();
|
|
|
|
LOG(INFO) << "Receive result for search stickers: " << to_string(ptr);
|
2021-11-08 13:20:38 +01:00
|
|
|
td_->stickers_manager_->on_find_stickers_success(emoji_, std::move(ptr));
|
2018-03-09 18:04:44 +01:00
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_error(Status status) final {
|
2020-03-30 23:35:58 +02:00
|
|
|
if (!G()->is_expected_error(status)) {
|
2019-02-14 16:36:16 +01:00
|
|
|
LOG(ERROR) << "Receive error for search stickers: " << status;
|
|
|
|
}
|
2021-11-08 13:20:38 +01:00
|
|
|
td_->stickers_manager_->on_find_stickers_fail(emoji_, std::move(status));
|
2018-03-09 18:04:44 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class GetEmojiKeywordsLanguageQuery final : public Td::ResultHandler {
|
2019-05-21 17:48:35 +02:00
|
|
|
Promise<vector<string>> promise_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit GetEmojiKeywordsLanguageQuery(Promise<vector<string>> &&promise) : promise_(std::move(promise)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void send(vector<string> &&language_codes) {
|
2020-03-15 22:17:11 +01:00
|
|
|
send_query(
|
|
|
|
G()->net_query_creator().create(telegram_api::messages_getEmojiKeywordsLanguages(std::move(language_codes))));
|
2019-05-21 17:48:35 +02:00
|
|
|
}
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_result(BufferSlice packet) final {
|
2019-05-21 17:48:35 +02:00
|
|
|
auto result_ptr = fetch_result<telegram_api::messages_getEmojiKeywordsLanguages>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
2021-11-08 12:19:57 +01:00
|
|
|
return on_error(result_ptr.move_as_error());
|
2019-05-21 17:48:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
auto result =
|
|
|
|
transform(result_ptr.move_as_ok(), [](auto &&emoji_language) { return std::move(emoji_language->lang_code_); });
|
|
|
|
promise_.set_value(std::move(result));
|
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_error(Status status) final {
|
2019-05-21 17:48:35 +02:00
|
|
|
promise_.set_error(std::move(status));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class GetEmojiKeywordsQuery final : public Td::ResultHandler {
|
2019-05-21 17:48:35 +02:00
|
|
|
Promise<telegram_api::object_ptr<telegram_api::emojiKeywordsDifference>> promise_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit GetEmojiKeywordsQuery(Promise<telegram_api::object_ptr<telegram_api::emojiKeywordsDifference>> &&promise)
|
|
|
|
: promise_(std::move(promise)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void send(const string &language_code) {
|
2020-03-15 22:17:11 +01:00
|
|
|
send_query(G()->net_query_creator().create(telegram_api::messages_getEmojiKeywords(language_code)));
|
2019-05-21 17:48:35 +02:00
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_result(BufferSlice packet) final {
|
2019-05-21 17:48:35 +02:00
|
|
|
auto result_ptr = fetch_result<telegram_api::messages_getEmojiKeywords>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
2021-11-08 12:19:57 +01:00
|
|
|
return on_error(result_ptr.move_as_error());
|
2019-05-21 17:48:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
promise_.set_value(result_ptr.move_as_ok());
|
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_error(Status status) final {
|
2019-05-21 17:48:35 +02:00
|
|
|
promise_.set_error(std::move(status));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class GetEmojiKeywordsDifferenceQuery final : public Td::ResultHandler {
|
2019-05-21 19:42:41 +02:00
|
|
|
Promise<telegram_api::object_ptr<telegram_api::emojiKeywordsDifference>> promise_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit GetEmojiKeywordsDifferenceQuery(
|
|
|
|
Promise<telegram_api::object_ptr<telegram_api::emojiKeywordsDifference>> &&promise)
|
|
|
|
: promise_(std::move(promise)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void send(const string &language_code, int32 version) {
|
2020-03-15 22:17:11 +01:00
|
|
|
send_query(
|
|
|
|
G()->net_query_creator().create(telegram_api::messages_getEmojiKeywordsDifference(language_code, version)));
|
2019-05-21 19:42:41 +02:00
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_result(BufferSlice packet) final {
|
2019-05-21 19:42:41 +02:00
|
|
|
auto result_ptr = fetch_result<telegram_api::messages_getEmojiKeywordsDifference>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
2021-11-08 12:19:57 +01:00
|
|
|
return on_error(result_ptr.move_as_error());
|
2019-05-21 19:42:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
promise_.set_value(result_ptr.move_as_ok());
|
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_error(Status status) final {
|
2019-05-21 19:42:41 +02:00
|
|
|
promise_.set_error(std::move(status));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class GetEmojiUrlQuery final : public Td::ResultHandler {
|
2019-05-21 21:44:13 +02:00
|
|
|
Promise<telegram_api::object_ptr<telegram_api::emojiURL>> promise_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit GetEmojiUrlQuery(Promise<telegram_api::object_ptr<telegram_api::emojiURL>> &&promise)
|
|
|
|
: promise_(std::move(promise)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void send(const string &language_code) {
|
2020-03-15 22:17:11 +01:00
|
|
|
send_query(G()->net_query_creator().create(telegram_api::messages_getEmojiURL(language_code)));
|
2019-05-21 21:44:13 +02:00
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_result(BufferSlice packet) final {
|
2019-05-21 21:44:13 +02:00
|
|
|
auto result_ptr = fetch_result<telegram_api::messages_getEmojiURL>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
2021-11-08 12:19:57 +01:00
|
|
|
return on_error(result_ptr.move_as_error());
|
2019-05-21 21:44:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
promise_.set_value(result_ptr.move_as_ok());
|
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_error(Status status) final {
|
2019-05-21 21:44:13 +02:00
|
|
|
promise_.set_error(std::move(status));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class GetArchivedStickerSetsQuery final : public Td::ResultHandler {
|
2018-12-31 20:04:05 +01:00
|
|
|
Promise<Unit> promise_;
|
2019-09-18 05:55:43 +02:00
|
|
|
StickerSetId offset_sticker_set_id_;
|
2022-07-14 15:17:19 +02:00
|
|
|
StickerType sticker_type_;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
public:
|
|
|
|
explicit GetArchivedStickerSetsQuery(Promise<Unit> &&promise) : promise_(std::move(promise)) {
|
|
|
|
}
|
|
|
|
|
2022-07-14 15:17:19 +02:00
|
|
|
void send(StickerType sticker_type, StickerSetId offset_sticker_set_id, int32 limit) {
|
2019-06-05 01:32:10 +02:00
|
|
|
offset_sticker_set_id_ = offset_sticker_set_id;
|
2022-07-14 15:17:19 +02:00
|
|
|
sticker_type_ = sticker_type;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
int32 flags = 0;
|
2022-07-14 15:17:19 +02:00
|
|
|
if (sticker_type_ == StickerType::Mask) {
|
2018-12-31 20:04:05 +01:00
|
|
|
flags |= telegram_api::messages_getArchivedStickers::MASKS_MASK;
|
|
|
|
}
|
2022-07-19 13:51:29 +02:00
|
|
|
if (sticker_type_ == StickerType::CustomEmoji) {
|
2022-07-14 15:42:27 +02:00
|
|
|
flags |= telegram_api::messages_getArchivedStickers::EMOJIS_MASK;
|
|
|
|
}
|
2022-07-01 13:33:59 +02:00
|
|
|
send_query(G()->net_query_creator().create(telegram_api::messages_getArchivedStickers(
|
|
|
|
flags, false /*ignored*/, false /*ignored*/, offset_sticker_set_id.get(), limit)));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_result(BufferSlice packet) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto result_ptr = fetch_result<telegram_api::messages_getArchivedStickers>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
2021-11-08 12:19:57 +01:00
|
|
|
return on_error(result_ptr.move_as_error());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
auto ptr = result_ptr.move_as_ok();
|
2020-09-24 15:08:04 +02:00
|
|
|
LOG(INFO) << "Receive result for GetArchivedStickerSetsQuery: " << to_string(ptr);
|
2022-07-14 15:17:19 +02:00
|
|
|
td_->stickers_manager_->on_get_archived_sticker_sets(sticker_type_, offset_sticker_set_id_, std::move(ptr->sets_),
|
2021-11-08 13:20:38 +01:00
|
|
|
ptr->count_);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
promise_.set_value(Unit());
|
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_error(Status status) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
promise_.set_error(std::move(status));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class GetFeaturedStickerSetsQuery final : public Td::ResultHandler {
|
2022-07-17 21:39:22 +02:00
|
|
|
StickerType sticker_type_;
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
2022-07-17 21:39:22 +02:00
|
|
|
void send(StickerType sticker_type, int64 hash) {
|
|
|
|
sticker_type_ = sticker_type;
|
|
|
|
switch (sticker_type) {
|
|
|
|
case StickerType::Regular:
|
|
|
|
send_query(G()->net_query_creator().create(telegram_api::messages_getFeaturedStickers(hash)));
|
|
|
|
break;
|
2022-07-19 13:51:29 +02:00
|
|
|
case StickerType::CustomEmoji:
|
2022-07-17 21:39:22 +02:00
|
|
|
send_query(G()->net_query_creator().create(telegram_api::messages_getFeaturedEmojiStickers(hash)));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_result(BufferSlice packet) final {
|
2022-07-17 21:39:22 +02:00
|
|
|
static_assert(std::is_same<telegram_api::messages_getFeaturedStickers::ReturnType,
|
|
|
|
telegram_api::messages_getFeaturedEmojiStickers::ReturnType>::value,
|
|
|
|
"");
|
2018-12-31 20:04:05 +01:00
|
|
|
auto result_ptr = fetch_result<telegram_api::messages_getFeaturedStickers>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
2021-11-08 12:19:57 +01:00
|
|
|
return on_error(result_ptr.move_as_error());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
auto ptr = result_ptr.move_as_ok();
|
2020-09-24 15:08:04 +02:00
|
|
|
LOG(DEBUG) << "Receive result for GetFeaturedStickerSetsQuery: " << to_string(ptr);
|
2022-07-17 21:39:22 +02:00
|
|
|
td_->stickers_manager_->on_get_featured_sticker_sets(sticker_type_, -1, -1, 0, std::move(ptr));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_error(Status status) final {
|
2022-07-17 21:39:22 +02:00
|
|
|
td_->stickers_manager_->on_get_featured_sticker_sets_failed(sticker_type_, -1, -1, 0, std::move(status));
|
2020-04-18 00:55:54 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class GetOldFeaturedStickerSetsQuery final : public Td::ResultHandler {
|
2020-04-18 00:55:54 +02:00
|
|
|
int32 offset_;
|
|
|
|
int32 limit_;
|
|
|
|
uint32 generation_;
|
|
|
|
|
|
|
|
public:
|
2022-07-17 21:39:22 +02:00
|
|
|
void send(StickerType sticker_type, int32 offset, int32 limit, uint32 generation) {
|
|
|
|
CHECK(sticker_type == StickerType::Regular);
|
2020-04-18 00:55:54 +02:00
|
|
|
offset_ = offset;
|
|
|
|
limit_ = limit;
|
|
|
|
generation_ = generation;
|
|
|
|
send_query(G()->net_query_creator().create(telegram_api::messages_getOldFeaturedStickers(offset, limit, 0)));
|
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_result(BufferSlice packet) final {
|
2020-04-18 00:55:54 +02:00
|
|
|
auto result_ptr = fetch_result<telegram_api::messages_getOldFeaturedStickers>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
2021-11-08 12:19:57 +01:00
|
|
|
return on_error(result_ptr.move_as_error());
|
2020-04-18 00:55:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
auto ptr = result_ptr.move_as_ok();
|
2020-09-24 15:08:04 +02:00
|
|
|
LOG(DEBUG) << "Receive result for GetOldFeaturedStickerSetsQuery: " << to_string(ptr);
|
2022-07-17 21:39:22 +02:00
|
|
|
td_->stickers_manager_->on_get_featured_sticker_sets(StickerType::Regular, offset_, limit_, generation_,
|
|
|
|
std::move(ptr));
|
2020-04-18 00:55:54 +02:00
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_error(Status status) final {
|
2022-07-17 21:39:22 +02:00
|
|
|
td_->stickers_manager_->on_get_featured_sticker_sets_failed(StickerType::Regular, offset_, limit_, generation_,
|
|
|
|
std::move(status));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class GetAttachedStickerSetsQuery final : public Td::ResultHandler {
|
2018-12-31 20:04:05 +01:00
|
|
|
Promise<Unit> promise_;
|
|
|
|
FileId file_id_;
|
2019-01-24 20:41:05 +01:00
|
|
|
string file_reference_;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
public:
|
|
|
|
explicit GetAttachedStickerSetsQuery(Promise<Unit> &&promise) : promise_(std::move(promise)) {
|
|
|
|
}
|
|
|
|
|
2019-01-24 20:41:05 +01:00
|
|
|
void send(FileId file_id, string &&file_reference,
|
|
|
|
tl_object_ptr<telegram_api::InputStickeredMedia> &&input_stickered_media) {
|
2018-12-31 20:04:05 +01:00
|
|
|
file_id_ = file_id;
|
2019-01-24 20:41:05 +01:00
|
|
|
file_reference_ = std::move(file_reference);
|
2020-03-15 22:17:11 +01:00
|
|
|
send_query(
|
|
|
|
G()->net_query_creator().create(telegram_api::messages_getAttachedStickers(std::move(input_stickered_media))));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_result(BufferSlice packet) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto result_ptr = fetch_result<telegram_api::messages_getAttachedStickers>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
2021-11-08 12:19:57 +01:00
|
|
|
return on_error(result_ptr.move_as_error());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-11-08 13:20:38 +01:00
|
|
|
td_->stickers_manager_->on_get_attached_sticker_sets(file_id_, result_ptr.move_as_ok());
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
promise_.set_value(Unit());
|
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_error(Status status) final {
|
2021-11-08 13:20:38 +01:00
|
|
|
if (!td_->auth_manager_->is_bot() && FileReferenceManager::is_file_reference_error(status)) {
|
2019-02-08 15:54:23 +01:00
|
|
|
VLOG(file_references) << "Receive " << status << " for " << file_id_;
|
2021-11-08 13:20:38 +01:00
|
|
|
td_->file_manager_->delete_file_reference(file_id_, file_reference_);
|
|
|
|
td_->file_reference_manager_->repair_file_reference(
|
2019-01-24 20:41:05 +01:00
|
|
|
file_id_,
|
|
|
|
PromiseCreator::lambda([file_id = file_id_, promise = std::move(promise_)](Result<Unit> result) mutable {
|
|
|
|
if (result.is_error()) {
|
|
|
|
return promise.set_error(Status::Error(400, "Failed to find the file"));
|
|
|
|
}
|
|
|
|
|
|
|
|
send_closure(G()->stickers_manager(), &StickersManager::send_get_attached_stickers_query, file_id,
|
|
|
|
std::move(promise));
|
|
|
|
}));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
promise_.set_error(std::move(status));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class GetRecentStickersQuery final : public Td::ResultHandler {
|
2019-02-02 11:54:40 +01:00
|
|
|
bool is_repair_ = false;
|
2019-02-02 11:30:49 +01:00
|
|
|
bool is_attached_ = false;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
public:
|
2021-09-03 11:27:59 +02:00
|
|
|
void send(bool is_repair, bool is_attached, int64 hash) {
|
2019-02-02 11:54:40 +01:00
|
|
|
is_repair_ = is_repair;
|
2018-12-31 20:04:05 +01:00
|
|
|
is_attached_ = is_attached;
|
|
|
|
int32 flags = 0;
|
|
|
|
if (is_attached) {
|
|
|
|
flags |= telegram_api::messages_getRecentStickers::ATTACHED_MASK;
|
|
|
|
}
|
|
|
|
|
|
|
|
send_query(G()->net_query_creator().create(
|
2020-03-15 22:17:11 +01:00
|
|
|
telegram_api::messages_getRecentStickers(flags, is_attached /*ignored*/, hash)));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_result(BufferSlice packet) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto result_ptr = fetch_result<telegram_api::messages_getRecentStickers>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
2021-11-08 12:19:57 +01:00
|
|
|
return on_error(result_ptr.move_as_error());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
auto ptr = result_ptr.move_as_ok();
|
|
|
|
LOG(DEBUG) << "Receive result for get recent " << (is_attached_ ? "attached " : "")
|
|
|
|
<< "stickers: " << to_string(ptr);
|
2021-11-08 13:20:38 +01:00
|
|
|
td_->stickers_manager_->on_get_recent_stickers(is_repair_, is_attached_, std::move(ptr));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_error(Status status) final {
|
2020-03-30 23:35:58 +02:00
|
|
|
if (!G()->is_expected_error(status)) {
|
2019-02-14 16:36:16 +01:00
|
|
|
LOG(ERROR) << "Receive error for get recent " << (is_attached_ ? "attached " : "") << "stickers: " << status;
|
|
|
|
}
|
2021-11-08 13:20:38 +01:00
|
|
|
td_->stickers_manager_->on_get_recent_stickers_failed(is_repair_, is_attached_, std::move(status));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class SaveRecentStickerQuery final : public Td::ResultHandler {
|
2018-12-31 20:04:05 +01:00
|
|
|
Promise<Unit> promise_;
|
2019-02-02 12:41:30 +01:00
|
|
|
FileId file_id_;
|
|
|
|
string file_reference_;
|
|
|
|
bool unsave_ = false;
|
2018-12-31 20:04:05 +01:00
|
|
|
bool is_attached_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit SaveRecentStickerQuery(Promise<Unit> &&promise) : promise_(std::move(promise)) {
|
|
|
|
}
|
|
|
|
|
2019-02-02 12:41:30 +01:00
|
|
|
void send(bool is_attached, FileId file_id, tl_object_ptr<telegram_api::inputDocument> &&input_document,
|
|
|
|
bool unsave) {
|
|
|
|
CHECK(input_document != nullptr);
|
|
|
|
CHECK(file_id.is_valid());
|
|
|
|
file_id_ = file_id;
|
|
|
|
file_reference_ = input_document->file_reference_.as_slice().str();
|
|
|
|
unsave_ = unsave;
|
2018-12-31 20:04:05 +01:00
|
|
|
is_attached_ = is_attached;
|
|
|
|
|
|
|
|
int32 flags = 0;
|
|
|
|
if (is_attached) {
|
|
|
|
flags |= telegram_api::messages_saveRecentSticker::ATTACHED_MASK;
|
|
|
|
}
|
|
|
|
|
2020-03-15 22:17:11 +01:00
|
|
|
send_query(G()->net_query_creator().create(
|
|
|
|
telegram_api::messages_saveRecentSticker(flags, is_attached /*ignored*/, std::move(input_document), unsave)));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_result(BufferSlice packet) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto result_ptr = fetch_result<telegram_api::messages_saveRecentSticker>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
2021-11-08 12:19:57 +01:00
|
|
|
return on_error(result_ptr.move_as_error());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool result = result_ptr.move_as_ok();
|
2019-02-02 11:30:49 +01:00
|
|
|
LOG(INFO) << "Receive result for save recent " << (is_attached_ ? "attached " : "") << "sticker: " << result;
|
2018-12-31 20:04:05 +01:00
|
|
|
if (!result) {
|
2021-11-08 13:20:38 +01:00
|
|
|
td_->stickers_manager_->reload_recent_stickers(is_attached_, true);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
promise_.set_value(Unit());
|
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_error(Status status) final {
|
2021-11-08 13:20:38 +01:00
|
|
|
if (!td_->auth_manager_->is_bot() && FileReferenceManager::is_file_reference_error(status)) {
|
2019-02-08 15:54:23 +01:00
|
|
|
VLOG(file_references) << "Receive " << status << " for " << file_id_;
|
2021-11-08 13:20:38 +01:00
|
|
|
td_->file_manager_->delete_file_reference(file_id_, file_reference_);
|
|
|
|
td_->file_reference_manager_->repair_file_reference(
|
2019-02-02 12:41:30 +01:00
|
|
|
file_id_, PromiseCreator::lambda([sticker_id = file_id_, is_attached = is_attached_, unsave = unsave_,
|
|
|
|
promise = std::move(promise_)](Result<Unit> result) mutable {
|
|
|
|
if (result.is_error()) {
|
|
|
|
return promise.set_error(Status::Error(400, "Failed to find the sticker"));
|
|
|
|
}
|
|
|
|
|
|
|
|
send_closure(G()->stickers_manager(), &StickersManager::send_save_recent_sticker_query, is_attached,
|
|
|
|
sticker_id, unsave, std::move(promise));
|
|
|
|
}));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-03-30 23:35:58 +02:00
|
|
|
if (!G()->is_expected_error(status)) {
|
2019-02-14 16:36:16 +01:00
|
|
|
LOG(ERROR) << "Receive error for save recent " << (is_attached_ ? "attached " : "") << "sticker: " << status;
|
|
|
|
}
|
2021-11-08 13:20:38 +01:00
|
|
|
td_->stickers_manager_->reload_recent_stickers(is_attached_, true);
|
2018-12-31 20:04:05 +01:00
|
|
|
promise_.set_error(std::move(status));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class ClearRecentStickersQuery final : public Td::ResultHandler {
|
2018-12-31 20:04:05 +01:00
|
|
|
Promise<Unit> promise_;
|
|
|
|
bool is_attached_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit ClearRecentStickersQuery(Promise<Unit> &&promise) : promise_(std::move(promise)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void send(bool is_attached) {
|
|
|
|
is_attached_ = is_attached;
|
|
|
|
|
|
|
|
int32 flags = 0;
|
|
|
|
if (is_attached) {
|
|
|
|
flags |= telegram_api::messages_clearRecentStickers::ATTACHED_MASK;
|
|
|
|
}
|
|
|
|
|
2020-03-15 22:17:11 +01:00
|
|
|
send_query(
|
|
|
|
G()->net_query_creator().create(telegram_api::messages_clearRecentStickers(flags, is_attached /*ignored*/)));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_result(BufferSlice packet) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto result_ptr = fetch_result<telegram_api::messages_clearRecentStickers>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
2021-11-08 12:19:57 +01:00
|
|
|
return on_error(result_ptr.move_as_error());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool result = result_ptr.move_as_ok();
|
2019-02-02 11:30:49 +01:00
|
|
|
LOG(INFO) << "Receive result for clear recent " << (is_attached_ ? "attached " : "") << "stickers: " << result;
|
2018-12-31 20:04:05 +01:00
|
|
|
if (!result) {
|
2021-11-08 13:20:38 +01:00
|
|
|
td_->stickers_manager_->reload_recent_stickers(is_attached_, true);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
promise_.set_value(Unit());
|
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_error(Status status) final {
|
2020-03-30 23:35:58 +02:00
|
|
|
if (!G()->is_expected_error(status)) {
|
2019-02-14 16:36:16 +01:00
|
|
|
LOG(ERROR) << "Receive error for clear recent " << (is_attached_ ? "attached " : "") << "stickers: " << status;
|
|
|
|
}
|
2021-11-08 13:20:38 +01:00
|
|
|
td_->stickers_manager_->reload_recent_stickers(is_attached_, true);
|
2018-12-31 20:04:05 +01:00
|
|
|
promise_.set_error(std::move(status));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class GetFavedStickersQuery final : public Td::ResultHandler {
|
2019-02-02 11:54:40 +01:00
|
|
|
bool is_repair_ = false;
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
2021-09-03 11:27:59 +02:00
|
|
|
void send(bool is_repair, int64 hash) {
|
2019-02-02 11:54:40 +01:00
|
|
|
is_repair_ = is_repair;
|
2020-03-15 22:17:11 +01:00
|
|
|
send_query(G()->net_query_creator().create(telegram_api::messages_getFavedStickers(hash)));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_result(BufferSlice packet) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto result_ptr = fetch_result<telegram_api::messages_getFavedStickers>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
2021-11-08 12:19:57 +01:00
|
|
|
return on_error(result_ptr.move_as_error());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
auto ptr = result_ptr.move_as_ok();
|
2021-11-08 13:20:38 +01:00
|
|
|
td_->stickers_manager_->on_get_favorite_stickers(is_repair_, std::move(ptr));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_error(Status status) final {
|
2020-03-30 23:35:58 +02:00
|
|
|
if (!G()->is_expected_error(status)) {
|
2019-02-14 16:36:16 +01:00
|
|
|
LOG(ERROR) << "Receive error for get favorite stickers: " << status;
|
|
|
|
}
|
2021-11-08 13:20:38 +01:00
|
|
|
td_->stickers_manager_->on_get_favorite_stickers_failed(is_repair_, std::move(status));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class FaveStickerQuery final : public Td::ResultHandler {
|
2019-02-02 12:14:20 +01:00
|
|
|
FileId file_id_;
|
|
|
|
string file_reference_;
|
|
|
|
bool unsave_ = false;
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
Promise<Unit> promise_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit FaveStickerQuery(Promise<Unit> &&promise) : promise_(std::move(promise)) {
|
|
|
|
}
|
|
|
|
|
2019-02-02 12:14:20 +01:00
|
|
|
void send(FileId file_id, tl_object_ptr<telegram_api::inputDocument> &&input_document, bool unsave) {
|
|
|
|
CHECK(input_document != nullptr);
|
|
|
|
CHECK(file_id.is_valid());
|
|
|
|
file_id_ = file_id;
|
|
|
|
file_reference_ = input_document->file_reference_.as_slice().str();
|
|
|
|
unsave_ = unsave;
|
|
|
|
|
2020-03-15 22:17:11 +01:00
|
|
|
send_query(G()->net_query_creator().create(telegram_api::messages_faveSticker(std::move(input_document), unsave)));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_result(BufferSlice packet) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto result_ptr = fetch_result<telegram_api::messages_faveSticker>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
2021-11-08 12:19:57 +01:00
|
|
|
return on_error(result_ptr.move_as_error());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool result = result_ptr.move_as_ok();
|
|
|
|
LOG(INFO) << "Receive result for fave sticker: " << result;
|
|
|
|
if (!result) {
|
2021-11-08 13:20:38 +01:00
|
|
|
td_->stickers_manager_->reload_favorite_stickers(true);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
promise_.set_value(Unit());
|
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_error(Status status) final {
|
2021-11-08 13:20:38 +01:00
|
|
|
if (!td_->auth_manager_->is_bot() && FileReferenceManager::is_file_reference_error(status)) {
|
2019-02-08 15:54:23 +01:00
|
|
|
VLOG(file_references) << "Receive " << status << " for " << file_id_;
|
2021-11-08 13:20:38 +01:00
|
|
|
td_->file_manager_->delete_file_reference(file_id_, file_reference_);
|
|
|
|
td_->file_reference_manager_->repair_file_reference(
|
2019-02-02 12:14:20 +01:00
|
|
|
file_id_, PromiseCreator::lambda([sticker_id = file_id_, unsave = unsave_,
|
|
|
|
promise = std::move(promise_)](Result<Unit> result) mutable {
|
|
|
|
if (result.is_error()) {
|
|
|
|
return promise.set_error(Status::Error(400, "Failed to find the sticker"));
|
|
|
|
}
|
|
|
|
|
|
|
|
send_closure(G()->stickers_manager(), &StickersManager::send_fave_sticker_query, sticker_id, unsave,
|
|
|
|
std::move(promise));
|
|
|
|
}));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-03-30 23:35:58 +02:00
|
|
|
if (!G()->is_expected_error(status)) {
|
2019-02-14 16:36:16 +01:00
|
|
|
LOG(ERROR) << "Receive error for fave sticker: " << status;
|
|
|
|
}
|
2021-11-08 13:20:38 +01:00
|
|
|
td_->stickers_manager_->reload_favorite_stickers(true);
|
2018-12-31 20:04:05 +01:00
|
|
|
promise_.set_error(std::move(status));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class ReorderStickerSetsQuery final : public Td::ResultHandler {
|
2022-07-14 15:17:19 +02:00
|
|
|
StickerType sticker_type_;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
public:
|
2022-07-14 15:17:19 +02:00
|
|
|
void send(StickerType sticker_type, const vector<StickerSetId> &sticker_set_ids) {
|
|
|
|
sticker_type_ = sticker_type;
|
2018-12-31 20:04:05 +01:00
|
|
|
int32 flags = 0;
|
2022-07-14 15:17:19 +02:00
|
|
|
if (sticker_type == StickerType::Mask) {
|
2018-12-31 20:04:05 +01:00
|
|
|
flags |= telegram_api::messages_reorderStickerSets::MASKS_MASK;
|
|
|
|
}
|
2022-07-19 13:51:29 +02:00
|
|
|
if (sticker_type == StickerType::CustomEmoji) {
|
2022-07-14 15:42:27 +02:00
|
|
|
flags |= telegram_api::messages_reorderStickerSets::EMOJIS_MASK;
|
|
|
|
}
|
2020-03-15 22:17:11 +01:00
|
|
|
send_query(G()->net_query_creator().create(telegram_api::messages_reorderStickerSets(
|
2022-07-01 13:33:59 +02:00
|
|
|
flags, false /*ignored*/, false /*ignored*/, StickersManager::convert_sticker_set_ids(sticker_set_ids))));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_result(BufferSlice packet) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto result_ptr = fetch_result<telegram_api::messages_reorderStickerSets>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
2021-11-08 12:19:57 +01:00
|
|
|
return on_error(result_ptr.move_as_error());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool result = result_ptr.move_as_ok();
|
|
|
|
if (!result) {
|
2021-11-08 12:19:57 +01:00
|
|
|
return on_error(Status::Error(400, "Result is false"));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_error(Status status) final {
|
2020-03-30 23:35:58 +02:00
|
|
|
if (!G()->is_expected_error(status)) {
|
2019-02-14 16:36:16 +01:00
|
|
|
LOG(ERROR) << "Receive error for ReorderStickerSetsQuery: " << status;
|
|
|
|
}
|
2022-07-14 15:17:19 +02:00
|
|
|
td_->stickers_manager_->reload_installed_sticker_sets(sticker_type_, true);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class GetStickerSetQuery final : public Td::ResultHandler {
|
2018-12-31 20:04:05 +01:00
|
|
|
Promise<Unit> promise_;
|
2019-09-18 05:55:43 +02:00
|
|
|
StickerSetId sticker_set_id_;
|
2019-04-19 01:36:48 +02:00
|
|
|
string sticker_set_name_;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
public:
|
|
|
|
explicit GetStickerSetQuery(Promise<Unit> &&promise) : promise_(std::move(promise)) {
|
|
|
|
}
|
|
|
|
|
2021-11-19 11:56:12 +01:00
|
|
|
void send(StickerSetId sticker_set_id, tl_object_ptr<telegram_api::InputStickerSet> &&input_sticker_set, int32 hash) {
|
2018-12-31 20:04:05 +01:00
|
|
|
sticker_set_id_ = sticker_set_id;
|
2019-04-19 01:36:48 +02:00
|
|
|
if (input_sticker_set->get_id() == telegram_api::inputStickerSetShortName::ID) {
|
|
|
|
sticker_set_name_ =
|
|
|
|
static_cast<const telegram_api::inputStickerSetShortName *>(input_sticker_set.get())->short_name_;
|
|
|
|
}
|
2021-11-19 11:56:12 +01:00
|
|
|
send_query(
|
|
|
|
G()->net_query_creator().create(telegram_api::messages_getStickerSet(std::move(input_sticker_set), hash)));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_result(BufferSlice packet) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto result_ptr = fetch_result<telegram_api::messages_getStickerSet>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
2021-11-08 12:19:57 +01:00
|
|
|
return on_error(result_ptr.move_as_error());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-11-09 17:07:55 +01:00
|
|
|
auto set_ptr = result_ptr.move_as_ok();
|
2021-11-19 11:56:12 +01:00
|
|
|
if (set_ptr->get_id() == telegram_api::messages_stickerSet::ID) {
|
|
|
|
auto set = static_cast<telegram_api::messages_stickerSet *>(set_ptr.get());
|
|
|
|
constexpr int64 GREAT_MINDS_COLOR_SET_ID = 151353307481243663;
|
|
|
|
if (set->set_->id_ == GREAT_MINDS_COLOR_SET_ID) {
|
|
|
|
string great_minds_name = "TelegramGreatMinds";
|
|
|
|
if (sticker_set_id_.get() == StickersManager::GREAT_MINDS_SET_ID ||
|
|
|
|
trim(to_lower(sticker_set_name_)) == to_lower(great_minds_name)) {
|
|
|
|
set->set_->id_ = StickersManager::GREAT_MINDS_SET_ID;
|
|
|
|
set->set_->short_name_ = std::move(great_minds_name);
|
|
|
|
}
|
2019-04-19 01:36:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-19 11:56:12 +01:00
|
|
|
td_->stickers_manager_->on_get_messages_sticker_set(sticker_set_id_, std::move(set_ptr), true,
|
|
|
|
"GetStickerSetQuery");
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
promise_.set_value(Unit());
|
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_error(Status status) final {
|
2020-09-24 15:08:04 +02:00
|
|
|
LOG(INFO) << "Receive error for GetStickerSetQuery: " << status;
|
2021-11-08 13:20:38 +01:00
|
|
|
td_->stickers_manager_->on_load_sticker_set_fail(sticker_set_id_, status);
|
2018-12-31 20:04:05 +01:00
|
|
|
promise_.set_error(std::move(status));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class ReloadSpecialStickerSetQuery final : public Td::ResultHandler {
|
2021-11-19 12:07:05 +01:00
|
|
|
StickerSetId sticker_set_id_;
|
2020-04-19 15:30:04 +02:00
|
|
|
SpecialStickerSetType type_;
|
2020-03-14 01:47:19 +01:00
|
|
|
|
2019-11-21 20:18:29 +01:00
|
|
|
public:
|
2021-11-19 12:07:05 +01:00
|
|
|
void send(StickerSetId sticker_set_id, SpecialStickerSetType type, int32 hash) {
|
|
|
|
sticker_set_id_ = sticker_set_id;
|
2020-04-19 15:30:04 +02:00
|
|
|
type_ = std::move(type);
|
2021-11-19 12:07:05 +01:00
|
|
|
send_query(
|
|
|
|
G()->net_query_creator().create(telegram_api::messages_getStickerSet(type_.get_input_sticker_set(), hash)));
|
2019-11-21 20:18:29 +01:00
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_result(BufferSlice packet) final {
|
2019-11-21 20:18:29 +01:00
|
|
|
auto result_ptr = fetch_result<telegram_api::messages_getStickerSet>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
2021-11-08 12:19:57 +01:00
|
|
|
return on_error(result_ptr.move_as_error());
|
2019-11-21 20:18:29 +01:00
|
|
|
}
|
|
|
|
|
2021-11-19 12:07:05 +01:00
|
|
|
auto set_ptr = result_ptr.move_as_ok();
|
|
|
|
if (set_ptr->get_id() == telegram_api::messages_stickerSet::ID) {
|
2022-06-21 16:34:24 +02:00
|
|
|
// sticker_set_id_ must be replaced always, because it could have been changed
|
|
|
|
// we must not pass sticker_set_id_ in order to allow its change
|
2021-11-19 12:07:05 +01:00
|
|
|
sticker_set_id_ = td_->stickers_manager_->on_get_messages_sticker_set(StickerSetId(), std::move(set_ptr), true,
|
|
|
|
"ReloadSpecialStickerSetQuery");
|
2022-06-21 16:34:24 +02:00
|
|
|
} else {
|
|
|
|
CHECK(set_ptr->get_id() == telegram_api::messages_stickerSetNotModified::ID);
|
|
|
|
// we received telegram_api::messages_stickerSetNotModified, and must pass sticker_set_id_ to handle it
|
|
|
|
// sticker_set_id_ can't be changed by this call
|
2021-11-19 12:07:05 +01:00
|
|
|
td_->stickers_manager_->on_get_messages_sticker_set(sticker_set_id_, std::move(set_ptr), false,
|
|
|
|
"ReloadSpecialStickerSetQuery");
|
|
|
|
}
|
|
|
|
if (sticker_set_id_.is_valid()) {
|
|
|
|
td_->stickers_manager_->on_get_special_sticker_set(type_, sticker_set_id_);
|
2020-04-19 23:41:26 +02:00
|
|
|
} else {
|
2021-11-08 12:19:57 +01:00
|
|
|
on_error(Status::Error(500, "Failed to add special sticker set"));
|
2019-11-21 20:18:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_error(Status status) final {
|
2020-03-14 01:47:19 +01:00
|
|
|
LOG(WARNING) << "Receive error for ReloadSpecialStickerSetQuery: " << status;
|
2021-11-08 13:20:38 +01:00
|
|
|
td_->stickers_manager_->on_load_special_sticker_set(type_, std::move(status));
|
2019-11-21 20:18:29 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class SearchStickerSetsQuery final : public Td::ResultHandler {
|
2018-03-05 23:44:11 +01:00
|
|
|
string query_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
void send(string query) {
|
|
|
|
query_ = std::move(query);
|
2020-03-15 22:17:11 +01:00
|
|
|
send_query(
|
|
|
|
G()->net_query_creator().create(telegram_api::messages_searchStickerSets(0, false /*ignored*/, query_, 0)));
|
2018-03-05 23:44:11 +01:00
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_result(BufferSlice packet) final {
|
2018-03-05 23:44:11 +01:00
|
|
|
auto result_ptr = fetch_result<telegram_api::messages_searchStickerSets>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
2021-11-08 12:19:57 +01:00
|
|
|
return on_error(result_ptr.move_as_error());
|
2018-03-05 23:44:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
auto ptr = result_ptr.move_as_ok();
|
|
|
|
LOG(INFO) << "Receive result for search sticker sets: " << to_string(ptr);
|
2021-11-08 13:20:38 +01:00
|
|
|
td_->stickers_manager_->on_find_sticker_sets_success(query_, std::move(ptr));
|
2018-03-05 23:44:11 +01:00
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_error(Status status) final {
|
2020-03-30 23:35:58 +02:00
|
|
|
if (!G()->is_expected_error(status)) {
|
2019-02-14 16:36:16 +01:00
|
|
|
LOG(ERROR) << "Receive error for search sticker sets: " << status;
|
|
|
|
}
|
2021-11-08 13:20:38 +01:00
|
|
|
td_->stickers_manager_->on_find_sticker_sets_fail(query_, std::move(status));
|
2018-03-05 23:44:11 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class InstallStickerSetQuery final : public Td::ResultHandler {
|
2018-12-31 20:04:05 +01:00
|
|
|
Promise<Unit> promise_;
|
2019-09-18 05:55:43 +02:00
|
|
|
StickerSetId set_id_;
|
2018-12-31 20:04:05 +01:00
|
|
|
bool is_archived_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit InstallStickerSetQuery(Promise<Unit> &&promise) : promise_(std::move(promise)) {
|
|
|
|
}
|
|
|
|
|
2019-09-18 05:55:43 +02:00
|
|
|
void send(StickerSetId set_id, tl_object_ptr<telegram_api::InputStickerSet> &&input_set, bool is_archived) {
|
2018-12-31 20:04:05 +01:00
|
|
|
set_id_ = set_id;
|
|
|
|
is_archived_ = is_archived;
|
2020-03-15 22:17:11 +01:00
|
|
|
send_query(
|
|
|
|
G()->net_query_creator().create(telegram_api::messages_installStickerSet(std::move(input_set), is_archived)));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_result(BufferSlice packet) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto result_ptr = fetch_result<telegram_api::messages_installStickerSet>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
2021-11-08 12:19:57 +01:00
|
|
|
return on_error(result_ptr.move_as_error());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-11-08 13:20:38 +01:00
|
|
|
td_->stickers_manager_->on_install_sticker_set(set_id_, is_archived_, result_ptr.move_as_ok());
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
promise_.set_value(Unit());
|
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_error(Status status) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK(status.is_error());
|
|
|
|
promise_.set_error(std::move(status));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class UninstallStickerSetQuery final : public Td::ResultHandler {
|
2018-12-31 20:04:05 +01:00
|
|
|
Promise<Unit> promise_;
|
2019-09-18 05:55:43 +02:00
|
|
|
StickerSetId set_id_;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
public:
|
|
|
|
explicit UninstallStickerSetQuery(Promise<Unit> &&promise) : promise_(std::move(promise)) {
|
|
|
|
}
|
|
|
|
|
2019-09-18 05:55:43 +02:00
|
|
|
void send(StickerSetId set_id, tl_object_ptr<telegram_api::InputStickerSet> &&input_set) {
|
2018-12-31 20:04:05 +01:00
|
|
|
set_id_ = set_id;
|
2020-03-15 22:17:11 +01:00
|
|
|
send_query(G()->net_query_creator().create(telegram_api::messages_uninstallStickerSet(std::move(input_set))));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_result(BufferSlice packet) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto result_ptr = fetch_result<telegram_api::messages_uninstallStickerSet>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
2021-11-08 12:19:57 +01:00
|
|
|
return on_error(result_ptr.move_as_error());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool result = result_ptr.move_as_ok();
|
|
|
|
if (!result) {
|
|
|
|
LOG(WARNING) << "Receive false in result to uninstallStickerSet";
|
|
|
|
} else {
|
2021-11-08 13:20:38 +01:00
|
|
|
td_->stickers_manager_->on_uninstall_sticker_set(set_id_);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
promise_.set_value(Unit());
|
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_error(Status status) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK(status.is_error());
|
|
|
|
promise_.set_error(std::move(status));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class ReadFeaturedStickerSetsQuery final : public Td::ResultHandler {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
2021-10-19 17:11:16 +02:00
|
|
|
void send(const vector<StickerSetId> &sticker_set_ids) {
|
2020-03-15 22:17:11 +01:00
|
|
|
send_query(G()->net_query_creator().create(
|
|
|
|
telegram_api::messages_readFeaturedStickers(StickersManager::convert_sticker_set_ids(sticker_set_ids))));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_result(BufferSlice packet) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto result_ptr = fetch_result<telegram_api::messages_readFeaturedStickers>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
2021-11-08 12:19:57 +01:00
|
|
|
return on_error(result_ptr.move_as_error());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool result = result_ptr.move_as_ok();
|
|
|
|
(void)result;
|
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_error(Status status) final {
|
2020-03-30 23:35:58 +02:00
|
|
|
if (!G()->is_expected_error(status)) {
|
2019-02-14 16:36:16 +01:00
|
|
|
LOG(ERROR) << "Receive error for ReadFeaturedStickerSetsQuery: " << status;
|
|
|
|
}
|
2022-09-18 20:57:13 +02:00
|
|
|
td_->stickers_manager_->reload_featured_sticker_sets(StickerType::Regular, true);
|
|
|
|
td_->stickers_manager_->reload_featured_sticker_sets(StickerType::CustomEmoji, true);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class UploadStickerFileQuery final : public Td::ResultHandler {
|
2018-12-31 20:04:05 +01:00
|
|
|
Promise<Unit> promise_;
|
|
|
|
FileId file_id_;
|
2019-01-25 02:38:11 +01:00
|
|
|
bool was_uploaded_ = false;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
public:
|
|
|
|
explicit UploadStickerFileQuery(Promise<Unit> &&promise) : promise_(std::move(promise)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void send(tl_object_ptr<telegram_api::InputPeer> &&input_peer, FileId file_id,
|
|
|
|
tl_object_ptr<telegram_api::InputMedia> &&input_media) {
|
|
|
|
CHECK(input_peer != nullptr);
|
|
|
|
CHECK(input_media != nullptr);
|
|
|
|
file_id_ = file_id;
|
2019-01-25 02:38:11 +01:00
|
|
|
was_uploaded_ = FileManager::extract_was_uploaded(input_media);
|
2018-12-31 20:04:05 +01:00
|
|
|
send_query(G()->net_query_creator().create(
|
2020-03-15 22:17:11 +01:00
|
|
|
telegram_api::messages_uploadMedia(std::move(input_peer), std::move(input_media))));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_result(BufferSlice packet) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto result_ptr = fetch_result<telegram_api::messages_uploadMedia>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
2021-11-08 12:19:57 +01:00
|
|
|
return on_error(result_ptr.move_as_error());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-11-08 13:20:38 +01:00
|
|
|
td_->stickers_manager_->on_uploaded_sticker_file(file_id_, result_ptr.move_as_ok(), std::move(promise_));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_error(Status status) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK(status.is_error());
|
2019-01-25 02:38:11 +01:00
|
|
|
if (was_uploaded_) {
|
|
|
|
CHECK(file_id_.is_valid());
|
|
|
|
if (begins_with(status.message(), "FILE_PART_") && ends_with(status.message(), "_MISSING")) {
|
2021-11-08 13:20:38 +01:00
|
|
|
// TODO td_->stickers_manager_->on_upload_sticker_file_part_missing(file_id_, to_integer<int32>(status.message().substr(10)));
|
2019-01-25 02:52:38 +01:00
|
|
|
// return;
|
2019-01-25 02:38:11 +01:00
|
|
|
} else {
|
|
|
|
if (status.code() != 429 && status.code() < 500 && !G()->close_flag()) {
|
2021-11-08 13:20:38 +01:00
|
|
|
td_->file_manager_->delete_partial_remote_location(file_id_);
|
2019-01-25 02:38:11 +01:00
|
|
|
}
|
|
|
|
}
|
2019-01-28 16:21:48 +01:00
|
|
|
} else if (FileReferenceManager::is_file_reference_error(status)) {
|
|
|
|
LOG(ERROR) << "Receive file reference error for UploadStickerFileQuery";
|
2019-01-25 02:38:11 +01:00
|
|
|
}
|
2021-11-08 13:20:38 +01:00
|
|
|
td_->file_manager_->cancel_upload(file_id_);
|
2018-12-31 20:04:05 +01:00
|
|
|
promise_.set_error(std::move(status));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class SuggestStickerSetShortNameQuery final : public Td::ResultHandler {
|
2021-06-17 19:38:03 +02:00
|
|
|
Promise<string> promise_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit SuggestStickerSetShortNameQuery(Promise<string> &&promise) : promise_(std::move(promise)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void send(const string &title) {
|
|
|
|
send_query(G()->net_query_creator().create(telegram_api::stickers_suggestShortName(title)));
|
|
|
|
}
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_result(BufferSlice packet) final {
|
2021-06-17 19:38:03 +02:00
|
|
|
auto result_ptr = fetch_result<telegram_api::stickers_suggestShortName>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
2021-11-08 12:19:57 +01:00
|
|
|
return on_error(result_ptr.move_as_error());
|
2021-06-17 19:38:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
auto ptr = result_ptr.move_as_ok();
|
|
|
|
promise_.set_value(std::move(ptr->short_name_));
|
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_error(Status status) final {
|
2021-06-17 19:38:03 +02:00
|
|
|
if (status.message() == "TITLE_INVALID") {
|
|
|
|
return promise_.set_value(string());
|
|
|
|
}
|
|
|
|
promise_.set_error(std::move(status));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class CheckStickerSetShortNameQuery final : public Td::ResultHandler {
|
2021-06-17 20:29:07 +02:00
|
|
|
Promise<bool> promise_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit CheckStickerSetShortNameQuery(Promise<bool> &&promise) : promise_(std::move(promise)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void send(const string &short_name) {
|
|
|
|
send_query(G()->net_query_creator().create(telegram_api::stickers_checkShortName(short_name)));
|
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_result(BufferSlice packet) final {
|
2021-06-17 20:29:07 +02:00
|
|
|
auto result_ptr = fetch_result<telegram_api::stickers_checkShortName>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
2021-11-08 12:19:57 +01:00
|
|
|
return on_error(result_ptr.move_as_error());
|
2021-06-17 20:29:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
promise_.set_value(result_ptr.move_as_ok());
|
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_error(Status status) final {
|
2021-06-17 20:29:07 +02:00
|
|
|
promise_.set_error(std::move(status));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class CreateNewStickerSetQuery final : public Td::ResultHandler {
|
2022-08-08 16:24:01 +02:00
|
|
|
Promise<td_api::object_ptr<td_api::stickerSet>> promise_;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
public:
|
2022-08-08 16:24:01 +02:00
|
|
|
explicit CreateNewStickerSetQuery(Promise<td_api::object_ptr<td_api::stickerSet>> &&promise)
|
|
|
|
: promise_(std::move(promise)) {
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void send(tl_object_ptr<telegram_api::InputUser> &&input_user, const string &title, const string &short_name,
|
2022-07-14 15:17:19 +02:00
|
|
|
StickerType sticker_type, StickerFormat sticker_format,
|
2022-01-18 17:20:43 +01:00
|
|
|
vector<tl_object_ptr<telegram_api::inputStickerSetItem>> &&input_stickers, const string &software) {
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK(input_user != nullptr);
|
|
|
|
|
|
|
|
int32 flags = 0;
|
2022-07-14 15:17:19 +02:00
|
|
|
if (sticker_type == StickerType::Mask) {
|
2018-12-31 20:04:05 +01:00
|
|
|
flags |= telegram_api::stickers_createStickerSet::MASKS_MASK;
|
|
|
|
}
|
2022-07-19 13:51:29 +02:00
|
|
|
if (sticker_type == StickerType::CustomEmoji) {
|
2022-07-14 15:42:27 +02:00
|
|
|
// flags |= telegram_api::stickers_createStickerSet::EMOJIS_MASK;
|
2022-07-19 13:51:29 +02:00
|
|
|
return on_error(Status::Error(400, "Can't create custom emoji sets"));
|
2022-07-14 15:42:27 +02:00
|
|
|
}
|
2022-01-18 17:20:43 +01:00
|
|
|
if (sticker_format == StickerFormat::Tgs) {
|
2020-03-23 16:03:13 +01:00
|
|
|
flags |= telegram_api::stickers_createStickerSet::ANIMATED_MASK;
|
|
|
|
}
|
2022-01-25 12:58:04 +01:00
|
|
|
if (sticker_format == StickerFormat::Webm) {
|
2022-01-28 23:53:50 +01:00
|
|
|
flags |= telegram_api::stickers_createStickerSet::VIDEOS_MASK;
|
2022-01-25 12:58:04 +01:00
|
|
|
}
|
2021-06-18 17:45:57 +02:00
|
|
|
if (!software.empty()) {
|
|
|
|
flags |= telegram_api::stickers_createStickerSet::SOFTWARE_MASK;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2022-02-05 22:16:16 +01:00
|
|
|
send_query(G()->net_query_creator().create(
|
|
|
|
telegram_api::stickers_createStickerSet(flags, false /*ignored*/, false /*ignored*/, false /*ignored*/,
|
|
|
|
std::move(input_user), title, short_name, nullptr,
|
|
|
|
std::move(input_stickers), software),
|
|
|
|
{{short_name}}));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_result(BufferSlice packet) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto result_ptr = fetch_result<telegram_api::stickers_createStickerSet>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
2021-11-08 12:19:57 +01:00
|
|
|
return on_error(result_ptr.move_as_error());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2022-08-08 16:24:01 +02:00
|
|
|
auto sticker_set_id = td_->stickers_manager_->on_get_messages_sticker_set(StickerSetId(), result_ptr.move_as_ok(),
|
|
|
|
true, "CreateNewStickerSetQuery");
|
|
|
|
if (!sticker_set_id.is_valid()) {
|
|
|
|
return on_error(Status::Error(500, "Created sticker set not found"));
|
|
|
|
}
|
|
|
|
promise_.set_value(td_->stickers_manager_->get_sticker_set_object(sticker_set_id));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_error(Status status) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK(status.is_error());
|
|
|
|
promise_.set_error(std::move(status));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class AddStickerToSetQuery final : public Td::ResultHandler {
|
2022-08-08 16:01:04 +02:00
|
|
|
Promise<td_api::object_ptr<td_api::stickerSet>> promise_;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
public:
|
2022-08-08 16:01:04 +02:00
|
|
|
explicit AddStickerToSetQuery(Promise<td_api::object_ptr<td_api::stickerSet>> &&promise)
|
|
|
|
: promise_(std::move(promise)) {
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void send(const string &short_name, tl_object_ptr<telegram_api::inputStickerSetItem> &&input_sticker) {
|
2022-02-05 22:16:16 +01:00
|
|
|
send_query(G()->net_query_creator().create(
|
|
|
|
telegram_api::stickers_addStickerToSet(make_tl_object<telegram_api::inputStickerSetShortName>(short_name),
|
|
|
|
std::move(input_sticker)),
|
|
|
|
{{short_name}}));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_result(BufferSlice packet) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto result_ptr = fetch_result<telegram_api::stickers_addStickerToSet>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
2021-11-08 12:19:57 +01:00
|
|
|
return on_error(result_ptr.move_as_error());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2022-08-08 16:01:04 +02:00
|
|
|
auto sticker_set_id = td_->stickers_manager_->on_get_messages_sticker_set(StickerSetId(), result_ptr.move_as_ok(),
|
|
|
|
true, "AddStickerToSetQuery");
|
2022-08-08 16:24:01 +02:00
|
|
|
if (!sticker_set_id.is_valid()) {
|
|
|
|
return on_error(Status::Error(500, "Sticker set not found"));
|
|
|
|
}
|
2022-08-08 16:01:04 +02:00
|
|
|
promise_.set_value(td_->stickers_manager_->get_sticker_set_object(sticker_set_id));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_error(Status status) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK(status.is_error());
|
|
|
|
promise_.set_error(std::move(status));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class SetStickerSetThumbnailQuery final : public Td::ResultHandler {
|
2022-08-08 16:31:27 +02:00
|
|
|
Promise<td_api::object_ptr<td_api::stickerSet>> promise_;
|
2020-03-20 14:36:07 +01:00
|
|
|
|
|
|
|
public:
|
2022-08-08 16:31:27 +02:00
|
|
|
explicit SetStickerSetThumbnailQuery(Promise<td_api::object_ptr<td_api::stickerSet>> &&promise)
|
|
|
|
: promise_(std::move(promise)) {
|
2020-03-20 14:36:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void send(const string &short_name, tl_object_ptr<telegram_api::InputDocument> &&input_document) {
|
2022-02-05 22:16:16 +01:00
|
|
|
send_query(G()->net_query_creator().create(
|
|
|
|
telegram_api::stickers_setStickerSetThumb(make_tl_object<telegram_api::inputStickerSetShortName>(short_name),
|
|
|
|
std::move(input_document)),
|
|
|
|
{{short_name}}));
|
2020-03-20 14:36:07 +01:00
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_result(BufferSlice packet) final {
|
2020-03-20 14:36:07 +01:00
|
|
|
auto result_ptr = fetch_result<telegram_api::stickers_setStickerSetThumb>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
2021-11-08 12:19:57 +01:00
|
|
|
return on_error(result_ptr.move_as_error());
|
2020-03-20 14:36:07 +01:00
|
|
|
}
|
|
|
|
|
2022-08-08 16:31:27 +02:00
|
|
|
auto sticker_set_id = td_->stickers_manager_->on_get_messages_sticker_set(StickerSetId(), result_ptr.move_as_ok(),
|
|
|
|
true, "SetStickerSetThumbnailQuery");
|
|
|
|
if (!sticker_set_id.is_valid()) {
|
|
|
|
return on_error(Status::Error(500, "Sticker set not found"));
|
|
|
|
}
|
|
|
|
promise_.set_value(td_->stickers_manager_->get_sticker_set_object(sticker_set_id));
|
2020-03-20 14:36:07 +01:00
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_error(Status status) final {
|
2020-03-20 14:36:07 +01:00
|
|
|
CHECK(status.is_error());
|
|
|
|
promise_.set_error(std::move(status));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class SetStickerPositionQuery final : public Td::ResultHandler {
|
2018-12-31 20:04:05 +01:00
|
|
|
Promise<Unit> promise_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit SetStickerPositionQuery(Promise<Unit> &&promise) : promise_(std::move(promise)) {
|
|
|
|
}
|
|
|
|
|
2022-02-05 22:16:16 +01:00
|
|
|
void send(const string &short_name, tl_object_ptr<telegram_api::inputDocument> &&input_document, int32 position) {
|
2022-04-17 22:07:00 +02:00
|
|
|
vector<ChainId> chain_ids;
|
|
|
|
if (!short_name.empty()) {
|
|
|
|
chain_ids.emplace_back(short_name);
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
send_query(G()->net_query_creator().create(
|
2022-04-17 22:07:00 +02:00
|
|
|
telegram_api::stickers_changeStickerPosition(std::move(input_document), position), std::move(chain_ids)));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_result(BufferSlice packet) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto result_ptr = fetch_result<telegram_api::stickers_changeStickerPosition>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
2021-11-08 12:19:57 +01:00
|
|
|
return on_error(result_ptr.move_as_error());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-11-08 13:20:38 +01:00
|
|
|
td_->stickers_manager_->on_get_messages_sticker_set(StickerSetId(), result_ptr.move_as_ok(), true,
|
|
|
|
"SetStickerPositionQuery");
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
promise_.set_value(Unit());
|
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_error(Status status) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK(status.is_error());
|
|
|
|
promise_.set_error(std::move(status));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class DeleteStickerFromSetQuery final : public Td::ResultHandler {
|
2018-12-31 20:04:05 +01:00
|
|
|
Promise<Unit> promise_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit DeleteStickerFromSetQuery(Promise<Unit> &&promise) : promise_(std::move(promise)) {
|
|
|
|
}
|
|
|
|
|
2022-02-05 22:16:16 +01:00
|
|
|
void send(const string &short_name, tl_object_ptr<telegram_api::inputDocument> &&input_document) {
|
2022-04-17 22:07:00 +02:00
|
|
|
vector<ChainId> chain_ids;
|
|
|
|
if (!short_name.empty()) {
|
|
|
|
chain_ids.emplace_back(short_name);
|
|
|
|
}
|
2022-02-05 22:16:16 +01:00
|
|
|
send_query(G()->net_query_creator().create(telegram_api::stickers_removeStickerFromSet(std::move(input_document)),
|
2022-04-17 22:07:00 +02:00
|
|
|
std::move(chain_ids)));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_result(BufferSlice packet) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto result_ptr = fetch_result<telegram_api::stickers_removeStickerFromSet>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
2021-11-08 12:19:57 +01:00
|
|
|
return on_error(result_ptr.move_as_error());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-11-08 13:20:38 +01:00
|
|
|
td_->stickers_manager_->on_get_messages_sticker_set(StickerSetId(), result_ptr.move_as_ok(), true,
|
|
|
|
"DeleteStickerFromSetQuery");
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
promise_.set_value(Unit());
|
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_error(Status status) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK(status.is_error());
|
|
|
|
promise_.set_error(std::move(status));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-07-19 15:00:04 +02:00
|
|
|
class GetCustomEmojiDocumentsQuery final : public Td::ResultHandler {
|
|
|
|
Promise<vector<telegram_api::object_ptr<telegram_api::Document>>> promise_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit GetCustomEmojiDocumentsQuery(Promise<vector<telegram_api::object_ptr<telegram_api::Document>>> &&promise)
|
|
|
|
: promise_(std::move(promise)) {
|
|
|
|
}
|
|
|
|
|
2022-10-03 00:26:32 +02:00
|
|
|
void send(vector<CustomEmojiId> &&custom_emoji_ids) {
|
|
|
|
auto document_ids =
|
|
|
|
transform(custom_emoji_ids, [](CustomEmojiId custom_emoji_id) { return custom_emoji_id.get(); });
|
2022-07-19 15:00:04 +02:00
|
|
|
send_query(
|
|
|
|
G()->net_query_creator().create(telegram_api::messages_getCustomEmojiDocuments(std::move(document_ids))));
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_result(BufferSlice packet) final {
|
|
|
|
auto result_ptr = fetch_result<telegram_api::messages_getCustomEmojiDocuments>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
|
|
|
return on_error(result_ptr.move_as_error());
|
|
|
|
}
|
|
|
|
|
|
|
|
promise_.set_value(result_ptr.move_as_ok());
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_error(Status status) final {
|
|
|
|
promise_.set_error(std::move(status));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-09-17 15:16:34 +02:00
|
|
|
class SendAnimatedEmojiClicksQuery final : public Td::ResultHandler {
|
|
|
|
DialogId dialog_id_;
|
2021-09-19 00:52:56 +02:00
|
|
|
string emoji_;
|
2021-09-17 15:16:34 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
void send(DialogId dialog_id, tl_object_ptr<telegram_api::InputPeer> &&input_peer,
|
2021-09-19 00:52:56 +02:00
|
|
|
tl_object_ptr<telegram_api::sendMessageEmojiInteraction> &&action) {
|
2021-09-17 15:16:34 +02:00
|
|
|
dialog_id_ = dialog_id;
|
|
|
|
CHECK(input_peer != nullptr);
|
2021-09-19 00:52:56 +02:00
|
|
|
CHECK(action != nullptr);
|
|
|
|
emoji_ = action->emoticon_;
|
2021-09-17 15:16:34 +02:00
|
|
|
|
|
|
|
int32 flags = 0;
|
|
|
|
send_query(G()->net_query_creator().create(
|
|
|
|
telegram_api::messages_setTyping(flags, std::move(input_peer), 0, std::move(action))));
|
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_result(BufferSlice packet) final {
|
2021-09-17 15:16:34 +02:00
|
|
|
auto result_ptr = fetch_result<telegram_api::messages_setTyping>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
2021-11-08 12:19:57 +01:00
|
|
|
return on_error(result_ptr.move_as_error());
|
2021-09-17 15:16:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ignore result
|
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_error(Status status) final {
|
2021-11-08 13:20:38 +01:00
|
|
|
if (!td_->messages_manager_->on_get_dialog_error(dialog_id_, status, "SendAnimatedEmojiClicksQuery")) {
|
2021-09-17 15:16:34 +02:00
|
|
|
LOG(INFO) << "Receive error for send animated emoji clicks: " << status;
|
|
|
|
}
|
2021-09-19 00:52:56 +02:00
|
|
|
|
2021-11-08 13:20:38 +01:00
|
|
|
td_->stickers_manager_->on_send_animated_emoji_clicks(dialog_id_, emoji_);
|
2021-09-17 15:16:34 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-08-07 23:55:22 +02:00
|
|
|
template <class StorerT>
|
|
|
|
void StickersManager::FoundStickers::store(StorerT &storer) const {
|
|
|
|
StickersManager *stickers_manager = storer.context()->td().get_actor_unsafe()->stickers_manager_.get();
|
|
|
|
td::store(narrow_cast<int32>(sticker_ids_.size()), storer);
|
|
|
|
for (auto sticker_id : sticker_ids_) {
|
|
|
|
stickers_manager->store_sticker(sticker_id, false, storer, "FoundStickers");
|
|
|
|
}
|
|
|
|
td::store(cache_time_, storer);
|
|
|
|
store_time(next_reload_time_, storer);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ParserT>
|
|
|
|
void StickersManager::FoundStickers::parse(ParserT &parser) {
|
|
|
|
StickersManager *stickers_manager = parser.context()->td().get_actor_unsafe()->stickers_manager_.get();
|
|
|
|
int32 size = parser.fetch_int();
|
|
|
|
sticker_ids_.resize(size);
|
|
|
|
for (auto &sticker_id : sticker_ids_) {
|
|
|
|
sticker_id = stickers_manager->parse_sticker(false, parser);
|
|
|
|
}
|
|
|
|
td::parse(cache_time_, parser);
|
|
|
|
parse_time(next_reload_time_, parser);
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
class StickersManager::StickerListLogEvent {
|
|
|
|
public:
|
|
|
|
vector<FileId> sticker_ids;
|
|
|
|
|
|
|
|
StickerListLogEvent() = default;
|
|
|
|
|
|
|
|
explicit StickerListLogEvent(vector<FileId> sticker_ids) : sticker_ids(std::move(sticker_ids)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class StorerT>
|
|
|
|
void store(StorerT &storer) const {
|
|
|
|
StickersManager *stickers_manager = storer.context()->td().get_actor_unsafe()->stickers_manager_.get();
|
|
|
|
td::store(narrow_cast<int32>(sticker_ids.size()), storer);
|
|
|
|
for (auto sticker_id : sticker_ids) {
|
2021-09-13 15:03:21 +02:00
|
|
|
stickers_manager->store_sticker(sticker_id, false, storer, "StickerListLogEvent");
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ParserT>
|
|
|
|
void parse(ParserT &parser) {
|
|
|
|
StickersManager *stickers_manager = parser.context()->td().get_actor_unsafe()->stickers_manager_.get();
|
|
|
|
int32 size = parser.fetch_int();
|
|
|
|
sticker_ids.resize(size);
|
|
|
|
for (auto &sticker_id : sticker_ids) {
|
|
|
|
sticker_id = stickers_manager->parse_sticker(false, parser);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class StickersManager::StickerSetListLogEvent {
|
|
|
|
public:
|
2022-06-17 18:16:17 +02:00
|
|
|
vector<StickerSetId> sticker_set_ids_;
|
|
|
|
bool is_premium_ = false;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
StickerSetListLogEvent() = default;
|
|
|
|
|
2022-06-17 18:16:17 +02:00
|
|
|
StickerSetListLogEvent(vector<StickerSetId> sticker_set_ids, bool is_premium)
|
|
|
|
: sticker_set_ids_(std::move(sticker_set_ids)), is_premium_(is_premium) {
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class StorerT>
|
|
|
|
void store(StorerT &storer) const {
|
2022-06-17 18:16:17 +02:00
|
|
|
BEGIN_STORE_FLAGS();
|
|
|
|
STORE_FLAG(is_premium_);
|
|
|
|
END_STORE_FLAGS();
|
|
|
|
td::store(sticker_set_ids_, storer);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class ParserT>
|
|
|
|
void parse(ParserT &parser) {
|
2022-06-17 18:16:17 +02:00
|
|
|
if (parser.version() >= static_cast<int32>(Version::AddStickerSetListFlags)) {
|
|
|
|
BEGIN_PARSE_FLAGS();
|
|
|
|
PARSE_FLAG(is_premium_);
|
|
|
|
END_PARSE_FLAGS();
|
|
|
|
}
|
|
|
|
td::parse(sticker_set_ids_, parser);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class StickersManager::UploadStickerFileCallback final : public FileManager::UploadCallback {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
2021-07-03 22:51:36 +02:00
|
|
|
void on_upload_ok(FileId file_id, tl_object_ptr<telegram_api::InputFile> input_file) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
send_closure_later(G()->stickers_manager(), &StickersManager::on_upload_sticker_file, file_id,
|
|
|
|
std::move(input_file));
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void on_upload_encrypted_ok(FileId file_id, tl_object_ptr<telegram_api::InputEncryptedFile> input_file) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void on_upload_secure_ok(FileId file_id, tl_object_ptr<telegram_api::InputSecureFile> input_file) final {
|
2018-03-27 15:11:15 +02:00
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void on_upload_error(FileId file_id, Status error) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
send_closure_later(G()->stickers_manager(), &StickersManager::on_upload_sticker_file_error, file_id,
|
|
|
|
std::move(error));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
StickersManager::StickersManager(Td *td, ActorShared<> parent) : td_(td), parent_(std::move(parent)) {
|
|
|
|
upload_sticker_file_callback_ = std::make_shared<UploadStickerFileCallback>();
|
2018-07-03 01:10:57 +02:00
|
|
|
|
2021-10-13 23:18:44 +02:00
|
|
|
on_update_animated_emoji_zoom();
|
2022-08-17 20:40:03 +02:00
|
|
|
on_update_recent_stickers_limit();
|
|
|
|
on_update_favorite_stickers_limit();
|
2021-09-17 18:59:31 +02:00
|
|
|
|
2021-09-24 15:53:42 +02:00
|
|
|
next_click_animated_emoji_message_time_ = Time::now();
|
|
|
|
next_update_animated_emoji_clicked_time_ = Time::now();
|
2019-11-21 20:18:29 +01:00
|
|
|
}
|
2019-11-21 18:22:05 +01:00
|
|
|
|
2022-07-20 12:40:14 +02:00
|
|
|
StickersManager::~StickersManager() {
|
2022-07-26 17:48:18 +02:00
|
|
|
Scheduler::instance()->destroy_on_scheduler(
|
|
|
|
G()->get_gc_scheduler_id(), stickers_, sticker_sets_, short_name_to_sticker_set_id_, attached_sticker_sets_,
|
|
|
|
found_stickers_, found_sticker_sets_, emoji_language_codes_, emoji_language_code_versions_,
|
|
|
|
emoji_language_code_last_difference_times_, reloaded_emoji_keywords_, premium_gift_messages_, dice_messages_,
|
2022-08-05 19:49:24 +02:00
|
|
|
emoji_messages_, custom_emoji_messages_, custom_emoji_to_sticker_id_);
|
2022-07-20 12:40:14 +02:00
|
|
|
}
|
|
|
|
|
2019-11-21 20:18:29 +01:00
|
|
|
void StickersManager::start_up() {
|
2020-04-17 15:12:12 +02:00
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
|
|
|
void StickersManager::init() {
|
2022-04-26 01:06:35 +02:00
|
|
|
if (is_inited_ || !td_->auth_manager_->is_authorized() || td_->auth_manager_->is_bot() || G()->close_flag()) {
|
2020-04-17 15:12:12 +02:00
|
|
|
return;
|
|
|
|
}
|
2020-06-04 00:19:37 +02:00
|
|
|
LOG(INFO) << "Init StickersManager";
|
|
|
|
is_inited_ = true;
|
2020-04-17 15:12:12 +02:00
|
|
|
|
2020-04-19 15:30:04 +02:00
|
|
|
{
|
2021-09-15 13:44:11 +02:00
|
|
|
auto &sticker_set = add_special_sticker_set(SpecialStickerSetType::animated_emoji());
|
2020-04-19 15:30:04 +02:00
|
|
|
if (G()->is_test_dc()) {
|
2021-09-15 13:44:11 +02:00
|
|
|
init_special_sticker_set(sticker_set, 1258816259751954, 4879754868529595811, "emojies");
|
2020-04-19 15:30:04 +02:00
|
|
|
} else {
|
2021-09-15 13:44:11 +02:00
|
|
|
init_special_sticker_set(sticker_set, 1258816259751983, 5100237018658464041, "AnimatedEmojies");
|
2020-04-19 15:30:04 +02:00
|
|
|
}
|
2021-09-15 13:44:11 +02:00
|
|
|
load_special_sticker_set_info_from_binlog(sticker_set);
|
|
|
|
}
|
|
|
|
if (!G()->is_test_dc()) {
|
|
|
|
auto &sticker_set = add_special_sticker_set(SpecialStickerSetType::animated_emoji_click());
|
|
|
|
load_special_sticker_set_info_from_binlog(sticker_set);
|
2020-04-19 15:30:04 +02:00
|
|
|
}
|
2022-09-02 12:59:49 +02:00
|
|
|
{
|
|
|
|
auto &sticker_set = add_special_sticker_set(SpecialStickerSetType::premium_gifts());
|
|
|
|
load_special_sticker_set_info_from_binlog(sticker_set);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
auto &sticker_set = add_special_sticker_set(SpecialStickerSetType::generic_animations());
|
|
|
|
load_special_sticker_set_info_from_binlog(sticker_set);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
auto &sticker_set = add_special_sticker_set(SpecialStickerSetType::default_statuses());
|
|
|
|
load_special_sticker_set_info_from_binlog(sticker_set);
|
|
|
|
}
|
2022-10-27 09:06:13 +02:00
|
|
|
{
|
|
|
|
auto &sticker_set = add_special_sticker_set(SpecialStickerSetType::default_topic_icons());
|
|
|
|
load_special_sticker_set_info_from_binlog(sticker_set);
|
|
|
|
}
|
2020-04-19 15:30:04 +02:00
|
|
|
|
2022-08-18 16:00:18 +02:00
|
|
|
dice_emojis_str_ =
|
|
|
|
td_->option_manager_->get_option_string("dice_emojis", "🎲\x01🎯\x01🏀\x01⚽\x01⚽️\x01🎰\x01🎳");
|
2020-06-04 00:19:37 +02:00
|
|
|
dice_emojis_ = full_split(dice_emojis_str_, '\x01');
|
2020-04-19 15:30:04 +02:00
|
|
|
for (auto &dice_emoji : dice_emojis_) {
|
2020-04-19 23:41:26 +02:00
|
|
|
auto &animated_dice_sticker_set = add_special_sticker_set(SpecialStickerSetType::animated_dice(dice_emoji));
|
|
|
|
load_special_sticker_set_info_from_binlog(animated_dice_sticker_set);
|
2019-11-21 20:18:29 +01:00
|
|
|
}
|
2020-06-04 00:19:37 +02:00
|
|
|
send_closure(G()->td(), &Td::send_update, get_update_dice_emojis_object());
|
|
|
|
|
2022-09-12 00:35:47 +02:00
|
|
|
load_active_reactions();
|
2022-01-24 14:43:38 +01:00
|
|
|
|
2020-06-04 00:19:37 +02:00
|
|
|
on_update_dice_success_values();
|
2022-08-17 23:37:19 +02:00
|
|
|
on_update_dice_emojis();
|
2020-04-18 00:55:54 +02:00
|
|
|
|
2021-10-05 22:20:49 +02:00
|
|
|
on_update_emoji_sounds();
|
|
|
|
|
2021-10-05 16:17:29 +02:00
|
|
|
on_update_disable_animated_emojis();
|
|
|
|
if (!disable_animated_emojis_) {
|
|
|
|
load_special_sticker_set(add_special_sticker_set(SpecialStickerSetType::animated_emoji()));
|
|
|
|
}
|
2022-07-22 14:40:24 +02:00
|
|
|
load_special_sticker_set(add_special_sticker_set(SpecialStickerSetType::premium_gifts()));
|
2021-10-04 15:08:51 +02:00
|
|
|
|
2020-04-18 00:55:54 +02:00
|
|
|
if (G()->parameters().use_file_db) {
|
|
|
|
auto old_featured_sticker_set_count_str = G()->td_db()->get_binlog_pmc()->get("old_featured_sticker_set_count");
|
|
|
|
if (!old_featured_sticker_set_count_str.empty()) {
|
2022-07-17 21:39:22 +02:00
|
|
|
old_featured_sticker_set_count_[static_cast<int32>(StickerType::Regular)] =
|
|
|
|
to_integer<int32>(old_featured_sticker_set_count_str);
|
2020-04-18 00:55:54 +02:00
|
|
|
}
|
|
|
|
if (!G()->td_db()->get_binlog_pmc()->get("invalidate_old_featured_sticker_sets").empty()) {
|
2022-07-17 21:39:22 +02:00
|
|
|
invalidate_old_featured_sticker_sets(StickerType::Regular);
|
2020-04-18 00:55:54 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
G()->td_db()->get_binlog_pmc()->erase("old_featured_sticker_set_count");
|
|
|
|
G()->td_db()->get_binlog_pmc()->erase("invalidate_old_featured_sticker_sets");
|
|
|
|
}
|
2020-04-19 15:30:04 +02:00
|
|
|
|
2022-08-18 16:00:18 +02:00
|
|
|
G()->td_db()->get_binlog_pmc()->erase("animated_dice_sticker_set"); // legacy
|
|
|
|
td_->option_manager_->set_option_empty("animated_dice_sticker_set_name"); // legacy
|
|
|
|
td_->option_manager_->set_option_empty("animated_emoji_sticker_set_name"); // legacy
|
2020-03-14 01:47:19 +01:00
|
|
|
}
|
|
|
|
|
2022-09-12 14:03:35 +02:00
|
|
|
td_api::object_ptr<td_api::emojiReaction> StickersManager::get_emoji_reaction_object(const string &emoji) {
|
|
|
|
load_reactions();
|
|
|
|
for (auto &reaction : reactions_.reactions_) {
|
|
|
|
if (reaction.reaction_ == emoji) {
|
|
|
|
return td_api::make_object<td_api::emojiReaction>(
|
|
|
|
reaction.reaction_, reaction.title_, reaction.is_active_, get_sticker_object(reaction.static_icon_),
|
|
|
|
get_sticker_object(reaction.appear_animation_), get_sticker_object(reaction.select_animation_),
|
|
|
|
get_sticker_object(reaction.activate_animation_), get_sticker_object(reaction.effect_animation_),
|
|
|
|
get_sticker_object(reaction.around_animation_), get_sticker_object(reaction.center_animation_));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2022-09-13 18:19:35 +02:00
|
|
|
vector<string> StickersManager::get_recent_reactions() {
|
|
|
|
load_recent_reactions();
|
|
|
|
return recent_reactions_.reactions_;
|
|
|
|
}
|
|
|
|
|
|
|
|
vector<string> StickersManager::get_top_reactions() {
|
|
|
|
load_top_reactions();
|
|
|
|
return top_reactions_.reactions_;
|
|
|
|
}
|
|
|
|
|
2022-09-12 19:17:14 +02:00
|
|
|
void StickersManager::add_recent_reaction(const string &reaction) {
|
|
|
|
load_recent_reactions();
|
|
|
|
|
|
|
|
auto &reactions = recent_reactions_.reactions_;
|
|
|
|
if (!reactions.empty() && reactions[0] == reaction) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto it = std::find(reactions.begin(), reactions.end(), reaction);
|
|
|
|
if (it == reactions.end()) {
|
|
|
|
if (static_cast<int32>(reactions.size()) == MAX_RECENT_REACTIONS) {
|
|
|
|
reactions.back() = reaction;
|
|
|
|
} else {
|
|
|
|
reactions.push_back(reaction);
|
|
|
|
}
|
|
|
|
it = reactions.end() - 1;
|
|
|
|
}
|
|
|
|
std::rotate(reactions.begin(), it, it + 1);
|
|
|
|
|
2022-09-12 20:55:30 +02:00
|
|
|
recent_reactions_.hash_ = get_reactions_hash(reactions);
|
2022-09-12 19:17:14 +02:00
|
|
|
}
|
|
|
|
|
2022-09-12 18:37:45 +02:00
|
|
|
void StickersManager::clear_recent_reactions(Promise<Unit> &&promise) {
|
|
|
|
load_recent_reactions();
|
|
|
|
|
|
|
|
if (recent_reactions_.reactions_.empty()) {
|
|
|
|
return promise.set_value(Unit());
|
|
|
|
}
|
|
|
|
|
|
|
|
recent_reactions_.hash_ = 0;
|
|
|
|
recent_reactions_.reactions_.clear();
|
|
|
|
|
|
|
|
td_->create_handler<ClearRecentReactionsQuery>(std::move(promise))->send();
|
|
|
|
}
|
|
|
|
|
2022-01-19 12:14:21 +01:00
|
|
|
void StickersManager::reload_reactions() {
|
2022-08-10 12:57:41 +02:00
|
|
|
if (G()->close_flag() || reactions_.are_being_reloaded_) {
|
2022-01-19 12:14:21 +01:00
|
|
|
return;
|
|
|
|
}
|
2022-08-10 12:57:41 +02:00
|
|
|
CHECK(!td_->auth_manager_->is_bot());
|
2022-01-19 12:14:21 +01:00
|
|
|
reactions_.are_being_reloaded_ = true;
|
2022-09-12 14:03:35 +02:00
|
|
|
load_reactions(); // must be after are_being_reloaded_ is set to true to avoid recursion
|
2022-01-19 12:14:21 +01:00
|
|
|
td_->create_handler<GetAvailableReactionsQuery>()->send(reactions_.hash_);
|
|
|
|
}
|
|
|
|
|
2022-09-12 18:14:02 +02:00
|
|
|
void StickersManager::reload_recent_reactions() {
|
|
|
|
if (G()->close_flag() || recent_reactions_.is_being_reloaded_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
CHECK(!td_->auth_manager_->is_bot());
|
|
|
|
recent_reactions_.is_being_reloaded_ = true;
|
|
|
|
load_recent_reactions(); // must be after is_being_reloaded_ is set to true to avoid recursion
|
2022-09-12 19:17:14 +02:00
|
|
|
td_->create_handler<GetRecentReactionsQuery>()->send(MAX_RECENT_REACTIONS, recent_reactions_.hash_);
|
2022-09-12 18:14:02 +02:00
|
|
|
}
|
|
|
|
|
2022-09-12 16:28:58 +02:00
|
|
|
void StickersManager::reload_top_reactions() {
|
|
|
|
if (G()->close_flag() || top_reactions_.is_being_reloaded_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
CHECK(!td_->auth_manager_->is_bot());
|
|
|
|
top_reactions_.is_being_reloaded_ = true;
|
|
|
|
load_top_reactions(); // must be after is_being_reloaded_ is set to true to avoid recursion
|
|
|
|
td_->create_handler<GetTopReactionsQuery>()->send(top_reactions_.hash_);
|
|
|
|
}
|
|
|
|
|
2021-12-01 17:59:45 +01:00
|
|
|
StickersManager::SpecialStickerSet &StickersManager::add_special_sticker_set(const SpecialStickerSetType &type) {
|
2022-02-10 16:03:03 +01:00
|
|
|
CHECK(!type.is_empty());
|
2022-02-11 17:27:32 +01:00
|
|
|
auto &result_ptr = special_sticker_sets_[type];
|
|
|
|
if (result_ptr == nullptr) {
|
|
|
|
result_ptr = make_unique<SpecialStickerSet>();
|
|
|
|
}
|
|
|
|
auto &result = *result_ptr;
|
2021-12-01 17:59:45 +01:00
|
|
|
if (result.type_.is_empty()) {
|
|
|
|
result.type_ = type;
|
2020-04-19 23:41:26 +02:00
|
|
|
} else {
|
2021-12-01 17:59:45 +01:00
|
|
|
CHECK(result.type_ == type);
|
2020-04-19 23:41:26 +02:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-03-14 01:47:19 +01:00
|
|
|
void StickersManager::init_special_sticker_set(SpecialStickerSet &sticker_set, int64 sticker_set_id, int64 access_hash,
|
|
|
|
string name) {
|
|
|
|
sticker_set.id_ = StickerSetId(sticker_set_id);
|
|
|
|
sticker_set.access_hash_ = access_hash;
|
2020-04-19 15:30:04 +02:00
|
|
|
sticker_set.short_name_ = std::move(name);
|
2020-03-14 01:47:19 +01:00
|
|
|
}
|
|
|
|
|
2020-04-19 23:41:26 +02:00
|
|
|
void StickersManager::load_special_sticker_set_info_from_binlog(SpecialStickerSet &sticker_set) {
|
2019-11-21 20:18:29 +01:00
|
|
|
if (G()->parameters().use_file_db) {
|
2020-04-19 15:30:04 +02:00
|
|
|
string sticker_set_string = G()->td_db()->get_binlog_pmc()->get(sticker_set.type_.type_);
|
2020-03-14 01:47:19 +01:00
|
|
|
if (!sticker_set_string.empty()) {
|
|
|
|
auto parts = full_split(sticker_set_string);
|
2019-11-21 20:18:29 +01:00
|
|
|
if (parts.size() != 3) {
|
2020-03-14 01:47:19 +01:00
|
|
|
LOG(ERROR) << "Can't parse " << sticker_set_string;
|
2019-11-21 20:18:29 +01:00
|
|
|
} else {
|
|
|
|
auto r_sticker_set_id = to_integer_safe<int64>(parts[0]);
|
|
|
|
auto r_sticker_set_access_hash = to_integer_safe<int64>(parts[1]);
|
|
|
|
auto sticker_set_name = parts[2];
|
|
|
|
if (r_sticker_set_id.is_error() || r_sticker_set_access_hash.is_error() ||
|
|
|
|
clean_username(sticker_set_name) != sticker_set_name || sticker_set_name.empty()) {
|
2020-03-14 01:47:19 +01:00
|
|
|
LOG(ERROR) << "Can't parse " << sticker_set_string;
|
2019-11-21 20:18:29 +01:00
|
|
|
} else {
|
2020-03-14 01:47:19 +01:00
|
|
|
init_special_sticker_set(sticker_set, r_sticker_set_id.ok(), r_sticker_set_access_hash.ok(),
|
|
|
|
std::move(sticker_set_name));
|
2019-11-21 20:18:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-21 18:22:05 +01:00
|
|
|
} else {
|
2020-04-19 15:30:04 +02:00
|
|
|
G()->td_db()->get_binlog_pmc()->erase(sticker_set.type_.type_);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!sticker_set.id_.is_valid()) {
|
|
|
|
return;
|
2019-11-21 18:22:05 +01:00
|
|
|
}
|
2019-11-21 20:18:29 +01:00
|
|
|
|
2020-03-14 01:47:19 +01:00
|
|
|
add_sticker_set(sticker_set.id_, sticker_set.access_hash_);
|
2022-07-31 02:17:43 +02:00
|
|
|
auto cleaned_username = clean_username(sticker_set.short_name_);
|
|
|
|
if (!cleaned_username.empty()) {
|
2022-08-03 21:42:52 +02:00
|
|
|
short_name_to_sticker_set_id_.set(cleaned_username, sticker_set.id_);
|
2022-02-10 16:03:03 +01:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-12-01 18:30:02 +01:00
|
|
|
void StickersManager::load_special_sticker_set_by_type(SpecialStickerSetType type) {
|
2020-04-19 23:41:26 +02:00
|
|
|
if (G()->close_flag()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-12-01 17:59:45 +01:00
|
|
|
auto &sticker_set = add_special_sticker_set(type);
|
2021-12-20 13:15:34 +01:00
|
|
|
if (!sticker_set.is_being_loaded_) {
|
|
|
|
return;
|
|
|
|
}
|
2020-04-19 23:41:26 +02:00
|
|
|
sticker_set.is_being_loaded_ = false;
|
|
|
|
load_special_sticker_set(sticker_set);
|
|
|
|
}
|
|
|
|
|
|
|
|
void StickersManager::load_special_sticker_set(SpecialStickerSet &sticker_set) {
|
2022-11-01 16:38:07 +01:00
|
|
|
CHECK(!td_->auth_manager_->is_bot() || sticker_set.type_ == SpecialStickerSetType::default_topic_icons());
|
2021-09-24 12:08:32 +02:00
|
|
|
if (sticker_set.is_being_loaded_) {
|
|
|
|
return;
|
|
|
|
}
|
2020-04-19 23:41:26 +02:00
|
|
|
sticker_set.is_being_loaded_ = true;
|
2021-10-27 14:19:07 +02:00
|
|
|
LOG(INFO) << "Load " << sticker_set.type_.type_ << " " << sticker_set.id_;
|
2020-04-19 23:41:26 +02:00
|
|
|
if (sticker_set.id_.is_valid()) {
|
2021-12-08 14:54:45 +01:00
|
|
|
auto s = get_sticker_set(sticker_set.id_);
|
|
|
|
CHECK(s != nullptr);
|
2022-08-07 18:47:50 +02:00
|
|
|
if (s->was_loaded_) {
|
|
|
|
reload_special_sticker_set(sticker_set, s->is_loaded_ ? s->hash_ : 0);
|
2021-12-08 14:54:45 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-19 23:41:26 +02:00
|
|
|
auto promise = PromiseCreator::lambda([actor_id = actor_id(this), type = sticker_set.type_](Result<Unit> &&result) {
|
|
|
|
send_closure(actor_id, &StickersManager::on_load_special_sticker_set, type,
|
|
|
|
result.is_ok() ? Status::OK() : result.move_as_error());
|
|
|
|
});
|
|
|
|
load_sticker_sets({sticker_set.id_}, std::move(promise));
|
|
|
|
} else {
|
2021-12-01 18:30:02 +01:00
|
|
|
reload_special_sticker_set(sticker_set, 0);
|
2020-04-19 23:41:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-01 20:05:40 +01:00
|
|
|
void StickersManager::reload_special_sticker_set_by_type(SpecialStickerSetType type, bool is_recursive) {
|
|
|
|
if (G()->close_flag()) {
|
|
|
|
return;
|
|
|
|
}
|
2022-05-12 15:34:13 +02:00
|
|
|
if (disable_animated_emojis_ &&
|
|
|
|
(type == SpecialStickerSetType::animated_emoji() || type == SpecialStickerSetType::animated_emoji_click())) {
|
|
|
|
return;
|
|
|
|
}
|
2021-12-01 20:05:40 +01:00
|
|
|
|
2021-12-01 18:30:02 +01:00
|
|
|
auto &sticker_set = add_special_sticker_set(type);
|
|
|
|
if (sticker_set.is_being_reloaded_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!sticker_set.id_.is_valid()) {
|
|
|
|
return reload_special_sticker_set(sticker_set, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto *s = get_sticker_set(sticker_set.id_);
|
2022-08-07 18:47:50 +02:00
|
|
|
if (s != nullptr && s->is_inited_ && s->was_loaded_) {
|
|
|
|
return reload_special_sticker_set(sticker_set, s->is_loaded_ ? s->hash_ : 0);
|
2021-12-01 20:05:40 +01:00
|
|
|
}
|
|
|
|
if (!is_recursive) {
|
|
|
|
auto promise = PromiseCreator::lambda([actor_id = actor_id(this), type = std::move(type)](Unit result) mutable {
|
|
|
|
send_closure(actor_id, &StickersManager::reload_special_sticker_set_by_type, std::move(type), true);
|
|
|
|
});
|
|
|
|
return load_sticker_sets({sticker_set.id_}, std::move(promise));
|
2021-11-19 12:07:05 +01:00
|
|
|
}
|
2021-12-01 18:30:02 +01:00
|
|
|
|
|
|
|
reload_special_sticker_set(sticker_set, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void StickersManager::reload_special_sticker_set(SpecialStickerSet &sticker_set, int32 hash) {
|
|
|
|
if (sticker_set.is_being_reloaded_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
sticker_set.is_being_reloaded_ = true;
|
2021-11-19 12:07:05 +01:00
|
|
|
td_->create_handler<ReloadSpecialStickerSetQuery>()->send(sticker_set.id_, sticker_set.type_, hash);
|
2020-04-19 23:41:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void StickersManager::on_load_special_sticker_set(const SpecialStickerSetType &type, Status result) {
|
|
|
|
if (G()->close_flag()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-12-01 17:59:45 +01:00
|
|
|
auto &special_sticker_set = add_special_sticker_set(type);
|
2021-12-01 18:30:02 +01:00
|
|
|
special_sticker_set.is_being_reloaded_ = false;
|
2020-04-19 23:41:26 +02:00
|
|
|
if (!special_sticker_set.is_being_loaded_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result.is_error()) {
|
2021-12-20 13:15:34 +01:00
|
|
|
LOG(INFO) << "Failed to load special sticker set " << type.type_ << ": " << result.error();
|
|
|
|
|
2020-04-19 23:41:26 +02:00
|
|
|
// failed to load the special sticker set; repeat after some time
|
|
|
|
create_actor<SleepActor>("RetryLoadSpecialStickerSetActor", Random::fast(300, 600),
|
2021-12-01 18:30:02 +01:00
|
|
|
PromiseCreator::lambda([actor_id = actor_id(this), type](Result<Unit> result) mutable {
|
|
|
|
send_closure(actor_id, &StickersManager::load_special_sticker_set_by_type,
|
|
|
|
std::move(type));
|
2020-04-19 23:41:26 +02:00
|
|
|
}))
|
|
|
|
.release();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
special_sticker_set.is_being_loaded_ = false;
|
|
|
|
|
2021-12-01 17:59:45 +01:00
|
|
|
if (type == SpecialStickerSetType::animated_emoji()) {
|
2022-04-13 16:40:12 +02:00
|
|
|
set_promises(pending_get_animated_emoji_queries_);
|
2022-07-22 14:40:24 +02:00
|
|
|
try_update_animated_emoji_messages();
|
2021-10-05 17:06:05 +02:00
|
|
|
return;
|
|
|
|
}
|
2022-07-15 14:24:08 +02:00
|
|
|
if (type == SpecialStickerSetType::premium_gifts()) {
|
2022-07-15 15:15:59 +02:00
|
|
|
set_promises(pending_get_premium_gift_option_sticker_queries_);
|
2022-07-22 14:40:24 +02:00
|
|
|
try_update_premium_gift_messages();
|
2022-07-15 14:24:08 +02:00
|
|
|
return;
|
|
|
|
}
|
2022-09-02 12:59:49 +02:00
|
|
|
if (type == SpecialStickerSetType::generic_animations()) {
|
2022-09-12 14:44:37 +02:00
|
|
|
set_promises(pending_get_generic_animations_queries_);
|
2022-09-02 12:59:49 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (type == SpecialStickerSetType::default_statuses()) {
|
2022-09-02 14:02:37 +02:00
|
|
|
set_promises(pending_get_default_statuses_queries_);
|
2022-09-02 12:59:49 +02:00
|
|
|
return;
|
|
|
|
}
|
2022-10-27 09:06:13 +02:00
|
|
|
if (type == SpecialStickerSetType::default_topic_icons()) {
|
|
|
|
set_promises(pending_get_default_topic_icons_queries_);
|
|
|
|
return;
|
|
|
|
}
|
2021-10-05 17:06:05 +02:00
|
|
|
|
2020-04-19 23:41:26 +02:00
|
|
|
CHECK(special_sticker_set.id_.is_valid());
|
|
|
|
auto sticker_set = get_sticker_set(special_sticker_set.id_);
|
|
|
|
CHECK(sticker_set != nullptr);
|
2022-08-07 18:47:50 +02:00
|
|
|
CHECK(sticker_set->was_loaded_);
|
2020-04-19 23:41:26 +02:00
|
|
|
|
2021-12-01 17:59:45 +01:00
|
|
|
if (type == SpecialStickerSetType::animated_emoji_click()) {
|
2021-09-17 14:51:42 +02:00
|
|
|
auto pending_get_requests = std::move(pending_get_animated_emoji_click_stickers_);
|
2021-09-15 23:41:40 +02:00
|
|
|
reset_to_empty(pending_get_animated_emoji_click_stickers_);
|
2021-09-17 14:51:42 +02:00
|
|
|
for (auto &pending_request : pending_get_requests) {
|
2022-05-12 17:02:27 +02:00
|
|
|
choose_animated_emoji_click_sticker(sticker_set, std::move(pending_request.message_text_),
|
|
|
|
pending_request.full_message_id_, pending_request.start_time_,
|
|
|
|
std::move(pending_request.promise_));
|
2021-09-15 23:41:40 +02:00
|
|
|
}
|
2021-09-17 14:51:42 +02:00
|
|
|
auto pending_click_requests = std::move(pending_on_animated_emoji_message_clicked_);
|
|
|
|
reset_to_empty(pending_on_animated_emoji_message_clicked_);
|
|
|
|
for (auto &pending_request : pending_click_requests) {
|
2021-09-17 18:59:31 +02:00
|
|
|
schedule_update_animated_emoji_clicked(sticker_set, pending_request.emoji_, pending_request.full_message_id_,
|
|
|
|
std::move(pending_request.clicks_));
|
2021-09-17 14:51:42 +02:00
|
|
|
}
|
2021-09-15 23:41:40 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto emoji = type.get_dice_emoji();
|
|
|
|
CHECK(!emoji.empty());
|
|
|
|
|
2020-04-19 23:41:26 +02:00
|
|
|
auto it = dice_messages_.find(emoji);
|
|
|
|
if (it == dice_messages_.end()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
vector<FullMessageId> full_message_ids;
|
2022-08-20 23:20:57 +02:00
|
|
|
it->second.foreach([&](const FullMessageId &full_message_id) { full_message_ids.push_back(full_message_id); });
|
2020-04-19 23:41:26 +02:00
|
|
|
CHECK(!full_message_ids.empty());
|
2021-11-10 18:39:35 +01:00
|
|
|
for (const auto &full_message_id : full_message_ids) {
|
2020-04-19 23:41:26 +02:00
|
|
|
td_->messages_manager_->on_external_update_message_content(full_message_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void StickersManager::tear_down() {
|
|
|
|
parent_.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
tl_object_ptr<td_api::MaskPoint> StickersManager::get_mask_point_object(int32 point) {
|
|
|
|
switch (point) {
|
|
|
|
case 0:
|
|
|
|
return td_api::make_object<td_api::maskPointForehead>();
|
|
|
|
case 1:
|
|
|
|
return td_api::make_object<td_api::maskPointEyes>();
|
|
|
|
case 2:
|
|
|
|
return td_api::make_object<td_api::maskPointMouth>();
|
|
|
|
case 3:
|
|
|
|
return td_api::make_object<td_api::maskPointChin>();
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-18 13:51:37 +02:00
|
|
|
StickerType StickersManager::get_sticker_type(FileId file_id) const {
|
2022-08-03 21:42:52 +02:00
|
|
|
const auto *sticker = get_sticker(file_id);
|
2022-07-18 13:51:37 +02:00
|
|
|
CHECK(sticker != nullptr);
|
2022-08-07 18:47:50 +02:00
|
|
|
return sticker->type_;
|
2022-07-18 13:51:37 +02:00
|
|
|
}
|
|
|
|
|
2022-10-03 00:26:32 +02:00
|
|
|
bool StickersManager::is_premium_custom_emoji(CustomEmojiId custom_emoji_id, bool default_result) const {
|
2022-08-04 13:48:10 +02:00
|
|
|
auto sticker_id = custom_emoji_to_sticker_id_.get(custom_emoji_id);
|
2022-09-05 19:26:10 +02:00
|
|
|
if (!sticker_id.is_valid()) {
|
2022-07-27 22:36:44 +02:00
|
|
|
return default_result;
|
2022-07-22 18:06:39 +02:00
|
|
|
}
|
2022-09-05 19:26:10 +02:00
|
|
|
const Sticker *s = get_sticker(sticker_id);
|
|
|
|
CHECK(s != nullptr);
|
2022-08-07 18:47:50 +02:00
|
|
|
return s->is_premium_;
|
2022-07-22 17:11:55 +02:00
|
|
|
}
|
|
|
|
|
2022-10-03 00:26:32 +02:00
|
|
|
CustomEmojiId StickersManager::get_custom_emoji_id(FileId sticker_id) const {
|
2022-07-22 17:27:12 +02:00
|
|
|
auto sticker_file_view = td_->file_manager_->get_file_view(sticker_id);
|
|
|
|
if (sticker_file_view.is_encrypted() || !sticker_file_view.has_remote_location() ||
|
|
|
|
!sticker_file_view.remote_location().is_document()) {
|
2022-10-03 00:26:32 +02:00
|
|
|
return CustomEmojiId();
|
2022-07-22 17:27:12 +02:00
|
|
|
}
|
2022-10-03 00:26:32 +02:00
|
|
|
return CustomEmojiId(sticker_file_view.remote_location().get_id());
|
2022-07-22 17:27:12 +02:00
|
|
|
}
|
|
|
|
|
2021-03-01 02:19:50 +01:00
|
|
|
vector<td_api::object_ptr<td_api::closedVectorPath>> StickersManager::get_sticker_minithumbnail(
|
2021-10-14 00:06:12 +02:00
|
|
|
CSlice path, StickerSetId sticker_set_id, int64 document_id, double zoom) {
|
2020-11-20 14:22:07 +01:00
|
|
|
if (path.empty()) {
|
2020-12-02 16:45:06 +01:00
|
|
|
return {};
|
2020-11-20 14:22:07 +01:00
|
|
|
}
|
|
|
|
|
2020-12-02 16:45:06 +01:00
|
|
|
auto buf = StackAllocator::alloc(1 << 9);
|
2020-11-20 14:22:07 +01:00
|
|
|
StringBuilder sb(buf.as_slice(), true);
|
|
|
|
|
2020-12-02 16:45:06 +01:00
|
|
|
sb << 'M';
|
2020-11-20 14:22:07 +01:00
|
|
|
for (unsigned char c : path) {
|
|
|
|
if (c >= 128 + 64) {
|
|
|
|
sb << "AACAAAAHAAALMAAAQASTAVAAAZaacaaaahaaalmaaaqastava.az0123456789-,"[c - 128 - 64];
|
|
|
|
} else {
|
|
|
|
if (c >= 128) {
|
|
|
|
sb << ',';
|
|
|
|
} else if (c >= 64) {
|
|
|
|
sb << '-';
|
|
|
|
}
|
|
|
|
sb << (c & 63);
|
|
|
|
}
|
|
|
|
}
|
2020-12-02 16:45:06 +01:00
|
|
|
sb << 'z';
|
2020-11-20 14:22:07 +01:00
|
|
|
|
|
|
|
CHECK(!sb.is_error());
|
2020-12-02 16:45:06 +01:00
|
|
|
path = sb.as_cslice();
|
2020-12-13 22:36:44 +01:00
|
|
|
LOG(DEBUG) << "Transform SVG path " << path;
|
2020-12-02 16:45:06 +01:00
|
|
|
|
|
|
|
size_t pos = 0;
|
|
|
|
auto skip_commas = [&path, &pos] {
|
|
|
|
while (path[pos] == ',') {
|
|
|
|
pos++;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
auto get_number = [&] {
|
|
|
|
skip_commas();
|
|
|
|
int sign = 1;
|
|
|
|
if (path[pos] == '-') {
|
|
|
|
sign = -1;
|
|
|
|
pos++;
|
|
|
|
}
|
|
|
|
double res = 0;
|
|
|
|
while (is_digit(path[pos])) {
|
|
|
|
res = res * 10 + path[pos++] - '0';
|
|
|
|
}
|
|
|
|
if (path[pos] == '.') {
|
|
|
|
pos++;
|
|
|
|
double mul = 0.1;
|
|
|
|
while (is_digit(path[pos])) {
|
|
|
|
res += (path[pos] - '0') * mul;
|
|
|
|
mul *= 0.1;
|
|
|
|
pos++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return sign * res;
|
|
|
|
};
|
2021-10-14 00:06:12 +02:00
|
|
|
auto make_point = [zoom](double x, double y) {
|
|
|
|
return td_api::make_object<td_api::point>(x * zoom, y * zoom);
|
2020-12-02 16:45:06 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
vector<td_api::object_ptr<td_api::closedVectorPath>> result;
|
|
|
|
double x = 0;
|
|
|
|
double y = 0;
|
|
|
|
while (path[pos] != '\0') {
|
|
|
|
skip_commas();
|
|
|
|
if (path[pos] == '\0') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-12-05 15:37:10 +01:00
|
|
|
while (path[pos] == 'm' || path[pos] == 'M') {
|
|
|
|
auto command = path[pos++];
|
|
|
|
do {
|
|
|
|
if (command == 'm') {
|
|
|
|
x += get_number();
|
|
|
|
y += get_number();
|
|
|
|
} else {
|
|
|
|
x = get_number();
|
|
|
|
y = get_number();
|
|
|
|
}
|
|
|
|
skip_commas();
|
|
|
|
} while (path[pos] != '\0' && !is_alpha(path[pos]));
|
2020-12-02 16:45:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
double start_x = x;
|
|
|
|
double start_y = y;
|
|
|
|
|
|
|
|
vector<td_api::object_ptr<td_api::VectorPathCommand>> commands;
|
|
|
|
bool have_last_end_control_point = false;
|
|
|
|
double last_end_control_point_x = 0;
|
|
|
|
double last_end_control_point_y = 0;
|
|
|
|
bool is_closed = false;
|
2020-12-05 15:37:10 +01:00
|
|
|
char command = '-';
|
2020-12-02 16:45:06 +01:00
|
|
|
while (!is_closed) {
|
|
|
|
skip_commas();
|
|
|
|
if (path[pos] == '\0') {
|
2021-03-02 02:48:02 +01:00
|
|
|
LOG(ERROR) << "Receive unclosed path " << path << " in a sticker " << document_id << " from " << sticker_set_id;
|
2020-12-02 16:45:06 +01:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
if (is_alpha(path[pos])) {
|
|
|
|
command = path[pos++];
|
|
|
|
}
|
|
|
|
switch (command) {
|
|
|
|
case 'l':
|
|
|
|
case 'L':
|
|
|
|
case 'h':
|
|
|
|
case 'H':
|
|
|
|
case 'v':
|
|
|
|
case 'V':
|
|
|
|
if (command == 'l' || command == 'h') {
|
|
|
|
x += get_number();
|
|
|
|
} else if (command == 'L' || command == 'H') {
|
|
|
|
x = get_number();
|
|
|
|
}
|
|
|
|
if (command == 'l' || command == 'v') {
|
|
|
|
y += get_number();
|
|
|
|
} else if (command == 'L' || command == 'V') {
|
|
|
|
y = get_number();
|
|
|
|
}
|
|
|
|
commands.push_back(td_api::make_object<td_api::vectorPathCommandLine>(make_point(x, y)));
|
|
|
|
have_last_end_control_point = false;
|
|
|
|
break;
|
|
|
|
case 'C':
|
|
|
|
case 'c':
|
|
|
|
case 'S':
|
|
|
|
case 's': {
|
|
|
|
double start_control_point_x;
|
|
|
|
double start_control_point_y;
|
|
|
|
if (command == 'S' || command == 's') {
|
|
|
|
if (have_last_end_control_point) {
|
|
|
|
start_control_point_x = 2 * x - last_end_control_point_x;
|
|
|
|
start_control_point_y = 2 * y - last_end_control_point_y;
|
|
|
|
} else {
|
|
|
|
start_control_point_x = x;
|
|
|
|
start_control_point_y = y;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
start_control_point_x = get_number();
|
|
|
|
start_control_point_y = get_number();
|
|
|
|
if (command == 'c') {
|
|
|
|
start_control_point_x += x;
|
|
|
|
start_control_point_y += y;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
last_end_control_point_x = get_number();
|
|
|
|
last_end_control_point_y = get_number();
|
|
|
|
if (command == 'c' || command == 's') {
|
|
|
|
last_end_control_point_x += x;
|
|
|
|
last_end_control_point_y += y;
|
|
|
|
}
|
|
|
|
have_last_end_control_point = true;
|
|
|
|
|
|
|
|
if (command == 'c' || command == 's') {
|
|
|
|
x += get_number();
|
|
|
|
y += get_number();
|
|
|
|
} else {
|
|
|
|
x = get_number();
|
|
|
|
y = get_number();
|
|
|
|
}
|
|
|
|
|
|
|
|
commands.push_back(td_api::make_object<td_api::vectorPathCommandCubicBezierCurve>(
|
|
|
|
make_point(start_control_point_x, start_control_point_y),
|
|
|
|
make_point(last_end_control_point_x, last_end_control_point_y), make_point(x, y)));
|
|
|
|
break;
|
|
|
|
}
|
2020-12-16 14:04:27 +01:00
|
|
|
case 'm':
|
|
|
|
case 'M':
|
|
|
|
pos--;
|
2020-12-22 00:09:08 +01:00
|
|
|
// fallthrough
|
2020-12-02 16:45:06 +01:00
|
|
|
case 'z':
|
|
|
|
case 'Z':
|
|
|
|
if (x != start_x || y != start_y) {
|
|
|
|
x = start_x;
|
|
|
|
y = start_y;
|
|
|
|
commands.push_back(td_api::make_object<td_api::vectorPathCommandLine>(make_point(x, y)));
|
|
|
|
}
|
|
|
|
if (!commands.empty()) {
|
|
|
|
result.push_back(td_api::make_object<td_api::closedVectorPath>(std::move(commands)));
|
2021-10-17 12:07:20 +02:00
|
|
|
commands.clear();
|
2020-12-02 16:45:06 +01:00
|
|
|
}
|
|
|
|
is_closed = true;
|
|
|
|
break;
|
|
|
|
default:
|
2021-03-02 02:48:02 +01:00
|
|
|
LOG(ERROR) << "Receive invalid command " << command << " at pos " << pos << " in a sticker " << document_id
|
|
|
|
<< " from " << sticker_set_id << ": " << path;
|
2020-12-02 16:45:06 +01:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
string svg;
|
|
|
|
for (const auto &vector_path : result) {
|
|
|
|
CHECK(!vector_path->commands_.empty());
|
|
|
|
svg += 'M';
|
|
|
|
auto add_point = [&](const td_api::object_ptr<td_api::point> &p) {
|
|
|
|
svg += to_string(static_cast<int>(p->x_));
|
|
|
|
svg += ',';
|
|
|
|
svg += to_string(static_cast<int>(p->y_));
|
|
|
|
svg += ',';
|
|
|
|
};
|
|
|
|
auto last_command = vector_path->commands_.back().get();
|
|
|
|
switch (last_command->get_id()) {
|
|
|
|
case td_api::vectorPathCommandLine::ID:
|
|
|
|
add_point(static_cast<const td_api::vectorPathCommandLine *>(last_command)->end_point_);
|
|
|
|
break;
|
|
|
|
case td_api::vectorPathCommandCubicBezierCurve::ID:
|
|
|
|
add_point(static_cast<const td_api::vectorPathCommandCubicBezierCurve *>(last_command)->end_point_);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
for (auto &command : vector_path->commands_) {
|
|
|
|
switch (command->get_id()) {
|
|
|
|
case td_api::vectorPathCommandLine::ID: {
|
|
|
|
auto line = static_cast<const td_api::vectorPathCommandLine *>(command.get());
|
|
|
|
svg += 'L';
|
|
|
|
add_point(line->end_point_);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case td_api::vectorPathCommandCubicBezierCurve::ID: {
|
|
|
|
auto curve = static_cast<const td_api::vectorPathCommandCubicBezierCurve *>(command.get());
|
|
|
|
svg += 'C';
|
|
|
|
add_point(curve->start_control_point_);
|
|
|
|
add_point(curve->end_control_point_);
|
|
|
|
add_point(curve->end_point_);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
svg += 'z';
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
return result;
|
2020-11-20 14:22:07 +01:00
|
|
|
}
|
|
|
|
|
2021-10-13 23:18:44 +02:00
|
|
|
tl_object_ptr<td_api::sticker> StickersManager::get_sticker_object(FileId file_id, bool for_animated_emoji,
|
|
|
|
bool for_clicked_animated_emoji) const {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (!file_id.is_valid()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2022-08-03 21:42:52 +02:00
|
|
|
const auto *sticker = get_sticker(file_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK(sticker != nullptr);
|
2022-08-07 18:47:50 +02:00
|
|
|
auto mask_position = sticker->point_ >= 0
|
|
|
|
? make_tl_object<td_api::maskPosition>(get_mask_point_object(sticker->point_),
|
|
|
|
sticker->x_shift_, sticker->y_shift_, sticker->scale_)
|
2018-12-31 20:04:05 +01:00
|
|
|
: nullptr;
|
|
|
|
|
2022-08-07 18:47:50 +02:00
|
|
|
const PhotoSize &thumbnail = sticker->m_thumbnail_.file_id.is_valid() ? sticker->m_thumbnail_ : sticker->s_thumbnail_;
|
2020-05-31 21:22:15 +02:00
|
|
|
auto thumbnail_format = PhotoFormat::Webp;
|
2022-07-19 13:45:37 +02:00
|
|
|
int64 document_id = 0;
|
2022-10-03 00:26:32 +02:00
|
|
|
CustomEmojiId custom_emoji_id;
|
2022-08-07 18:47:50 +02:00
|
|
|
if (!sticker->set_id_.is_valid()) {
|
|
|
|
auto sticker_file_view = td_->file_manager_->get_file_view(sticker->file_id_);
|
2021-03-02 02:40:10 +01:00
|
|
|
if (sticker_file_view.is_encrypted()) {
|
2020-05-31 21:22:15 +02:00
|
|
|
// uploaded to secret chats stickers have JPEG thumbnail instead of server-generated WEBP
|
|
|
|
thumbnail_format = PhotoFormat::Jpeg;
|
2021-03-02 02:40:10 +01:00
|
|
|
} else {
|
2021-03-02 02:48:02 +01:00
|
|
|
if (sticker_file_view.has_remote_location() && sticker_file_view.remote_location().is_document()) {
|
|
|
|
document_id = sticker_file_view.remote_location().get_id();
|
|
|
|
}
|
2021-03-02 02:50:14 +01:00
|
|
|
|
|
|
|
if (thumbnail.file_id.is_valid()) {
|
|
|
|
auto thumbnail_file_view = td_->file_manager_->get_file_view(thumbnail.file_id);
|
|
|
|
if (ends_with(thumbnail_file_view.suggested_path(), ".jpg")) {
|
|
|
|
thumbnail_format = PhotoFormat::Jpeg;
|
|
|
|
}
|
2021-03-02 02:40:10 +01:00
|
|
|
}
|
2020-05-31 21:22:15 +02:00
|
|
|
}
|
2022-08-07 18:47:50 +02:00
|
|
|
} else if (sticker->type_ == StickerType::CustomEmoji) {
|
2022-10-03 00:26:32 +02:00
|
|
|
custom_emoji_id = get_custom_emoji_id(sticker->file_id_);
|
2020-05-31 21:22:15 +02:00
|
|
|
}
|
|
|
|
auto thumbnail_object = get_thumbnail_object(td_->file_manager_.get(), thumbnail, thumbnail_format);
|
2022-08-07 18:47:50 +02:00
|
|
|
int32 width = sticker->dimensions_.width;
|
|
|
|
int32 height = sticker->dimensions_.height;
|
2021-10-14 00:06:12 +02:00
|
|
|
double zoom = 1.0;
|
2022-08-07 18:47:50 +02:00
|
|
|
if ((is_sticker_format_vector(sticker->format_) || sticker->type_ == StickerType::CustomEmoji) &&
|
2022-08-05 16:58:16 +02:00
|
|
|
(for_animated_emoji || for_clicked_animated_emoji)) {
|
2022-08-07 18:47:50 +02:00
|
|
|
if (sticker->type_ == StickerType::CustomEmoji && max(width, height) <= 100) {
|
2022-08-05 16:58:16 +02:00
|
|
|
zoom *= 5.12;
|
|
|
|
}
|
2021-10-13 23:18:44 +02:00
|
|
|
width = static_cast<int32>(width * zoom + 0.5);
|
|
|
|
height = static_cast<int32>(height * zoom + 0.5);
|
2022-09-26 15:13:57 +02:00
|
|
|
if (for_clicked_animated_emoji) {
|
|
|
|
zoom *= 3;
|
|
|
|
width *= 3;
|
|
|
|
height *= 3;
|
|
|
|
}
|
2021-10-13 23:18:44 +02:00
|
|
|
}
|
2022-08-07 18:47:50 +02:00
|
|
|
auto premium_animation_object = sticker->premium_animation_file_id_.is_valid()
|
|
|
|
? td_->file_manager_->get_file_object(sticker->premium_animation_file_id_)
|
2022-04-26 15:01:29 +02:00
|
|
|
: nullptr;
|
|
|
|
return td_api::make_object<td_api::sticker>(
|
2022-08-07 18:47:50 +02:00
|
|
|
sticker->set_id_.get(), width, height, sticker->alt_, get_sticker_format_object(sticker->format_),
|
2022-10-03 00:26:32 +02:00
|
|
|
get_sticker_type_object(sticker->type_), std::move(mask_position), custom_emoji_id.get(),
|
2022-08-07 18:47:50 +02:00
|
|
|
get_sticker_minithumbnail(sticker->minithumbnail_, sticker->set_id_, document_id, zoom),
|
|
|
|
std::move(thumbnail_object), sticker->is_premium_, std::move(premium_animation_object),
|
2022-07-15 12:37:46 +02:00
|
|
|
td_->file_manager_->get_file_object(file_id));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2018-09-20 21:25:09 +02:00
|
|
|
tl_object_ptr<td_api::stickers> StickersManager::get_stickers_object(const vector<FileId> &sticker_ids) const {
|
2022-09-30 14:55:46 +02:00
|
|
|
return td_api::make_object<td_api::stickers>(
|
|
|
|
transform(sticker_ids, [&](FileId sticker_id) { return get_sticker_object(sticker_id); }));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2020-10-15 14:59:51 +02:00
|
|
|
tl_object_ptr<td_api::DiceStickers> StickersManager::get_dice_stickers_object(const string &emoji, int32 value) const {
|
2020-04-19 23:41:26 +02:00
|
|
|
if (td_->auth_manager_->is_bot()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (!td::contains(dice_emojis_, emoji)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto it = special_sticker_sets_.find(SpecialStickerSetType::animated_dice(emoji));
|
|
|
|
if (it == special_sticker_sets_.end()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2022-02-11 17:27:32 +01:00
|
|
|
auto sticker_set_id = it->second->id_;
|
2020-10-15 14:59:51 +02:00
|
|
|
if (!sticker_set_id.is_valid()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto sticker_set = get_sticker_set(sticker_set_id);
|
|
|
|
CHECK(sticker_set != nullptr);
|
2022-08-07 18:47:50 +02:00
|
|
|
if (!sticker_set->was_loaded_) {
|
2020-10-15 14:59:51 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto get_sticker = [&](int32 value) {
|
2022-08-07 18:47:50 +02:00
|
|
|
return get_sticker_object(sticker_set->sticker_ids_[value], true);
|
2020-10-15 14:59:51 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
if (emoji == "🎰") {
|
2022-08-07 18:47:50 +02:00
|
|
|
if (sticker_set->sticker_ids_.size() < 21 || value < 0 || value > 64) {
|
2020-10-15 14:59:51 +02:00
|
|
|
return nullptr;
|
2020-04-19 23:41:26 +02:00
|
|
|
}
|
2020-10-15 14:59:51 +02:00
|
|
|
|
2020-10-20 20:40:05 +02:00
|
|
|
int32 background_id = value == 1 || value == 22 || value == 43 || value == 64 ? 1 : 0;
|
2020-10-15 14:59:51 +02:00
|
|
|
int32 lever_id = 2;
|
|
|
|
int32 left_reel_id = value == 64 ? 3 : 8;
|
|
|
|
int32 center_reel_id = value == 64 ? 9 : 14;
|
|
|
|
int32 right_reel_id = value == 64 ? 15 : 20;
|
|
|
|
if (value != 0 && value != 64) {
|
|
|
|
left_reel_id = 4 + (value % 4);
|
|
|
|
center_reel_id = 10 + ((value + 3) / 4 % 4);
|
|
|
|
right_reel_id = 16 + ((value + 15) / 16 % 4);
|
|
|
|
}
|
|
|
|
return td_api::make_object<td_api::diceStickersSlotMachine>(get_sticker(background_id), get_sticker(lever_id),
|
|
|
|
get_sticker(left_reel_id), get_sticker(center_reel_id),
|
|
|
|
get_sticker(right_reel_id));
|
|
|
|
}
|
|
|
|
|
2022-08-07 18:47:50 +02:00
|
|
|
if (value >= 0 && value < static_cast<int32>(sticker_set->sticker_ids_.size())) {
|
2020-10-15 14:59:51 +02:00
|
|
|
return td_api::make_object<td_api::diceStickersRegular>(get_sticker(value));
|
2020-04-19 23:41:26 +02:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-04-21 18:08:01 +02:00
|
|
|
int32 StickersManager::get_dice_success_animation_frame_number(const string &emoji, int32 value) const {
|
2020-04-21 13:51:46 +02:00
|
|
|
if (td_->auth_manager_->is_bot()) {
|
2020-04-21 18:08:01 +02:00
|
|
|
return std::numeric_limits<int32>::max();
|
2020-04-21 13:51:46 +02:00
|
|
|
}
|
|
|
|
if (value == 0 || !td::contains(dice_emojis_, emoji)) {
|
2020-04-21 18:08:01 +02:00
|
|
|
return std::numeric_limits<int32>::max();
|
2020-04-21 13:51:46 +02:00
|
|
|
}
|
|
|
|
auto pos = static_cast<size_t>(std::find(dice_emojis_.begin(), dice_emojis_.end(), emoji) - dice_emojis_.begin());
|
|
|
|
if (pos >= dice_success_values_.size()) {
|
2020-04-21 18:08:01 +02:00
|
|
|
return std::numeric_limits<int32>::max();
|
2020-04-21 13:51:46 +02:00
|
|
|
}
|
|
|
|
|
2020-04-21 18:08:01 +02:00
|
|
|
auto &result = dice_success_values_[pos];
|
|
|
|
return result.first == value ? result.second : std::numeric_limits<int32>::max();
|
2020-04-21 13:51:46 +02:00
|
|
|
}
|
|
|
|
|
2022-01-18 17:20:43 +01:00
|
|
|
PhotoFormat StickersManager::get_sticker_set_thumbnail_format(StickerFormat sticker_format) {
|
|
|
|
switch (sticker_format) {
|
|
|
|
case StickerFormat::Unknown:
|
|
|
|
return PhotoFormat::Webp;
|
|
|
|
case StickerFormat::Webp:
|
|
|
|
return PhotoFormat::Webp;
|
|
|
|
case StickerFormat::Tgs:
|
|
|
|
return PhotoFormat::Tgs;
|
2022-01-25 12:58:04 +01:00
|
|
|
case StickerFormat::Webm:
|
|
|
|
return PhotoFormat::Webm;
|
2022-01-18 17:20:43 +01:00
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return PhotoFormat::Webp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-11 16:07:56 +01:00
|
|
|
double StickersManager::get_sticker_set_minithumbnail_zoom(const StickerSet *sticker_set) {
|
2022-08-07 18:47:50 +02:00
|
|
|
if (sticker_set->sticker_format_ == StickerFormat::Tgs) {
|
2022-02-11 16:07:56 +01:00
|
|
|
return 100.0 / 512.0;
|
|
|
|
}
|
|
|
|
return 1.0;
|
|
|
|
}
|
|
|
|
|
2022-07-16 14:45:30 +02:00
|
|
|
td_api::object_ptr<td_api::thumbnail> StickersManager::get_sticker_set_thumbnail_object(
|
|
|
|
const StickerSet *sticker_set) const {
|
|
|
|
CHECK(sticker_set != nullptr);
|
2022-08-07 18:47:50 +02:00
|
|
|
if (sticker_set->thumbnail_document_id_ != 0 && sticker_set->sticker_type_ == StickerType::CustomEmoji) {
|
|
|
|
for (auto sticker_id : sticker_set->sticker_ids_) {
|
2022-07-16 16:22:30 +02:00
|
|
|
auto file_view = td_->file_manager_->get_file_view(sticker_id);
|
|
|
|
if (file_view.has_remote_location() && !file_view.remote_location().is_web() &&
|
2022-08-07 18:47:50 +02:00
|
|
|
file_view.remote_location().get_id() == sticker_set->thumbnail_document_id_) {
|
2022-07-16 16:22:30 +02:00
|
|
|
const Sticker *s = get_sticker(sticker_id);
|
2022-08-07 18:47:50 +02:00
|
|
|
auto thumbnail_format = get_sticker_set_thumbnail_format(s->format_);
|
2022-07-16 16:22:30 +02:00
|
|
|
PhotoSize thumbnail;
|
|
|
|
thumbnail.type = 't';
|
|
|
|
thumbnail.size = static_cast<int32>(file_view.size());
|
2022-08-07 18:47:50 +02:00
|
|
|
thumbnail.dimensions = s->dimensions_;
|
|
|
|
thumbnail.file_id = s->file_id_;
|
2022-07-16 16:22:30 +02:00
|
|
|
return get_thumbnail_object(td_->file_manager_.get(), thumbnail, thumbnail_format);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-08-07 18:47:50 +02:00
|
|
|
auto thumbnail_format = get_sticker_set_thumbnail_format(sticker_set->sticker_format_);
|
|
|
|
return get_thumbnail_object(td_->file_manager_.get(), sticker_set->thumbnail_, thumbnail_format);
|
2022-07-16 14:45:30 +02:00
|
|
|
}
|
|
|
|
|
2019-09-18 05:55:43 +02:00
|
|
|
tl_object_ptr<td_api::stickerSet> StickersManager::get_sticker_set_object(StickerSetId sticker_set_id) const {
|
2018-12-31 20:04:05 +01:00
|
|
|
const StickerSet *sticker_set = get_sticker_set(sticker_set_id);
|
|
|
|
CHECK(sticker_set != nullptr);
|
2022-08-07 18:47:50 +02:00
|
|
|
CHECK(sticker_set->was_loaded_);
|
|
|
|
sticker_set->was_update_sent_ = true;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
std::vector<tl_object_ptr<td_api::sticker>> stickers;
|
2019-05-21 17:48:35 +02:00
|
|
|
std::vector<tl_object_ptr<td_api::emojis>> emojis;
|
2022-08-07 18:47:50 +02:00
|
|
|
for (auto sticker_id : sticker_set->sticker_ids_) {
|
2018-12-31 20:04:05 +01:00
|
|
|
stickers.push_back(get_sticker_object(sticker_id));
|
|
|
|
|
2021-09-15 23:41:40 +02:00
|
|
|
vector<string> sticker_emojis;
|
2018-12-31 20:04:05 +01:00
|
|
|
auto it = sticker_set->sticker_emojis_map_.find(sticker_id);
|
2021-09-15 23:41:40 +02:00
|
|
|
if (it != sticker_set->sticker_emojis_map_.end()) {
|
|
|
|
sticker_emojis = it->second;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2021-09-15 23:41:40 +02:00
|
|
|
emojis.push_back(make_tl_object<td_api::emojis>(std::move(sticker_emojis)));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2019-07-15 02:43:05 +02:00
|
|
|
return make_tl_object<td_api::stickerSet>(
|
2022-08-07 18:47:50 +02:00
|
|
|
sticker_set->id_.get(), sticker_set->title_, sticker_set->short_name_,
|
|
|
|
get_sticker_set_thumbnail_object(sticker_set),
|
|
|
|
get_sticker_minithumbnail(sticker_set->minithumbnail_, sticker_set->id_, -2,
|
2022-02-11 16:07:56 +01:00
|
|
|
get_sticker_set_minithumbnail_zoom(sticker_set)),
|
2022-08-07 18:47:50 +02:00
|
|
|
sticker_set->is_installed_ && !sticker_set->is_archived_, sticker_set->is_archived_, sticker_set->is_official_,
|
|
|
|
get_sticker_format_object(sticker_set->sticker_format_), get_sticker_type_object(sticker_set->sticker_type_),
|
|
|
|
sticker_set->is_viewed_, std::move(stickers), std::move(emojis));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
tl_object_ptr<td_api::stickerSets> StickersManager::get_sticker_sets_object(int32 total_count,
|
2019-09-18 05:55:43 +02:00
|
|
|
const vector<StickerSetId> &sticker_set_ids,
|
2018-09-20 21:25:09 +02:00
|
|
|
size_t covers_limit) const {
|
2018-12-31 20:04:05 +01:00
|
|
|
vector<tl_object_ptr<td_api::stickerSetInfo>> result;
|
|
|
|
result.reserve(sticker_set_ids.size());
|
|
|
|
for (auto sticker_set_id : sticker_set_ids) {
|
2022-06-17 07:59:54 +02:00
|
|
|
auto sticker_set_info = get_sticker_set_info_object(sticker_set_id, covers_limit, false);
|
2018-01-16 13:42:51 +01:00
|
|
|
if (sticker_set_info->size_ != 0) {
|
|
|
|
result.push_back(std::move(sticker_set_info));
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2018-01-16 13:42:51 +01:00
|
|
|
auto result_size = narrow_cast<int32>(result.size());
|
|
|
|
if (total_count < result_size) {
|
|
|
|
if (total_count != -1) {
|
|
|
|
LOG(ERROR) << "Have total_count = " << total_count << ", but there are " << result_size << " results";
|
|
|
|
}
|
|
|
|
total_count = result_size;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
return make_tl_object<td_api::stickerSets>(total_count, std::move(result));
|
|
|
|
}
|
|
|
|
|
2019-09-18 05:55:43 +02:00
|
|
|
tl_object_ptr<td_api::stickerSetInfo> StickersManager::get_sticker_set_info_object(StickerSetId sticker_set_id,
|
2022-06-17 07:59:54 +02:00
|
|
|
size_t covers_limit,
|
|
|
|
bool prefer_premium) const {
|
2018-12-31 20:04:05 +01:00
|
|
|
const StickerSet *sticker_set = get_sticker_set(sticker_set_id);
|
|
|
|
CHECK(sticker_set != nullptr);
|
2022-08-07 18:47:50 +02:00
|
|
|
CHECK(sticker_set->is_inited_);
|
|
|
|
sticker_set->was_update_sent_ = true;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2022-06-17 07:59:54 +02:00
|
|
|
vector<td_api::object_ptr<td_api::sticker>> stickers;
|
|
|
|
if (prefer_premium) {
|
2022-08-08 20:13:22 +02:00
|
|
|
CHECK(!td_->auth_manager_->is_bot());
|
2022-06-17 07:59:54 +02:00
|
|
|
vector<FileId> regular_sticker_ids;
|
|
|
|
vector<FileId> premium_sticker_ids;
|
2022-08-07 20:10:15 +02:00
|
|
|
std::tie(regular_sticker_ids, premium_sticker_ids) = split_stickers_by_premium(sticker_set);
|
2022-08-18 16:00:18 +02:00
|
|
|
auto is_premium = td_->option_manager_->get_option_boolean("is_premium");
|
2022-06-18 10:16:32 +02:00
|
|
|
size_t max_premium_stickers = is_premium ? covers_limit : 1;
|
2022-06-17 07:59:54 +02:00
|
|
|
if (premium_sticker_ids.size() > max_premium_stickers) {
|
|
|
|
premium_sticker_ids.resize(max_premium_stickers);
|
|
|
|
}
|
2022-06-18 10:16:32 +02:00
|
|
|
CHECK(premium_sticker_ids.size() <= covers_limit);
|
|
|
|
if (regular_sticker_ids.size() > covers_limit - premium_sticker_ids.size()) {
|
|
|
|
regular_sticker_ids.resize(covers_limit - premium_sticker_ids.size());
|
|
|
|
}
|
|
|
|
if (!is_premium) {
|
|
|
|
std::swap(premium_sticker_ids, regular_sticker_ids);
|
|
|
|
}
|
2022-06-17 07:59:54 +02:00
|
|
|
|
|
|
|
append(premium_sticker_ids, regular_sticker_ids);
|
|
|
|
for (auto sticker_id : premium_sticker_ids) {
|
|
|
|
stickers.push_back(get_sticker_object(sticker_id));
|
|
|
|
if (stickers.size() >= covers_limit) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2022-08-07 18:47:50 +02:00
|
|
|
for (auto sticker_id : sticker_set->sticker_ids_) {
|
2022-06-17 07:59:54 +02:00
|
|
|
stickers.push_back(get_sticker_object(sticker_id));
|
|
|
|
if (stickers.size() >= covers_limit) {
|
|
|
|
break;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-07 18:47:50 +02:00
|
|
|
auto actual_count = narrow_cast<int32>(sticker_set->sticker_ids_.size());
|
2018-12-31 20:04:05 +01:00
|
|
|
return make_tl_object<td_api::stickerSetInfo>(
|
2022-08-07 18:47:50 +02:00
|
|
|
sticker_set->id_.get(), sticker_set->title_, sticker_set->short_name_,
|
|
|
|
get_sticker_set_thumbnail_object(sticker_set),
|
|
|
|
get_sticker_minithumbnail(sticker_set->minithumbnail_, sticker_set->id_, -3,
|
2022-02-11 16:07:56 +01:00
|
|
|
get_sticker_set_minithumbnail_zoom(sticker_set)),
|
2022-08-07 18:47:50 +02:00
|
|
|
sticker_set->is_installed_ && !sticker_set->is_archived_, sticker_set->is_archived_, sticker_set->is_official_,
|
|
|
|
get_sticker_format_object(sticker_set->sticker_format_), get_sticker_type_object(sticker_set->sticker_type_),
|
|
|
|
sticker_set->is_viewed_, sticker_set->was_loaded_ ? actual_count : max(actual_count, sticker_set->sticker_count_),
|
2022-07-14 15:17:19 +02:00
|
|
|
std::move(stickers));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2022-07-22 14:01:44 +02:00
|
|
|
td_api::object_ptr<td_api::sticker> StickersManager::get_premium_gift_sticker_object(int32 month_count) {
|
|
|
|
auto it = premium_gift_messages_.find(month_count);
|
|
|
|
if (it == premium_gift_messages_.end()) {
|
|
|
|
return get_sticker_object(get_premium_gift_option_sticker_id(month_count));
|
|
|
|
} else {
|
2022-08-07 18:47:50 +02:00
|
|
|
return get_sticker_object(it->second->sticker_id_);
|
2022-07-22 14:01:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const StickersManager::StickerSet *StickersManager::get_premium_gift_sticker_set() {
|
|
|
|
if (td_->auth_manager_->is_bot()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
auto &special_sticker_set = add_special_sticker_set(SpecialStickerSetType::premium_gifts());
|
|
|
|
if (!special_sticker_set.id_.is_valid()) {
|
|
|
|
load_special_sticker_set(special_sticker_set);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto sticker_set = get_sticker_set(special_sticker_set.id_);
|
|
|
|
CHECK(sticker_set != nullptr);
|
2022-08-07 18:47:50 +02:00
|
|
|
if (!sticker_set->was_loaded_) {
|
2022-07-22 14:01:44 +02:00
|
|
|
load_special_sticker_set(special_sticker_set);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return sticker_set;
|
|
|
|
}
|
|
|
|
|
2022-07-15 15:15:59 +02:00
|
|
|
FileId StickersManager::get_premium_gift_option_sticker_id(const StickerSet *sticker_set, int32 month_count) {
|
2022-08-07 18:47:50 +02:00
|
|
|
if (sticker_set == nullptr || sticker_set->sticker_ids_.empty() || month_count <= 0) {
|
2022-07-15 15:15:59 +02:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
int32 number = [month_count] {
|
|
|
|
switch (month_count) {
|
|
|
|
case 1:
|
|
|
|
return 1;
|
|
|
|
case 3:
|
|
|
|
return 2;
|
|
|
|
case 6:
|
|
|
|
return 3;
|
|
|
|
case 12:
|
|
|
|
return 4;
|
|
|
|
case 24:
|
|
|
|
return 5;
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}();
|
|
|
|
|
2022-08-07 18:47:50 +02:00
|
|
|
for (auto sticker_id : sticker_set->sticker_ids_) {
|
2022-07-15 15:15:59 +02:00
|
|
|
auto it = sticker_set->sticker_emojis_map_.find(sticker_id);
|
|
|
|
if (it != sticker_set->sticker_emojis_map_.end()) {
|
|
|
|
for (auto &emoji : it->second) {
|
|
|
|
if (get_emoji_number(emoji) == number) {
|
|
|
|
return sticker_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// there is no match; return the first sticker
|
2022-08-07 18:47:50 +02:00
|
|
|
return sticker_set->sticker_ids_[0];
|
2022-07-15 15:15:59 +02:00
|
|
|
}
|
|
|
|
|
2022-07-22 14:01:44 +02:00
|
|
|
FileId StickersManager::get_premium_gift_option_sticker_id(int32 month_count) {
|
|
|
|
return get_premium_gift_option_sticker_id(get_premium_gift_sticker_set(), month_count);
|
|
|
|
}
|
|
|
|
|
2021-10-05 17:06:05 +02:00
|
|
|
const StickersManager::StickerSet *StickersManager::get_animated_emoji_sticker_set() {
|
|
|
|
if (td_->auth_manager_->is_bot() || disable_animated_emojis_) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
auto &special_sticker_set = add_special_sticker_set(SpecialStickerSetType::animated_emoji());
|
|
|
|
if (!special_sticker_set.id_.is_valid()) {
|
|
|
|
load_special_sticker_set(special_sticker_set);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto sticker_set = get_sticker_set(special_sticker_set.id_);
|
|
|
|
CHECK(sticker_set != nullptr);
|
2022-08-07 18:47:50 +02:00
|
|
|
if (!sticker_set->was_loaded_) {
|
2021-10-05 17:06:05 +02:00
|
|
|
load_special_sticker_set(special_sticker_set);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return sticker_set;
|
|
|
|
}
|
|
|
|
|
2021-10-05 15:12:04 +02:00
|
|
|
std::pair<FileId, int> StickersManager::get_animated_emoji_sticker(const StickerSet *sticker_set, const string &emoji) {
|
2021-10-05 17:06:05 +02:00
|
|
|
if (sticker_set == nullptr) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-05-12 17:02:27 +02:00
|
|
|
auto emoji_without_modifiers = remove_emoji_modifiers(emoji);
|
2021-10-05 15:12:04 +02:00
|
|
|
auto it = sticker_set->emoji_stickers_map_.find(emoji_without_modifiers);
|
|
|
|
if (it == sticker_set->emoji_stickers_map_.end()) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2021-12-22 11:40:20 +01:00
|
|
|
auto emoji_without_selectors = remove_emoji_selectors(emoji);
|
2021-10-05 15:12:04 +02:00
|
|
|
// trying to find full emoji match
|
|
|
|
for (const auto &sticker_id : it->second) {
|
|
|
|
auto emoji_it = sticker_set->sticker_emojis_map_.find(sticker_id);
|
|
|
|
CHECK(emoji_it != sticker_set->sticker_emojis_map_.end());
|
2021-12-22 11:40:20 +01:00
|
|
|
for (auto &sticker_emoji : emoji_it->second) {
|
|
|
|
if (remove_emoji_selectors(sticker_emoji) == emoji_without_selectors) {
|
|
|
|
return {sticker_id, 0};
|
|
|
|
}
|
2021-10-05 15:12:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// trying to find match without Fitzpatrick modifiers
|
2021-12-22 11:40:20 +01:00
|
|
|
int modifier_id = get_fitzpatrick_modifier(emoji_without_selectors);
|
2021-10-05 15:12:04 +02:00
|
|
|
if (modifier_id > 0) {
|
|
|
|
for (const auto &sticker_id : it->second) {
|
|
|
|
auto emoji_it = sticker_set->sticker_emojis_map_.find(sticker_id);
|
|
|
|
CHECK(emoji_it != sticker_set->sticker_emojis_map_.end());
|
2021-12-22 11:40:20 +01:00
|
|
|
for (auto &sticker_emoji : emoji_it->second) {
|
|
|
|
if (remove_emoji_selectors(sticker_emoji) == Slice(emoji_without_selectors).remove_suffix(4)) {
|
|
|
|
return {sticker_id, modifier_id};
|
|
|
|
}
|
2021-10-05 15:12:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// there is no match
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<FileId, int> StickersManager::get_animated_emoji_sticker(const string &emoji) {
|
2021-10-05 17:06:05 +02:00
|
|
|
return get_animated_emoji_sticker(get_animated_emoji_sticker_set(), emoji);
|
2021-10-05 15:12:04 +02:00
|
|
|
}
|
|
|
|
|
2021-10-05 22:20:49 +02:00
|
|
|
FileId StickersManager::get_animated_emoji_sound_file_id(const string &emoji) const {
|
|
|
|
auto it = emoji_sounds_.find(remove_fitzpatrick_modifier(emoji).str());
|
|
|
|
if (it == emoji_sounds_.end()) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
2022-10-03 00:26:32 +02:00
|
|
|
FileId StickersManager::get_custom_animated_emoji_sticker_id(CustomEmojiId custom_emoji_id) const {
|
2022-08-05 20:56:05 +02:00
|
|
|
if (disable_animated_emojis_) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
return custom_emoji_to_sticker_id_.get(custom_emoji_id);
|
|
|
|
}
|
|
|
|
|
2022-08-05 16:58:16 +02:00
|
|
|
td_api::object_ptr<td_api::animatedEmoji> StickersManager::get_animated_emoji_object(const string &emoji,
|
2022-10-03 00:26:32 +02:00
|
|
|
CustomEmojiId custom_emoji_id) {
|
2022-08-22 08:20:37 +02:00
|
|
|
if (td_->auth_manager_->is_bot() || disable_animated_emojis_) {
|
2022-08-05 20:56:05 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2022-10-03 00:26:32 +02:00
|
|
|
if (custom_emoji_id.is_valid()) {
|
2022-08-05 19:49:24 +02:00
|
|
|
auto it = custom_emoji_messages_.find(custom_emoji_id);
|
2022-08-05 20:56:05 +02:00
|
|
|
auto sticker_id = it == custom_emoji_messages_.end() ? get_custom_animated_emoji_sticker_id(custom_emoji_id)
|
2022-08-07 18:47:50 +02:00
|
|
|
: it->second->sticker_id_;
|
2022-09-25 22:40:42 +02:00
|
|
|
auto sticker = get_sticker_object(sticker_id, true);
|
|
|
|
auto default_custom_emoji_dimension = static_cast<int32>(512 * animated_emoji_zoom_ + 0.5);
|
|
|
|
auto sticker_width = sticker == nullptr ? default_custom_emoji_dimension : sticker->width_;
|
|
|
|
auto sticker_height = sticker == nullptr ? default_custom_emoji_dimension : sticker->height_;
|
|
|
|
return td_api::make_object<td_api::animatedEmoji>(std::move(sticker), sticker_width, sticker_height, 0, nullptr);
|
2022-08-05 16:58:16 +02:00
|
|
|
}
|
|
|
|
|
2021-10-05 17:06:05 +02:00
|
|
|
auto it = emoji_messages_.find(emoji);
|
2021-10-26 15:50:18 +02:00
|
|
|
if (it == emoji_messages_.end()) {
|
|
|
|
return get_animated_emoji_object(get_animated_emoji_sticker(emoji), get_animated_emoji_sound_file_id(emoji));
|
|
|
|
} else {
|
2022-08-07 18:47:50 +02:00
|
|
|
return get_animated_emoji_object(it->second->animated_emoji_sticker_, it->second->sound_file_id_);
|
2021-10-26 15:50:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
td_api::object_ptr<td_api::animatedEmoji> StickersManager::get_animated_emoji_object(
|
|
|
|
std::pair<FileId, int> animated_sticker, FileId sound_file_id) const {
|
2021-10-26 15:15:01 +02:00
|
|
|
if (!animated_sticker.first.is_valid()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2022-09-25 22:40:42 +02:00
|
|
|
auto sticker = get_sticker_object(animated_sticker.first, true);
|
|
|
|
CHECK(sticker != nullptr);
|
|
|
|
auto sticker_width = sticker->width_;
|
|
|
|
auto sticker_height = sticker->height_;
|
2021-10-26 15:15:01 +02:00
|
|
|
return td_api::make_object<td_api::animatedEmoji>(
|
2022-09-25 22:40:42 +02:00
|
|
|
std::move(sticker), sticker_width, sticker_height, animated_sticker.second,
|
2021-10-26 15:15:01 +02:00
|
|
|
sound_file_id.is_valid() ? td_->file_manager_->get_file_object(sound_file_id) : nullptr);
|
2021-10-04 15:08:51 +02:00
|
|
|
}
|
|
|
|
|
2019-09-18 05:55:43 +02:00
|
|
|
tl_object_ptr<telegram_api::InputStickerSet> StickersManager::get_input_sticker_set(StickerSetId sticker_set_id) const {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto sticker_set = get_sticker_set(sticker_set_id);
|
|
|
|
if (sticker_set == nullptr) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return get_input_sticker_set(sticker_set);
|
|
|
|
}
|
|
|
|
|
2022-07-25 20:18:43 +02:00
|
|
|
class StickersManager::CustomEmojiLogEvent {
|
|
|
|
public:
|
|
|
|
FileId sticker_id;
|
|
|
|
|
|
|
|
CustomEmojiLogEvent() = default;
|
|
|
|
|
|
|
|
explicit CustomEmojiLogEvent(FileId sticker_id) : sticker_id(sticker_id) {
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class StorerT>
|
|
|
|
void store(StorerT &storer) const {
|
|
|
|
BEGIN_STORE_FLAGS();
|
|
|
|
END_STORE_FLAGS();
|
|
|
|
StickersManager *stickers_manager = storer.context()->td().get_actor_unsafe()->stickers_manager_.get();
|
|
|
|
stickers_manager->store_sticker(sticker_id, false, storer, "CustomEmoji");
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ParserT>
|
|
|
|
void parse(ParserT &parser) {
|
|
|
|
BEGIN_PARSE_FLAGS();
|
|
|
|
END_PARSE_FLAGS();
|
|
|
|
StickersManager *stickers_manager = parser.context()->td().get_actor_unsafe()->stickers_manager_.get();
|
|
|
|
sticker_id = stickers_manager->parse_sticker(false, parser);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-10-03 00:26:32 +02:00
|
|
|
string StickersManager::get_custom_emoji_database_key(CustomEmojiId custom_emoji_id) {
|
|
|
|
return PSTRING() << "emoji" << custom_emoji_id.get();
|
2022-07-25 20:18:43 +02:00
|
|
|
}
|
|
|
|
|
2018-09-27 03:19:03 +02:00
|
|
|
FileId StickersManager::on_get_sticker(unique_ptr<Sticker> new_sticker, bool replace) {
|
2022-08-07 18:47:50 +02:00
|
|
|
auto file_id = new_sticker->file_id_;
|
2019-08-01 02:58:49 +02:00
|
|
|
CHECK(file_id.is_valid());
|
2022-10-03 00:26:32 +02:00
|
|
|
CustomEmojiId updated_custom_emoji_id;
|
2022-08-03 21:42:52 +02:00
|
|
|
auto *s = get_sticker(file_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (s == nullptr) {
|
2022-08-03 21:42:52 +02:00
|
|
|
s = new_sticker.get();
|
|
|
|
stickers_.set(file_id, std::move(new_sticker));
|
2018-12-31 20:04:05 +01:00
|
|
|
} else if (replace) {
|
2022-08-07 18:47:50 +02:00
|
|
|
CHECK(s->file_id_ == file_id);
|
2022-07-22 18:06:39 +02:00
|
|
|
|
2022-08-07 18:47:50 +02:00
|
|
|
if (s->type_ == StickerType::CustomEmoji) {
|
2022-07-22 18:06:39 +02:00
|
|
|
auto custom_emoji_id = get_custom_emoji_id(file_id);
|
2022-10-03 00:26:32 +02:00
|
|
|
if (custom_emoji_id.is_valid() && custom_emoji_to_sticker_id_.get(custom_emoji_id) == file_id) {
|
2022-08-04 13:48:10 +02:00
|
|
|
custom_emoji_to_sticker_id_.erase(custom_emoji_id);
|
2022-08-05 19:49:24 +02:00
|
|
|
updated_custom_emoji_id = custom_emoji_id;
|
2022-07-22 18:06:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-25 20:18:43 +02:00
|
|
|
bool is_changed = false;
|
2022-08-07 18:47:50 +02:00
|
|
|
if (s->dimensions_ != new_sticker->dimensions_ && new_sticker->dimensions_.width != 0) {
|
2021-12-02 12:15:37 +01:00
|
|
|
LOG(DEBUG) << "Sticker " << file_id << " dimensions have changed";
|
2022-08-07 18:47:50 +02:00
|
|
|
s->dimensions_ = new_sticker->dimensions_;
|
2022-07-25 20:18:43 +02:00
|
|
|
is_changed = true;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2022-08-07 18:47:50 +02:00
|
|
|
if (s->set_id_ != new_sticker->set_id_ && new_sticker->set_id_.is_valid()) {
|
|
|
|
LOG_IF(ERROR, s->set_id_.is_valid()) << "Sticker " << file_id << " set_id has changed";
|
|
|
|
s->set_id_ = new_sticker->set_id_;
|
2022-07-25 20:18:43 +02:00
|
|
|
is_changed = true;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2022-08-07 18:47:50 +02:00
|
|
|
if (s->alt_ != new_sticker->alt_ && !new_sticker->alt_.empty()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
LOG(DEBUG) << "Sticker " << file_id << " emoji has changed";
|
2022-08-07 18:47:50 +02:00
|
|
|
s->alt_ = std::move(new_sticker->alt_);
|
2022-07-25 20:18:43 +02:00
|
|
|
is_changed = true;
|
2020-11-09 21:41:23 +01:00
|
|
|
}
|
2022-08-07 18:47:50 +02:00
|
|
|
if (s->minithumbnail_ != new_sticker->minithumbnail_) {
|
2020-11-09 21:41:23 +01:00
|
|
|
LOG(DEBUG) << "Sticker " << file_id << " minithumbnail has changed";
|
2022-08-07 18:47:50 +02:00
|
|
|
s->minithumbnail_ = std::move(new_sticker->minithumbnail_);
|
2022-07-25 20:18:43 +02:00
|
|
|
is_changed = true;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2022-08-07 18:47:50 +02:00
|
|
|
if (s->s_thumbnail_ != new_sticker->s_thumbnail_ && new_sticker->s_thumbnail_.file_id.is_valid()) {
|
|
|
|
LOG_IF(INFO, s->s_thumbnail_.file_id.is_valid()) << "Sticker " << file_id << " s thumbnail has changed from "
|
|
|
|
<< s->s_thumbnail_ << " to " << new_sticker->s_thumbnail_;
|
|
|
|
s->s_thumbnail_ = std::move(new_sticker->s_thumbnail_);
|
2022-07-25 20:18:43 +02:00
|
|
|
is_changed = true;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2022-08-07 18:47:50 +02:00
|
|
|
if (s->m_thumbnail_ != new_sticker->m_thumbnail_ && new_sticker->m_thumbnail_.file_id.is_valid()) {
|
|
|
|
LOG_IF(INFO, s->m_thumbnail_.file_id.is_valid()) << "Sticker " << file_id << " m thumbnail has changed from "
|
|
|
|
<< s->m_thumbnail_ << " to " << new_sticker->m_thumbnail_;
|
|
|
|
s->m_thumbnail_ = std::move(new_sticker->m_thumbnail_);
|
2022-07-25 20:18:43 +02:00
|
|
|
is_changed = true;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2022-08-07 18:47:50 +02:00
|
|
|
s->is_premium_ = new_sticker->is_premium_;
|
|
|
|
s->premium_animation_file_id_ = new_sticker->premium_animation_file_id_;
|
|
|
|
if (s->format_ != new_sticker->format_ && new_sticker->format_ != StickerFormat::Unknown) {
|
|
|
|
s->format_ = new_sticker->format_;
|
2022-07-25 20:18:43 +02:00
|
|
|
is_changed = true;
|
2019-07-15 02:43:05 +02:00
|
|
|
}
|
2022-08-07 18:47:50 +02:00
|
|
|
if (s->type_ != new_sticker->type_ && new_sticker->type_ != StickerType::Regular) {
|
|
|
|
s->type_ = new_sticker->type_;
|
2022-07-25 20:18:43 +02:00
|
|
|
is_changed = true;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2022-08-07 18:47:50 +02:00
|
|
|
if (s->point_ != new_sticker->point_ && new_sticker->point_ != -1) {
|
|
|
|
s->point_ = new_sticker->point_;
|
|
|
|
s->x_shift_ = new_sticker->x_shift_;
|
|
|
|
s->y_shift_ = new_sticker->y_shift_;
|
|
|
|
s->scale_ = new_sticker->scale_;
|
2022-07-25 20:18:43 +02:00
|
|
|
is_changed = true;
|
|
|
|
}
|
2022-08-07 18:47:50 +02:00
|
|
|
if (s->emoji_receive_date_ < new_sticker->emoji_receive_date_) {
|
2022-07-25 22:04:53 +02:00
|
|
|
LOG(DEBUG) << "Update custom emoji file " << file_id << " receive date";
|
2022-08-07 18:47:50 +02:00
|
|
|
s->emoji_receive_date_ = new_sticker->emoji_receive_date_;
|
2022-07-25 20:18:43 +02:00
|
|
|
is_changed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_changed) {
|
2022-08-07 18:47:50 +02:00
|
|
|
s->is_from_database_ = false;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-07 18:47:50 +02:00
|
|
|
if (s->type_ == StickerType::CustomEmoji) {
|
|
|
|
s->is_being_reloaded_ = false;
|
2022-07-22 18:06:39 +02:00
|
|
|
auto custom_emoji_id = get_custom_emoji_id(file_id);
|
2022-10-03 00:26:32 +02:00
|
|
|
if (custom_emoji_id.is_valid()) {
|
2022-08-05 19:49:24 +02:00
|
|
|
custom_emoji_to_sticker_id_.set(custom_emoji_id, file_id);
|
2022-10-03 00:26:32 +02:00
|
|
|
CHECK(updated_custom_emoji_id == custom_emoji_id || !updated_custom_emoji_id.is_valid());
|
2022-08-05 19:49:24 +02:00
|
|
|
updated_custom_emoji_id = custom_emoji_id;
|
2022-08-07 18:47:50 +02:00
|
|
|
if (!s->is_from_database_ && G()->parameters().use_file_db && !G()->close_flag()) {
|
2022-10-03 00:26:32 +02:00
|
|
|
LOG(INFO) << "Save " << custom_emoji_id << " to database";
|
2022-08-07 18:47:50 +02:00
|
|
|
s->is_from_database_ = true;
|
2022-07-25 20:18:43 +02:00
|
|
|
|
|
|
|
CustomEmojiLogEvent log_event(file_id);
|
|
|
|
G()->td_db()->get_sqlite_pmc()->set(get_custom_emoji_database_key(custom_emoji_id),
|
|
|
|
log_event_store(log_event).as_slice().str(), Auto());
|
|
|
|
}
|
2022-07-22 18:06:39 +02:00
|
|
|
}
|
|
|
|
}
|
2022-10-03 00:26:32 +02:00
|
|
|
if (updated_custom_emoji_id.is_valid()) {
|
2022-08-05 19:49:24 +02:00
|
|
|
try_update_custom_emoji_messages(updated_custom_emoji_id);
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
return file_id;
|
|
|
|
}
|
|
|
|
|
2021-03-01 21:48:48 +01:00
|
|
|
bool StickersManager::has_webp_thumbnail(const vector<tl_object_ptr<telegram_api::PhotoSize>> &thumbnails) {
|
|
|
|
// server tries to always replace user-provided thumbnail with server-side WEBP thumbnail
|
|
|
|
// but there can be some old sticker documents or some big stickers
|
|
|
|
for (auto &size : thumbnails) {
|
|
|
|
switch (size->get_id()) {
|
|
|
|
case telegram_api::photoStrippedSize::ID:
|
|
|
|
case telegram_api::photoSizeProgressive::ID:
|
|
|
|
// WEBP thumbnail can't have stripped size or be progressive
|
|
|
|
return false;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-07-15 03:26:41 +02:00
|
|
|
return true;
|
2018-06-01 00:23:07 +02:00
|
|
|
}
|
|
|
|
|
2022-01-18 17:28:32 +01:00
|
|
|
std::pair<int64, FileId> StickersManager::on_get_sticker_document(tl_object_ptr<telegram_api::Document> &&document_ptr,
|
|
|
|
StickerFormat expected_format) {
|
2022-01-19 12:14:21 +01:00
|
|
|
if (document_ptr == nullptr) {
|
|
|
|
return {};
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
int32 document_constructor_id = document_ptr->get_id();
|
|
|
|
if (document_constructor_id == telegram_api::documentEmpty::ID) {
|
|
|
|
LOG(ERROR) << "Empty sticker document received";
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
CHECK(document_constructor_id == telegram_api::document::ID);
|
|
|
|
auto document = move_tl_object_as<telegram_api::document>(document_ptr);
|
|
|
|
|
|
|
|
if (!DcId::is_valid(document->dc_id_)) {
|
|
|
|
LOG(ERROR) << "Wrong dc_id = " << document->dc_id_ << " in document " << to_string(document);
|
|
|
|
return {};
|
|
|
|
}
|
2019-06-08 11:26:35 +02:00
|
|
|
auto dc_id = DcId::internal(document->dc_id_);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
Dimensions dimensions;
|
|
|
|
tl_object_ptr<telegram_api::documentAttributeSticker> sticker;
|
2022-07-14 16:11:36 +02:00
|
|
|
tl_object_ptr<telegram_api::documentAttributeCustomEmoji> custom_emoji;
|
2018-12-31 20:04:05 +01:00
|
|
|
for (auto &attribute : document->attributes_) {
|
|
|
|
switch (attribute->get_id()) {
|
2022-01-25 12:58:04 +01:00
|
|
|
case telegram_api::documentAttributeVideo::ID: {
|
|
|
|
auto video = move_tl_object_as<telegram_api::documentAttributeVideo>(attribute);
|
|
|
|
dimensions = get_dimensions(video->w_, video->h_, "sticker documentAttributeVideo");
|
|
|
|
break;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
case telegram_api::documentAttributeImageSize::ID: {
|
|
|
|
auto image_size = move_tl_object_as<telegram_api::documentAttributeImageSize>(attribute);
|
2021-01-23 20:04:35 +01:00
|
|
|
dimensions = get_dimensions(image_size->w_, image_size->h_, "sticker documentAttributeImageSize");
|
2018-12-31 20:04:05 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case telegram_api::documentAttributeSticker::ID:
|
|
|
|
sticker = move_tl_object_as<telegram_api::documentAttributeSticker>(attribute);
|
|
|
|
break;
|
2022-07-14 16:11:36 +02:00
|
|
|
case telegram_api::documentAttributeCustomEmoji::ID:
|
|
|
|
custom_emoji = move_tl_object_as<telegram_api::documentAttributeCustomEmoji>(attribute);
|
|
|
|
break;
|
2018-12-31 20:04:05 +01:00
|
|
|
default:
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2022-07-14 16:11:36 +02:00
|
|
|
if (sticker == nullptr && custom_emoji == nullptr) {
|
2020-05-01 11:19:52 +02:00
|
|
|
if (document->mime_type_ != "application/x-bad-tgsticker") {
|
|
|
|
LOG(ERROR) << "Have no attributeSticker in sticker " << to_string(document);
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-02-17 18:45:58 +01:00
|
|
|
auto format = get_sticker_format_by_mime_type(document->mime_type_);
|
2022-01-26 15:30:57 +01:00
|
|
|
if (format == StickerFormat::Unknown || (expected_format != StickerFormat::Unknown && format != expected_format)) {
|
2022-01-18 17:28:32 +01:00
|
|
|
LOG(ERROR) << "Expected sticker of the type " << expected_format << ", but received of the type " << format;
|
|
|
|
return {};
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
int64 document_id = document->id_;
|
2019-07-18 18:47:29 +02:00
|
|
|
FileId sticker_id =
|
|
|
|
td_->file_manager_->register_remote(FullRemoteFileLocation(FileType::Sticker, document_id, document->access_hash_,
|
|
|
|
dc_id, document->file_reference_.as_slice().str()),
|
|
|
|
FileLocationSource::FromServer, DialogId(), document->size_, 0,
|
2022-01-18 17:20:43 +01:00
|
|
|
PSTRING() << document_id << get_sticker_format_extension(format));
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2019-03-01 20:51:33 +01:00
|
|
|
PhotoSize thumbnail;
|
2020-11-09 21:41:23 +01:00
|
|
|
string minithumbnail;
|
2021-03-01 21:48:48 +01:00
|
|
|
auto thumbnail_format = has_webp_thumbnail(document->thumbs_) ? PhotoFormat::Webp : PhotoFormat::Jpeg;
|
2022-04-26 15:01:29 +02:00
|
|
|
FileId premium_animation_file_id;
|
2019-03-15 14:52:55 +01:00
|
|
|
for (auto &thumb : document->thumbs_) {
|
2021-10-27 15:52:22 +02:00
|
|
|
auto photo_size = get_photo_size(td_->file_manager_.get(), PhotoSizeSource::thumbnail(FileType::Thumbnail, 0),
|
|
|
|
document_id, document->access_hash_, document->file_reference_.as_slice().str(),
|
|
|
|
dc_id, DialogId(), std::move(thumb), thumbnail_format);
|
2019-03-15 14:52:55 +01:00
|
|
|
if (photo_size.get_offset() == 0) {
|
2020-11-09 21:41:23 +01:00
|
|
|
if (!thumbnail.file_id.is_valid()) {
|
|
|
|
thumbnail = std::move(photo_size.get<0>());
|
|
|
|
}
|
2019-03-15 14:52:55 +01:00
|
|
|
break;
|
|
|
|
} else {
|
2021-03-03 14:33:17 +01:00
|
|
|
if (thumbnail_format == PhotoFormat::Webp) {
|
|
|
|
minithumbnail = std::move(photo_size.get<1>());
|
|
|
|
}
|
2019-03-15 14:52:55 +01:00
|
|
|
}
|
2019-03-01 20:51:33 +01:00
|
|
|
}
|
2022-04-26 15:01:29 +02:00
|
|
|
for (auto &thumb : document->video_thumbs_) {
|
|
|
|
if (thumb->type_ == "f") {
|
|
|
|
if (!premium_animation_file_id.is_valid()) {
|
|
|
|
premium_animation_file_id =
|
|
|
|
register_photo_size(td_->file_manager_.get(), PhotoSizeSource::thumbnail(FileType::Thumbnail, 'f'),
|
|
|
|
document_id, document->access_hash_, document->file_reference_.as_slice().str(),
|
|
|
|
DialogId(), thumb->size_, dc_id, get_sticker_format_photo_format(format));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2022-04-26 15:01:29 +02:00
|
|
|
create_sticker(sticker_id, premium_animation_file_id, std::move(minithumbnail), std::move(thumbnail), dimensions,
|
2022-07-14 16:11:36 +02:00
|
|
|
std::move(sticker), std::move(custom_emoji), format, nullptr);
|
2018-12-31 20:04:05 +01:00
|
|
|
return {document_id, sticker_id};
|
|
|
|
}
|
|
|
|
|
|
|
|
StickersManager::Sticker *StickersManager::get_sticker(FileId file_id) {
|
2022-08-03 21:42:52 +02:00
|
|
|
return stickers_.get_pointer(file_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const StickersManager::Sticker *StickersManager::get_sticker(FileId file_id) const {
|
2022-08-03 21:42:52 +02:00
|
|
|
return stickers_.get_pointer(file_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2019-09-18 05:55:43 +02:00
|
|
|
StickersManager::StickerSet *StickersManager::get_sticker_set(StickerSetId sticker_set_id) {
|
2022-08-03 21:42:52 +02:00
|
|
|
return sticker_sets_.get_pointer(sticker_set_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2019-09-18 05:55:43 +02:00
|
|
|
const StickersManager::StickerSet *StickersManager::get_sticker_set(StickerSetId sticker_set_id) const {
|
2022-08-03 21:42:52 +02:00
|
|
|
return sticker_sets_.get_pointer(sticker_set_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2019-09-18 05:55:43 +02:00
|
|
|
StickerSetId StickersManager::get_sticker_set_id(const tl_object_ptr<telegram_api::InputStickerSet> &set_ptr) {
|
2018-06-01 00:23:07 +02:00
|
|
|
CHECK(set_ptr != nullptr);
|
|
|
|
switch (set_ptr->get_id()) {
|
|
|
|
case telegram_api::inputStickerSetEmpty::ID:
|
2019-09-18 05:55:43 +02:00
|
|
|
return StickerSetId();
|
2018-06-01 00:23:07 +02:00
|
|
|
case telegram_api::inputStickerSetID::ID:
|
2019-09-18 05:55:43 +02:00
|
|
|
return StickerSetId(static_cast<const telegram_api::inputStickerSetID *>(set_ptr.get())->id_);
|
2018-06-01 00:23:07 +02:00
|
|
|
case telegram_api::inputStickerSetShortName::ID:
|
|
|
|
LOG(ERROR) << "Receive sticker set by its short name";
|
|
|
|
return search_sticker_set(static_cast<const telegram_api::inputStickerSetShortName *>(set_ptr.get())->short_name_,
|
|
|
|
Auto());
|
2020-03-14 01:47:19 +01:00
|
|
|
case telegram_api::inputStickerSetAnimatedEmoji::ID:
|
2021-09-15 13:44:11 +02:00
|
|
|
case telegram_api::inputStickerSetAnimatedEmojiAnimations::ID:
|
2022-07-15 14:24:08 +02:00
|
|
|
case telegram_api::inputStickerSetPremiumGifts::ID:
|
2022-09-02 12:59:49 +02:00
|
|
|
case telegram_api::inputStickerSetEmojiGenericAnimations::ID:
|
|
|
|
case telegram_api::inputStickerSetEmojiDefaultStatuses::ID:
|
2022-10-07 17:04:00 +02:00
|
|
|
case telegram_api::inputStickerSetEmojiDefaultTopicIcons::ID:
|
2020-04-19 15:30:04 +02:00
|
|
|
LOG(ERROR) << "Receive special sticker set " << to_string(set_ptr);
|
2021-12-01 17:59:45 +01:00
|
|
|
return add_special_sticker_set(SpecialStickerSetType(set_ptr)).id_;
|
2020-05-17 00:06:23 +02:00
|
|
|
case telegram_api::inputStickerSetDice::ID:
|
|
|
|
LOG(ERROR) << "Receive special sticker set " << to_string(set_ptr);
|
|
|
|
return StickerSetId();
|
2018-06-01 00:23:07 +02:00
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
2019-09-18 05:55:43 +02:00
|
|
|
return StickerSetId();
|
2018-06-01 00:23:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-18 05:55:43 +02:00
|
|
|
StickerSetId StickersManager::add_sticker_set(tl_object_ptr<telegram_api::InputStickerSet> &&set_ptr) {
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK(set_ptr != nullptr);
|
|
|
|
switch (set_ptr->get_id()) {
|
|
|
|
case telegram_api::inputStickerSetEmpty::ID:
|
2019-09-18 05:55:43 +02:00
|
|
|
return StickerSetId();
|
2018-12-31 20:04:05 +01:00
|
|
|
case telegram_api::inputStickerSetID::ID: {
|
|
|
|
auto set = move_tl_object_as<telegram_api::inputStickerSetID>(set_ptr);
|
2019-09-18 05:55:43 +02:00
|
|
|
StickerSetId set_id{set->id_};
|
2018-12-31 20:04:05 +01:00
|
|
|
add_sticker_set(set_id, set->access_hash_);
|
|
|
|
return set_id;
|
|
|
|
}
|
|
|
|
case telegram_api::inputStickerSetShortName::ID: {
|
|
|
|
auto set = move_tl_object_as<telegram_api::inputStickerSetShortName>(set_ptr);
|
|
|
|
LOG(ERROR) << "Receive sticker set by its short name";
|
|
|
|
return search_sticker_set(set->short_name_, Auto());
|
|
|
|
}
|
2020-03-14 01:47:19 +01:00
|
|
|
case telegram_api::inputStickerSetAnimatedEmoji::ID:
|
2021-09-15 13:44:11 +02:00
|
|
|
case telegram_api::inputStickerSetAnimatedEmojiAnimations::ID:
|
2022-07-15 14:24:08 +02:00
|
|
|
case telegram_api::inputStickerSetPremiumGifts::ID:
|
2022-09-02 12:59:49 +02:00
|
|
|
case telegram_api::inputStickerSetEmojiGenericAnimations::ID:
|
|
|
|
case telegram_api::inputStickerSetEmojiDefaultStatuses::ID:
|
2022-10-07 17:04:00 +02:00
|
|
|
case telegram_api::inputStickerSetEmojiDefaultTopicIcons::ID:
|
2020-04-19 15:30:04 +02:00
|
|
|
LOG(ERROR) << "Receive special sticker set " << to_string(set_ptr);
|
2021-12-01 17:59:45 +01:00
|
|
|
return add_special_sticker_set(SpecialStickerSetType(set_ptr)).id_;
|
2020-05-17 00:06:23 +02:00
|
|
|
case telegram_api::inputStickerSetDice::ID:
|
|
|
|
LOG(ERROR) << "Receive special sticker set " << to_string(set_ptr);
|
|
|
|
return StickerSetId();
|
2018-12-31 20:04:05 +01:00
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
2019-09-18 05:55:43 +02:00
|
|
|
return StickerSetId();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-18 05:55:43 +02:00
|
|
|
StickersManager::StickerSet *StickersManager::add_sticker_set(StickerSetId sticker_set_id, int64 access_hash) {
|
2022-02-10 16:03:03 +01:00
|
|
|
if (!sticker_set_id.is_valid()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2022-08-03 21:42:52 +02:00
|
|
|
auto *s = get_sticker_set(sticker_set_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (s == nullptr) {
|
2022-08-03 21:42:52 +02:00
|
|
|
auto sticker_set = make_unique<StickerSet>();
|
|
|
|
s = sticker_set.get();
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2022-08-07 18:47:50 +02:00
|
|
|
s->id_ = sticker_set_id;
|
|
|
|
s->access_hash_ = access_hash;
|
|
|
|
s->is_changed_ = false;
|
|
|
|
s->need_save_to_database_ = false;
|
2022-08-03 21:42:52 +02:00
|
|
|
|
|
|
|
sticker_sets_.set(sticker_set_id, std::move(sticker_set));
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
2022-08-07 18:47:50 +02:00
|
|
|
CHECK(s->id_ == sticker_set_id);
|
|
|
|
if (s->access_hash_ != access_hash) {
|
2020-06-04 00:19:37 +02:00
|
|
|
LOG(INFO) << "Access hash of " << sticker_set_id << " changed";
|
2022-08-07 18:47:50 +02:00
|
|
|
s->access_hash_ = access_hash;
|
|
|
|
s->need_save_to_database_ = true;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
2022-08-03 21:42:52 +02:00
|
|
|
return s;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
FileId StickersManager::get_sticker_thumbnail_file_id(FileId file_id) const {
|
2022-08-03 21:42:52 +02:00
|
|
|
auto *sticker = get_sticker(file_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK(sticker != nullptr);
|
2022-08-07 18:47:50 +02:00
|
|
|
return sticker->s_thumbnail_.file_id;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void StickersManager::delete_sticker_thumbnail(FileId file_id) {
|
2022-08-03 21:42:52 +02:00
|
|
|
auto *sticker = get_sticker(file_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK(sticker != nullptr);
|
2022-08-07 18:47:50 +02:00
|
|
|
sticker->s_thumbnail_ = PhotoSize();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2019-02-02 11:30:49 +01:00
|
|
|
vector<FileId> StickersManager::get_sticker_file_ids(FileId file_id) const {
|
|
|
|
vector<FileId> result;
|
|
|
|
auto sticker = get_sticker(file_id);
|
|
|
|
CHECK(sticker != nullptr);
|
|
|
|
result.push_back(file_id);
|
2022-08-07 18:47:50 +02:00
|
|
|
if (sticker->s_thumbnail_.file_id.is_valid()) {
|
|
|
|
result.push_back(sticker->s_thumbnail_.file_id);
|
2019-02-02 11:30:49 +01:00
|
|
|
}
|
2022-08-07 18:47:50 +02:00
|
|
|
if (sticker->m_thumbnail_.file_id.is_valid()) {
|
|
|
|
result.push_back(sticker->m_thumbnail_.file_id);
|
2019-02-02 11:30:49 +01:00
|
|
|
}
|
2022-08-07 18:47:50 +02:00
|
|
|
if (sticker->premium_animation_file_id_.is_valid()) {
|
|
|
|
result.push_back(sticker->premium_animation_file_id_);
|
2022-04-26 15:01:29 +02:00
|
|
|
}
|
2019-02-02 11:30:49 +01:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
FileId StickersManager::dup_sticker(FileId new_id, FileId old_id) {
|
|
|
|
const Sticker *old_sticker = get_sticker(old_id);
|
|
|
|
CHECK(old_sticker != nullptr);
|
2022-08-03 21:42:52 +02:00
|
|
|
|
|
|
|
CHECK(get_sticker(new_id) == nullptr);
|
|
|
|
auto new_sticker = make_unique<Sticker>(*old_sticker);
|
2022-08-07 18:47:50 +02:00
|
|
|
new_sticker->file_id_ = new_id;
|
2022-04-26 15:01:29 +02:00
|
|
|
// there is no reason to dup m_thumbnail and premium_animation_file_id
|
2022-08-07 18:47:50 +02:00
|
|
|
new_sticker->s_thumbnail_.file_id = td_->file_manager_->dup_file_id(new_sticker->s_thumbnail_.file_id);
|
2022-08-03 21:42:52 +02:00
|
|
|
stickers_.set(new_id, std::move(new_sticker));
|
2018-12-31 20:04:05 +01:00
|
|
|
return new_id;
|
|
|
|
}
|
|
|
|
|
2022-08-03 20:38:03 +02:00
|
|
|
void StickersManager::merge_stickers(FileId new_id, FileId old_id) {
|
2021-08-26 17:50:28 +02:00
|
|
|
CHECK(old_id.is_valid() && new_id.is_valid());
|
|
|
|
CHECK(new_id != old_id);
|
2018-01-05 16:50:35 +01:00
|
|
|
|
|
|
|
LOG(INFO) << "Merge stickers " << new_id << " and " << old_id;
|
2018-12-31 20:04:05 +01:00
|
|
|
const Sticker *old_ = get_sticker(old_id);
|
|
|
|
CHECK(old_ != nullptr);
|
|
|
|
|
2022-08-03 20:58:07 +02:00
|
|
|
const auto *new_ = get_sticker(new_id);
|
|
|
|
if (new_ == nullptr) {
|
2022-08-03 20:38:03 +02:00
|
|
|
dup_sticker(new_id, old_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
2022-08-07 18:47:50 +02:00
|
|
|
if (old_->set_id_ == new_->set_id_ &&
|
|
|
|
(old_->alt_ != new_->alt_ || old_->set_id_ != new_->set_id_ ||
|
|
|
|
(!is_sticker_format_vector(old_->format_) && !is_sticker_format_vector(new_->format_) &&
|
|
|
|
old_->dimensions_.width != 0 && old_->dimensions_.height != 0 && old_->dimensions_ != new_->dimensions_))) {
|
|
|
|
LOG(ERROR) << "Sticker has changed: alt = (" << old_->alt_ << ", " << new_->alt_ << "), set_id = ("
|
|
|
|
<< old_->set_id_ << ", " << new_->set_id_ << "), dimensions = (" << old_->dimensions_ << ", "
|
|
|
|
<< new_->dimensions_ << ")";
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2022-08-07 18:47:50 +02:00
|
|
|
if (old_->s_thumbnail_ != new_->s_thumbnail_) {
|
|
|
|
// LOG_STATUS(td_->file_manager_->merge(new_->s_thumbnail_.file_id, old_->s_thumbnail_.file_id));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2022-08-07 18:47:50 +02:00
|
|
|
if (old_->m_thumbnail_ != new_->m_thumbnail_) {
|
|
|
|
// LOG_STATUS(td_->file_manager_->merge(new_->m_thumbnail_.file_id, old_->m_thumbnail_.file_id));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
LOG_STATUS(td_->file_manager_->merge(new_id, old_id));
|
|
|
|
}
|
|
|
|
|
|
|
|
tl_object_ptr<telegram_api::InputStickerSet> StickersManager::get_input_sticker_set(const StickerSet *set) {
|
|
|
|
CHECK(set != nullptr);
|
2022-08-07 18:47:50 +02:00
|
|
|
return make_tl_object<telegram_api::inputStickerSetID>(set->id_.get(), set->access_hash_);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2022-07-14 15:17:19 +02:00
|
|
|
void StickersManager::reload_installed_sticker_sets(StickerType sticker_type, bool force) {
|
2019-04-29 23:37:55 +02:00
|
|
|
if (G()->close_flag()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-07-24 13:01:58 +02:00
|
|
|
auto type = static_cast<int32>(sticker_type);
|
|
|
|
auto &next_load_time = next_installed_sticker_sets_load_time_[type];
|
2020-11-23 18:33:35 +01:00
|
|
|
if (!td_->auth_manager_->is_bot() && next_load_time >= 0 && (next_load_time < Time::now() || force)) {
|
2018-12-31 20:04:05 +01:00
|
|
|
LOG_IF(INFO, force) << "Reload sticker sets";
|
2020-11-23 18:33:35 +01:00
|
|
|
next_load_time = -1;
|
2022-07-24 13:01:58 +02:00
|
|
|
td_->create_handler<GetAllStickersQuery>()->send(sticker_type, installed_sticker_sets_hash_[type]);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-17 21:39:22 +02:00
|
|
|
void StickersManager::reload_featured_sticker_sets(StickerType sticker_type, bool force) {
|
2019-04-29 23:37:55 +02:00
|
|
|
if (G()->close_flag()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-07-17 21:39:22 +02:00
|
|
|
auto type = static_cast<int32>(sticker_type);
|
|
|
|
auto &next_load_time = next_featured_sticker_sets_load_time_[type];
|
2020-11-23 18:33:35 +01:00
|
|
|
if (!td_->auth_manager_->is_bot() && next_load_time >= 0 && (next_load_time < Time::now() || force)) {
|
2020-04-18 00:55:54 +02:00
|
|
|
LOG_IF(INFO, force) << "Reload trending sticker sets";
|
2020-11-23 18:33:35 +01:00
|
|
|
next_load_time = -1;
|
2022-07-17 21:39:22 +02:00
|
|
|
td_->create_handler<GetFeaturedStickerSetsQuery>()->send(sticker_type, featured_sticker_sets_hash_[type]);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-17 21:39:22 +02:00
|
|
|
void StickersManager::reload_old_featured_sticker_sets(StickerType sticker_type, uint32 generation) {
|
|
|
|
if (sticker_type != StickerType::Regular) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto type = static_cast<int32>(sticker_type);
|
|
|
|
if (generation != 0 && generation != old_featured_sticker_set_generation_[type]) {
|
2020-04-18 00:55:54 +02:00
|
|
|
return;
|
|
|
|
}
|
2022-07-17 21:39:22 +02:00
|
|
|
td_->create_handler<GetOldFeaturedStickerSetsQuery>()->send(
|
|
|
|
sticker_type, static_cast<int32>(old_featured_sticker_set_ids_[type].size()), OLD_FEATURED_STICKER_SET_SLICE_SIZE,
|
|
|
|
old_featured_sticker_set_generation_[type]);
|
2020-04-18 00:55:54 +02:00
|
|
|
}
|
|
|
|
|
2019-09-18 05:55:43 +02:00
|
|
|
StickerSetId StickersManager::on_get_input_sticker_set(FileId sticker_file_id,
|
|
|
|
tl_object_ptr<telegram_api::InputStickerSet> &&set_ptr,
|
|
|
|
MultiPromiseActor *load_data_multipromise_ptr) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (set_ptr == nullptr) {
|
2019-09-18 05:55:43 +02:00
|
|
|
return StickerSetId();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
switch (set_ptr->get_id()) {
|
|
|
|
case telegram_api::inputStickerSetEmpty::ID:
|
2019-09-18 05:55:43 +02:00
|
|
|
return StickerSetId();
|
2018-12-31 20:04:05 +01:00
|
|
|
case telegram_api::inputStickerSetID::ID: {
|
|
|
|
auto set = move_tl_object_as<telegram_api::inputStickerSetID>(set_ptr);
|
2019-09-18 05:55:43 +02:00
|
|
|
StickerSetId set_id{set->id_};
|
2018-12-31 20:04:05 +01:00
|
|
|
add_sticker_set(set_id, set->access_hash_);
|
|
|
|
return set_id;
|
|
|
|
}
|
|
|
|
case telegram_api::inputStickerSetShortName::ID: {
|
|
|
|
auto set = move_tl_object_as<telegram_api::inputStickerSetShortName>(set_ptr);
|
|
|
|
if (load_data_multipromise_ptr == nullptr) {
|
2020-05-05 23:56:44 +02:00
|
|
|
LOG(ERROR) << "Receive sticker set " << set->short_name_ << " by its short name";
|
2018-12-31 20:04:05 +01:00
|
|
|
return search_sticker_set(set->short_name_, Auto());
|
|
|
|
}
|
|
|
|
auto set_id = search_sticker_set(set->short_name_, load_data_multipromise_ptr->get_promise());
|
2019-09-18 05:55:43 +02:00
|
|
|
if (!set_id.is_valid()) {
|
2021-10-06 22:33:18 +02:00
|
|
|
load_data_multipromise_ptr->add_promise(PromiseCreator::lambda(
|
|
|
|
[actor_id = actor_id(this), sticker_file_id, short_name = set->short_name_](Result<Unit> result) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (result.is_ok()) {
|
|
|
|
// just in case
|
2021-10-06 22:33:18 +02:00
|
|
|
send_closure(actor_id, &StickersManager::on_resolve_sticker_set_short_name, sticker_file_id,
|
|
|
|
short_name);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
2020-05-22 17:07:04 +02:00
|
|
|
// always return empty StickerSetId, because we can't trust the set_id provided by the peer in the secret chat
|
|
|
|
// the real sticker set id will be set in on_get_sticker if and only if the sticker is really from the set
|
|
|
|
return StickerSetId();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2020-03-14 01:47:19 +01:00
|
|
|
case telegram_api::inputStickerSetAnimatedEmoji::ID:
|
2021-09-15 13:44:11 +02:00
|
|
|
case telegram_api::inputStickerSetAnimatedEmojiAnimations::ID:
|
2022-07-15 14:24:08 +02:00
|
|
|
case telegram_api::inputStickerSetPremiumGifts::ID:
|
2022-09-02 12:59:49 +02:00
|
|
|
case telegram_api::inputStickerSetEmojiGenericAnimations::ID:
|
|
|
|
case telegram_api::inputStickerSetEmojiDefaultStatuses::ID:
|
2022-10-07 17:04:00 +02:00
|
|
|
case telegram_api::inputStickerSetEmojiDefaultTopicIcons::ID:
|
2021-12-01 17:59:45 +01:00
|
|
|
return add_special_sticker_set(SpecialStickerSetType(set_ptr)).id_;
|
2020-05-17 00:06:23 +02:00
|
|
|
case telegram_api::inputStickerSetDice::ID:
|
|
|
|
return StickerSetId();
|
2018-12-31 20:04:05 +01:00
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
2019-09-18 05:55:43 +02:00
|
|
|
return StickerSetId();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void StickersManager::on_resolve_sticker_set_short_name(FileId sticker_file_id, const string &short_name) {
|
2021-10-06 22:33:18 +02:00
|
|
|
if (G()->close_flag()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
LOG(INFO) << "Resolve sticker " << sticker_file_id << " set to " << short_name;
|
2019-09-18 05:55:43 +02:00
|
|
|
StickerSetId set_id = search_sticker_set(short_name, Auto());
|
|
|
|
if (set_id.is_valid()) {
|
2022-08-03 21:42:52 +02:00
|
|
|
auto *s = get_sticker(sticker_file_id);
|
2020-04-17 14:06:08 +02:00
|
|
|
CHECK(s != nullptr);
|
2022-08-07 18:47:50 +02:00
|
|
|
if (s->set_id_ != set_id) {
|
|
|
|
s->set_id_ = set_id;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-15 03:01:26 +02:00
|
|
|
void StickersManager::add_sticker_thumbnail(Sticker *s, PhotoSize thumbnail) {
|
|
|
|
if (!thumbnail.file_id.is_valid()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (thumbnail.type == 'm') {
|
2022-08-07 18:47:50 +02:00
|
|
|
s->m_thumbnail_ = std::move(thumbnail);
|
2019-07-15 03:01:26 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (thumbnail.type == 's' || thumbnail.type == 't') {
|
2022-08-07 18:47:50 +02:00
|
|
|
s->s_thumbnail_ = std::move(thumbnail);
|
2019-07-15 03:01:26 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
LOG(ERROR) << "Receive sticker thumbnail of unsupported type " << thumbnail.type;
|
|
|
|
}
|
|
|
|
|
2022-04-26 15:01:29 +02:00
|
|
|
void StickersManager::create_sticker(FileId file_id, FileId premium_animation_file_id, string minithumbnail,
|
|
|
|
PhotoSize thumbnail, Dimensions dimensions,
|
2022-01-18 17:20:43 +01:00
|
|
|
tl_object_ptr<telegram_api::documentAttributeSticker> sticker,
|
2022-07-14 16:11:36 +02:00
|
|
|
tl_object_ptr<telegram_api::documentAttributeCustomEmoji> custom_emoji,
|
2022-01-18 17:20:43 +01:00
|
|
|
StickerFormat format, MultiPromiseActor *load_data_multipromise_ptr) {
|
2022-02-17 18:45:58 +01:00
|
|
|
if (format == StickerFormat::Unknown && sticker == nullptr) {
|
|
|
|
auto old_sticker = get_sticker(file_id);
|
|
|
|
if (old_sticker != nullptr) {
|
2022-08-07 18:47:50 +02:00
|
|
|
format = old_sticker->format_;
|
2022-02-17 18:45:58 +01:00
|
|
|
} else {
|
|
|
|
// guess format by file extension
|
|
|
|
auto file_view = td_->file_manager_->get_file_view(file_id);
|
|
|
|
auto suggested_path = file_view.suggested_path();
|
|
|
|
const PathView path_view(suggested_path);
|
|
|
|
format = get_sticker_format_by_extension(path_view.extension());
|
|
|
|
if (format == StickerFormat::Unknown) {
|
|
|
|
format = StickerFormat::Webp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-01-18 17:20:43 +01:00
|
|
|
if (is_sticker_format_vector(format) && dimensions.width == 0) {
|
2022-07-14 16:11:36 +02:00
|
|
|
dimensions.width = custom_emoji != nullptr ? 100 : 512;
|
|
|
|
dimensions.height = custom_emoji != nullptr ? 100 : 512;
|
2019-07-15 02:43:05 +02:00
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
auto s = make_unique<Sticker>();
|
2022-08-07 18:47:50 +02:00
|
|
|
s->file_id_ = file_id;
|
|
|
|
s->dimensions_ = dimensions;
|
2021-05-27 20:15:30 +02:00
|
|
|
if (!td_->auth_manager_->is_bot()) {
|
2022-08-07 18:47:50 +02:00
|
|
|
s->minithumbnail_ = std::move(minithumbnail);
|
2021-05-27 20:15:30 +02:00
|
|
|
}
|
2021-10-19 17:11:16 +02:00
|
|
|
add_sticker_thumbnail(s.get(), std::move(thumbnail));
|
2022-07-15 12:37:46 +02:00
|
|
|
if (premium_animation_file_id.is_valid()) {
|
2022-08-07 18:47:50 +02:00
|
|
|
s->is_premium_ = true;
|
2022-07-15 12:37:46 +02:00
|
|
|
}
|
2022-08-07 18:47:50 +02:00
|
|
|
s->premium_animation_file_id_ = premium_animation_file_id;
|
2018-12-31 20:04:05 +01:00
|
|
|
if (sticker != nullptr) {
|
2022-08-07 18:47:50 +02:00
|
|
|
s->set_id_ = on_get_input_sticker_set(file_id, std::move(sticker->stickerset_), load_data_multipromise_ptr);
|
|
|
|
s->alt_ = std::move(sticker->alt_);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2022-07-14 14:02:55 +02:00
|
|
|
if ((sticker->flags_ & telegram_api::documentAttributeSticker::MASK_MASK) != 0) {
|
2022-08-07 18:47:50 +02:00
|
|
|
s->type_ = StickerType::Mask;
|
2022-07-14 14:02:55 +02:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
if ((sticker->flags_ & telegram_api::documentAttributeSticker::MASK_COORDS_MASK) != 0) {
|
|
|
|
CHECK(sticker->mask_coords_ != nullptr);
|
|
|
|
int32 point = sticker->mask_coords_->n_;
|
|
|
|
if (0 <= point && point <= 3) {
|
2022-08-07 18:47:50 +02:00
|
|
|
s->point_ = sticker->mask_coords_->n_;
|
|
|
|
s->x_shift_ = sticker->mask_coords_->x_;
|
|
|
|
s->y_shift_ = sticker->mask_coords_->y_;
|
|
|
|
s->scale_ = sticker->mask_coords_->zoom_;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
2022-07-14 16:11:36 +02:00
|
|
|
} else if (custom_emoji != nullptr) {
|
2022-08-07 18:47:50 +02:00
|
|
|
s->set_id_ = on_get_input_sticker_set(file_id, std::move(custom_emoji->stickerset_), load_data_multipromise_ptr);
|
|
|
|
s->alt_ = std::move(custom_emoji->alt_);
|
|
|
|
s->type_ = StickerType::CustomEmoji;
|
|
|
|
s->is_premium_ = !custom_emoji->free_;
|
|
|
|
s->emoji_receive_date_ = G()->unix_time();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2022-08-07 18:47:50 +02:00
|
|
|
s->format_ = format;
|
2022-07-14 16:11:36 +02:00
|
|
|
on_get_sticker(std::move(s),
|
|
|
|
(sticker != nullptr || custom_emoji != nullptr) && load_data_multipromise_ptr == nullptr);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool StickersManager::has_input_media(FileId sticker_file_id, bool is_secret) const {
|
|
|
|
auto file_view = td_->file_manager_->get_file_view(sticker_file_id);
|
|
|
|
if (is_secret) {
|
2020-05-25 21:30:26 +02:00
|
|
|
const Sticker *sticker = get_sticker(sticker_file_id);
|
|
|
|
CHECK(sticker != nullptr);
|
2018-03-27 15:11:15 +02:00
|
|
|
if (file_view.is_encrypted_secret()) {
|
2020-05-25 21:30:26 +02:00
|
|
|
if (!file_view.encryption_key().empty() && file_view.has_remote_location() &&
|
2022-08-07 18:47:50 +02:00
|
|
|
!sticker->s_thumbnail_.file_id.is_valid()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return true;
|
|
|
|
}
|
2018-03-27 15:11:15 +02:00
|
|
|
} else if (!file_view.is_encrypted()) {
|
2022-08-07 18:47:50 +02:00
|
|
|
if (sticker->set_id_.is_valid()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
// stickers within a set can be sent by id and access_hash
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (file_view.is_encrypted()) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-05-25 21:30:26 +02:00
|
|
|
if (td_->auth_manager_->is_bot() && file_view.has_remote_location()) {
|
|
|
|
return true;
|
|
|
|
}
|
2022-07-03 10:53:01 +02:00
|
|
|
const Sticker *sticker = get_sticker(sticker_file_id);
|
|
|
|
CHECK(sticker != nullptr);
|
2022-08-07 18:47:50 +02:00
|
|
|
if (sticker->set_id_.is_valid()) {
|
2022-07-03 10:53:01 +02:00
|
|
|
// stickers within a set doesn't need to be duped
|
|
|
|
return true;
|
|
|
|
}
|
2020-05-25 21:30:26 +02:00
|
|
|
// having remote location is not enough to have InputMedia, because the file may not have valid file_reference
|
|
|
|
// also file_id needs to be duped, because upload can be called to repair the file_reference and every upload
|
|
|
|
// request must have unique file_id
|
|
|
|
if (/* file_view.has_remote_location() || */ file_view.has_url()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
SecretInputMedia StickersManager::get_secret_input_media(FileId sticker_file_id,
|
|
|
|
tl_object_ptr<telegram_api::InputEncryptedFile> input_file,
|
2022-05-11 06:46:06 +02:00
|
|
|
BufferSlice thumbnail, int32 layer) const {
|
2018-12-31 20:04:05 +01:00
|
|
|
const Sticker *sticker = get_sticker(sticker_file_id);
|
|
|
|
CHECK(sticker != nullptr);
|
|
|
|
auto file_view = td_->file_manager_->get_file_view(sticker_file_id);
|
2018-03-27 15:11:15 +02:00
|
|
|
if (file_view.is_encrypted_secret()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (file_view.has_remote_location()) {
|
2019-11-17 20:41:28 +01:00
|
|
|
input_file = file_view.main_remote_location().as_input_encrypted_file();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
if (!input_file) {
|
|
|
|
return {};
|
|
|
|
}
|
2022-08-07 18:47:50 +02:00
|
|
|
if (sticker->s_thumbnail_.file_id.is_valid() && thumbnail.empty()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return {};
|
|
|
|
}
|
2018-03-27 15:11:15 +02:00
|
|
|
} else if (!file_view.is_encrypted()) {
|
2022-08-07 18:47:50 +02:00
|
|
|
if (!sticker->set_id_.is_valid()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
// stickers without set can't be sent by id and access_hash
|
|
|
|
return {};
|
|
|
|
}
|
2018-03-27 15:11:15 +02:00
|
|
|
} else {
|
|
|
|
return {};
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
tl_object_ptr<secret_api::InputStickerSet> input_sticker_set = make_tl_object<secret_api::inputStickerSetEmpty>();
|
2022-08-07 18:47:50 +02:00
|
|
|
if (sticker->set_id_.is_valid()) {
|
|
|
|
const StickerSet *sticker_set = get_sticker_set(sticker->set_id_);
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK(sticker_set != nullptr);
|
2022-08-07 18:47:50 +02:00
|
|
|
if (sticker_set->is_inited_) {
|
|
|
|
input_sticker_set = make_tl_object<secret_api::inputStickerSetShortName>(sticker_set->short_name_);
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
|
|
|
// TODO load sticker set
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-17 15:46:20 +02:00
|
|
|
vector<tl_object_ptr<secret_api::DocumentAttribute>> attributes;
|
|
|
|
attributes.push_back(
|
2022-08-07 18:47:50 +02:00
|
|
|
secret_api::make_object<secret_api::documentAttributeSticker>(sticker->alt_, std::move(input_sticker_set)));
|
|
|
|
if (sticker->dimensions_.width != 0 && sticker->dimensions_.height != 0) {
|
|
|
|
attributes.push_back(secret_api::make_object<secret_api::documentAttributeImageSize>(sticker->dimensions_.width,
|
|
|
|
sticker->dimensions_.height));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2018-03-27 15:11:15 +02:00
|
|
|
if (file_view.is_encrypted_secret()) {
|
2022-05-11 00:53:18 +02:00
|
|
|
return {std::move(input_file),
|
|
|
|
std::move(thumbnail),
|
2022-08-07 18:47:50 +02:00
|
|
|
sticker->s_thumbnail_.dimensions,
|
|
|
|
get_sticker_format_mime_type(sticker->format_),
|
2022-05-11 00:53:18 +02:00
|
|
|
file_view,
|
|
|
|
std::move(attributes),
|
2022-05-11 06:46:06 +02:00
|
|
|
string(),
|
|
|
|
layer};
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
2018-03-27 15:11:15 +02:00
|
|
|
CHECK(!file_view.is_encrypted());
|
2018-12-31 20:04:05 +01:00
|
|
|
auto &remote_location = file_view.remote_location();
|
2019-07-15 02:43:05 +02:00
|
|
|
if (remote_location.is_web()) {
|
|
|
|
// web stickers shouldn't have set_id
|
2022-08-07 18:47:50 +02:00
|
|
|
LOG(ERROR) << "Have a web sticker in " << sticker->set_id_;
|
2019-07-15 02:43:05 +02:00
|
|
|
return {};
|
|
|
|
}
|
2022-05-11 16:17:20 +02:00
|
|
|
if (file_view.size() > 1000000000) {
|
2022-08-07 18:47:50 +02:00
|
|
|
LOG(ERROR) << "Have a sticker of size " << file_view.size() << " in " << sticker->set_id_;
|
2022-05-11 16:17:20 +02:00
|
|
|
return {};
|
|
|
|
}
|
2022-01-18 17:20:43 +01:00
|
|
|
return SecretInputMedia{
|
|
|
|
nullptr, make_tl_object<secret_api::decryptedMessageMediaExternalDocument>(
|
|
|
|
remote_location.get_id(), remote_location.get_access_hash(), 0 /*date*/,
|
2022-08-07 18:47:50 +02:00
|
|
|
get_sticker_format_mime_type(sticker->format_), narrow_cast<int32>(file_view.size()),
|
2022-01-18 17:20:43 +01:00
|
|
|
make_tl_object<secret_api::photoSizeEmpty>("t"), remote_location.get_dc_id().get_raw_id(),
|
|
|
|
std::move(attributes))};
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tl_object_ptr<telegram_api::InputMedia> StickersManager::get_input_media(
|
|
|
|
FileId file_id, tl_object_ptr<telegram_api::InputFile> input_file,
|
2020-12-19 12:31:42 +01:00
|
|
|
tl_object_ptr<telegram_api::InputFile> input_thumbnail, const string &emoji) const {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto file_view = td_->file_manager_->get_file_view(file_id);
|
|
|
|
if (file_view.is_encrypted()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2019-11-17 20:41:28 +01:00
|
|
|
if (file_view.has_remote_location() && !file_view.main_remote_location().is_web() && input_file == nullptr) {
|
2020-12-19 12:31:42 +01:00
|
|
|
int32 flags = 0;
|
|
|
|
if (!emoji.empty()) {
|
|
|
|
flags |= telegram_api::inputMediaDocument::QUERY_MASK;
|
|
|
|
}
|
|
|
|
return make_tl_object<telegram_api::inputMediaDocument>(flags, file_view.main_remote_location().as_input_document(),
|
|
|
|
0, emoji);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
if (file_view.has_url()) {
|
2018-01-30 18:06:54 +01:00
|
|
|
return make_tl_object<telegram_api::inputMediaDocumentExternal>(0, file_view.url(), 0);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (input_file != nullptr) {
|
|
|
|
const Sticker *s = get_sticker(file_id);
|
|
|
|
CHECK(s != nullptr);
|
|
|
|
|
|
|
|
vector<tl_object_ptr<telegram_api::DocumentAttribute>> attributes;
|
2022-08-07 18:47:50 +02:00
|
|
|
if (s->dimensions_.width != 0 && s->dimensions_.height != 0) {
|
2018-12-31 20:04:05 +01:00
|
|
|
attributes.push_back(
|
2022-08-07 18:47:50 +02:00
|
|
|
make_tl_object<telegram_api::documentAttributeImageSize>(s->dimensions_.width, s->dimensions_.height));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
attributes.push_back(make_tl_object<telegram_api::documentAttributeSticker>(
|
2022-08-07 18:47:50 +02:00
|
|
|
0, false /*ignored*/, s->alt_, make_tl_object<telegram_api::inputStickerSetEmpty>(), nullptr));
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
int32 flags = 0;
|
|
|
|
if (input_thumbnail != nullptr) {
|
|
|
|
flags |= telegram_api::inputMediaUploadedDocument::THUMB_MASK;
|
|
|
|
}
|
2022-08-07 18:47:50 +02:00
|
|
|
auto mime_type = get_sticker_format_mime_type(s->format_);
|
2018-12-31 20:04:05 +01:00
|
|
|
return make_tl_object<telegram_api::inputMediaUploadedDocument>(
|
2020-06-22 01:02:21 +02:00
|
|
|
flags, false /*ignored*/, false /*ignored*/, std::move(input_file), std::move(input_thumbnail), mime_type,
|
|
|
|
std::move(attributes), vector<tl_object_ptr<telegram_api::InputDocument>>(), 0);
|
2019-02-12 02:50:30 +01:00
|
|
|
} else {
|
|
|
|
CHECK(!file_view.has_remote_location());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-12-29 02:59:34 +01:00
|
|
|
StickerSetId StickersManager::on_get_sticker_set(tl_object_ptr<telegram_api::stickerSet> &&set, bool is_changed,
|
|
|
|
const char *source) {
|
2019-09-18 05:55:43 +02:00
|
|
|
CHECK(set != nullptr);
|
|
|
|
StickerSetId set_id{set->id_};
|
2018-12-31 20:04:05 +01:00
|
|
|
StickerSet *s = add_sticker_set(set_id, set->access_hash_);
|
2022-02-10 16:03:03 +01:00
|
|
|
if (s == nullptr) {
|
|
|
|
return {};
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-02-22 01:36:40 +01:00
|
|
|
bool is_installed = (set->flags_ & telegram_api::stickerSet::INSTALLED_DATE_MASK) != 0;
|
2021-11-01 19:53:23 +01:00
|
|
|
bool is_archived = set->archived_;
|
|
|
|
bool is_official = set->official_;
|
2022-01-25 12:58:04 +01:00
|
|
|
StickerFormat sticker_format =
|
2022-01-28 23:53:50 +01:00
|
|
|
set->videos_ ? StickerFormat::Webm : (set->animated_ ? StickerFormat::Tgs : StickerFormat::Webp);
|
2022-07-14 15:42:27 +02:00
|
|
|
StickerType sticker_type =
|
2022-07-19 13:51:29 +02:00
|
|
|
set->emojis_ ? StickerType::CustomEmoji : (set->masks_ ? StickerType::Mask : StickerType::Regular);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2019-05-17 16:35:45 +02:00
|
|
|
PhotoSize thumbnail;
|
2020-11-09 21:41:23 +01:00
|
|
|
string minithumbnail;
|
2022-07-16 16:22:30 +02:00
|
|
|
int64 thumbnail_document_id = 0;
|
2020-12-14 14:52:25 +01:00
|
|
|
for (auto &thumb : set->thumbs_) {
|
2021-10-27 15:52:22 +02:00
|
|
|
auto photo_size =
|
|
|
|
get_photo_size(td_->file_manager_.get(),
|
2022-08-07 18:47:50 +02:00
|
|
|
PhotoSizeSource::sticker_set_thumbnail(set_id.get(), s->access_hash_, set->thumb_version_), 0, 0,
|
2021-10-27 15:52:22 +02:00
|
|
|
"", DcId::create(set->thumb_dc_id_), DialogId(), std::move(thumb),
|
2022-01-18 17:20:43 +01:00
|
|
|
get_sticker_set_thumbnail_format(sticker_format));
|
2019-05-17 17:08:17 +02:00
|
|
|
if (photo_size.get_offset() == 0) {
|
2020-11-09 21:41:23 +01:00
|
|
|
if (!thumbnail.file_id.is_valid()) {
|
|
|
|
thumbnail = std::move(photo_size.get<0>());
|
|
|
|
}
|
2019-05-17 17:08:17 +02:00
|
|
|
} else {
|
2020-11-09 21:41:23 +01:00
|
|
|
minithumbnail = std::move(photo_size.get<1>());
|
2019-05-17 17:08:17 +02:00
|
|
|
}
|
2019-05-17 16:35:45 +02:00
|
|
|
}
|
2022-07-16 16:22:30 +02:00
|
|
|
if ((set->flags_ & telegram_api::stickerSet::THUMB_DOCUMENT_ID_MASK) != 0) {
|
|
|
|
thumbnail_document_id = set->thumb_document_id_;
|
|
|
|
}
|
2022-08-07 18:47:50 +02:00
|
|
|
if (!s->is_inited_) {
|
2020-06-04 00:19:37 +02:00
|
|
|
LOG(INFO) << "Init " << set_id;
|
2022-08-07 18:47:50 +02:00
|
|
|
s->is_inited_ = true;
|
|
|
|
s->title_ = std::move(set->title_);
|
|
|
|
s->short_name_ = std::move(set->short_name_);
|
2021-05-27 20:15:30 +02:00
|
|
|
if (!td_->auth_manager_->is_bot()) {
|
2022-08-07 18:47:50 +02:00
|
|
|
s->minithumbnail_ = std::move(minithumbnail);
|
|
|
|
}
|
|
|
|
s->thumbnail_ = std::move(thumbnail);
|
|
|
|
s->thumbnail_document_id_ = thumbnail_document_id;
|
|
|
|
s->is_thumbnail_reloaded_ = true;
|
|
|
|
s->are_legacy_sticker_thumbnails_reloaded_ = true;
|
|
|
|
s->sticker_count_ = set->count_;
|
|
|
|
s->hash_ = set->hash_;
|
|
|
|
s->is_official_ = is_official;
|
|
|
|
s->sticker_format_ = sticker_format;
|
|
|
|
s->sticker_type_ = sticker_type;
|
|
|
|
s->is_changed_ = true;
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
2022-08-07 18:47:50 +02:00
|
|
|
CHECK(s->id_ == set_id);
|
|
|
|
auto type = static_cast<int32>(s->sticker_type_);
|
|
|
|
if (s->access_hash_ != set->access_hash_) {
|
2019-09-18 05:55:43 +02:00
|
|
|
LOG(INFO) << "Access hash of " << set_id << " has changed";
|
2022-08-07 18:47:50 +02:00
|
|
|
s->access_hash_ = set->access_hash_;
|
|
|
|
s->need_save_to_database_ = true;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2022-08-07 18:47:50 +02:00
|
|
|
if (s->title_ != set->title_) {
|
2019-09-18 05:55:43 +02:00
|
|
|
LOG(INFO) << "Title of " << set_id << " has changed";
|
2022-08-07 18:47:50 +02:00
|
|
|
s->title_ = std::move(set->title_);
|
|
|
|
s->is_changed_ = true;
|
2018-03-05 23:44:11 +01:00
|
|
|
|
2022-07-24 13:01:58 +02:00
|
|
|
if (installed_sticker_sets_hints_[type].has_key(set_id.get())) {
|
2022-08-07 18:47:50 +02:00
|
|
|
installed_sticker_sets_hints_[type].add(set_id.get(), PSLICE() << s->title_ << ' ' << s->short_name_);
|
2018-03-05 23:44:11 +01:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2022-08-07 18:47:50 +02:00
|
|
|
if (s->short_name_ != set->short_name_) {
|
|
|
|
LOG(ERROR) << "Short name of " << set_id << " has changed from \"" << s->short_name_ << "\" to \""
|
2019-12-29 02:59:34 +01:00
|
|
|
<< set->short_name_ << "\" from " << source;
|
2022-08-07 18:47:50 +02:00
|
|
|
short_name_to_sticker_set_id_.erase(clean_username(s->short_name_));
|
|
|
|
s->short_name_ = std::move(set->short_name_);
|
|
|
|
s->is_changed_ = true;
|
2018-03-05 23:44:11 +01:00
|
|
|
|
2022-07-24 13:01:58 +02:00
|
|
|
if (installed_sticker_sets_hints_[type].has_key(set_id.get())) {
|
2022-08-07 18:47:50 +02:00
|
|
|
installed_sticker_sets_hints_[type].add(set_id.get(), PSLICE() << s->title_ << ' ' << s->short_name_);
|
2018-03-05 23:44:11 +01:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2022-08-07 18:47:50 +02:00
|
|
|
if (s->minithumbnail_ != minithumbnail) {
|
2020-11-09 21:41:23 +01:00
|
|
|
LOG(INFO) << "Minithumbnail of " << set_id << " has changed";
|
2022-08-07 18:47:50 +02:00
|
|
|
s->minithumbnail_ = std::move(minithumbnail);
|
|
|
|
s->is_changed_ = true;
|
2020-11-09 21:41:23 +01:00
|
|
|
}
|
2022-08-07 18:47:50 +02:00
|
|
|
if (s->thumbnail_ != thumbnail) {
|
|
|
|
LOG(INFO) << "Thumbnail of " << set_id << " has changed from " << s->thumbnail_ << " to " << thumbnail;
|
|
|
|
s->thumbnail_ = std::move(thumbnail);
|
|
|
|
s->is_changed_ = true;
|
2019-05-17 16:35:45 +02:00
|
|
|
}
|
2022-08-07 18:47:50 +02:00
|
|
|
if (s->thumbnail_document_id_ != thumbnail_document_id) {
|
|
|
|
LOG(INFO) << "Thumbnail of " << set_id << " has changed from " << s->thumbnail_document_id_ << " to "
|
2022-07-16 16:22:30 +02:00
|
|
|
<< thumbnail_document_id;
|
2022-08-07 18:47:50 +02:00
|
|
|
s->thumbnail_document_id_ = thumbnail_document_id;
|
|
|
|
s->is_changed_ = true;
|
2022-07-16 16:22:30 +02:00
|
|
|
}
|
2022-08-07 18:47:50 +02:00
|
|
|
if (!s->is_thumbnail_reloaded_ || !s->are_legacy_sticker_thumbnails_reloaded_) {
|
2020-10-30 23:32:18 +01:00
|
|
|
LOG(INFO) << "Sticker thumbnails and thumbnail of " << set_id << " was reloaded";
|
2022-08-07 18:47:50 +02:00
|
|
|
s->is_thumbnail_reloaded_ = true;
|
|
|
|
s->are_legacy_sticker_thumbnails_reloaded_ = true;
|
|
|
|
s->need_save_to_database_ = true;
|
2019-05-17 17:08:17 +02:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2022-08-07 18:47:50 +02:00
|
|
|
if (s->sticker_count_ != set->count_ || s->hash_ != set->hash_) {
|
|
|
|
LOG(INFO) << "Number of stickers in " << set_id << " changed from " << s->sticker_count_ << " to " << set->count_;
|
|
|
|
s->is_loaded_ = false;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2022-08-07 18:47:50 +02:00
|
|
|
s->sticker_count_ = set->count_;
|
|
|
|
s->hash_ = set->hash_;
|
|
|
|
if (s->was_loaded_) {
|
|
|
|
s->need_save_to_database_ = true;
|
2020-04-17 14:06:08 +02:00
|
|
|
} else {
|
2022-08-07 18:47:50 +02:00
|
|
|
s->is_changed_ = true;
|
2020-04-17 14:06:08 +02:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2022-08-07 18:47:50 +02:00
|
|
|
if (s->is_official_ != is_official) {
|
2020-06-04 00:19:37 +02:00
|
|
|
LOG(INFO) << "Official flag of " << set_id << " changed to " << is_official;
|
2022-08-07 18:47:50 +02:00
|
|
|
s->is_official_ = is_official;
|
|
|
|
s->is_changed_ = true;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2022-08-07 18:47:50 +02:00
|
|
|
if (s->sticker_format_ != sticker_format) {
|
|
|
|
LOG(ERROR) << "Format of stickers in " << set_id << "/" << s->short_name_ << " has changed from "
|
|
|
|
<< s->sticker_format_ << " to " << sticker_format << " from " << source;
|
|
|
|
s->sticker_format_ = sticker_format;
|
|
|
|
s->is_changed_ = true;
|
2020-01-02 16:41:42 +01:00
|
|
|
}
|
2022-08-07 18:47:50 +02:00
|
|
|
LOG_IF(ERROR, s->sticker_type_ != sticker_type)
|
|
|
|
<< "Type of " << set_id << "/" << s->short_name_ << " has changed from " << s->sticker_type_ << " to "
|
2022-07-14 15:17:19 +02:00
|
|
|
<< sticker_type << " from " << source;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2022-08-07 18:47:50 +02:00
|
|
|
auto cleaned_username = clean_username(s->short_name_);
|
2022-02-10 16:03:03 +01:00
|
|
|
if (!cleaned_username.empty()) {
|
2022-08-03 21:42:52 +02:00
|
|
|
short_name_to_sticker_set_id_.set(cleaned_username, set_id);
|
2022-02-10 16:03:03 +01:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
on_update_sticker_set(s, is_installed, is_archived, is_changed);
|
|
|
|
|
|
|
|
return set_id;
|
|
|
|
}
|
|
|
|
|
2019-09-18 05:55:43 +02:00
|
|
|
StickerSetId StickersManager::on_get_sticker_set_covered(tl_object_ptr<telegram_api::StickerSetCovered> &&set_ptr,
|
2019-12-29 02:59:34 +01:00
|
|
|
bool is_changed, const char *source) {
|
2019-09-18 05:55:43 +02:00
|
|
|
StickerSetId set_id;
|
2018-12-31 20:04:05 +01:00
|
|
|
switch (set_ptr->get_id()) {
|
|
|
|
case telegram_api::stickerSetCovered::ID: {
|
|
|
|
auto covered_set = move_tl_object_as<telegram_api::stickerSetCovered>(set_ptr);
|
2019-12-29 02:59:34 +01:00
|
|
|
set_id = on_get_sticker_set(std::move(covered_set->set_), is_changed, source);
|
2019-09-18 05:55:43 +02:00
|
|
|
if (!set_id.is_valid()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto sticker_set = get_sticker_set(set_id);
|
|
|
|
CHECK(sticker_set != nullptr);
|
2022-08-07 18:47:50 +02:00
|
|
|
CHECK(sticker_set->is_inited_);
|
|
|
|
if (sticker_set->was_loaded_) {
|
2018-12-31 20:04:05 +01:00
|
|
|
break;
|
|
|
|
}
|
2022-08-07 18:47:50 +02:00
|
|
|
if (sticker_set->sticker_count_ == 0) {
|
2018-12-31 20:04:05 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-08-07 18:47:50 +02:00
|
|
|
auto &sticker_ids = sticker_set->sticker_ids_;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2022-08-07 18:47:50 +02:00
|
|
|
auto sticker_id = on_get_sticker_document(std::move(covered_set->cover_), sticker_set->sticker_format_).second;
|
2019-10-22 01:12:58 +02:00
|
|
|
if (sticker_id.is_valid() && !td::contains(sticker_ids, sticker_id)) {
|
2018-12-31 20:04:05 +01:00
|
|
|
sticker_ids.push_back(sticker_id);
|
2022-08-07 18:47:50 +02:00
|
|
|
sticker_set->is_changed_ = true;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case telegram_api::stickerSetMultiCovered::ID: {
|
|
|
|
auto multicovered_set = move_tl_object_as<telegram_api::stickerSetMultiCovered>(set_ptr);
|
2019-12-29 02:59:34 +01:00
|
|
|
set_id = on_get_sticker_set(std::move(multicovered_set->set_), is_changed, source);
|
2019-09-18 05:55:43 +02:00
|
|
|
if (!set_id.is_valid()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto sticker_set = get_sticker_set(set_id);
|
|
|
|
CHECK(sticker_set != nullptr);
|
2022-08-07 18:47:50 +02:00
|
|
|
CHECK(sticker_set->is_inited_);
|
|
|
|
if (sticker_set->was_loaded_) {
|
2018-12-31 20:04:05 +01:00
|
|
|
break;
|
|
|
|
}
|
2022-08-07 18:47:50 +02:00
|
|
|
auto &sticker_ids = sticker_set->sticker_ids_;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
for (auto &cover : multicovered_set->covers_) {
|
2022-08-07 18:47:50 +02:00
|
|
|
auto sticker_id = on_get_sticker_document(std::move(cover), sticker_set->sticker_format_).second;
|
2019-10-22 01:12:58 +02:00
|
|
|
if (sticker_id.is_valid() && !td::contains(sticker_ids, sticker_id)) {
|
2018-12-31 20:04:05 +01:00
|
|
|
sticker_ids.push_back(sticker_id);
|
2022-08-07 18:47:50 +02:00
|
|
|
sticker_set->is_changed_ = true;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2022-07-18 20:44:44 +02:00
|
|
|
case telegram_api::stickerSetFullCovered::ID: {
|
|
|
|
auto set = move_tl_object_as<telegram_api::stickerSetFullCovered>(set_ptr);
|
|
|
|
auto sticker_set = telegram_api::make_object<telegram_api::messages_stickerSet>(
|
2022-09-26 19:00:45 +02:00
|
|
|
std::move(set->set_), std::move(set->packs_), std::move(set->keywords_), std::move(set->documents_));
|
2022-07-18 20:44:44 +02:00
|
|
|
return on_get_messages_sticker_set(StickerSetId(), std::move(sticker_set), is_changed, source);
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
return set_id;
|
|
|
|
}
|
|
|
|
|
2019-11-21 20:18:29 +01:00
|
|
|
StickerSetId StickersManager::on_get_messages_sticker_set(StickerSetId sticker_set_id,
|
2021-11-09 17:07:55 +01:00
|
|
|
tl_object_ptr<telegram_api::messages_StickerSet> &&set_ptr,
|
2019-12-29 02:59:34 +01:00
|
|
|
bool is_changed, const char *source) {
|
2021-11-09 17:07:55 +01:00
|
|
|
LOG(INFO) << "Receive sticker set " << to_string(set_ptr);
|
|
|
|
if (set_ptr->get_id() == telegram_api::messages_stickerSetNotModified::ID) {
|
|
|
|
if (!sticker_set_id.is_valid()) {
|
|
|
|
LOG(ERROR) << "Receive unexpected stickerSetNotModified from " << source;
|
2021-11-19 11:56:12 +01:00
|
|
|
} else {
|
|
|
|
auto s = get_sticker_set(sticker_set_id);
|
|
|
|
CHECK(s != nullptr);
|
2022-08-07 18:47:50 +02:00
|
|
|
CHECK(s->is_inited_);
|
|
|
|
CHECK(s->was_loaded_);
|
2021-11-19 11:56:12 +01:00
|
|
|
|
2022-08-07 18:47:50 +02:00
|
|
|
s->is_loaded_ = true;
|
|
|
|
s->expires_at_ = G()->unix_time() +
|
|
|
|
(td_->auth_manager_->is_bot() ? Random::fast(10 * 60, 15 * 60) : Random::fast(30 * 60, 50 * 60));
|
2021-11-09 17:07:55 +01:00
|
|
|
}
|
|
|
|
return sticker_set_id;
|
|
|
|
}
|
|
|
|
auto set = move_tl_object_as<telegram_api::messages_stickerSet>(set_ptr);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2019-12-29 02:59:34 +01:00
|
|
|
auto set_id = on_get_sticker_set(std::move(set->set_), is_changed, source);
|
2019-09-18 05:55:43 +02:00
|
|
|
if (!set_id.is_valid()) {
|
2022-06-21 16:34:24 +02:00
|
|
|
return StickerSetId();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2019-09-18 05:55:43 +02:00
|
|
|
if (sticker_set_id.is_valid() && sticker_set_id != set_id) {
|
2019-12-29 02:59:34 +01:00
|
|
|
LOG(ERROR) << "Expected " << sticker_set_id << ", but receive " << set_id << " from " << source;
|
2021-06-25 20:21:17 +02:00
|
|
|
on_load_sticker_set_fail(sticker_set_id, Status::Error(500, "Internal Server Error: wrong sticker set received"));
|
2019-11-21 20:18:29 +01:00
|
|
|
return StickerSetId();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
auto s = get_sticker_set(set_id);
|
|
|
|
CHECK(s != nullptr);
|
2022-08-07 18:47:50 +02:00
|
|
|
CHECK(s->is_inited_);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2022-08-07 18:47:50 +02:00
|
|
|
s->expires_at_ = G()->unix_time() +
|
|
|
|
(td_->auth_manager_->is_bot() ? Random::fast(10 * 60, 15 * 60) : Random::fast(30 * 60, 50 * 60));
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2022-08-07 18:47:50 +02:00
|
|
|
if (s->is_loaded_) {
|
2021-11-14 08:19:03 +01:00
|
|
|
update_sticker_set(s, "on_get_messages_sticker_set");
|
2018-12-31 20:04:05 +01:00
|
|
|
send_update_installed_sticker_sets();
|
2019-11-21 20:18:29 +01:00
|
|
|
return set_id;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2022-08-07 18:47:50 +02:00
|
|
|
s->was_loaded_ = true;
|
|
|
|
s->is_loaded_ = true;
|
|
|
|
s->is_changed_ = true;
|
2022-09-26 23:20:13 +02:00
|
|
|
s->are_keywords_loaded_ = true;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2022-02-07 20:41:07 +01:00
|
|
|
FlatHashMap<int64, FileId> document_id_to_sticker_id;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2022-08-07 18:47:50 +02:00
|
|
|
s->sticker_ids_.clear();
|
2022-08-08 20:13:22 +02:00
|
|
|
s->premium_sticker_positions_.clear();
|
2019-12-30 04:35:53 +01:00
|
|
|
bool is_bot = td_->auth_manager_->is_bot();
|
2022-09-26 23:20:13 +02:00
|
|
|
for (auto &document_ptr : set->documents_) {
|
2022-08-07 18:47:50 +02:00
|
|
|
auto sticker_id = on_get_sticker_document(std::move(document_ptr), s->sticker_format_);
|
2022-02-09 22:59:52 +01:00
|
|
|
if (!sticker_id.second.is_valid() || sticker_id.first == 0) {
|
2018-12-31 20:04:05 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-08-07 19:35:13 +02:00
|
|
|
if (!is_bot && get_sticker(sticker_id.second)->is_premium_) {
|
|
|
|
s->premium_sticker_positions_.push_back(static_cast<int32>(s->sticker_ids_.size()));
|
|
|
|
}
|
2022-08-07 18:47:50 +02:00
|
|
|
s->sticker_ids_.push_back(sticker_id.second);
|
2019-12-30 04:35:53 +01:00
|
|
|
if (!is_bot) {
|
2022-02-07 20:41:07 +01:00
|
|
|
document_id_to_sticker_id.emplace(sticker_id.first, sticker_id.second);
|
2019-12-30 04:35:53 +01:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2022-08-07 18:47:50 +02:00
|
|
|
if (static_cast<int32>(s->sticker_ids_.size()) != s->sticker_count_) {
|
|
|
|
LOG(ERROR) << "Wrong sticker set size " << s->sticker_count_ << " instead of " << s->sticker_ids_.size()
|
|
|
|
<< " specified in " << set_id << "/" << s->short_name_ << " from " << source;
|
|
|
|
s->sticker_count_ = static_cast<int32>(s->sticker_ids_.size());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2019-12-30 04:35:53 +01:00
|
|
|
if (!is_bot) {
|
|
|
|
s->emoji_stickers_map_.clear();
|
|
|
|
s->sticker_emojis_map_.clear();
|
2022-09-27 15:16:36 +02:00
|
|
|
s->keyword_stickers_map_.clear();
|
2022-09-26 23:20:13 +02:00
|
|
|
s->sticker_keywords_map_.clear();
|
|
|
|
for (auto &pack : set->packs_) {
|
2022-05-12 17:02:27 +02:00
|
|
|
auto cleaned_emoji = remove_emoji_modifiers(pack->emoticon_);
|
2022-02-11 15:46:57 +01:00
|
|
|
if (cleaned_emoji.empty()) {
|
2022-08-07 18:47:50 +02:00
|
|
|
LOG(ERROR) << "Receive empty emoji in " << set_id << "/" << s->short_name_ << " from " << source;
|
2022-02-11 15:46:57 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-12-30 04:35:53 +01:00
|
|
|
vector<FileId> stickers;
|
|
|
|
stickers.reserve(pack->documents_.size());
|
|
|
|
for (int64 document_id : pack->documents_) {
|
|
|
|
auto it = document_id_to_sticker_id.find(document_id);
|
|
|
|
if (it == document_id_to_sticker_id.end()) {
|
2022-08-07 18:47:50 +02:00
|
|
|
LOG(ERROR) << "Can't find document with ID " << document_id << " in " << set_id << "/" << s->short_name_
|
2020-06-06 21:38:47 +02:00
|
|
|
<< " from " << source;
|
2019-12-30 04:35:53 +01:00
|
|
|
continue;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2019-12-30 04:35:53 +01:00
|
|
|
stickers.push_back(it->second);
|
|
|
|
s->sticker_emojis_map_[it->second].push_back(pack->emoticon_);
|
|
|
|
}
|
2022-02-11 15:46:57 +01:00
|
|
|
|
|
|
|
auto &sticker_ids = s->emoji_stickers_map_[cleaned_emoji];
|
|
|
|
for (auto sticker_id : stickers) {
|
|
|
|
if (!td::contains(sticker_ids, sticker_id)) {
|
|
|
|
sticker_ids.push_back(sticker_id);
|
2019-12-30 04:35:53 +01:00
|
|
|
}
|
2018-07-31 22:48:54 +02:00
|
|
|
}
|
|
|
|
}
|
2022-09-26 23:20:13 +02:00
|
|
|
for (auto &keywords : set->keywords_) {
|
|
|
|
auto document_id = keywords->document_id_;
|
|
|
|
auto it = document_id_to_sticker_id.find(document_id);
|
|
|
|
if (it == document_id_to_sticker_id.end()) {
|
|
|
|
LOG(ERROR) << "Can't find document with ID " << document_id << " in " << set_id << "/" << s->short_name_
|
|
|
|
<< " from " << source;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_inserted = s->sticker_keywords_map_.emplace(it->second, std::move(keywords->keyword_)).second;
|
|
|
|
if (!is_inserted) {
|
|
|
|
LOG(ERROR) << "Receive twice document with ID " << document_id << " in " << set_id << "/" << s->short_name_
|
|
|
|
<< " from " << source;
|
|
|
|
}
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-11-14 08:19:03 +01:00
|
|
|
update_sticker_set(s, "on_get_messages_sticker_set 2");
|
2018-12-31 20:04:05 +01:00
|
|
|
update_load_requests(s, true, Status::OK());
|
|
|
|
send_update_installed_sticker_sets();
|
2021-10-05 17:06:05 +02:00
|
|
|
|
|
|
|
if (set_id == add_special_sticker_set(SpecialStickerSetType::animated_emoji()).id_) {
|
|
|
|
try_update_animated_emoji_messages();
|
|
|
|
}
|
2022-07-22 14:01:44 +02:00
|
|
|
if (set_id == add_special_sticker_set(SpecialStickerSetType::premium_gifts()).id_) {
|
|
|
|
try_update_premium_gift_messages();
|
|
|
|
}
|
2021-10-05 17:06:05 +02:00
|
|
|
|
2019-11-21 20:18:29 +01:00
|
|
|
return set_id;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2019-09-18 05:55:43 +02:00
|
|
|
void StickersManager::on_load_sticker_set_fail(StickerSetId sticker_set_id, const Status &error) {
|
|
|
|
if (!sticker_set_id.is_valid()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
update_load_requests(get_sticker_set(sticker_set_id), true, error);
|
|
|
|
}
|
|
|
|
|
|
|
|
void StickersManager::update_load_requests(StickerSet *sticker_set, bool with_stickers, const Status &status) {
|
|
|
|
if (sticker_set == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (with_stickers) {
|
2022-08-07 18:47:50 +02:00
|
|
|
for (auto load_request_id : sticker_set->load_requests_) {
|
2018-12-31 20:04:05 +01:00
|
|
|
update_load_request(load_request_id, status);
|
|
|
|
}
|
|
|
|
|
2022-08-07 18:47:50 +02:00
|
|
|
sticker_set->load_requests_.clear();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2022-08-07 18:47:50 +02:00
|
|
|
for (auto load_request_id : sticker_set->load_without_stickers_requests_) {
|
2018-12-31 20:04:05 +01:00
|
|
|
update_load_request(load_request_id, status);
|
|
|
|
}
|
|
|
|
|
2022-08-07 18:47:50 +02:00
|
|
|
sticker_set->load_without_stickers_requests_.clear();
|
2019-06-17 19:09:45 +02:00
|
|
|
|
|
|
|
if (status.message() == "STICKERSET_INVALID") {
|
|
|
|
// the sticker set is likely to be deleted
|
|
|
|
// clear short_name_to_sticker_set_id_ to allow next searchStickerSet request to succeed
|
2022-08-07 18:47:50 +02:00
|
|
|
short_name_to_sticker_set_id_.erase(clean_username(sticker_set->short_name_));
|
2019-06-17 19:09:45 +02:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void StickersManager::update_load_request(uint32 load_request_id, const Status &status) {
|
|
|
|
auto it = sticker_set_load_requests_.find(load_request_id);
|
|
|
|
CHECK(it != sticker_set_load_requests_.end());
|
2022-08-07 18:47:50 +02:00
|
|
|
CHECK(it->second.left_queries_ > 0);
|
|
|
|
if (status.is_error() && it->second.error_.is_ok()) {
|
|
|
|
it->second.error_ = status.clone();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2022-08-07 18:47:50 +02:00
|
|
|
if (--it->second.left_queries_ == 0) {
|
|
|
|
if (it->second.error_.is_ok()) {
|
|
|
|
it->second.promise_.set_value(Unit());
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
2022-08-07 18:47:50 +02:00
|
|
|
it->second.promise_.set_error(std::move(it->second.error_));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
sticker_set_load_requests_.erase(it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-19 23:41:26 +02:00
|
|
|
void StickersManager::on_get_special_sticker_set(const SpecialStickerSetType &type, StickerSetId sticker_set_id) {
|
2019-11-21 20:18:29 +01:00
|
|
|
auto s = get_sticker_set(sticker_set_id);
|
|
|
|
CHECK(s != nullptr);
|
2022-08-07 18:47:50 +02:00
|
|
|
CHECK(s->is_inited_);
|
|
|
|
CHECK(s->is_loaded_);
|
2019-11-21 20:18:29 +01:00
|
|
|
|
2022-08-07 18:47:50 +02:00
|
|
|
LOG(INFO) << "Receive special sticker set " << type.type_ << ": " << sticker_set_id << ' ' << s->access_hash_ << ' '
|
|
|
|
<< s->short_name_;
|
2021-12-01 17:59:45 +01:00
|
|
|
auto &sticker_set = add_special_sticker_set(type);
|
2022-08-07 18:47:50 +02:00
|
|
|
if (sticker_set_id == sticker_set.id_ && s->access_hash_ == sticker_set.access_hash_ &&
|
|
|
|
s->short_name_ == sticker_set.short_name_ && !s->short_name_.empty()) {
|
2020-04-19 23:41:26 +02:00
|
|
|
on_load_special_sticker_set(type, Status::OK());
|
2019-11-21 20:18:29 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-03-14 01:47:19 +01:00
|
|
|
sticker_set.id_ = sticker_set_id;
|
2022-08-07 18:47:50 +02:00
|
|
|
sticker_set.access_hash_ = s->access_hash_;
|
|
|
|
sticker_set.short_name_ = clean_username(s->short_name_);
|
2020-04-19 15:30:04 +02:00
|
|
|
sticker_set.type_ = type;
|
2019-11-21 20:18:29 +01:00
|
|
|
|
2022-11-01 16:38:07 +01:00
|
|
|
if (!td_->auth_manager_->is_bot()) {
|
|
|
|
G()->td_db()->get_binlog_pmc()->set(type.type_, PSTRING()
|
|
|
|
<< sticker_set.id_.get() << ' ' << sticker_set.access_hash_
|
|
|
|
<< ' ' << sticker_set.short_name_);
|
|
|
|
}
|
|
|
|
|
2022-07-22 14:40:24 +02:00
|
|
|
sticker_set.is_being_loaded_ = true;
|
2020-04-19 23:41:26 +02:00
|
|
|
on_load_special_sticker_set(type, Status::OK());
|
2019-11-21 20:18:29 +01:00
|
|
|
}
|
|
|
|
|
2022-09-12 00:35:47 +02:00
|
|
|
td_api::object_ptr<td_api::updateActiveEmojiReactions> StickersManager::get_update_active_emoji_reactions_object()
|
|
|
|
const {
|
|
|
|
return td_api::make_object<td_api::updateActiveEmojiReactions>(vector<string>(active_reactions_));
|
|
|
|
}
|
|
|
|
|
|
|
|
void StickersManager::save_active_reactions() {
|
2022-11-03 09:17:43 +01:00
|
|
|
LOG(INFO) << "Save " << active_reactions_.size() << " active reactions";
|
2022-09-12 00:35:47 +02:00
|
|
|
G()->td_db()->get_binlog_pmc()->set("active_reactions", log_event_store(active_reactions_).as_slice().str());
|
2022-01-19 12:14:21 +01:00
|
|
|
}
|
|
|
|
|
2022-01-24 14:43:38 +01:00
|
|
|
void StickersManager::save_reactions() {
|
2022-11-03 09:17:43 +01:00
|
|
|
LOG(INFO) << "Save " << reactions_.reactions_.size() << " available reactions";
|
2022-10-19 17:06:33 +02:00
|
|
|
are_reactions_loaded_from_database_ = true;
|
2022-01-24 14:43:38 +01:00
|
|
|
G()->td_db()->get_binlog_pmc()->set("reactions", log_event_store(reactions_).as_slice().str());
|
|
|
|
}
|
|
|
|
|
2022-09-12 18:14:02 +02:00
|
|
|
void StickersManager::save_recent_reactions() {
|
2022-11-03 09:17:43 +01:00
|
|
|
LOG(INFO) << "Save " << recent_reactions_.reactions_.size() << " recent reactions";
|
2022-10-19 17:06:33 +02:00
|
|
|
are_recent_reactions_loaded_from_database_ = true;
|
2022-09-12 18:14:02 +02:00
|
|
|
G()->td_db()->get_binlog_pmc()->set("recent_reactions", log_event_store(recent_reactions_).as_slice().str());
|
|
|
|
}
|
|
|
|
|
2022-09-12 16:28:58 +02:00
|
|
|
void StickersManager::save_top_reactions() {
|
2022-11-03 09:17:43 +01:00
|
|
|
LOG(INFO) << "Save " << top_reactions_.reactions_.size() << " top reactions";
|
2022-10-19 17:06:33 +02:00
|
|
|
are_top_reactions_loaded_from_database_ = true;
|
2022-09-12 16:28:58 +02:00
|
|
|
G()->td_db()->get_binlog_pmc()->set("top_reactions", log_event_store(top_reactions_).as_slice().str());
|
|
|
|
}
|
|
|
|
|
2022-09-12 00:35:47 +02:00
|
|
|
void StickersManager::load_active_reactions() {
|
2022-11-03 09:17:43 +01:00
|
|
|
LOG(INFO) << "Loading active reactions";
|
2022-09-12 00:35:47 +02:00
|
|
|
string active_reactions = G()->td_db()->get_binlog_pmc()->get("active_reactions");
|
|
|
|
if (active_reactions.empty()) {
|
|
|
|
return reload_reactions();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto status = log_event_parse(active_reactions_, active_reactions);
|
|
|
|
if (status.is_error()) {
|
|
|
|
LOG(ERROR) << "Can't load active reactions: " << status;
|
|
|
|
active_reactions_ = {};
|
|
|
|
return reload_reactions();
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG(INFO) << "Successfully loaded " << active_reactions_.size() << " active reactions";
|
2022-09-12 20:39:23 +02:00
|
|
|
|
|
|
|
td_->messages_manager_->set_active_reactions(vector<string>(active_reactions_));
|
|
|
|
|
2022-09-12 00:35:47 +02:00
|
|
|
send_closure(G()->td(), &Td::send_update, get_update_active_emoji_reactions_object());
|
|
|
|
}
|
|
|
|
|
2022-01-24 14:43:38 +01:00
|
|
|
void StickersManager::load_reactions() {
|
2022-09-12 14:03:35 +02:00
|
|
|
if (are_reactions_loaded_from_database_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
are_reactions_loaded_from_database_ = true;
|
|
|
|
|
2022-11-03 09:17:43 +01:00
|
|
|
LOG(INFO) << "Loading available reactions";
|
2022-01-24 14:43:38 +01:00
|
|
|
string reactions = G()->td_db()->get_binlog_pmc()->get("reactions");
|
|
|
|
if (reactions.empty()) {
|
|
|
|
return reload_reactions();
|
|
|
|
}
|
|
|
|
|
2022-09-12 14:03:35 +02:00
|
|
|
auto new_reactions = reactions_;
|
|
|
|
auto status = log_event_parse(new_reactions, reactions);
|
2022-01-24 14:43:38 +01:00
|
|
|
if (status.is_error()) {
|
|
|
|
LOG(ERROR) << "Can't load available reactions: " << status;
|
|
|
|
return reload_reactions();
|
|
|
|
}
|
2022-09-12 14:03:35 +02:00
|
|
|
for (auto &reaction : new_reactions.reactions_) {
|
2022-02-09 22:59:52 +01:00
|
|
|
if (!reaction.is_valid()) {
|
|
|
|
LOG(ERROR) << "Loaded invalid reaction";
|
|
|
|
return reload_reactions();
|
|
|
|
}
|
|
|
|
}
|
2022-09-12 14:03:35 +02:00
|
|
|
reactions_ = std::move(new_reactions);
|
2022-01-24 14:43:38 +01:00
|
|
|
|
|
|
|
LOG(INFO) << "Successfully loaded " << reactions_.reactions_.size() << " available reactions";
|
|
|
|
|
|
|
|
update_active_reactions();
|
|
|
|
}
|
|
|
|
|
2022-09-12 18:14:02 +02:00
|
|
|
void StickersManager::load_recent_reactions() {
|
|
|
|
if (are_recent_reactions_loaded_from_database_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
are_recent_reactions_loaded_from_database_ = true;
|
|
|
|
|
2022-11-03 09:17:43 +01:00
|
|
|
LOG(INFO) << "Loading recent reactions";
|
2022-09-12 18:14:02 +02:00
|
|
|
string recent_reactions = G()->td_db()->get_binlog_pmc()->get("recent_reactions");
|
|
|
|
if (recent_reactions.empty()) {
|
|
|
|
return reload_recent_reactions();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto status = log_event_parse(recent_reactions_, recent_reactions);
|
|
|
|
if (status.is_error()) {
|
|
|
|
LOG(ERROR) << "Can't load recent reactions: " << status;
|
|
|
|
recent_reactions_ = {};
|
|
|
|
return reload_recent_reactions();
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG(INFO) << "Successfully loaded " << recent_reactions_.reactions_.size() << " recent reactions";
|
|
|
|
}
|
|
|
|
|
2022-09-12 16:28:58 +02:00
|
|
|
void StickersManager::load_top_reactions() {
|
|
|
|
if (are_top_reactions_loaded_from_database_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
are_top_reactions_loaded_from_database_ = true;
|
|
|
|
|
2022-11-03 09:17:43 +01:00
|
|
|
LOG(INFO) << "Loading top reactions";
|
2022-09-12 16:28:58 +02:00
|
|
|
string top_reactions = G()->td_db()->get_binlog_pmc()->get("top_reactions");
|
|
|
|
if (top_reactions.empty()) {
|
|
|
|
return reload_top_reactions();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto status = log_event_parse(top_reactions_, top_reactions);
|
|
|
|
if (status.is_error()) {
|
|
|
|
LOG(ERROR) << "Can't load top reactions: " << status;
|
|
|
|
top_reactions_ = {};
|
|
|
|
return reload_top_reactions();
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG(INFO) << "Successfully loaded " << top_reactions_.reactions_.size() << " top reactions";
|
|
|
|
}
|
|
|
|
|
2022-01-24 14:43:38 +01:00
|
|
|
void StickersManager::update_active_reactions() {
|
2022-09-08 16:45:19 +02:00
|
|
|
vector<string> active_reactions;
|
2022-01-24 14:43:38 +01:00
|
|
|
for (auto &reaction : reactions_.reactions_) {
|
|
|
|
if (reaction.is_active_) {
|
2022-08-24 22:31:15 +02:00
|
|
|
active_reactions.emplace_back(reaction.reaction_);
|
2022-01-24 14:43:38 +01:00
|
|
|
}
|
|
|
|
}
|
2022-09-12 00:35:47 +02:00
|
|
|
if (active_reactions == active_reactions_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
active_reactions_ = active_reactions;
|
|
|
|
|
|
|
|
save_active_reactions();
|
|
|
|
|
|
|
|
send_closure(G()->td(), &Td::send_update, get_update_active_emoji_reactions_object());
|
|
|
|
|
2022-01-24 14:43:38 +01:00
|
|
|
td_->messages_manager_->set_active_reactions(std::move(active_reactions));
|
|
|
|
}
|
|
|
|
|
2022-01-19 12:14:21 +01:00
|
|
|
void StickersManager::on_get_available_reactions(
|
|
|
|
tl_object_ptr<telegram_api::messages_AvailableReactions> &&available_reactions_ptr) {
|
|
|
|
CHECK(reactions_.are_being_reloaded_);
|
|
|
|
reactions_.are_being_reloaded_ = false;
|
|
|
|
|
|
|
|
if (available_reactions_ptr == nullptr) {
|
|
|
|
// failed to get available reactions
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32 constructor_id = available_reactions_ptr->get_id();
|
|
|
|
if (constructor_id == telegram_api::messages_availableReactionsNotModified::ID) {
|
|
|
|
LOG(INFO) << "Available reactions are not modified";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
CHECK(constructor_id == telegram_api::messages_availableReactions::ID);
|
|
|
|
auto available_reactions = move_tl_object_as<telegram_api::messages_availableReactions>(available_reactions_ptr);
|
|
|
|
vector<Reaction> new_reactions;
|
|
|
|
for (auto &available_reaction : available_reactions->reactions_) {
|
|
|
|
Reaction reaction;
|
|
|
|
reaction.is_active_ = !available_reaction->inactive_;
|
2022-04-26 13:40:06 +02:00
|
|
|
reaction.is_premium_ = available_reaction->premium_;
|
2022-01-19 12:14:21 +01:00
|
|
|
reaction.reaction_ = std::move(available_reaction->reaction_);
|
|
|
|
reaction.title_ = std::move(available_reaction->title_);
|
|
|
|
reaction.static_icon_ =
|
|
|
|
on_get_sticker_document(std::move(available_reaction->static_icon_), StickerFormat::Webp).second;
|
|
|
|
reaction.appear_animation_ =
|
|
|
|
on_get_sticker_document(std::move(available_reaction->appear_animation_), StickerFormat::Tgs).second;
|
|
|
|
reaction.select_animation_ =
|
|
|
|
on_get_sticker_document(std::move(available_reaction->select_animation_), StickerFormat::Tgs).second;
|
|
|
|
reaction.activate_animation_ =
|
|
|
|
on_get_sticker_document(std::move(available_reaction->activate_animation_), StickerFormat::Tgs).second;
|
|
|
|
reaction.effect_animation_ =
|
|
|
|
on_get_sticker_document(std::move(available_reaction->effect_animation_), StickerFormat::Tgs).second;
|
|
|
|
reaction.around_animation_ =
|
|
|
|
on_get_sticker_document(std::move(available_reaction->around_animation_), StickerFormat::Tgs).second;
|
|
|
|
reaction.center_animation_ =
|
|
|
|
on_get_sticker_document(std::move(available_reaction->center_icon_), StickerFormat::Tgs).second;
|
2022-02-09 22:59:52 +01:00
|
|
|
|
|
|
|
if (!reaction.is_valid()) {
|
2022-01-19 12:14:21 +01:00
|
|
|
LOG(ERROR) << "Receive invalid reaction " << reaction.reaction_;
|
|
|
|
continue;
|
|
|
|
}
|
2022-08-24 22:31:15 +02:00
|
|
|
if (reaction.is_premium_) {
|
|
|
|
LOG(ERROR) << "Receive premium reaction " << reaction.reaction_;
|
|
|
|
continue;
|
|
|
|
}
|
2022-01-19 16:10:16 +01:00
|
|
|
|
2022-01-19 12:14:21 +01:00
|
|
|
new_reactions.push_back(std::move(reaction));
|
|
|
|
}
|
|
|
|
reactions_.reactions_ = std::move(new_reactions);
|
|
|
|
reactions_.hash_ = available_reactions->hash_;
|
2022-01-19 16:59:48 +01:00
|
|
|
|
2022-01-24 14:43:38 +01:00
|
|
|
save_reactions();
|
|
|
|
|
|
|
|
update_active_reactions();
|
2022-01-19 12:14:21 +01:00
|
|
|
}
|
|
|
|
|
2022-09-12 18:14:02 +02:00
|
|
|
void StickersManager::on_get_recent_reactions(tl_object_ptr<telegram_api::messages_Reactions> &&reactions_ptr) {
|
|
|
|
CHECK(recent_reactions_.is_being_reloaded_);
|
|
|
|
recent_reactions_.is_being_reloaded_ = false;
|
|
|
|
|
|
|
|
if (reactions_ptr == nullptr) {
|
|
|
|
// failed to get recent reactions
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32 constructor_id = reactions_ptr->get_id();
|
|
|
|
if (constructor_id == telegram_api::messages_reactionsNotModified::ID) {
|
|
|
|
LOG(INFO) << "Top reactions are not modified";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
CHECK(constructor_id == telegram_api::messages_reactions::ID);
|
|
|
|
auto reactions = move_tl_object_as<telegram_api::messages_reactions>(reactions_ptr);
|
|
|
|
auto new_reactions =
|
|
|
|
transform(reactions->reactions_, [](const telegram_api::object_ptr<telegram_api::Reaction> &reaction) {
|
|
|
|
return get_message_reaction_string(reaction);
|
|
|
|
});
|
|
|
|
if (new_reactions == recent_reactions_.reactions_ && recent_reactions_.hash_ == reactions->hash_) {
|
|
|
|
LOG(INFO) << "Top reactions are not modified";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
recent_reactions_.reactions_ = std::move(new_reactions);
|
|
|
|
recent_reactions_.hash_ = reactions->hash_;
|
|
|
|
|
2022-09-12 20:55:30 +02:00
|
|
|
auto expected_hash = get_reactions_hash(recent_reactions_.reactions_);
|
|
|
|
if (recent_reactions_.hash_ != expected_hash) {
|
|
|
|
LOG(ERROR) << "Receive hash " << recent_reactions_.hash_ << " instead of " << expected_hash << " for reactions "
|
|
|
|
<< recent_reactions_.reactions_;
|
|
|
|
}
|
|
|
|
|
2022-09-12 18:14:02 +02:00
|
|
|
save_recent_reactions();
|
|
|
|
}
|
|
|
|
|
2022-09-12 16:28:58 +02:00
|
|
|
void StickersManager::on_get_top_reactions(tl_object_ptr<telegram_api::messages_Reactions> &&reactions_ptr) {
|
|
|
|
CHECK(top_reactions_.is_being_reloaded_);
|
|
|
|
top_reactions_.is_being_reloaded_ = false;
|
|
|
|
|
|
|
|
if (reactions_ptr == nullptr) {
|
|
|
|
// failed to get top reactions
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32 constructor_id = reactions_ptr->get_id();
|
|
|
|
if (constructor_id == telegram_api::messages_reactionsNotModified::ID) {
|
|
|
|
LOG(INFO) << "Top reactions are not modified";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
CHECK(constructor_id == telegram_api::messages_reactions::ID);
|
|
|
|
auto reactions = move_tl_object_as<telegram_api::messages_reactions>(reactions_ptr);
|
|
|
|
auto new_reactions =
|
|
|
|
transform(reactions->reactions_, [](const telegram_api::object_ptr<telegram_api::Reaction> &reaction) {
|
|
|
|
return get_message_reaction_string(reaction);
|
|
|
|
});
|
|
|
|
if (new_reactions == top_reactions_.reactions_ && top_reactions_.hash_ == reactions->hash_) {
|
|
|
|
LOG(INFO) << "Top reactions are not modified";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
top_reactions_.reactions_ = std::move(new_reactions);
|
|
|
|
top_reactions_.hash_ = reactions->hash_;
|
|
|
|
|
|
|
|
save_top_reactions();
|
|
|
|
}
|
|
|
|
|
2022-07-14 15:17:19 +02:00
|
|
|
void StickersManager::on_get_installed_sticker_sets(StickerType sticker_type,
|
2018-12-31 20:04:05 +01:00
|
|
|
tl_object_ptr<telegram_api::messages_AllStickers> &&stickers_ptr) {
|
2022-07-24 13:01:58 +02:00
|
|
|
auto type = static_cast<int32>(sticker_type);
|
|
|
|
next_installed_sticker_sets_load_time_[type] = Time::now_cached() + Random::fast(30 * 60, 50 * 60);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
CHECK(stickers_ptr != nullptr);
|
|
|
|
int32 constructor_id = stickers_ptr->get_id();
|
|
|
|
if (constructor_id == telegram_api::messages_allStickersNotModified::ID) {
|
2022-07-14 15:17:19 +02:00
|
|
|
LOG(INFO) << sticker_type << " stickers are not modified";
|
2018-12-31 20:04:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
CHECK(constructor_id == telegram_api::messages_allStickers::ID);
|
|
|
|
auto stickers = move_tl_object_as<telegram_api::messages_allStickers>(stickers_ptr);
|
|
|
|
|
2022-03-11 19:38:48 +01:00
|
|
|
FlatHashSet<StickerSetId, StickerSetIdHash> uninstalled_sticker_sets;
|
2022-07-24 13:01:58 +02:00
|
|
|
for (auto &sticker_set_id : installed_sticker_set_ids_[type]) {
|
2022-03-11 19:38:48 +01:00
|
|
|
uninstalled_sticker_sets.insert(sticker_set_id);
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2019-09-18 05:55:43 +02:00
|
|
|
vector<StickerSetId> sets_to_load;
|
|
|
|
vector<StickerSetId> installed_sticker_set_ids;
|
|
|
|
vector<int32> debug_hashes;
|
|
|
|
vector<int64> debug_sticker_set_ids;
|
2018-03-05 23:44:11 +01:00
|
|
|
std::reverse(stickers->sets_.begin(), stickers->sets_.end()); // apply installed sticker sets in reverse order
|
2018-12-31 20:04:05 +01:00
|
|
|
for (auto &set : stickers->sets_) {
|
2019-09-18 05:55:43 +02:00
|
|
|
debug_hashes.push_back(set->hash_);
|
|
|
|
debug_sticker_set_ids.push_back(set->id_);
|
2019-12-29 02:59:34 +01:00
|
|
|
StickerSetId set_id = on_get_sticker_set(std::move(set), false, "on_get_installed_sticker_sets");
|
2019-09-18 05:55:43 +02:00
|
|
|
if (!set_id.is_valid()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto sticker_set = get_sticker_set(set_id);
|
|
|
|
CHECK(sticker_set != nullptr);
|
2022-08-07 18:47:50 +02:00
|
|
|
LOG_IF(ERROR, !sticker_set->is_installed_) << "Receive non-installed sticker set in getAllStickers";
|
|
|
|
LOG_IF(ERROR, sticker_set->is_archived_) << "Receive archived sticker set in getAllStickers";
|
|
|
|
LOG_IF(ERROR, sticker_set->sticker_type_ != sticker_type)
|
|
|
|
<< "Receive sticker set of a wrong type in getAllStickers";
|
|
|
|
CHECK(sticker_set->is_inited_);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2022-08-07 18:47:50 +02:00
|
|
|
if (sticker_set->is_installed_ && !sticker_set->is_archived_ && sticker_set->sticker_type_ == sticker_type) {
|
2018-02-13 21:41:37 +01:00
|
|
|
installed_sticker_set_ids.push_back(set_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
uninstalled_sticker_sets.erase(set_id);
|
|
|
|
}
|
2021-11-14 08:19:03 +01:00
|
|
|
update_sticker_set(sticker_set, "on_get_installed_sticker_sets");
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2022-08-07 18:47:50 +02:00
|
|
|
if (!sticker_set->is_archived_ && !sticker_set->is_loaded_) {
|
2018-12-31 20:04:05 +01:00
|
|
|
sets_to_load.push_back(set_id);
|
|
|
|
}
|
|
|
|
}
|
2019-09-18 05:55:43 +02:00
|
|
|
std::reverse(debug_hashes.begin(), debug_hashes.end());
|
2018-03-05 23:44:11 +01:00
|
|
|
std::reverse(installed_sticker_set_ids.begin(), installed_sticker_set_ids.end());
|
2019-09-18 05:55:43 +02:00
|
|
|
std::reverse(debug_sticker_set_ids.begin(), debug_sticker_set_ids.end());
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
if (!sets_to_load.empty()) {
|
|
|
|
load_sticker_sets(std::move(sets_to_load), Auto());
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto set_id : uninstalled_sticker_sets) {
|
|
|
|
auto sticker_set = get_sticker_set(set_id);
|
|
|
|
CHECK(sticker_set != nullptr);
|
2022-08-07 18:47:50 +02:00
|
|
|
CHECK(sticker_set->is_installed_ && !sticker_set->is_archived_);
|
2018-12-31 20:04:05 +01:00
|
|
|
on_update_sticker_set(sticker_set, false, false, true);
|
2021-11-14 08:19:03 +01:00
|
|
|
update_sticker_set(sticker_set, "on_get_installed_sticker_sets 2");
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2022-07-14 15:17:19 +02:00
|
|
|
on_load_installed_sticker_sets_finished(sticker_type, std::move(installed_sticker_set_ids));
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2022-07-24 13:01:58 +02:00
|
|
|
if (installed_sticker_sets_hash_[type] != stickers->hash_) {
|
2022-01-25 12:58:04 +01:00
|
|
|
LOG(ERROR) << "Sticker sets hash mismatch: server hash list = " << debug_hashes << ", client hash list = "
|
2022-07-24 13:01:58 +02:00
|
|
|
<< transform(installed_sticker_set_ids_[type],
|
2022-08-07 18:47:50 +02:00
|
|
|
[this](StickerSetId sticker_set_id) { return get_sticker_set(sticker_set_id)->hash_; })
|
2022-01-25 12:58:04 +01:00
|
|
|
<< ", server sticker set list = " << debug_sticker_set_ids
|
2022-07-24 13:01:58 +02:00
|
|
|
<< ", client sticker set list = " << installed_sticker_set_ids_[type]
|
|
|
|
<< ", server hash = " << stickers->hash_ << ", client hash = " << installed_sticker_sets_hash_[type];
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-14 15:17:19 +02:00
|
|
|
void StickersManager::on_get_installed_sticker_sets_failed(StickerType sticker_type, Status error) {
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK(error.is_error());
|
2022-07-24 13:01:58 +02:00
|
|
|
auto type = static_cast<int32>(sticker_type);
|
|
|
|
next_installed_sticker_sets_load_time_[type] = Time::now_cached() + Random::fast(5, 10);
|
|
|
|
fail_promises(load_installed_sticker_sets_queries_[type], std::move(error));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2022-09-27 17:29:10 +02:00
|
|
|
const std::map<string, vector<FileId>> &StickersManager::get_sticker_set_keywords(const StickerSet *sticker_set) {
|
2022-09-27 15:16:36 +02:00
|
|
|
if (sticker_set->keyword_stickers_map_.empty()) {
|
|
|
|
for (auto &sticker_id_keywords : sticker_set->sticker_keywords_map_) {
|
|
|
|
for (auto &keyword : Hints::fix_words(transform(sticker_id_keywords.second, utf8_prepare_search_string))) {
|
|
|
|
CHECK(!keyword.empty());
|
|
|
|
sticker_set->keyword_stickers_map_[keyword].push_back(sticker_id_keywords.first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return sticker_set->keyword_stickers_map_;
|
|
|
|
}
|
|
|
|
|
2022-09-27 17:29:10 +02:00
|
|
|
void StickersManager::find_sticker_set_stickers(const StickerSet *sticker_set, const string &query,
|
2022-09-27 19:52:47 +02:00
|
|
|
const string &prepared_query, vector<FileId> &result) {
|
2022-09-27 17:29:10 +02:00
|
|
|
auto it = sticker_set->emoji_stickers_map_.find(query);
|
|
|
|
if (it != sticker_set->emoji_stickers_map_.end()) {
|
|
|
|
LOG(INFO) << "Add " << it->second << " stickers from " << sticker_set->id_;
|
|
|
|
append(result, it->second);
|
|
|
|
}
|
2022-09-27 19:52:47 +02:00
|
|
|
|
|
|
|
if (!prepared_query.empty()) {
|
|
|
|
const auto &keywords_map = get_sticker_set_keywords(sticker_set);
|
|
|
|
auto keywords_it = keywords_map.lower_bound(prepared_query);
|
|
|
|
if (keywords_it != keywords_map.end() && begins_with(keywords_it->first, prepared_query)) {
|
|
|
|
FlatHashSet<FileId, FileIdHash> found_sticker_ids;
|
|
|
|
if (it != sticker_set->emoji_stickers_map_.end()) {
|
|
|
|
for (auto file_id : it->second) {
|
|
|
|
found_sticker_ids.insert(file_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
do {
|
|
|
|
for (auto file_id : keywords_it->second) {
|
|
|
|
if (found_sticker_ids.insert(file_id).second) {
|
|
|
|
result.push_back(file_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
++keywords_it;
|
|
|
|
} while (keywords_it != keywords_map.end() && begins_with(keywords_it->first, prepared_query));
|
|
|
|
}
|
|
|
|
}
|
2022-09-27 17:29:10 +02:00
|
|
|
}
|
|
|
|
|
2022-09-27 19:52:47 +02:00
|
|
|
bool StickersManager::can_found_sticker_by_query(FileId sticker_id, const string &query,
|
|
|
|
const string &prepared_query) const {
|
2022-09-27 17:29:10 +02:00
|
|
|
const Sticker *s = get_sticker(sticker_id);
|
|
|
|
CHECK(s != nullptr);
|
|
|
|
if (remove_emoji_modifiers(s->alt_) == query) {
|
|
|
|
// fast path
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
const StickerSet *sticker_set = get_sticker_set(s->set_id_);
|
|
|
|
if (sticker_set == nullptr || !sticker_set->was_loaded_) {
|
|
|
|
return false;
|
|
|
|
}
|
2022-09-27 19:52:47 +02:00
|
|
|
auto it = sticker_set->emoji_stickers_map_.find(query);
|
|
|
|
if (it != sticker_set->emoji_stickers_map_.end()) {
|
|
|
|
if (td::contains(it->second, sticker_id)) {
|
2022-09-27 17:29:10 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2022-09-27 19:52:47 +02:00
|
|
|
|
|
|
|
if (!prepared_query.empty()) {
|
|
|
|
const auto &keywords_map = get_sticker_set_keywords(sticker_set);
|
|
|
|
auto keywords_it = keywords_map.lower_bound(prepared_query);
|
|
|
|
if (keywords_it != keywords_map.end() && begins_with(keywords_it->first, prepared_query)) {
|
|
|
|
do {
|
|
|
|
if (td::contains(keywords_it->second, sticker_id)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
++keywords_it;
|
|
|
|
} while (keywords_it != keywords_map.end() && begins_with(keywords_it->first, prepared_query));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-27 17:29:10 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-06-17 07:59:54 +02:00
|
|
|
std::pair<vector<FileId>, vector<FileId>> StickersManager::split_stickers_by_premium(
|
|
|
|
const vector<FileId> &sticker_ids) const {
|
2022-08-08 20:13:22 +02:00
|
|
|
CHECK(!td_->auth_manager_->is_bot());
|
2022-06-16 20:12:20 +02:00
|
|
|
vector<FileId> regular_sticker_ids;
|
|
|
|
vector<FileId> premium_sticker_ids;
|
|
|
|
for (const auto &sticker_id : sticker_ids) {
|
|
|
|
if (sticker_id.is_valid()) {
|
|
|
|
const Sticker *s = get_sticker(sticker_id);
|
|
|
|
CHECK(s != nullptr);
|
2022-08-07 18:47:50 +02:00
|
|
|
if (s->is_premium_) {
|
2022-06-16 20:12:20 +02:00
|
|
|
premium_sticker_ids.push_back(sticker_id);
|
|
|
|
} else {
|
|
|
|
regular_sticker_ids.push_back(sticker_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return {std::move(regular_sticker_ids), std::move(premium_sticker_ids)};
|
|
|
|
}
|
|
|
|
|
2022-08-07 20:10:15 +02:00
|
|
|
std::pair<vector<FileId>, vector<FileId>> StickersManager::split_stickers_by_premium(
|
|
|
|
const StickerSet *sticker_set) const {
|
2022-08-08 20:13:22 +02:00
|
|
|
CHECK(!td_->auth_manager_->is_bot());
|
2022-08-07 20:10:15 +02:00
|
|
|
if (!sticker_set->was_loaded_) {
|
|
|
|
return split_stickers_by_premium(sticker_set->sticker_ids_);
|
|
|
|
}
|
|
|
|
if (sticker_set->premium_sticker_positions_.empty()) {
|
|
|
|
return {sticker_set->sticker_ids_, {}};
|
|
|
|
}
|
|
|
|
vector<FileId> regular_sticker_ids;
|
|
|
|
vector<FileId> premium_sticker_ids;
|
|
|
|
size_t premium_pos = 0;
|
|
|
|
for (size_t i = 0; i < sticker_set->sticker_ids_.size(); i++) {
|
|
|
|
if (premium_pos < sticker_set->premium_sticker_positions_.size() &&
|
|
|
|
static_cast<size_t>(sticker_set->premium_sticker_positions_[premium_pos]) == i) {
|
|
|
|
premium_sticker_ids.push_back(sticker_set->sticker_ids_[i]);
|
|
|
|
premium_pos++;
|
|
|
|
} else {
|
|
|
|
regular_sticker_ids.push_back(sticker_set->sticker_ids_[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CHECK(premium_pos == sticker_set->premium_sticker_positions_.size());
|
|
|
|
return {std::move(regular_sticker_ids), std::move(premium_sticker_ids)};
|
|
|
|
}
|
|
|
|
|
2022-09-27 19:52:47 +02:00
|
|
|
vector<FileId> StickersManager::get_stickers(StickerType sticker_type, string query, int32 limit, DialogId dialog_id,
|
2022-08-05 13:06:06 +02:00
|
|
|
bool force, Promise<Unit> &&promise) {
|
2022-08-06 22:39:54 +02:00
|
|
|
if (G()->close_flag()) {
|
2022-08-10 13:04:36 +02:00
|
|
|
promise.set_error(Global::request_aborted_error());
|
2022-08-06 22:39:54 +02:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
if (limit <= 0) {
|
2021-09-24 09:59:51 +02:00
|
|
|
promise.set_error(Status::Error(400, "Parameter limit must be positive"));
|
2018-12-31 20:04:05 +01:00
|
|
|
return {};
|
|
|
|
}
|
2022-08-06 22:39:54 +02:00
|
|
|
|
2022-07-24 13:53:48 +02:00
|
|
|
auto type = static_cast<int32>(sticker_type);
|
2022-07-24 13:01:58 +02:00
|
|
|
if (!are_installed_sticker_sets_loaded_[type]) {
|
2022-08-06 22:39:54 +02:00
|
|
|
CHECK(force == false);
|
|
|
|
load_installed_sticker_sets(
|
|
|
|
sticker_type,
|
2022-09-27 19:52:47 +02:00
|
|
|
PromiseCreator::lambda([actor_id = actor_id(this), sticker_type, query = std::move(query), limit, dialog_id,
|
2022-08-06 22:39:54 +02:00
|
|
|
force, promise = std::move(promise)](Result<Unit> result) mutable {
|
|
|
|
if (result.is_error()) {
|
|
|
|
promise.set_error(result.move_as_error());
|
|
|
|
} else {
|
2022-09-27 19:52:47 +02:00
|
|
|
send_closure(actor_id, &StickersManager::get_stickers, sticker_type, std::move(query), limit, dialog_id,
|
2022-08-10 17:38:30 +02:00
|
|
|
force, std::move(promise));
|
2022-08-06 22:39:54 +02:00
|
|
|
}
|
|
|
|
}));
|
2018-12-31 20:04:05 +01:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-09-27 19:52:47 +02:00
|
|
|
remove_emoji_modifiers_in_place(query);
|
|
|
|
if (!query.empty()) {
|
2022-08-01 15:56:21 +02:00
|
|
|
if (sticker_type == StickerType::Regular) {
|
|
|
|
if (!are_recent_stickers_loaded_[0 /*is_attached*/]) {
|
|
|
|
load_recent_stickers(false, std::move(promise));
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
if (!are_favorite_stickers_loaded_) {
|
|
|
|
load_favorite_stickers(std::move(promise));
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
} else if (sticker_type == StickerType::CustomEmoji) {
|
|
|
|
if (!are_featured_sticker_sets_loaded_[type]) {
|
|
|
|
load_featured_sticker_sets(sticker_type, std::move(promise));
|
|
|
|
return {};
|
|
|
|
}
|
2018-03-09 16:00:07 +01:00
|
|
|
}
|
2022-08-01 15:56:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
vector<StickerSetId> examined_sticker_set_ids = installed_sticker_set_ids_[type];
|
2022-09-27 19:52:47 +02:00
|
|
|
if (!query.empty() && sticker_type == StickerType::CustomEmoji) {
|
2022-08-12 17:39:36 +02:00
|
|
|
append(examined_sticker_set_ids, featured_sticker_set_ids_[type]);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2019-09-18 05:55:43 +02:00
|
|
|
vector<StickerSetId> sets_to_load;
|
2018-12-31 20:04:05 +01:00
|
|
|
bool need_load = false;
|
2022-08-01 15:56:21 +02:00
|
|
|
for (const auto &sticker_set_id : examined_sticker_set_ids) {
|
2018-12-31 20:04:05 +01:00
|
|
|
const StickerSet *sticker_set = get_sticker_set(sticker_set_id);
|
|
|
|
CHECK(sticker_set != nullptr);
|
2022-08-07 18:47:50 +02:00
|
|
|
CHECK(sticker_set->is_inited_);
|
|
|
|
if (!sticker_set->is_loaded_) {
|
2018-12-31 20:04:05 +01:00
|
|
|
sets_to_load.push_back(sticker_set_id);
|
2022-08-07 18:47:50 +02:00
|
|
|
if (!sticker_set->was_loaded_) {
|
2018-12-31 20:04:05 +01:00
|
|
|
need_load = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
vector<FileId> prepend_sticker_ids;
|
2022-09-27 19:52:47 +02:00
|
|
|
if (!query.empty() && sticker_type == StickerType::Regular) {
|
2018-12-31 20:04:05 +01:00
|
|
|
prepend_sticker_ids.reserve(favorite_sticker_ids_.size() + recent_sticker_ids_[0].size());
|
2018-03-09 16:00:07 +01:00
|
|
|
append(prepend_sticker_ids, recent_sticker_ids_[0]);
|
|
|
|
for (auto sticker_id : favorite_sticker_ids_) {
|
2019-10-22 01:12:58 +02:00
|
|
|
if (!td::contains(prepend_sticker_ids, sticker_id)) {
|
2018-12-31 20:04:05 +01:00
|
|
|
prepend_sticker_ids.push_back(sticker_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-26 01:54:35 +02:00
|
|
|
auto prefer_animated = [this](FileId lhs, FileId rhs) {
|
|
|
|
const Sticker *lhs_s = get_sticker(lhs);
|
|
|
|
const Sticker *rhs_s = get_sticker(rhs);
|
2021-11-14 07:05:33 +01:00
|
|
|
CHECK(lhs_s != nullptr && rhs_s != nullptr);
|
2022-08-07 18:47:50 +02:00
|
|
|
return is_sticker_format_animated(lhs_s->format_) && !is_sticker_format_animated(rhs_s->format_);
|
2019-07-26 01:54:35 +02:00
|
|
|
};
|
2019-09-03 20:41:11 +02:00
|
|
|
// std::stable_sort(prepend_sticker_ids.begin(), prepend_sticker_ids.begin() + recent_sticker_ids_[0].size(),
|
|
|
|
// prefer_animated);
|
2019-07-26 01:54:35 +02:00
|
|
|
std::stable_sort(prepend_sticker_ids.begin() + recent_sticker_ids_[0].size(), prepend_sticker_ids.end(),
|
|
|
|
prefer_animated);
|
|
|
|
|
2018-07-31 19:48:38 +02:00
|
|
|
LOG(INFO) << "Have " << recent_sticker_ids_[0] << " recent and " << favorite_sticker_ids_ << " favorite stickers";
|
2018-02-13 21:41:37 +01:00
|
|
|
for (const auto &sticker_id : prepend_sticker_ids) {
|
2018-12-31 20:04:05 +01:00
|
|
|
const Sticker *s = get_sticker(sticker_id);
|
2021-11-14 07:05:33 +01:00
|
|
|
CHECK(s != nullptr);
|
2022-08-07 18:47:50 +02:00
|
|
|
LOG(INFO) << "Have prepend sticker " << sticker_id << " from " << s->set_id_;
|
|
|
|
if (s->set_id_.is_valid() && !td::contains(sets_to_load, s->set_id_)) {
|
|
|
|
const StickerSet *sticker_set = get_sticker_set(s->set_id_);
|
|
|
|
if (sticker_set == nullptr || !sticker_set->is_loaded_) {
|
|
|
|
sets_to_load.push_back(s->set_id_);
|
|
|
|
if (sticker_set == nullptr || !sticker_set->was_loaded_) {
|
2018-12-31 20:04:05 +01:00
|
|
|
need_load = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!sets_to_load.empty()) {
|
|
|
|
if (need_load && !force) {
|
|
|
|
load_sticker_sets(std::move(sets_to_load),
|
|
|
|
PromiseCreator::lambda([promise = std::move(promise)](Result<Unit> result) mutable {
|
2018-01-25 17:39:10 +01:00
|
|
|
if (result.is_error() && result.error().message() != "STICKERSET_INVALID") {
|
2018-12-31 20:04:05 +01:00
|
|
|
LOG(ERROR) << "Failed to load sticker sets: " << result.error();
|
|
|
|
}
|
|
|
|
promise.set_value(Unit());
|
|
|
|
}));
|
|
|
|
return {};
|
|
|
|
} else {
|
|
|
|
load_sticker_sets(std::move(sets_to_load), Auto());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-05 13:06:06 +02:00
|
|
|
bool allow_premium = false;
|
|
|
|
if (sticker_type == StickerType::CustomEmoji) {
|
|
|
|
switch (dialog_id.get_type()) {
|
|
|
|
case DialogType::User:
|
|
|
|
if (dialog_id.get_user_id() == td_->contacts_manager_->get_my_id()) {
|
|
|
|
allow_premium = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case DialogType::SecretChat:
|
|
|
|
if (td_->contacts_manager_->get_secret_chat_layer(dialog_id.get_secret_chat_id()) <
|
|
|
|
static_cast<int32>(SecretChatLayer::SpoilerAndCustomEmojiEntities)) {
|
|
|
|
promise.set_value(Unit());
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
vector<FileId> result;
|
|
|
|
auto limit_size_t = static_cast<size_t>(limit);
|
2022-09-27 19:52:47 +02:00
|
|
|
if (query.empty()) {
|
2022-08-01 15:56:21 +02:00
|
|
|
for (const auto &sticker_set_id : examined_sticker_set_ids) {
|
2018-12-31 20:04:05 +01:00
|
|
|
const StickerSet *sticker_set = get_sticker_set(sticker_set_id);
|
2022-08-07 18:47:50 +02:00
|
|
|
if (sticker_set == nullptr || !sticker_set->was_loaded_) {
|
2018-12-31 20:04:05 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-08-07 18:47:50 +02:00
|
|
|
append(result, sticker_set->sticker_ids_);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (result.size() > limit_size_t) {
|
|
|
|
result.resize(limit_size_t);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2022-09-27 19:52:47 +02:00
|
|
|
auto prepared_query = utf8_prepare_search_string(query);
|
|
|
|
LOG(INFO) << "Search stickers by " << query << " and keyword " << prepared_query;
|
2019-07-26 01:54:35 +02:00
|
|
|
vector<const StickerSet *> examined_sticker_sets;
|
2022-08-01 15:56:21 +02:00
|
|
|
for (const auto &sticker_set_id : examined_sticker_set_ids) {
|
2018-12-31 20:04:05 +01:00
|
|
|
const StickerSet *sticker_set = get_sticker_set(sticker_set_id);
|
2022-08-07 18:47:50 +02:00
|
|
|
if (sticker_set == nullptr || !sticker_set->was_loaded_) {
|
2018-12-31 20:04:05 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-10-22 01:12:58 +02:00
|
|
|
if (!td::contains(examined_sticker_sets, sticker_set)) {
|
2019-07-26 01:54:35 +02:00
|
|
|
examined_sticker_sets.push_back(sticker_set);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
std::stable_sort(
|
2022-01-18 17:20:43 +01:00
|
|
|
examined_sticker_sets.begin(), examined_sticker_sets.end(), [](const StickerSet *lhs, const StickerSet *rhs) {
|
2022-08-07 18:47:50 +02:00
|
|
|
if (lhs->is_installed_ != rhs->is_installed_) {
|
|
|
|
return lhs->is_installed_;
|
2022-08-01 15:56:21 +02:00
|
|
|
}
|
2022-08-07 18:47:50 +02:00
|
|
|
if (lhs->is_archived_ != rhs->is_archived_) {
|
|
|
|
return lhs->is_archived_;
|
2022-08-01 15:56:21 +02:00
|
|
|
}
|
2022-08-07 18:47:50 +02:00
|
|
|
return is_sticker_format_animated(lhs->sticker_format_) && !is_sticker_format_animated(rhs->sticker_format_);
|
2022-01-18 17:20:43 +01:00
|
|
|
});
|
2019-07-26 01:54:35 +02:00
|
|
|
for (auto sticker_set : examined_sticker_sets) {
|
2022-09-27 19:52:47 +02:00
|
|
|
find_sticker_set_stickers(sticker_set, query, prepared_query, result);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
vector<FileId> sorted;
|
2018-02-12 11:37:54 +01:00
|
|
|
sorted.reserve(min(limit_size_t, result.size()));
|
2018-03-09 16:00:07 +01:00
|
|
|
auto recent_stickers_size = recent_sticker_ids_[0].size();
|
|
|
|
const size_t MAX_RECENT_STICKERS = 5;
|
|
|
|
for (size_t i = 0; i < prepend_sticker_ids.size(); i++) {
|
|
|
|
if (sorted.size() == MAX_RECENT_STICKERS && i < recent_stickers_size) {
|
2018-07-31 19:17:46 +02:00
|
|
|
LOG(INFO) << "Skip recent sticker " << prepend_sticker_ids[i];
|
2018-03-09 16:00:07 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto sticker_id = prepend_sticker_ids[i];
|
2018-12-31 20:04:05 +01:00
|
|
|
bool is_good = false;
|
|
|
|
auto it = std::find(result.begin(), result.end(), sticker_id);
|
|
|
|
if (it != result.end()) {
|
2018-07-31 19:48:38 +02:00
|
|
|
LOG(INFO) << "Found prepend sticker " << sticker_id << " in installed packs at position "
|
|
|
|
<< (it - result.begin());
|
2018-12-31 20:04:05 +01:00
|
|
|
*it = FileId();
|
|
|
|
is_good = true;
|
2022-09-27 19:52:47 +02:00
|
|
|
} else if (can_found_sticker_by_query(sticker_id, query, prepared_query)) {
|
|
|
|
LOG(INFO) << "Found prepend sticker " << sticker_id;
|
2022-09-27 17:29:10 +02:00
|
|
|
is_good = true;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (is_good) {
|
|
|
|
sorted.push_back(sticker_id);
|
|
|
|
if (sorted.size() == limit_size_t) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (sorted.size() != limit_size_t) {
|
2022-06-16 20:12:20 +02:00
|
|
|
vector<FileId> regular_sticker_ids;
|
2022-06-10 13:31:28 +02:00
|
|
|
vector<FileId> premium_sticker_ids;
|
2022-06-16 20:12:20 +02:00
|
|
|
std::tie(regular_sticker_ids, premium_sticker_ids) = split_stickers_by_premium(result);
|
2022-08-18 16:00:18 +02:00
|
|
|
if (td_->option_manager_->get_option_boolean("is_premium") || allow_premium) {
|
|
|
|
auto normal_count = td_->option_manager_->get_option_integer("stickers_normal_by_emoji_per_premium_num", 2);
|
2022-06-10 13:31:28 +02:00
|
|
|
if (normal_count < 0) {
|
|
|
|
normal_count = 2;
|
|
|
|
}
|
|
|
|
if (normal_count > 10) {
|
|
|
|
normal_count = 10;
|
|
|
|
}
|
|
|
|
// premium users have normal_count normal stickers per each premium
|
|
|
|
size_t normal_pos = 0;
|
|
|
|
size_t premium_pos = 0;
|
|
|
|
normal_count++;
|
2022-06-16 20:12:20 +02:00
|
|
|
for (size_t pos = 1; normal_pos < regular_sticker_ids.size() || premium_pos < premium_sticker_ids.size();
|
2022-06-10 13:31:28 +02:00
|
|
|
pos++) {
|
|
|
|
if (pos % normal_count == 0 && premium_pos < premium_sticker_ids.size()) {
|
|
|
|
auto sticker_id = premium_sticker_ids[premium_pos++];
|
|
|
|
LOG(INFO) << "Add premium sticker " << sticker_id << " from installed sticker set";
|
|
|
|
sorted.push_back(sticker_id);
|
2022-06-16 20:12:20 +02:00
|
|
|
} else if (normal_pos < regular_sticker_ids.size()) {
|
|
|
|
auto sticker_id = regular_sticker_ids[normal_pos++];
|
2022-06-10 13:31:28 +02:00
|
|
|
LOG(INFO) << "Add normal sticker " << sticker_id << " from installed sticker set";
|
|
|
|
sorted.push_back(sticker_id);
|
|
|
|
}
|
|
|
|
if (sorted.size() == limit_size_t) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2022-06-16 20:12:20 +02:00
|
|
|
for (const auto &sticker_id : regular_sticker_ids) {
|
2022-06-10 13:31:28 +02:00
|
|
|
LOG(INFO) << "Add normal sticker " << sticker_id << " from installed sticker set";
|
2018-12-31 20:04:05 +01:00
|
|
|
sorted.push_back(sticker_id);
|
|
|
|
if (sorted.size() == limit_size_t) {
|
|
|
|
break;
|
|
|
|
}
|
2022-06-10 13:31:28 +02:00
|
|
|
}
|
2022-08-01 15:56:21 +02:00
|
|
|
if (sorted.size() < limit_size_t) {
|
2022-08-18 16:00:18 +02:00
|
|
|
auto premium_count = td_->option_manager_->get_option_integer("stickers_premium_by_emoji_num", 0);
|
2022-06-10 13:31:28 +02:00
|
|
|
if (premium_count > 0) {
|
|
|
|
for (const auto &sticker_id : premium_sticker_ids) {
|
|
|
|
LOG(INFO) << "Add premium sticker " << sticker_id << " from installed sticker set";
|
|
|
|
sorted.push_back(sticker_id);
|
|
|
|
if (sorted.size() == limit_size_t || --premium_count == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
result = std::move(sorted);
|
|
|
|
}
|
|
|
|
|
|
|
|
promise.set_value(Unit());
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-08-07 23:55:22 +02:00
|
|
|
string StickersManager::get_found_stickers_database_key(const string &emoji) {
|
|
|
|
return PSTRING() << "found_stickers" << emoji;
|
|
|
|
}
|
|
|
|
|
2022-08-03 20:38:03 +02:00
|
|
|
void StickersManager::search_stickers(string emoji, int32 limit,
|
|
|
|
Promise<td_api::object_ptr<td_api::stickers>> &&promise) {
|
2022-08-06 23:33:16 +02:00
|
|
|
if (limit == 0) {
|
|
|
|
return promise.set_value(get_stickers_object({}));
|
|
|
|
}
|
|
|
|
if (limit < 0) {
|
2022-08-01 17:50:08 +02:00
|
|
|
return promise.set_error(Status::Error(400, "Parameter limit must be positive"));
|
2018-03-09 18:04:44 +01:00
|
|
|
}
|
|
|
|
if (limit > MAX_FOUND_STICKERS) {
|
|
|
|
limit = MAX_FOUND_STICKERS;
|
|
|
|
}
|
|
|
|
if (emoji.empty()) {
|
2022-08-01 17:50:08 +02:00
|
|
|
return promise.set_error(Status::Error(400, "Emoji must be non-empty"));
|
2018-03-09 18:04:44 +01:00
|
|
|
}
|
|
|
|
|
2021-09-20 12:45:49 +02:00
|
|
|
remove_emoji_modifiers_in_place(emoji);
|
2018-03-09 18:04:44 +01:00
|
|
|
if (emoji.empty()) {
|
2022-08-01 17:50:08 +02:00
|
|
|
return promise.set_value(get_stickers_object({}));
|
2018-03-09 18:04:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
auto it = found_stickers_.find(emoji);
|
2022-08-08 09:10:23 +02:00
|
|
|
if (it != found_stickers_.end()) {
|
2021-01-15 12:53:00 +01:00
|
|
|
const auto &sticker_ids = it->second.sticker_ids_;
|
2021-01-15 11:57:45 +01:00
|
|
|
auto result_size = min(static_cast<size_t>(limit), sticker_ids.size());
|
2022-08-08 09:10:23 +02:00
|
|
|
promise.set_value(get_stickers_object({sticker_ids.begin(), sticker_ids.begin() + result_size}));
|
|
|
|
if (Time::now() < it->second.next_reload_time_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
promise = Promise<td_api::object_ptr<td_api::stickers>>();
|
|
|
|
limit = 0;
|
2018-03-09 18:04:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
auto &promises = search_stickers_queries_[emoji];
|
2022-08-01 17:50:08 +02:00
|
|
|
promises.emplace_back(limit, std::move(promise));
|
2018-03-09 18:04:44 +01:00
|
|
|
if (promises.size() == 1u) {
|
2021-09-03 11:27:59 +02:00
|
|
|
int64 hash = 0;
|
2021-01-15 12:53:00 +01:00
|
|
|
if (it != found_stickers_.end()) {
|
|
|
|
hash = get_recent_stickers_hash(it->second.sticker_ids_);
|
2022-08-07 23:55:22 +02:00
|
|
|
td_->create_handler<SearchStickersQuery>()->send(std::move(emoji), hash);
|
|
|
|
return;
|
2021-01-15 12:53:00 +01:00
|
|
|
}
|
2022-08-07 23:55:22 +02:00
|
|
|
|
|
|
|
if (G()->parameters().use_file_db) {
|
|
|
|
LOG(INFO) << "Trying to load stickers for " << emoji << " from database";
|
|
|
|
G()->td_db()->get_sqlite_pmc()->get(
|
|
|
|
get_found_stickers_database_key(emoji), PromiseCreator::lambda([emoji](string value) mutable {
|
|
|
|
send_closure(G()->stickers_manager(), &StickersManager::on_load_found_stickers_from_database,
|
|
|
|
std::move(emoji), std::move(value));
|
|
|
|
}));
|
|
|
|
} else {
|
|
|
|
td_->create_handler<SearchStickersQuery>()->send(std::move(emoji), 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void StickersManager::on_load_found_stickers_from_database(string emoji, string value) {
|
|
|
|
if (G()->close_flag()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (value.empty()) {
|
|
|
|
LOG(INFO) << "Stickers for " << emoji << " aren't found in database";
|
|
|
|
td_->create_handler<SearchStickersQuery>()->send(std::move(emoji), 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG(INFO) << "Successfully loaded stickers for " << emoji << " from database";
|
|
|
|
|
|
|
|
auto &found_stickers = found_stickers_[emoji];
|
|
|
|
CHECK(found_stickers.next_reload_time_ == 0);
|
|
|
|
auto status = log_event_parse(found_stickers, value);
|
|
|
|
if (status.is_error()) {
|
|
|
|
LOG(ERROR) << "Can't load stickers for emoji: " << status << ' ' << format::as_hex_dump<4>(Slice(value));
|
|
|
|
found_stickers_.erase(emoji);
|
|
|
|
td_->create_handler<SearchStickersQuery>()->send(std::move(emoji), 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
on_search_stickers_finished(emoji, found_stickers);
|
|
|
|
}
|
|
|
|
|
|
|
|
void StickersManager::on_search_stickers_finished(const string &emoji, const FoundStickers &found_stickers) {
|
|
|
|
auto it = search_stickers_queries_.find(emoji);
|
|
|
|
CHECK(it != search_stickers_queries_.end());
|
|
|
|
CHECK(!it->second.empty());
|
|
|
|
auto queries = std::move(it->second);
|
|
|
|
search_stickers_queries_.erase(it);
|
|
|
|
|
|
|
|
const auto &sticker_ids = found_stickers.sticker_ids_;
|
|
|
|
for (auto &query : queries) {
|
|
|
|
auto result_size = min(static_cast<size_t>(query.first), sticker_ids.size());
|
|
|
|
query.second.set_value(get_stickers_object({sticker_ids.begin(), sticker_ids.begin() + result_size}));
|
2018-03-09 18:04:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void StickersManager::on_find_stickers_success(const string &emoji,
|
|
|
|
tl_object_ptr<telegram_api::messages_Stickers> &&stickers) {
|
|
|
|
CHECK(stickers != nullptr);
|
|
|
|
switch (stickers->get_id()) {
|
2021-01-15 12:53:00 +01:00
|
|
|
case telegram_api::messages_stickersNotModified::ID: {
|
|
|
|
auto it = found_stickers_.find(emoji);
|
|
|
|
if (it == found_stickers_.end()) {
|
|
|
|
return on_find_stickers_fail(emoji, Status::Error(500, "Receive messages.stickerNotModified"));
|
|
|
|
}
|
|
|
|
auto &found_stickers = it->second;
|
|
|
|
found_stickers.next_reload_time_ = Time::now() + found_stickers.cache_time_;
|
2022-08-07 23:55:22 +02:00
|
|
|
return on_search_stickers_finished(emoji, found_stickers);
|
2021-01-15 12:53:00 +01:00
|
|
|
}
|
2018-03-09 18:04:44 +01:00
|
|
|
case telegram_api::messages_stickers::ID: {
|
2021-01-15 12:53:00 +01:00
|
|
|
auto received_stickers = move_tl_object_as<telegram_api::messages_stickers>(stickers);
|
2018-03-09 18:04:44 +01:00
|
|
|
|
2021-01-15 12:53:00 +01:00
|
|
|
auto &found_stickers = found_stickers_[emoji];
|
|
|
|
found_stickers.cache_time_ = 300;
|
|
|
|
found_stickers.next_reload_time_ = Time::now() + found_stickers.cache_time_;
|
|
|
|
found_stickers.sticker_ids_.clear();
|
|
|
|
|
|
|
|
for (auto &sticker : received_stickers->stickers_) {
|
2022-01-18 17:28:32 +01:00
|
|
|
FileId sticker_id = on_get_sticker_document(std::move(sticker), StickerFormat::Unknown).second;
|
2018-03-09 18:04:44 +01:00
|
|
|
if (sticker_id.is_valid()) {
|
2021-01-15 12:53:00 +01:00
|
|
|
found_stickers.sticker_ids_.push_back(sticker_id);
|
2018-03-09 18:04:44 +01:00
|
|
|
}
|
|
|
|
}
|
2022-08-07 23:55:22 +02:00
|
|
|
|
|
|
|
if (G()->parameters().use_file_db && !G()->close_flag()) {
|
|
|
|
LOG(INFO) << "Save stickers for " << emoji << " to database";
|
|
|
|
G()->td_db()->get_sqlite_pmc()->set(get_found_stickers_database_key(emoji),
|
|
|
|
log_event_store(found_stickers).as_slice().str(), Auto());
|
|
|
|
}
|
|
|
|
|
|
|
|
return on_search_stickers_finished(emoji, found_stickers);
|
2018-03-09 18:04:44 +01:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void StickersManager::on_find_stickers_fail(const string &emoji, Status &&error) {
|
2021-01-15 12:53:00 +01:00
|
|
|
if (found_stickers_.count(emoji) != 0) {
|
|
|
|
found_stickers_[emoji].cache_time_ = Random::fast(40, 80);
|
|
|
|
return on_find_stickers_success(emoji, make_tl_object<telegram_api::messages_stickersNotModified>());
|
|
|
|
}
|
2018-03-09 18:04:44 +01:00
|
|
|
|
|
|
|
auto it = search_stickers_queries_.find(emoji);
|
|
|
|
CHECK(it != search_stickers_queries_.end());
|
|
|
|
CHECK(!it->second.empty());
|
2022-08-01 17:50:08 +02:00
|
|
|
auto queries = std::move(it->second);
|
2018-03-09 18:04:44 +01:00
|
|
|
search_stickers_queries_.erase(it);
|
|
|
|
|
2022-08-01 17:50:08 +02:00
|
|
|
for (auto &query : queries) {
|
|
|
|
query.second.set_error(error.clone());
|
|
|
|
}
|
2018-03-09 18:04:44 +01:00
|
|
|
}
|
|
|
|
|
2022-08-06 23:33:16 +02:00
|
|
|
void StickersManager::get_premium_stickers(int32 limit, Promise<td_api::object_ptr<td_api::stickers>> &&promise) {
|
|
|
|
TRY_STATUS_PROMISE(promise, G()->close_status());
|
|
|
|
|
|
|
|
if (limit == 0) {
|
|
|
|
return promise.set_value(get_stickers_object({}));
|
|
|
|
}
|
|
|
|
if (limit > MAX_FOUND_STICKERS) {
|
|
|
|
limit = MAX_FOUND_STICKERS;
|
|
|
|
}
|
|
|
|
|
|
|
|
MultiPromiseActorSafe mpas{"GetPremiumStickersMultiPromiseActor"};
|
|
|
|
mpas.add_promise(PromiseCreator::lambda(
|
|
|
|
[actor_id = actor_id(this), limit, promise = std::move(promise)](Result<Unit> result) mutable {
|
|
|
|
if (result.is_error()) {
|
|
|
|
promise.set_error(result.move_as_error());
|
|
|
|
} else {
|
|
|
|
send_closure(actor_id, &StickersManager::do_get_premium_stickers, limit, std::move(promise));
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
|
|
|
auto lock = mpas.get_promise();
|
|
|
|
search_stickers("📂⭐️", limit,
|
|
|
|
PromiseCreator::lambda(
|
|
|
|
[promise = mpas.get_promise()](Result<td_api::object_ptr<td_api::stickers>> result) mutable {
|
|
|
|
if (result.is_error()) {
|
|
|
|
promise.set_error(result.move_as_error());
|
|
|
|
} else {
|
|
|
|
promise.set_value(Unit());
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
get_stickers(StickerType::Regular, string(), 1, DialogId(), false, mpas.get_promise());
|
|
|
|
lock.set_value(Unit());
|
|
|
|
}
|
|
|
|
|
|
|
|
void StickersManager::do_get_premium_stickers(int32 limit, Promise<td_api::object_ptr<td_api::stickers>> &&promise) {
|
|
|
|
auto type = static_cast<int32>(StickerType::Regular);
|
|
|
|
CHECK(are_installed_sticker_sets_loaded_[type]);
|
|
|
|
|
|
|
|
vector<FileId> sticker_ids;
|
|
|
|
auto limit_size_t = static_cast<size_t>(limit);
|
|
|
|
for (const auto &sticker_set_id : installed_sticker_set_ids_[type]) {
|
|
|
|
const StickerSet *sticker_set = get_sticker_set(sticker_set_id);
|
2022-08-07 18:47:50 +02:00
|
|
|
if (sticker_set == nullptr || !sticker_set->was_loaded_) {
|
2022-08-06 23:33:16 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-08-07 19:35:13 +02:00
|
|
|
for (auto premium_sticker_position : sticker_set->premium_sticker_positions_) {
|
|
|
|
sticker_ids.push_back(sticker_set->sticker_ids_[premium_sticker_position]);
|
2022-08-06 23:33:16 +02:00
|
|
|
if (sticker_ids.size() == limit_size_t) {
|
|
|
|
return promise.set_value(get_stickers_object(sticker_ids));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto it = found_stickers_.find(remove_emoji_modifiers("📂⭐️"));
|
|
|
|
CHECK(it != found_stickers_.end());
|
|
|
|
for (auto sticker_id : it->second.sticker_ids_) {
|
|
|
|
if (td::contains(sticker_ids, sticker_id)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
sticker_ids.push_back(sticker_id);
|
|
|
|
if (sticker_ids.size() == limit_size_t) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
promise.set_value(get_stickers_object(sticker_ids));
|
|
|
|
}
|
|
|
|
|
2022-07-14 15:17:19 +02:00
|
|
|
vector<StickerSetId> StickersManager::get_installed_sticker_sets(StickerType sticker_type, Promise<Unit> &&promise) {
|
2022-07-24 13:01:58 +02:00
|
|
|
auto type = static_cast<int32>(sticker_type);
|
|
|
|
if (!are_installed_sticker_sets_loaded_[type]) {
|
2022-07-14 15:17:19 +02:00
|
|
|
load_installed_sticker_sets(sticker_type, std::move(promise));
|
2018-12-31 20:04:05 +01:00
|
|
|
return {};
|
|
|
|
}
|
2022-07-14 15:17:19 +02:00
|
|
|
reload_installed_sticker_sets(sticker_type, false);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
promise.set_value(Unit());
|
2022-07-24 13:01:58 +02:00
|
|
|
return installed_sticker_set_ids_[type];
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool StickersManager::update_sticker_set_cache(const StickerSet *sticker_set, Promise<Unit> &promise) {
|
|
|
|
CHECK(sticker_set != nullptr);
|
2022-08-07 18:47:50 +02:00
|
|
|
auto set_id = sticker_set->id_;
|
|
|
|
if (!sticker_set->is_loaded_) {
|
|
|
|
if (!sticker_set->was_loaded_ || td_->auth_manager_->is_bot()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
load_sticker_sets({set_id}, std::move(promise));
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
load_sticker_sets({set_id}, Auto());
|
|
|
|
}
|
2022-08-07 18:47:50 +02:00
|
|
|
} else if (sticker_set->is_installed_) {
|
|
|
|
reload_installed_sticker_sets(sticker_set->sticker_type_, false);
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
2022-08-07 18:47:50 +02:00
|
|
|
if (G()->unix_time() >= sticker_set->expires_at_) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (td_->auth_manager_->is_bot()) {
|
2022-08-07 18:47:50 +02:00
|
|
|
do_reload_sticker_set(set_id, get_input_sticker_set(sticker_set), sticker_set->hash_, std::move(promise));
|
2018-12-31 20:04:05 +01:00
|
|
|
return true;
|
|
|
|
} else {
|
2022-08-07 18:47:50 +02:00
|
|
|
do_reload_sticker_set(set_id, get_input_sticker_set(sticker_set), sticker_set->hash_, Auto());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-09-18 05:55:43 +02:00
|
|
|
StickerSetId StickersManager::get_sticker_set(StickerSetId set_id, Promise<Unit> &&promise) {
|
2018-12-31 20:04:05 +01:00
|
|
|
const StickerSet *sticker_set = get_sticker_set(set_id);
|
|
|
|
if (sticker_set == nullptr) {
|
2019-09-18 05:55:43 +02:00
|
|
|
if (set_id.get() == GREAT_MINDS_SET_ID) {
|
2021-11-19 11:56:12 +01:00
|
|
|
do_reload_sticker_set(set_id, make_tl_object<telegram_api::inputStickerSetID>(set_id.get(), 0), 0,
|
2019-09-18 05:55:43 +02:00
|
|
|
std::move(promise));
|
|
|
|
return StickerSetId();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
promise.set_error(Status::Error(400, "Sticker set not found"));
|
2019-09-18 05:55:43 +02:00
|
|
|
return StickerSetId();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (update_sticker_set_cache(sticker_set, promise)) {
|
2019-09-18 05:55:43 +02:00
|
|
|
return StickerSetId();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
promise.set_value(Unit());
|
|
|
|
return set_id;
|
|
|
|
}
|
|
|
|
|
2019-09-18 05:55:43 +02:00
|
|
|
StickerSetId StickersManager::search_sticker_set(const string &short_name_to_search, Promise<Unit> &&promise) {
|
2018-12-31 20:04:05 +01:00
|
|
|
string short_name = clean_username(short_name_to_search);
|
2022-08-03 21:42:52 +02:00
|
|
|
const StickerSet *sticker_set = get_sticker_set(short_name_to_sticker_set_id_.get(short_name));
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
if (sticker_set == nullptr) {
|
|
|
|
auto set_to_load = make_tl_object<telegram_api::inputStickerSetShortName>(short_name);
|
2021-11-19 11:56:12 +01:00
|
|
|
do_reload_sticker_set(StickerSetId(), std::move(set_to_load), 0, std::move(promise));
|
2019-09-18 05:55:43 +02:00
|
|
|
return StickerSetId();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (update_sticker_set_cache(sticker_set, promise)) {
|
2019-09-18 05:55:43 +02:00
|
|
|
return StickerSetId();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
promise.set_value(Unit());
|
2022-08-07 18:47:50 +02:00
|
|
|
return sticker_set->id_;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2022-07-14 15:17:19 +02:00
|
|
|
std::pair<int32, vector<StickerSetId>> StickersManager::search_installed_sticker_sets(StickerType sticker_type,
|
2019-09-18 05:55:43 +02:00
|
|
|
const string &query, int32 limit,
|
|
|
|
Promise<Unit> &&promise) {
|
2022-07-14 15:17:19 +02:00
|
|
|
LOG(INFO) << "Search installed " << sticker_type << " sticker sets with query = \"" << query
|
2018-03-05 23:44:11 +01:00
|
|
|
<< "\" and limit = " << limit;
|
|
|
|
|
|
|
|
if (limit < 0) {
|
|
|
|
promise.set_error(Status::Error(400, "Limit must be non-negative"));
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-07-24 13:01:58 +02:00
|
|
|
auto type = static_cast<int32>(sticker_type);
|
|
|
|
if (!are_installed_sticker_sets_loaded_[type]) {
|
2022-07-14 15:17:19 +02:00
|
|
|
load_installed_sticker_sets(sticker_type, std::move(promise));
|
2018-03-05 23:44:11 +01:00
|
|
|
return {};
|
|
|
|
}
|
2022-07-14 15:17:19 +02:00
|
|
|
reload_installed_sticker_sets(sticker_type, false);
|
2018-03-05 23:44:11 +01:00
|
|
|
|
2022-07-24 13:01:58 +02:00
|
|
|
std::pair<size_t, vector<int64>> result = installed_sticker_sets_hints_[type].search(query, limit);
|
2018-03-05 23:44:11 +01:00
|
|
|
promise.set_value(Unit());
|
2019-09-18 05:55:43 +02:00
|
|
|
return {narrow_cast<int32>(result.first), convert_sticker_set_ids(result.second)};
|
2018-03-05 23:44:11 +01:00
|
|
|
}
|
|
|
|
|
2019-09-18 05:55:43 +02:00
|
|
|
vector<StickerSetId> StickersManager::search_sticker_sets(const string &query, Promise<Unit> &&promise) {
|
2018-03-05 23:44:11 +01:00
|
|
|
auto q = clean_name(query, 1000);
|
|
|
|
auto it = found_sticker_sets_.find(q);
|
|
|
|
if (it != found_sticker_sets_.end()) {
|
|
|
|
promise.set_value(Unit());
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto &promises = search_sticker_sets_queries_[q];
|
|
|
|
promises.push_back(std::move(promise));
|
|
|
|
if (promises.size() == 1u) {
|
|
|
|
td_->create_handler<SearchStickerSetsQuery>()->send(std::move(q));
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
void StickersManager::on_find_sticker_sets_success(
|
|
|
|
const string &query, tl_object_ptr<telegram_api::messages_FoundStickerSets> &&sticker_sets) {
|
|
|
|
CHECK(sticker_sets != nullptr);
|
|
|
|
switch (sticker_sets->get_id()) {
|
|
|
|
case telegram_api::messages_foundStickerSetsNotModified::ID:
|
|
|
|
return on_find_sticker_sets_fail(query, Status::Error(500, "Receive messages.foundStickerSetsNotModified"));
|
|
|
|
case telegram_api::messages_foundStickerSets::ID: {
|
|
|
|
auto found_stickers_sets = move_tl_object_as<telegram_api::messages_foundStickerSets>(sticker_sets);
|
2019-09-18 05:55:43 +02:00
|
|
|
vector<StickerSetId> &sticker_set_ids = found_sticker_sets_[query];
|
2018-03-05 23:44:11 +01:00
|
|
|
CHECK(sticker_set_ids.empty());
|
|
|
|
|
|
|
|
for (auto &sticker_set : found_stickers_sets->sets_) {
|
2019-12-29 02:59:34 +01:00
|
|
|
StickerSetId set_id = on_get_sticker_set_covered(std::move(sticker_set), true, "on_find_sticker_sets_success");
|
2019-09-18 05:55:43 +02:00
|
|
|
if (!set_id.is_valid()) {
|
2018-03-05 23:44:11 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-11-14 08:19:03 +01:00
|
|
|
update_sticker_set(get_sticker_set(set_id), "on_find_sticker_sets_success");
|
2018-03-05 23:44:11 +01:00
|
|
|
sticker_set_ids.push_back(set_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
send_update_installed_sticker_sets();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto it = search_sticker_sets_queries_.find(query);
|
|
|
|
CHECK(it != search_sticker_sets_queries_.end());
|
|
|
|
CHECK(!it->second.empty());
|
|
|
|
auto promises = std::move(it->second);
|
|
|
|
search_sticker_sets_queries_.erase(it);
|
|
|
|
|
2022-04-13 16:40:12 +02:00
|
|
|
set_promises(promises);
|
2018-03-05 23:44:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void StickersManager::on_find_sticker_sets_fail(const string &query, Status &&error) {
|
|
|
|
CHECK(found_sticker_sets_.count(query) == 0);
|
|
|
|
|
|
|
|
auto it = search_sticker_sets_queries_.find(query);
|
|
|
|
CHECK(it != search_sticker_sets_queries_.end());
|
|
|
|
CHECK(!it->second.empty());
|
|
|
|
auto promises = std::move(it->second);
|
|
|
|
search_sticker_sets_queries_.erase(it);
|
|
|
|
|
2022-04-13 16:40:12 +02:00
|
|
|
fail_promises(promises, std::move(error));
|
2018-03-05 23:44:11 +01:00
|
|
|
}
|
|
|
|
|
2019-09-18 05:55:43 +02:00
|
|
|
void StickersManager::change_sticker_set(StickerSetId set_id, bool is_installed, bool is_archived,
|
|
|
|
Promise<Unit> &&promise) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (is_installed && is_archived) {
|
|
|
|
return promise.set_error(Status::Error(400, "Sticker set can't be installed and archived simultaneously"));
|
|
|
|
}
|
|
|
|
const StickerSet *sticker_set = get_sticker_set(set_id);
|
|
|
|
if (sticker_set == nullptr) {
|
|
|
|
return promise.set_error(Status::Error(400, "Sticker set not found"));
|
|
|
|
}
|
2022-08-07 18:47:50 +02:00
|
|
|
if (!sticker_set->is_inited_) {
|
2018-12-31 20:04:05 +01:00
|
|
|
load_sticker_sets({set_id}, std::move(promise));
|
|
|
|
return;
|
|
|
|
}
|
2022-08-07 18:47:50 +02:00
|
|
|
auto type = static_cast<int32>(sticker_set->sticker_type_);
|
2022-07-24 13:01:58 +02:00
|
|
|
if (!are_installed_sticker_sets_loaded_[type]) {
|
2022-08-07 18:47:50 +02:00
|
|
|
load_installed_sticker_sets(sticker_set->sticker_type_, std::move(promise));
|
2018-03-10 12:15:23 +01:00
|
|
|
return;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
if (is_archived) {
|
|
|
|
is_installed = true;
|
|
|
|
}
|
|
|
|
if (is_installed) {
|
2022-08-07 18:47:50 +02:00
|
|
|
if (sticker_set->is_installed_ && is_archived == sticker_set->is_archived_) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return promise.set_value(Unit());
|
|
|
|
}
|
|
|
|
|
|
|
|
td_->create_handler<InstallStickerSetQuery>(std::move(promise))
|
|
|
|
->send(set_id, get_input_sticker_set(sticker_set), is_archived);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-08-07 18:47:50 +02:00
|
|
|
if (!sticker_set->is_installed_) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return promise.set_value(Unit());
|
|
|
|
}
|
|
|
|
|
|
|
|
td_->create_handler<UninstallStickerSetQuery>(std::move(promise))->send(set_id, get_input_sticker_set(sticker_set));
|
|
|
|
}
|
|
|
|
|
|
|
|
void StickersManager::on_update_sticker_set(StickerSet *sticker_set, bool is_installed, bool is_archived,
|
|
|
|
bool is_changed, bool from_database) {
|
2022-08-07 18:47:50 +02:00
|
|
|
LOG(INFO) << "Update " << sticker_set->id_ << ": installed = " << is_installed << ", archived = " << is_archived
|
2020-06-04 00:19:37 +02:00
|
|
|
<< ", changed = " << is_changed << ", from_database = " << from_database;
|
2022-08-07 18:47:50 +02:00
|
|
|
CHECK(sticker_set->is_inited_);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (is_archived) {
|
|
|
|
is_installed = true;
|
|
|
|
}
|
2022-08-07 18:47:50 +02:00
|
|
|
if (sticker_set->is_installed_ == is_installed && sticker_set->is_archived_ == is_archived) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-08-07 18:47:50 +02:00
|
|
|
bool was_added = sticker_set->is_installed_ && !sticker_set->is_archived_;
|
|
|
|
bool was_archived = sticker_set->is_archived_;
|
|
|
|
sticker_set->is_installed_ = is_installed;
|
|
|
|
sticker_set->is_archived_ = is_archived;
|
2018-12-31 20:04:05 +01:00
|
|
|
if (!from_database) {
|
2022-08-07 18:47:50 +02:00
|
|
|
sticker_set->is_changed_ = true;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2022-08-07 18:47:50 +02:00
|
|
|
bool is_added = sticker_set->is_installed_ && !sticker_set->is_archived_;
|
|
|
|
auto type = static_cast<int32>(sticker_set->sticker_type_);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (was_added != is_added) {
|
2022-07-14 15:17:19 +02:00
|
|
|
vector<StickerSetId> &sticker_set_ids = installed_sticker_set_ids_[type];
|
|
|
|
need_update_installed_sticker_sets_[type] = true;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
if (is_added) {
|
2022-08-07 18:47:50 +02:00
|
|
|
installed_sticker_sets_hints_[type].add(sticker_set->id_.get(),
|
|
|
|
PSLICE() << sticker_set->title_ << ' ' << sticker_set->short_name_);
|
|
|
|
sticker_set_ids.insert(sticker_set_ids.begin(), sticker_set->id_);
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
2022-08-07 18:47:50 +02:00
|
|
|
installed_sticker_sets_hints_[type].remove(sticker_set->id_.get());
|
|
|
|
td::remove(sticker_set_ids, sticker_set->id_);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (was_archived != is_archived && is_changed) {
|
2022-07-14 15:17:19 +02:00
|
|
|
int32 &total_count = total_archived_sticker_set_count_[type];
|
|
|
|
vector<StickerSetId> &sticker_set_ids = archived_sticker_set_ids_[type];
|
2018-12-31 20:04:05 +01:00
|
|
|
if (total_count < 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_archived) {
|
2022-08-07 18:47:50 +02:00
|
|
|
if (!td::contains(sticker_set_ids, sticker_set->id_)) {
|
2019-06-05 01:32:10 +02:00
|
|
|
total_count++;
|
2022-08-07 18:47:50 +02:00
|
|
|
sticker_set_ids.insert(sticker_set_ids.begin(), sticker_set->id_);
|
2019-06-05 01:32:10 +02:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
|
|
|
total_count--;
|
2019-06-05 01:32:10 +02:00
|
|
|
if (total_count < 0) {
|
|
|
|
LOG(ERROR) << "Total count of archived sticker sets became negative";
|
|
|
|
total_count = 0;
|
|
|
|
}
|
2022-08-07 18:47:50 +02:00
|
|
|
td::remove(sticker_set_ids, sticker_set->id_);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-14 15:17:19 +02:00
|
|
|
void StickersManager::load_installed_sticker_sets(StickerType sticker_type, Promise<Unit> &&promise) {
|
2022-07-24 13:01:58 +02:00
|
|
|
auto type = static_cast<int32>(sticker_type);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (td_->auth_manager_->is_bot()) {
|
2022-07-24 13:01:58 +02:00
|
|
|
are_installed_sticker_sets_loaded_[type] = true;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2022-07-24 13:01:58 +02:00
|
|
|
if (are_installed_sticker_sets_loaded_[type]) {
|
2018-12-31 20:04:05 +01:00
|
|
|
promise.set_value(Unit());
|
|
|
|
return;
|
|
|
|
}
|
2022-07-24 13:01:58 +02:00
|
|
|
load_installed_sticker_sets_queries_[type].push_back(std::move(promise));
|
|
|
|
if (load_installed_sticker_sets_queries_[type].size() == 1u) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (G()->parameters().use_file_db) {
|
2022-07-14 15:17:19 +02:00
|
|
|
LOG(INFO) << "Trying to load installed " << sticker_type << " sticker sets from database";
|
|
|
|
G()->td_db()->get_sqlite_pmc()->get(
|
2022-07-24 13:01:58 +02:00
|
|
|
PSTRING() << "sss" << type, PromiseCreator::lambda([sticker_type](string value) {
|
2022-07-14 15:17:19 +02:00
|
|
|
send_closure(G()->stickers_manager(), &StickersManager::on_load_installed_sticker_sets_from_database,
|
|
|
|
sticker_type, std::move(value));
|
|
|
|
}));
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
2022-07-14 15:17:19 +02:00
|
|
|
LOG(INFO) << "Trying to load installed " << sticker_type << " sticker sets from server";
|
|
|
|
reload_installed_sticker_sets(sticker_type, true);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-14 15:17:19 +02:00
|
|
|
void StickersManager::on_load_installed_sticker_sets_from_database(StickerType sticker_type, string value) {
|
2020-05-16 01:25:03 +02:00
|
|
|
if (G()->close_flag()) {
|
|
|
|
return;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
if (value.empty()) {
|
2022-07-14 15:17:19 +02:00
|
|
|
LOG(INFO) << "Installed " << sticker_type << " sticker sets aren't found in database";
|
|
|
|
reload_installed_sticker_sets(sticker_type, true);
|
2018-12-31 20:04:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-07-14 15:17:19 +02:00
|
|
|
LOG(INFO) << "Successfully loaded installed " << sticker_type << " sticker set list of size " << value.size()
|
|
|
|
<< " from database";
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
StickerSetListLogEvent log_event;
|
2019-05-29 21:24:27 +02:00
|
|
|
auto status = log_event_parse(log_event, value);
|
|
|
|
if (status.is_error()) {
|
|
|
|
// can't happen unless database is broken
|
2020-04-18 00:55:54 +02:00
|
|
|
LOG(ERROR) << "Can't load installed sticker set list: " << status << ' ' << format::as_hex_dump<4>(Slice(value));
|
2022-07-14 15:17:19 +02:00
|
|
|
return reload_installed_sticker_sets(sticker_type, true);
|
2019-05-29 21:24:27 +02:00
|
|
|
}
|
2022-06-17 18:16:17 +02:00
|
|
|
CHECK(!log_event.is_premium_);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2019-09-18 05:55:43 +02:00
|
|
|
vector<StickerSetId> sets_to_load;
|
2022-06-17 18:16:17 +02:00
|
|
|
for (auto sticker_set_id : log_event.sticker_set_ids_) {
|
2018-12-31 20:04:05 +01:00
|
|
|
StickerSet *sticker_set = get_sticker_set(sticker_set_id);
|
|
|
|
CHECK(sticker_set != nullptr);
|
2022-08-07 18:47:50 +02:00
|
|
|
if (!sticker_set->is_inited_) {
|
2018-12-31 20:04:05 +01:00
|
|
|
sets_to_load.push_back(sticker_set_id);
|
|
|
|
}
|
|
|
|
}
|
2018-02-27 02:14:47 +01:00
|
|
|
std::reverse(sets_to_load.begin(), sets_to_load.end()); // load installed sticker sets in reverse order
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
load_sticker_sets_without_stickers(
|
|
|
|
std::move(sets_to_load),
|
2022-07-14 15:17:19 +02:00
|
|
|
PromiseCreator::lambda([sticker_type,
|
|
|
|
sticker_set_ids = std::move(log_event.sticker_set_ids_)](Result<Unit> result) mutable {
|
|
|
|
if (result.is_ok()) {
|
|
|
|
send_closure(G()->stickers_manager(), &StickersManager::on_load_installed_sticker_sets_finished, sticker_type,
|
|
|
|
std::move(sticker_set_ids), true);
|
|
|
|
} else {
|
|
|
|
send_closure(G()->stickers_manager(), &StickersManager::reload_installed_sticker_sets, sticker_type, true);
|
|
|
|
}
|
|
|
|
}));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2022-07-14 15:17:19 +02:00
|
|
|
void StickersManager::on_load_installed_sticker_sets_finished(StickerType sticker_type,
|
2019-09-18 05:55:43 +02:00
|
|
|
vector<StickerSetId> &&installed_sticker_set_ids,
|
2018-12-31 20:04:05 +01:00
|
|
|
bool from_database) {
|
2018-02-13 21:41:37 +01:00
|
|
|
bool need_reload = false;
|
2019-09-18 05:55:43 +02:00
|
|
|
vector<StickerSetId> old_installed_sticker_set_ids;
|
2022-07-24 13:01:58 +02:00
|
|
|
auto type = static_cast<int32>(sticker_type);
|
|
|
|
if (!are_installed_sticker_sets_loaded_[type] && !installed_sticker_set_ids_[type].empty()) {
|
|
|
|
old_installed_sticker_set_ids = std::move(installed_sticker_set_ids_[type]);
|
2018-02-13 21:41:37 +01:00
|
|
|
}
|
2022-07-24 13:01:58 +02:00
|
|
|
installed_sticker_set_ids_[type].clear();
|
2018-02-13 21:41:37 +01:00
|
|
|
for (auto set_id : installed_sticker_set_ids) {
|
2019-09-18 05:55:43 +02:00
|
|
|
CHECK(set_id.is_valid());
|
2018-02-13 21:41:37 +01:00
|
|
|
|
|
|
|
auto sticker_set = get_sticker_set(set_id);
|
|
|
|
CHECK(sticker_set != nullptr);
|
2022-08-07 18:47:50 +02:00
|
|
|
CHECK(sticker_set->is_inited_);
|
|
|
|
if (sticker_set->is_installed_ && !sticker_set->is_archived_ && sticker_set->sticker_type_ == sticker_type) {
|
2022-07-24 13:01:58 +02:00
|
|
|
installed_sticker_set_ids_[type].push_back(set_id);
|
2018-02-13 21:41:37 +01:00
|
|
|
} else {
|
|
|
|
need_reload = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (need_reload) {
|
2022-07-14 15:17:19 +02:00
|
|
|
LOG(ERROR) << "Reload installed " << sticker_type << " sticker sets, because only "
|
2022-07-24 13:01:58 +02:00
|
|
|
<< installed_sticker_set_ids_[type].size() << " of " << installed_sticker_set_ids.size()
|
|
|
|
<< " are really installed after loading from " << (from_database ? "database" : "server");
|
2022-07-14 15:17:19 +02:00
|
|
|
reload_installed_sticker_sets(sticker_type, true);
|
2018-02-27 02:14:47 +01:00
|
|
|
} else if (!old_installed_sticker_set_ids.empty() &&
|
2022-07-24 13:01:58 +02:00
|
|
|
old_installed_sticker_set_ids != installed_sticker_set_ids_[type]) {
|
2022-07-14 15:17:19 +02:00
|
|
|
LOG(ERROR) << "Reload installed " << sticker_type << " sticker sets, because they has changed from "
|
2022-07-24 13:01:58 +02:00
|
|
|
<< old_installed_sticker_set_ids << " to " << installed_sticker_set_ids_[type] << " after loading from "
|
2022-07-14 15:17:19 +02:00
|
|
|
<< (from_database ? "database" : "server");
|
|
|
|
reload_installed_sticker_sets(sticker_type, true);
|
2018-02-13 21:41:37 +01:00
|
|
|
}
|
|
|
|
|
2022-07-24 13:01:58 +02:00
|
|
|
are_installed_sticker_sets_loaded_[type] = true;
|
|
|
|
need_update_installed_sticker_sets_[type] = true;
|
2018-12-31 20:04:05 +01:00
|
|
|
send_update_installed_sticker_sets(from_database);
|
2022-07-24 13:01:58 +02:00
|
|
|
set_promises(load_installed_sticker_sets_queries_[type]);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2019-09-18 05:55:43 +02:00
|
|
|
string StickersManager::get_sticker_set_database_key(StickerSetId set_id) {
|
|
|
|
return PSTRING() << "ss" << set_id.get();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2019-09-18 05:55:43 +02:00
|
|
|
string StickersManager::get_full_sticker_set_database_key(StickerSetId set_id) {
|
|
|
|
return PSTRING() << "ssf" << set_id.get();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-11-14 08:19:03 +01:00
|
|
|
string StickersManager::get_sticker_set_database_value(const StickerSet *s, bool with_stickers, const char *source) {
|
2018-12-31 20:04:05 +01:00
|
|
|
LogEventStorerCalcLength storer_calc_length;
|
2021-11-14 08:19:03 +01:00
|
|
|
store_sticker_set(s, with_stickers, storer_calc_length, source);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
BufferSlice value_buffer{storer_calc_length.get_length()};
|
|
|
|
auto value = value_buffer.as_slice();
|
|
|
|
|
2022-08-07 18:47:50 +02:00
|
|
|
LOG(DEBUG) << "Serialized size of " << s->id_ << " is " << value.size();
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-06-25 01:58:43 +02:00
|
|
|
LogEventStorerUnsafe storer_unsafe(value.ubegin());
|
2021-11-14 08:19:03 +01:00
|
|
|
store_sticker_set(s, with_stickers, storer_unsafe, source);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
return value.str();
|
|
|
|
}
|
|
|
|
|
2021-11-14 08:19:03 +01:00
|
|
|
void StickersManager::update_sticker_set(StickerSet *sticker_set, const char *source) {
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK(sticker_set != nullptr);
|
2022-08-07 18:47:50 +02:00
|
|
|
if (sticker_set->is_changed_ || sticker_set->need_save_to_database_) {
|
2020-05-16 01:45:26 +02:00
|
|
|
if (G()->parameters().use_file_db && !G()->close_flag()) {
|
2022-08-07 18:47:50 +02:00
|
|
|
LOG(INFO) << "Save " << sticker_set->id_ << " to database from " << source;
|
|
|
|
if (sticker_set->is_inited_) {
|
|
|
|
G()->td_db()->get_sqlite_pmc()->set(get_sticker_set_database_key(sticker_set->id_),
|
2021-11-14 08:19:03 +01:00
|
|
|
get_sticker_set_database_value(sticker_set, false, source), Auto());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2022-08-07 18:47:50 +02:00
|
|
|
if (sticker_set->was_loaded_) {
|
|
|
|
G()->td_db()->get_sqlite_pmc()->set(get_full_sticker_set_database_key(sticker_set->id_),
|
2021-11-14 08:19:03 +01:00
|
|
|
get_sticker_set_database_value(sticker_set, true, source), Auto());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
2022-08-07 18:47:50 +02:00
|
|
|
if (sticker_set->is_changed_ && sticker_set->was_loaded_ && sticker_set->was_update_sent_) {
|
2020-04-17 14:56:57 +02:00
|
|
|
send_closure(G()->td(), &Td::send_update,
|
2022-08-07 18:47:50 +02:00
|
|
|
td_api::make_object<td_api::updateStickerSet>(get_sticker_set_object(sticker_set->id_)));
|
2020-04-17 14:56:57 +02:00
|
|
|
}
|
2022-08-07 18:47:50 +02:00
|
|
|
sticker_set->is_changed_ = false;
|
|
|
|
sticker_set->need_save_to_database_ = false;
|
|
|
|
if (sticker_set->is_inited_) {
|
2018-12-31 20:04:05 +01:00
|
|
|
update_load_requests(sticker_set, false, Status::OK());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-18 05:55:43 +02:00
|
|
|
void StickersManager::load_sticker_sets(vector<StickerSetId> &&sticker_set_ids, Promise<Unit> &&promise) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (sticker_set_ids.empty()) {
|
|
|
|
promise.set_value(Unit());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-02-10 16:03:03 +01:00
|
|
|
CHECK(current_sticker_set_load_request_ < std::numeric_limits<uint32>::max());
|
|
|
|
auto load_request_id = ++current_sticker_set_load_request_;
|
2018-12-31 20:04:05 +01:00
|
|
|
StickerSetLoadRequest &load_request = sticker_set_load_requests_[load_request_id];
|
2022-08-07 18:47:50 +02:00
|
|
|
load_request.promise_ = std::move(promise);
|
|
|
|
load_request.left_queries_ = sticker_set_ids.size();
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
for (auto sticker_set_id : sticker_set_ids) {
|
|
|
|
StickerSet *sticker_set = get_sticker_set(sticker_set_id);
|
|
|
|
CHECK(sticker_set != nullptr);
|
2022-08-07 18:47:50 +02:00
|
|
|
CHECK(!sticker_set->is_loaded_);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2022-08-07 18:47:50 +02:00
|
|
|
sticker_set->load_requests_.push_back(load_request_id);
|
|
|
|
if (sticker_set->load_requests_.size() == 1u) {
|
|
|
|
if (G()->parameters().use_file_db && !sticker_set->was_loaded_) {
|
2019-09-18 05:55:43 +02:00
|
|
|
LOG(INFO) << "Trying to load " << sticker_set_id << " with stickers from database";
|
2018-12-31 20:04:05 +01:00
|
|
|
G()->td_db()->get_sqlite_pmc()->get(
|
|
|
|
get_full_sticker_set_database_key(sticker_set_id), PromiseCreator::lambda([sticker_set_id](string value) {
|
|
|
|
send_closure(G()->stickers_manager(), &StickersManager::on_load_sticker_set_from_database, sticker_set_id,
|
|
|
|
true, std::move(value));
|
|
|
|
}));
|
|
|
|
} else {
|
2019-09-18 05:55:43 +02:00
|
|
|
LOG(INFO) << "Trying to load " << sticker_set_id << " with stickers from server";
|
2021-11-19 11:56:12 +01:00
|
|
|
do_reload_sticker_set(sticker_set_id, get_input_sticker_set(sticker_set), 0, Auto());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-18 05:55:43 +02:00
|
|
|
void StickersManager::load_sticker_sets_without_stickers(vector<StickerSetId> &&sticker_set_ids,
|
|
|
|
Promise<Unit> &&promise) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (sticker_set_ids.empty()) {
|
|
|
|
promise.set_value(Unit());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-02-10 16:03:03 +01:00
|
|
|
CHECK(current_sticker_set_load_request_ < std::numeric_limits<uint32>::max());
|
|
|
|
auto load_request_id = ++current_sticker_set_load_request_;
|
2018-12-31 20:04:05 +01:00
|
|
|
StickerSetLoadRequest &load_request = sticker_set_load_requests_[load_request_id];
|
2022-08-07 18:47:50 +02:00
|
|
|
load_request.promise_ = std::move(promise);
|
|
|
|
load_request.left_queries_ = sticker_set_ids.size();
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
for (auto sticker_set_id : sticker_set_ids) {
|
|
|
|
StickerSet *sticker_set = get_sticker_set(sticker_set_id);
|
|
|
|
CHECK(sticker_set != nullptr);
|
2022-08-07 18:47:50 +02:00
|
|
|
CHECK(!sticker_set->is_inited_);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2022-08-07 18:47:50 +02:00
|
|
|
if (!sticker_set->load_requests_.empty()) {
|
|
|
|
sticker_set->load_requests_.push_back(load_request_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
2022-08-07 18:47:50 +02:00
|
|
|
sticker_set->load_without_stickers_requests_.push_back(load_request_id);
|
|
|
|
if (sticker_set->load_without_stickers_requests_.size() == 1u) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (G()->parameters().use_file_db) {
|
2019-09-18 05:55:43 +02:00
|
|
|
LOG(INFO) << "Trying to load " << sticker_set_id << " from database";
|
2018-12-31 20:04:05 +01:00
|
|
|
G()->td_db()->get_sqlite_pmc()->get(
|
|
|
|
get_sticker_set_database_key(sticker_set_id), PromiseCreator::lambda([sticker_set_id](string value) {
|
|
|
|
send_closure(G()->stickers_manager(), &StickersManager::on_load_sticker_set_from_database,
|
|
|
|
sticker_set_id, false, std::move(value));
|
|
|
|
}));
|
|
|
|
} else {
|
2019-09-18 05:55:43 +02:00
|
|
|
LOG(INFO) << "Trying to load " << sticker_set_id << " from server";
|
2021-11-19 11:56:12 +01:00
|
|
|
do_reload_sticker_set(sticker_set_id, get_input_sticker_set(sticker_set), 0, Auto());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-18 05:55:43 +02:00
|
|
|
void StickersManager::on_load_sticker_set_from_database(StickerSetId sticker_set_id, bool with_stickers, string value) {
|
2020-05-16 01:25:03 +02:00
|
|
|
if (G()->close_flag()) {
|
|
|
|
return;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
StickerSet *sticker_set = get_sticker_set(sticker_set_id);
|
|
|
|
CHECK(sticker_set != nullptr);
|
2022-08-07 18:47:50 +02:00
|
|
|
if (sticker_set->was_loaded_) {
|
2019-09-18 05:55:43 +02:00
|
|
|
LOG(INFO) << "Receive from database previously loaded " << sticker_set_id;
|
2018-12-31 20:04:05 +01:00
|
|
|
return;
|
|
|
|
}
|
2022-08-07 18:47:50 +02:00
|
|
|
if (!with_stickers && sticker_set->is_inited_) {
|
2019-09-18 05:55:43 +02:00
|
|
|
LOG(INFO) << "Receive from database previously inited " << sticker_set_id;
|
2018-12-31 20:04:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-13 20:35:37 +02:00
|
|
|
// it is possible that a server reload_sticker_set request has failed and cleared requests list with an error
|
2018-12-31 20:04:05 +01:00
|
|
|
if (with_stickers) {
|
2022-08-07 18:47:50 +02:00
|
|
|
// CHECK(!sticker_set->load_requests_.empty());
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
2022-08-07 18:47:50 +02:00
|
|
|
// CHECK(!sticker_set->load_without_stickers_requests_.empty());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2019-09-13 20:35:37 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
if (value.empty()) {
|
2020-04-18 00:55:54 +02:00
|
|
|
LOG(INFO) << "Failed to find in the database " << sticker_set_id;
|
2021-11-19 11:56:12 +01:00
|
|
|
return do_reload_sticker_set(sticker_set_id, get_input_sticker_set(sticker_set), 0, Auto());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2019-09-18 05:55:43 +02:00
|
|
|
LOG(INFO) << "Successfully loaded " << sticker_set_id << " with" << (with_stickers ? "" : "out")
|
2018-12-31 20:04:05 +01:00
|
|
|
<< " stickers of size " << value.size() << " from database";
|
|
|
|
|
2022-08-07 18:47:50 +02:00
|
|
|
auto old_sticker_count = sticker_set->sticker_ids_.size();
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
{
|
2022-08-07 18:47:50 +02:00
|
|
|
LOG_IF(ERROR, sticker_set->is_changed_) << sticker_set_id << " with" << (with_stickers ? "" : "out")
|
|
|
|
<< " stickers was changed before it is loaded from database";
|
2018-12-31 20:04:05 +01:00
|
|
|
LogEventParser parser(value);
|
|
|
|
parse_sticker_set(sticker_set, parser);
|
2022-08-07 18:47:50 +02:00
|
|
|
LOG_IF(ERROR, sticker_set->is_changed_)
|
2019-09-18 05:55:43 +02:00
|
|
|
<< sticker_set_id << " with" << (with_stickers ? "" : "out") << " stickers is changed";
|
2018-12-31 20:04:05 +01:00
|
|
|
parser.fetch_end();
|
2018-12-28 18:15:52 +01:00
|
|
|
auto status = parser.get_status();
|
|
|
|
if (status.is_error()) {
|
2019-06-13 22:16:12 +02:00
|
|
|
G()->td_db()->get_sqlite_sync_pmc()->erase(with_stickers ? get_full_sticker_set_database_key(sticker_set_id)
|
|
|
|
: get_sticker_set_database_key(sticker_set_id));
|
|
|
|
// need to crash, because the current StickerSet state is spoiled by parse_sticker_set
|
2019-09-18 05:55:43 +02:00
|
|
|
LOG(FATAL) << "Failed to parse " << sticker_set_id << ": " << status << ' '
|
2018-12-28 18:15:52 +01:00
|
|
|
<< format::as_hex_dump<4>(Slice(value));
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2022-09-26 23:20:13 +02:00
|
|
|
if (!sticker_set->are_keywords_loaded_ || !sticker_set->is_thumbnail_reloaded_ ||
|
|
|
|
!sticker_set->are_legacy_sticker_thumbnails_reloaded_) {
|
2021-11-19 11:56:12 +01:00
|
|
|
do_reload_sticker_set(sticker_set_id, get_input_sticker_set(sticker_set), 0, Auto());
|
2019-05-17 17:08:17 +02:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2022-08-07 18:47:50 +02:00
|
|
|
if (with_stickers && old_sticker_count < get_max_featured_sticker_count(sticker_set->sticker_type_) &&
|
|
|
|
old_sticker_count < sticker_set->sticker_ids_.size()) {
|
|
|
|
sticker_set->need_save_to_database_ = true;
|
2021-11-14 08:19:03 +01:00
|
|
|
update_sticker_set(sticker_set, "on_load_sticker_set_from_database");
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
update_load_requests(sticker_set, with_stickers, Status::OK());
|
|
|
|
}
|
|
|
|
|
2019-09-18 05:55:43 +02:00
|
|
|
void StickersManager::reload_sticker_set(StickerSetId sticker_set_id, int64 access_hash, Promise<Unit> &&promise) {
|
|
|
|
do_reload_sticker_set(sticker_set_id,
|
2021-11-19 11:56:12 +01:00
|
|
|
make_tl_object<telegram_api::inputStickerSetID>(sticker_set_id.get(), access_hash), 0,
|
2019-07-30 17:43:29 +02:00
|
|
|
std::move(promise));
|
|
|
|
}
|
|
|
|
|
2019-09-18 05:55:43 +02:00
|
|
|
void StickersManager::do_reload_sticker_set(StickerSetId sticker_set_id,
|
2019-07-30 17:43:29 +02:00
|
|
|
tl_object_ptr<telegram_api::InputStickerSet> &&input_sticker_set,
|
2021-11-19 11:56:12 +01:00
|
|
|
int32 hash, Promise<Unit> &&promise) const {
|
2021-10-07 15:36:21 +02:00
|
|
|
TRY_STATUS_PROMISE(promise, G()->close_status());
|
2021-11-19 11:56:12 +01:00
|
|
|
td_->create_handler<GetStickerSetQuery>(std::move(promise))->send(sticker_set_id, std::move(input_sticker_set), hash);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2019-09-18 05:55:43 +02:00
|
|
|
void StickersManager::on_install_sticker_set(StickerSetId set_id, bool is_archived,
|
2018-12-31 20:04:05 +01:00
|
|
|
tl_object_ptr<telegram_api::messages_StickerSetInstallResult> &&result) {
|
|
|
|
StickerSet *sticker_set = get_sticker_set(set_id);
|
|
|
|
CHECK(sticker_set != nullptr);
|
|
|
|
on_update_sticker_set(sticker_set, true, is_archived, true);
|
2021-11-14 08:19:03 +01:00
|
|
|
update_sticker_set(sticker_set, "on_install_sticker_set");
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
switch (result->get_id()) {
|
|
|
|
case telegram_api::messages_stickerSetInstallResultSuccess::ID:
|
|
|
|
break;
|
|
|
|
case telegram_api::messages_stickerSetInstallResultArchive::ID: {
|
|
|
|
auto archived_sets = move_tl_object_as<telegram_api::messages_stickerSetInstallResultArchive>(result);
|
|
|
|
for (auto &archived_set_ptr : archived_sets->sets_) {
|
2019-12-29 02:59:34 +01:00
|
|
|
StickerSetId archived_sticker_set_id =
|
|
|
|
on_get_sticker_set_covered(std::move(archived_set_ptr), true, "on_install_sticker_set");
|
2019-09-18 05:55:43 +02:00
|
|
|
if (archived_sticker_set_id.is_valid()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto archived_sticker_set = get_sticker_set(archived_sticker_set_id);
|
|
|
|
CHECK(archived_sticker_set != nullptr);
|
2021-11-14 08:19:03 +01:00
|
|
|
update_sticker_set(archived_sticker_set, "on_install_sticker_set 2");
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
|
|
|
|
send_update_installed_sticker_sets();
|
|
|
|
}
|
|
|
|
|
2019-09-18 05:55:43 +02:00
|
|
|
void StickersManager::on_uninstall_sticker_set(StickerSetId set_id) {
|
2018-12-31 20:04:05 +01:00
|
|
|
StickerSet *sticker_set = get_sticker_set(set_id);
|
|
|
|
CHECK(sticker_set != nullptr);
|
|
|
|
on_update_sticker_set(sticker_set, false, false, true);
|
2021-11-14 08:19:03 +01:00
|
|
|
update_sticker_set(sticker_set, "on_uninstall_sticker_set");
|
2018-12-31 20:04:05 +01:00
|
|
|
send_update_installed_sticker_sets();
|
|
|
|
}
|
|
|
|
|
2020-04-19 10:17:51 +02:00
|
|
|
td_api::object_ptr<td_api::updateDiceEmojis> StickersManager::get_update_dice_emojis_object() const {
|
2020-04-19 15:30:04 +02:00
|
|
|
return td_api::make_object<td_api::updateDiceEmojis>(vector<string>(dice_emojis_));
|
2020-04-19 10:17:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void StickersManager::on_update_dice_emojis() {
|
2020-04-21 19:12:35 +02:00
|
|
|
if (G()->close_flag()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (td_->auth_manager_->is_bot()) {
|
2022-08-18 16:00:18 +02:00
|
|
|
td_->option_manager_->set_option_empty("dice_emojis");
|
2020-04-21 19:12:35 +02:00
|
|
|
return;
|
|
|
|
}
|
2020-06-04 00:19:37 +02:00
|
|
|
if (!is_inited_) {
|
|
|
|
return;
|
|
|
|
}
|
2020-04-21 19:12:35 +02:00
|
|
|
|
2022-08-18 16:00:18 +02:00
|
|
|
auto dice_emojis_str =
|
|
|
|
td_->option_manager_->get_option_string("dice_emojis", "🎲\x01🎯\x01🏀\x01⚽\x01⚽️\x01🎰\x01🎳");
|
2020-04-19 15:30:04 +02:00
|
|
|
if (dice_emojis_str == dice_emojis_str_) {
|
2020-04-19 10:17:51 +02:00
|
|
|
return;
|
|
|
|
}
|
2020-04-19 15:30:04 +02:00
|
|
|
dice_emojis_str_ = std::move(dice_emojis_str);
|
2020-05-17 00:06:23 +02:00
|
|
|
auto new_dice_emojis = full_split(dice_emojis_str_, '\x01');
|
|
|
|
for (auto &emoji : new_dice_emojis) {
|
|
|
|
if (!td::contains(dice_emojis_, emoji)) {
|
|
|
|
auto &special_sticker_set = add_special_sticker_set(SpecialStickerSetType::animated_dice(emoji));
|
2021-10-08 11:18:01 +02:00
|
|
|
if (special_sticker_set.id_.is_valid()) {
|
|
|
|
// drop information about the sticker set to reload it
|
|
|
|
special_sticker_set.id_ = StickerSetId();
|
|
|
|
special_sticker_set.access_hash_ = 0;
|
|
|
|
special_sticker_set.short_name_.clear();
|
|
|
|
}
|
2020-05-17 00:06:23 +02:00
|
|
|
|
2020-06-04 00:19:37 +02:00
|
|
|
if (G()->parameters().use_file_db) {
|
|
|
|
LOG(INFO) << "Load new dice sticker set for emoji " << emoji;
|
|
|
|
load_special_sticker_set(special_sticker_set);
|
|
|
|
}
|
2020-05-17 00:06:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
dice_emojis_ = std::move(new_dice_emojis);
|
2020-04-19 10:17:51 +02:00
|
|
|
|
|
|
|
send_closure(G()->td(), &Td::send_update, get_update_dice_emojis_object());
|
|
|
|
}
|
|
|
|
|
2020-04-21 13:51:46 +02:00
|
|
|
void StickersManager::on_update_dice_success_values() {
|
2020-04-21 19:12:35 +02:00
|
|
|
if (G()->close_flag()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (td_->auth_manager_->is_bot()) {
|
2022-08-18 16:00:18 +02:00
|
|
|
td_->option_manager_->set_option_empty("dice_success_values");
|
2020-04-21 19:12:35 +02:00
|
|
|
return;
|
|
|
|
}
|
2020-06-04 00:19:37 +02:00
|
|
|
if (!is_inited_) {
|
|
|
|
return;
|
|
|
|
}
|
2020-04-21 19:12:35 +02:00
|
|
|
|
2022-08-18 16:00:18 +02:00
|
|
|
auto dice_success_values_str =
|
|
|
|
td_->option_manager_->get_option_string("dice_success_values", "0,6:62,5:110,5:110,5:110,64:110,6:110");
|
2020-04-21 13:51:46 +02:00
|
|
|
if (dice_success_values_str == dice_success_values_str_) {
|
|
|
|
return;
|
|
|
|
}
|
2020-06-04 00:19:37 +02:00
|
|
|
|
|
|
|
LOG(INFO) << "Change dice success values to " << dice_success_values_str;
|
2020-04-21 13:51:46 +02:00
|
|
|
dice_success_values_str_ = std::move(dice_success_values_str);
|
2020-04-21 18:08:01 +02:00
|
|
|
dice_success_values_ = transform(full_split(dice_success_values_str_, ','), [](Slice value) {
|
|
|
|
auto result = split(value, ':');
|
|
|
|
return std::make_pair(to_integer<int32>(result.first), to_integer<int32>(result.second));
|
|
|
|
});
|
2020-04-21 13:51:46 +02:00
|
|
|
}
|
|
|
|
|
2021-10-05 22:20:49 +02:00
|
|
|
void StickersManager::on_update_emoji_sounds() {
|
|
|
|
if (G()->close_flag() || !is_inited_ || td_->auth_manager_->is_bot()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-08-18 16:00:18 +02:00
|
|
|
auto emoji_sounds_str = td_->option_manager_->get_option_string("emoji_sounds");
|
2021-10-05 22:20:49 +02:00
|
|
|
if (emoji_sounds_str == emoji_sounds_str_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG(INFO) << "Change emoji sounds to " << emoji_sounds_str;
|
|
|
|
emoji_sounds_str_ = std::move(emoji_sounds_str);
|
|
|
|
|
2021-10-08 12:41:22 +02:00
|
|
|
vector<FileId> old_file_ids;
|
|
|
|
for (auto &emoji_sound : emoji_sounds_) {
|
|
|
|
old_file_ids.push_back(emoji_sound.second);
|
|
|
|
}
|
2021-10-05 22:20:49 +02:00
|
|
|
emoji_sounds_.clear();
|
2021-10-08 12:41:22 +02:00
|
|
|
|
|
|
|
vector<FileId> new_file_ids;
|
2021-10-05 22:20:49 +02:00
|
|
|
auto sounds = full_split(Slice(emoji_sounds_str_), ',');
|
|
|
|
CHECK(sounds.size() % 2 == 0);
|
|
|
|
for (size_t i = 0; i < sounds.size(); i += 2) {
|
|
|
|
vector<Slice> parts = full_split(sounds[i + 1], ':');
|
|
|
|
CHECK(parts.size() == 3);
|
|
|
|
auto id = to_integer<int64>(parts[0]);
|
|
|
|
auto access_hash = to_integer<int64>(parts[1]);
|
2021-10-05 23:55:22 +02:00
|
|
|
auto dc_id = G()->net_query_dispatcher().get_main_dc_id();
|
2021-10-05 22:20:49 +02:00
|
|
|
auto file_reference = base64url_decode(parts[2]).move_as_ok();
|
2021-10-05 23:55:22 +02:00
|
|
|
int32 expected_size = 7000;
|
2021-10-05 22:20:49 +02:00
|
|
|
auto suggested_file_name = PSTRING() << static_cast<uint64>(id) << '.'
|
|
|
|
<< MimeType::to_extension("audio/ogg", "oga");
|
|
|
|
auto file_id = td_->file_manager_->register_remote(
|
2021-10-05 23:55:22 +02:00
|
|
|
FullRemoteFileLocation(FileType::VoiceNote, id, access_hash, dc_id, std::move(file_reference)),
|
|
|
|
FileLocationSource::FromServer, DialogId(), 0, expected_size, std::move(suggested_file_name));
|
2021-10-05 22:20:49 +02:00
|
|
|
CHECK(file_id.is_valid());
|
2022-02-10 16:03:03 +01:00
|
|
|
auto cleaned_emoji = remove_fitzpatrick_modifier(sounds[i]).str();
|
|
|
|
if (!cleaned_emoji.empty()) {
|
|
|
|
emoji_sounds_.emplace(cleaned_emoji, file_id);
|
|
|
|
new_file_ids.push_back(file_id);
|
|
|
|
}
|
2021-10-05 22:20:49 +02:00
|
|
|
}
|
2021-10-08 12:41:22 +02:00
|
|
|
td_->file_manager_->change_files_source(get_app_config_file_source_id(), old_file_ids, new_file_ids);
|
|
|
|
|
2021-10-05 22:20:49 +02:00
|
|
|
try_update_animated_emoji_messages();
|
|
|
|
}
|
|
|
|
|
2021-10-05 16:17:29 +02:00
|
|
|
void StickersManager::on_update_disable_animated_emojis() {
|
2021-10-05 22:20:49 +02:00
|
|
|
if (G()->close_flag() || !is_inited_ || td_->auth_manager_->is_bot()) {
|
2021-10-05 16:17:29 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-08-18 16:00:18 +02:00
|
|
|
auto disable_animated_emojis = td_->option_manager_->get_option_boolean("disable_animated_emoji");
|
2021-10-05 16:17:29 +02:00
|
|
|
if (disable_animated_emojis == disable_animated_emojis_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
disable_animated_emojis_ = disable_animated_emojis;
|
|
|
|
if (!disable_animated_emojis_) {
|
2021-12-01 20:05:40 +01:00
|
|
|
reload_special_sticker_set_by_type(SpecialStickerSetType::animated_emoji());
|
|
|
|
reload_special_sticker_set_by_type(SpecialStickerSetType::animated_emoji_click());
|
2021-10-05 16:17:29 +02:00
|
|
|
}
|
|
|
|
try_update_animated_emoji_messages();
|
2022-08-05 20:56:05 +02:00
|
|
|
|
2022-10-03 00:26:32 +02:00
|
|
|
vector<CustomEmojiId> custom_emoji_ids;
|
2022-08-05 20:56:05 +02:00
|
|
|
for (auto &it : custom_emoji_messages_) {
|
|
|
|
custom_emoji_ids.push_back(it.first);
|
|
|
|
}
|
|
|
|
for (auto custom_emoji_id : custom_emoji_ids) {
|
|
|
|
try_update_custom_emoji_messages(custom_emoji_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!disable_animated_emojis_) {
|
|
|
|
for (size_t i = 0; i < custom_emoji_ids.size(); i += MAX_GET_CUSTOM_EMOJI_STICKERS) {
|
2022-10-03 00:26:32 +02:00
|
|
|
auto end_i = td::min(i + MAX_GET_CUSTOM_EMOJI_STICKERS, custom_emoji_ids.size());
|
|
|
|
get_custom_emoji_stickers({custom_emoji_ids.begin() + i, custom_emoji_ids.begin() + end_i}, true, Auto());
|
2022-08-05 20:56:05 +02:00
|
|
|
}
|
|
|
|
}
|
2021-10-05 16:17:29 +02:00
|
|
|
}
|
|
|
|
|
2022-08-23 17:24:21 +02:00
|
|
|
void StickersManager::on_update_sticker_sets(StickerType sticker_type) {
|
|
|
|
auto type = static_cast<int32>(sticker_type);
|
|
|
|
archived_sticker_set_ids_[type].clear();
|
|
|
|
total_archived_sticker_set_count_[type] = -1;
|
|
|
|
reload_installed_sticker_sets(sticker_type, true);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-10-05 16:17:29 +02:00
|
|
|
void StickersManager::try_update_animated_emoji_messages() {
|
2021-10-05 17:06:05 +02:00
|
|
|
auto sticker_set = get_animated_emoji_sticker_set();
|
2021-10-05 16:17:29 +02:00
|
|
|
vector<FullMessageId> full_message_ids;
|
2021-10-05 17:06:05 +02:00
|
|
|
for (auto &it : emoji_messages_) {
|
|
|
|
auto new_animated_sticker = get_animated_emoji_sticker(sticker_set, it.first);
|
2021-10-05 22:20:49 +02:00
|
|
|
auto new_sound_file_id = get_animated_emoji_sound_file_id(it.first);
|
2022-08-07 18:47:50 +02:00
|
|
|
if (new_animated_sticker != it.second->animated_emoji_sticker_ ||
|
|
|
|
(new_animated_sticker.first.is_valid() && new_sound_file_id != it.second->sound_file_id_)) {
|
|
|
|
it.second->animated_emoji_sticker_ = new_animated_sticker;
|
|
|
|
it.second->sound_file_id_ = new_sound_file_id;
|
2022-08-20 23:20:57 +02:00
|
|
|
it.second->full_message_ids_.foreach(
|
|
|
|
[&](const FullMessageId &full_message_id) { full_message_ids.push_back(full_message_id); });
|
2021-10-05 16:17:29 +02:00
|
|
|
}
|
|
|
|
}
|
2021-11-10 18:39:35 +01:00
|
|
|
for (const auto &full_message_id : full_message_ids) {
|
2021-10-05 16:17:29 +02:00
|
|
|
td_->messages_manager_->on_external_update_message_content(full_message_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-03 00:26:32 +02:00
|
|
|
void StickersManager::try_update_custom_emoji_messages(CustomEmojiId custom_emoji_id) {
|
2022-08-05 19:49:24 +02:00
|
|
|
auto it = custom_emoji_messages_.find(custom_emoji_id);
|
|
|
|
if (it == custom_emoji_messages_.end()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
vector<FullMessageId> full_message_ids;
|
2022-08-05 20:56:05 +02:00
|
|
|
auto new_sticker_id = get_custom_animated_emoji_sticker_id(custom_emoji_id);
|
2022-08-07 18:47:50 +02:00
|
|
|
if (new_sticker_id != it->second->sticker_id_) {
|
|
|
|
it->second->sticker_id_ = new_sticker_id;
|
2022-08-20 23:20:57 +02:00
|
|
|
it->second->full_message_ids_.foreach(
|
|
|
|
[&](const FullMessageId &full_message_id) { full_message_ids.push_back(full_message_id); });
|
2022-08-05 19:49:24 +02:00
|
|
|
}
|
|
|
|
for (const auto &full_message_id : full_message_ids) {
|
|
|
|
td_->messages_manager_->on_external_update_message_content(full_message_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-22 14:01:44 +02:00
|
|
|
void StickersManager::try_update_premium_gift_messages() {
|
|
|
|
auto sticker_set = get_premium_gift_sticker_set();
|
|
|
|
vector<FullMessageId> full_message_ids;
|
|
|
|
for (auto &it : premium_gift_messages_) {
|
|
|
|
auto new_sticker_id = get_premium_gift_option_sticker_id(sticker_set, it.first);
|
2022-08-07 18:47:50 +02:00
|
|
|
if (new_sticker_id != it.second->sticker_id_) {
|
|
|
|
it.second->sticker_id_ = new_sticker_id;
|
|
|
|
for (const auto &full_message_id : it.second->full_message_ids_) {
|
2022-07-22 14:01:44 +02:00
|
|
|
full_message_ids.push_back(full_message_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (const auto &full_message_id : full_message_ids) {
|
|
|
|
td_->messages_manager_->on_external_update_message_content(full_message_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void StickersManager::register_premium_gift(int32 months, FullMessageId full_message_id, const char *source) {
|
2022-07-22 12:30:24 +02:00
|
|
|
if (td_->auth_manager_->is_bot() || months == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG(INFO) << "Register premium gift for " << months << " months from " << full_message_id << " from " << source;
|
2022-07-22 14:01:44 +02:00
|
|
|
auto &premium_gift_messages_ptr = premium_gift_messages_[months];
|
|
|
|
if (premium_gift_messages_ptr == nullptr) {
|
|
|
|
premium_gift_messages_ptr = make_unique<GiftPremiumMessages>();
|
2022-07-22 12:30:24 +02:00
|
|
|
}
|
2022-07-22 14:01:44 +02:00
|
|
|
auto &premium_gift_messages = *premium_gift_messages_ptr;
|
2022-07-22 12:30:24 +02:00
|
|
|
|
2022-08-07 18:47:50 +02:00
|
|
|
if (premium_gift_messages.full_message_ids_.empty()) {
|
|
|
|
premium_gift_messages.sticker_id_ = get_premium_gift_option_sticker_id(months);
|
2022-07-22 12:30:24 +02:00
|
|
|
}
|
2022-07-22 14:01:44 +02:00
|
|
|
|
2022-08-07 18:47:50 +02:00
|
|
|
bool is_inserted = premium_gift_messages.full_message_ids_.insert(full_message_id).second;
|
2022-07-22 14:01:44 +02:00
|
|
|
LOG_CHECK(is_inserted) << source << " " << months << " " << full_message_id;
|
2022-07-22 12:30:24 +02:00
|
|
|
}
|
|
|
|
|
2022-07-22 14:01:44 +02:00
|
|
|
void StickersManager::unregister_premium_gift(int32 months, FullMessageId full_message_id, const char *source) {
|
2022-07-22 12:30:24 +02:00
|
|
|
if (td_->auth_manager_->is_bot() || months == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG(INFO) << "Unregister premium gift for " << months << " months from " << full_message_id << " from " << source;
|
2022-07-22 14:01:44 +02:00
|
|
|
auto it = premium_gift_messages_.find(months);
|
|
|
|
CHECK(it != premium_gift_messages_.end());
|
2022-08-07 18:47:50 +02:00
|
|
|
auto &message_ids = it->second->full_message_ids_;
|
2022-07-22 12:30:24 +02:00
|
|
|
auto is_deleted = message_ids.erase(full_message_id) > 0;
|
|
|
|
LOG_CHECK(is_deleted) << source << " " << months << " " << full_message_id;
|
|
|
|
|
|
|
|
if (message_ids.empty()) {
|
2022-07-22 14:01:44 +02:00
|
|
|
premium_gift_messages_.erase(it);
|
2022-07-22 12:30:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-19 23:41:26 +02:00
|
|
|
void StickersManager::register_dice(const string &emoji, int32 value, FullMessageId full_message_id,
|
|
|
|
const char *source) {
|
|
|
|
CHECK(!emoji.empty());
|
|
|
|
if (td_->auth_manager_->is_bot()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG(INFO) << "Register dice " << emoji << " with value " << value << " from " << full_message_id << " from "
|
|
|
|
<< source;
|
2022-08-20 23:20:57 +02:00
|
|
|
dice_messages_[emoji].insert(full_message_id);
|
2020-04-19 23:41:26 +02:00
|
|
|
|
2020-05-17 00:06:23 +02:00
|
|
|
if (!td::contains(dice_emojis_, emoji)) {
|
|
|
|
if (full_message_id.get_message_id().is_any_server() &&
|
|
|
|
full_message_id.get_dialog_id().get_type() != DialogType::SecretChat) {
|
2021-10-06 00:17:10 +02:00
|
|
|
send_closure(G()->config_manager(), &ConfigManager::reget_app_config, Promise<Unit>());
|
2020-05-17 00:06:23 +02:00
|
|
|
}
|
2020-04-19 23:41:26 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto &special_sticker_set = add_special_sticker_set(SpecialStickerSetType::animated_dice(emoji));
|
|
|
|
bool need_load = false;
|
2021-12-01 18:30:02 +01:00
|
|
|
StickerSet *sticker_set = nullptr;
|
2020-04-19 23:41:26 +02:00
|
|
|
if (!special_sticker_set.id_.is_valid()) {
|
|
|
|
need_load = true;
|
|
|
|
} else {
|
2021-12-01 18:30:02 +01:00
|
|
|
sticker_set = get_sticker_set(special_sticker_set.id_);
|
2020-04-19 23:41:26 +02:00
|
|
|
CHECK(sticker_set != nullptr);
|
2022-08-07 18:47:50 +02:00
|
|
|
need_load = !sticker_set->was_loaded_;
|
2020-04-19 23:41:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (need_load) {
|
|
|
|
LOG(INFO) << "Waiting for a dice sticker set needed in " << full_message_id;
|
2021-09-24 12:08:32 +02:00
|
|
|
load_special_sticker_set(special_sticker_set);
|
2020-04-19 23:41:26 +02:00
|
|
|
} else {
|
|
|
|
// TODO reload once in a while
|
2022-08-07 18:47:50 +02:00
|
|
|
// reload_special_sticker_set(special_sticker_set, sticker_set->is_loaded_ ? sticker_set->hash_ : 0);
|
2020-04-19 23:41:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void StickersManager::unregister_dice(const string &emoji, int32 value, FullMessageId full_message_id,
|
|
|
|
const char *source) {
|
|
|
|
CHECK(!emoji.empty());
|
|
|
|
if (td_->auth_manager_->is_bot()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG(INFO) << "Unregister dice " << emoji << " with value " << value << " from " << full_message_id << " from "
|
|
|
|
<< source;
|
|
|
|
auto &message_ids = dice_messages_[emoji];
|
2020-10-08 22:04:40 +02:00
|
|
|
auto is_deleted = message_ids.erase(full_message_id) > 0;
|
2020-04-19 23:41:26 +02:00
|
|
|
LOG_CHECK(is_deleted) << source << " " << emoji << " " << value << " " << full_message_id;
|
|
|
|
|
|
|
|
if (message_ids.empty()) {
|
|
|
|
dice_messages_.erase(emoji);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-03 00:26:32 +02:00
|
|
|
void StickersManager::register_emoji(const string &emoji, CustomEmojiId custom_emoji_id, FullMessageId full_message_id,
|
2022-08-05 16:58:16 +02:00
|
|
|
const char *source) {
|
2021-10-04 15:08:51 +02:00
|
|
|
CHECK(!emoji.empty());
|
|
|
|
if (td_->auth_manager_->is_bot()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-10-03 00:26:32 +02:00
|
|
|
LOG(INFO) << "Register emoji " << emoji << " with " << custom_emoji_id << " from " << full_message_id << " from "
|
|
|
|
<< source;
|
|
|
|
if (custom_emoji_id.is_valid()) {
|
2022-08-05 19:49:24 +02:00
|
|
|
auto &emoji_messages_ptr = custom_emoji_messages_[custom_emoji_id];
|
|
|
|
if (emoji_messages_ptr == nullptr) {
|
|
|
|
emoji_messages_ptr = make_unique<CustomEmojiMessages>();
|
|
|
|
}
|
|
|
|
auto &emoji_messages = *emoji_messages_ptr;
|
2022-08-07 18:47:50 +02:00
|
|
|
if (emoji_messages.full_message_ids_.empty()) {
|
2022-09-05 20:04:08 +02:00
|
|
|
if (!disable_animated_emojis_ && custom_emoji_to_sticker_id_.count(custom_emoji_id) == 0) {
|
|
|
|
load_custom_emoji_sticker_from_database_force(custom_emoji_id);
|
|
|
|
if (custom_emoji_to_sticker_id_.count(custom_emoji_id) == 0) {
|
|
|
|
get_custom_emoji_stickers({custom_emoji_id}, false, Promise<td_api::object_ptr<td_api::stickers>>());
|
|
|
|
}
|
2022-08-05 20:56:05 +02:00
|
|
|
}
|
2022-09-05 20:04:08 +02:00
|
|
|
emoji_messages.sticker_id_ = get_custom_animated_emoji_sticker_id(custom_emoji_id);
|
2022-08-05 19:49:24 +02:00
|
|
|
}
|
2022-08-20 23:20:57 +02:00
|
|
|
emoji_messages.full_message_ids_.insert(full_message_id);
|
2022-08-05 16:58:16 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-02-11 17:27:32 +01:00
|
|
|
auto &emoji_messages_ptr = emoji_messages_[emoji];
|
|
|
|
if (emoji_messages_ptr == nullptr) {
|
|
|
|
emoji_messages_ptr = make_unique<EmojiMessages>();
|
|
|
|
}
|
|
|
|
auto &emoji_messages = *emoji_messages_ptr;
|
2022-08-07 18:47:50 +02:00
|
|
|
if (emoji_messages.full_message_ids_.empty()) {
|
|
|
|
emoji_messages.animated_emoji_sticker_ = get_animated_emoji_sticker(emoji);
|
|
|
|
emoji_messages.sound_file_id_ = get_animated_emoji_sound_file_id(emoji);
|
2021-10-04 15:08:51 +02:00
|
|
|
}
|
2022-08-20 23:20:57 +02:00
|
|
|
emoji_messages.full_message_ids_.insert(full_message_id);
|
2021-10-04 15:08:51 +02:00
|
|
|
}
|
|
|
|
|
2022-10-03 00:26:32 +02:00
|
|
|
void StickersManager::unregister_emoji(const string &emoji, CustomEmojiId custom_emoji_id,
|
|
|
|
FullMessageId full_message_id, const char *source) {
|
2021-10-04 15:08:51 +02:00
|
|
|
CHECK(!emoji.empty());
|
|
|
|
if (td_->auth_manager_->is_bot()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-10-03 00:26:32 +02:00
|
|
|
LOG(INFO) << "Unregister emoji " << emoji << " with " << custom_emoji_id << " from " << full_message_id << " from "
|
|
|
|
<< source;
|
|
|
|
if (custom_emoji_id.is_valid()) {
|
2022-08-05 19:49:24 +02:00
|
|
|
auto it = custom_emoji_messages_.find(custom_emoji_id);
|
|
|
|
CHECK(it != custom_emoji_messages_.end());
|
2022-08-07 18:47:50 +02:00
|
|
|
auto &full_message_ids = it->second->full_message_ids_;
|
2022-08-05 19:49:24 +02:00
|
|
|
auto is_deleted = full_message_ids.erase(full_message_id) > 0;
|
|
|
|
LOG_CHECK(is_deleted) << source << ' ' << custom_emoji_id << ' ' << full_message_id;
|
|
|
|
|
|
|
|
if (full_message_ids.empty()) {
|
|
|
|
custom_emoji_messages_.erase(it);
|
|
|
|
}
|
2022-08-05 16:58:16 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-10-05 17:06:05 +02:00
|
|
|
auto it = emoji_messages_.find(emoji);
|
|
|
|
CHECK(it != emoji_messages_.end());
|
2022-08-07 18:47:50 +02:00
|
|
|
auto &full_message_ids = it->second->full_message_ids_;
|
2021-10-05 17:06:05 +02:00
|
|
|
auto is_deleted = full_message_ids.erase(full_message_id) > 0;
|
|
|
|
LOG_CHECK(is_deleted) << source << ' ' << emoji << ' ' << full_message_id;
|
|
|
|
|
|
|
|
if (full_message_ids.empty()) {
|
|
|
|
emoji_messages_.erase(it);
|
2021-10-04 15:08:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-26 16:59:15 +02:00
|
|
|
void StickersManager::get_animated_emoji(string emoji, bool is_recursive,
|
|
|
|
Promise<td_api::object_ptr<td_api::animatedEmoji>> &&promise) {
|
|
|
|
TRY_STATUS_PROMISE(promise, G()->close_status());
|
|
|
|
|
|
|
|
auto &special_sticker_set = add_special_sticker_set(SpecialStickerSetType::animated_emoji());
|
|
|
|
auto sticker_set = get_sticker_set(special_sticker_set.id_);
|
2022-08-07 18:47:50 +02:00
|
|
|
if (sticker_set == nullptr || !sticker_set->was_loaded_) {
|
2021-10-26 16:59:15 +02:00
|
|
|
if (is_recursive) {
|
|
|
|
return promise.set_value(nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
pending_get_animated_emoji_queries_.push_back(
|
|
|
|
PromiseCreator::lambda([actor_id = actor_id(this), emoji = std::move(emoji),
|
|
|
|
promise = std::move(promise)](Result<Unit> &&result) mutable {
|
|
|
|
if (result.is_error()) {
|
|
|
|
promise.set_error(result.move_as_error());
|
|
|
|
} else {
|
|
|
|
send_closure(actor_id, &StickersManager::get_animated_emoji, std::move(emoji), true, std::move(promise));
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
load_special_sticker_set(special_sticker_set);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
promise.set_value(get_animated_emoji_object(get_animated_emoji_sticker(sticker_set, emoji),
|
|
|
|
get_animated_emoji_sound_file_id(emoji)));
|
|
|
|
}
|
|
|
|
|
2022-05-23 13:05:35 +02:00
|
|
|
void StickersManager::get_all_animated_emojis(bool is_recursive,
|
|
|
|
Promise<td_api::object_ptr<td_api::emojis>> &&promise) {
|
|
|
|
TRY_STATUS_PROMISE(promise, G()->close_status());
|
|
|
|
|
|
|
|
auto &special_sticker_set = add_special_sticker_set(SpecialStickerSetType::animated_emoji());
|
|
|
|
auto sticker_set = get_sticker_set(special_sticker_set.id_);
|
2022-08-07 18:47:50 +02:00
|
|
|
if (sticker_set == nullptr || !sticker_set->was_loaded_) {
|
2022-05-23 13:05:35 +02:00
|
|
|
if (is_recursive) {
|
|
|
|
return promise.set_value(td_api::make_object<td_api::emojis>());
|
|
|
|
}
|
|
|
|
|
|
|
|
pending_get_animated_emoji_queries_.push_back(PromiseCreator::lambda(
|
|
|
|
[actor_id = actor_id(this), promise = std::move(promise)](Result<Unit> &&result) mutable {
|
|
|
|
if (result.is_error()) {
|
|
|
|
promise.set_error(result.move_as_error());
|
|
|
|
} else {
|
|
|
|
send_closure(actor_id, &StickersManager::get_all_animated_emojis, true, std::move(promise));
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
load_special_sticker_set(special_sticker_set);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-08-07 18:47:50 +02:00
|
|
|
auto emojis = transform(sticker_set->sticker_ids_, [&](FileId sticker_id) {
|
2022-05-23 13:05:35 +02:00
|
|
|
auto s = get_sticker(sticker_id);
|
|
|
|
CHECK(s != nullptr);
|
2022-08-07 18:47:50 +02:00
|
|
|
return s->alt_;
|
2022-05-23 13:05:35 +02:00
|
|
|
});
|
|
|
|
promise.set_value(td_api::make_object<td_api::emojis>(std::move(emojis)));
|
|
|
|
}
|
|
|
|
|
2022-09-12 14:44:37 +02:00
|
|
|
void StickersManager::get_custom_emoji_reaction_generic_animations(
|
2022-09-30 14:55:46 +02:00
|
|
|
bool is_recursive, Promise<td_api::object_ptr<td_api::stickers>> &&promise) {
|
2022-09-12 14:44:37 +02:00
|
|
|
TRY_STATUS_PROMISE(promise, G()->close_status());
|
|
|
|
|
|
|
|
auto &special_sticker_set = add_special_sticker_set(SpecialStickerSetType::generic_animations());
|
|
|
|
auto sticker_set = get_sticker_set(special_sticker_set.id_);
|
|
|
|
if (sticker_set == nullptr || !sticker_set->was_loaded_) {
|
|
|
|
if (is_recursive) {
|
2022-09-30 14:55:46 +02:00
|
|
|
return promise.set_value(td_api::make_object<td_api::stickers>());
|
2022-09-12 14:44:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pending_get_generic_animations_queries_.push_back(PromiseCreator::lambda(
|
|
|
|
[actor_id = actor_id(this), promise = std::move(promise)](Result<Unit> &&result) mutable {
|
|
|
|
if (result.is_error()) {
|
|
|
|
promise.set_error(result.move_as_error());
|
|
|
|
} else {
|
|
|
|
send_closure(actor_id, &StickersManager::get_custom_emoji_reaction_generic_animations, true,
|
|
|
|
std::move(promise));
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
load_special_sticker_set(special_sticker_set);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-09-30 14:55:46 +02:00
|
|
|
promise.set_value(get_stickers_object(sticker_set->sticker_ids_));
|
2022-09-12 14:44:37 +02:00
|
|
|
}
|
|
|
|
|
2022-09-02 14:02:37 +02:00
|
|
|
void StickersManager::get_default_emoji_statuses(bool is_recursive,
|
2022-09-07 09:25:21 +02:00
|
|
|
Promise<td_api::object_ptr<td_api::emojiStatuses>> &&promise) {
|
2022-09-02 14:02:37 +02:00
|
|
|
TRY_STATUS_PROMISE(promise, G()->close_status());
|
|
|
|
|
|
|
|
auto &special_sticker_set = add_special_sticker_set(SpecialStickerSetType::default_statuses());
|
|
|
|
auto sticker_set = get_sticker_set(special_sticker_set.id_);
|
|
|
|
if (sticker_set == nullptr || !sticker_set->was_loaded_) {
|
|
|
|
if (is_recursive) {
|
2022-09-07 09:25:21 +02:00
|
|
|
return promise.set_value(td_api::make_object<td_api::emojiStatuses>());
|
2022-09-02 14:02:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pending_get_default_statuses_queries_.push_back(PromiseCreator::lambda(
|
|
|
|
[actor_id = actor_id(this), promise = std::move(promise)](Result<Unit> &&result) mutable {
|
|
|
|
if (result.is_error()) {
|
|
|
|
promise.set_error(result.move_as_error());
|
|
|
|
} else {
|
|
|
|
send_closure(actor_id, &StickersManager::get_default_emoji_statuses, true, std::move(promise));
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
load_special_sticker_set(special_sticker_set);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-09-07 09:25:21 +02:00
|
|
|
vector<td_api::object_ptr<td_api::emojiStatus>> statuses;
|
2022-09-02 14:02:37 +02:00
|
|
|
for (auto sticker_id : sticker_set->sticker_ids_) {
|
|
|
|
auto custom_emoji_id = get_custom_emoji_id(sticker_id);
|
2022-10-03 00:26:32 +02:00
|
|
|
if (!custom_emoji_id.is_valid()) {
|
2022-09-02 14:02:37 +02:00
|
|
|
LOG(ERROR) << "Ignore wrong sticker " << sticker_id;
|
|
|
|
continue;
|
|
|
|
}
|
2022-10-27 11:53:53 +02:00
|
|
|
statuses.push_back(td_api::make_object<td_api::emojiStatus>(custom_emoji_id.get()));
|
2022-09-02 14:02:37 +02:00
|
|
|
if (statuses.size() >= 8) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-09-07 09:25:21 +02:00
|
|
|
promise.set_value(td_api::make_object<td_api::emojiStatuses>(std::move(statuses)));
|
2022-09-02 14:02:37 +02:00
|
|
|
}
|
|
|
|
|
2022-10-03 00:26:32 +02:00
|
|
|
bool StickersManager::is_default_emoji_status(CustomEmojiId custom_emoji_id) {
|
2022-09-02 14:39:07 +02:00
|
|
|
auto &special_sticker_set = add_special_sticker_set(SpecialStickerSetType::default_statuses());
|
|
|
|
auto sticker_set = get_sticker_set(special_sticker_set.id_);
|
|
|
|
if (sticker_set == nullptr || !sticker_set->was_loaded_) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for (auto sticker_id : sticker_set->sticker_ids_) {
|
|
|
|
if (get_custom_emoji_id(sticker_id) == custom_emoji_id) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-10-27 09:06:13 +02:00
|
|
|
void StickersManager::get_default_topic_icons(bool is_recursive,
|
|
|
|
Promise<td_api::object_ptr<td_api::stickers>> &&promise) {
|
|
|
|
TRY_STATUS_PROMISE(promise, G()->close_status());
|
|
|
|
|
|
|
|
auto &special_sticker_set = add_special_sticker_set(SpecialStickerSetType::default_topic_icons());
|
|
|
|
auto sticker_set = get_sticker_set(special_sticker_set.id_);
|
|
|
|
if (sticker_set == nullptr || !sticker_set->was_loaded_) {
|
|
|
|
if (is_recursive) {
|
|
|
|
return promise.set_value(td_api::make_object<td_api::stickers>());
|
|
|
|
}
|
|
|
|
|
|
|
|
pending_get_default_topic_icons_queries_.push_back(PromiseCreator::lambda(
|
|
|
|
[actor_id = actor_id(this), promise = std::move(promise)](Result<Unit> &&result) mutable {
|
|
|
|
if (result.is_error()) {
|
|
|
|
promise.set_error(result.move_as_error());
|
|
|
|
} else {
|
|
|
|
send_closure(actor_id, &StickersManager::get_default_topic_icons, true, std::move(promise));
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
load_special_sticker_set(special_sticker_set);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-11-01 16:38:07 +01:00
|
|
|
if (!is_recursive && td_->auth_manager_->is_bot() && G()->unix_time() >= sticker_set->expires_at_) {
|
|
|
|
auto reload_promise = PromiseCreator::lambda(
|
|
|
|
[actor_id = actor_id(this), promise = std::move(promise)](Result<Unit> &&result) mutable {
|
|
|
|
if (result.is_error()) {
|
|
|
|
promise.set_error(result.move_as_error());
|
|
|
|
} else {
|
|
|
|
send_closure(actor_id, &StickersManager::get_default_topic_icons, true, std::move(promise));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
do_reload_sticker_set(sticker_set->id_, get_input_sticker_set(sticker_set), sticker_set->hash_,
|
|
|
|
std::move(reload_promise));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-10-27 09:06:13 +02:00
|
|
|
promise.set_value(get_stickers_object(sticker_set->sticker_ids_));
|
|
|
|
}
|
|
|
|
|
2022-10-03 00:26:32 +02:00
|
|
|
void StickersManager::load_custom_emoji_sticker_from_database_force(CustomEmojiId custom_emoji_id) {
|
2022-09-05 20:04:08 +02:00
|
|
|
if (!G()->parameters().use_file_db) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto value = G()->td_db()->get_sqlite_sync_pmc()->get(get_custom_emoji_database_key(custom_emoji_id));
|
|
|
|
if (value.empty()) {
|
2022-10-03 00:26:32 +02:00
|
|
|
LOG(INFO) << "Failed to load " << custom_emoji_id << " from database";
|
2022-09-05 20:04:08 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-10-03 00:26:32 +02:00
|
|
|
LOG(INFO) << "Synchronously loaded " << custom_emoji_id << " of size " << value.size() << " from database";
|
2022-09-05 20:04:08 +02:00
|
|
|
CustomEmojiLogEvent log_event;
|
|
|
|
if (log_event_parse(log_event, value).is_error()) {
|
2022-10-03 00:26:32 +02:00
|
|
|
LOG(ERROR) << "Delete invalid " << custom_emoji_id << " value from database";
|
2022-09-05 20:04:08 +02:00
|
|
|
G()->td_db()->get_sqlite_sync_pmc()->erase(get_custom_emoji_database_key(custom_emoji_id));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-03 00:26:32 +02:00
|
|
|
void StickersManager::load_custom_emoji_sticker_from_database(CustomEmojiId custom_emoji_id, Promise<Unit> &&promise) {
|
|
|
|
CHECK(custom_emoji_id.is_valid());
|
2022-07-25 22:04:53 +02:00
|
|
|
auto &queries = custom_emoji_load_queries_[custom_emoji_id];
|
|
|
|
queries.push_back(std::move(promise));
|
|
|
|
if (queries.size() == 1) {
|
2022-10-03 00:26:32 +02:00
|
|
|
LOG(INFO) << "Trying to load " << custom_emoji_id << " from database";
|
2022-07-25 22:04:53 +02:00
|
|
|
G()->td_db()->get_sqlite_pmc()->get(
|
|
|
|
get_custom_emoji_database_key(custom_emoji_id), PromiseCreator::lambda([custom_emoji_id](string value) {
|
|
|
|
send_closure(G()->stickers_manager(), &StickersManager::on_load_custom_emoji_from_database, custom_emoji_id,
|
|
|
|
std::move(value));
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-03 00:26:32 +02:00
|
|
|
void StickersManager::on_load_custom_emoji_from_database(CustomEmojiId custom_emoji_id, string value) {
|
2022-07-25 22:04:53 +02:00
|
|
|
if (G()->close_flag()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!value.empty()) {
|
2022-10-03 00:26:32 +02:00
|
|
|
LOG(INFO) << "Successfully loaded " << custom_emoji_id << " of size " << value.size() << " from database";
|
2022-07-25 22:04:53 +02:00
|
|
|
CustomEmojiLogEvent log_event;
|
|
|
|
if (log_event_parse(log_event, value).is_error()) {
|
2022-10-03 00:26:32 +02:00
|
|
|
LOG(ERROR) << "Delete invalid " << custom_emoji_id << " value from database";
|
2022-07-25 22:04:53 +02:00
|
|
|
G()->td_db()->get_sqlite_pmc()->erase(get_custom_emoji_database_key(custom_emoji_id), Auto());
|
|
|
|
}
|
|
|
|
} else {
|
2022-10-03 00:26:32 +02:00
|
|
|
LOG(INFO) << "Failed to load " << custom_emoji_id << " from database";
|
2022-07-25 22:04:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
auto it = custom_emoji_load_queries_.find(custom_emoji_id);
|
|
|
|
CHECK(it != custom_emoji_load_queries_.end());
|
|
|
|
CHECK(!it->second.empty());
|
|
|
|
auto promises = std::move(it->second);
|
|
|
|
custom_emoji_load_queries_.erase(it);
|
|
|
|
|
|
|
|
set_promises(promises);
|
|
|
|
}
|
|
|
|
|
2022-07-26 17:48:18 +02:00
|
|
|
td_api::object_ptr<td_api::stickers> StickersManager::get_custom_emoji_stickers_object(
|
2022-10-03 00:26:32 +02:00
|
|
|
const vector<CustomEmojiId> &custom_emoji_ids) {
|
2022-07-26 17:48:18 +02:00
|
|
|
vector<td_api::object_ptr<td_api::sticker>> stickers;
|
2022-07-26 18:29:37 +02:00
|
|
|
auto update_before_date = G()->unix_time() - 86400;
|
2022-10-03 00:26:32 +02:00
|
|
|
vector<CustomEmojiId> reload_custom_emoji_ids;
|
|
|
|
for (auto custom_emoji_id : custom_emoji_ids) {
|
|
|
|
auto file_id = custom_emoji_to_sticker_id_.get(custom_emoji_id);
|
2022-09-05 19:26:10 +02:00
|
|
|
if (file_id.is_valid()) {
|
2022-08-04 13:48:10 +02:00
|
|
|
auto s = get_sticker(file_id);
|
2022-09-05 19:26:10 +02:00
|
|
|
CHECK(s != nullptr);
|
|
|
|
CHECK(s->type_ == StickerType::CustomEmoji);
|
2022-08-07 18:47:50 +02:00
|
|
|
if (s->emoji_receive_date_ < update_before_date && !s->is_being_reloaded_) {
|
|
|
|
s->is_being_reloaded_ = true;
|
2022-10-03 00:26:32 +02:00
|
|
|
reload_custom_emoji_ids.push_back(custom_emoji_id);
|
2022-07-26 18:29:37 +02:00
|
|
|
}
|
2022-09-05 19:26:10 +02:00
|
|
|
|
|
|
|
auto sticker = get_sticker_object(file_id);
|
|
|
|
CHECK(sticker != nullptr);
|
2022-07-26 17:48:18 +02:00
|
|
|
stickers.push_back(std::move(sticker));
|
|
|
|
}
|
|
|
|
}
|
2022-10-03 00:26:32 +02:00
|
|
|
if (!reload_custom_emoji_ids.empty()) {
|
|
|
|
LOG(INFO) << "Reload " << reload_custom_emoji_ids;
|
2022-07-26 18:29:37 +02:00
|
|
|
auto promise = PromiseCreator::lambda(
|
|
|
|
[actor_id =
|
|
|
|
actor_id(this)](Result<vector<telegram_api::object_ptr<telegram_api::Document>>> r_documents) mutable {
|
|
|
|
send_closure(actor_id, &StickersManager::on_get_custom_emoji_documents, std::move(r_documents),
|
2022-10-03 00:26:32 +02:00
|
|
|
vector<CustomEmojiId>(), Promise<td_api::object_ptr<td_api::stickers>>());
|
2022-07-26 18:29:37 +02:00
|
|
|
});
|
2022-10-03 00:26:32 +02:00
|
|
|
td_->create_handler<GetCustomEmojiDocumentsQuery>(std::move(promise))->send(std::move(reload_custom_emoji_ids));
|
2022-07-26 18:29:37 +02:00
|
|
|
}
|
2022-07-26 17:48:18 +02:00
|
|
|
return td_api::make_object<td_api::stickers>(std::move(stickers));
|
|
|
|
}
|
|
|
|
|
2022-10-03 00:26:32 +02:00
|
|
|
void StickersManager::get_custom_emoji_stickers(vector<CustomEmojiId> &&custom_emoji_ids, bool use_database,
|
2022-07-19 15:00:04 +02:00
|
|
|
Promise<td_api::object_ptr<td_api::stickers>> &&promise) {
|
2022-07-25 22:04:53 +02:00
|
|
|
TRY_STATUS_PROMISE(promise, G()->close_status());
|
|
|
|
|
2022-10-03 00:26:32 +02:00
|
|
|
if (custom_emoji_ids.size() > MAX_GET_CUSTOM_EMOJI_STICKERS) {
|
2022-07-26 19:05:48 +02:00
|
|
|
return promise.set_error(Status::Error(400, "Too many custom emoji identifiers specified"));
|
|
|
|
}
|
|
|
|
|
2022-10-03 00:26:32 +02:00
|
|
|
FlatHashSet<CustomEmojiId, CustomEmojiIdHash> unique_custom_emoji_ids;
|
|
|
|
size_t j = 0;
|
|
|
|
for (size_t i = 0; i < custom_emoji_ids.size(); i++) {
|
|
|
|
auto custom_emoji_id = custom_emoji_ids[i];
|
|
|
|
if (custom_emoji_id.is_valid() && unique_custom_emoji_ids.insert(custom_emoji_id).second) {
|
|
|
|
custom_emoji_ids[j++] = custom_emoji_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
custom_emoji_ids.resize(j);
|
2022-07-26 17:48:18 +02:00
|
|
|
|
2022-10-03 00:26:32 +02:00
|
|
|
vector<CustomEmojiId> unknown_custom_emoji_ids;
|
|
|
|
for (auto custom_emoji_id : custom_emoji_ids) {
|
|
|
|
if (custom_emoji_to_sticker_id_.count(custom_emoji_id) == 0) {
|
|
|
|
unknown_custom_emoji_ids.push_back(custom_emoji_id);
|
2022-07-24 18:54:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-03 00:26:32 +02:00
|
|
|
if (unknown_custom_emoji_ids.empty()) {
|
|
|
|
return promise.set_value(get_custom_emoji_stickers_object(custom_emoji_ids));
|
2022-07-24 18:54:15 +02:00
|
|
|
}
|
|
|
|
|
2022-07-25 22:04:53 +02:00
|
|
|
if (use_database && G()->parameters().use_file_db) {
|
|
|
|
MultiPromiseActorSafe mpas{"LoadCustomEmojiMultiPromiseActor"};
|
2022-10-03 00:26:32 +02:00
|
|
|
mpas.add_promise(PromiseCreator::lambda(
|
|
|
|
[actor_id = actor_id(this), custom_emoji_ids, promise = std::move(promise)](Unit) mutable {
|
|
|
|
send_closure(actor_id, &StickersManager::get_custom_emoji_stickers, std::move(custom_emoji_ids), false,
|
2022-07-25 22:04:53 +02:00
|
|
|
std::move(promise));
|
|
|
|
}));
|
|
|
|
|
|
|
|
auto lock = mpas.get_promise();
|
2022-10-03 00:26:32 +02:00
|
|
|
for (auto custom_emoji_id : unknown_custom_emoji_ids) {
|
|
|
|
load_custom_emoji_sticker_from_database(custom_emoji_id, mpas.get_promise());
|
2022-07-25 22:04:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return lock.set_value(Unit());
|
|
|
|
}
|
|
|
|
|
2022-07-26 17:48:18 +02:00
|
|
|
auto query_promise = PromiseCreator::lambda(
|
2022-10-03 00:26:32 +02:00
|
|
|
[actor_id = actor_id(this), custom_emoji_ids = std::move(custom_emoji_ids), promise = std::move(promise)](
|
2022-07-26 17:48:18 +02:00
|
|
|
Result<vector<telegram_api::object_ptr<telegram_api::Document>>> r_documents) mutable {
|
2022-07-19 15:00:04 +02:00
|
|
|
send_closure(actor_id, &StickersManager::on_get_custom_emoji_documents, std::move(r_documents),
|
2022-10-03 00:26:32 +02:00
|
|
|
std::move(custom_emoji_ids), std::move(promise));
|
2022-07-19 15:00:04 +02:00
|
|
|
});
|
2022-10-03 00:26:32 +02:00
|
|
|
td_->create_handler<GetCustomEmojiDocumentsQuery>(std::move(query_promise))
|
|
|
|
->send(std::move(unknown_custom_emoji_ids));
|
2022-07-19 15:00:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void StickersManager::on_get_custom_emoji_documents(
|
2022-10-03 00:26:32 +02:00
|
|
|
Result<vector<telegram_api::object_ptr<telegram_api::Document>>> &&r_documents,
|
|
|
|
vector<CustomEmojiId> &&custom_emoji_ids, Promise<td_api::object_ptr<td_api::stickers>> &&promise) {
|
2022-07-19 15:00:04 +02:00
|
|
|
TRY_STATUS_PROMISE(promise, G()->close_status());
|
|
|
|
if (r_documents.is_error()) {
|
|
|
|
return promise.set_error(r_documents.move_as_error());
|
|
|
|
}
|
|
|
|
auto documents = r_documents.move_as_ok();
|
|
|
|
|
|
|
|
for (auto &document : documents) {
|
2022-07-28 11:56:53 +02:00
|
|
|
LOG(INFO) << "Receive " << to_string(document);
|
|
|
|
if (document->get_id() == telegram_api::documentEmpty::ID) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-07-26 17:48:18 +02:00
|
|
|
on_get_sticker_document(std::move(document), StickerFormat::Unknown);
|
2022-07-19 15:00:04 +02:00
|
|
|
}
|
|
|
|
|
2022-10-03 00:26:32 +02:00
|
|
|
promise.set_value(get_custom_emoji_stickers_object(custom_emoji_ids));
|
2022-07-19 15:00:04 +02:00
|
|
|
}
|
|
|
|
|
2022-07-15 15:15:59 +02:00
|
|
|
void StickersManager::get_premium_gift_option_sticker(int32 month_count, bool is_recursive,
|
|
|
|
Promise<td_api::object_ptr<td_api::sticker>> &&promise) {
|
|
|
|
TRY_STATUS_PROMISE(promise, G()->close_status());
|
|
|
|
|
|
|
|
auto &special_sticker_set = add_special_sticker_set(SpecialStickerSetType::premium_gifts());
|
|
|
|
auto sticker_set = get_sticker_set(special_sticker_set.id_);
|
2022-08-07 18:47:50 +02:00
|
|
|
if (sticker_set == nullptr || !sticker_set->was_loaded_) {
|
2022-07-15 15:15:59 +02:00
|
|
|
if (is_recursive) {
|
|
|
|
return promise.set_value(nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
pending_get_premium_gift_option_sticker_queries_.push_back(PromiseCreator::lambda(
|
|
|
|
[actor_id = actor_id(this), month_count, promise = std::move(promise)](Result<Unit> &&result) mutable {
|
|
|
|
if (result.is_error()) {
|
|
|
|
promise.set_error(result.move_as_error());
|
|
|
|
} else {
|
|
|
|
send_closure(actor_id, &StickersManager::get_premium_gift_option_sticker, month_count, true,
|
|
|
|
std::move(promise));
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
load_special_sticker_set(special_sticker_set);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
promise.set_value(get_sticker_object(get_premium_gift_option_sticker_id(sticker_set, month_count)));
|
|
|
|
}
|
|
|
|
|
2021-09-15 23:41:40 +02:00
|
|
|
void StickersManager::get_animated_emoji_click_sticker(const string &message_text, FullMessageId full_message_id,
|
|
|
|
Promise<td_api::object_ptr<td_api::sticker>> &&promise) {
|
2021-11-04 09:41:07 +01:00
|
|
|
if (disable_animated_emojis_ || td_->auth_manager_->is_bot()) {
|
2021-11-01 21:19:22 +01:00
|
|
|
return promise.set_value(nullptr);
|
|
|
|
}
|
|
|
|
|
2021-09-15 23:41:40 +02:00
|
|
|
auto &special_sticker_set = add_special_sticker_set(SpecialStickerSetType::animated_emoji_click());
|
|
|
|
if (!special_sticker_set.id_.is_valid()) {
|
|
|
|
// don't wait for the first load of the sticker set from the server
|
2021-09-24 12:08:32 +02:00
|
|
|
load_special_sticker_set(special_sticker_set);
|
2021-09-15 23:41:40 +02:00
|
|
|
return promise.set_value(nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto sticker_set = get_sticker_set(special_sticker_set.id_);
|
|
|
|
CHECK(sticker_set != nullptr);
|
2022-08-07 18:47:50 +02:00
|
|
|
if (sticker_set->was_loaded_) {
|
2021-09-15 23:41:40 +02:00
|
|
|
return choose_animated_emoji_click_sticker(sticker_set, message_text, full_message_id, Time::now(),
|
|
|
|
std::move(promise));
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG(INFO) << "Waiting for an emoji click sticker set needed in " << full_message_id;
|
2021-09-24 12:08:32 +02:00
|
|
|
load_special_sticker_set(special_sticker_set);
|
2021-09-15 23:41:40 +02:00
|
|
|
|
|
|
|
PendingGetAnimatedEmojiClickSticker pending_request;
|
|
|
|
pending_request.message_text_ = message_text;
|
|
|
|
pending_request.full_message_id_ = full_message_id;
|
|
|
|
pending_request.start_time_ = Time::now();
|
|
|
|
pending_request.promise_ = std::move(promise);
|
|
|
|
pending_get_animated_emoji_click_stickers_.push_back(std::move(pending_request));
|
|
|
|
}
|
|
|
|
|
|
|
|
int StickersManager::get_emoji_number(Slice emoji) {
|
|
|
|
// '0'-'9' + U+20E3
|
|
|
|
auto data = emoji.ubegin();
|
|
|
|
if (emoji.size() != 4 || emoji[0] < '0' || emoji[0] > '9' || data[1] != 0xE2 || data[2] != 0x83 || data[3] != 0xA3) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return emoji[0] - '0';
|
|
|
|
}
|
|
|
|
|
2021-10-05 15:12:04 +02:00
|
|
|
vector<FileId> StickersManager::get_animated_emoji_click_stickers(const StickerSet *sticker_set, Slice emoji) const {
|
2021-09-17 15:58:20 +02:00
|
|
|
vector<FileId> result;
|
2022-08-07 18:47:50 +02:00
|
|
|
for (auto sticker_id : sticker_set->sticker_ids_) {
|
2021-09-17 15:58:20 +02:00
|
|
|
auto s = get_sticker(sticker_id);
|
|
|
|
CHECK(s != nullptr);
|
2022-08-07 18:47:50 +02:00
|
|
|
if (remove_emoji_modifiers(s->alt_) == emoji) {
|
2021-09-17 15:58:20 +02:00
|
|
|
result.push_back(sticker_id);
|
|
|
|
}
|
|
|
|
}
|
2021-09-17 16:18:39 +02:00
|
|
|
if (result.empty()) {
|
|
|
|
const static vector<string> heart_emojis{"💛", "💙", "💚", "💜", "🧡", "🖤", "🤎", "🤍"};
|
|
|
|
if (td::contains(heart_emojis, emoji)) {
|
2021-10-05 15:12:04 +02:00
|
|
|
return get_animated_emoji_click_stickers(sticker_set, Slice("❤"));
|
2021-09-17 16:18:39 +02:00
|
|
|
}
|
|
|
|
}
|
2021-09-17 15:58:20 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-05-12 17:02:27 +02:00
|
|
|
void StickersManager::choose_animated_emoji_click_sticker(const StickerSet *sticker_set, string message_text,
|
2021-09-15 23:41:40 +02:00
|
|
|
FullMessageId full_message_id, double start_time,
|
|
|
|
Promise<td_api::object_ptr<td_api::sticker>> &&promise) {
|
2022-08-07 18:47:50 +02:00
|
|
|
CHECK(sticker_set->was_loaded_);
|
2022-05-12 17:02:27 +02:00
|
|
|
remove_emoji_modifiers_in_place(message_text);
|
2021-09-15 23:41:40 +02:00
|
|
|
if (message_text.empty()) {
|
|
|
|
return promise.set_error(Status::Error(400, "Message is not an animated emoji message"));
|
|
|
|
}
|
|
|
|
|
2021-11-04 09:41:07 +01:00
|
|
|
if (disable_animated_emojis_ || td_->auth_manager_->is_bot()) {
|
2021-11-01 21:19:22 +01:00
|
|
|
return promise.set_value(nullptr);
|
|
|
|
}
|
|
|
|
|
2021-09-19 19:52:46 +02:00
|
|
|
auto now = Time::now();
|
|
|
|
if (last_clicked_animated_emoji_ == message_text && last_clicked_animated_emoji_full_message_id_ == full_message_id &&
|
2021-09-24 15:53:42 +02:00
|
|
|
next_click_animated_emoji_message_time_ >= now + 2 * MIN_ANIMATED_EMOJI_CLICK_DELAY) {
|
2021-09-19 19:52:46 +02:00
|
|
|
return promise.set_value(nullptr);
|
|
|
|
}
|
|
|
|
|
2021-10-05 15:12:04 +02:00
|
|
|
auto all_sticker_ids = get_animated_emoji_click_stickers(sticker_set, message_text);
|
2021-09-15 23:41:40 +02:00
|
|
|
vector<std::pair<int, FileId>> found_stickers;
|
2021-09-17 15:58:20 +02:00
|
|
|
for (auto sticker_id : all_sticker_ids) {
|
|
|
|
auto it = sticker_set->sticker_emojis_map_.find(sticker_id);
|
|
|
|
if (it != sticker_set->sticker_emojis_map_.end()) {
|
|
|
|
for (auto &emoji : it->second) {
|
|
|
|
auto number = get_emoji_number(emoji);
|
|
|
|
if (number > 0) {
|
|
|
|
found_stickers.emplace_back(number, sticker_id);
|
2021-09-15 23:41:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (found_stickers.empty()) {
|
2022-08-05 20:56:05 +02:00
|
|
|
LOG(INFO) << "There is no click effect for " << message_text << " from " << full_message_id;
|
2021-09-15 23:41:40 +02:00
|
|
|
return promise.set_value(nullptr);
|
|
|
|
}
|
|
|
|
|
2021-09-16 22:22:34 +02:00
|
|
|
if (last_clicked_animated_emoji_full_message_id_ != full_message_id) {
|
|
|
|
flush_pending_animated_emoji_clicks();
|
2021-09-20 12:45:49 +02:00
|
|
|
last_clicked_animated_emoji_full_message_id_ = full_message_id;
|
|
|
|
}
|
|
|
|
if (last_clicked_animated_emoji_ != message_text) {
|
2021-09-16 22:22:34 +02:00
|
|
|
pending_animated_emoji_clicks_.clear();
|
2022-05-12 17:02:27 +02:00
|
|
|
last_clicked_animated_emoji_ = std::move(message_text);
|
2021-09-16 22:22:34 +02:00
|
|
|
}
|
2021-09-17 16:59:07 +02:00
|
|
|
|
|
|
|
if (!pending_animated_emoji_clicks_.empty() && found_stickers.size() >= 2) {
|
|
|
|
for (auto it = found_stickers.begin(); it != found_stickers.end(); ++it) {
|
|
|
|
if (it->first == pending_animated_emoji_clicks_.back().first) {
|
|
|
|
found_stickers.erase(it);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CHECK(!found_stickers.empty());
|
|
|
|
auto result = found_stickers[Random::fast(0, narrow_cast<int>(found_stickers.size()) - 1)];
|
|
|
|
|
2021-09-16 22:22:34 +02:00
|
|
|
pending_animated_emoji_clicks_.emplace_back(result.first, start_time);
|
|
|
|
if (pending_animated_emoji_clicks_.size() == 5) {
|
|
|
|
flush_pending_animated_emoji_clicks();
|
|
|
|
} else {
|
|
|
|
set_timeout_in(0.5);
|
|
|
|
}
|
2021-09-24 15:53:42 +02:00
|
|
|
if (now >= next_click_animated_emoji_message_time_) {
|
|
|
|
next_click_animated_emoji_message_time_ = now + MIN_ANIMATED_EMOJI_CLICK_DELAY;
|
2021-10-13 23:18:44 +02:00
|
|
|
promise.set_value(get_sticker_object(result.second, false, true));
|
2021-09-19 19:43:47 +02:00
|
|
|
} else {
|
2021-09-24 15:53:42 +02:00
|
|
|
create_actor<SleepActor>("SendClickAnimatedEmojiMessageResponse", next_click_animated_emoji_message_time_ - now,
|
2021-09-19 19:43:47 +02:00
|
|
|
PromiseCreator::lambda([actor_id = actor_id(this), sticker_id = result.second,
|
|
|
|
promise = std::move(promise)](Result<Unit> result) mutable {
|
|
|
|
send_closure(actor_id, &StickersManager::send_click_animated_emoji_message_response,
|
|
|
|
sticker_id, std::move(promise));
|
|
|
|
}))
|
|
|
|
.release();
|
2021-09-24 15:53:42 +02:00
|
|
|
next_click_animated_emoji_message_time_ += MIN_ANIMATED_EMOJI_CLICK_DELAY;
|
2021-09-19 19:43:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void StickersManager::send_click_animated_emoji_message_response(
|
|
|
|
FileId sticker_id, Promise<td_api::object_ptr<td_api::sticker>> &&promise) {
|
2021-10-07 15:36:21 +02:00
|
|
|
TRY_STATUS_PROMISE(promise, G()->close_status());
|
2021-10-13 23:18:44 +02:00
|
|
|
promise.set_value(get_sticker_object(sticker_id, false, true));
|
2021-09-16 22:22:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void StickersManager::timeout_expired() {
|
|
|
|
flush_pending_animated_emoji_clicks();
|
|
|
|
}
|
|
|
|
|
|
|
|
void StickersManager::flush_pending_animated_emoji_clicks() {
|
2022-10-20 12:28:07 +02:00
|
|
|
if (G()->close_flag()) {
|
2021-09-16 22:22:34 +02:00
|
|
|
return;
|
|
|
|
}
|
2022-10-20 12:28:07 +02:00
|
|
|
if (pending_animated_emoji_clicks_.empty()) {
|
2022-10-13 13:51:00 +02:00
|
|
|
return;
|
|
|
|
}
|
2021-09-16 22:22:34 +02:00
|
|
|
auto clicks = std::move(pending_animated_emoji_clicks_);
|
|
|
|
pending_animated_emoji_clicks_.clear();
|
|
|
|
auto full_message_id = last_clicked_animated_emoji_full_message_id_;
|
2021-09-19 19:52:46 +02:00
|
|
|
last_clicked_animated_emoji_full_message_id_ = FullMessageId();
|
2021-09-17 15:16:34 +02:00
|
|
|
auto emoji = std::move(last_clicked_animated_emoji_);
|
2021-09-16 22:22:34 +02:00
|
|
|
last_clicked_animated_emoji_.clear();
|
|
|
|
|
|
|
|
if (td_->messages_manager_->is_message_edited_recently(full_message_id, 1)) {
|
|
|
|
// includes deleted full_message_id
|
|
|
|
return;
|
|
|
|
}
|
2021-09-19 00:52:56 +02:00
|
|
|
auto dialog_id = full_message_id.get_dialog_id();
|
|
|
|
auto input_peer = td_->messages_manager_->get_input_peer(dialog_id, AccessRights::Write);
|
2021-09-17 15:16:34 +02:00
|
|
|
if (input_peer == nullptr) {
|
2021-09-16 22:22:34 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
double start_time = clicks[0].second;
|
|
|
|
auto data = json_encode<string>(json_object([&clicks, start_time](auto &o) {
|
|
|
|
o("v", 1);
|
|
|
|
o("a", json_array(clicks, [start_time](auto &click) {
|
|
|
|
return json_object([&click, start_time](auto &o) {
|
|
|
|
o("i", click.first);
|
|
|
|
auto t = static_cast<int32>((click.second - start_time) * 100);
|
|
|
|
o("t", JsonRaw(PSLICE() << (t / 100) << '.' << (t < 10 ? "0" : "") << (t % 100)));
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
}));
|
2021-09-17 15:16:34 +02:00
|
|
|
|
|
|
|
td_->create_handler<SendAnimatedEmojiClicksQuery>()->send(
|
2021-09-19 00:52:56 +02:00
|
|
|
dialog_id, std::move(input_peer),
|
2021-09-17 15:16:34 +02:00
|
|
|
make_tl_object<telegram_api::sendMessageEmojiInteraction>(
|
|
|
|
emoji, full_message_id.get_message_id().get_server_message_id().get(),
|
|
|
|
make_tl_object<telegram_api::dataJSON>(data)));
|
2021-09-19 00:52:56 +02:00
|
|
|
|
|
|
|
on_send_animated_emoji_clicks(dialog_id, emoji);
|
|
|
|
}
|
|
|
|
|
|
|
|
void StickersManager::on_send_animated_emoji_clicks(DialogId dialog_id, const string &emoji) {
|
|
|
|
flush_sent_animated_emoji_clicks();
|
|
|
|
|
2022-08-07 18:47:50 +02:00
|
|
|
if (!sent_animated_emoji_clicks_.empty() && sent_animated_emoji_clicks_.back().dialog_id_ == dialog_id &&
|
|
|
|
sent_animated_emoji_clicks_.back().emoji_ == emoji) {
|
|
|
|
sent_animated_emoji_clicks_.back().send_time_ = Time::now();
|
2021-09-19 00:52:56 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SentAnimatedEmojiClicks clicks;
|
2022-08-07 18:47:50 +02:00
|
|
|
clicks.send_time_ = Time::now();
|
|
|
|
clicks.dialog_id_ = dialog_id;
|
|
|
|
clicks.emoji_ = emoji;
|
2021-09-19 00:52:56 +02:00
|
|
|
sent_animated_emoji_clicks_.push_back(std::move(clicks));
|
|
|
|
}
|
|
|
|
|
|
|
|
void StickersManager::flush_sent_animated_emoji_clicks() {
|
|
|
|
if (sent_animated_emoji_clicks_.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto min_send_time = Time::now() - 30.0;
|
|
|
|
auto it = sent_animated_emoji_clicks_.begin();
|
2022-08-07 18:47:50 +02:00
|
|
|
while (it != sent_animated_emoji_clicks_.end() && it->send_time_ <= min_send_time) {
|
2021-09-19 00:52:56 +02:00
|
|
|
++it;
|
|
|
|
}
|
|
|
|
sent_animated_emoji_clicks_.erase(sent_animated_emoji_clicks_.begin(), it);
|
|
|
|
}
|
|
|
|
|
2022-05-12 17:02:27 +02:00
|
|
|
bool StickersManager::is_sent_animated_emoji_click(DialogId dialog_id, const string &emoji) {
|
2021-09-19 00:52:56 +02:00
|
|
|
flush_sent_animated_emoji_clicks();
|
|
|
|
for (const auto &click : sent_animated_emoji_clicks_) {
|
2022-08-07 18:47:50 +02:00
|
|
|
if (click.dialog_id_ == dialog_id && click.emoji_ == emoji) {
|
2021-09-19 00:52:56 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2021-09-15 23:41:40 +02:00
|
|
|
}
|
|
|
|
|
2022-05-12 17:02:27 +02:00
|
|
|
Status StickersManager::on_animated_emoji_message_clicked(string &&emoji, FullMessageId full_message_id, string data) {
|
2021-11-01 21:19:22 +01:00
|
|
|
if (td_->auth_manager_->is_bot() || disable_animated_emojis_) {
|
2021-10-05 15:12:04 +02:00
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2021-09-17 14:51:42 +02:00
|
|
|
TRY_RESULT(value, json_decode(data));
|
|
|
|
if (value.type() != JsonValue::Type::Object) {
|
|
|
|
return Status::Error("Expected an object");
|
|
|
|
}
|
|
|
|
auto &object = value.get_object();
|
|
|
|
TRY_RESULT(version, get_json_object_int_field(object, "v", false));
|
|
|
|
if (version != 1) {
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
TRY_RESULT(array_value, get_json_object_field(object, "a", JsonValue::Type::Array, false));
|
|
|
|
auto &array = array_value.get_array();
|
|
|
|
if (array.size() > 20) {
|
|
|
|
return Status::Error("Click array is too big");
|
|
|
|
}
|
|
|
|
vector<std::pair<int, double>> clicks;
|
2021-09-17 18:59:31 +02:00
|
|
|
double previous_start_time = 0.0;
|
|
|
|
double adjustment = 0.0;
|
2021-09-17 14:51:42 +02:00
|
|
|
for (auto &click : array) {
|
|
|
|
if (click.type() != JsonValue::Type::Object) {
|
|
|
|
return Status::Error("Expected clicks as JSON objects");
|
|
|
|
}
|
|
|
|
auto &click_object = click.get_object();
|
|
|
|
TRY_RESULT(index, get_json_object_int_field(click_object, "i", false));
|
|
|
|
if (index <= 0 || index > 9) {
|
|
|
|
return Status::Error("Wrong index");
|
|
|
|
}
|
|
|
|
TRY_RESULT(start_time, get_json_object_double_field(click_object, "t", false));
|
2021-09-17 18:59:31 +02:00
|
|
|
if (!std::isfinite(start_time)) {
|
|
|
|
return Status::Error("Receive invalid start time");
|
|
|
|
}
|
|
|
|
if (start_time < previous_start_time) {
|
|
|
|
return Status::Error("Non-monotonic start time");
|
2021-09-17 14:51:42 +02:00
|
|
|
}
|
2021-09-17 18:59:31 +02:00
|
|
|
if (start_time > previous_start_time + 3) {
|
|
|
|
return Status::Error("Too big delay between clicks");
|
|
|
|
}
|
|
|
|
previous_start_time = start_time;
|
|
|
|
|
|
|
|
auto adjusted_start_time =
|
|
|
|
clicks.empty() ? 0.0 : max(start_time + adjustment, clicks.back().second + MIN_ANIMATED_EMOJI_CLICK_DELAY);
|
|
|
|
adjustment = adjusted_start_time - start_time;
|
|
|
|
clicks.emplace_back(static_cast<int>(index), adjusted_start_time);
|
2021-09-17 14:51:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
auto &special_sticker_set = add_special_sticker_set(SpecialStickerSetType::animated_emoji_click());
|
|
|
|
if (special_sticker_set.id_.is_valid()) {
|
|
|
|
auto sticker_set = get_sticker_set(special_sticker_set.id_);
|
|
|
|
CHECK(sticker_set != nullptr);
|
2022-08-07 18:47:50 +02:00
|
|
|
if (sticker_set->was_loaded_) {
|
2021-09-17 18:59:31 +02:00
|
|
|
schedule_update_animated_emoji_clicked(sticker_set, emoji, full_message_id, std::move(clicks));
|
2021-09-17 14:51:42 +02:00
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG(INFO) << "Waiting for an emoji click sticker set needed in " << full_message_id;
|
2021-09-24 12:08:32 +02:00
|
|
|
load_special_sticker_set(special_sticker_set);
|
2021-09-17 14:51:42 +02:00
|
|
|
|
|
|
|
PendingOnAnimatedEmojiClicked pending_request;
|
2022-05-12 17:02:27 +02:00
|
|
|
pending_request.emoji_ = std::move(emoji);
|
2021-09-17 14:51:42 +02:00
|
|
|
pending_request.full_message_id_ = full_message_id;
|
|
|
|
pending_request.clicks_ = std::move(clicks);
|
|
|
|
pending_on_animated_emoji_message_clicked_.push_back(std::move(pending_request));
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2021-09-20 12:45:49 +02:00
|
|
|
void StickersManager::schedule_update_animated_emoji_clicked(const StickerSet *sticker_set, Slice emoji,
|
2021-09-17 18:59:31 +02:00
|
|
|
FullMessageId full_message_id,
|
|
|
|
vector<std::pair<int, double>> clicks) {
|
|
|
|
if (clicks.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
2021-09-17 14:51:42 +02:00
|
|
|
if (td_->messages_manager_->is_message_edited_recently(full_message_id, 2)) {
|
|
|
|
// includes deleted full_message_id
|
|
|
|
return;
|
|
|
|
}
|
2021-09-18 20:26:22 +02:00
|
|
|
auto dialog_id = full_message_id.get_dialog_id();
|
|
|
|
if (!td_->messages_manager_->have_input_peer(dialog_id, AccessRights::Write)) {
|
|
|
|
return;
|
|
|
|
}
|
2021-09-17 14:51:42 +02:00
|
|
|
|
2021-10-05 15:12:04 +02:00
|
|
|
auto all_sticker_ids = get_animated_emoji_click_stickers(sticker_set, emoji);
|
2022-02-07 20:41:07 +01:00
|
|
|
FlatHashMap<int, FileId> sticker_ids;
|
2021-09-17 15:58:20 +02:00
|
|
|
for (auto sticker_id : all_sticker_ids) {
|
|
|
|
auto it = sticker_set->sticker_emojis_map_.find(sticker_id);
|
|
|
|
if (it != sticker_set->sticker_emojis_map_.end()) {
|
|
|
|
for (auto &sticker_emoji : it->second) {
|
|
|
|
auto number = get_emoji_number(sticker_emoji);
|
|
|
|
if (number > 0) {
|
|
|
|
sticker_ids[number] = sticker_id;
|
2021-09-17 14:51:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-17 22:36:25 +02:00
|
|
|
auto now = Time::now();
|
2021-09-24 15:53:42 +02:00
|
|
|
auto start_time = max(now, next_update_animated_emoji_clicked_time_);
|
2021-10-19 17:11:16 +02:00
|
|
|
for (const auto &click : clicks) {
|
|
|
|
auto index = click.first;
|
2022-02-09 22:59:52 +01:00
|
|
|
if (index <= 0) {
|
|
|
|
return;
|
|
|
|
}
|
2021-09-17 18:59:31 +02:00
|
|
|
auto sticker_id = sticker_ids[index];
|
2021-09-17 14:51:42 +02:00
|
|
|
if (!sticker_id.is_valid()) {
|
2021-09-17 18:59:31 +02:00
|
|
|
LOG(INFO) << "Failed to find sticker for " << emoji << " with index " << index;
|
2021-09-17 14:51:42 +02:00
|
|
|
return;
|
|
|
|
}
|
2021-09-17 22:36:25 +02:00
|
|
|
create_actor<SleepActor>(
|
2021-10-19 17:11:16 +02:00
|
|
|
"SendUpdateAnimatedEmojiClicked", start_time + click.second - now,
|
2021-09-17 22:36:25 +02:00
|
|
|
PromiseCreator::lambda([actor_id = actor_id(this), full_message_id, sticker_id](Result<Unit> result) {
|
|
|
|
send_closure(actor_id, &StickersManager::send_update_animated_emoji_clicked, full_message_id, sticker_id);
|
|
|
|
}))
|
2021-09-17 18:59:31 +02:00
|
|
|
.release();
|
|
|
|
}
|
2021-09-24 15:53:42 +02:00
|
|
|
next_update_animated_emoji_clicked_time_ = start_time + clicks.back().second + MIN_ANIMATED_EMOJI_CLICK_DELAY;
|
2021-09-17 18:59:31 +02:00
|
|
|
}
|
|
|
|
|
2021-09-17 22:36:25 +02:00
|
|
|
void StickersManager::send_update_animated_emoji_clicked(FullMessageId full_message_id, FileId sticker_id) {
|
2021-11-04 09:41:07 +01:00
|
|
|
if (G()->close_flag() || disable_animated_emojis_ || td_->auth_manager_->is_bot()) {
|
2021-09-19 19:43:47 +02:00
|
|
|
return;
|
|
|
|
}
|
2021-09-17 18:59:31 +02:00
|
|
|
if (td_->messages_manager_->is_message_edited_recently(full_message_id, 2)) {
|
|
|
|
// includes deleted full_message_id
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto dialog_id = full_message_id.get_dialog_id();
|
|
|
|
if (!td_->messages_manager_->have_input_peer(dialog_id, AccessRights::Write)) {
|
|
|
|
return;
|
2021-09-17 14:51:42 +02:00
|
|
|
}
|
|
|
|
|
2021-10-13 23:18:44 +02:00
|
|
|
send_closure(
|
|
|
|
G()->td(), &Td::send_update,
|
|
|
|
td_api::make_object<td_api::updateAnimatedEmojiMessageClicked>(
|
|
|
|
dialog_id.get(), full_message_id.get_message_id().get(), get_sticker_object(sticker_id, false, true)));
|
2021-09-17 14:51:42 +02:00
|
|
|
}
|
|
|
|
|
2022-01-19 16:59:48 +01:00
|
|
|
bool StickersManager::is_active_reaction(const string &reaction) const {
|
|
|
|
for (auto &supported_reaction : reactions_.reactions_) {
|
|
|
|
if (supported_reaction.reaction_ == reaction) {
|
|
|
|
return supported_reaction.is_active_;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2022-01-04 14:33:57 +01:00
|
|
|
}
|
|
|
|
|
2019-09-18 05:55:43 +02:00
|
|
|
void StickersManager::view_featured_sticker_sets(const vector<StickerSetId> &sticker_set_ids) {
|
2018-12-31 20:04:05 +01:00
|
|
|
for (auto sticker_set_id : sticker_set_ids) {
|
|
|
|
auto set = get_sticker_set(sticker_set_id);
|
2022-08-07 18:47:50 +02:00
|
|
|
if (set != nullptr && !set->is_viewed_) {
|
|
|
|
auto type = static_cast<int32>(set->sticker_type_);
|
2022-07-17 21:39:22 +02:00
|
|
|
if (td::contains(featured_sticker_set_ids_[type], sticker_set_id)) {
|
|
|
|
need_update_featured_sticker_sets_[type] = true;
|
2020-04-18 00:55:54 +02:00
|
|
|
}
|
2022-08-07 18:47:50 +02:00
|
|
|
set->is_viewed_ = true;
|
2018-12-31 20:04:05 +01:00
|
|
|
pending_viewed_featured_sticker_set_ids_.insert(sticker_set_id);
|
2021-11-14 08:19:03 +01:00
|
|
|
update_sticker_set(set, "view_featured_sticker_sets");
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-17 21:39:22 +02:00
|
|
|
for (int32 type = 0; type < MAX_STICKER_TYPE; type++) {
|
|
|
|
send_update_featured_sticker_sets(static_cast<StickerType>(type));
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
if (!pending_viewed_featured_sticker_set_ids_.empty() && !pending_featured_sticker_set_views_timeout_.has_timeout()) {
|
2020-04-18 00:55:54 +02:00
|
|
|
LOG(INFO) << "Have pending viewed trending sticker sets";
|
2018-12-31 20:04:05 +01:00
|
|
|
pending_featured_sticker_set_views_timeout_.set_callback(read_featured_sticker_sets);
|
|
|
|
pending_featured_sticker_set_views_timeout_.set_callback_data(static_cast<void *>(td_));
|
|
|
|
pending_featured_sticker_set_views_timeout_.set_timeout_in(MAX_FEATURED_STICKER_SET_VIEW_DELAY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void StickersManager::read_featured_sticker_sets(void *td_void) {
|
2021-11-13 20:37:41 +01:00
|
|
|
if (G()->close_flag()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK(td_void != nullptr);
|
|
|
|
auto td = static_cast<Td *>(td_void);
|
|
|
|
|
|
|
|
auto &set_ids = td->stickers_manager_->pending_viewed_featured_sticker_set_ids_;
|
2022-03-12 11:22:07 +01:00
|
|
|
vector<StickerSetId> sticker_set_ids;
|
|
|
|
for (auto sticker_set_id : set_ids) {
|
|
|
|
sticker_set_ids.push_back(sticker_set_id);
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
set_ids.clear();
|
2022-03-12 11:22:07 +01:00
|
|
|
td->create_handler<ReadFeaturedStickerSetsQuery>()->send(std::move(sticker_set_ids));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2022-07-14 15:17:19 +02:00
|
|
|
std::pair<int32, vector<StickerSetId>> StickersManager::get_archived_sticker_sets(StickerType sticker_type,
|
2019-09-18 05:55:43 +02:00
|
|
|
StickerSetId offset_sticker_set_id,
|
|
|
|
int32 limit, bool force,
|
|
|
|
Promise<Unit> &&promise) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (limit <= 0) {
|
2021-09-24 09:59:51 +02:00
|
|
|
promise.set_error(Status::Error(400, "Parameter limit must be positive"));
|
2018-12-31 20:04:05 +01:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-07-24 13:01:58 +02:00
|
|
|
auto type = static_cast<int32>(sticker_type);
|
|
|
|
vector<StickerSetId> &sticker_set_ids = archived_sticker_set_ids_[type];
|
|
|
|
int32 total_count = total_archived_sticker_set_count_[type];
|
2019-06-05 01:32:10 +02:00
|
|
|
if (total_count >= 0) {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto offset_it = sticker_set_ids.begin();
|
2019-09-18 05:55:43 +02:00
|
|
|
if (offset_sticker_set_id.is_valid()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
offset_it = std::find(sticker_set_ids.begin(), sticker_set_ids.end(), offset_sticker_set_id);
|
|
|
|
if (offset_it == sticker_set_ids.end()) {
|
|
|
|
offset_it = sticker_set_ids.begin();
|
|
|
|
} else {
|
|
|
|
++offset_it;
|
|
|
|
}
|
|
|
|
}
|
2019-09-18 05:55:43 +02:00
|
|
|
vector<StickerSetId> result;
|
2018-12-31 20:04:05 +01:00
|
|
|
while (result.size() < static_cast<size_t>(limit)) {
|
|
|
|
if (offset_it == sticker_set_ids.end()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
auto sticker_set_id = *offset_it++;
|
2019-09-18 05:55:43 +02:00
|
|
|
if (!sticker_set_id.is_valid()) { // end of the list
|
2018-12-31 20:04:05 +01:00
|
|
|
promise.set_value(Unit());
|
|
|
|
return {total_count, std::move(result)};
|
|
|
|
}
|
|
|
|
result.push_back(sticker_set_id);
|
|
|
|
}
|
|
|
|
if (result.size() == static_cast<size_t>(limit) || force) {
|
|
|
|
promise.set_value(Unit());
|
|
|
|
return {total_count, std::move(result)};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-14 15:17:19 +02:00
|
|
|
td_->create_handler<GetArchivedStickerSetsQuery>(std::move(promise))
|
|
|
|
->send(sticker_type, offset_sticker_set_id, limit);
|
2018-12-31 20:04:05 +01:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
void StickersManager::on_get_archived_sticker_sets(
|
2022-07-14 15:17:19 +02:00
|
|
|
StickerType sticker_type, StickerSetId offset_sticker_set_id,
|
2019-09-18 05:55:43 +02:00
|
|
|
vector<tl_object_ptr<telegram_api::StickerSetCovered>> &&sticker_sets, int32 total_count) {
|
2022-07-24 13:01:58 +02:00
|
|
|
auto type = static_cast<int32>(sticker_type);
|
|
|
|
vector<StickerSetId> &sticker_set_ids = archived_sticker_set_ids_[type];
|
2019-09-18 05:55:43 +02:00
|
|
|
if (!sticker_set_ids.empty() && sticker_set_ids.back() == StickerSetId()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return;
|
|
|
|
}
|
2019-06-05 01:32:10 +02:00
|
|
|
if (total_count < 0) {
|
|
|
|
LOG(ERROR) << "Receive " << total_count << " as total count of archived sticker sets";
|
|
|
|
}
|
|
|
|
|
2021-09-06 12:58:40 +02:00
|
|
|
// if 0 sticker sets are received, then set offset_sticker_set_id was found and there are no stickers after it
|
|
|
|
// or it wasn't found and there are no archived sets at all
|
2019-06-05 01:32:10 +02:00
|
|
|
bool is_last =
|
2019-09-18 05:55:43 +02:00
|
|
|
sticker_sets.empty() && (!offset_sticker_set_id.is_valid() ||
|
|
|
|
(!sticker_set_ids.empty() && offset_sticker_set_id == sticker_set_ids.back()));
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2022-07-24 13:01:58 +02:00
|
|
|
total_archived_sticker_set_count_[type] = total_count;
|
2018-12-31 20:04:05 +01:00
|
|
|
for (auto &sticker_set_covered : sticker_sets) {
|
2019-12-29 02:59:34 +01:00
|
|
|
auto sticker_set_id =
|
|
|
|
on_get_sticker_set_covered(std::move(sticker_set_covered), false, "on_get_archived_sticker_sets");
|
2019-09-18 05:55:43 +02:00
|
|
|
if (sticker_set_id.is_valid()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto sticker_set = get_sticker_set(sticker_set_id);
|
|
|
|
CHECK(sticker_set != nullptr);
|
2021-11-14 08:19:03 +01:00
|
|
|
update_sticker_set(sticker_set, "on_get_archived_sticker_sets");
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2019-10-22 01:12:58 +02:00
|
|
|
if (!td::contains(sticker_set_ids, sticker_set_id)) {
|
2018-12-31 20:04:05 +01:00
|
|
|
sticker_set_ids.push_back(sticker_set_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-05 01:32:10 +02:00
|
|
|
if (sticker_set_ids.size() >= static_cast<size_t>(total_count) || is_last) {
|
|
|
|
if (sticker_set_ids.size() != static_cast<size_t>(total_count)) {
|
|
|
|
LOG(ERROR) << "Expected total of " << total_count << " archived sticker sets, but " << sticker_set_ids.size()
|
2018-12-31 20:04:05 +01:00
|
|
|
<< " found";
|
2022-07-24 13:01:58 +02:00
|
|
|
total_archived_sticker_set_count_[type] = static_cast<int32>(sticker_set_ids.size());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2019-09-18 05:55:43 +02:00
|
|
|
sticker_set_ids.push_back(StickerSetId());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
send_update_installed_sticker_sets();
|
|
|
|
}
|
|
|
|
|
2022-07-17 21:39:22 +02:00
|
|
|
td_api::object_ptr<td_api::trendingStickerSets> StickersManager::get_featured_sticker_sets(StickerType sticker_type,
|
|
|
|
int32 offset, int32 limit,
|
2022-06-17 18:16:17 +02:00
|
|
|
Promise<Unit> &&promise) {
|
2020-04-18 00:55:54 +02:00
|
|
|
if (offset < 0) {
|
2021-09-24 09:59:51 +02:00
|
|
|
promise.set_error(Status::Error(400, "Parameter offset must be non-negative"));
|
2020-04-18 00:55:54 +02:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (limit < 0) {
|
2021-09-24 09:59:51 +02:00
|
|
|
promise.set_error(Status::Error(400, "Parameter limit must be non-negative"));
|
2020-04-18 00:55:54 +02:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
if (limit == 0) {
|
|
|
|
offset = 0;
|
|
|
|
}
|
|
|
|
|
2022-07-17 21:39:22 +02:00
|
|
|
if (sticker_type == StickerType::Mask) {
|
|
|
|
promise.set_value(Unit());
|
|
|
|
return get_trending_sticker_sets_object(sticker_type, {});
|
|
|
|
}
|
|
|
|
auto type = static_cast<int32>(sticker_type);
|
|
|
|
|
|
|
|
if (!are_featured_sticker_sets_loaded_[type]) {
|
|
|
|
load_featured_sticker_sets(sticker_type, std::move(promise));
|
2018-12-31 20:04:05 +01:00
|
|
|
return {};
|
|
|
|
}
|
2022-07-17 21:39:22 +02:00
|
|
|
reload_featured_sticker_sets(sticker_type, false);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2022-07-17 21:39:22 +02:00
|
|
|
auto set_count = static_cast<int32>(featured_sticker_set_ids_[type].size());
|
2020-04-18 00:55:54 +02:00
|
|
|
if (offset < set_count) {
|
|
|
|
if (limit > set_count - offset) {
|
|
|
|
limit = set_count - offset;
|
|
|
|
}
|
|
|
|
promise.set_value(Unit());
|
2022-07-17 21:39:22 +02:00
|
|
|
auto begin = featured_sticker_set_ids_[type].begin() + offset;
|
|
|
|
return get_trending_sticker_sets_object(sticker_type, {begin, begin + limit});
|
2020-04-18 00:55:54 +02:00
|
|
|
}
|
|
|
|
|
2022-07-17 21:39:22 +02:00
|
|
|
if (offset == set_count && are_old_featured_sticker_sets_invalidated_[type]) {
|
|
|
|
invalidate_old_featured_sticker_sets(sticker_type);
|
2020-04-18 00:55:54 +02:00
|
|
|
}
|
|
|
|
|
2022-07-17 21:39:22 +02:00
|
|
|
auto total_count =
|
|
|
|
set_count + (old_featured_sticker_set_count_[type] == -1 ? 1 : old_featured_sticker_set_count_[type]);
|
|
|
|
if (offset < total_count || old_featured_sticker_set_count_[type] == -1) {
|
2020-04-18 00:55:54 +02:00
|
|
|
offset -= set_count;
|
2022-07-17 21:39:22 +02:00
|
|
|
set_count = static_cast<int32>(old_featured_sticker_set_ids_[type].size());
|
2020-04-18 00:55:54 +02:00
|
|
|
if (offset < set_count) {
|
|
|
|
if (limit > set_count - offset) {
|
|
|
|
limit = set_count - offset;
|
|
|
|
}
|
|
|
|
promise.set_value(Unit());
|
2022-07-17 21:39:22 +02:00
|
|
|
auto begin = old_featured_sticker_set_ids_[type].begin() + offset;
|
|
|
|
return get_trending_sticker_sets_object(sticker_type, {begin, begin + limit});
|
2020-04-18 00:55:54 +02:00
|
|
|
}
|
|
|
|
if (offset > set_count) {
|
|
|
|
promise.set_error(
|
|
|
|
Status::Error(400, "Too big offset specified; trending sticker sets can be received only consequently"));
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-07-17 21:39:22 +02:00
|
|
|
load_old_featured_sticker_sets(sticker_type, std::move(promise));
|
2020-04-18 00:55:54 +02:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
promise.set_value(Unit());
|
2022-07-17 21:39:22 +02:00
|
|
|
return get_trending_sticker_sets_object(sticker_type, {});
|
2020-04-18 00:55:54 +02:00
|
|
|
}
|
|
|
|
|
2022-07-17 21:39:22 +02:00
|
|
|
void StickersManager::on_old_featured_sticker_sets_invalidated(StickerType sticker_type) {
|
|
|
|
if (sticker_type != StickerType::Regular) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto type = static_cast<int32>(sticker_type);
|
2020-04-18 00:55:54 +02:00
|
|
|
LOG(INFO) << "Invalidate old trending sticker sets";
|
2022-07-17 21:39:22 +02:00
|
|
|
are_old_featured_sticker_sets_invalidated_[type] = true;
|
2020-04-18 00:55:54 +02:00
|
|
|
|
|
|
|
if (!G()->parameters().use_file_db) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
G()->td_db()->get_binlog_pmc()->set("invalidate_old_featured_sticker_sets", "1");
|
|
|
|
}
|
|
|
|
|
2022-07-17 21:39:22 +02:00
|
|
|
void StickersManager::invalidate_old_featured_sticker_sets(StickerType sticker_type) {
|
2020-05-16 01:45:26 +02:00
|
|
|
if (G()->close_flag()) {
|
|
|
|
return;
|
|
|
|
}
|
2022-07-17 21:39:22 +02:00
|
|
|
if (sticker_type != StickerType::Regular) {
|
|
|
|
return;
|
|
|
|
}
|
2020-05-16 01:45:26 +02:00
|
|
|
|
2022-07-17 21:39:22 +02:00
|
|
|
auto type = static_cast<int32>(sticker_type);
|
2020-04-18 00:55:54 +02:00
|
|
|
LOG(INFO) << "Invalidate old featured sticker sets";
|
|
|
|
if (G()->parameters().use_file_db) {
|
|
|
|
G()->td_db()->get_binlog_pmc()->erase("invalidate_old_featured_sticker_sets");
|
|
|
|
G()->td_db()->get_sqlite_pmc()->erase_by_prefix("sssoldfeatured", Auto());
|
|
|
|
}
|
2022-07-17 21:39:22 +02:00
|
|
|
are_old_featured_sticker_sets_invalidated_[type] = false;
|
|
|
|
old_featured_sticker_set_ids_[type].clear();
|
2020-04-18 00:55:54 +02:00
|
|
|
|
2022-07-17 21:39:22 +02:00
|
|
|
old_featured_sticker_set_generation_[type]++;
|
2022-04-13 16:40:12 +02:00
|
|
|
fail_promises(load_old_featured_sticker_sets_queries_, Status::Error(400, "Trending sticker sets were updated"));
|
2020-04-18 00:55:54 +02:00
|
|
|
}
|
|
|
|
|
2022-07-17 21:39:22 +02:00
|
|
|
void StickersManager::set_old_featured_sticker_set_count(StickerType sticker_type, int32 count) {
|
|
|
|
auto type = static_cast<int32>(sticker_type);
|
|
|
|
if (old_featured_sticker_set_count_[type] == count) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (sticker_type != StickerType::Regular) {
|
2020-04-18 00:55:54 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-07-17 21:39:22 +02:00
|
|
|
on_old_featured_sticker_sets_invalidated(sticker_type);
|
2020-04-18 00:55:54 +02:00
|
|
|
|
2022-07-17 21:39:22 +02:00
|
|
|
old_featured_sticker_set_count_[type] = count;
|
|
|
|
need_update_featured_sticker_sets_[type] = true;
|
2020-04-18 00:55:54 +02:00
|
|
|
|
|
|
|
if (!G()->parameters().use_file_db) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG(INFO) << "Save old trending sticker set count " << count << " to binlog";
|
|
|
|
G()->td_db()->get_binlog_pmc()->set("old_featured_sticker_set_count", to_string(count));
|
|
|
|
}
|
|
|
|
|
2022-07-17 21:39:22 +02:00
|
|
|
void StickersManager::fix_old_featured_sticker_set_count(StickerType sticker_type) {
|
|
|
|
auto type = static_cast<int32>(sticker_type);
|
|
|
|
auto known_count = static_cast<int32>(old_featured_sticker_set_ids_[type].size());
|
|
|
|
if (old_featured_sticker_set_count_[type] < known_count) {
|
|
|
|
if (old_featured_sticker_set_count_[type] >= 0) {
|
|
|
|
LOG(ERROR) << "Have old trending sticker set count " << old_featured_sticker_set_count_[type] << ", but have "
|
2020-04-18 00:55:54 +02:00
|
|
|
<< known_count << " old trending sticker sets";
|
|
|
|
}
|
2022-07-17 21:39:22 +02:00
|
|
|
set_old_featured_sticker_set_count(sticker_type, known_count);
|
2020-04-18 00:55:54 +02:00
|
|
|
}
|
2022-07-17 21:39:22 +02:00
|
|
|
if (old_featured_sticker_set_count_[type] > known_count && known_count % OLD_FEATURED_STICKER_SET_SLICE_SIZE != 0) {
|
|
|
|
LOG(ERROR) << "Have " << known_count << " old sticker sets out of " << old_featured_sticker_set_count_[type];
|
|
|
|
set_old_featured_sticker_set_count(sticker_type, known_count);
|
2020-04-18 00:55:54 +02:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void StickersManager::on_get_featured_sticker_sets(
|
2022-07-17 21:39:22 +02:00
|
|
|
StickerType sticker_type, int32 offset, int32 limit, uint32 generation,
|
2018-12-31 20:04:05 +01:00
|
|
|
tl_object_ptr<telegram_api::messages_FeaturedStickers> &&sticker_sets_ptr) {
|
2022-07-17 21:39:22 +02:00
|
|
|
auto type = static_cast<int32>(sticker_type);
|
2020-04-18 00:55:54 +02:00
|
|
|
if (offset < 0) {
|
2022-07-17 21:39:22 +02:00
|
|
|
next_featured_sticker_sets_load_time_[type] = Time::now_cached() + Random::fast(30 * 60, 50 * 60);
|
2020-04-18 00:55:54 +02:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
int32 constructor_id = sticker_sets_ptr->get_id();
|
|
|
|
if (constructor_id == telegram_api::messages_featuredStickersNotModified::ID) {
|
2020-04-18 00:55:54 +02:00
|
|
|
LOG(INFO) << "Trending sticker sets are not modified";
|
|
|
|
auto *stickers = static_cast<const telegram_api::messages_featuredStickersNotModified *>(sticker_sets_ptr.get());
|
2022-07-17 21:39:22 +02:00
|
|
|
if (offset >= 0 && generation == old_featured_sticker_set_generation_[type]) {
|
|
|
|
set_old_featured_sticker_set_count(sticker_type, stickers->count_);
|
|
|
|
fix_old_featured_sticker_set_count(sticker_type);
|
2020-04-18 00:55:54 +02:00
|
|
|
}
|
2022-07-17 21:39:22 +02:00
|
|
|
send_update_featured_sticker_sets(sticker_type);
|
2018-12-31 20:04:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
CHECK(constructor_id == telegram_api::messages_featuredStickers::ID);
|
|
|
|
auto featured_stickers = move_tl_object_as<telegram_api::messages_featuredStickers>(sticker_sets_ptr);
|
|
|
|
|
2022-07-17 21:39:22 +02:00
|
|
|
if (featured_stickers->premium_ != are_featured_sticker_sets_premium_[type]) {
|
|
|
|
on_old_featured_sticker_sets_invalidated(sticker_type);
|
2022-06-17 18:16:17 +02:00
|
|
|
if (offset >= 0) {
|
2022-07-17 21:39:22 +02:00
|
|
|
featured_stickers->premium_ = are_featured_sticker_sets_premium_[type];
|
|
|
|
reload_featured_sticker_sets(sticker_type, true);
|
2022-06-17 18:16:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-17 21:39:22 +02:00
|
|
|
if (offset >= 0 && generation == old_featured_sticker_set_generation_[type]) {
|
|
|
|
set_old_featured_sticker_set_count(sticker_type, featured_stickers->count_);
|
2020-04-18 00:55:54 +02:00
|
|
|
// the count will be fixed in on_load_old_featured_sticker_sets_finished
|
|
|
|
}
|
|
|
|
|
2022-03-11 19:38:48 +01:00
|
|
|
FlatHashSet<StickerSetId, StickerSetIdHash> unread_sticker_set_ids;
|
2019-11-06 13:12:16 +01:00
|
|
|
for (auto &unread_sticker_set_id : featured_stickers->unread_) {
|
2022-03-11 19:38:48 +01:00
|
|
|
StickerSetId sticker_set_id(unread_sticker_set_id);
|
|
|
|
if (sticker_set_id.is_valid()) {
|
|
|
|
unread_sticker_set_ids.insert(sticker_set_id);
|
|
|
|
}
|
2019-11-06 13:12:16 +01:00
|
|
|
}
|
|
|
|
|
2019-09-18 05:55:43 +02:00
|
|
|
vector<StickerSetId> featured_sticker_set_ids;
|
2018-12-31 20:04:05 +01:00
|
|
|
for (auto &sticker_set : featured_stickers->sets_) {
|
2019-12-29 02:59:34 +01:00
|
|
|
StickerSetId set_id = on_get_sticker_set_covered(std::move(sticker_set), true, "on_get_featured_sticker_sets");
|
2019-09-18 05:55:43 +02:00
|
|
|
if (!set_id.is_valid()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto set = get_sticker_set(set_id);
|
|
|
|
CHECK(set != nullptr);
|
|
|
|
bool is_viewed = unread_sticker_set_ids.count(set_id) == 0;
|
2022-08-07 18:47:50 +02:00
|
|
|
if (is_viewed != set->is_viewed_) {
|
|
|
|
set->is_viewed_ = is_viewed;
|
|
|
|
set->is_changed_ = true;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2022-06-17 18:16:17 +02:00
|
|
|
update_sticker_set(set, "on_get_featured_sticker_sets 2");
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
featured_sticker_set_ids.push_back(set_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
send_update_installed_sticker_sets();
|
|
|
|
|
2020-04-18 00:55:54 +02:00
|
|
|
if (offset >= 0) {
|
2022-07-17 21:39:22 +02:00
|
|
|
if (generation == old_featured_sticker_set_generation_[type] && sticker_type == StickerType::Regular) {
|
2020-05-16 01:45:26 +02:00
|
|
|
if (G()->parameters().use_file_db && !G()->close_flag()) {
|
2022-07-17 21:39:22 +02:00
|
|
|
LOG(INFO) << "Save old trending sticker sets to database with offset "
|
|
|
|
<< old_featured_sticker_set_ids_[type].size();
|
|
|
|
CHECK(old_featured_sticker_set_ids_[type].size() % OLD_FEATURED_STICKER_SET_SLICE_SIZE == 0);
|
2022-06-17 18:16:17 +02:00
|
|
|
StickerSetListLogEvent log_event(featured_sticker_set_ids, false);
|
2022-07-17 21:39:22 +02:00
|
|
|
G()->td_db()->get_sqlite_pmc()->set(PSTRING() << "sssoldfeatured" << old_featured_sticker_set_ids_[type].size(),
|
2020-04-18 00:55:54 +02:00
|
|
|
log_event_store(log_event).as_slice().str(), Auto());
|
|
|
|
}
|
2022-07-17 21:39:22 +02:00
|
|
|
on_load_old_featured_sticker_sets_finished(sticker_type, generation, std::move(featured_sticker_set_ids));
|
2020-04-18 00:55:54 +02:00
|
|
|
}
|
|
|
|
|
2022-07-17 21:39:22 +02:00
|
|
|
send_update_featured_sticker_sets(sticker_type); // because of changed count
|
2020-04-18 00:55:54 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-07-17 21:39:22 +02:00
|
|
|
on_load_featured_sticker_sets_finished(sticker_type, std::move(featured_sticker_set_ids),
|
|
|
|
featured_stickers->premium_);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2022-07-17 21:39:22 +02:00
|
|
|
LOG_IF(ERROR, featured_sticker_sets_hash_[type] != featured_stickers->hash_) << "Trending sticker sets hash mismatch";
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2022-08-10 12:57:41 +02:00
|
|
|
if (G()->parameters().use_file_db && !G()->close_flag()) {
|
|
|
|
LOG(INFO) << "Save trending sticker sets to database";
|
|
|
|
StickerSetListLogEvent log_event(featured_sticker_set_ids_[type], are_featured_sticker_sets_premium_[type]);
|
|
|
|
G()->td_db()->get_sqlite_pmc()->set(PSTRING() << "sssfeatured" << get_featured_sticker_suffix(sticker_type),
|
|
|
|
log_event_store(log_event).as_slice().str(), Auto());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-17 21:39:22 +02:00
|
|
|
void StickersManager::on_get_featured_sticker_sets_failed(StickerType sticker_type, int32 offset, int32 limit,
|
|
|
|
uint32 generation, Status error) {
|
|
|
|
auto type = static_cast<int32>(sticker_type);
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK(error.is_error());
|
2020-04-18 00:55:54 +02:00
|
|
|
if (offset >= 0) {
|
2022-07-17 21:39:22 +02:00
|
|
|
if (generation != old_featured_sticker_set_generation_[type] || sticker_type != StickerType::Regular) {
|
2020-04-18 00:55:54 +02:00
|
|
|
return;
|
|
|
|
}
|
2022-04-13 16:40:12 +02:00
|
|
|
fail_promises(load_old_featured_sticker_sets_queries_, std::move(error));
|
2020-04-18 00:55:54 +02:00
|
|
|
} else {
|
2022-07-17 21:39:22 +02:00
|
|
|
next_featured_sticker_sets_load_time_[type] = Time::now_cached() + Random::fast(5, 10);
|
|
|
|
fail_promises(load_featured_sticker_sets_queries_[type], std::move(error));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-17 21:39:22 +02:00
|
|
|
void StickersManager::load_featured_sticker_sets(StickerType sticker_type, Promise<Unit> &&promise) {
|
2022-09-18 20:57:13 +02:00
|
|
|
CHECK(sticker_type != StickerType::Mask);
|
2022-07-17 21:39:22 +02:00
|
|
|
auto type = static_cast<int32>(sticker_type);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (td_->auth_manager_->is_bot()) {
|
2022-07-17 21:39:22 +02:00
|
|
|
are_featured_sticker_sets_loaded_[type] = true;
|
|
|
|
old_featured_sticker_set_count_[type] = 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2022-07-17 21:39:22 +02:00
|
|
|
if (are_featured_sticker_sets_loaded_[type]) {
|
2018-12-31 20:04:05 +01:00
|
|
|
promise.set_value(Unit());
|
|
|
|
return;
|
|
|
|
}
|
2022-07-17 21:39:22 +02:00
|
|
|
load_featured_sticker_sets_queries_[type].push_back(std::move(promise));
|
|
|
|
if (load_featured_sticker_sets_queries_[type].size() == 1u) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (G()->parameters().use_file_db) {
|
2020-04-18 00:55:54 +02:00
|
|
|
LOG(INFO) << "Trying to load trending sticker sets from database";
|
2022-07-17 21:39:22 +02:00
|
|
|
G()->td_db()->get_sqlite_pmc()->get(PSTRING() << "sssfeatured" << get_featured_sticker_suffix(sticker_type),
|
|
|
|
PromiseCreator::lambda([sticker_type](string value) {
|
2018-12-31 20:04:05 +01:00
|
|
|
send_closure(G()->stickers_manager(),
|
|
|
|
&StickersManager::on_load_featured_sticker_sets_from_database,
|
2022-07-17 21:39:22 +02:00
|
|
|
sticker_type, std::move(value));
|
2018-12-31 20:04:05 +01:00
|
|
|
}));
|
|
|
|
} else {
|
2020-04-18 00:55:54 +02:00
|
|
|
LOG(INFO) << "Trying to load trending sticker sets from server";
|
2022-07-17 21:39:22 +02:00
|
|
|
reload_featured_sticker_sets(sticker_type, true);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-17 21:39:22 +02:00
|
|
|
void StickersManager::on_load_featured_sticker_sets_from_database(StickerType sticker_type, string value) {
|
2020-05-16 01:25:03 +02:00
|
|
|
if (G()->close_flag()) {
|
|
|
|
return;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
if (value.empty()) {
|
2022-07-17 21:39:22 +02:00
|
|
|
LOG(INFO) << "Trending " << sticker_type << " sticker sets aren't found in database";
|
|
|
|
reload_featured_sticker_sets(sticker_type, true);
|
2018-12-31 20:04:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-07-17 21:39:22 +02:00
|
|
|
LOG(INFO) << "Successfully loaded trending " << sticker_type << " sticker set list of size " << value.size()
|
|
|
|
<< " from database";
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
StickerSetListLogEvent log_event;
|
2019-05-29 21:24:27 +02:00
|
|
|
auto status = log_event_parse(log_event, value);
|
|
|
|
if (status.is_error()) {
|
|
|
|
// can't happen unless database is broken
|
2020-04-18 00:55:54 +02:00
|
|
|
LOG(ERROR) << "Can't load trending sticker set list: " << status << ' ' << format::as_hex_dump<4>(Slice(value));
|
2022-07-17 21:39:22 +02:00
|
|
|
return reload_featured_sticker_sets(sticker_type, true);
|
2019-05-29 21:24:27 +02:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2019-09-18 05:55:43 +02:00
|
|
|
vector<StickerSetId> sets_to_load;
|
2022-06-17 18:16:17 +02:00
|
|
|
for (auto sticker_set_id : log_event.sticker_set_ids_) {
|
2018-12-31 20:04:05 +01:00
|
|
|
StickerSet *sticker_set = get_sticker_set(sticker_set_id);
|
|
|
|
CHECK(sticker_set != nullptr);
|
2022-08-07 18:47:50 +02:00
|
|
|
if (!sticker_set->is_inited_) {
|
2018-12-31 20:04:05 +01:00
|
|
|
sets_to_load.push_back(sticker_set_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
load_sticker_sets_without_stickers(
|
2022-07-17 21:39:22 +02:00
|
|
|
std::move(sets_to_load),
|
|
|
|
PromiseCreator::lambda([sticker_type, sticker_set_ids = std::move(log_event.sticker_set_ids_),
|
|
|
|
is_premium = log_event.is_premium_](Result<> result) mutable {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (result.is_ok()) {
|
2022-07-17 21:39:22 +02:00
|
|
|
send_closure(G()->stickers_manager(), &StickersManager::on_load_featured_sticker_sets_finished, sticker_type,
|
2022-06-17 18:16:17 +02:00
|
|
|
std::move(sticker_set_ids), is_premium);
|
2020-04-18 00:55:54 +02:00
|
|
|
} else {
|
2022-07-17 21:39:22 +02:00
|
|
|
send_closure(G()->stickers_manager(), &StickersManager::reload_featured_sticker_sets, sticker_type, true);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2022-07-17 21:39:22 +02:00
|
|
|
void StickersManager::on_load_featured_sticker_sets_finished(StickerType sticker_type,
|
|
|
|
vector<StickerSetId> &&featured_sticker_set_ids,
|
2022-06-17 18:16:17 +02:00
|
|
|
bool is_premium) {
|
2022-07-17 21:39:22 +02:00
|
|
|
auto type = static_cast<int32>(sticker_type);
|
|
|
|
if (!featured_sticker_set_ids_[type].empty() && featured_sticker_set_ids != featured_sticker_set_ids_[type]) {
|
2020-04-18 00:55:54 +02:00
|
|
|
// always invalidate old featured sticker sets when current featured sticker sets change
|
2022-07-17 21:39:22 +02:00
|
|
|
on_old_featured_sticker_sets_invalidated(sticker_type);
|
2020-04-18 00:55:54 +02:00
|
|
|
}
|
2022-07-17 21:39:22 +02:00
|
|
|
featured_sticker_set_ids_[type] = std::move(featured_sticker_set_ids);
|
|
|
|
are_featured_sticker_sets_premium_[type] = is_premium;
|
|
|
|
are_featured_sticker_sets_loaded_[type] = true;
|
|
|
|
need_update_featured_sticker_sets_[type] = true;
|
|
|
|
send_update_featured_sticker_sets(sticker_type);
|
|
|
|
set_promises(load_featured_sticker_sets_queries_[type]);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2022-07-17 21:39:22 +02:00
|
|
|
void StickersManager::load_old_featured_sticker_sets(StickerType sticker_type, Promise<Unit> &&promise) {
|
|
|
|
CHECK(sticker_type == StickerType::Regular);
|
2020-04-18 00:55:54 +02:00
|
|
|
CHECK(!td_->auth_manager_->is_bot());
|
2022-07-17 21:39:22 +02:00
|
|
|
auto type = static_cast<int32>(sticker_type);
|
|
|
|
CHECK(old_featured_sticker_set_ids_[type].size() % OLD_FEATURED_STICKER_SET_SLICE_SIZE == 0);
|
2020-04-18 00:55:54 +02:00
|
|
|
load_old_featured_sticker_sets_queries_.push_back(std::move(promise));
|
|
|
|
if (load_old_featured_sticker_sets_queries_.size() == 1u) {
|
|
|
|
if (G()->parameters().use_file_db) {
|
|
|
|
LOG(INFO) << "Trying to load old trending sticker sets from database with offset "
|
2022-07-17 21:39:22 +02:00
|
|
|
<< old_featured_sticker_set_ids_[type].size();
|
2020-04-18 00:55:54 +02:00
|
|
|
G()->td_db()->get_sqlite_pmc()->get(
|
2022-07-17 21:39:22 +02:00
|
|
|
PSTRING() << "sssoldfeatured" << old_featured_sticker_set_ids_[type].size(),
|
|
|
|
PromiseCreator::lambda([sticker_type, generation = old_featured_sticker_set_generation_[type]](string value) {
|
2020-04-18 00:55:54 +02:00
|
|
|
send_closure(G()->stickers_manager(), &StickersManager::on_load_old_featured_sticker_sets_from_database,
|
2022-07-17 21:39:22 +02:00
|
|
|
sticker_type, generation, std::move(value));
|
2020-04-18 00:55:54 +02:00
|
|
|
}));
|
|
|
|
} else {
|
|
|
|
LOG(INFO) << "Trying to load old trending sticker sets from server with offset "
|
2022-07-17 21:39:22 +02:00
|
|
|
<< old_featured_sticker_set_ids_[type].size();
|
|
|
|
reload_old_featured_sticker_sets(sticker_type);
|
2020-04-18 00:55:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-17 21:39:22 +02:00
|
|
|
void StickersManager::on_load_old_featured_sticker_sets_from_database(StickerType sticker_type, uint32 generation,
|
|
|
|
string value) {
|
2020-05-16 01:25:03 +02:00
|
|
|
if (G()->close_flag()) {
|
|
|
|
return;
|
|
|
|
}
|
2022-07-17 21:39:22 +02:00
|
|
|
CHECK(sticker_type == StickerType::Regular);
|
|
|
|
auto type = static_cast<int32>(sticker_type);
|
|
|
|
if (generation != old_featured_sticker_set_generation_[type]) {
|
2020-04-18 00:55:54 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (value.empty()) {
|
|
|
|
LOG(INFO) << "Old trending sticker sets aren't found in database";
|
2022-07-17 21:39:22 +02:00
|
|
|
return reload_old_featured_sticker_sets(sticker_type);
|
2020-04-18 00:55:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
LOG(INFO) << "Successfully loaded old trending sticker set list of size " << value.size()
|
2022-07-17 21:39:22 +02:00
|
|
|
<< " from database with offset " << old_featured_sticker_set_ids_[type].size();
|
2020-04-18 00:55:54 +02:00
|
|
|
|
|
|
|
StickerSetListLogEvent log_event;
|
|
|
|
auto status = log_event_parse(log_event, value);
|
|
|
|
if (status.is_error()) {
|
|
|
|
// can't happen unless database is broken
|
|
|
|
LOG(ERROR) << "Can't load old trending sticker set list: " << status << ' ' << format::as_hex_dump<4>(Slice(value));
|
2022-07-17 21:39:22 +02:00
|
|
|
return reload_old_featured_sticker_sets(sticker_type);
|
2020-04-18 00:55:54 +02:00
|
|
|
}
|
2022-06-17 18:16:17 +02:00
|
|
|
CHECK(!log_event.is_premium_);
|
2020-04-18 00:55:54 +02:00
|
|
|
|
|
|
|
vector<StickerSetId> sets_to_load;
|
2022-06-17 18:16:17 +02:00
|
|
|
for (auto sticker_set_id : log_event.sticker_set_ids_) {
|
2020-04-18 00:55:54 +02:00
|
|
|
StickerSet *sticker_set = get_sticker_set(sticker_set_id);
|
|
|
|
CHECK(sticker_set != nullptr);
|
2022-08-07 18:47:50 +02:00
|
|
|
if (!sticker_set->is_inited_) {
|
2020-04-18 00:55:54 +02:00
|
|
|
sets_to_load.push_back(sticker_set_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
load_sticker_sets_without_stickers(
|
|
|
|
std::move(sets_to_load),
|
|
|
|
PromiseCreator::lambda(
|
2022-07-17 21:39:22 +02:00
|
|
|
[sticker_type, generation, sticker_set_ids = std::move(log_event.sticker_set_ids_)](Result<> result) mutable {
|
2020-04-18 00:55:54 +02:00
|
|
|
if (result.is_ok()) {
|
|
|
|
send_closure(G()->stickers_manager(), &StickersManager::on_load_old_featured_sticker_sets_finished,
|
2022-07-17 21:39:22 +02:00
|
|
|
sticker_type, generation, std::move(sticker_set_ids));
|
2020-04-18 00:55:54 +02:00
|
|
|
} else {
|
2022-07-17 21:39:22 +02:00
|
|
|
send_closure(G()->stickers_manager(), &StickersManager::reload_old_featured_sticker_sets, sticker_type,
|
|
|
|
generation);
|
2020-04-18 00:55:54 +02:00
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2022-07-17 21:39:22 +02:00
|
|
|
void StickersManager::on_load_old_featured_sticker_sets_finished(StickerType sticker_type, uint32 generation,
|
2020-04-18 00:55:54 +02:00
|
|
|
vector<StickerSetId> &&featured_sticker_set_ids) {
|
2022-07-17 21:39:22 +02:00
|
|
|
auto type = static_cast<int32>(sticker_type);
|
|
|
|
if (generation != old_featured_sticker_set_generation_[type]) {
|
|
|
|
fix_old_featured_sticker_set_count(sticker_type); // must never be needed
|
2020-04-18 00:55:54 +02:00
|
|
|
return;
|
|
|
|
}
|
2022-07-17 21:39:22 +02:00
|
|
|
CHECK(sticker_type == StickerType::Regular);
|
|
|
|
append(old_featured_sticker_set_ids_[type], std::move(featured_sticker_set_ids));
|
|
|
|
fix_old_featured_sticker_set_count(sticker_type);
|
2022-04-13 16:40:12 +02:00
|
|
|
set_promises(load_old_featured_sticker_sets_queries_);
|
2020-04-18 00:55:54 +02:00
|
|
|
}
|
|
|
|
|
2019-09-18 05:55:43 +02:00
|
|
|
vector<StickerSetId> StickersManager::get_attached_sticker_sets(FileId file_id, Promise<Unit> &&promise) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (!file_id.is_valid()) {
|
2021-09-24 09:59:51 +02:00
|
|
|
promise.set_error(Status::Error(400, "Wrong file_id specified"));
|
2018-12-31 20:04:05 +01:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2019-01-24 20:41:05 +01:00
|
|
|
auto it = attached_sticker_sets_.find(file_id);
|
|
|
|
if (it != attached_sticker_sets_.end()) {
|
|
|
|
promise.set_value(Unit());
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
send_get_attached_stickers_query(file_id, std::move(promise));
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
void StickersManager::send_get_attached_stickers_query(FileId file_id, Promise<Unit> &&promise) {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto file_view = td_->file_manager_->get_file_view(file_id);
|
|
|
|
if (file_view.empty()) {
|
2021-09-24 09:59:51 +02:00
|
|
|
return promise.set_error(Status::Error(400, "File not found"));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-10-01 13:34:42 +02:00
|
|
|
if (!file_view.has_remote_location() ||
|
|
|
|
(!file_view.remote_location().is_document() && !file_view.remote_location().is_photo()) ||
|
2018-12-31 20:04:05 +01:00
|
|
|
file_view.remote_location().is_web()) {
|
2019-01-24 20:41:05 +01:00
|
|
|
return promise.set_value(Unit());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
tl_object_ptr<telegram_api::InputStickeredMedia> input_stickered_media;
|
2019-01-24 20:41:05 +01:00
|
|
|
string file_reference;
|
2019-11-17 20:41:28 +01:00
|
|
|
if (file_view.main_remote_location().is_photo()) {
|
|
|
|
auto input_photo = file_view.main_remote_location().as_input_photo();
|
2019-01-24 20:41:05 +01:00
|
|
|
file_reference = input_photo->file_reference_.as_slice().str();
|
|
|
|
input_stickered_media = make_tl_object<telegram_api::inputStickeredMediaPhoto>(std::move(input_photo));
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
2019-11-17 20:41:28 +01:00
|
|
|
auto input_document = file_view.main_remote_location().as_input_document();
|
2019-01-24 20:41:05 +01:00
|
|
|
file_reference = input_document->file_reference_.as_slice().str();
|
|
|
|
input_stickered_media = make_tl_object<telegram_api::inputStickeredMediaDocument>(std::move(input_document));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2019-01-24 20:41:05 +01:00
|
|
|
td_->create_handler<GetAttachedStickerSetsQuery>(std::move(promise))
|
|
|
|
->send(file_id, std::move(file_reference), std::move(input_stickered_media));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void StickersManager::on_get_attached_sticker_sets(
|
|
|
|
FileId file_id, vector<tl_object_ptr<telegram_api::StickerSetCovered>> &&sticker_sets) {
|
2022-02-10 16:03:03 +01:00
|
|
|
CHECK(file_id.is_valid());
|
2019-09-18 05:55:43 +02:00
|
|
|
vector<StickerSetId> &sticker_set_ids = attached_sticker_sets_[file_id];
|
2018-12-31 20:04:05 +01:00
|
|
|
sticker_set_ids.clear();
|
|
|
|
for (auto &sticker_set_covered : sticker_sets) {
|
2019-12-29 02:59:34 +01:00
|
|
|
auto sticker_set_id =
|
|
|
|
on_get_sticker_set_covered(std::move(sticker_set_covered), true, "on_get_attached_sticker_sets");
|
2019-09-18 05:55:43 +02:00
|
|
|
if (sticker_set_id.is_valid()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto sticker_set = get_sticker_set(sticker_set_id);
|
|
|
|
CHECK(sticker_set != nullptr);
|
2021-11-14 08:19:03 +01:00
|
|
|
update_sticker_set(sticker_set, "on_get_attached_sticker_sets");
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
sticker_set_ids.push_back(sticker_set_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
send_update_installed_sticker_sets();
|
|
|
|
}
|
|
|
|
|
2018-02-13 21:41:37 +01:00
|
|
|
// -1 - order can't be applied, because some sticker sets aren't loaded or aren't installed,
|
2018-12-31 20:04:05 +01:00
|
|
|
// 0 - order wasn't changed, 1 - order was partly replaced by the new order, 2 - order was replaced by the new order
|
2022-07-14 15:17:19 +02:00
|
|
|
int StickersManager::apply_installed_sticker_sets_order(StickerType sticker_type,
|
|
|
|
const vector<StickerSetId> &sticker_set_ids) {
|
2022-07-24 13:01:58 +02:00
|
|
|
auto type = static_cast<int32>(sticker_type);
|
|
|
|
if (!are_installed_sticker_sets_loaded_[type]) {
|
2018-02-13 21:41:37 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2022-07-24 13:01:58 +02:00
|
|
|
vector<StickerSetId> ¤t_sticker_set_ids = installed_sticker_set_ids_[type];
|
2018-12-31 20:04:05 +01:00
|
|
|
if (sticker_set_ids == current_sticker_set_ids) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-03-11 19:38:48 +01:00
|
|
|
FlatHashSet<StickerSetId, StickerSetIdHash> valid_set_ids;
|
|
|
|
for (auto sticker_set_id : current_sticker_set_ids) {
|
|
|
|
valid_set_ids.insert(sticker_set_id);
|
|
|
|
}
|
|
|
|
|
2019-09-18 05:55:43 +02:00
|
|
|
vector<StickerSetId> new_sticker_set_ids;
|
2018-12-31 20:04:05 +01:00
|
|
|
for (auto sticker_set_id : sticker_set_ids) {
|
|
|
|
auto it = valid_set_ids.find(sticker_set_id);
|
|
|
|
if (it != valid_set_ids.end()) {
|
|
|
|
new_sticker_set_ids.push_back(sticker_set_id);
|
|
|
|
valid_set_ids.erase(it);
|
2018-02-13 21:41:37 +01:00
|
|
|
} else {
|
|
|
|
return -1;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (new_sticker_set_ids.empty()) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (!valid_set_ids.empty()) {
|
2019-09-18 05:55:43 +02:00
|
|
|
vector<StickerSetId> missed_sticker_set_ids;
|
2018-12-31 20:04:05 +01:00
|
|
|
for (auto sticker_set_id : current_sticker_set_ids) {
|
|
|
|
auto it = valid_set_ids.find(sticker_set_id);
|
|
|
|
if (it != valid_set_ids.end()) {
|
|
|
|
missed_sticker_set_ids.push_back(sticker_set_id);
|
|
|
|
valid_set_ids.erase(it);
|
|
|
|
}
|
|
|
|
}
|
2018-02-13 21:41:37 +01:00
|
|
|
append(missed_sticker_set_ids, new_sticker_set_ids);
|
2018-12-31 20:04:05 +01:00
|
|
|
new_sticker_set_ids = std::move(missed_sticker_set_ids);
|
|
|
|
}
|
2018-02-13 21:41:37 +01:00
|
|
|
CHECK(valid_set_ids.empty());
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
if (new_sticker_set_ids == current_sticker_set_ids) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
current_sticker_set_ids = std::move(new_sticker_set_ids);
|
|
|
|
|
2022-07-24 13:01:58 +02:00
|
|
|
need_update_installed_sticker_sets_[type] = true;
|
2018-12-31 20:04:05 +01:00
|
|
|
if (sticker_set_ids != current_sticker_set_ids) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
2022-07-14 15:17:19 +02:00
|
|
|
void StickersManager::on_update_sticker_sets_order(StickerType sticker_type,
|
|
|
|
const vector<StickerSetId> &sticker_set_ids) {
|
|
|
|
int result = apply_installed_sticker_sets_order(sticker_type, sticker_set_ids);
|
2018-02-13 21:41:37 +01:00
|
|
|
if (result < 0) {
|
2022-07-14 15:17:19 +02:00
|
|
|
return reload_installed_sticker_sets(sticker_type, true);
|
2018-02-13 21:41:37 +01:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
if (result > 0) {
|
|
|
|
send_update_installed_sticker_sets();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-02 17:35:57 +02:00
|
|
|
// -1 - sticker set can't be moved to top, 0 - order wasn't changed, 1 - sticker set was moved to top
|
|
|
|
int StickersManager::move_installed_sticker_set_to_top(StickerType sticker_type, StickerSetId sticker_set_id) {
|
2022-09-04 20:35:11 +02:00
|
|
|
LOG(INFO) << "Move " << sticker_set_id << " to top of " << sticker_type;
|
2022-09-02 17:35:57 +02:00
|
|
|
auto type = static_cast<int32>(sticker_type);
|
|
|
|
if (!are_installed_sticker_sets_loaded_[type]) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
vector<StickerSetId> ¤t_sticker_set_ids = installed_sticker_set_ids_[type];
|
|
|
|
auto it = std::find(current_sticker_set_ids.begin(), current_sticker_set_ids.end(), sticker_set_id);
|
|
|
|
if (it == current_sticker_set_ids.end()) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (sticker_set_id == current_sticker_set_ids[0]) {
|
|
|
|
CHECK(it == current_sticker_set_ids.begin());
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::rotate(current_sticker_set_ids.begin(), it, it + 1);
|
|
|
|
|
|
|
|
need_update_installed_sticker_sets_[type] = true;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void StickersManager::on_update_move_sticker_set_to_top(StickerType sticker_type, StickerSetId sticker_set_id) {
|
|
|
|
int result = move_installed_sticker_set_to_top(sticker_type, sticker_set_id);
|
|
|
|
if (result < 0) {
|
|
|
|
return reload_installed_sticker_sets(sticker_type, true);
|
|
|
|
}
|
|
|
|
if (result > 0) {
|
|
|
|
send_update_installed_sticker_sets();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-14 15:17:19 +02:00
|
|
|
void StickersManager::reorder_installed_sticker_sets(StickerType sticker_type,
|
|
|
|
const vector<StickerSetId> &sticker_set_ids,
|
2018-12-31 20:04:05 +01:00
|
|
|
Promise<Unit> &&promise) {
|
2022-07-14 15:17:19 +02:00
|
|
|
auto result = apply_installed_sticker_sets_order(sticker_type, sticker_set_ids);
|
2018-02-13 21:41:37 +01:00
|
|
|
if (result < 0) {
|
|
|
|
return promise.set_error(Status::Error(400, "Wrong sticker set list"));
|
|
|
|
}
|
|
|
|
if (result > 0) {
|
2022-07-24 13:01:58 +02:00
|
|
|
auto type = static_cast<int32>(sticker_type);
|
|
|
|
td_->create_handler<ReorderStickerSetsQuery>()->send(sticker_type, installed_sticker_set_ids_[type]);
|
2018-12-31 20:04:05 +01:00
|
|
|
send_update_installed_sticker_sets();
|
|
|
|
}
|
|
|
|
promise.set_value(Unit());
|
|
|
|
}
|
|
|
|
|
2022-09-04 20:35:11 +02:00
|
|
|
void StickersManager::move_sticker_set_to_top_by_sticker_id(FileId sticker_id) {
|
|
|
|
LOG(INFO) << "Move to top sticker set of " << sticker_id;
|
|
|
|
const auto *s = get_sticker(sticker_id);
|
|
|
|
if (s == nullptr || !s->set_id_.is_valid()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (s->type_ == StickerType::CustomEmoji) {
|
|
|
|
// just in case
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (move_installed_sticker_set_to_top(s->type_, s->set_id_) > 0) {
|
|
|
|
send_update_installed_sticker_sets();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-03 00:26:32 +02:00
|
|
|
void StickersManager::move_sticker_set_to_top_by_custom_emoji_ids(const vector<CustomEmojiId> &custom_emoji_ids) {
|
2022-09-04 20:35:11 +02:00
|
|
|
LOG(INFO) << "Move to top sticker set of " << custom_emoji_ids;
|
|
|
|
StickerSetId sticker_set_id;
|
|
|
|
for (auto custom_emoji_id : custom_emoji_ids) {
|
|
|
|
auto sticker_id = custom_emoji_to_sticker_id_.get(custom_emoji_id);
|
|
|
|
if (!sticker_id.is_valid()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto *s = get_sticker(sticker_id);
|
2022-09-05 19:26:10 +02:00
|
|
|
CHECK(s != nullptr);
|
|
|
|
CHECK(s->type_ == StickerType::CustomEmoji);
|
|
|
|
if (!s->set_id_.is_valid()) {
|
2022-09-04 20:35:11 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (s->set_id_ != sticker_set_id) {
|
|
|
|
if (sticker_set_id.is_valid()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
sticker_set_id = s->set_id_;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CHECK(sticker_set_id.is_valid());
|
|
|
|
if (move_installed_sticker_set_to_top(StickerType::CustomEmoji, sticker_set_id) > 0) {
|
|
|
|
send_update_installed_sticker_sets();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-18 17:20:43 +01:00
|
|
|
Result<std::tuple<FileId, bool, bool, StickerFormat>> StickersManager::prepare_input_sticker(
|
2022-07-21 16:31:27 +02:00
|
|
|
td_api::inputSticker *sticker, StickerType sticker_type) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (sticker == nullptr) {
|
2021-09-24 09:59:51 +02:00
|
|
|
return Status::Error(400, "Input sticker must be non-empty");
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2022-01-25 14:56:10 +01:00
|
|
|
if (!clean_input_string(sticker->emojis_)) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return Status::Error(400, "Emojis must be encoded in UTF-8");
|
|
|
|
}
|
|
|
|
|
2022-07-14 12:58:33 +02:00
|
|
|
if (sticker->format_ == nullptr) {
|
|
|
|
return Status::Error(400, "Sticker format must be non-empty");
|
2022-01-25 14:56:10 +01:00
|
|
|
}
|
|
|
|
|
2022-07-21 16:31:27 +02:00
|
|
|
return prepare_input_file(sticker->sticker_, get_sticker_format(sticker->format_), sticker_type, false);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2022-01-18 17:20:43 +01:00
|
|
|
Result<std::tuple<FileId, bool, bool, StickerFormat>> StickersManager::prepare_input_file(
|
2022-07-17 22:49:46 +02:00
|
|
|
const tl_object_ptr<td_api::InputFile> &input_file, StickerFormat format, StickerType type, bool for_thumbnail) {
|
2022-01-31 18:22:07 +01:00
|
|
|
auto file_type = format == StickerFormat::Tgs ? FileType::Sticker : FileType::Document;
|
2022-01-18 17:20:43 +01:00
|
|
|
auto r_file_id = td_->file_manager_->get_input_file_id(file_type, input_file, DialogId(), for_thumbnail, false);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (r_file_id.is_error()) {
|
2021-09-24 09:59:51 +02:00
|
|
|
return Status::Error(400, r_file_id.error().message());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
auto file_id = r_file_id.move_as_ok();
|
2020-03-20 14:36:07 +01:00
|
|
|
if (file_id.empty()) {
|
2022-01-18 17:20:43 +01:00
|
|
|
return std::make_tuple(FileId(), false, false, StickerFormat::Unknown);
|
2020-03-20 14:36:07 +01:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2022-01-31 18:41:11 +01:00
|
|
|
if (format == StickerFormat::Tgs) {
|
2020-03-23 16:03:13 +01:00
|
|
|
int32 width = for_thumbnail ? 100 : 512;
|
2022-04-26 15:01:29 +02:00
|
|
|
create_sticker(file_id, FileId(), string(), PhotoSize(), get_dimensions(width, width, "prepare_input_file"),
|
2022-07-14 16:11:36 +02:00
|
|
|
nullptr, nullptr, format, nullptr);
|
2022-01-31 18:41:11 +01:00
|
|
|
} else if (format == StickerFormat::Webm) {
|
|
|
|
td_->documents_manager_->create_document(file_id, string(), PhotoSize(), "sticker.webm", "video/webm", false);
|
2020-03-23 16:03:13 +01:00
|
|
|
} else {
|
|
|
|
td_->documents_manager_->create_document(file_id, string(), PhotoSize(), "sticker.png", "image/png", false);
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
FileView file_view = td_->file_manager_->get_file_view(file_id);
|
|
|
|
if (file_view.is_encrypted()) {
|
|
|
|
return Status::Error(400, "Can't use encrypted file");
|
|
|
|
}
|
|
|
|
|
2019-11-17 20:41:28 +01:00
|
|
|
if (file_view.has_remote_location() && file_view.main_remote_location().is_web()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return Status::Error(400, "Can't use web file to create a sticker");
|
|
|
|
}
|
|
|
|
bool is_url = false;
|
|
|
|
bool is_local = false;
|
|
|
|
if (file_view.has_remote_location()) {
|
2019-11-17 20:41:28 +01:00
|
|
|
CHECK(file_view.main_remote_location().is_document());
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
|
|
|
if (file_view.has_url()) {
|
|
|
|
is_url = true;
|
|
|
|
} else {
|
2022-01-18 17:20:43 +01:00
|
|
|
if (file_view.has_local_location() &&
|
2022-07-17 22:49:46 +02:00
|
|
|
file_view.expected_size() > get_max_sticker_file_size(format, type, for_thumbnail)) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return Status::Error(400, "File is too big");
|
|
|
|
}
|
|
|
|
is_local = true;
|
|
|
|
}
|
|
|
|
}
|
2022-01-18 17:20:43 +01:00
|
|
|
return std::make_tuple(file_id, is_url, is_local, format);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2022-01-25 14:56:10 +01:00
|
|
|
FileId StickersManager::upload_sticker_file(UserId user_id, tl_object_ptr<td_api::inputSticker> &&sticker,
|
2018-12-31 20:04:05 +01:00
|
|
|
Promise<Unit> &&promise) {
|
2021-06-21 21:25:26 +02:00
|
|
|
bool is_bot = td_->auth_manager_->is_bot();
|
|
|
|
if (!is_bot) {
|
|
|
|
user_id = td_->contacts_manager_->get_my_id();
|
|
|
|
}
|
|
|
|
|
2021-12-16 20:46:03 +01:00
|
|
|
auto r_input_user = td_->contacts_manager_->get_input_user(user_id);
|
|
|
|
if (r_input_user.is_error()) {
|
|
|
|
promise.set_error(r_input_user.move_as_error());
|
2018-12-31 20:04:05 +01:00
|
|
|
return FileId();
|
|
|
|
}
|
|
|
|
|
2022-07-21 16:31:27 +02:00
|
|
|
// StickerType::Regular has less restrictions
|
|
|
|
auto r_file_id = prepare_input_sticker(sticker.get(), StickerType::Regular);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (r_file_id.is_error()) {
|
|
|
|
promise.set_error(r_file_id.move_as_error());
|
|
|
|
return FileId();
|
|
|
|
}
|
|
|
|
auto file_id = std::get<0>(r_file_id.ok());
|
|
|
|
auto is_url = std::get<1>(r_file_id.ok());
|
|
|
|
auto is_local = std::get<2>(r_file_id.ok());
|
|
|
|
|
|
|
|
if (is_url) {
|
|
|
|
do_upload_sticker_file(user_id, file_id, nullptr, std::move(promise));
|
|
|
|
} else if (is_local) {
|
|
|
|
upload_sticker_file(user_id, file_id, std::move(promise));
|
|
|
|
} else {
|
|
|
|
promise.set_value(Unit());
|
|
|
|
}
|
|
|
|
|
|
|
|
return file_id;
|
|
|
|
}
|
|
|
|
|
2022-01-25 14:56:10 +01:00
|
|
|
tl_object_ptr<telegram_api::inputStickerSetItem> StickersManager::get_input_sticker(const td_api::inputSticker *sticker,
|
2018-12-31 20:04:05 +01:00
|
|
|
FileId file_id) const {
|
2020-03-23 16:03:13 +01:00
|
|
|
CHECK(sticker != nullptr);
|
2018-12-31 20:04:05 +01:00
|
|
|
FileView file_view = td_->file_manager_->get_file_view(file_id);
|
|
|
|
CHECK(file_view.has_remote_location());
|
2019-11-17 20:41:28 +01:00
|
|
|
auto input_document = file_view.main_remote_location().as_input_document();
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
tl_object_ptr<telegram_api::maskCoords> mask_coords;
|
2022-07-21 14:54:43 +02:00
|
|
|
auto mask_position = sticker->mask_position_.get();
|
|
|
|
if (mask_position != nullptr && mask_position->point_ != nullptr) {
|
|
|
|
auto point = [mask_point_id = mask_position->point_->get_id()] {
|
|
|
|
switch (mask_point_id) {
|
|
|
|
case td_api::maskPointForehead::ID:
|
|
|
|
return 0;
|
|
|
|
case td_api::maskPointEyes::ID:
|
|
|
|
return 1;
|
|
|
|
case td_api::maskPointMouth::ID:
|
|
|
|
return 2;
|
|
|
|
case td_api::maskPointChin::ID:
|
|
|
|
return 3;
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}();
|
|
|
|
mask_coords = make_tl_object<telegram_api::maskCoords>(point, mask_position->x_shift_, mask_position->y_shift_,
|
|
|
|
mask_position->scale_);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int32 flags = 0;
|
|
|
|
if (mask_coords != nullptr) {
|
|
|
|
flags |= telegram_api::inputStickerSetItem::MASK_COORDS_MASK;
|
|
|
|
}
|
|
|
|
|
2022-01-25 14:56:10 +01:00
|
|
|
return make_tl_object<telegram_api::inputStickerSetItem>(flags, std::move(input_document), sticker->emojis_,
|
|
|
|
std::move(mask_coords));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-06-17 19:38:03 +02:00
|
|
|
void StickersManager::get_suggested_sticker_set_name(string title, Promise<string> &&promise) {
|
|
|
|
title = strip_empty_characters(title, MAX_STICKER_SET_TITLE_LENGTH);
|
|
|
|
if (title.empty()) {
|
2022-09-10 21:48:34 +02:00
|
|
|
return promise.set_error(Status::Error(400, "Sticker set title must be non-empty"));
|
2021-06-17 19:38:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
td_->create_handler<SuggestStickerSetShortNameQuery>(std::move(promise))->send(title);
|
|
|
|
}
|
|
|
|
|
2021-06-17 20:29:07 +02:00
|
|
|
void StickersManager::check_sticker_set_name(const string &name, Promise<CheckStickerSetNameResult> &&promise) {
|
|
|
|
if (name.empty()) {
|
|
|
|
return promise.set_value(CheckStickerSetNameResult::Invalid);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto request_promise = PromiseCreator::lambda([promise = std::move(promise)](Result<bool> result) mutable {
|
|
|
|
if (result.is_error()) {
|
|
|
|
auto error = result.move_as_error();
|
|
|
|
if (error.message() == "SHORT_NAME_INVALID") {
|
|
|
|
return promise.set_value(CheckStickerSetNameResult::Invalid);
|
|
|
|
}
|
|
|
|
if (error.message() == "SHORT_NAME_OCCUPIED") {
|
|
|
|
return promise.set_value(CheckStickerSetNameResult::Occupied);
|
|
|
|
}
|
|
|
|
return promise.set_error(std::move(error));
|
|
|
|
}
|
|
|
|
|
|
|
|
promise.set_value(CheckStickerSetNameResult::Ok);
|
|
|
|
});
|
|
|
|
|
|
|
|
return td_->create_handler<CheckStickerSetShortNameQuery>(std::move(request_promise))->send(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
td_api::object_ptr<td_api::CheckStickerSetNameResult> StickersManager::get_check_sticker_set_name_result_object(
|
|
|
|
CheckStickerSetNameResult result) {
|
|
|
|
switch (result) {
|
|
|
|
case CheckStickerSetNameResult::Ok:
|
|
|
|
return td_api::make_object<td_api::checkStickerSetNameResultOk>();
|
|
|
|
case CheckStickerSetNameResult::Invalid:
|
|
|
|
return td_api::make_object<td_api::checkStickerSetNameResultNameInvalid>();
|
|
|
|
case CheckStickerSetNameResult::Occupied:
|
|
|
|
return td_api::make_object<td_api::checkStickerSetNameResultNameOccupied>();
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-08 16:24:01 +02:00
|
|
|
void StickersManager::create_new_sticker_set(UserId user_id, string title, string short_name, StickerType sticker_type,
|
2022-07-21 16:31:27 +02:00
|
|
|
vector<td_api::object_ptr<td_api::inputSticker>> &&stickers,
|
2022-08-08 16:24:01 +02:00
|
|
|
string software,
|
|
|
|
Promise<td_api::object_ptr<td_api::stickerSet>> &&promise) {
|
2021-06-17 19:14:48 +02:00
|
|
|
bool is_bot = td_->auth_manager_->is_bot();
|
|
|
|
if (!is_bot) {
|
|
|
|
user_id = td_->contacts_manager_->get_my_id();
|
|
|
|
}
|
2021-12-16 20:46:03 +01:00
|
|
|
|
|
|
|
TRY_RESULT_PROMISE(promise, input_user, td_->contacts_manager_->get_input_user(user_id));
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
title = strip_empty_characters(title, MAX_STICKER_SET_TITLE_LENGTH);
|
|
|
|
if (title.empty()) {
|
2022-09-10 21:48:34 +02:00
|
|
|
return promise.set_error(Status::Error(400, "Sticker set title must be non-empty"));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
short_name = strip_empty_characters(short_name, MAX_STICKER_SET_SHORT_NAME_LENGTH);
|
|
|
|
if (short_name.empty()) {
|
2022-09-10 21:48:34 +02:00
|
|
|
return promise.set_error(Status::Error(400, "Sticker set name must be non-empty"));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2020-03-23 16:03:13 +01:00
|
|
|
if (stickers.empty()) {
|
|
|
|
return promise.set_error(Status::Error(400, "At least 1 sticker must be specified"));
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
vector<FileId> file_ids;
|
|
|
|
file_ids.reserve(stickers.size());
|
|
|
|
vector<FileId> local_file_ids;
|
|
|
|
vector<FileId> url_file_ids;
|
2022-03-11 19:38:48 +01:00
|
|
|
FlatHashSet<int32> sticker_formats;
|
2022-01-18 17:20:43 +01:00
|
|
|
StickerFormat sticker_format = StickerFormat::Unknown;
|
2018-12-31 20:04:05 +01:00
|
|
|
for (auto &sticker : stickers) {
|
2022-07-21 16:31:27 +02:00
|
|
|
auto r_file_id = prepare_input_sticker(sticker.get(), sticker_type);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (r_file_id.is_error()) {
|
|
|
|
return promise.set_error(r_file_id.move_as_error());
|
|
|
|
}
|
|
|
|
auto file_id = std::get<0>(r_file_id.ok());
|
|
|
|
auto is_url = std::get<1>(r_file_id.ok());
|
|
|
|
auto is_local = std::get<2>(r_file_id.ok());
|
2022-01-18 17:20:43 +01:00
|
|
|
sticker_format = std::get<3>(r_file_id.ok());
|
|
|
|
if (is_sticker_format_animated(sticker_format) && is_url) {
|
|
|
|
return promise.set_error(Status::Error(400, "Animated stickers can't be uploaded by URL"));
|
2020-03-23 16:03:13 +01:00
|
|
|
}
|
2022-07-14 14:02:55 +02:00
|
|
|
sticker_formats.insert(static_cast<int32>(get_sticker_format(sticker->format_)) + 1);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
file_ids.push_back(file_id);
|
|
|
|
if (is_url) {
|
|
|
|
url_file_ids.push_back(file_id);
|
|
|
|
} else if (is_local) {
|
|
|
|
local_file_ids.push_back(file_id);
|
|
|
|
}
|
|
|
|
}
|
2022-01-18 17:20:43 +01:00
|
|
|
if (sticker_formats.size() != 1) {
|
|
|
|
return promise.set_error(Status::Error(400, "All stickers must be of the same format"));
|
2020-03-23 16:03:13 +01:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
auto pending_new_sticker_set = make_unique<PendingNewStickerSet>();
|
2022-08-07 18:47:50 +02:00
|
|
|
pending_new_sticker_set->user_id_ = user_id;
|
|
|
|
pending_new_sticker_set->title_ = std::move(title);
|
|
|
|
pending_new_sticker_set->short_name_ = short_name;
|
|
|
|
pending_new_sticker_set->sticker_format_ = sticker_format;
|
|
|
|
pending_new_sticker_set->sticker_type_ = sticker_type;
|
|
|
|
pending_new_sticker_set->file_ids_ = std::move(file_ids);
|
|
|
|
pending_new_sticker_set->stickers_ = std::move(stickers);
|
|
|
|
pending_new_sticker_set->software_ = std::move(software);
|
|
|
|
pending_new_sticker_set->promise_ = std::move(promise);
|
|
|
|
|
|
|
|
auto &multipromise = pending_new_sticker_set->upload_files_multipromise_;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
int64 random_id;
|
|
|
|
do {
|
|
|
|
random_id = Random::secure_int64();
|
2022-05-01 22:03:06 +02:00
|
|
|
} while (random_id == 0 || pending_new_sticker_sets_.count(random_id) > 0);
|
2018-12-31 20:04:05 +01:00
|
|
|
pending_new_sticker_sets_[random_id] = std::move(pending_new_sticker_set);
|
|
|
|
|
2021-10-07 14:28:52 +02:00
|
|
|
multipromise.add_promise(PromiseCreator::lambda([actor_id = actor_id(this), random_id](Result<Unit> result) {
|
|
|
|
send_closure_later(actor_id, &StickersManager::on_new_stickers_uploaded, random_id, std::move(result));
|
2018-12-31 20:04:05 +01:00
|
|
|
}));
|
|
|
|
auto lock_promise = multipromise.get_promise();
|
|
|
|
|
|
|
|
for (auto file_id : url_file_ids) {
|
|
|
|
do_upload_sticker_file(user_id, file_id, nullptr, multipromise.get_promise());
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto file_id : local_file_ids) {
|
|
|
|
upload_sticker_file(user_id, file_id, multipromise.get_promise());
|
|
|
|
}
|
|
|
|
|
|
|
|
lock_promise.set_value(Unit());
|
|
|
|
}
|
|
|
|
|
|
|
|
void StickersManager::upload_sticker_file(UserId user_id, FileId file_id, Promise<Unit> &&promise) {
|
2020-03-23 16:03:13 +01:00
|
|
|
FileId upload_file_id;
|
|
|
|
if (td_->file_manager_->get_file_view(file_id).get_type() == FileType::Sticker) {
|
2020-12-19 12:31:42 +01:00
|
|
|
CHECK(get_input_media(file_id, nullptr, nullptr, string()) == nullptr);
|
2020-03-23 16:03:13 +01:00
|
|
|
upload_file_id = dup_sticker(td_->file_manager_->dup_file_id(file_id), file_id);
|
|
|
|
} else {
|
|
|
|
CHECK(td_->documents_manager_->get_input_media(file_id, nullptr, nullptr) == nullptr);
|
|
|
|
upload_file_id = td_->documents_manager_->dup_document(td_->file_manager_->dup_file_id(file_id), file_id);
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2022-02-10 16:03:03 +01:00
|
|
|
CHECK(upload_file_id.is_valid());
|
2018-12-31 20:04:05 +01:00
|
|
|
being_uploaded_files_[upload_file_id] = {user_id, std::move(promise)};
|
|
|
|
LOG(INFO) << "Ask to upload sticker file " << upload_file_id;
|
|
|
|
td_->file_manager_->upload(upload_file_id, upload_sticker_file_callback_, 2, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void StickersManager::on_upload_sticker_file(FileId file_id, tl_object_ptr<telegram_api::InputFile> input_file) {
|
|
|
|
LOG(INFO) << "Sticker file " << file_id << " has been uploaded";
|
|
|
|
|
|
|
|
auto it = being_uploaded_files_.find(file_id);
|
|
|
|
CHECK(it != being_uploaded_files_.end());
|
|
|
|
|
|
|
|
auto user_id = it->second.first;
|
|
|
|
auto promise = std::move(it->second.second);
|
|
|
|
|
|
|
|
being_uploaded_files_.erase(it);
|
|
|
|
|
|
|
|
do_upload_sticker_file(user_id, file_id, std::move(input_file), std::move(promise));
|
|
|
|
}
|
|
|
|
|
|
|
|
void StickersManager::on_upload_sticker_file_error(FileId file_id, Status status) {
|
|
|
|
if (G()->close_flag()) {
|
|
|
|
// do not fail upload if closing
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG(WARNING) << "Sticker file " << file_id << " has upload error " << status;
|
|
|
|
CHECK(status.is_error());
|
|
|
|
|
|
|
|
auto it = being_uploaded_files_.find(file_id);
|
|
|
|
CHECK(it != being_uploaded_files_.end());
|
|
|
|
|
|
|
|
auto promise = std::move(it->second.second);
|
|
|
|
|
|
|
|
being_uploaded_files_.erase(it);
|
|
|
|
|
|
|
|
// TODO FILE_PART_X_MISSING support
|
|
|
|
|
|
|
|
promise.set_error(Status::Error(status.code() > 0 ? status.code() : 500,
|
|
|
|
status.message())); // TODO CHECK that status has always a code
|
|
|
|
}
|
|
|
|
|
|
|
|
void StickersManager::do_upload_sticker_file(UserId user_id, FileId file_id,
|
|
|
|
tl_object_ptr<telegram_api::InputFile> &&input_file,
|
|
|
|
Promise<Unit> &&promise) {
|
2022-10-13 13:51:00 +02:00
|
|
|
TRY_STATUS_PROMISE(promise, G()->close_status());
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
DialogId dialog_id(user_id);
|
|
|
|
auto input_peer = td_->messages_manager_->get_input_peer(dialog_id, AccessRights::Write);
|
|
|
|
if (input_peer == nullptr) {
|
2022-10-13 13:51:00 +02:00
|
|
|
if (input_file != nullptr) {
|
|
|
|
td_->file_manager_->cancel_upload(file_id);
|
|
|
|
}
|
2021-09-24 09:59:51 +02:00
|
|
|
return promise.set_error(Status::Error(400, "Have no access to the user"));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2020-03-23 16:03:13 +01:00
|
|
|
FileView file_view = td_->file_manager_->get_file_view(file_id);
|
2022-01-18 17:20:43 +01:00
|
|
|
FileType file_type = file_view.get_type();
|
2020-03-23 16:03:13 +01:00
|
|
|
|
2019-01-25 02:38:11 +01:00
|
|
|
bool had_input_file = input_file != nullptr;
|
2022-01-18 17:20:43 +01:00
|
|
|
auto input_media = file_type == FileType::Sticker
|
|
|
|
? get_input_media(file_id, std::move(input_file), nullptr, string())
|
|
|
|
: td_->documents_manager_->get_input_media(file_id, std::move(input_file), nullptr);
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK(input_media != nullptr);
|
2019-01-25 02:38:11 +01:00
|
|
|
if (had_input_file && !FileManager::extract_was_uploaded(input_media)) {
|
2022-10-13 13:51:00 +02:00
|
|
|
// if we had InputFile, but has failed to use it for input_media, then we need to immediately cancel file upload
|
2019-01-25 02:38:11 +01:00
|
|
|
// so the next upload with the same file can succeed
|
2019-01-25 02:52:38 +01:00
|
|
|
td_->file_manager_->cancel_upload(file_id);
|
2019-01-25 02:38:11 +01:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
td_->create_handler<UploadStickerFileQuery>(std::move(promise))
|
|
|
|
->send(std::move(input_peer), file_id, std::move(input_media));
|
|
|
|
}
|
|
|
|
|
|
|
|
void StickersManager::on_uploaded_sticker_file(FileId file_id, tl_object_ptr<telegram_api::MessageMedia> media,
|
|
|
|
Promise<Unit> &&promise) {
|
|
|
|
CHECK(media != nullptr);
|
2020-03-23 16:03:13 +01:00
|
|
|
LOG(INFO) << "Receive uploaded sticker file " << to_string(media);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (media->get_id() != telegram_api::messageMediaDocument::ID) {
|
|
|
|
return promise.set_error(Status::Error(400, "Can't upload sticker file: wrong file type"));
|
|
|
|
}
|
|
|
|
|
|
|
|
auto message_document = move_tl_object_as<telegram_api::messageMediaDocument>(media);
|
|
|
|
auto document_ptr = std::move(message_document->document_);
|
|
|
|
int32 document_id = document_ptr->get_id();
|
|
|
|
if (document_id == telegram_api::documentEmpty::ID) {
|
|
|
|
return promise.set_error(Status::Error(400, "Can't upload sticker file: empty file"));
|
|
|
|
}
|
|
|
|
CHECK(document_id == telegram_api::document::ID);
|
|
|
|
|
2020-03-23 16:03:13 +01:00
|
|
|
FileView file_view = td_->file_manager_->get_file_view(file_id);
|
2022-01-18 17:20:43 +01:00
|
|
|
FileType file_type = file_view.get_type();
|
|
|
|
auto expected_document_type = file_type == FileType::Sticker ? Document::Type::Sticker : Document::Type::General;
|
2020-03-23 16:03:13 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
auto parsed_document = td_->documents_manager_->on_get_document(
|
|
|
|
move_tl_object_as<telegram_api::document>(document_ptr), DialogId(), nullptr);
|
2020-03-23 16:03:13 +01:00
|
|
|
if (parsed_document.type != expected_document_type) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return promise.set_error(Status::Error(400, "Wrong file type"));
|
|
|
|
}
|
|
|
|
|
2021-08-26 17:50:28 +02:00
|
|
|
if (parsed_document.file_id != file_id) {
|
2022-01-18 17:20:43 +01:00
|
|
|
if (file_type == FileType::Sticker) {
|
2022-08-03 20:38:03 +02:00
|
|
|
merge_stickers(parsed_document.file_id, file_id);
|
2021-08-26 17:50:28 +02:00
|
|
|
} else {
|
|
|
|
// must not delete the old document, because the file_id could be used for simultaneous URL uploads
|
2022-08-03 20:38:03 +02:00
|
|
|
td_->documents_manager_->merge_documents(parsed_document.file_id, file_id);
|
2021-08-26 17:50:28 +02:00
|
|
|
}
|
2020-03-23 16:03:13 +01:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
promise.set_value(Unit());
|
|
|
|
}
|
|
|
|
|
|
|
|
void StickersManager::on_new_stickers_uploaded(int64 random_id, Result<Unit> result) {
|
2022-08-10 12:57:41 +02:00
|
|
|
if (G()->close_flag()) {
|
|
|
|
result = Global::request_aborted_error();
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
auto it = pending_new_sticker_sets_.find(random_id);
|
|
|
|
CHECK(it != pending_new_sticker_sets_.end());
|
|
|
|
|
|
|
|
auto pending_new_sticker_set = std::move(it->second);
|
|
|
|
CHECK(pending_new_sticker_set != nullptr);
|
|
|
|
|
|
|
|
pending_new_sticker_sets_.erase(it);
|
|
|
|
|
|
|
|
if (result.is_error()) {
|
2022-08-07 18:47:50 +02:00
|
|
|
pending_new_sticker_set->promise_.set_error(result.move_as_error());
|
2018-12-31 20:04:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-08-07 18:47:50 +02:00
|
|
|
CHECK(pending_new_sticker_set->upload_files_multipromise_.promise_count() == 0);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2022-08-07 18:47:50 +02:00
|
|
|
auto &promise = pending_new_sticker_set->promise_;
|
|
|
|
TRY_RESULT_PROMISE(promise, input_user, td_->contacts_manager_->get_input_user(pending_new_sticker_set->user_id_));
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2022-08-07 18:47:50 +02:00
|
|
|
StickerFormat sticker_format = pending_new_sticker_set->sticker_format_;
|
|
|
|
StickerType sticker_type = pending_new_sticker_set->sticker_type_;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2022-08-07 18:47:50 +02:00
|
|
|
auto sticker_count = pending_new_sticker_set->stickers_.size();
|
2018-12-31 20:04:05 +01:00
|
|
|
vector<tl_object_ptr<telegram_api::inputStickerSetItem>> input_stickers;
|
|
|
|
input_stickers.reserve(sticker_count);
|
|
|
|
for (size_t i = 0; i < sticker_count; i++) {
|
|
|
|
input_stickers.push_back(
|
2022-08-07 18:47:50 +02:00
|
|
|
get_input_sticker(pending_new_sticker_set->stickers_[i].get(), pending_new_sticker_set->file_ids_[i]));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2022-08-08 16:24:01 +02:00
|
|
|
td_->create_handler<CreateNewStickerSetQuery>(std::move(promise))
|
2022-08-07 18:47:50 +02:00
|
|
|
->send(std::move(input_user), pending_new_sticker_set->title_, pending_new_sticker_set->short_name_, sticker_type,
|
|
|
|
sticker_format, std::move(input_stickers), pending_new_sticker_set->software_);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2022-08-08 16:01:04 +02:00
|
|
|
void StickersManager::add_sticker_to_set(UserId user_id, string short_name,
|
|
|
|
tl_object_ptr<td_api::inputSticker> &&sticker,
|
|
|
|
Promise<td_api::object_ptr<td_api::stickerSet>> &&promise) {
|
2021-12-16 20:46:03 +01:00
|
|
|
TRY_RESULT_PROMISE(promise, input_user, td_->contacts_manager_->get_input_user(user_id));
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2022-07-31 02:17:43 +02:00
|
|
|
short_name = clean_username(strip_empty_characters(short_name, MAX_STICKER_SET_SHORT_NAME_LENGTH));
|
2018-12-31 20:04:05 +01:00
|
|
|
if (short_name.empty()) {
|
2022-09-10 21:48:34 +02:00
|
|
|
return promise.set_error(Status::Error(400, "Sticker set name must be non-empty"));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2022-08-03 21:42:52 +02:00
|
|
|
const StickerSet *sticker_set = get_sticker_set(short_name_to_sticker_set_id_.get(short_name));
|
2022-08-07 18:47:50 +02:00
|
|
|
if (sticker_set != nullptr && sticker_set->was_loaded_) {
|
2022-07-21 15:06:58 +02:00
|
|
|
return do_add_sticker_to_set(user_id, short_name, std::move(sticker), std::move(promise));
|
|
|
|
}
|
|
|
|
|
|
|
|
do_reload_sticker_set(
|
|
|
|
StickerSetId(), make_tl_object<telegram_api::inputStickerSetShortName>(short_name), 0,
|
|
|
|
PromiseCreator::lambda([actor_id = actor_id(this), user_id, short_name, sticker = std::move(sticker),
|
|
|
|
promise = std::move(promise)](Result<Unit> result) mutable {
|
|
|
|
if (result.is_error()) {
|
|
|
|
promise.set_error(result.move_as_error());
|
|
|
|
} else {
|
|
|
|
send_closure(actor_id, &StickersManager::do_add_sticker_to_set, user_id, std::move(short_name),
|
|
|
|
std::move(sticker), std::move(promise));
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
void StickersManager::do_add_sticker_to_set(UserId user_id, string short_name,
|
2022-08-08 16:01:04 +02:00
|
|
|
tl_object_ptr<td_api::inputSticker> &&sticker,
|
|
|
|
Promise<td_api::object_ptr<td_api::stickerSet>> &&promise) {
|
2022-07-21 15:06:58 +02:00
|
|
|
TRY_STATUS_PROMISE(promise, G()->close_status());
|
|
|
|
|
2022-08-03 21:42:52 +02:00
|
|
|
const StickerSet *sticker_set = get_sticker_set(short_name_to_sticker_set_id_.get(short_name));
|
2022-08-07 18:47:50 +02:00
|
|
|
if (sticker_set == nullptr || !sticker_set->was_loaded_) {
|
2022-07-21 15:06:58 +02:00
|
|
|
return promise.set_error(Status::Error(400, "Sticker set not found"));
|
|
|
|
}
|
|
|
|
|
2022-08-07 18:47:50 +02:00
|
|
|
auto r_file_id = prepare_input_sticker(sticker.get(), sticker_set->sticker_type_);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (r_file_id.is_error()) {
|
|
|
|
return promise.set_error(r_file_id.move_as_error());
|
|
|
|
}
|
|
|
|
auto file_id = std::get<0>(r_file_id.ok());
|
|
|
|
auto is_url = std::get<1>(r_file_id.ok());
|
|
|
|
auto is_local = std::get<2>(r_file_id.ok());
|
|
|
|
|
|
|
|
auto pending_add_sticker_to_set = make_unique<PendingAddStickerToSet>();
|
2022-08-07 18:47:50 +02:00
|
|
|
pending_add_sticker_to_set->short_name_ = short_name;
|
|
|
|
pending_add_sticker_to_set->file_id_ = file_id;
|
|
|
|
pending_add_sticker_to_set->sticker_ = std::move(sticker);
|
|
|
|
pending_add_sticker_to_set->promise_ = std::move(promise);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
int64 random_id;
|
|
|
|
do {
|
|
|
|
random_id = Random::secure_int64();
|
2022-05-01 22:03:06 +02:00
|
|
|
} while (random_id == 0 || pending_add_sticker_to_sets_.count(random_id) > 0);
|
2018-12-31 20:04:05 +01:00
|
|
|
pending_add_sticker_to_sets_[random_id] = std::move(pending_add_sticker_to_set);
|
|
|
|
|
|
|
|
auto on_upload_promise = PromiseCreator::lambda([random_id](Result<Unit> result) {
|
|
|
|
send_closure(G()->stickers_manager(), &StickersManager::on_added_sticker_uploaded, random_id, std::move(result));
|
|
|
|
});
|
|
|
|
|
|
|
|
if (is_url) {
|
|
|
|
do_upload_sticker_file(user_id, file_id, nullptr, std::move(on_upload_promise));
|
|
|
|
} else if (is_local) {
|
|
|
|
upload_sticker_file(user_id, file_id, std::move(on_upload_promise));
|
|
|
|
} else {
|
|
|
|
on_upload_promise.set_value(Unit());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void StickersManager::on_added_sticker_uploaded(int64 random_id, Result<Unit> result) {
|
2022-08-10 12:57:41 +02:00
|
|
|
if (G()->close_flag()) {
|
|
|
|
result = Global::request_aborted_error();
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
auto it = pending_add_sticker_to_sets_.find(random_id);
|
|
|
|
CHECK(it != pending_add_sticker_to_sets_.end());
|
|
|
|
|
|
|
|
auto pending_add_sticker_to_set = std::move(it->second);
|
|
|
|
CHECK(pending_add_sticker_to_set != nullptr);
|
|
|
|
|
|
|
|
pending_add_sticker_to_sets_.erase(it);
|
|
|
|
|
|
|
|
if (result.is_error()) {
|
2022-08-07 18:47:50 +02:00
|
|
|
pending_add_sticker_to_set->promise_.set_error(result.move_as_error());
|
2018-12-31 20:04:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-08-07 18:47:50 +02:00
|
|
|
td_->create_handler<AddStickerToSetQuery>(std::move(pending_add_sticker_to_set->promise_))
|
|
|
|
->send(pending_add_sticker_to_set->short_name_,
|
|
|
|
get_input_sticker(pending_add_sticker_to_set->sticker_.get(), pending_add_sticker_to_set->file_id_));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2022-08-08 16:31:27 +02:00
|
|
|
void StickersManager::set_sticker_set_thumbnail(UserId user_id, string short_name,
|
|
|
|
tl_object_ptr<td_api::InputFile> &&thumbnail,
|
|
|
|
Promise<td_api::object_ptr<td_api::stickerSet>> &&promise) {
|
2021-12-16 20:46:03 +01:00
|
|
|
TRY_RESULT_PROMISE(promise, input_user, td_->contacts_manager_->get_input_user(user_id));
|
2020-03-20 14:36:07 +01:00
|
|
|
|
2020-03-23 16:03:13 +01:00
|
|
|
short_name = clean_username(strip_empty_characters(short_name, MAX_STICKER_SET_SHORT_NAME_LENGTH));
|
2020-03-20 14:36:07 +01:00
|
|
|
if (short_name.empty()) {
|
2022-09-10 21:48:34 +02:00
|
|
|
return promise.set_error(Status::Error(400, "Sticker set name must be non-empty"));
|
2020-03-20 14:36:07 +01:00
|
|
|
}
|
|
|
|
|
2022-08-03 21:42:52 +02:00
|
|
|
const StickerSet *sticker_set = get_sticker_set(short_name_to_sticker_set_id_.get(short_name));
|
2022-08-07 18:47:50 +02:00
|
|
|
if (sticker_set != nullptr && sticker_set->was_loaded_) {
|
2020-03-23 16:03:13 +01:00
|
|
|
return do_set_sticker_set_thumbnail(user_id, short_name, std::move(thumbnail), std::move(promise));
|
|
|
|
}
|
|
|
|
|
|
|
|
do_reload_sticker_set(
|
2021-11-19 11:56:12 +01:00
|
|
|
StickerSetId(), make_tl_object<telegram_api::inputStickerSetShortName>(short_name), 0,
|
2020-03-23 16:03:13 +01:00
|
|
|
PromiseCreator::lambda([actor_id = actor_id(this), user_id, short_name, thumbnail = std::move(thumbnail),
|
|
|
|
promise = std::move(promise)](Result<Unit> result) mutable {
|
|
|
|
if (result.is_error()) {
|
|
|
|
promise.set_error(result.move_as_error());
|
|
|
|
} else {
|
|
|
|
send_closure(actor_id, &StickersManager::do_set_sticker_set_thumbnail, user_id, std::move(short_name),
|
|
|
|
std::move(thumbnail), std::move(promise));
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
void StickersManager::do_set_sticker_set_thumbnail(UserId user_id, string short_name,
|
|
|
|
tl_object_ptr<td_api::InputFile> &&thumbnail,
|
2022-08-08 16:31:27 +02:00
|
|
|
Promise<td_api::object_ptr<td_api::stickerSet>> &&promise) {
|
2021-10-07 15:36:21 +02:00
|
|
|
TRY_STATUS_PROMISE(promise, G()->close_status());
|
2021-09-28 16:31:00 +02:00
|
|
|
|
2022-08-03 21:42:52 +02:00
|
|
|
const StickerSet *sticker_set = get_sticker_set(short_name_to_sticker_set_id_.get(short_name));
|
2022-08-07 18:47:50 +02:00
|
|
|
if (sticker_set == nullptr || !sticker_set->was_loaded_) {
|
2021-09-24 09:59:51 +02:00
|
|
|
return promise.set_error(Status::Error(400, "Sticker set not found"));
|
2020-03-23 16:03:13 +01:00
|
|
|
}
|
|
|
|
|
2022-08-07 18:47:50 +02:00
|
|
|
auto r_file_id = prepare_input_file(thumbnail, sticker_set->sticker_format_, sticker_set->sticker_type_, true);
|
2020-03-20 14:36:07 +01:00
|
|
|
if (r_file_id.is_error()) {
|
|
|
|
return promise.set_error(r_file_id.move_as_error());
|
|
|
|
}
|
|
|
|
auto file_id = std::get<0>(r_file_id.ok());
|
|
|
|
auto is_url = std::get<1>(r_file_id.ok());
|
|
|
|
auto is_local = std::get<2>(r_file_id.ok());
|
|
|
|
|
|
|
|
if (!file_id.is_valid()) {
|
|
|
|
td_->create_handler<SetStickerSetThumbnailQuery>(std::move(promise))
|
|
|
|
->send(short_name, telegram_api::make_object<telegram_api::inputDocumentEmpty>());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto pending_set_sticker_set_thumbnail = make_unique<PendingSetStickerSetThumbnail>();
|
2022-08-07 18:47:50 +02:00
|
|
|
pending_set_sticker_set_thumbnail->short_name_ = short_name;
|
|
|
|
pending_set_sticker_set_thumbnail->file_id_ = file_id;
|
|
|
|
pending_set_sticker_set_thumbnail->promise_ = std::move(promise);
|
2020-03-20 14:36:07 +01:00
|
|
|
|
|
|
|
int64 random_id;
|
|
|
|
do {
|
|
|
|
random_id = Random::secure_int64();
|
2022-05-01 22:03:06 +02:00
|
|
|
} while (random_id == 0 || pending_set_sticker_set_thumbnails_.count(random_id) > 0);
|
2020-03-20 14:36:07 +01:00
|
|
|
pending_set_sticker_set_thumbnails_[random_id] = std::move(pending_set_sticker_set_thumbnail);
|
|
|
|
|
|
|
|
auto on_upload_promise = PromiseCreator::lambda([random_id](Result<Unit> result) {
|
|
|
|
send_closure(G()->stickers_manager(), &StickersManager::on_sticker_set_thumbnail_uploaded, random_id,
|
|
|
|
std::move(result));
|
|
|
|
});
|
|
|
|
|
|
|
|
if (is_url) {
|
|
|
|
do_upload_sticker_file(user_id, file_id, nullptr, std::move(on_upload_promise));
|
|
|
|
} else if (is_local) {
|
|
|
|
upload_sticker_file(user_id, file_id, std::move(on_upload_promise));
|
|
|
|
} else {
|
|
|
|
on_upload_promise.set_value(Unit());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void StickersManager::on_sticker_set_thumbnail_uploaded(int64 random_id, Result<Unit> result) {
|
2022-08-10 12:57:41 +02:00
|
|
|
if (G()->close_flag()) {
|
|
|
|
result = Global::request_aborted_error();
|
|
|
|
}
|
|
|
|
|
2020-03-20 14:36:07 +01:00
|
|
|
auto it = pending_set_sticker_set_thumbnails_.find(random_id);
|
|
|
|
CHECK(it != pending_set_sticker_set_thumbnails_.end());
|
|
|
|
|
|
|
|
auto pending_set_sticker_set_thumbnail = std::move(it->second);
|
|
|
|
CHECK(pending_set_sticker_set_thumbnail != nullptr);
|
|
|
|
|
|
|
|
pending_set_sticker_set_thumbnails_.erase(it);
|
|
|
|
|
|
|
|
if (result.is_error()) {
|
2022-08-07 18:47:50 +02:00
|
|
|
pending_set_sticker_set_thumbnail->promise_.set_error(result.move_as_error());
|
2020-03-20 14:36:07 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-08-07 18:47:50 +02:00
|
|
|
FileView file_view = td_->file_manager_->get_file_view(pending_set_sticker_set_thumbnail->file_id_);
|
2020-03-20 14:36:07 +01:00
|
|
|
CHECK(file_view.has_remote_location());
|
|
|
|
|
2022-08-07 18:47:50 +02:00
|
|
|
td_->create_handler<SetStickerSetThumbnailQuery>(std::move(pending_set_sticker_set_thumbnail->promise_))
|
|
|
|
->send(pending_set_sticker_set_thumbnail->short_name_, file_view.main_remote_location().as_input_document());
|
2020-03-20 14:36:07 +01:00
|
|
|
}
|
|
|
|
|
2022-02-05 22:16:16 +01:00
|
|
|
string StickersManager::get_sticker_set_short_name(FileId sticker_id) const {
|
|
|
|
string sticker_set_short_name;
|
|
|
|
const Sticker *s = get_sticker(sticker_id);
|
2022-08-07 18:47:50 +02:00
|
|
|
if (s != nullptr && s->set_id_.is_valid()) {
|
|
|
|
const StickerSet *sticker_set = get_sticker_set(s->set_id_);
|
2022-02-05 22:16:16 +01:00
|
|
|
if (sticker_set != nullptr) {
|
2022-08-07 18:47:50 +02:00
|
|
|
return sticker_set->short_name_;
|
2022-04-17 22:07:00 +02:00
|
|
|
} else {
|
2022-08-07 18:47:50 +02:00
|
|
|
return to_string(s->set_id_.get());
|
2022-02-05 22:16:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return string();
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void StickersManager::set_sticker_position_in_set(const tl_object_ptr<td_api::InputFile> &sticker, int32 position,
|
|
|
|
Promise<Unit> &&promise) {
|
|
|
|
if (position < 0) {
|
2021-09-24 09:59:51 +02:00
|
|
|
return promise.set_error(Status::Error(400, "Wrong sticker position specified"));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
auto r_file_id = td_->file_manager_->get_input_file_id(FileType::Sticker, sticker, DialogId(), false, false);
|
|
|
|
if (r_file_id.is_error()) {
|
2021-09-24 09:59:51 +02:00
|
|
|
return promise.set_error(Status::Error(400, r_file_id.error().message())); // TODO do not drop error code
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
auto file_id = r_file_id.move_as_ok();
|
|
|
|
auto file_view = td_->file_manager_->get_file_view(file_id);
|
2019-11-17 20:41:28 +01:00
|
|
|
if (!file_view.has_remote_location() || !file_view.main_remote_location().is_document() ||
|
|
|
|
file_view.main_remote_location().is_web()) {
|
2021-09-24 09:59:51 +02:00
|
|
|
return promise.set_error(Status::Error(400, "Wrong sticker file specified"));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
td_->create_handler<SetStickerPositionQuery>(std::move(promise))
|
2022-02-05 22:16:16 +01:00
|
|
|
->send(get_sticker_set_short_name(file_id), file_view.main_remote_location().as_input_document(), position);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void StickersManager::remove_sticker_from_set(const tl_object_ptr<td_api::InputFile> &sticker,
|
|
|
|
Promise<Unit> &&promise) {
|
|
|
|
auto r_file_id = td_->file_manager_->get_input_file_id(FileType::Sticker, sticker, DialogId(), false, false);
|
|
|
|
if (r_file_id.is_error()) {
|
2021-09-24 09:59:51 +02:00
|
|
|
return promise.set_error(Status::Error(400, r_file_id.error().message())); // TODO do not drop error code
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
auto file_id = r_file_id.move_as_ok();
|
|
|
|
auto file_view = td_->file_manager_->get_file_view(file_id);
|
2019-11-17 20:41:28 +01:00
|
|
|
if (!file_view.has_remote_location() || !file_view.main_remote_location().is_document() ||
|
|
|
|
file_view.main_remote_location().is_web()) {
|
2021-09-24 09:59:51 +02:00
|
|
|
return promise.set_error(Status::Error(400, "Wrong sticker file specified"));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
td_->create_handler<DeleteStickerFromSetQuery>(std::move(promise))
|
2022-02-05 22:16:16 +01:00
|
|
|
->send(get_sticker_set_short_name(file_id), file_view.main_remote_location().as_input_document());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
vector<FileId> StickersManager::get_attached_sticker_file_ids(const vector<int32> &int_file_ids) {
|
|
|
|
vector<FileId> result;
|
|
|
|
|
|
|
|
result.reserve(int_file_ids.size());
|
|
|
|
for (auto int_file_id : int_file_ids) {
|
2018-03-07 00:37:39 +01:00
|
|
|
FileId file_id(int_file_id, 0);
|
2019-01-06 19:26:19 +01:00
|
|
|
const Sticker *s = get_sticker(file_id);
|
|
|
|
if (s == nullptr) {
|
2018-12-31 20:04:05 +01:00
|
|
|
LOG(WARNING) << "Can't find sticker " << file_id;
|
|
|
|
continue;
|
|
|
|
}
|
2022-08-07 18:47:50 +02:00
|
|
|
if (!s->set_id_.is_valid()) {
|
2019-01-06 19:26:19 +01:00
|
|
|
// only stickers from sticker sets can be attached to files
|
|
|
|
continue;
|
|
|
|
}
|
2022-08-07 18:47:50 +02:00
|
|
|
if (s->type_ == StickerType::CustomEmoji) {
|
2022-07-19 13:51:29 +02:00
|
|
|
// custom emoji stickers can't can be attached to files
|
2022-07-18 13:51:37 +02:00
|
|
|
continue;
|
|
|
|
}
|
2019-01-06 19:26:19 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
auto file_view = td_->file_manager_->get_file_view(file_id);
|
|
|
|
CHECK(!file_view.empty());
|
|
|
|
if (!file_view.has_remote_location()) {
|
2019-01-06 19:26:19 +01:00
|
|
|
LOG(ERROR) << "Sticker " << file_id << " has no remote location";
|
2018-12-31 20:04:05 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (file_view.remote_location().is_web()) {
|
2019-01-06 19:26:19 +01:00
|
|
|
LOG(ERROR) << "Sticker " << file_id << " is web";
|
2018-12-31 20:04:05 +01:00
|
|
|
continue;
|
|
|
|
}
|
2018-03-27 15:11:15 +02:00
|
|
|
if (!file_view.remote_location().is_document()) {
|
2019-01-06 19:26:19 +01:00
|
|
|
LOG(ERROR) << "Sticker " << file_id << " is encrypted";
|
2018-03-27 15:11:15 +02:00
|
|
|
continue;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
result.push_back(file_id);
|
|
|
|
|
|
|
|
if (!td_->auth_manager_->is_bot()) {
|
|
|
|
add_recent_sticker_by_id(true, file_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-09-03 11:27:59 +02:00
|
|
|
int64 StickersManager::get_sticker_sets_hash(const vector<StickerSetId> &sticker_set_ids) const {
|
|
|
|
vector<uint64> numbers;
|
2018-12-31 20:04:05 +01:00
|
|
|
numbers.reserve(sticker_set_ids.size());
|
|
|
|
for (auto sticker_set_id : sticker_set_ids) {
|
|
|
|
const StickerSet *sticker_set = get_sticker_set(sticker_set_id);
|
|
|
|
CHECK(sticker_set != nullptr);
|
2022-08-07 18:47:50 +02:00
|
|
|
CHECK(sticker_set->is_inited_);
|
|
|
|
numbers.push_back(sticker_set->hash_);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
return get_vector_hash(numbers);
|
|
|
|
}
|
|
|
|
|
2022-07-17 21:39:22 +02:00
|
|
|
int64 StickersManager::get_featured_sticker_sets_hash(StickerType sticker_type) const {
|
|
|
|
auto type = static_cast<int32>(sticker_type);
|
2021-09-03 11:27:59 +02:00
|
|
|
vector<uint64> numbers;
|
2022-07-17 21:39:22 +02:00
|
|
|
numbers.reserve(featured_sticker_set_ids_[type].size() * 2);
|
|
|
|
for (auto sticker_set_id : featured_sticker_set_ids_[type]) {
|
2018-12-31 20:04:05 +01:00
|
|
|
const StickerSet *sticker_set = get_sticker_set(sticker_set_id);
|
|
|
|
CHECK(sticker_set != nullptr);
|
2022-08-07 18:47:50 +02:00
|
|
|
CHECK(sticker_set->is_inited_);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-09-03 11:27:59 +02:00
|
|
|
numbers.push_back(sticker_set_id.get());
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2022-08-07 18:47:50 +02:00
|
|
|
if (!sticker_set->is_viewed_) {
|
2018-12-31 20:04:05 +01:00
|
|
|
numbers.push_back(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return get_vector_hash(numbers);
|
|
|
|
}
|
|
|
|
|
2019-09-18 05:55:43 +02:00
|
|
|
vector<int64> StickersManager::convert_sticker_set_ids(const vector<StickerSetId> &sticker_set_ids) {
|
|
|
|
return transform(sticker_set_ids, [](StickerSetId sticker_set_id) { return sticker_set_id.get(); });
|
|
|
|
}
|
|
|
|
|
|
|
|
vector<StickerSetId> StickersManager::convert_sticker_set_ids(const vector<int64> &sticker_set_ids) {
|
|
|
|
return transform(sticker_set_ids, [](int64 sticker_set_id) { return StickerSetId(sticker_set_id); });
|
|
|
|
}
|
|
|
|
|
2018-09-20 21:25:09 +02:00
|
|
|
td_api::object_ptr<td_api::updateInstalledStickerSets> StickersManager::get_update_installed_sticker_sets_object(
|
2022-07-14 15:17:19 +02:00
|
|
|
StickerType sticker_type) const {
|
2022-07-24 13:01:58 +02:00
|
|
|
auto type = static_cast<int32>(sticker_type);
|
2019-09-18 05:55:43 +02:00
|
|
|
return td_api::make_object<td_api::updateInstalledStickerSets>(
|
2022-07-24 13:01:58 +02:00
|
|
|
get_sticker_type_object(sticker_type), convert_sticker_set_ids(installed_sticker_set_ids_[type]));
|
2018-09-20 21:25:09 +02:00
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void StickersManager::send_update_installed_sticker_sets(bool from_database) {
|
2022-07-14 15:17:19 +02:00
|
|
|
for (int32 type = 0; type < MAX_STICKER_TYPE; type++) {
|
|
|
|
auto sticker_type = static_cast<StickerType>(type);
|
|
|
|
if (need_update_installed_sticker_sets_[type]) {
|
|
|
|
need_update_installed_sticker_sets_[type] = false;
|
|
|
|
if (are_installed_sticker_sets_loaded_[type]) {
|
|
|
|
installed_sticker_sets_hash_[type] = get_sticker_sets_hash(installed_sticker_set_ids_[type]);
|
|
|
|
send_closure(G()->td(), &Td::send_update, get_update_installed_sticker_sets_object(sticker_type));
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2020-05-16 01:45:26 +02:00
|
|
|
if (G()->parameters().use_file_db && !from_database && !G()->close_flag()) {
|
2022-07-14 15:17:19 +02:00
|
|
|
LOG(INFO) << "Save installed " << sticker_type << " sticker sets to database";
|
|
|
|
StickerSetListLogEvent log_event(installed_sticker_set_ids_[type], false);
|
|
|
|
G()->td_db()->get_sqlite_pmc()->set(PSTRING() << "sss" << type, log_event_store(log_event).as_slice().str(),
|
2018-12-31 20:04:05 +01:00
|
|
|
Auto());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-17 21:39:22 +02:00
|
|
|
size_t StickersManager::get_max_featured_sticker_count(StickerType sticker_type) {
|
|
|
|
switch (sticker_type) {
|
|
|
|
case StickerType::Regular:
|
|
|
|
return 5;
|
|
|
|
case StickerType::Mask:
|
|
|
|
return 5;
|
2022-07-19 13:51:29 +02:00
|
|
|
case StickerType::CustomEmoji:
|
2022-07-17 21:39:22 +02:00
|
|
|
return 16;
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Slice StickersManager::get_featured_sticker_suffix(StickerType sticker_type) {
|
|
|
|
switch (sticker_type) {
|
|
|
|
case StickerType::Regular:
|
|
|
|
return Slice();
|
|
|
|
case StickerType::Mask:
|
|
|
|
return Slice("1");
|
2022-07-19 13:51:29 +02:00
|
|
|
case StickerType::CustomEmoji:
|
2022-07-17 21:39:22 +02:00
|
|
|
return Slice("2");
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return Slice();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-17 18:16:17 +02:00
|
|
|
td_api::object_ptr<td_api::trendingStickerSets> StickersManager::get_trending_sticker_sets_object(
|
2022-07-17 21:39:22 +02:00
|
|
|
StickerType sticker_type, const vector<StickerSetId> &sticker_set_ids) const {
|
|
|
|
auto type = static_cast<int32>(sticker_type);
|
|
|
|
auto total_count = static_cast<int32>(featured_sticker_set_ids_[type].size()) +
|
|
|
|
(old_featured_sticker_set_count_[type] == -1 ? 1 : old_featured_sticker_set_count_[type]);
|
2022-06-17 18:16:17 +02:00
|
|
|
|
|
|
|
vector<tl_object_ptr<td_api::stickerSetInfo>> result;
|
|
|
|
result.reserve(sticker_set_ids.size());
|
|
|
|
for (auto sticker_set_id : sticker_set_ids) {
|
2022-07-17 21:39:22 +02:00
|
|
|
auto sticker_set_info = get_sticker_set_info_object(sticker_set_id, get_max_featured_sticker_count(sticker_type),
|
|
|
|
are_featured_sticker_sets_premium_[type]);
|
2022-06-17 18:16:17 +02:00
|
|
|
if (sticker_set_info->size_ != 0) {
|
|
|
|
result.push_back(std::move(sticker_set_info));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto result_size = narrow_cast<int32>(result.size());
|
|
|
|
CHECK(total_count >= result_size);
|
|
|
|
return td_api::make_object<td_api::trendingStickerSets>(total_count, std::move(result),
|
2022-07-17 21:39:22 +02:00
|
|
|
are_featured_sticker_sets_premium_[type]);
|
2022-06-17 18:16:17 +02:00
|
|
|
}
|
|
|
|
|
2022-07-17 21:39:22 +02:00
|
|
|
td_api::object_ptr<td_api::updateTrendingStickerSets> StickersManager::get_update_trending_sticker_sets_object(
|
|
|
|
StickerType sticker_type) const {
|
|
|
|
auto type = static_cast<int32>(sticker_type);
|
2018-09-20 21:25:09 +02:00
|
|
|
return td_api::make_object<td_api::updateTrendingStickerSets>(
|
2022-07-17 21:39:22 +02:00
|
|
|
get_sticker_type_object(sticker_type),
|
|
|
|
get_trending_sticker_sets_object(sticker_type, featured_sticker_set_ids_[type]));
|
2018-09-20 21:25:09 +02:00
|
|
|
}
|
|
|
|
|
2022-07-17 21:39:22 +02:00
|
|
|
void StickersManager::send_update_featured_sticker_sets(StickerType sticker_type) {
|
|
|
|
auto type = static_cast<int32>(sticker_type);
|
|
|
|
if (need_update_featured_sticker_sets_[type]) {
|
|
|
|
need_update_featured_sticker_sets_[type] = false;
|
|
|
|
featured_sticker_sets_hash_[type] = get_featured_sticker_sets_hash(sticker_type);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2022-07-17 21:39:22 +02:00
|
|
|
send_closure(G()->td(), &Td::send_update, get_update_trending_sticker_sets_object(sticker_type));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void StickersManager::reload_recent_stickers(bool is_attached, bool force) {
|
2019-04-29 23:37:55 +02:00
|
|
|
if (G()->close_flag()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
auto &next_load_time = next_recent_stickers_load_time_[is_attached];
|
2020-11-23 18:33:35 +01:00
|
|
|
if (!td_->auth_manager_->is_bot() && next_load_time >= 0 && (next_load_time < Time::now() || force)) {
|
2019-02-02 11:30:49 +01:00
|
|
|
LOG_IF(INFO, force) << "Reload recent " << (is_attached ? "attached " : "") << "stickers";
|
2020-11-23 18:33:35 +01:00
|
|
|
next_load_time = -1;
|
2019-02-02 11:30:49 +01:00
|
|
|
td_->create_handler<GetRecentStickersQuery>()->send(false, is_attached, recent_stickers_hash_[is_attached]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-02 11:54:40 +01:00
|
|
|
void StickersManager::repair_recent_stickers(bool is_attached, Promise<Unit> &&promise) {
|
2019-02-02 11:30:49 +01:00
|
|
|
if (td_->auth_manager_->is_bot()) {
|
2021-12-02 12:15:37 +01:00
|
|
|
return promise.set_error(Status::Error(400, "Bots have no recent stickers"));
|
2019-02-02 11:30:49 +01:00
|
|
|
}
|
|
|
|
|
2019-02-02 11:54:40 +01:00
|
|
|
repair_recent_stickers_queries_[is_attached].push_back(std::move(promise));
|
|
|
|
if (repair_recent_stickers_queries_[is_attached].size() == 1u) {
|
2019-02-02 11:30:49 +01:00
|
|
|
td_->create_handler<GetRecentStickersQuery>()->send(true, is_attached, 0);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
vector<FileId> StickersManager::get_recent_stickers(bool is_attached, Promise<Unit> &&promise) {
|
|
|
|
if (!are_recent_stickers_loaded_[is_attached]) {
|
|
|
|
load_recent_stickers(is_attached, std::move(promise));
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
reload_recent_stickers(is_attached, false);
|
|
|
|
|
|
|
|
promise.set_value(Unit());
|
|
|
|
return recent_sticker_ids_[is_attached];
|
|
|
|
}
|
|
|
|
|
|
|
|
void StickersManager::load_recent_stickers(bool is_attached, Promise<Unit> &&promise) {
|
|
|
|
if (td_->auth_manager_->is_bot()) {
|
|
|
|
are_recent_stickers_loaded_[is_attached] = true;
|
|
|
|
}
|
|
|
|
if (are_recent_stickers_loaded_[is_attached]) {
|
|
|
|
promise.set_value(Unit());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
load_recent_stickers_queries_[is_attached].push_back(std::move(promise));
|
|
|
|
if (load_recent_stickers_queries_[is_attached].size() == 1u) {
|
|
|
|
if (G()->parameters().use_file_db) {
|
|
|
|
LOG(INFO) << "Trying to load recent " << (is_attached ? "attached " : "") << "stickers from database";
|
|
|
|
G()->td_db()->get_sqlite_pmc()->get(
|
|
|
|
is_attached ? "ssr1" : "ssr0", PromiseCreator::lambda([is_attached](string value) {
|
|
|
|
send_closure(G()->stickers_manager(), &StickersManager::on_load_recent_stickers_from_database, is_attached,
|
|
|
|
std::move(value));
|
|
|
|
}));
|
|
|
|
} else {
|
|
|
|
LOG(INFO) << "Trying to load recent " << (is_attached ? "attached " : "") << "stickers from server";
|
|
|
|
reload_recent_stickers(is_attached, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void StickersManager::on_load_recent_stickers_from_database(bool is_attached, string value) {
|
2020-05-16 01:25:03 +02:00
|
|
|
if (G()->close_flag()) {
|
|
|
|
return;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
if (value.empty()) {
|
|
|
|
LOG(INFO) << "Recent " << (is_attached ? "attached " : "") << "stickers aren't found in database";
|
|
|
|
reload_recent_stickers(is_attached, true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG(INFO) << "Successfully loaded recent " << (is_attached ? "attached " : "") << "stickers list of size "
|
|
|
|
<< value.size() << " from database";
|
|
|
|
|
|
|
|
StickerListLogEvent log_event;
|
2019-05-29 21:24:27 +02:00
|
|
|
auto status = log_event_parse(log_event, value);
|
|
|
|
if (status.is_error()) {
|
|
|
|
// can't happen unless database is broken, but has been seen in the wild
|
|
|
|
LOG(ERROR) << "Can't load recent stickers: " << status << ' ' << format::as_hex_dump<4>(Slice(value));
|
|
|
|
return reload_recent_stickers(is_attached, true);
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
on_load_recent_stickers_finished(is_attached, std::move(log_event.sticker_ids), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void StickersManager::on_load_recent_stickers_finished(bool is_attached, vector<FileId> &&recent_sticker_ids,
|
|
|
|
bool from_database) {
|
2018-03-08 15:17:53 +01:00
|
|
|
if (static_cast<int32>(recent_sticker_ids.size()) > recent_stickers_limit_) {
|
|
|
|
recent_sticker_ids.resize(recent_stickers_limit_);
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
recent_sticker_ids_[is_attached] = std::move(recent_sticker_ids);
|
|
|
|
are_recent_stickers_loaded_[is_attached] = true;
|
2021-10-31 23:35:39 +01:00
|
|
|
send_update_recent_stickers(is_attached, from_database);
|
2022-04-13 16:40:12 +02:00
|
|
|
set_promises(load_recent_stickers_queries_[is_attached]);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2019-02-02 11:54:40 +01:00
|
|
|
void StickersManager::on_get_recent_stickers(bool is_repair, bool is_attached,
|
2018-12-31 20:04:05 +01:00
|
|
|
tl_object_ptr<telegram_api::messages_RecentStickers> &&stickers_ptr) {
|
|
|
|
CHECK(!td_->auth_manager_->is_bot());
|
2019-02-02 11:54:40 +01:00
|
|
|
if (!is_repair) {
|
2019-02-02 11:30:49 +01:00
|
|
|
next_recent_stickers_load_time_[is_attached] = Time::now_cached() + Random::fast(30 * 60, 50 * 60);
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
CHECK(stickers_ptr != nullptr);
|
|
|
|
int32 constructor_id = stickers_ptr->get_id();
|
|
|
|
if (constructor_id == telegram_api::messages_recentStickersNotModified::ID) {
|
2019-02-02 11:54:40 +01:00
|
|
|
if (is_repair) {
|
2019-02-02 11:30:49 +01:00
|
|
|
return on_get_recent_stickers_failed(true, is_attached, Status::Error(500, "Failed to reload recent stickers"));
|
|
|
|
}
|
|
|
|
LOG(INFO) << (is_attached ? "Attached r" : "R") << "ecent stickers are not modified";
|
2018-12-31 20:04:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
CHECK(constructor_id == telegram_api::messages_recentStickers::ID);
|
|
|
|
auto stickers = move_tl_object_as<telegram_api::messages_recentStickers>(stickers_ptr);
|
|
|
|
|
|
|
|
vector<FileId> recent_sticker_ids;
|
|
|
|
recent_sticker_ids.reserve(stickers->stickers_.size());
|
|
|
|
for (auto &document_ptr : stickers->stickers_) {
|
2022-01-18 17:28:32 +01:00
|
|
|
auto sticker_id = on_get_sticker_document(std::move(document_ptr), StickerFormat::Unknown).second;
|
2018-12-31 20:04:05 +01:00
|
|
|
if (!sticker_id.is_valid()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
recent_sticker_ids.push_back(sticker_id);
|
|
|
|
}
|
|
|
|
|
2019-02-02 11:54:40 +01:00
|
|
|
if (is_repair) {
|
2022-04-13 16:40:12 +02:00
|
|
|
set_promises(repair_recent_stickers_queries_[is_attached]);
|
2019-02-02 11:30:49 +01:00
|
|
|
} else {
|
|
|
|
on_load_recent_stickers_finished(is_attached, std::move(recent_sticker_ids));
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2019-02-02 11:30:49 +01:00
|
|
|
LOG_IF(ERROR, recent_stickers_hash_[is_attached] != stickers->hash_) << "Stickers hash mismatch";
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2019-02-02 11:54:40 +01:00
|
|
|
void StickersManager::on_get_recent_stickers_failed(bool is_repair, bool is_attached, Status error) {
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK(error.is_error());
|
2019-02-02 11:54:40 +01:00
|
|
|
if (!is_repair) {
|
2019-02-02 11:30:49 +01:00
|
|
|
next_recent_stickers_load_time_[is_attached] = Time::now_cached() + Random::fast(5, 10);
|
|
|
|
}
|
2022-04-13 16:40:12 +02:00
|
|
|
fail_promises(is_repair ? repair_recent_stickers_queries_[is_attached] : load_recent_stickers_queries_[is_attached],
|
|
|
|
std::move(error));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-09-03 11:27:59 +02:00
|
|
|
int64 StickersManager::get_recent_stickers_hash(const vector<FileId> &sticker_ids) const {
|
|
|
|
vector<uint64> numbers;
|
|
|
|
numbers.reserve(sticker_ids.size());
|
2018-12-31 20:04:05 +01:00
|
|
|
for (auto sticker_id : sticker_ids) {
|
|
|
|
auto sticker = get_sticker(sticker_id);
|
|
|
|
CHECK(sticker != nullptr);
|
2018-03-07 00:02:49 +01:00
|
|
|
auto file_view = td_->file_manager_->get_file_view(sticker_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK(file_view.has_remote_location());
|
2020-03-23 23:12:23 +01:00
|
|
|
if (!file_view.remote_location().is_document()) {
|
|
|
|
LOG(ERROR) << "Recent sticker remote location is not document: " << file_view.remote_location();
|
|
|
|
continue;
|
|
|
|
}
|
2021-09-03 11:27:59 +02:00
|
|
|
numbers.push_back(file_view.remote_location().get_id());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
return get_vector_hash(numbers);
|
|
|
|
}
|
|
|
|
|
2019-04-26 04:49:48 +02:00
|
|
|
FileSourceId StickersManager::get_recent_stickers_file_source_id(int is_attached) {
|
2019-02-02 11:54:40 +01:00
|
|
|
if (!recent_stickers_file_source_id_[is_attached].is_valid()) {
|
|
|
|
recent_stickers_file_source_id_[is_attached] =
|
2019-04-26 05:09:26 +02:00
|
|
|
td_->file_reference_manager_->create_recent_stickers_file_source(is_attached != 0);
|
2019-02-02 11:30:49 +01:00
|
|
|
}
|
2019-02-02 11:54:40 +01:00
|
|
|
return recent_stickers_file_source_id_[is_attached];
|
2019-02-02 11:30:49 +01:00
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void StickersManager::add_recent_sticker(bool is_attached, const tl_object_ptr<td_api::InputFile> &input_file,
|
|
|
|
Promise<Unit> &&promise) {
|
|
|
|
if (!are_recent_stickers_loaded_[is_attached]) {
|
|
|
|
load_recent_stickers(is_attached, std::move(promise));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto r_file_id = td_->file_manager_->get_input_file_id(FileType::Sticker, input_file, DialogId(), false, false);
|
|
|
|
if (r_file_id.is_error()) {
|
2021-09-24 09:59:51 +02:00
|
|
|
return promise.set_error(Status::Error(400, r_file_id.error().message())); // TODO do not drop error code
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2019-11-30 19:22:40 +01:00
|
|
|
add_recent_sticker_impl(is_attached, r_file_id.ok(), true, std::move(promise));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2019-02-02 12:41:30 +01:00
|
|
|
void StickersManager::send_save_recent_sticker_query(bool is_attached, FileId sticker_id, bool unsave,
|
|
|
|
Promise<Unit> &&promise) {
|
2021-10-07 15:36:21 +02:00
|
|
|
TRY_STATUS_PROMISE(promise, G()->close_status());
|
2019-04-08 00:14:09 +02:00
|
|
|
|
2019-02-02 12:41:30 +01:00
|
|
|
// TODO invokeAfter and log event
|
|
|
|
auto file_view = td_->file_manager_->get_file_view(sticker_id);
|
|
|
|
CHECK(file_view.has_remote_location());
|
|
|
|
CHECK(file_view.remote_location().is_document());
|
|
|
|
CHECK(!file_view.remote_location().is_web());
|
|
|
|
td_->create_handler<SaveRecentStickerQuery>(std::move(promise))
|
|
|
|
->send(is_attached, sticker_id, file_view.remote_location().as_input_document(), unsave);
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void StickersManager::add_recent_sticker_by_id(bool is_attached, FileId sticker_id) {
|
|
|
|
// TODO log event
|
2019-11-30 19:22:40 +01:00
|
|
|
add_recent_sticker_impl(is_attached, sticker_id, false, Auto());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2019-11-30 19:22:40 +01:00
|
|
|
void StickersManager::add_recent_sticker_impl(bool is_attached, FileId sticker_id, bool add_on_server,
|
|
|
|
Promise<Unit> &&promise) {
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK(!td_->auth_manager_->is_bot());
|
|
|
|
|
2018-12-12 13:57:24 +01:00
|
|
|
LOG(INFO) << "Add recent " << (is_attached ? "attached " : "") << "sticker " << sticker_id;
|
2018-12-31 20:04:05 +01:00
|
|
|
if (!are_recent_stickers_loaded_[is_attached]) {
|
2019-11-30 19:22:40 +01:00
|
|
|
load_recent_stickers(is_attached, PromiseCreator::lambda([is_attached, sticker_id, add_on_server,
|
2018-12-31 20:04:05 +01:00
|
|
|
promise = std::move(promise)](Result<> result) mutable {
|
|
|
|
if (result.is_ok()) {
|
2019-11-30 19:22:40 +01:00
|
|
|
send_closure(G()->stickers_manager(), &StickersManager::add_recent_sticker_impl,
|
|
|
|
is_attached, sticker_id, add_on_server, std::move(promise));
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
|
|
|
promise.set_error(result.move_as_error());
|
|
|
|
}
|
|
|
|
}));
|
2019-11-30 19:22:40 +01:00
|
|
|
return;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2019-02-16 03:29:28 +01:00
|
|
|
auto is_equal = [sticker_id](FileId file_id) {
|
|
|
|
return file_id == sticker_id || (file_id.get_remote() == sticker_id.get_remote() && sticker_id.get_remote() != 0);
|
|
|
|
};
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
vector<FileId> &sticker_ids = recent_sticker_ids_[is_attached];
|
2019-02-16 03:29:28 +01:00
|
|
|
if (!sticker_ids.empty() && is_equal(sticker_ids[0])) {
|
2018-03-08 19:04:28 +01:00
|
|
|
if (sticker_ids[0].get_remote() == 0 && sticker_id.get_remote() != 0) {
|
|
|
|
sticker_ids[0] = sticker_id;
|
|
|
|
save_recent_stickers_to_database(is_attached);
|
|
|
|
}
|
|
|
|
|
2019-11-30 19:22:40 +01:00
|
|
|
return promise.set_value(Unit());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
auto sticker = get_sticker(sticker_id);
|
|
|
|
if (sticker == nullptr) {
|
2021-09-24 09:59:51 +02:00
|
|
|
return promise.set_error(Status::Error(400, "Sticker not found"));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2022-08-07 18:47:50 +02:00
|
|
|
if (!sticker->set_id_.is_valid()) {
|
2021-09-24 09:59:51 +02:00
|
|
|
return promise.set_error(Status::Error(400, "Stickers without sticker set can't be added to recent"));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2022-08-07 18:47:50 +02:00
|
|
|
if (sticker->type_ == StickerType::CustomEmoji) {
|
2022-07-19 13:51:29 +02:00
|
|
|
return promise.set_error(Status::Error(400, "Custom emoji stickers can't be added to recent"));
|
2022-07-18 13:51:37 +02:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
auto file_view = td_->file_manager_->get_file_view(sticker_id);
|
|
|
|
if (!file_view.has_remote_location()) {
|
2021-09-24 09:59:51 +02:00
|
|
|
return promise.set_error(Status::Error(400, "Can save only sent stickers"));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
if (file_view.remote_location().is_web()) {
|
2021-09-24 09:59:51 +02:00
|
|
|
return promise.set_error(Status::Error(400, "Can't save web stickers"));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-03-27 15:11:15 +02:00
|
|
|
if (!file_view.remote_location().is_document()) {
|
2021-09-24 09:59:51 +02:00
|
|
|
return promise.set_error(Status::Error(400, "Can't save encrypted stickers"));
|
2018-03-27 15:11:15 +02:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2019-02-16 03:29:28 +01:00
|
|
|
auto it = std::find_if(sticker_ids.begin(), sticker_ids.end(), is_equal);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (it == sticker_ids.end()) {
|
2018-03-08 14:49:45 +01:00
|
|
|
if (static_cast<int32>(sticker_ids.size()) == recent_stickers_limit_) {
|
2018-12-31 20:04:05 +01:00
|
|
|
sticker_ids.back() = sticker_id;
|
|
|
|
} else {
|
|
|
|
sticker_ids.push_back(sticker_id);
|
|
|
|
}
|
|
|
|
it = sticker_ids.end() - 1;
|
|
|
|
}
|
|
|
|
std::rotate(sticker_ids.begin(), it, it + 1);
|
2018-03-08 19:04:28 +01:00
|
|
|
if (sticker_ids[0].get_remote() == 0 && sticker_id.get_remote() != 0) {
|
|
|
|
sticker_ids[0] = sticker_id;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-10-31 23:35:39 +01:00
|
|
|
send_update_recent_stickers(is_attached);
|
2019-11-30 19:22:40 +01:00
|
|
|
if (add_on_server) {
|
|
|
|
send_save_recent_sticker_query(is_attached, sticker_id, false, std::move(promise));
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void StickersManager::remove_recent_sticker(bool is_attached, const tl_object_ptr<td_api::InputFile> &input_file,
|
|
|
|
Promise<Unit> &&promise) {
|
|
|
|
if (!are_recent_stickers_loaded_[is_attached]) {
|
|
|
|
load_recent_stickers(is_attached, std::move(promise));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto r_file_id = td_->file_manager_->get_input_file_id(FileType::Sticker, input_file, DialogId(), false, false);
|
|
|
|
if (r_file_id.is_error()) {
|
2021-09-24 09:59:51 +02:00
|
|
|
return promise.set_error(Status::Error(400, r_file_id.error().message())); // TODO do not drop error code
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
vector<FileId> &sticker_ids = recent_sticker_ids_[is_attached];
|
|
|
|
FileId file_id = r_file_id.ok();
|
2019-10-22 01:12:58 +02:00
|
|
|
if (!td::remove(sticker_ids, file_id)) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return promise.set_value(Unit());
|
|
|
|
}
|
|
|
|
|
|
|
|
auto sticker = get_sticker(file_id);
|
|
|
|
if (sticker == nullptr) {
|
2021-09-24 09:59:51 +02:00
|
|
|
return promise.set_error(Status::Error(400, "Sticker not found"));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2019-02-02 12:41:30 +01:00
|
|
|
send_save_recent_sticker_query(is_attached, file_id, true, std::move(promise));
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-10-31 23:35:39 +01:00
|
|
|
send_update_recent_stickers(is_attached);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void StickersManager::clear_recent_stickers(bool is_attached, Promise<Unit> &&promise) {
|
|
|
|
if (!are_recent_stickers_loaded_[is_attached]) {
|
|
|
|
load_recent_stickers(is_attached, std::move(promise));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
vector<FileId> &sticker_ids = recent_sticker_ids_[is_attached];
|
|
|
|
if (sticker_ids.empty()) {
|
|
|
|
return promise.set_value(Unit());
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO invokeAfter
|
|
|
|
td_->create_handler<ClearRecentStickersQuery>(std::move(promise))->send(is_attached);
|
|
|
|
|
|
|
|
sticker_ids.clear();
|
|
|
|
|
2021-10-31 23:35:39 +01:00
|
|
|
send_update_recent_stickers(is_attached);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2018-09-20 21:25:09 +02:00
|
|
|
td_api::object_ptr<td_api::updateRecentStickers> StickersManager::get_update_recent_stickers_object(
|
|
|
|
int is_attached) const {
|
|
|
|
return td_api::make_object<td_api::updateRecentStickers>(
|
2019-01-21 18:19:02 +01:00
|
|
|
is_attached != 0, td_->file_manager_->get_file_ids_object(recent_sticker_ids_[is_attached]));
|
2018-09-20 21:25:09 +02:00
|
|
|
}
|
|
|
|
|
2021-10-31 23:35:39 +01:00
|
|
|
void StickersManager::send_update_recent_stickers(bool is_attached, bool from_database) {
|
|
|
|
if (!are_recent_stickers_loaded_[is_attached]) {
|
|
|
|
return;
|
|
|
|
}
|
2019-02-02 11:30:49 +01:00
|
|
|
|
2021-10-31 23:35:39 +01:00
|
|
|
vector<FileId> new_recent_sticker_file_ids;
|
|
|
|
for (auto &sticker_id : recent_sticker_ids_[is_attached]) {
|
|
|
|
append(new_recent_sticker_file_ids, get_sticker_file_ids(sticker_id));
|
|
|
|
}
|
|
|
|
std::sort(new_recent_sticker_file_ids.begin(), new_recent_sticker_file_ids.end());
|
|
|
|
if (new_recent_sticker_file_ids != recent_sticker_file_ids_[is_attached]) {
|
|
|
|
td_->file_manager_->change_files_source(get_recent_stickers_file_source_id(is_attached),
|
|
|
|
recent_sticker_file_ids_[is_attached], new_recent_sticker_file_ids);
|
|
|
|
recent_sticker_file_ids_[is_attached] = std::move(new_recent_sticker_file_ids);
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-10-31 23:35:39 +01:00
|
|
|
recent_stickers_hash_[is_attached] = get_recent_stickers_hash(recent_sticker_ids_[is_attached]);
|
|
|
|
send_closure(G()->td(), &Td::send_update, get_update_recent_stickers_object(is_attached));
|
|
|
|
|
|
|
|
if (!from_database) {
|
|
|
|
save_recent_stickers_to_database(is_attached != 0);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-08 19:04:28 +01:00
|
|
|
void StickersManager::save_recent_stickers_to_database(bool is_attached) {
|
2020-05-16 01:45:26 +02:00
|
|
|
if (G()->parameters().use_file_db && !G()->close_flag()) {
|
2018-03-08 19:04:28 +01:00
|
|
|
LOG(INFO) << "Save recent " << (is_attached ? "attached " : "") << "stickers to database";
|
|
|
|
StickerListLogEvent log_event(recent_sticker_ids_[is_attached]);
|
|
|
|
G()->td_db()->get_sqlite_pmc()->set(is_attached ? "ssr1" : "ssr0", log_event_store(log_event).as_slice().str(),
|
|
|
|
Auto());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-13 23:18:44 +02:00
|
|
|
void StickersManager::on_update_animated_emoji_zoom() {
|
2022-08-18 16:00:18 +02:00
|
|
|
animated_emoji_zoom_ =
|
|
|
|
static_cast<double>(td_->option_manager_->get_option_integer("animated_emoji_zoom", 625000000)) * 1e-9;
|
2021-10-13 23:18:44 +02:00
|
|
|
}
|
|
|
|
|
2022-08-17 20:40:03 +02:00
|
|
|
void StickersManager::on_update_recent_stickers_limit() {
|
2022-08-18 16:00:18 +02:00
|
|
|
auto recent_stickers_limit =
|
|
|
|
narrow_cast<int32>(td_->option_manager_->get_option_integer("recent_stickers_limit", 200));
|
2018-03-08 14:49:45 +01:00
|
|
|
if (recent_stickers_limit != recent_stickers_limit_) {
|
|
|
|
if (recent_stickers_limit > 0) {
|
|
|
|
LOG(INFO) << "Update recent stickers limit to " << recent_stickers_limit;
|
|
|
|
recent_stickers_limit_ = recent_stickers_limit;
|
|
|
|
for (int is_attached = 0; is_attached < 2; is_attached++) {
|
|
|
|
if (static_cast<int32>(recent_sticker_ids_[is_attached].size()) > recent_stickers_limit) {
|
|
|
|
recent_sticker_ids_[is_attached].resize(recent_stickers_limit);
|
2021-12-28 00:01:20 +01:00
|
|
|
send_update_recent_stickers(is_attached != 0);
|
2018-03-08 14:49:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
LOG(ERROR) << "Receive wrong recent stickers limit = " << recent_stickers_limit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-17 20:40:03 +02:00
|
|
|
void StickersManager::on_update_favorite_stickers_limit() {
|
2022-08-18 16:00:18 +02:00
|
|
|
auto favorite_stickers_limit =
|
|
|
|
narrow_cast<int32>(td_->option_manager_->get_option_integer("favorite_stickers_limit", 5));
|
2018-12-31 20:04:05 +01:00
|
|
|
if (favorite_stickers_limit != favorite_stickers_limit_) {
|
|
|
|
if (favorite_stickers_limit > 0) {
|
|
|
|
LOG(INFO) << "Update favorite stickers limit to " << favorite_stickers_limit;
|
|
|
|
favorite_stickers_limit_ = favorite_stickers_limit;
|
|
|
|
if (static_cast<int32>(favorite_sticker_ids_.size()) > favorite_stickers_limit) {
|
|
|
|
favorite_sticker_ids_.resize(favorite_stickers_limit);
|
|
|
|
send_update_favorite_stickers();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
LOG(ERROR) << "Receive wrong favorite stickers limit = " << favorite_stickers_limit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void StickersManager::reload_favorite_stickers(bool force) {
|
2019-04-29 23:37:55 +02:00
|
|
|
if (G()->close_flag()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-11-21 14:34:39 +01:00
|
|
|
auto &next_load_time = next_favorite_stickers_load_time_;
|
2020-11-23 18:33:35 +01:00
|
|
|
if (!td_->auth_manager_->is_bot() && next_load_time >= 0 && (next_load_time < Time::now() || force)) {
|
2018-12-31 20:04:05 +01:00
|
|
|
LOG_IF(INFO, force) << "Reload favorite stickers";
|
2020-11-23 18:33:35 +01:00
|
|
|
next_load_time = -1;
|
2019-02-02 11:54:40 +01:00
|
|
|
td_->create_handler<GetFavedStickersQuery>()->send(false, get_favorite_stickers_hash());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void StickersManager::repair_favorite_stickers(Promise<Unit> &&promise) {
|
|
|
|
if (td_->auth_manager_->is_bot()) {
|
2021-12-02 12:15:37 +01:00
|
|
|
return promise.set_error(Status::Error(400, "Bots have no favorite stickers"));
|
2019-02-02 11:54:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
repair_favorite_stickers_queries_.push_back(std::move(promise));
|
|
|
|
if (repair_favorite_stickers_queries_.size() == 1u) {
|
|
|
|
td_->create_handler<GetFavedStickersQuery>()->send(true, 0);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
vector<FileId> StickersManager::get_favorite_stickers(Promise<Unit> &&promise) {
|
|
|
|
if (!are_favorite_stickers_loaded_) {
|
|
|
|
load_favorite_stickers(std::move(promise));
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
reload_favorite_stickers(false);
|
|
|
|
|
|
|
|
promise.set_value(Unit());
|
|
|
|
return favorite_sticker_ids_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void StickersManager::load_favorite_stickers(Promise<Unit> &&promise) {
|
|
|
|
if (td_->auth_manager_->is_bot()) {
|
|
|
|
are_favorite_stickers_loaded_ = true;
|
|
|
|
}
|
|
|
|
if (are_favorite_stickers_loaded_) {
|
|
|
|
promise.set_value(Unit());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
load_favorite_stickers_queries_.push_back(std::move(promise));
|
|
|
|
if (load_favorite_stickers_queries_.size() == 1u) {
|
|
|
|
if (G()->parameters().use_file_db) {
|
|
|
|
LOG(INFO) << "Trying to load favorite stickers from database";
|
|
|
|
G()->td_db()->get_sqlite_pmc()->get("ssfav", PromiseCreator::lambda([](string value) {
|
|
|
|
send_closure(G()->stickers_manager(),
|
|
|
|
&StickersManager::on_load_favorite_stickers_from_database,
|
|
|
|
std::move(value));
|
|
|
|
}));
|
|
|
|
} else {
|
|
|
|
LOG(INFO) << "Trying to load favorite stickers from server";
|
|
|
|
reload_favorite_stickers(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void StickersManager::on_load_favorite_stickers_from_database(const string &value) {
|
2020-05-16 01:25:03 +02:00
|
|
|
if (G()->close_flag()) {
|
|
|
|
return;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
if (value.empty()) {
|
|
|
|
LOG(INFO) << "Favorite stickers aren't found in database";
|
|
|
|
reload_favorite_stickers(true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG(INFO) << "Successfully loaded favorite stickers list of size " << value.size() << " from database";
|
|
|
|
|
|
|
|
StickerListLogEvent log_event;
|
2019-05-29 21:24:27 +02:00
|
|
|
auto status = log_event_parse(log_event, value);
|
|
|
|
if (status.is_error()) {
|
|
|
|
// can't happen unless database is broken, but has been seen in the wild
|
|
|
|
LOG(ERROR) << "Can't load favorite stickers: " << status << ' ' << format::as_hex_dump<4>(Slice(value));
|
|
|
|
return reload_favorite_stickers(true);
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
on_load_favorite_stickers_finished(std::move(log_event.sticker_ids), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void StickersManager::on_load_favorite_stickers_finished(vector<FileId> &&favorite_sticker_ids, bool from_database) {
|
2018-03-08 15:17:53 +01:00
|
|
|
if (static_cast<int32>(favorite_sticker_ids.size()) > favorite_stickers_limit_) {
|
|
|
|
favorite_sticker_ids.resize(favorite_stickers_limit_);
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
favorite_sticker_ids_ = std::move(favorite_sticker_ids);
|
|
|
|
are_favorite_stickers_loaded_ = true;
|
|
|
|
send_update_favorite_stickers(from_database);
|
2022-04-13 16:40:12 +02:00
|
|
|
set_promises(load_favorite_stickers_queries_);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void StickersManager::on_get_favorite_stickers(
|
2019-02-02 11:54:40 +01:00
|
|
|
bool is_repair, tl_object_ptr<telegram_api::messages_FavedStickers> &&favorite_stickers_ptr) {
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK(!td_->auth_manager_->is_bot());
|
2019-02-02 11:54:40 +01:00
|
|
|
if (!is_repair) {
|
|
|
|
next_favorite_stickers_load_time_ = Time::now_cached() + Random::fast(30 * 60, 50 * 60);
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
CHECK(favorite_stickers_ptr != nullptr);
|
|
|
|
int32 constructor_id = favorite_stickers_ptr->get_id();
|
|
|
|
if (constructor_id == telegram_api::messages_favedStickersNotModified::ID) {
|
2019-02-02 11:54:40 +01:00
|
|
|
if (is_repair) {
|
|
|
|
return on_get_favorite_stickers_failed(true, Status::Error(500, "Failed to reload favorite stickers"));
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
LOG(INFO) << "Favorite stickers are not modified";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
CHECK(constructor_id == telegram_api::messages_favedStickers::ID);
|
|
|
|
auto favorite_stickers = move_tl_object_as<telegram_api::messages_favedStickers>(favorite_stickers_ptr);
|
|
|
|
|
|
|
|
// TODO use favorite_stickers->packs_
|
|
|
|
|
|
|
|
vector<FileId> favorite_sticker_ids;
|
|
|
|
favorite_sticker_ids.reserve(favorite_stickers->stickers_.size());
|
|
|
|
for (auto &document_ptr : favorite_stickers->stickers_) {
|
2022-01-18 17:28:32 +01:00
|
|
|
auto sticker_id = on_get_sticker_document(std::move(document_ptr), StickerFormat::Unknown).second;
|
2018-12-31 20:04:05 +01:00
|
|
|
if (!sticker_id.is_valid()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
favorite_sticker_ids.push_back(sticker_id);
|
|
|
|
}
|
|
|
|
|
2019-02-02 11:54:40 +01:00
|
|
|
if (is_repair) {
|
2022-04-13 16:40:12 +02:00
|
|
|
set_promises(repair_favorite_stickers_queries_);
|
2019-02-02 11:54:40 +01:00
|
|
|
} else {
|
|
|
|
on_load_favorite_stickers_finished(std::move(favorite_sticker_ids));
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2019-02-02 11:54:40 +01:00
|
|
|
LOG_IF(ERROR, get_favorite_stickers_hash() != favorite_stickers->hash_) << "Favorite stickers hash mismatch";
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2019-02-02 11:54:40 +01:00
|
|
|
void StickersManager::on_get_favorite_stickers_failed(bool is_repair, Status error) {
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK(error.is_error());
|
2019-02-02 11:54:40 +01:00
|
|
|
if (!is_repair) {
|
|
|
|
next_favorite_stickers_load_time_ = Time::now_cached() + Random::fast(5, 10);
|
|
|
|
}
|
2022-04-13 16:40:12 +02:00
|
|
|
fail_promises(is_repair ? repair_favorite_stickers_queries_ : load_favorite_stickers_queries_, std::move(error));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-09-03 11:27:59 +02:00
|
|
|
int64 StickersManager::get_favorite_stickers_hash() const {
|
2018-12-31 20:04:05 +01:00
|
|
|
return get_recent_stickers_hash(favorite_sticker_ids_);
|
|
|
|
}
|
|
|
|
|
2021-10-08 12:41:22 +02:00
|
|
|
FileSourceId StickersManager::get_app_config_file_source_id() {
|
|
|
|
if (!app_config_file_source_id_.is_valid()) {
|
|
|
|
app_config_file_source_id_ = td_->file_reference_manager_->create_app_config_file_source();
|
|
|
|
}
|
|
|
|
return app_config_file_source_id_;
|
|
|
|
}
|
|
|
|
|
2019-02-02 12:14:20 +01:00
|
|
|
FileSourceId StickersManager::get_favorite_stickers_file_source_id() {
|
|
|
|
if (!favorite_stickers_file_source_id_.is_valid()) {
|
|
|
|
favorite_stickers_file_source_id_ = td_->file_reference_manager_->create_favorite_stickers_file_source();
|
|
|
|
}
|
|
|
|
return favorite_stickers_file_source_id_;
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void StickersManager::add_favorite_sticker(const tl_object_ptr<td_api::InputFile> &input_file,
|
|
|
|
Promise<Unit> &&promise) {
|
|
|
|
if (!are_favorite_stickers_loaded_) {
|
|
|
|
load_favorite_stickers(std::move(promise));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto r_file_id = td_->file_manager_->get_input_file_id(FileType::Sticker, input_file, DialogId(), false, false);
|
|
|
|
if (r_file_id.is_error()) {
|
2021-09-24 09:59:51 +02:00
|
|
|
return promise.set_error(Status::Error(400, r_file_id.error().message())); // TODO do not drop error code
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2019-11-30 19:22:40 +01:00
|
|
|
add_favorite_sticker_impl(r_file_id.ok(), true, std::move(promise));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2019-02-02 12:14:20 +01:00
|
|
|
void StickersManager::send_fave_sticker_query(FileId sticker_id, bool unsave, Promise<Unit> &&promise) {
|
2021-10-07 15:36:21 +02:00
|
|
|
TRY_STATUS_PROMISE(promise, G()->close_status());
|
2019-04-08 16:57:41 +02:00
|
|
|
|
2019-02-02 12:14:20 +01:00
|
|
|
// TODO invokeAfter and log event
|
|
|
|
auto file_view = td_->file_manager_->get_file_view(sticker_id);
|
|
|
|
CHECK(file_view.has_remote_location());
|
|
|
|
CHECK(file_view.remote_location().is_document());
|
|
|
|
CHECK(!file_view.remote_location().is_web());
|
|
|
|
td_->create_handler<FaveStickerQuery>(std::move(promise))
|
|
|
|
->send(sticker_id, file_view.remote_location().as_input_document(), unsave);
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void StickersManager::add_favorite_sticker_by_id(FileId sticker_id) {
|
|
|
|
// TODO log event
|
2019-11-30 19:22:40 +01:00
|
|
|
add_favorite_sticker_impl(sticker_id, false, Auto());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2019-11-30 19:22:40 +01:00
|
|
|
void StickersManager::add_favorite_sticker_impl(FileId sticker_id, bool add_on_server, Promise<Unit> &&promise) {
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK(!td_->auth_manager_->is_bot());
|
|
|
|
|
|
|
|
if (!are_favorite_stickers_loaded_) {
|
2019-11-30 19:22:40 +01:00
|
|
|
load_favorite_stickers(
|
|
|
|
PromiseCreator::lambda([sticker_id, add_on_server, promise = std::move(promise)](Result<> result) mutable {
|
|
|
|
if (result.is_ok()) {
|
|
|
|
send_closure(G()->stickers_manager(), &StickersManager::add_favorite_sticker_impl, sticker_id,
|
|
|
|
add_on_server, std::move(promise));
|
|
|
|
} else {
|
|
|
|
promise.set_error(result.move_as_error());
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
return;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2019-02-16 03:29:28 +01:00
|
|
|
auto is_equal = [sticker_id](FileId file_id) {
|
|
|
|
return file_id == sticker_id || (file_id.get_remote() == sticker_id.get_remote() && sticker_id.get_remote() != 0);
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!favorite_sticker_ids_.empty() && is_equal(favorite_sticker_ids_[0])) {
|
2018-03-08 19:04:28 +01:00
|
|
|
if (favorite_sticker_ids_[0].get_remote() == 0 && sticker_id.get_remote() != 0) {
|
|
|
|
favorite_sticker_ids_[0] = sticker_id;
|
|
|
|
save_favorite_stickers_to_database();
|
|
|
|
}
|
|
|
|
|
2019-11-30 19:22:40 +01:00
|
|
|
return promise.set_value(Unit());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
auto sticker = get_sticker(sticker_id);
|
|
|
|
if (sticker == nullptr) {
|
2021-09-24 09:59:51 +02:00
|
|
|
return promise.set_error(Status::Error(400, "Sticker not found"));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2022-08-07 18:47:50 +02:00
|
|
|
if (!sticker->set_id_.is_valid()) {
|
2022-07-18 13:51:37 +02:00
|
|
|
return promise.set_error(Status::Error(400, "Stickers without sticker set can't be added to favorite"));
|
|
|
|
}
|
2022-08-07 18:47:50 +02:00
|
|
|
if (sticker->type_ == StickerType::CustomEmoji) {
|
2022-07-19 13:51:29 +02:00
|
|
|
return promise.set_error(Status::Error(400, "Custom emoji stickers can't be added to favorite"));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
auto file_view = td_->file_manager_->get_file_view(sticker_id);
|
|
|
|
if (!file_view.has_remote_location()) {
|
2021-09-24 09:59:51 +02:00
|
|
|
return promise.set_error(Status::Error(400, "Can add to favorites only sent stickers"));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-03-27 15:11:15 +02:00
|
|
|
if (file_view.remote_location().is_web()) {
|
2021-09-24 09:59:51 +02:00
|
|
|
return promise.set_error(Status::Error(400, "Can't add to favorites web stickers"));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-03-27 15:11:15 +02:00
|
|
|
if (!file_view.remote_location().is_document()) {
|
2021-09-24 09:59:51 +02:00
|
|
|
return promise.set_error(Status::Error(400, "Can't add to favorites encrypted stickers"));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2019-02-16 03:29:28 +01:00
|
|
|
auto it = std::find_if(favorite_sticker_ids_.begin(), favorite_sticker_ids_.end(), is_equal);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (it == favorite_sticker_ids_.end()) {
|
|
|
|
if (static_cast<int32>(favorite_sticker_ids_.size()) == favorite_stickers_limit_) {
|
|
|
|
favorite_sticker_ids_.back() = sticker_id;
|
|
|
|
} else {
|
|
|
|
favorite_sticker_ids_.push_back(sticker_id);
|
|
|
|
}
|
|
|
|
it = favorite_sticker_ids_.end() - 1;
|
|
|
|
}
|
|
|
|
std::rotate(favorite_sticker_ids_.begin(), it, it + 1);
|
2018-03-08 19:04:28 +01:00
|
|
|
if (favorite_sticker_ids_[0].get_remote() == 0 && sticker_id.get_remote() != 0) {
|
|
|
|
favorite_sticker_ids_[0] = sticker_id;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
send_update_favorite_stickers();
|
2019-11-30 19:22:40 +01:00
|
|
|
if (add_on_server) {
|
|
|
|
send_fave_sticker_query(sticker_id, false, std::move(promise));
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void StickersManager::remove_favorite_sticker(const tl_object_ptr<td_api::InputFile> &input_file,
|
|
|
|
Promise<Unit> &&promise) {
|
|
|
|
if (!are_favorite_stickers_loaded_) {
|
|
|
|
load_favorite_stickers(std::move(promise));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto r_file_id = td_->file_manager_->get_input_file_id(FileType::Sticker, input_file, DialogId(), false, false);
|
|
|
|
if (r_file_id.is_error()) {
|
2021-09-24 09:59:51 +02:00
|
|
|
return promise.set_error(Status::Error(400, r_file_id.error().message())); // TODO do not drop error code
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
FileId file_id = r_file_id.ok();
|
2022-07-03 11:17:04 +02:00
|
|
|
auto is_equal = [sticker_id = file_id](FileId file_id) {
|
|
|
|
return file_id == sticker_id || (file_id.get_remote() == sticker_id.get_remote() && sticker_id.get_remote() != 0);
|
|
|
|
};
|
|
|
|
if (!td::remove_if(favorite_sticker_ids_, is_equal)) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return promise.set_value(Unit());
|
|
|
|
}
|
|
|
|
|
|
|
|
auto sticker = get_sticker(file_id);
|
|
|
|
if (sticker == nullptr) {
|
2021-09-24 09:59:51 +02:00
|
|
|
return promise.set_error(Status::Error(400, "Sticker not found"));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2019-02-02 12:14:20 +01:00
|
|
|
send_fave_sticker_query(file_id, true, std::move(promise));
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
send_update_favorite_stickers();
|
|
|
|
}
|
|
|
|
|
2018-09-20 21:25:09 +02:00
|
|
|
td_api::object_ptr<td_api::updateFavoriteStickers> StickersManager::get_update_favorite_stickers_object() const {
|
2019-01-21 18:19:02 +01:00
|
|
|
return td_api::make_object<td_api::updateFavoriteStickers>(
|
|
|
|
td_->file_manager_->get_file_ids_object(favorite_sticker_ids_));
|
2018-09-20 21:25:09 +02:00
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void StickersManager::send_update_favorite_stickers(bool from_database) {
|
|
|
|
if (are_favorite_stickers_loaded_) {
|
2019-02-02 11:54:40 +01:00
|
|
|
vector<FileId> new_favorite_sticker_file_ids;
|
|
|
|
for (auto &sticker_id : favorite_sticker_ids_) {
|
|
|
|
append(new_favorite_sticker_file_ids, get_sticker_file_ids(sticker_id));
|
|
|
|
}
|
|
|
|
std::sort(new_favorite_sticker_file_ids.begin(), new_favorite_sticker_file_ids.end());
|
|
|
|
if (new_favorite_sticker_file_ids != favorite_sticker_file_ids_) {
|
|
|
|
td_->file_manager_->change_files_source(get_favorite_stickers_file_source_id(), favorite_sticker_file_ids_,
|
|
|
|
new_favorite_sticker_file_ids);
|
|
|
|
favorite_sticker_file_ids_ = std::move(new_favorite_sticker_file_ids);
|
|
|
|
}
|
|
|
|
|
2018-09-20 21:25:09 +02:00
|
|
|
send_closure(G()->td(), &Td::send_update, get_update_favorite_stickers_object());
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-03-08 19:04:28 +01:00
|
|
|
if (!from_database) {
|
|
|
|
save_favorite_stickers_to_database();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-08 19:04:28 +01:00
|
|
|
void StickersManager::save_favorite_stickers_to_database() {
|
2020-05-16 01:45:26 +02:00
|
|
|
if (G()->parameters().use_file_db && !G()->close_flag()) {
|
2018-03-08 19:04:28 +01:00
|
|
|
LOG(INFO) << "Save favorite stickers to database";
|
|
|
|
StickerListLogEvent log_event(favorite_sticker_ids_);
|
|
|
|
G()->td_db()->get_sqlite_pmc()->set("ssfav", log_event_store(log_event).as_slice().str(), Auto());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
vector<string> StickersManager::get_sticker_emojis(const tl_object_ptr<td_api::InputFile> &input_file,
|
|
|
|
Promise<Unit> &&promise) {
|
|
|
|
auto r_file_id = td_->file_manager_->get_input_file_id(FileType::Sticker, input_file, DialogId(), false, false);
|
|
|
|
if (r_file_id.is_error()) {
|
2021-09-24 09:59:51 +02:00
|
|
|
promise.set_error(Status::Error(400, r_file_id.error().message())); // TODO do not drop error code
|
2018-12-31 20:04:05 +01:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
FileId file_id = r_file_id.ok();
|
|
|
|
|
|
|
|
auto sticker = get_sticker(file_id);
|
|
|
|
if (sticker == nullptr) {
|
|
|
|
promise.set_value(Unit());
|
|
|
|
return {};
|
|
|
|
}
|
2022-08-07 18:47:50 +02:00
|
|
|
if (!sticker->set_id_.is_valid()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
promise.set_value(Unit());
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
auto file_view = td_->file_manager_->get_file_view(file_id);
|
|
|
|
if (!file_view.has_remote_location()) {
|
|
|
|
promise.set_value(Unit());
|
|
|
|
return {};
|
|
|
|
}
|
2018-03-27 15:11:15 +02:00
|
|
|
if (!file_view.remote_location().is_document()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
promise.set_value(Unit());
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
if (file_view.remote_location().is_web()) {
|
|
|
|
promise.set_value(Unit());
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-08-07 18:47:50 +02:00
|
|
|
const StickerSet *sticker_set = get_sticker_set(sticker->set_id_);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (update_sticker_set_cache(sticker_set, promise)) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
promise.set_value(Unit());
|
|
|
|
auto it = sticker_set->sticker_emojis_map_.find(file_id);
|
|
|
|
if (it == sticker_set->sticker_emojis_map_.end()) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
2019-05-21 17:48:35 +02:00
|
|
|
string StickersManager::get_emoji_language_code_version_database_key(const string &language_code) {
|
|
|
|
return PSTRING() << "emojiv$" << language_code;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32 StickersManager::get_emoji_language_code_version(const string &language_code) {
|
|
|
|
auto it = emoji_language_code_versions_.find(language_code);
|
|
|
|
if (it != emoji_language_code_versions_.end()) {
|
|
|
|
return it->second;
|
|
|
|
}
|
2022-02-10 16:03:03 +01:00
|
|
|
if (language_code.empty()) {
|
|
|
|
return 0;
|
|
|
|
}
|
2019-05-21 17:48:35 +02:00
|
|
|
auto &result = emoji_language_code_versions_[language_code];
|
|
|
|
result = to_integer<int32>(
|
|
|
|
G()->td_db()->get_sqlite_sync_pmc()->get(get_emoji_language_code_version_database_key(language_code)));
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-05-21 19:42:41 +02:00
|
|
|
string StickersManager::get_emoji_language_code_last_difference_time_database_key(const string &language_code) {
|
|
|
|
return PSTRING() << "emojid$" << language_code;
|
|
|
|
}
|
|
|
|
|
|
|
|
double StickersManager::get_emoji_language_code_last_difference_time(const string &language_code) {
|
|
|
|
auto it = emoji_language_code_last_difference_times_.find(language_code);
|
|
|
|
if (it != emoji_language_code_last_difference_times_.end()) {
|
|
|
|
return it->second;
|
|
|
|
}
|
2022-02-10 16:03:03 +01:00
|
|
|
if (language_code.empty()) {
|
|
|
|
return Time::now_cached() - G()->unix_time();
|
|
|
|
}
|
2019-05-21 19:42:41 +02:00
|
|
|
auto &result = emoji_language_code_last_difference_times_[language_code];
|
2021-10-20 00:27:02 +02:00
|
|
|
auto old_unix_time = to_integer<int32>(G()->td_db()->get_sqlite_sync_pmc()->get(
|
2019-05-21 19:42:41 +02:00
|
|
|
get_emoji_language_code_last_difference_time_database_key(language_code)));
|
|
|
|
int32 passed_time = max(static_cast<int32>(0), G()->unix_time() - old_unix_time);
|
|
|
|
result = Time::now_cached() - passed_time;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-05-21 17:48:35 +02:00
|
|
|
string StickersManager::get_language_emojis_database_key(const string &language_code, const string &text) {
|
|
|
|
return PSTRING() << "emoji$" << language_code << '$' << text;
|
|
|
|
}
|
|
|
|
|
|
|
|
vector<string> StickersManager::search_language_emojis(const string &language_code, const string &text,
|
2021-10-21 11:51:16 +02:00
|
|
|
bool exact_match) {
|
2019-05-21 17:48:35 +02:00
|
|
|
LOG(INFO) << "Search for \"" << text << "\" in language " << language_code;
|
|
|
|
auto key = get_language_emojis_database_key(language_code, text);
|
|
|
|
if (exact_match) {
|
|
|
|
string emojis = G()->td_db()->get_sqlite_sync_pmc()->get(key);
|
|
|
|
return full_split(emojis, '$');
|
|
|
|
} else {
|
|
|
|
vector<string> result;
|
|
|
|
G()->td_db()->get_sqlite_sync_pmc()->get_by_prefix(key, [&result](Slice key, Slice value) {
|
|
|
|
for (auto &emoji : full_split(value, '$')) {
|
|
|
|
result.push_back(emoji.str());
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
string StickersManager::get_emoji_language_codes_database_key(const vector<string> &language_codes) {
|
|
|
|
return PSTRING() << "emojilc$" << implode(language_codes, '$');
|
|
|
|
}
|
|
|
|
|
|
|
|
void StickersManager::load_language_codes(vector<string> language_codes, string key, Promise<Unit> &&promise) {
|
|
|
|
auto &promises = load_language_codes_queries_[key];
|
|
|
|
promises.push_back(std::move(promise));
|
|
|
|
if (promises.size() != 1) {
|
|
|
|
// query has already been sent, just wait for the result
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto query_promise =
|
|
|
|
PromiseCreator::lambda([actor_id = actor_id(this), key = std::move(key)](Result<vector<string>> &&result) {
|
|
|
|
send_closure(actor_id, &StickersManager::on_get_language_codes, key, std::move(result));
|
|
|
|
});
|
|
|
|
td_->create_handler<GetEmojiKeywordsLanguageQuery>(std::move(query_promise))->send(std::move(language_codes));
|
|
|
|
}
|
|
|
|
|
|
|
|
void StickersManager::on_get_language_codes(const string &key, Result<vector<string>> &&result) {
|
|
|
|
auto queries_it = load_language_codes_queries_.find(key);
|
|
|
|
CHECK(queries_it != load_language_codes_queries_.end());
|
|
|
|
CHECK(!queries_it->second.empty());
|
|
|
|
auto promises = std::move(queries_it->second);
|
|
|
|
load_language_codes_queries_.erase(queries_it);
|
|
|
|
|
|
|
|
if (result.is_error()) {
|
2020-03-30 23:35:58 +02:00
|
|
|
if (!G()->is_expected_error(result.error())) {
|
2019-06-12 15:05:22 +02:00
|
|
|
LOG(ERROR) << "Receive " << result.error() << " from GetEmojiKeywordsLanguageQuery";
|
2019-05-21 17:48:35 +02:00
|
|
|
}
|
2022-04-13 16:40:12 +02:00
|
|
|
fail_promises(promises, result.move_as_error());
|
2019-05-21 17:48:35 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto language_codes = result.move_as_ok();
|
2020-01-08 17:25:36 +01:00
|
|
|
LOG(INFO) << "Receive language codes " << language_codes << " for emojis search with key " << key;
|
|
|
|
td::remove_if(language_codes, [](const string &language_code) {
|
2019-10-21 15:25:56 +02:00
|
|
|
if (language_code.empty() || language_code.find('$') != string::npos) {
|
|
|
|
LOG(ERROR) << "Receive language_code \"" << language_code << '"';
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
2019-05-21 17:48:35 +02:00
|
|
|
if (language_codes.empty()) {
|
|
|
|
LOG(ERROR) << "Language codes list is empty";
|
|
|
|
language_codes.emplace_back("en");
|
|
|
|
}
|
2020-12-30 16:50:57 +01:00
|
|
|
td::unique(language_codes);
|
2019-05-21 17:48:35 +02:00
|
|
|
|
|
|
|
auto it = emoji_language_codes_.find(key);
|
|
|
|
CHECK(it != emoji_language_codes_.end());
|
2020-01-08 17:25:36 +01:00
|
|
|
if (it->second != language_codes) {
|
|
|
|
LOG(INFO) << "Update emoji language codes for " << key << " to " << language_codes;
|
2020-05-16 01:45:26 +02:00
|
|
|
if (!G()->close_flag()) {
|
|
|
|
CHECK(G()->parameters().use_file_db);
|
|
|
|
G()->td_db()->get_sqlite_pmc()->set(key, implode(language_codes, '$'), Auto());
|
|
|
|
}
|
2020-01-08 17:25:36 +01:00
|
|
|
it->second = std::move(language_codes);
|
|
|
|
}
|
2019-05-21 17:48:35 +02:00
|
|
|
|
2022-04-13 16:40:12 +02:00
|
|
|
set_promises(promises);
|
2019-05-21 17:48:35 +02:00
|
|
|
}
|
|
|
|
|
2020-04-22 14:17:53 +02:00
|
|
|
vector<string> StickersManager::get_emoji_language_codes(const vector<string> &input_language_codes, Slice text,
|
2020-04-22 14:02:56 +02:00
|
|
|
Promise<Unit> &promise) {
|
2022-10-01 10:29:09 +02:00
|
|
|
vector<string> language_codes = td_->language_pack_manager_.get_actor_unsafe()->get_used_language_codes();
|
2019-05-21 17:48:35 +02:00
|
|
|
auto system_language_code = G()->mtproto_header().get_system_language_code();
|
2020-10-01 16:21:26 +02:00
|
|
|
if (system_language_code.size() >= 2 && system_language_code.find('$') == string::npos &&
|
|
|
|
(system_language_code.size() == 2 || system_language_code[2] == '-')) {
|
|
|
|
language_codes.push_back(system_language_code.substr(0, 2));
|
2019-05-21 17:48:35 +02:00
|
|
|
}
|
2020-04-22 14:17:53 +02:00
|
|
|
for (auto &input_language_code : input_language_codes) {
|
2020-10-01 16:21:26 +02:00
|
|
|
if (input_language_code.size() >= 2 && input_language_code.find('$') == string::npos &&
|
|
|
|
(input_language_code.size() == 2 || input_language_code[2] == '-')) {
|
|
|
|
language_codes.push_back(input_language_code.substr(0, 2));
|
2020-04-22 14:17:53 +02:00
|
|
|
}
|
2020-01-08 17:07:40 +01:00
|
|
|
}
|
2020-04-22 14:02:56 +02:00
|
|
|
if (!text.empty()) {
|
|
|
|
uint32 code = 0;
|
2022-08-19 15:37:44 +02:00
|
|
|
next_utf8_unsafe(text.ubegin(), &code);
|
2020-04-22 14:02:56 +02:00
|
|
|
if ((0x410 <= code && code <= 0x44F) || code == 0x401 || code == 0x451) {
|
|
|
|
// the first letter is cyrillic
|
|
|
|
if (!td::contains(language_codes, "ru") && !td::contains(language_codes, "uk") &&
|
|
|
|
!td::contains(language_codes, "bg") && !td::contains(language_codes, "be") &&
|
|
|
|
!td::contains(language_codes, "mk") && !td::contains(language_codes, "sr") &&
|
|
|
|
!td::contains(language_codes, "mn") && !td::contains(language_codes, "ky") &&
|
|
|
|
!td::contains(language_codes, "kk") && !td::contains(language_codes, "uz") &&
|
|
|
|
!td::contains(language_codes, "tk")) {
|
|
|
|
language_codes.push_back("ru");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-21 17:48:35 +02:00
|
|
|
|
|
|
|
if (language_codes.empty()) {
|
2020-10-01 16:21:26 +02:00
|
|
|
LOG(INFO) << "List of language codes is empty";
|
2019-05-21 17:48:35 +02:00
|
|
|
language_codes.push_back("en");
|
|
|
|
}
|
2020-12-30 16:50:57 +01:00
|
|
|
td::unique(language_codes);
|
2019-05-21 17:48:35 +02:00
|
|
|
|
2020-01-08 17:07:40 +01:00
|
|
|
LOG(DEBUG) << "Have language codes " << language_codes;
|
2019-05-21 17:48:35 +02:00
|
|
|
auto key = get_emoji_language_codes_database_key(language_codes);
|
|
|
|
auto it = emoji_language_codes_.find(key);
|
|
|
|
if (it == emoji_language_codes_.end()) {
|
|
|
|
it = emoji_language_codes_.emplace(key, full_split(G()->td_db()->get_sqlite_sync_pmc()->get(key), '$')).first;
|
2022-02-10 16:03:03 +01:00
|
|
|
td::remove_if(it->second, [](const string &language_code) {
|
|
|
|
if (language_code.empty() || language_code.find('$') != string::npos) {
|
|
|
|
LOG(ERROR) << "Loaded language_code \"" << language_code << '"';
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
2019-05-21 17:48:35 +02:00
|
|
|
}
|
|
|
|
if (it->second.empty()) {
|
|
|
|
load_language_codes(std::move(language_codes), std::move(key), std::move(promise));
|
2019-05-21 19:42:41 +02:00
|
|
|
} else {
|
2020-10-01 16:21:26 +02:00
|
|
|
LOG(DEBUG) << "Have emoji language codes " << it->second;
|
2019-05-21 19:42:41 +02:00
|
|
|
double now = Time::now_cached();
|
|
|
|
for (auto &language_code : it->second) {
|
|
|
|
double last_difference_time = get_emoji_language_code_last_difference_time(language_code);
|
2019-06-27 14:31:29 +02:00
|
|
|
if (last_difference_time < now - EMOJI_KEYWORDS_UPDATE_DELAY &&
|
|
|
|
get_emoji_language_code_version(language_code) != 0) {
|
2019-05-21 19:42:41 +02:00
|
|
|
load_emoji_keywords_difference(language_code);
|
|
|
|
}
|
|
|
|
}
|
2020-01-08 17:25:36 +01:00
|
|
|
if (reloaded_emoji_keywords_.insert(key).second) {
|
|
|
|
load_language_codes(std::move(language_codes), std::move(key), Auto());
|
|
|
|
}
|
2019-05-21 17:48:35 +02:00
|
|
|
}
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
void StickersManager::load_emoji_keywords(const string &language_code, Promise<Unit> &&promise) {
|
|
|
|
auto &promises = load_emoji_keywords_queries_[language_code];
|
|
|
|
promises.push_back(std::move(promise));
|
|
|
|
if (promises.size() != 1) {
|
|
|
|
// query has already been sent, just wait for the result
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto query_promise = PromiseCreator::lambda(
|
|
|
|
[actor_id = actor_id(this),
|
|
|
|
language_code](Result<telegram_api::object_ptr<telegram_api::emojiKeywordsDifference>> &&result) mutable {
|
|
|
|
send_closure(actor_id, &StickersManager::on_get_emoji_keywords, language_code, std::move(result));
|
|
|
|
});
|
|
|
|
td_->create_handler<GetEmojiKeywordsQuery>(std::move(query_promise))->send(language_code);
|
|
|
|
}
|
|
|
|
|
|
|
|
void StickersManager::on_get_emoji_keywords(
|
|
|
|
const string &language_code, Result<telegram_api::object_ptr<telegram_api::emojiKeywordsDifference>> &&result) {
|
|
|
|
auto it = load_emoji_keywords_queries_.find(language_code);
|
|
|
|
CHECK(it != load_emoji_keywords_queries_.end());
|
|
|
|
auto promises = std::move(it->second);
|
2021-10-07 14:28:52 +02:00
|
|
|
CHECK(!promises.empty());
|
2019-05-21 17:48:35 +02:00
|
|
|
load_emoji_keywords_queries_.erase(it);
|
|
|
|
|
|
|
|
if (result.is_error()) {
|
2020-03-30 23:35:58 +02:00
|
|
|
if (!G()->is_expected_error(result.error())) {
|
2019-06-12 15:05:22 +02:00
|
|
|
LOG(ERROR) << "Receive " << result.error() << " from GetEmojiKeywordsQuery";
|
2019-05-21 17:48:35 +02:00
|
|
|
}
|
2022-04-13 16:40:12 +02:00
|
|
|
fail_promises(promises, result.move_as_error());
|
2019-05-21 17:48:35 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto version = get_emoji_language_code_version(language_code);
|
|
|
|
CHECK(version == 0);
|
|
|
|
|
|
|
|
MultiPromiseActorSafe mpas{"SaveEmojiKeywordsMultiPromiseActor"};
|
|
|
|
for (auto &promise : promises) {
|
|
|
|
mpas.add_promise(std::move(promise));
|
|
|
|
}
|
|
|
|
|
|
|
|
auto lock = mpas.get_promise();
|
|
|
|
|
|
|
|
auto keywords = result.move_as_ok();
|
|
|
|
LOG(INFO) << "Receive " << keywords->keywords_.size() << " emoji keywords for language " << language_code;
|
|
|
|
LOG_IF(ERROR, language_code != keywords->lang_code_)
|
|
|
|
<< "Receive keywords for " << keywords->lang_code_ << " instead of " << language_code;
|
|
|
|
LOG_IF(ERROR, keywords->from_version_ != 0) << "Receive keywords from version " << keywords->from_version_;
|
|
|
|
version = keywords->version_;
|
|
|
|
if (version <= 0) {
|
2019-05-21 19:42:41 +02:00
|
|
|
LOG(ERROR) << "Receive keywords of version " << version;
|
2019-05-21 17:48:35 +02:00
|
|
|
version = 1;
|
|
|
|
}
|
|
|
|
for (auto &keyword_ptr : keywords->keywords_) {
|
|
|
|
switch (keyword_ptr->get_id()) {
|
|
|
|
case telegram_api::emojiKeyword::ID: {
|
|
|
|
auto keyword = telegram_api::move_object_as<telegram_api::emojiKeyword>(keyword_ptr);
|
|
|
|
auto text = utf8_to_lower(keyword->keyword_);
|
|
|
|
bool is_good = true;
|
|
|
|
for (auto &emoji : keyword->emoticons_) {
|
|
|
|
if (emoji.find('$') != string::npos) {
|
|
|
|
LOG(ERROR) << "Receive emoji \"" << emoji << "\" from server for " << text;
|
|
|
|
is_good = false;
|
|
|
|
}
|
|
|
|
}
|
2020-05-16 01:45:26 +02:00
|
|
|
if (is_good && !G()->close_flag()) {
|
|
|
|
CHECK(G()->parameters().use_file_db);
|
2019-05-21 17:48:35 +02:00
|
|
|
G()->td_db()->get_sqlite_pmc()->set(get_language_emojis_database_key(language_code, text),
|
|
|
|
implode(keyword->emoticons_, '$'), mpas.get_promise());
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case telegram_api::emojiKeywordDeleted::ID:
|
|
|
|
LOG(ERROR) << "Receive emojiKeywordDeleted in keywords for " << language_code;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
}
|
2020-05-16 01:45:26 +02:00
|
|
|
if (!G()->close_flag()) {
|
|
|
|
CHECK(G()->parameters().use_file_db);
|
|
|
|
G()->td_db()->get_sqlite_pmc()->set(get_emoji_language_code_version_database_key(language_code), to_string(version),
|
|
|
|
mpas.get_promise());
|
|
|
|
G()->td_db()->get_sqlite_pmc()->set(get_emoji_language_code_last_difference_time_database_key(language_code),
|
|
|
|
to_string(G()->unix_time()), mpas.get_promise());
|
|
|
|
}
|
2019-05-21 17:48:35 +02:00
|
|
|
emoji_language_code_versions_[language_code] = version;
|
2019-05-21 19:42:41 +02:00
|
|
|
emoji_language_code_last_difference_times_[language_code] = static_cast<int32>(Time::now_cached());
|
2019-05-21 17:48:35 +02:00
|
|
|
|
|
|
|
lock.set_value(Unit());
|
|
|
|
}
|
|
|
|
|
2019-05-21 19:42:41 +02:00
|
|
|
void StickersManager::load_emoji_keywords_difference(const string &language_code) {
|
|
|
|
LOG(INFO) << "Load emoji keywords difference for language " << language_code;
|
2022-02-10 16:03:03 +01:00
|
|
|
CHECK(!language_code.empty());
|
2019-05-21 19:42:41 +02:00
|
|
|
emoji_language_code_last_difference_times_[language_code] =
|
|
|
|
Time::now_cached() + 1e9; // prevent simultaneous requests
|
|
|
|
int32 from_version = get_emoji_language_code_version(language_code);
|
|
|
|
auto query_promise = PromiseCreator::lambda(
|
|
|
|
[actor_id = actor_id(this), language_code,
|
|
|
|
from_version](Result<telegram_api::object_ptr<telegram_api::emojiKeywordsDifference>> &&result) mutable {
|
|
|
|
send_closure(actor_id, &StickersManager::on_get_emoji_keywords_difference, language_code, from_version,
|
|
|
|
std::move(result));
|
|
|
|
});
|
|
|
|
td_->create_handler<GetEmojiKeywordsDifferenceQuery>(std::move(query_promise))->send(language_code, from_version);
|
|
|
|
}
|
|
|
|
|
|
|
|
void StickersManager::on_get_emoji_keywords_difference(
|
|
|
|
const string &language_code, int32 from_version,
|
|
|
|
Result<telegram_api::object_ptr<telegram_api::emojiKeywordsDifference>> &&result) {
|
2021-12-12 21:50:05 +01:00
|
|
|
if (G()->close_flag()) {
|
2022-08-10 13:04:36 +02:00
|
|
|
result = Global::request_aborted_error();
|
2021-12-12 21:50:05 +01:00
|
|
|
}
|
2019-05-21 19:42:41 +02:00
|
|
|
if (result.is_error()) {
|
2020-03-30 23:35:58 +02:00
|
|
|
if (!G()->is_expected_error(result.error())) {
|
2019-06-12 15:05:22 +02:00
|
|
|
LOG(ERROR) << "Receive " << result.error() << " from GetEmojiKeywordsDifferenceQuery";
|
2019-05-21 19:42:41 +02:00
|
|
|
}
|
|
|
|
emoji_language_code_last_difference_times_[language_code] = Time::now_cached() - EMOJI_KEYWORDS_UPDATE_DELAY - 2;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto version = get_emoji_language_code_version(language_code);
|
|
|
|
CHECK(version == from_version);
|
|
|
|
|
|
|
|
auto keywords = result.move_as_ok();
|
|
|
|
LOG(INFO) << "Receive " << keywords->keywords_.size() << " emoji keywords difference for language " << language_code;
|
|
|
|
LOG_IF(ERROR, language_code != keywords->lang_code_)
|
|
|
|
<< "Receive keywords for " << keywords->lang_code_ << " instead of " << language_code;
|
|
|
|
LOG_IF(ERROR, keywords->from_version_ != from_version)
|
|
|
|
<< "Receive keywords from version " << keywords->from_version_ << " instead of " << from_version;
|
|
|
|
if (keywords->version_ < version) {
|
|
|
|
LOG(ERROR) << "Receive keywords of version " << keywords->version_ << ", but have of version " << version;
|
|
|
|
keywords->version_ = version;
|
|
|
|
}
|
|
|
|
version = keywords->version_;
|
2022-02-07 20:41:07 +01:00
|
|
|
FlatHashMap<string, string> key_values;
|
2021-12-12 21:50:05 +01:00
|
|
|
key_values.emplace(get_emoji_language_code_version_database_key(language_code), to_string(version));
|
|
|
|
key_values.emplace(get_emoji_language_code_last_difference_time_database_key(language_code),
|
|
|
|
to_string(G()->unix_time()));
|
2019-05-21 19:42:41 +02:00
|
|
|
for (auto &keyword_ptr : keywords->keywords_) {
|
|
|
|
switch (keyword_ptr->get_id()) {
|
|
|
|
case telegram_api::emojiKeyword::ID: {
|
|
|
|
auto keyword = telegram_api::move_object_as<telegram_api::emojiKeyword>(keyword_ptr);
|
|
|
|
auto text = utf8_to_lower(keyword->keyword_);
|
|
|
|
bool is_good = true;
|
|
|
|
for (auto &emoji : keyword->emoticons_) {
|
|
|
|
if (emoji.find('$') != string::npos) {
|
|
|
|
LOG(ERROR) << "Receive emoji \"" << emoji << "\" from server for " << text;
|
|
|
|
is_good = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (is_good) {
|
|
|
|
vector<string> emojis = search_language_emojis(language_code, text, true);
|
|
|
|
bool is_changed = false;
|
|
|
|
for (auto &emoji : keyword->emoticons_) {
|
2019-10-22 01:12:58 +02:00
|
|
|
if (!td::contains(emojis, emoji)) {
|
2019-05-21 19:42:41 +02:00
|
|
|
emojis.push_back(emoji);
|
|
|
|
is_changed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (is_changed) {
|
2021-12-12 21:50:05 +01:00
|
|
|
key_values.emplace(get_language_emojis_database_key(language_code, text), implode(emojis, '$'));
|
2019-05-21 19:42:41 +02:00
|
|
|
} else {
|
2021-12-12 21:50:05 +01:00
|
|
|
LOG(INFO) << "Emoji keywords not changed for \"" << text << "\" from version " << from_version
|
|
|
|
<< " to version " << version;
|
2019-05-21 19:42:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case telegram_api::emojiKeywordDeleted::ID: {
|
|
|
|
auto keyword = telegram_api::move_object_as<telegram_api::emojiKeywordDeleted>(keyword_ptr);
|
|
|
|
auto text = utf8_to_lower(keyword->keyword_);
|
|
|
|
vector<string> emojis = search_language_emojis(language_code, text, true);
|
|
|
|
bool is_changed = false;
|
|
|
|
for (auto &emoji : keyword->emoticons_) {
|
2019-10-22 01:12:58 +02:00
|
|
|
if (td::remove(emojis, emoji)) {
|
2019-05-21 19:42:41 +02:00
|
|
|
is_changed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (is_changed) {
|
2021-12-12 21:50:05 +01:00
|
|
|
key_values.emplace(get_language_emojis_database_key(language_code, text), implode(emojis, '$'));
|
2019-05-21 19:42:41 +02:00
|
|
|
} else {
|
2021-12-12 21:50:05 +01:00
|
|
|
LOG(INFO) << "Emoji keywords not changed for \"" << text << "\" from version " << from_version
|
|
|
|
<< " to version " << version;
|
2019-05-21 19:42:41 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
}
|
2021-12-12 21:50:05 +01:00
|
|
|
G()->td_db()->get_sqlite_pmc()->set_all(
|
|
|
|
std::move(key_values), PromiseCreator::lambda([actor_id = actor_id(this), language_code, version](Unit) mutable {
|
|
|
|
send_closure(actor_id, &StickersManager::finish_get_emoji_keywords_difference, std::move(language_code),
|
|
|
|
version);
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
void StickersManager::finish_get_emoji_keywords_difference(string language_code, int32 version) {
|
|
|
|
if (G()->close_flag()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG(INFO) << "Finished to get emoji keywords difference for language " << language_code;
|
2019-05-21 19:42:41 +02:00
|
|
|
emoji_language_code_versions_[language_code] = version;
|
|
|
|
emoji_language_code_last_difference_times_[language_code] = static_cast<int32>(Time::now_cached());
|
|
|
|
}
|
|
|
|
|
2020-04-22 14:17:53 +02:00
|
|
|
vector<string> StickersManager::search_emojis(const string &text, bool exact_match,
|
|
|
|
const vector<string> &input_language_codes, bool force,
|
|
|
|
Promise<Unit> &&promise) {
|
2019-05-21 17:48:35 +02:00
|
|
|
if (text.empty() || !G()->parameters().use_file_db /* have SQLite PMC */) {
|
|
|
|
promise.set_value(Unit());
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2020-04-22 14:17:53 +02:00
|
|
|
auto language_codes = get_emoji_language_codes(input_language_codes, text, promise);
|
2019-05-21 17:48:35 +02:00
|
|
|
if (language_codes.empty()) {
|
|
|
|
// promise was consumed
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
vector<string> languages_to_load;
|
|
|
|
for (auto &language_code : language_codes) {
|
2022-02-10 16:03:03 +01:00
|
|
|
CHECK(!language_code.empty());
|
2019-05-21 17:48:35 +02:00
|
|
|
auto version = get_emoji_language_code_version(language_code);
|
|
|
|
if (version == 0) {
|
|
|
|
languages_to_load.push_back(language_code);
|
2019-05-21 19:42:41 +02:00
|
|
|
} else {
|
|
|
|
LOG(DEBUG) << "Found language " << language_code << " with version " << version;
|
2019-05-21 17:48:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!languages_to_load.empty()) {
|
|
|
|
if (!force) {
|
|
|
|
MultiPromiseActorSafe mpas{"LoadEmojiLanguagesMultiPromiseActor"};
|
|
|
|
mpas.add_promise(std::move(promise));
|
|
|
|
|
|
|
|
auto lock = mpas.get_promise();
|
|
|
|
for (auto &language_code : languages_to_load) {
|
|
|
|
load_emoji_keywords(language_code, mpas.get_promise());
|
|
|
|
}
|
|
|
|
lock.set_value(Unit());
|
|
|
|
return {};
|
|
|
|
} else {
|
|
|
|
LOG(ERROR) << "Have no " << languages_to_load << " emoji keywords";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto text_lowered = utf8_to_lower(text);
|
|
|
|
vector<string> result;
|
|
|
|
for (auto &language_code : language_codes) {
|
|
|
|
combine(result, search_language_emojis(language_code, text_lowered, exact_match));
|
|
|
|
}
|
|
|
|
|
2020-12-30 16:50:57 +01:00
|
|
|
td::unique(result);
|
2019-05-21 17:48:35 +02:00
|
|
|
|
|
|
|
promise.set_value(Unit());
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-05-21 21:44:13 +02:00
|
|
|
int64 StickersManager::get_emoji_suggestions_url(const string &language_code, Promise<Unit> &&promise) {
|
|
|
|
int64 random_id = 0;
|
|
|
|
do {
|
|
|
|
random_id = Random::secure_int64();
|
2022-05-01 22:03:06 +02:00
|
|
|
} while (random_id == 0 || emoji_suggestions_urls_.count(random_id) > 0);
|
2019-05-21 21:44:13 +02:00
|
|
|
emoji_suggestions_urls_[random_id]; // reserve place for result
|
|
|
|
|
|
|
|
auto query_promise =
|
|
|
|
PromiseCreator::lambda([actor_id = actor_id(this), random_id, promise = std::move(promise)](
|
|
|
|
Result<telegram_api::object_ptr<telegram_api::emojiURL>> &&result) mutable {
|
|
|
|
send_closure(actor_id, &StickersManager::on_get_emoji_suggestions_url, random_id, std::move(promise),
|
|
|
|
std::move(result));
|
|
|
|
});
|
|
|
|
td_->create_handler<GetEmojiUrlQuery>(std::move(query_promise))->send(language_code);
|
|
|
|
return random_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
void StickersManager::on_get_emoji_suggestions_url(
|
|
|
|
int64 random_id, Promise<Unit> &&promise, Result<telegram_api::object_ptr<telegram_api::emojiURL>> &&r_emoji_url) {
|
|
|
|
auto it = emoji_suggestions_urls_.find(random_id);
|
|
|
|
CHECK(it != emoji_suggestions_urls_.end());
|
|
|
|
auto &result = it->second;
|
|
|
|
CHECK(result.empty());
|
|
|
|
|
|
|
|
if (r_emoji_url.is_error()) {
|
|
|
|
emoji_suggestions_urls_.erase(it);
|
|
|
|
return promise.set_error(r_emoji_url.move_as_error());
|
|
|
|
}
|
|
|
|
|
|
|
|
auto emoji_url = r_emoji_url.move_as_ok();
|
|
|
|
result = std::move(emoji_url->url_);
|
|
|
|
promise.set_value(Unit());
|
|
|
|
}
|
|
|
|
|
|
|
|
td_api::object_ptr<td_api::httpUrl> StickersManager::get_emoji_suggestions_url_result(int64 random_id) {
|
|
|
|
auto it = emoji_suggestions_urls_.find(random_id);
|
|
|
|
CHECK(it != emoji_suggestions_urls_.end());
|
|
|
|
auto result = td_api::make_object<td_api::httpUrl>(it->second);
|
|
|
|
emoji_suggestions_urls_.erase(it);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-09-20 21:25:09 +02:00
|
|
|
void StickersManager::get_current_state(vector<td_api::object_ptr<td_api::Update>> &updates) const {
|
2018-09-21 09:15:07 +02:00
|
|
|
if (td_->auth_manager_->is_bot()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-09-12 00:35:47 +02:00
|
|
|
if (!active_reactions_.empty()) {
|
|
|
|
updates.push_back(get_update_active_emoji_reactions_object());
|
2022-01-19 12:14:21 +01:00
|
|
|
}
|
2022-07-14 15:17:19 +02:00
|
|
|
for (int32 type = 0; type < MAX_STICKER_TYPE; type++) {
|
|
|
|
if (are_installed_sticker_sets_loaded_[type]) {
|
|
|
|
updates.push_back(get_update_installed_sticker_sets_object(static_cast<StickerType>(type)));
|
2018-09-20 21:25:09 +02:00
|
|
|
}
|
2022-07-17 21:39:22 +02:00
|
|
|
if (are_featured_sticker_sets_loaded_[type]) {
|
|
|
|
updates.push_back(get_update_trending_sticker_sets_object(static_cast<StickerType>(type)));
|
|
|
|
}
|
2018-09-20 21:25:09 +02:00
|
|
|
}
|
2022-07-17 21:39:22 +02:00
|
|
|
|
2018-09-20 21:25:09 +02:00
|
|
|
for (int is_attached = 0; is_attached < 2; is_attached++) {
|
|
|
|
if (are_recent_stickers_loaded_[is_attached]) {
|
|
|
|
updates.push_back(get_update_recent_stickers_object(is_attached));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (are_favorite_stickers_loaded_) {
|
|
|
|
updates.push_back(get_update_favorite_stickers_object());
|
|
|
|
}
|
2020-04-19 10:17:51 +02:00
|
|
|
if (!dice_emojis_.empty()) {
|
|
|
|
updates.push_back(get_update_dice_emojis_object());
|
|
|
|
}
|
2018-09-20 21:25:09 +02:00
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|