2018-12-31 20:04:05 +01:00
|
|
|
//
|
2022-12-31 22:28:08 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
|
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/HashtagHints.h"
|
|
|
|
|
2019-01-06 20:59:17 +01:00
|
|
|
#include "td/telegram/Global.h"
|
|
|
|
#include "td/telegram/TdDb.h"
|
2021-09-18 23:47:05 +02:00
|
|
|
#include "td/telegram/TdParameters.h"
|
2019-01-06 20:59:17 +01:00
|
|
|
|
2018-07-18 03:11:48 +02:00
|
|
|
#include "td/db/SqliteKeyValueAsync.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2022-11-23 17:37:32 +01:00
|
|
|
#include "td/utils/HashTableUtils.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/logging.h"
|
|
|
|
#include "td/utils/tl_helpers.h"
|
2019-11-17 17:30:53 +01:00
|
|
|
#include "td/utils/utf8.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
|
|
|
HashtagHints::HashtagHints(string mode, ActorShared<> parent) : mode_(std::move(mode)), parent_(std::move(parent)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void HashtagHints::start_up() {
|
|
|
|
if (G()->parameters().use_file_db) { // TODO hashtag hints should not depend on use_file_db
|
|
|
|
G()->td_db()->get_sqlite_pmc()->get(get_key(),
|
|
|
|
PromiseCreator::lambda([actor_id = actor_id(this)](Result<string> res) {
|
|
|
|
send_closure(actor_id, &HashtagHints::from_db, std::move(res), false);
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void HashtagHints::hashtag_used(const string &hashtag) {
|
|
|
|
if (!sync_with_db_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
hashtag_used_impl(hashtag);
|
|
|
|
G()->td_db()->get_sqlite_pmc()->set(get_key(), serialize(keys_to_strings(hints_.search_empty(101).second)),
|
|
|
|
Promise<>());
|
|
|
|
}
|
|
|
|
|
|
|
|
void HashtagHints::remove_hashtag(string hashtag, Promise<> promise) {
|
|
|
|
if (!sync_with_db_) {
|
|
|
|
promise.set_value(Unit());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (hashtag[0] == '#') {
|
|
|
|
hashtag = hashtag.substr(1);
|
|
|
|
}
|
2022-11-23 17:37:32 +01:00
|
|
|
auto key = Hash<string>()(hashtag);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (hints_.has_key(key)) {
|
|
|
|
hints_.remove(key);
|
|
|
|
G()->td_db()->get_sqlite_pmc()->set(get_key(), serialize(keys_to_strings(hints_.search_empty(101).second)),
|
|
|
|
Promise<>());
|
|
|
|
promise.set_value(Unit()); // set promise explicitly, because sqlite_pmc waits for too long before setting promise
|
|
|
|
} else {
|
|
|
|
promise.set_value(Unit());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void HashtagHints::query(const string &prefix, int32 limit, Promise<std::vector<string>> promise) {
|
|
|
|
if (!sync_with_db_) {
|
|
|
|
promise.set_value(std::vector<string>());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto result = prefix.empty() ? hints_.search_empty(limit) : hints_.search(prefix, limit);
|
|
|
|
promise.set_value(keys_to_strings(result.second));
|
|
|
|
}
|
|
|
|
|
|
|
|
string HashtagHints::get_key() const {
|
|
|
|
return "hashtag_hints#" + mode_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void HashtagHints::hashtag_used_impl(const string &hashtag) {
|
2019-11-17 17:30:53 +01:00
|
|
|
if (!check_utf8(hashtag)) {
|
|
|
|
LOG(ERROR) << "Trying to add invalid UTF-8 hashtag \"" << hashtag << '"';
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-11-23 17:37:32 +01:00
|
|
|
auto key = Hash<string>()(hashtag);
|
2018-12-31 20:04:05 +01:00
|
|
|
hints_.add(key, hashtag);
|
|
|
|
hints_.set_rating(key, -++counter_);
|
|
|
|
}
|
|
|
|
|
|
|
|
void HashtagHints::from_db(Result<string> data, bool dummy) {
|
2020-05-16 01:25:03 +02:00
|
|
|
if (G()->close_flag()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
sync_with_db_ = true;
|
|
|
|
if (data.is_error() || data.ok().empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
std::vector<string> hashtags;
|
|
|
|
auto status = unserialize(hashtags, data.ok());
|
|
|
|
if (status.is_error()) {
|
2019-11-17 17:30:53 +01:00
|
|
|
LOG(ERROR) << "Failed to unserialize hashtag hints: " << status;
|
2018-12-31 20:04:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto it = hashtags.rbegin(); it != hashtags.rend(); ++it) {
|
|
|
|
hashtag_used_impl(*it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<string> HashtagHints::keys_to_strings(const std::vector<int64> &keys) {
|
|
|
|
std::vector<string> result;
|
|
|
|
result.reserve(keys.size());
|
|
|
|
for (auto &it : keys) {
|
|
|
|
result.push_back(hints_.key_to_string(it));
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
} // namespace td
|