2019-09-11 09:26:13 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
|
|
|
the official desktop application for the Telegram messaging service.
|
|
|
|
|
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
|
|
|
*/
|
|
|
|
#include "data/data_message_reactions.h"
|
|
|
|
|
|
|
|
#include "history/history.h"
|
|
|
|
#include "history/history_item.h"
|
|
|
|
#include "main/main_session.h"
|
2022-01-13 16:39:44 +00:00
|
|
|
#include "main/main_account.h"
|
|
|
|
#include "main/main_app_config.h"
|
2022-01-21 12:48:14 +00:00
|
|
|
#include "data/data_user.h"
|
2019-09-11 10:55:39 +00:00
|
|
|
#include "data/data_session.h"
|
2022-01-28 14:11:38 +00:00
|
|
|
#include "data/data_histories.h"
|
2022-01-10 14:22:43 +00:00
|
|
|
#include "data/data_changes.h"
|
2021-12-20 15:30:01 +00:00
|
|
|
#include "data/data_document.h"
|
|
|
|
#include "data/data_document_media.h"
|
2022-08-19 12:08:11 +00:00
|
|
|
#include "data/data_peer_values.h"
|
2022-08-16 17:57:40 +00:00
|
|
|
#include "data/stickers/data_custom_emoji.h"
|
2022-05-18 13:37:24 +00:00
|
|
|
#include "storage/localimageloader.h"
|
|
|
|
#include "ui/image/image_location_factory.h"
|
2022-08-29 12:34:21 +00:00
|
|
|
#include "ui/animated_icon.h"
|
2022-08-16 11:28:17 +00:00
|
|
|
#include "mtproto/mtproto_config.h"
|
2021-12-07 11:11:27 +00:00
|
|
|
#include "base/timer_rpl.h"
|
2022-08-24 08:42:01 +00:00
|
|
|
#include "base/call_delayed.h"
|
2019-09-11 09:26:13 +00:00
|
|
|
#include "apiwrap.h"
|
2021-12-20 15:30:01 +00:00
|
|
|
#include "styles/style_chat.h"
|
2019-09-11 09:26:13 +00:00
|
|
|
|
|
|
|
namespace Data {
|
2021-12-07 11:11:27 +00:00
|
|
|
namespace {
|
|
|
|
|
2021-12-28 18:20:32 +00:00
|
|
|
constexpr auto kRefreshFullListEach = 60 * 60 * crl::time(1000);
|
|
|
|
constexpr auto kPollEach = 20 * crl::time(1000);
|
2022-01-05 16:53:16 +00:00
|
|
|
constexpr auto kSizeForDownscale = 64;
|
2022-08-24 08:42:01 +00:00
|
|
|
constexpr auto kRecentRequestTimeout = 10 * crl::time(1000);
|
|
|
|
constexpr auto kRecentReactionsLimit = 40;
|
|
|
|
constexpr auto kTopRequestDelay = 60 * crl::time(1000);
|
2022-08-30 15:21:14 +00:00
|
|
|
constexpr auto kTopReactionsLimit = 14;
|
2021-12-07 11:11:27 +00:00
|
|
|
|
2022-08-16 11:28:17 +00:00
|
|
|
[[nodiscard]] QString ReactionIdToLog(const ReactionId &id) {
|
|
|
|
if (const auto custom = id.custom()) {
|
|
|
|
return "custom:" + QString::number(custom);
|
|
|
|
}
|
|
|
|
return id.emoji();
|
|
|
|
}
|
|
|
|
|
2022-08-24 08:42:01 +00:00
|
|
|
[[nodiscard]] std::vector<ReactionId> ListFromMTP(
|
|
|
|
const MTPDmessages_reactions &data) {
|
|
|
|
const auto &list = data.vreactions().v;
|
|
|
|
auto result = std::vector<ReactionId>();
|
|
|
|
result.reserve(list.size());
|
|
|
|
for (const auto &reaction : list) {
|
|
|
|
const auto id = ReactionFromMTP(reaction);
|
|
|
|
if (id.empty()) {
|
|
|
|
LOG(("API Error: reactionEmpty in messages.reactions."));
|
|
|
|
} else {
|
|
|
|
result.push_back(id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-08-25 20:56:38 +00:00
|
|
|
[[nodiscard]] Reaction CustomReaction(not_null<DocumentData*> document) {
|
|
|
|
return Reaction{
|
|
|
|
.id = { { document->id } },
|
|
|
|
.title = "Custom reaction",
|
|
|
|
.appearAnimation = document,
|
|
|
|
.selectAnimation = document,
|
|
|
|
.centerIcon = document,
|
|
|
|
.active = true,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-08-30 07:08:19 +00:00
|
|
|
[[nodiscard]] int SentReactionsLimit(not_null<HistoryItem*> item) {
|
|
|
|
const auto session = &item->history()->session();
|
|
|
|
const auto config = &session->account().appConfig();
|
|
|
|
return session->premium()
|
|
|
|
? config->get<int>("reactions_user_max_premium", 3)
|
|
|
|
: config->get<int>("reactions_user_max_default", 1);
|
|
|
|
}
|
|
|
|
|
2021-12-07 11:11:27 +00:00
|
|
|
} // namespace
|
|
|
|
|
2022-08-26 09:35:19 +00:00
|
|
|
PossibleItemReactionsRef LookupPossibleReactions(
|
|
|
|
not_null<HistoryItem*> item) {
|
2022-08-19 12:08:11 +00:00
|
|
|
if (!item->canReact()) {
|
|
|
|
return {};
|
|
|
|
}
|
2022-08-26 09:35:19 +00:00
|
|
|
auto result = PossibleItemReactionsRef();
|
2022-08-19 12:08:11 +00:00
|
|
|
const auto peer = item->history()->peer;
|
|
|
|
const auto session = &peer->session();
|
|
|
|
const auto reactions = &session->data().reactions();
|
|
|
|
const auto &full = reactions->list(Reactions::Type::Active);
|
2022-08-25 20:56:38 +00:00
|
|
|
const auto &top = reactions->list(Reactions::Type::Top);
|
|
|
|
const auto &recent = reactions->list(Reactions::Type::Recent);
|
2022-08-19 12:08:11 +00:00
|
|
|
const auto &all = item->reactions();
|
|
|
|
const auto limit = UniqueReactionsLimit(peer);
|
2022-09-12 06:07:07 +00:00
|
|
|
const auto premiumPossible = session->premiumPossible();
|
2022-08-25 20:56:38 +00:00
|
|
|
const auto limited = (all.size() >= limit) && [&] {
|
|
|
|
const auto my = item->chosenReactions();
|
|
|
|
if (my.empty()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return true; // #TODO reactions
|
|
|
|
}();
|
|
|
|
auto added = base::flat_set<ReactionId>();
|
|
|
|
const auto add = [&](auto predicate) {
|
2022-08-30 15:21:14 +00:00
|
|
|
auto &&all = ranges::views::concat(top, recent, full);
|
2022-08-25 20:56:38 +00:00
|
|
|
for (const auto &reaction : all) {
|
|
|
|
if (predicate(reaction)) {
|
2022-08-26 19:53:48 +00:00
|
|
|
if (added.emplace(reaction.id).second) {
|
|
|
|
result.recent.push_back(&reaction);
|
|
|
|
}
|
2022-08-25 20:56:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
reactions->clearTemporary();
|
|
|
|
if (limited) {
|
2022-08-19 12:08:11 +00:00
|
|
|
result.recent.reserve(all.size());
|
2022-08-25 20:56:38 +00:00
|
|
|
add([&](const Reaction &reaction) {
|
|
|
|
return ranges::contains(all, reaction.id, &MessageReaction::id);
|
|
|
|
});
|
|
|
|
for (const auto &reaction : all) {
|
2022-08-19 12:08:11 +00:00
|
|
|
const auto id = reaction.id;
|
2022-08-25 20:56:38 +00:00
|
|
|
if (!added.contains(id)) {
|
|
|
|
if (const auto temp = reactions->lookupTemporary(id)) {
|
|
|
|
result.recent.push_back(temp);
|
|
|
|
}
|
2022-08-19 12:08:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2022-08-22 09:15:34 +00:00
|
|
|
const auto &allowed = PeerAllowedReactions(peer);
|
|
|
|
result.recent.reserve((allowed.type == AllowedReactionsType::Some)
|
|
|
|
? allowed.some.size()
|
2022-08-19 12:08:11 +00:00
|
|
|
: full.size());
|
2022-08-25 20:56:38 +00:00
|
|
|
add([&](const Reaction &reaction) {
|
2022-08-19 12:08:11 +00:00
|
|
|
const auto id = reaction.id;
|
2022-09-12 06:07:07 +00:00
|
|
|
if (id.custom() && !premiumPossible) {
|
|
|
|
return false;
|
|
|
|
} else if ((allowed.type == AllowedReactionsType::Some)
|
2022-08-22 09:15:34 +00:00
|
|
|
&& !ranges::contains(allowed.some, id)) {
|
2022-08-25 20:56:38 +00:00
|
|
|
return false;
|
2022-08-30 09:21:58 +00:00
|
|
|
} else if (id.custom()
|
|
|
|
&& allowed.type == AllowedReactionsType::Default) {
|
|
|
|
return false;
|
2022-08-19 12:08:11 +00:00
|
|
|
} else if (reaction.premium
|
|
|
|
&& !session->premium()
|
2022-08-25 20:56:38 +00:00
|
|
|
&& !ranges::contains(all, id, &MessageReaction::id)) {
|
2022-09-12 06:07:07 +00:00
|
|
|
if (premiumPossible) {
|
2022-08-19 12:08:11 +00:00
|
|
|
result.morePremiumAvailable = true;
|
|
|
|
}
|
2022-08-25 20:56:38 +00:00
|
|
|
return false;
|
2022-08-19 12:08:11 +00:00
|
|
|
}
|
2022-08-25 20:56:38 +00:00
|
|
|
return true;
|
|
|
|
});
|
2022-09-12 06:07:07 +00:00
|
|
|
result.customAllowed = (allowed.type == AllowedReactionsType::All)
|
|
|
|
&& premiumPossible;
|
2022-08-19 12:08:11 +00:00
|
|
|
}
|
|
|
|
const auto i = ranges::find(
|
|
|
|
result.recent,
|
2022-08-24 08:42:01 +00:00
|
|
|
reactions->favoriteId(),
|
2022-08-19 12:08:11 +00:00
|
|
|
&Reaction::id);
|
|
|
|
if (i != end(result.recent) && i != begin(result.recent)) {
|
|
|
|
std::rotate(begin(result.recent), i, i + 1);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-08-26 09:35:19 +00:00
|
|
|
PossibleItemReactions::PossibleItemReactions(
|
|
|
|
const PossibleItemReactionsRef &other)
|
|
|
|
: recent(other.recent | ranges::views::transform([](const auto &value) {
|
|
|
|
return *value;
|
|
|
|
}) | ranges::to_vector)
|
|
|
|
, morePremiumAvailable(other.morePremiumAvailable)
|
|
|
|
, customAllowed(other.customAllowed) {
|
|
|
|
}
|
|
|
|
|
2021-12-28 18:20:32 +00:00
|
|
|
Reactions::Reactions(not_null<Session*> owner)
|
|
|
|
: _owner(owner)
|
2022-08-24 08:42:01 +00:00
|
|
|
, _topRefreshTimer([=] { refreshTop(); })
|
2021-12-28 18:20:32 +00:00
|
|
|
, _repaintTimer([=] { repaintCollected(); }) {
|
2022-08-24 08:42:01 +00:00
|
|
|
refreshDefault();
|
2021-12-07 11:11:27 +00:00
|
|
|
|
|
|
|
base::timer_each(
|
2021-12-28 18:20:32 +00:00
|
|
|
kRefreshFullListEach
|
2021-12-07 11:11:27 +00:00
|
|
|
) | rpl::start_with_next([=] {
|
2022-08-24 08:42:01 +00:00
|
|
|
refreshDefault();
|
2021-12-07 11:11:27 +00:00
|
|
|
}, _lifetime);
|
2021-12-28 18:20:32 +00:00
|
|
|
|
|
|
|
_owner->session().changes().messageUpdates(
|
|
|
|
MessageUpdate::Flag::Destroyed
|
|
|
|
) | rpl::start_with_next([=](const MessageUpdate &update) {
|
|
|
|
const auto item = update.item;
|
|
|
|
_pollingItems.remove(item);
|
|
|
|
_pollItems.remove(item);
|
|
|
|
_repaintItems.remove(item);
|
|
|
|
}, _lifetime);
|
2022-01-13 16:39:44 +00:00
|
|
|
|
2022-08-30 06:53:07 +00:00
|
|
|
crl::on_main(&owner->session(), [=] {
|
|
|
|
// applyFavorite accesses not yet constructed parts of session.
|
|
|
|
rpl::single(rpl::empty) | rpl::then(
|
|
|
|
_owner->session().mtp().config().updates()
|
|
|
|
) | rpl::map([=] {
|
|
|
|
const auto &config = _owner->session().mtp().configValues();
|
|
|
|
return config.reactionDefaultCustom
|
|
|
|
? ReactionId{ DocumentId(config.reactionDefaultCustom) }
|
|
|
|
: ReactionId{ config.reactionDefaultEmoji };
|
|
|
|
}) | rpl::filter([=](const ReactionId &id) {
|
|
|
|
return !_saveFaveRequestId;
|
|
|
|
}) | rpl::start_with_next([=](ReactionId &&id) {
|
|
|
|
applyFavorite(id);
|
|
|
|
}, _lifetime);
|
|
|
|
});
|
2021-12-07 11:11:27 +00:00
|
|
|
}
|
|
|
|
|
2022-01-05 12:10:17 +00:00
|
|
|
Reactions::~Reactions() = default;
|
|
|
|
|
2022-08-26 09:35:19 +00:00
|
|
|
Main::Session &Reactions::session() const {
|
|
|
|
return _owner->session();
|
|
|
|
}
|
|
|
|
|
2022-08-24 08:42:01 +00:00
|
|
|
void Reactions::refreshTop() {
|
|
|
|
requestTop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Reactions::refreshRecent() {
|
|
|
|
requestRecent();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Reactions::refreshRecentDelayed() {
|
|
|
|
if (_recentRequestId || _recentRequestScheduled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_recentRequestScheduled = true;
|
|
|
|
base::call_delayed(kRecentRequestTimeout, &_owner->session(), [=] {
|
|
|
|
if (_recentRequestScheduled) {
|
|
|
|
requestRecent();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void Reactions::refreshDefault() {
|
|
|
|
requestDefault();
|
2021-12-13 11:26:19 +00:00
|
|
|
}
|
|
|
|
|
2021-12-22 11:56:02 +00:00
|
|
|
const std::vector<Reaction> &Reactions::list(Type type) const {
|
|
|
|
switch (type) {
|
|
|
|
case Type::Active: return _active;
|
2022-08-24 08:42:01 +00:00
|
|
|
case Type::Recent: return _recent;
|
|
|
|
case Type::Top: return _top;
|
2021-12-22 11:56:02 +00:00
|
|
|
case Type::All: return _available;
|
|
|
|
}
|
|
|
|
Unexpected("Type in Reactions::list.");
|
2021-12-07 11:11:27 +00:00
|
|
|
}
|
|
|
|
|
2022-08-24 08:42:01 +00:00
|
|
|
ReactionId Reactions::favoriteId() const {
|
|
|
|
return _favoriteId;
|
2022-01-13 16:39:44 +00:00
|
|
|
}
|
|
|
|
|
2022-08-24 08:42:01 +00:00
|
|
|
const Reaction *Reactions::favorite() const {
|
|
|
|
return _favorite ? &*_favorite : nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Reactions::setFavorite(const ReactionId &id) {
|
2022-01-13 19:32:44 +00:00
|
|
|
const auto api = &_owner->session().api();
|
|
|
|
if (_saveFaveRequestId) {
|
|
|
|
api->request(_saveFaveRequestId).cancel();
|
|
|
|
}
|
|
|
|
_saveFaveRequestId = api->request(MTPmessages_SetDefaultReaction(
|
2022-08-24 08:42:01 +00:00
|
|
|
ReactionToMTP(id)
|
2022-01-13 19:32:44 +00:00
|
|
|
)).done([=] {
|
|
|
|
_saveFaveRequestId = 0;
|
|
|
|
}).fail([=] {
|
|
|
|
_saveFaveRequestId = 0;
|
|
|
|
}).send();
|
|
|
|
|
2022-08-24 08:42:01 +00:00
|
|
|
applyFavorite(id);
|
|
|
|
}
|
|
|
|
|
2022-09-01 09:29:10 +00:00
|
|
|
DocumentData *Reactions::chooseGenericAnimation(
|
|
|
|
not_null<DocumentData*> custom) const {
|
|
|
|
const auto sticker = custom->sticker();
|
|
|
|
const auto i = sticker
|
|
|
|
? ranges::find(
|
|
|
|
_available,
|
|
|
|
::Data::ReactionId{ { sticker->alt } },
|
|
|
|
&::Data::Reaction::id)
|
|
|
|
: end(_available);
|
|
|
|
if (i != end(_available) && i->aroundAnimation) {
|
|
|
|
const auto view = i->aroundAnimation->createMediaView();
|
|
|
|
view->checkStickerLarge();
|
|
|
|
if (view->loaded()) {
|
|
|
|
return i->aroundAnimation;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (_genericAnimations.empty()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
auto copy = _genericAnimations;
|
|
|
|
ranges::shuffle(copy);
|
|
|
|
const auto first = copy.front();
|
|
|
|
const auto view = first->createMediaView();
|
|
|
|
view->checkStickerLarge();
|
|
|
|
if (view->loaded()) {
|
|
|
|
return first;
|
|
|
|
}
|
|
|
|
const auto k = ranges::find_if(copy, [&](not_null<DocumentData*> value) {
|
|
|
|
return value->createMediaView()->loaded();
|
|
|
|
});
|
|
|
|
return (k != end(copy)) ? (*k) : first;
|
|
|
|
}
|
|
|
|
|
2022-08-24 08:42:01 +00:00
|
|
|
void Reactions::applyFavorite(const ReactionId &id) {
|
|
|
|
if (_favoriteId != id) {
|
|
|
|
_favoriteId = id;
|
|
|
|
_favorite = resolveById(_favoriteId);
|
|
|
|
if (!_favorite && _unresolvedFavoriteId != _favoriteId) {
|
|
|
|
_unresolvedFavoriteId = _favoriteId;
|
|
|
|
resolve(_favoriteId);
|
|
|
|
}
|
|
|
|
_favoriteUpdated.fire({});
|
2022-01-13 19:32:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-24 08:42:01 +00:00
|
|
|
rpl::producer<> Reactions::topUpdates() const {
|
|
|
|
return _topUpdated.events();
|
|
|
|
}
|
|
|
|
|
|
|
|
rpl::producer<> Reactions::recentUpdates() const {
|
|
|
|
return _recentUpdated.events();
|
|
|
|
}
|
|
|
|
|
|
|
|
rpl::producer<> Reactions::defaultUpdates() const {
|
|
|
|
return _defaultUpdated.events();
|
|
|
|
}
|
|
|
|
|
|
|
|
rpl::producer<> Reactions::favoriteUpdates() const {
|
|
|
|
return _favoriteUpdated.events();
|
2021-12-13 11:26:19 +00:00
|
|
|
}
|
|
|
|
|
2022-08-16 11:28:17 +00:00
|
|
|
void Reactions::preloadImageFor(const ReactionId &id) {
|
2022-08-24 08:42:01 +00:00
|
|
|
if (_images.contains(id) || id.emoji().isEmpty()) {
|
2021-12-20 15:30:01 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-08-16 11:28:17 +00:00
|
|
|
auto &set = _images.emplace(id).first->second;
|
|
|
|
const auto i = ranges::find(_available, id, &Reaction::id);
|
2022-01-10 09:43:26 +00:00
|
|
|
const auto document = (i == end(_available))
|
|
|
|
? nullptr
|
|
|
|
: i->centerIcon
|
|
|
|
? i->centerIcon
|
2022-08-29 12:34:21 +00:00
|
|
|
: i->selectAnimation.get();
|
2021-12-20 15:30:01 +00:00
|
|
|
if (document) {
|
2022-01-10 09:43:26 +00:00
|
|
|
loadImage(set, document, !i->centerIcon);
|
2021-12-20 15:30:01 +00:00
|
|
|
} else if (!_waitingForList) {
|
2021-12-31 14:49:52 +00:00
|
|
|
_waitingForList = true;
|
2022-08-24 08:42:01 +00:00
|
|
|
refreshRecent();
|
2021-12-20 15:30:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-16 11:28:17 +00:00
|
|
|
void Reactions::preloadAnimationsFor(const ReactionId &id) {
|
2022-09-01 09:29:10 +00:00
|
|
|
const auto custom = id.custom();
|
|
|
|
const auto document = custom ? _owner->document(custom).get() : nullptr;
|
|
|
|
const auto customSticker = document ? document->sticker() : nullptr;
|
|
|
|
const auto findId = custom
|
|
|
|
? ReactionId{ { customSticker ? customSticker->alt : QString() } }
|
|
|
|
: id;
|
|
|
|
const auto i = ranges::find(_available, findId, &Reaction::id);
|
2022-01-11 15:33:14 +00:00
|
|
|
if (i == end(_available)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto preload = [&](DocumentData *document) {
|
|
|
|
const auto view = document
|
|
|
|
? document->activeMediaView()
|
|
|
|
: nullptr;
|
|
|
|
if (view) {
|
|
|
|
view->checkStickerLarge();
|
|
|
|
}
|
|
|
|
};
|
2022-09-01 09:29:10 +00:00
|
|
|
|
|
|
|
if (!custom) {
|
|
|
|
preload(i->centerIcon);
|
|
|
|
}
|
2022-01-11 15:33:14 +00:00
|
|
|
preload(i->aroundAnimation);
|
|
|
|
}
|
|
|
|
|
2021-12-20 15:30:01 +00:00
|
|
|
QImage Reactions::resolveImageFor(
|
2022-08-16 11:28:17 +00:00
|
|
|
const ReactionId &emoji,
|
2021-12-20 15:30:01 +00:00
|
|
|
ImageSize size) {
|
|
|
|
const auto i = _images.find(emoji);
|
|
|
|
if (i == end(_images)) {
|
|
|
|
preloadImageFor(emoji);
|
|
|
|
}
|
|
|
|
auto &set = (i != end(_images)) ? i->second : _images[emoji];
|
2022-01-05 12:10:17 +00:00
|
|
|
const auto resolve = [&](QImage &image, int size) {
|
|
|
|
const auto factor = style::DevicePixelRatio();
|
2022-08-29 12:34:21 +00:00
|
|
|
const auto frameSize = set.fromSelectAnimation
|
2022-01-10 09:43:26 +00:00
|
|
|
? (size / 2)
|
|
|
|
: size;
|
2022-01-05 12:10:17 +00:00
|
|
|
image = set.icon->frame().scaled(
|
2022-01-10 09:43:26 +00:00
|
|
|
frameSize * factor,
|
|
|
|
frameSize * factor,
|
2022-01-05 12:10:17 +00:00
|
|
|
Qt::IgnoreAspectRatio,
|
|
|
|
Qt::SmoothTransformation);
|
2022-08-29 12:34:21 +00:00
|
|
|
if (set.fromSelectAnimation) {
|
2022-01-10 09:43:26 +00:00
|
|
|
auto result = QImage(
|
|
|
|
size * factor,
|
|
|
|
size * factor,
|
|
|
|
QImage::Format_ARGB32_Premultiplied);
|
|
|
|
result.fill(Qt::transparent);
|
|
|
|
|
|
|
|
auto p = QPainter(&result);
|
|
|
|
p.drawImage(
|
|
|
|
(size - frameSize) * factor / 2,
|
|
|
|
(size - frameSize) * factor / 2,
|
|
|
|
image);
|
|
|
|
p.end();
|
|
|
|
|
|
|
|
std::swap(result, image);
|
|
|
|
}
|
2022-01-05 12:10:17 +00:00
|
|
|
image.setDevicePixelRatio(factor);
|
|
|
|
};
|
|
|
|
if (set.bottomInfo.isNull() && set.icon) {
|
2022-01-10 09:43:26 +00:00
|
|
|
resolve(set.bottomInfo, st::reactionInfoImage);
|
2022-01-13 13:22:42 +00:00
|
|
|
resolve(set.inlineList, st::reactionInlineImage);
|
2022-01-05 12:10:17 +00:00
|
|
|
crl::async([icon = std::move(set.icon)]{});
|
|
|
|
}
|
2021-12-20 15:30:01 +00:00
|
|
|
switch (size) {
|
|
|
|
case ImageSize::BottomInfo: return set.bottomInfo;
|
|
|
|
case ImageSize::InlineList: return set.inlineList;
|
|
|
|
}
|
|
|
|
Unexpected("ImageSize in Reactions::resolveImageFor.");
|
|
|
|
}
|
|
|
|
|
|
|
|
void Reactions::resolveImages() {
|
2022-08-16 11:28:17 +00:00
|
|
|
for (auto &[id, set] : _images) {
|
2022-01-05 12:10:17 +00:00
|
|
|
if (!set.bottomInfo.isNull() || set.icon || set.media) {
|
2021-12-20 15:30:01 +00:00
|
|
|
continue;
|
|
|
|
}
|
2022-08-16 11:28:17 +00:00
|
|
|
const auto i = ranges::find(_available, id, &Reaction::id);
|
2022-01-10 09:43:26 +00:00
|
|
|
const auto document = (i == end(_available))
|
|
|
|
? nullptr
|
|
|
|
: i->centerIcon
|
|
|
|
? i->centerIcon
|
2022-08-29 12:34:21 +00:00
|
|
|
: i->selectAnimation.get();
|
2021-12-20 15:30:01 +00:00
|
|
|
if (document) {
|
2022-01-10 09:43:26 +00:00
|
|
|
loadImage(set, document, !i->centerIcon);
|
2021-12-20 15:30:01 +00:00
|
|
|
} else {
|
2022-08-16 11:28:17 +00:00
|
|
|
LOG(("API Error: Reaction '%1' not found!"
|
|
|
|
).arg(ReactionIdToLog(id)));
|
2021-12-20 15:30:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Reactions::loadImage(
|
|
|
|
ImageSet &set,
|
2022-01-10 09:43:26 +00:00
|
|
|
not_null<DocumentData*> document,
|
2022-08-29 12:34:21 +00:00
|
|
|
bool fromSelectAnimation) {
|
2022-01-05 12:10:17 +00:00
|
|
|
if (!set.bottomInfo.isNull() || set.icon) {
|
2021-12-20 15:30:01 +00:00
|
|
|
return;
|
|
|
|
} else if (!set.media) {
|
2022-08-29 12:34:21 +00:00
|
|
|
set.fromSelectAnimation = fromSelectAnimation;
|
2021-12-20 15:30:01 +00:00
|
|
|
set.media = document->createMediaView();
|
2022-01-05 12:10:17 +00:00
|
|
|
set.media->checkStickerLarge();
|
2021-12-20 15:30:01 +00:00
|
|
|
}
|
2022-01-05 12:10:17 +00:00
|
|
|
if (set.media->loaded()) {
|
2022-08-29 12:34:21 +00:00
|
|
|
setAnimatedIcon(set);
|
2021-12-20 15:30:01 +00:00
|
|
|
} else if (!_imagesLoadLifetime) {
|
|
|
|
document->session().downloaderTaskFinished(
|
|
|
|
) | rpl::start_with_next([=] {
|
|
|
|
downloadTaskFinished();
|
|
|
|
}, _imagesLoadLifetime);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-29 12:34:21 +00:00
|
|
|
void Reactions::setAnimatedIcon(ImageSet &set) {
|
2022-01-05 16:53:16 +00:00
|
|
|
const auto size = style::ConvertScale(kSizeForDownscale);
|
2022-08-29 12:34:21 +00:00
|
|
|
set.icon = Ui::MakeAnimatedIcon({
|
|
|
|
.generator = DocumentIconFrameGenerator(set.media),
|
2022-01-05 12:10:17 +00:00
|
|
|
.sizeOverride = QSize(size, size),
|
|
|
|
});
|
2021-12-20 15:30:01 +00:00
|
|
|
set.media = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Reactions::downloadTaskFinished() {
|
|
|
|
auto hasOne = false;
|
|
|
|
for (auto &[emoji, set] : _images) {
|
|
|
|
if (!set.media) {
|
|
|
|
continue;
|
2022-01-05 12:10:17 +00:00
|
|
|
} else if (set.media->loaded()) {
|
2022-08-29 12:34:21 +00:00
|
|
|
setAnimatedIcon(set);
|
2021-12-20 15:30:01 +00:00
|
|
|
} else {
|
|
|
|
hasOne = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!hasOne) {
|
|
|
|
_imagesLoadLifetime.destroy();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-24 08:42:01 +00:00
|
|
|
void Reactions::requestTop() {
|
|
|
|
if (_topRequestId) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto &api = _owner->session().api();
|
|
|
|
_topRefreshTimer.cancel();
|
|
|
|
_topRequestId = api.request(MTPmessages_GetTopReactions(
|
|
|
|
MTP_int(kTopReactionsLimit),
|
|
|
|
MTP_long(_topHash)
|
|
|
|
)).done([=](const MTPmessages_Reactions &result) {
|
|
|
|
_topRequestId = 0;
|
|
|
|
result.match([&](const MTPDmessages_reactions &data) {
|
|
|
|
updateTop(data);
|
|
|
|
}, [](const MTPDmessages_reactionsNotModified&) {
|
|
|
|
});
|
|
|
|
}).fail([=] {
|
|
|
|
_topRequestId = 0;
|
|
|
|
_topHash = 0;
|
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Reactions::requestRecent() {
|
|
|
|
if (_recentRequestId) {
|
|
|
|
return;
|
|
|
|
}
|
2021-12-07 11:11:27 +00:00
|
|
|
auto &api = _owner->session().api();
|
2022-08-24 08:42:01 +00:00
|
|
|
_recentRequestScheduled = false;
|
|
|
|
_recentRequestId = api.request(MTPmessages_GetRecentReactions(
|
|
|
|
MTP_int(kRecentReactionsLimit),
|
|
|
|
MTP_long(_recentHash)
|
|
|
|
)).done([=](const MTPmessages_Reactions &result) {
|
|
|
|
_recentRequestId = 0;
|
|
|
|
result.match([&](const MTPDmessages_reactions &data) {
|
|
|
|
updateRecent(data);
|
|
|
|
}, [](const MTPDmessages_reactionsNotModified&) {
|
|
|
|
});
|
|
|
|
}).fail([=] {
|
|
|
|
_recentRequestId = 0;
|
|
|
|
_recentHash = 0;
|
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Reactions::requestDefault() {
|
|
|
|
if (_defaultRequestId) {
|
2021-12-22 11:56:02 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-08-24 08:42:01 +00:00
|
|
|
auto &api = _owner->session().api();
|
|
|
|
_defaultRequestId = api.request(MTPmessages_GetAvailableReactions(
|
|
|
|
MTP_int(_defaultHash)
|
2021-12-07 11:11:27 +00:00
|
|
|
)).done([=](const MTPmessages_AvailableReactions &result) {
|
2022-08-24 08:42:01 +00:00
|
|
|
_defaultRequestId = 0;
|
2021-12-07 11:11:27 +00:00
|
|
|
result.match([&](const MTPDmessages_availableReactions &data) {
|
2022-08-24 08:42:01 +00:00
|
|
|
updateDefault(data);
|
2021-12-07 11:11:27 +00:00
|
|
|
}, [&](const MTPDmessages_availableReactionsNotModified &) {
|
|
|
|
});
|
|
|
|
}).fail([=] {
|
2022-08-24 08:42:01 +00:00
|
|
|
_defaultRequestId = 0;
|
|
|
|
_defaultHash = 0;
|
2021-12-07 11:11:27 +00:00
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
2022-09-01 09:29:10 +00:00
|
|
|
void Reactions::requestGeneric() {
|
|
|
|
if (_genericRequestId) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto &api = _owner->session().api();
|
|
|
|
_genericRequestId = api.request(MTPmessages_GetStickerSet(
|
|
|
|
MTP_inputStickerSetEmojiGenericAnimations(),
|
|
|
|
MTP_int(0) // hash
|
|
|
|
)).done([=](const MTPmessages_StickerSet &result) {
|
|
|
|
_genericRequestId = 0;
|
|
|
|
result.match([&](const MTPDmessages_stickerSet &data) {
|
|
|
|
updateGeneric(data);
|
|
|
|
}, [](const MTPDmessages_stickerSetNotModified &) {
|
|
|
|
LOG(("API Error: Unexpected messages.stickerSetNotModified."));
|
|
|
|
});
|
|
|
|
}).fail([=] {
|
|
|
|
_genericRequestId = 0;
|
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
2022-08-24 08:42:01 +00:00
|
|
|
void Reactions::updateTop(const MTPDmessages_reactions &data) {
|
|
|
|
_topHash = data.vhash().v;
|
|
|
|
_topIds = ListFromMTP(data);
|
|
|
|
_top = resolveByIds(_topIds, _unresolvedTop);
|
|
|
|
_topUpdated.fire({});
|
|
|
|
}
|
|
|
|
|
|
|
|
void Reactions::updateRecent(const MTPDmessages_reactions &data) {
|
|
|
|
_recentHash = data.vhash().v;
|
|
|
|
_recentIds = ListFromMTP(data);
|
|
|
|
_recent = resolveByIds(_recentIds, _unresolvedRecent);
|
|
|
|
recentUpdated();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Reactions::updateDefault(const MTPDmessages_availableReactions &data) {
|
|
|
|
_defaultHash = data.vhash().v;
|
2022-01-15 09:38:47 +00:00
|
|
|
|
|
|
|
const auto &list = data.vreactions().v;
|
|
|
|
const auto oldCache = base::take(_iconsCache);
|
|
|
|
const auto toCache = [&](DocumentData *document) {
|
|
|
|
if (document) {
|
|
|
|
_iconsCache.emplace(document, document->createMediaView());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
_active.clear();
|
|
|
|
_available.clear();
|
|
|
|
_active.reserve(list.size());
|
|
|
|
_available.reserve(list.size());
|
|
|
|
_iconsCache.reserve(list.size() * 4);
|
|
|
|
for (const auto &reaction : list) {
|
|
|
|
if (const auto parsed = parse(reaction)) {
|
|
|
|
_available.push_back(*parsed);
|
|
|
|
if (parsed->active) {
|
|
|
|
_active.push_back(*parsed);
|
|
|
|
toCache(parsed->appearAnimation);
|
|
|
|
toCache(parsed->selectAnimation);
|
|
|
|
toCache(parsed->centerIcon);
|
|
|
|
toCache(parsed->aroundAnimation);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (_waitingForList) {
|
|
|
|
_waitingForList = false;
|
|
|
|
resolveImages();
|
|
|
|
}
|
2022-08-24 08:42:01 +00:00
|
|
|
defaultUpdated();
|
|
|
|
}
|
|
|
|
|
2022-09-01 09:29:10 +00:00
|
|
|
void Reactions::updateGeneric(const MTPDmessages_stickerSet &data) {
|
|
|
|
const auto oldCache = base::take(_genericCache);
|
|
|
|
const auto toCache = [&](not_null<DocumentData*> document) {
|
|
|
|
if (document->sticker()) {
|
|
|
|
_genericAnimations.push_back(document);
|
|
|
|
_genericCache.emplace(document, document->createMediaView());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const auto &list = data.vdocuments().v;
|
|
|
|
_genericAnimations.clear();
|
|
|
|
_genericAnimations.reserve(list.size());
|
|
|
|
_genericCache.reserve(list.size());
|
|
|
|
for (const auto &sticker : data.vdocuments().v) {
|
|
|
|
toCache(_owner->processDocument(sticker));
|
|
|
|
}
|
|
|
|
if (!_genericCache.empty()) {
|
|
|
|
_genericCache.front().second->checkStickerLarge();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-24 08:42:01 +00:00
|
|
|
void Reactions::recentUpdated() {
|
|
|
|
_topRefreshTimer.callOnce(kTopRequestDelay);
|
|
|
|
_recentUpdated.fire({});
|
|
|
|
}
|
|
|
|
|
|
|
|
void Reactions::defaultUpdated() {
|
|
|
|
refreshTop();
|
|
|
|
refreshRecent();
|
2022-09-01 09:29:10 +00:00
|
|
|
if (_genericAnimations.empty()) {
|
|
|
|
requestGeneric();
|
|
|
|
}
|
2022-08-24 08:42:01 +00:00
|
|
|
_defaultUpdated.fire({});
|
|
|
|
}
|
|
|
|
|
|
|
|
not_null<CustomEmojiManager::Listener*> Reactions::resolveListener() {
|
|
|
|
return static_cast<CustomEmojiManager::Listener*>(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Reactions::customEmojiResolveDone(not_null<DocumentData*> document) {
|
|
|
|
const auto id = ReactionId{ { document->id } };
|
|
|
|
const auto favorite = (_unresolvedFavoriteId == id);
|
|
|
|
const auto i = _unresolvedTop.find(id);
|
|
|
|
const auto top = (i != end(_unresolvedTop));
|
|
|
|
const auto j = _unresolvedRecent.find(id);
|
|
|
|
const auto recent = (j != end(_unresolvedRecent));
|
|
|
|
if (favorite) {
|
|
|
|
_unresolvedFavoriteId = ReactionId();
|
|
|
|
_favorite = resolveById(_favoriteId);
|
|
|
|
}
|
|
|
|
if (top) {
|
|
|
|
_unresolvedTop.erase(i);
|
|
|
|
_top = resolveByIds(_topIds, _unresolvedTop);
|
|
|
|
}
|
|
|
|
if (recent) {
|
|
|
|
_unresolvedRecent.erase(j);
|
|
|
|
_recent = resolveByIds(_recentIds, _unresolvedRecent);
|
|
|
|
}
|
|
|
|
if (favorite) {
|
|
|
|
_favoriteUpdated.fire({});
|
|
|
|
}
|
|
|
|
if (top) {
|
|
|
|
_topUpdated.fire({});
|
|
|
|
}
|
|
|
|
if (recent) {
|
|
|
|
_recentUpdated.fire({});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<Reaction> Reactions::resolveById(const ReactionId &id) {
|
|
|
|
if (const auto emoji = id.emoji(); !emoji.isEmpty()) {
|
|
|
|
const auto i = ranges::find(_available, id, &Reaction::id);
|
|
|
|
if (i != end(_available)) {
|
|
|
|
return *i;
|
|
|
|
}
|
|
|
|
} else if (const auto customId = id.custom()) {
|
|
|
|
const auto document = _owner->document(customId);
|
|
|
|
if (document->sticker()) {
|
2022-08-25 20:56:38 +00:00
|
|
|
return CustomReaction(document);
|
2022-08-24 08:42:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<Reaction> Reactions::resolveByIds(
|
|
|
|
const std::vector<ReactionId> &ids,
|
|
|
|
base::flat_set<ReactionId> &unresolved) {
|
|
|
|
auto result = std::vector<Reaction>();
|
|
|
|
result.reserve(ids.size());
|
|
|
|
for (const auto &id : ids) {
|
|
|
|
if (const auto resolved = resolveById(id)) {
|
|
|
|
result.push_back(*resolved);
|
|
|
|
} else if (unresolved.emplace(id).second) {
|
|
|
|
resolve(id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Reactions::resolve(const ReactionId &id) {
|
|
|
|
if (const auto emoji = id.emoji(); !emoji.isEmpty()) {
|
|
|
|
refreshDefault();
|
|
|
|
} else if (const auto customId = id.custom()) {
|
|
|
|
_owner->customEmojiManager().resolve(
|
|
|
|
customId,
|
|
|
|
resolveListener());
|
|
|
|
}
|
2022-01-15 09:38:47 +00:00
|
|
|
}
|
|
|
|
|
2021-12-07 11:11:27 +00:00
|
|
|
std::optional<Reaction> Reactions::parse(const MTPAvailableReaction &entry) {
|
|
|
|
return entry.match([&](const MTPDavailableReaction &data) {
|
|
|
|
const auto emoji = qs(data.vreaction());
|
|
|
|
const auto known = (Ui::Emoji::Find(emoji) != nullptr);
|
|
|
|
if (!known) {
|
|
|
|
LOG(("API Error: Unknown emoji in reactions: %1").arg(emoji));
|
|
|
|
}
|
|
|
|
return known
|
|
|
|
? std::make_optional(Reaction{
|
2022-08-16 11:28:17 +00:00
|
|
|
.id = ReactionId{ emoji },
|
2021-12-07 11:11:27 +00:00
|
|
|
.title = qs(data.vtitle()),
|
2022-08-24 08:42:01 +00:00
|
|
|
//.staticIcon = _owner->processDocument(data.vstatic_icon()),
|
2021-12-22 11:56:02 +00:00
|
|
|
.appearAnimation = _owner->processDocument(
|
|
|
|
data.vappear_animation()),
|
2022-08-29 12:34:21 +00:00
|
|
|
.selectAnimation = _owner->processDocument(
|
|
|
|
data.vselect_animation()),
|
2022-01-11 14:13:04 +00:00
|
|
|
//.activateAnimation = _owner->processDocument(
|
|
|
|
// data.vactivate_animation()),
|
|
|
|
//.activateEffects = _owner->processDocument(
|
|
|
|
// data.veffect_animation()),
|
2022-01-05 12:10:17 +00:00
|
|
|
.centerIcon = (data.vcenter_icon()
|
2022-01-10 09:43:26 +00:00
|
|
|
? _owner->processDocument(*data.vcenter_icon()).get()
|
|
|
|
: nullptr),
|
2022-01-05 10:37:35 +00:00
|
|
|
.aroundAnimation = (data.varound_animation()
|
|
|
|
? _owner->processDocument(
|
|
|
|
*data.varound_animation()).get()
|
|
|
|
: nullptr),
|
2021-12-22 11:56:02 +00:00
|
|
|
.active = !data.is_inactive(),
|
2022-05-27 15:42:05 +00:00
|
|
|
.premium = data.is_premium(),
|
2021-12-07 11:11:27 +00:00
|
|
|
})
|
|
|
|
: std::nullopt;
|
|
|
|
});
|
|
|
|
}
|
2019-09-11 09:26:13 +00:00
|
|
|
|
2022-08-25 20:56:38 +00:00
|
|
|
void Reactions::send(not_null<HistoryItem*> item, bool addToRecent) {
|
2021-12-27 21:37:00 +00:00
|
|
|
const auto id = item->fullId();
|
|
|
|
auto &api = _owner->session().api();
|
|
|
|
auto i = _sentRequests.find(id);
|
|
|
|
if (i != end(_sentRequests)) {
|
|
|
|
api.request(i->second).cancel();
|
|
|
|
} else {
|
|
|
|
i = _sentRequests.emplace(id).first;
|
|
|
|
}
|
2022-08-25 20:56:38 +00:00
|
|
|
const auto chosen = item->chosenReactions();
|
|
|
|
using Flag = MTPmessages_SendReaction::Flag;
|
|
|
|
const auto flags = (chosen.empty() ? Flag(0) : Flag::f_reaction)
|
|
|
|
| (addToRecent ? Flag::f_add_to_recent : Flag(0));
|
2021-12-27 21:37:00 +00:00
|
|
|
i->second = api.request(MTPmessages_SendReaction(
|
|
|
|
MTP_flags(flags),
|
|
|
|
item->history()->peer->input,
|
|
|
|
MTP_int(id.msg),
|
2022-08-25 20:56:38 +00:00
|
|
|
MTP_vector<MTPReaction>(chosen | ranges::views::transform(
|
|
|
|
ReactionToMTP
|
|
|
|
) | ranges::to<QVector<MTPReaction>>())
|
2021-12-27 21:37:00 +00:00
|
|
|
)).done([=](const MTPUpdates &result) {
|
|
|
|
_sentRequests.remove(id);
|
|
|
|
_owner->session().api().applyUpdates(result);
|
|
|
|
}).fail([=](const MTP::Error &error) {
|
|
|
|
_sentRequests.remove(id);
|
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
2021-12-28 18:20:32 +00:00
|
|
|
void Reactions::poll(not_null<HistoryItem*> item, crl::time now) {
|
|
|
|
// Group them by one second.
|
|
|
|
const auto last = item->lastReactionsRefreshTime();
|
|
|
|
const auto grouped = ((last + 999) / 1000) * 1000;
|
|
|
|
if (!grouped || item->history()->peer->isUser()) {
|
|
|
|
// First reaction always edits message.
|
|
|
|
return;
|
|
|
|
} else if (const auto left = grouped + kPollEach - now; left > 0) {
|
|
|
|
if (!_repaintItems.contains(item)) {
|
|
|
|
_repaintItems.emplace(item, grouped + kPollEach);
|
|
|
|
if (!_repaintTimer.isActive()
|
|
|
|
|| _repaintTimer.remainingTime() > left) {
|
|
|
|
_repaintTimer.callOnce(left);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (!_pollingItems.contains(item)) {
|
|
|
|
if (_pollItems.empty() && !_pollRequestId) {
|
|
|
|
crl::on_main(&_owner->session(), [=] {
|
|
|
|
pollCollected();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
_pollItems.emplace(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-28 22:06:01 +00:00
|
|
|
void Reactions::updateAllInHistory(not_null<PeerData*> peer, bool enabled) {
|
|
|
|
if (const auto history = _owner->historyLoaded(peer)) {
|
|
|
|
history->reactionsEnabledChanged(enabled);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-25 20:56:38 +00:00
|
|
|
void Reactions::clearTemporary() {
|
|
|
|
_temporary.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
Reaction *Reactions::lookupTemporary(const ReactionId &id) {
|
|
|
|
if (const auto emoji = id.emoji(); !emoji.isEmpty()) {
|
|
|
|
const auto i = ranges::find(_available, id, &Reaction::id);
|
|
|
|
return (i != end(_available)) ? &*i : nullptr;
|
|
|
|
} else if (const auto customId = id.custom()) {
|
|
|
|
if (const auto i = _temporary.find(customId); i != end(_temporary)) {
|
|
|
|
return &i->second;
|
|
|
|
}
|
|
|
|
const auto document = _owner->document(customId);
|
|
|
|
if (document->sticker()) {
|
|
|
|
return &_temporary.emplace(
|
|
|
|
customId,
|
|
|
|
CustomReaction(document)).first->second;
|
|
|
|
}
|
|
|
|
_owner->customEmojiManager().resolve(
|
|
|
|
customId,
|
|
|
|
resolveListener());
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-12-28 18:20:32 +00:00
|
|
|
void Reactions::repaintCollected() {
|
|
|
|
const auto now = crl::now();
|
2022-01-19 15:52:13 +00:00
|
|
|
auto closest = crl::time();
|
2021-12-28 18:20:32 +00:00
|
|
|
for (auto i = begin(_repaintItems); i != end(_repaintItems);) {
|
|
|
|
if (i->second <= now) {
|
|
|
|
_owner->requestItemRepaint(i->first);
|
|
|
|
i = _repaintItems.erase(i);
|
|
|
|
} else {
|
|
|
|
if (!closest || i->second < closest) {
|
|
|
|
closest = i->second;
|
|
|
|
}
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (closest) {
|
|
|
|
_repaintTimer.callOnce(closest - now);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Reactions::pollCollected() {
|
|
|
|
auto toRequest = base::flat_map<not_null<PeerData*>, QVector<MTPint>>();
|
|
|
|
_pollingItems = std::move(_pollItems);
|
|
|
|
for (const auto &item : _pollingItems) {
|
|
|
|
toRequest[item->history()->peer].push_back(MTP_int(item->id));
|
|
|
|
}
|
|
|
|
auto &api = _owner->session().api();
|
|
|
|
for (const auto &[peer, ids] : toRequest) {
|
|
|
|
const auto finalize = [=] {
|
|
|
|
const auto now = crl::now();
|
|
|
|
for (const auto &item : base::take(_pollingItems)) {
|
|
|
|
const auto last = item->lastReactionsRefreshTime();
|
|
|
|
if (last && last + kPollEach <= now) {
|
|
|
|
item->updateReactions(nullptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_pollRequestId = 0;
|
|
|
|
if (!_pollItems.empty()) {
|
|
|
|
crl::on_main(&_owner->session(), [=] {
|
|
|
|
pollCollected();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
_pollRequestId = api.request(MTPmessages_GetMessagesReactions(
|
|
|
|
peer->input,
|
|
|
|
MTP_vector<MTPint>(ids)
|
|
|
|
)).done([=](const MTPUpdates &result) {
|
|
|
|
_owner->session().api().applyUpdates(result);
|
|
|
|
finalize();
|
|
|
|
}).fail([=] {
|
|
|
|
finalize();
|
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-27 21:37:00 +00:00
|
|
|
bool Reactions::sending(not_null<HistoryItem*> item) const {
|
|
|
|
return _sentRequests.contains(item->fullId());
|
2019-09-11 09:26:13 +00:00
|
|
|
}
|
|
|
|
|
2022-01-28 14:11:38 +00:00
|
|
|
bool Reactions::HasUnread(const MTPMessageReactions &data) {
|
|
|
|
return data.match([&](const MTPDmessageReactions &data) {
|
|
|
|
if (const auto &recent = data.vrecent_reactions()) {
|
|
|
|
for (const auto &one : recent->v) {
|
|
|
|
if (one.match([&](const MTPDmessagePeerReaction &data) {
|
|
|
|
return data.is_unread();
|
|
|
|
})) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void Reactions::CheckUnknownForUnread(
|
|
|
|
not_null<Session*> owner,
|
|
|
|
const MTPMessage &message) {
|
|
|
|
message.match([&](const MTPDmessage &data) {
|
|
|
|
if (data.vreactions() && HasUnread(*data.vreactions())) {
|
|
|
|
const auto peerId = peerFromMTP(data.vpeer_id());
|
|
|
|
if (const auto history = owner->historyLoaded(peerId)) {
|
|
|
|
owner->histories().requestDialogEntry(history);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, [](const auto &) {
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-09-11 09:26:13 +00:00
|
|
|
MessageReactions::MessageReactions(not_null<HistoryItem*> item)
|
|
|
|
: _item(item) {
|
|
|
|
}
|
|
|
|
|
2022-08-25 20:56:38 +00:00
|
|
|
void MessageReactions::add(const ReactionId &id, bool addToRecent) {
|
|
|
|
Expects(!id.empty());
|
|
|
|
|
2022-01-13 10:13:08 +00:00
|
|
|
const auto history = _item->history();
|
|
|
|
const auto self = history->session().user();
|
2022-08-30 07:08:19 +00:00
|
|
|
const auto myLimit = SentReactionsLimit(_item);
|
2022-08-25 20:56:38 +00:00
|
|
|
if (ranges::contains(chosen(), id)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto my = 0;
|
|
|
|
_list.erase(ranges::remove_if(_list, [&](MessageReaction &one) {
|
|
|
|
const auto removing = one.my && (my == myLimit || ++my == myLimit);
|
|
|
|
if (!removing) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
one.my = false;
|
|
|
|
const auto removed = !--one.count;
|
|
|
|
const auto j = _recent.find(one.id);
|
2022-01-13 10:13:08 +00:00
|
|
|
if (j != end(_recent)) {
|
2022-01-27 12:55:34 +00:00
|
|
|
j->second.erase(
|
|
|
|
ranges::remove(j->second, self, &RecentReaction::peer),
|
|
|
|
end(j->second));
|
2022-08-25 20:56:38 +00:00
|
|
|
if (j->second.empty()) {
|
2022-01-13 10:13:08 +00:00
|
|
|
_recent.erase(j);
|
2022-08-25 20:56:38 +00:00
|
|
|
} else {
|
|
|
|
Assert(!removed);
|
2022-01-13 10:13:08 +00:00
|
|
|
}
|
|
|
|
}
|
2022-08-25 20:56:38 +00:00
|
|
|
return removed;
|
|
|
|
}), end(_list));
|
2022-08-26 09:35:19 +00:00
|
|
|
if (_item->canViewReactions() || history->peer->isUser()) {
|
2022-08-25 20:56:38 +00:00
|
|
|
auto &list = _recent[id];
|
|
|
|
list.insert(begin(list), RecentReaction{ self });
|
2019-09-11 09:26:13 +00:00
|
|
|
}
|
2022-08-25 20:56:38 +00:00
|
|
|
const auto i = ranges::find(_list, id, &MessageReaction::id);
|
|
|
|
if (i != end(_list)) {
|
|
|
|
i->my = true;
|
|
|
|
++i->count;
|
|
|
|
std::rotate(i, i + 1, end(_list));
|
|
|
|
} else {
|
|
|
|
_list.push_back({ .id = id, .count = 1, .my = true });
|
2019-09-11 09:26:13 +00:00
|
|
|
}
|
2022-01-13 10:13:08 +00:00
|
|
|
auto &owner = history->owner();
|
2022-08-25 20:56:38 +00:00
|
|
|
owner.reactions().send(_item, addToRecent);
|
2021-12-28 18:20:32 +00:00
|
|
|
owner.notifyItemDataChange(_item);
|
2019-09-11 09:26:13 +00:00
|
|
|
}
|
|
|
|
|
2022-08-25 20:56:38 +00:00
|
|
|
void MessageReactions::remove(const ReactionId &id) {
|
|
|
|
const auto history = _item->history();
|
|
|
|
const auto self = history->session().user();
|
|
|
|
const auto i = ranges::find(_list, id, &MessageReaction::id);
|
|
|
|
const auto j = _recent.find(id);
|
|
|
|
if (i == end(_list)) {
|
|
|
|
Assert(j == end(_recent));
|
|
|
|
return;
|
|
|
|
} else if (!i->my) {
|
|
|
|
Assert(j == end(_recent)
|
|
|
|
|| !ranges::contains(j->second, self, &RecentReaction::peer));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
i->my = false;
|
|
|
|
const auto removed = !--i->count;
|
|
|
|
if (removed) {
|
|
|
|
_list.erase(i);
|
|
|
|
}
|
|
|
|
if (j != end(_recent)) {
|
|
|
|
j->second.erase(
|
|
|
|
ranges::remove(j->second, self, &RecentReaction::peer),
|
|
|
|
end(j->second));
|
|
|
|
if (j->second.empty()) {
|
|
|
|
_recent.erase(j);
|
|
|
|
} else {
|
|
|
|
Assert(!removed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
auto &owner = history->owner();
|
|
|
|
owner.reactions().send(_item, false);
|
|
|
|
owner.notifyItemDataChange(_item);
|
2021-12-21 13:23:59 +00:00
|
|
|
}
|
|
|
|
|
2022-01-28 07:44:12 +00:00
|
|
|
bool MessageReactions::checkIfChanged(
|
|
|
|
const QVector<MTPReactionCount> &list,
|
|
|
|
const QVector<MTPMessagePeerReaction> &recent) const {
|
|
|
|
auto &owner = _item->history()->owner();
|
|
|
|
if (owner.reactions().sending(_item)) {
|
|
|
|
// We'll apply non-stale data from the request response.
|
|
|
|
return false;
|
|
|
|
}
|
2022-08-16 11:28:17 +00:00
|
|
|
auto existing = base::flat_set<ReactionId>();
|
2022-01-28 07:44:12 +00:00
|
|
|
for (const auto &count : list) {
|
|
|
|
const auto changed = count.match([&](const MTPDreactionCount &data) {
|
2022-08-25 20:56:38 +00:00
|
|
|
const auto id = ReactionFromMTP(data.vreaction());
|
2022-01-28 07:44:12 +00:00
|
|
|
const auto nowCount = data.vcount().v;
|
2022-08-25 20:56:38 +00:00
|
|
|
const auto i = ranges::find(_list, id, &MessageReaction::id);
|
|
|
|
const auto wasCount = (i != end(_list)) ? i->count : 0;
|
2022-01-28 07:44:12 +00:00
|
|
|
if (wasCount != nowCount) {
|
|
|
|
return true;
|
|
|
|
}
|
2022-08-25 20:56:38 +00:00
|
|
|
existing.emplace(id);
|
2022-01-28 07:44:12 +00:00
|
|
|
return false;
|
|
|
|
});
|
|
|
|
if (changed) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2022-08-25 20:56:38 +00:00
|
|
|
for (const auto &reaction : _list) {
|
|
|
|
if (!existing.contains(reaction.id)) {
|
2022-01-28 07:44:12 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2022-08-16 11:28:17 +00:00
|
|
|
auto parsed = base::flat_map<ReactionId, std::vector<RecentReaction>>();
|
2022-01-28 07:44:12 +00:00
|
|
|
for (const auto &reaction : recent) {
|
|
|
|
reaction.match([&](const MTPDmessagePeerReaction &data) {
|
2022-08-25 20:56:38 +00:00
|
|
|
const auto id = ReactionFromMTP(data.vreaction());
|
|
|
|
if (ranges::contains(_list, id, &MessageReaction::id)) {
|
|
|
|
parsed[id].push_back(RecentReaction{
|
2022-01-28 07:44:12 +00:00
|
|
|
.peer = owner.peer(peerFromMTP(data.vpeer_id())),
|
|
|
|
.unread = data.is_unread(),
|
|
|
|
.big = data.is_big(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return !ranges::equal(_recent, parsed, [](
|
|
|
|
const auto &a,
|
|
|
|
const auto &b) {
|
|
|
|
return ranges::equal(a.second, b.second, [](
|
|
|
|
const RecentReaction &a,
|
|
|
|
const RecentReaction &b) {
|
|
|
|
return (a.peer == b.peer) && (a.big == b.big);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-01-31 08:19:21 +00:00
|
|
|
bool MessageReactions::change(
|
2019-09-11 09:26:13 +00:00
|
|
|
const QVector<MTPReactionCount> &list,
|
2022-01-21 12:48:14 +00:00
|
|
|
const QVector<MTPMessagePeerReaction> &recent,
|
2019-09-11 09:26:13 +00:00
|
|
|
bool ignoreChosen) {
|
2022-01-13 10:13:08 +00:00
|
|
|
auto &owner = _item->history()->owner();
|
|
|
|
if (owner.reactions().sending(_item)) {
|
2019-09-11 09:26:13 +00:00
|
|
|
// We'll apply non-stale data from the request response.
|
2022-01-31 08:19:21 +00:00
|
|
|
return false;
|
2019-09-11 09:26:13 +00:00
|
|
|
}
|
2021-12-28 18:20:32 +00:00
|
|
|
auto changed = false;
|
2022-08-16 11:28:17 +00:00
|
|
|
auto existing = base::flat_set<ReactionId>();
|
2022-08-25 20:56:38 +00:00
|
|
|
auto order = base::flat_map<ReactionId, int>();
|
2019-09-11 09:26:13 +00:00
|
|
|
for (const auto &count : list) {
|
|
|
|
count.match([&](const MTPDreactionCount &data) {
|
2022-08-25 20:56:38 +00:00
|
|
|
const auto id = ReactionFromMTP(data.vreaction());
|
|
|
|
const auto &chosen = data.vchosen_order();
|
|
|
|
if (!ignoreChosen && chosen) {
|
|
|
|
order[id] = chosen->v;
|
2021-12-28 18:20:32 +00:00
|
|
|
}
|
2022-08-25 20:56:38 +00:00
|
|
|
const auto i = ranges::find(_list, id, &MessageReaction::id);
|
2021-12-28 18:20:32 +00:00
|
|
|
const auto nowCount = data.vcount().v;
|
2022-08-25 20:56:38 +00:00
|
|
|
if (i == end(_list)) {
|
2021-12-28 18:20:32 +00:00
|
|
|
changed = true;
|
2022-08-25 20:56:38 +00:00
|
|
|
_list.push_back({
|
|
|
|
.id = id,
|
|
|
|
.count = nowCount,
|
|
|
|
.my = (!ignoreChosen && chosen)
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
const auto nowMy = ignoreChosen ? i->my : chosen.has_value();
|
|
|
|
if (i->count != nowCount || i->my != nowMy) {
|
|
|
|
i->count = nowCount;
|
|
|
|
i->my = nowMy;
|
|
|
|
changed = true;
|
|
|
|
}
|
2019-09-11 09:26:13 +00:00
|
|
|
}
|
2022-08-25 20:56:38 +00:00
|
|
|
existing.emplace(id);
|
2019-09-11 09:26:13 +00:00
|
|
|
});
|
|
|
|
}
|
2022-08-25 20:56:38 +00:00
|
|
|
if (!ignoreChosen && !order.empty()) {
|
|
|
|
const auto min = std::numeric_limits<int>::min();
|
|
|
|
const auto proj = [&](const MessageReaction &reaction) {
|
|
|
|
return reaction.my ? order[reaction.id] : min;
|
|
|
|
};
|
|
|
|
const auto correctOrder = [&] {
|
|
|
|
auto previousOrder = min;
|
|
|
|
for (const auto &reaction : _list) {
|
|
|
|
const auto nowOrder = proj(reaction);
|
|
|
|
if (nowOrder < previousOrder) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
previousOrder = nowOrder;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}();
|
|
|
|
if (!correctOrder) {
|
|
|
|
changed = true;
|
|
|
|
ranges::sort(_list, std::less(), proj);
|
|
|
|
}
|
|
|
|
}
|
2019-09-11 09:26:13 +00:00
|
|
|
if (_list.size() != existing.size()) {
|
2021-12-28 18:20:32 +00:00
|
|
|
changed = true;
|
2019-09-11 09:26:13 +00:00
|
|
|
for (auto i = begin(_list); i != end(_list);) {
|
2022-08-25 20:56:38 +00:00
|
|
|
if (!existing.contains(i->id)) {
|
2019-09-11 09:26:13 +00:00
|
|
|
i = _list.erase(i);
|
|
|
|
} else {
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-08-16 11:28:17 +00:00
|
|
|
auto parsed = base::flat_map<ReactionId, std::vector<RecentReaction>>();
|
2022-01-13 10:13:08 +00:00
|
|
|
for (const auto &reaction : recent) {
|
2022-01-21 12:48:14 +00:00
|
|
|
reaction.match([&](const MTPDmessagePeerReaction &data) {
|
2022-08-25 20:56:38 +00:00
|
|
|
const auto id = ReactionFromMTP(data.vreaction());
|
|
|
|
const auto i = ranges::find(_list, id, &MessageReaction::id);
|
|
|
|
if (i != end(_list)) {
|
|
|
|
auto &list = parsed[id];
|
|
|
|
if (list.size() < i->count) {
|
|
|
|
list.push_back(RecentReaction{
|
|
|
|
.peer = owner.peer(peerFromMTP(data.vpeer_id())),
|
|
|
|
.unread = data.is_unread(),
|
|
|
|
.big = data.is_big(),
|
|
|
|
});
|
|
|
|
}
|
2022-01-13 10:13:08 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (_recent != parsed) {
|
|
|
|
_recent = std::move(parsed);
|
|
|
|
changed = true;
|
|
|
|
}
|
2022-01-31 08:19:21 +00:00
|
|
|
return changed;
|
2019-09-11 09:26:13 +00:00
|
|
|
}
|
|
|
|
|
2022-08-25 20:56:38 +00:00
|
|
|
const std::vector<MessageReaction> &MessageReactions::list() const {
|
2019-09-11 09:26:13 +00:00
|
|
|
return _list;
|
|
|
|
}
|
|
|
|
|
2022-01-13 10:13:08 +00:00
|
|
|
auto MessageReactions::recent() const
|
2022-08-16 11:28:17 +00:00
|
|
|
-> const base::flat_map<ReactionId, std::vector<RecentReaction>> & {
|
2022-01-13 10:13:08 +00:00
|
|
|
return _recent;
|
|
|
|
}
|
|
|
|
|
2021-12-21 13:23:59 +00:00
|
|
|
bool MessageReactions::empty() const {
|
|
|
|
return _list.empty();
|
|
|
|
}
|
|
|
|
|
2022-01-31 15:29:46 +00:00
|
|
|
bool MessageReactions::hasUnread() const {
|
2022-01-27 14:38:59 +00:00
|
|
|
for (auto &[emoji, list] : _recent) {
|
2022-01-31 15:29:46 +00:00
|
|
|
if (ranges::contains(list, true, &RecentReaction::unread)) {
|
|
|
|
return true;
|
2022-01-27 14:38:59 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-31 15:29:46 +00:00
|
|
|
return false;
|
2022-01-27 14:38:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MessageReactions::markRead() {
|
|
|
|
for (auto &[emoji, list] : _recent) {
|
|
|
|
for (auto &reaction : list) {
|
|
|
|
reaction.unread = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-25 20:56:38 +00:00
|
|
|
std::vector<ReactionId> MessageReactions::chosen() const {
|
|
|
|
return _list
|
|
|
|
| ranges::views::filter(&MessageReaction::my)
|
|
|
|
| ranges::views::transform(&MessageReaction::id)
|
|
|
|
| ranges::to_vector;
|
2019-09-11 09:26:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Data
|