2016-09-09 15:52:46 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2016-09-09 15:52:46 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2016-09-09 15:52:46 +00:00
|
|
|
*/
|
2020-06-08 17:24:36 +00:00
|
|
|
#include "data/stickers/data_stickers.h"
|
2016-09-09 15:52:46 +00:00
|
|
|
|
2020-06-09 16:57:05 +00:00
|
|
|
#include "api/api_hash.h"
|
2017-09-26 11:49:16 +00:00
|
|
|
#include "data/data_document.h"
|
2018-01-04 10:22:53 +00:00
|
|
|
#include "data/data_session.h"
|
2020-06-08 17:24:36 +00:00
|
|
|
#include "data/data_user.h"
|
2021-10-18 21:36:55 +00:00
|
|
|
#include "ui/boxes/confirm_box.h"
|
2017-04-13 08:27:10 +00:00
|
|
|
#include "lang/lang_keys.h"
|
2020-06-08 17:24:36 +00:00
|
|
|
#include "history/history.h"
|
|
|
|
#include "history/history_item.h"
|
|
|
|
#include "history/history_item_components.h"
|
2016-09-09 15:52:46 +00:00
|
|
|
#include "apiwrap.h"
|
2020-06-09 16:57:05 +00:00
|
|
|
#include "storage/storage_account.h"
|
2020-06-18 18:04:16 +00:00
|
|
|
#include "core/application.h"
|
|
|
|
#include "core/core_settings.h"
|
2019-07-24 11:45:24 +00:00
|
|
|
#include "main/main_session.h"
|
2020-06-17 09:36:25 +00:00
|
|
|
#include "mtproto/mtproto_config.h"
|
2016-12-23 13:21:01 +00:00
|
|
|
#include "ui/toast/toast.h"
|
2020-05-28 10:00:51 +00:00
|
|
|
#include "ui/image/image_location_factory.h"
|
2019-07-10 17:28:33 +00:00
|
|
|
#include "base/unixtime.h"
|
2022-02-28 03:59:36 +00:00
|
|
|
#include "boxes/abstract_box.h" // Ui::show().
|
2017-04-06 14:38:10 +00:00
|
|
|
#include "styles/style_chat_helpers.h"
|
2016-09-09 15:52:46 +00:00
|
|
|
|
2020-06-08 17:24:36 +00:00
|
|
|
namespace Data {
|
2020-08-19 14:04:05 +00:00
|
|
|
namespace {
|
|
|
|
|
2021-07-09 06:08:58 +00:00
|
|
|
using SetFlag = StickersSetFlag;
|
|
|
|
|
2020-08-19 14:04:05 +00:00
|
|
|
void RemoveFromSet(
|
|
|
|
StickersSets &sets,
|
|
|
|
not_null<DocumentData*> document,
|
|
|
|
uint64 setId) {
|
|
|
|
const auto it = sets.find(setId);
|
|
|
|
if (it == sets.end()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto set = it->second.get();
|
|
|
|
const auto index = set->stickers.indexOf(document);
|
|
|
|
if (index < 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
set->stickers.removeAt(index);
|
|
|
|
if (!set->dates.empty()) {
|
|
|
|
set->dates.erase(set->dates.begin() + index);
|
|
|
|
}
|
|
|
|
for (auto i = set->emoji.begin(); i != set->emoji.end();) {
|
|
|
|
const auto index = i->indexOf(document);
|
|
|
|
if (index >= 0) {
|
|
|
|
i->removeAt(index);
|
|
|
|
if (i->empty()) {
|
|
|
|
i = set->emoji.erase(i);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
if (set->stickers.empty()) {
|
|
|
|
sets.erase(it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2020-06-08 17:24:36 +00:00
|
|
|
Stickers::Stickers(not_null<Session*> owner) : _owner(owner) {
|
|
|
|
}
|
|
|
|
|
|
|
|
Session &Stickers::owner() const {
|
|
|
|
return *_owner;
|
|
|
|
}
|
|
|
|
|
|
|
|
Main::Session &Stickers::session() const {
|
|
|
|
return _owner->session();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Stickers::notifyUpdated() {
|
|
|
|
_updated.fire({});
|
|
|
|
}
|
|
|
|
|
|
|
|
rpl::producer<> Stickers::updated() const {
|
|
|
|
return _updated.events();
|
|
|
|
}
|
|
|
|
|
2021-03-25 11:05:33 +00:00
|
|
|
void Stickers::notifyRecentUpdated(Recent recent) {
|
|
|
|
_recentUpdated.fire(std::move(recent));
|
2020-06-08 17:24:36 +00:00
|
|
|
}
|
|
|
|
|
2021-03-25 11:05:33 +00:00
|
|
|
rpl::producer<Stickers::Recent> Stickers::recentUpdated() const {
|
2020-06-08 17:24:36 +00:00
|
|
|
return _recentUpdated.events();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Stickers::notifySavedGifsUpdated() {
|
|
|
|
_savedGifsUpdated.fire({});
|
|
|
|
}
|
|
|
|
|
|
|
|
rpl::producer<> Stickers::savedGifsUpdated() const {
|
|
|
|
return _savedGifsUpdated.events();
|
|
|
|
}
|
2019-06-27 16:57:32 +00:00
|
|
|
|
2021-03-29 22:28:19 +00:00
|
|
|
void Stickers::notifyStickerSetInstalled(uint64 setId) {
|
|
|
|
_stickerSetInstalled.fire(std::move(setId));
|
|
|
|
}
|
|
|
|
|
|
|
|
rpl::producer<uint64> Stickers::stickerSetInstalled() const {
|
|
|
|
return _stickerSetInstalled.events();
|
|
|
|
}
|
|
|
|
|
2020-06-12 12:12:34 +00:00
|
|
|
void Stickers::incrementSticker(not_null<DocumentData*> document) {
|
2021-07-08 16:42:57 +00:00
|
|
|
if (!document->sticker() || !document->sticker()->set) {
|
2020-06-12 12:12:34 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool writeRecentStickers = false;
|
|
|
|
auto &sets = setsRef();
|
|
|
|
auto it = sets.find(Data::Stickers::CloudRecentSetId);
|
|
|
|
if (it == sets.cend()) {
|
|
|
|
if (it == sets.cend()) {
|
|
|
|
it = sets.emplace(
|
|
|
|
Data::Stickers::CloudRecentSetId,
|
|
|
|
std::make_unique<Data::StickersSet>(
|
|
|
|
&session().data(),
|
|
|
|
Data::Stickers::CloudRecentSetId,
|
2021-08-25 08:15:05 +00:00
|
|
|
uint64(0), // accessHash
|
|
|
|
uint64(0), // hash
|
2020-06-12 12:12:34 +00:00
|
|
|
tr::lng_recent_stickers(tr::now),
|
|
|
|
QString(),
|
|
|
|
0, // count
|
2021-07-09 06:08:58 +00:00
|
|
|
SetFlag::Special,
|
2020-06-12 12:12:34 +00:00
|
|
|
TimeId(0))).first;
|
|
|
|
} else {
|
|
|
|
it->second->title = tr::lng_recent_stickers(tr::now);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const auto set = it->second.get();
|
|
|
|
auto removedFromEmoji = std::vector<not_null<EmojiPtr>>();
|
|
|
|
auto index = set->stickers.indexOf(document);
|
|
|
|
if (index > 0) {
|
|
|
|
if (set->dates.empty()) {
|
|
|
|
session().api().requestRecentStickersForce();
|
|
|
|
} else {
|
|
|
|
Assert(set->dates.size() == set->stickers.size());
|
|
|
|
set->dates.erase(set->dates.begin() + index);
|
|
|
|
}
|
|
|
|
set->stickers.removeAt(index);
|
|
|
|
for (auto i = set->emoji.begin(); i != set->emoji.end();) {
|
|
|
|
if (const auto index = i->indexOf(document); index >= 0) {
|
|
|
|
removedFromEmoji.emplace_back(i.key());
|
|
|
|
i->removeAt(index);
|
|
|
|
if (i->isEmpty()) {
|
|
|
|
i = set->emoji.erase(i);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (index) {
|
|
|
|
if (set->dates.size() == set->stickers.size()) {
|
|
|
|
set->dates.insert(set->dates.begin(), base::unixtime::now());
|
|
|
|
}
|
|
|
|
set->stickers.push_front(document);
|
|
|
|
if (const auto emojiList = getEmojiListFromSet(document)) {
|
2021-09-08 10:53:54 +00:00
|
|
|
for (const auto &emoji : *emojiList) {
|
2020-06-12 12:12:34 +00:00
|
|
|
set->emoji[emoji].push_front(document);
|
|
|
|
}
|
|
|
|
} else if (!removedFromEmoji.empty()) {
|
|
|
|
for (const auto emoji : removedFromEmoji) {
|
|
|
|
set->emoji[emoji].push_front(document);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
session().api().requestRecentStickersForce();
|
|
|
|
}
|
|
|
|
|
|
|
|
writeRecentStickers = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove that sticker from old recent, now it is in cloud recent stickers.
|
|
|
|
bool writeOldRecent = false;
|
|
|
|
auto &recent = getRecentPack();
|
|
|
|
for (auto i = recent.begin(), e = recent.end(); i != e; ++i) {
|
|
|
|
if (i->first == document) {
|
|
|
|
writeOldRecent = true;
|
|
|
|
recent.erase(i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-06-17 09:36:25 +00:00
|
|
|
while (!recent.isEmpty()
|
|
|
|
&& (set->stickers.size() + recent.size()
|
|
|
|
> session().serverConfig().stickersRecentLimit)) {
|
2020-06-12 12:12:34 +00:00
|
|
|
writeOldRecent = true;
|
|
|
|
recent.pop_back();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (writeOldRecent) {
|
2020-06-18 12:47:09 +00:00
|
|
|
session().saveSettings();
|
2020-06-12 12:12:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove that sticker from custom stickers, now it is in cloud recent stickers.
|
|
|
|
bool writeInstalledStickers = false;
|
|
|
|
auto customIt = sets.find(Data::Stickers::CustomSetId);
|
|
|
|
if (customIt != sets.cend()) {
|
|
|
|
const auto custom = customIt->second.get();
|
|
|
|
int removeIndex = custom->stickers.indexOf(document);
|
|
|
|
if (removeIndex >= 0) {
|
|
|
|
custom->stickers.removeAt(removeIndex);
|
|
|
|
if (custom->stickers.isEmpty()) {
|
|
|
|
sets.erase(customIt);
|
|
|
|
}
|
|
|
|
writeInstalledStickers = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (writeInstalledStickers) {
|
|
|
|
session().local().writeInstalledStickers();
|
|
|
|
}
|
|
|
|
if (writeRecentStickers) {
|
|
|
|
session().local().writeRecentStickers();
|
|
|
|
}
|
|
|
|
notifyRecentUpdated();
|
|
|
|
}
|
|
|
|
|
2020-06-08 17:24:36 +00:00
|
|
|
void Stickers::addSavedGif(not_null<DocumentData*> document) {
|
|
|
|
const auto index = _savedGifs.indexOf(document);
|
|
|
|
if (!index) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (index > 0) {
|
|
|
|
_savedGifs.remove(index);
|
|
|
|
}
|
|
|
|
_savedGifs.push_front(document);
|
2020-06-17 09:36:25 +00:00
|
|
|
if (_savedGifs.size() > session().serverConfig().savedGifsLimit) {
|
2020-06-08 17:24:36 +00:00
|
|
|
_savedGifs.pop_back();
|
|
|
|
}
|
2020-06-09 16:57:05 +00:00
|
|
|
session().local().writeSavedGifs();
|
2020-06-08 17:24:36 +00:00
|
|
|
|
|
|
|
notifySavedGifsUpdated();
|
|
|
|
setLastSavedGifsUpdate(0);
|
|
|
|
session().api().updateStickers();
|
|
|
|
}
|
2016-09-09 15:52:46 +00:00
|
|
|
|
2020-06-08 17:24:36 +00:00
|
|
|
void Stickers::checkSavedGif(not_null<HistoryItem*> item) {
|
|
|
|
if (item->Has<HistoryMessageForwarded>()
|
|
|
|
|| (!item->out()
|
|
|
|
&& item->history()->peer != session().user())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (const auto media = item->media()) {
|
|
|
|
if (const auto document = media->document()) {
|
|
|
|
if (document->isGifv()) {
|
|
|
|
addSavedGif(document);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Stickers::applyArchivedResult(
|
|
|
|
const MTPDmessages_stickerSetInstallResultArchive &d) {
|
2019-07-05 13:38:38 +00:00
|
|
|
auto &v = d.vsets().v;
|
2020-06-08 17:24:36 +00:00
|
|
|
StickersSetsOrder archived;
|
2016-09-09 15:52:46 +00:00
|
|
|
archived.reserve(v.size());
|
|
|
|
QMap<uint64, uint64> setsToRequest;
|
2021-03-30 15:51:45 +00:00
|
|
|
|
|
|
|
auto masksCount = 0;
|
|
|
|
auto stickersCount = 0;
|
2020-05-28 10:00:51 +00:00
|
|
|
for (const auto &stickerSet : v) {
|
2016-09-09 15:52:46 +00:00
|
|
|
const MTPDstickerSet *setData = nullptr;
|
|
|
|
switch (stickerSet.type()) {
|
|
|
|
case mtpc_stickerSetCovered: {
|
|
|
|
auto &d = stickerSet.c_stickerSetCovered();
|
2019-07-05 13:38:38 +00:00
|
|
|
if (d.vset().type() == mtpc_stickerSet) {
|
|
|
|
setData = &d.vset().c_stickerSet();
|
2016-09-09 15:52:46 +00:00
|
|
|
}
|
|
|
|
} break;
|
|
|
|
case mtpc_stickerSetMultiCovered: {
|
|
|
|
auto &d = stickerSet.c_stickerSetMultiCovered();
|
2019-07-05 13:38:38 +00:00
|
|
|
if (d.vset().type() == mtpc_stickerSet) {
|
|
|
|
setData = &d.vset().c_stickerSet();
|
2016-09-09 15:52:46 +00:00
|
|
|
}
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
if (setData) {
|
2020-06-08 17:24:36 +00:00
|
|
|
auto set = feedSet(*setData);
|
2016-09-09 15:52:46 +00:00
|
|
|
if (set->stickers.isEmpty()) {
|
2021-08-25 08:15:05 +00:00
|
|
|
setsToRequest.insert(set->id, set->accessHash);
|
2016-09-09 15:52:46 +00:00
|
|
|
}
|
2021-07-09 06:08:58 +00:00
|
|
|
const auto masks = !!(set->flags & SetFlag::Masks);
|
2021-03-30 15:51:45 +00:00
|
|
|
(masks ? masksCount : stickersCount)++;
|
|
|
|
auto &order = masks ? maskSetsOrderRef() : setsOrderRef();
|
|
|
|
const auto index = order.indexOf(set->id);
|
2016-09-09 15:52:46 +00:00
|
|
|
if (index >= 0) {
|
|
|
|
order.removeAt(index);
|
|
|
|
}
|
|
|
|
archived.push_back(set->id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!setsToRequest.isEmpty()) {
|
|
|
|
for (auto i = setsToRequest.cbegin(), e = setsToRequest.cend(); i != e; ++i) {
|
2020-06-08 17:24:36 +00:00
|
|
|
session().api().scheduleStickerSetRequest(i.key(), i.value());
|
2016-09-09 15:52:46 +00:00
|
|
|
}
|
2020-06-08 17:24:36 +00:00
|
|
|
session().api().requestStickerSets();
|
2016-09-09 15:52:46 +00:00
|
|
|
}
|
2021-03-30 15:51:45 +00:00
|
|
|
if (stickersCount) {
|
|
|
|
session().local().writeInstalledStickers();
|
|
|
|
session().local().writeArchivedStickers();
|
|
|
|
}
|
|
|
|
if (masksCount) {
|
|
|
|
session().local().writeInstalledMasks();
|
|
|
|
session().local().writeArchivedMasks();
|
|
|
|
}
|
2016-12-23 13:21:01 +00:00
|
|
|
|
2020-04-29 14:52:40 +00:00
|
|
|
Ui::Toast::Show(Ui::Toast::Config{
|
|
|
|
.text = { tr::lng_stickers_packs_archived(tr::now) },
|
|
|
|
.st = &st::stickersToast,
|
|
|
|
.multiline = true,
|
|
|
|
});
|
2020-06-08 17:24:36 +00:00
|
|
|
//Ui::show(
|
|
|
|
// Box<StickersBox>(archived, &session()),
|
|
|
|
// Ui::LayerOption::KeepOther);
|
2016-09-09 15:52:46 +00:00
|
|
|
|
2020-06-08 17:24:36 +00:00
|
|
|
notifyUpdated();
|
2016-09-09 15:52:46 +00:00
|
|
|
}
|
|
|
|
|
2020-06-08 17:24:36 +00:00
|
|
|
void Stickers::installLocally(uint64 setId) {
|
|
|
|
auto &sets = setsRef();
|
2016-09-09 15:52:46 +00:00
|
|
|
auto it = sets.find(setId);
|
|
|
|
if (it == sets.end()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-28 10:00:51 +00:00
|
|
|
const auto set = it->second.get();
|
|
|
|
auto flags = set->flags;
|
2021-07-09 06:08:58 +00:00
|
|
|
set->flags &= ~(SetFlag::Archived | SetFlag::Unread);
|
|
|
|
set->flags |= SetFlag::Installed;
|
2020-05-28 10:00:51 +00:00
|
|
|
set->installDate = base::unixtime::now();
|
|
|
|
auto changedFlags = flags ^ set->flags;
|
2016-09-09 15:52:46 +00:00
|
|
|
|
2021-07-09 06:08:58 +00:00
|
|
|
const auto masks = !!(flags & SetFlag::Masks);
|
2021-03-30 15:51:45 +00:00
|
|
|
auto &order = masks ? maskSetsOrderRef() : setsOrderRef();
|
2016-09-09 15:52:46 +00:00
|
|
|
int insertAtIndex = 0, currentIndex = order.indexOf(setId);
|
|
|
|
if (currentIndex != insertAtIndex) {
|
|
|
|
if (currentIndex > 0) {
|
|
|
|
order.removeAt(currentIndex);
|
|
|
|
}
|
|
|
|
order.insert(insertAtIndex, setId);
|
|
|
|
}
|
|
|
|
|
2020-05-28 10:00:51 +00:00
|
|
|
auto customIt = sets.find(CustomSetId);
|
|
|
|
if (customIt != sets.cend()) {
|
|
|
|
const auto custom = customIt->second.get();
|
|
|
|
for (const auto sticker : std::as_const(set->stickers)) {
|
2016-09-09 15:52:46 +00:00
|
|
|
int removeIndex = custom->stickers.indexOf(sticker);
|
|
|
|
if (removeIndex >= 0) custom->stickers.removeAt(removeIndex);
|
|
|
|
}
|
|
|
|
if (custom->stickers.isEmpty()) {
|
2020-05-28 10:00:51 +00:00
|
|
|
sets.erase(customIt);
|
2016-09-09 15:52:46 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-09 16:57:05 +00:00
|
|
|
session().local().writeInstalledStickers();
|
2021-07-09 06:08:58 +00:00
|
|
|
if (changedFlags & SetFlag::Unread) {
|
2020-06-09 16:57:05 +00:00
|
|
|
session().local().writeFeaturedStickers();
|
2018-01-25 12:28:48 +00:00
|
|
|
}
|
2021-07-09 06:08:58 +00:00
|
|
|
if (changedFlags & SetFlag::Archived) {
|
2021-03-30 15:51:45 +00:00
|
|
|
auto &archivedOrder = masks
|
|
|
|
? archivedMaskSetsOrderRef()
|
|
|
|
: archivedSetsOrderRef();
|
|
|
|
const auto index = archivedOrder.indexOf(setId);
|
2016-09-09 15:52:46 +00:00
|
|
|
if (index >= 0) {
|
2021-03-30 15:51:45 +00:00
|
|
|
archivedOrder.removeAt(index);
|
|
|
|
if (masks) {
|
|
|
|
session().local().writeArchivedMasks();
|
|
|
|
} else {
|
|
|
|
session().local().writeArchivedStickers();
|
|
|
|
}
|
2016-09-09 15:52:46 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-08 17:24:36 +00:00
|
|
|
notifyUpdated();
|
2016-09-09 15:52:46 +00:00
|
|
|
}
|
|
|
|
|
2020-06-08 17:24:36 +00:00
|
|
|
void Stickers::undoInstallLocally(uint64 setId) {
|
|
|
|
const auto &sets = this->sets();
|
2020-05-31 08:18:24 +00:00
|
|
|
const auto it = sets.find(setId);
|
2016-09-09 15:52:46 +00:00
|
|
|
if (it == sets.end()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-28 10:00:51 +00:00
|
|
|
const auto set = it->second.get();
|
2021-07-09 06:08:58 +00:00
|
|
|
set->flags &= ~SetFlag::Installed;
|
2020-05-28 10:00:51 +00:00
|
|
|
set->installDate = TimeId(0);
|
2016-09-09 15:52:46 +00:00
|
|
|
|
2020-06-08 17:24:36 +00:00
|
|
|
auto &order = setsOrderRef();
|
2016-09-09 15:52:46 +00:00
|
|
|
int currentIndex = order.indexOf(setId);
|
|
|
|
if (currentIndex >= 0) {
|
|
|
|
order.removeAt(currentIndex);
|
|
|
|
}
|
|
|
|
|
2020-06-09 16:57:05 +00:00
|
|
|
session().local().writeInstalledStickers();
|
2020-06-08 17:24:36 +00:00
|
|
|
notifyUpdated();
|
2016-09-09 15:52:46 +00:00
|
|
|
|
2017-09-15 17:34:41 +00:00
|
|
|
Ui::show(
|
2022-02-27 08:23:20 +00:00
|
|
|
Ui::MakeInformBox(tr::lng_stickers_not_found()),
|
2019-09-18 11:19:05 +00:00
|
|
|
Ui::LayerOption::KeepOther);
|
2016-09-09 15:52:46 +00:00
|
|
|
}
|
|
|
|
|
2020-06-08 17:24:36 +00:00
|
|
|
bool Stickers::isFaved(not_null<const DocumentData*> document) {
|
|
|
|
const auto &sets = this->sets();
|
2020-05-28 10:00:51 +00:00
|
|
|
const auto it = sets.find(FavedSetId);
|
|
|
|
if (it == sets.cend()) {
|
2018-08-28 11:32:27 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-03-13 08:26:58 +00:00
|
|
|
for (const auto sticker : std::as_const(it->second->stickers)) {
|
2018-08-28 11:32:27 +00:00
|
|
|
if (sticker == document) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2017-08-02 20:57:49 +00:00
|
|
|
}
|
|
|
|
|
2020-06-08 17:24:36 +00:00
|
|
|
void Stickers::checkFavedLimit(StickersSet &set) {
|
2020-06-17 09:36:25 +00:00
|
|
|
if (set.stickers.size() <= session().serverConfig().stickersFavedLimit) {
|
2017-08-05 11:57:23 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto removing = set.stickers.back();
|
|
|
|
set.stickers.pop_back();
|
|
|
|
for (auto i = set.emoji.begin(); i != set.emoji.end();) {
|
|
|
|
auto index = i->indexOf(removing);
|
|
|
|
if (index >= 0) {
|
|
|
|
i->removeAt(index);
|
|
|
|
if (i->empty()) {
|
|
|
|
i = set.emoji.erase(i);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-08 17:24:36 +00:00
|
|
|
void Stickers::pushFavedToFront(
|
|
|
|
StickersSet &set,
|
2017-08-17 08:31:24 +00:00
|
|
|
not_null<DocumentData*> document,
|
|
|
|
const std::vector<not_null<EmojiPtr>> &emojiList) {
|
2017-08-15 14:34:30 +00:00
|
|
|
set.stickers.push_front(document);
|
|
|
|
for (auto emoji : emojiList) {
|
|
|
|
set.emoji[emoji].push_front(document);
|
|
|
|
}
|
2020-06-08 17:24:36 +00:00
|
|
|
checkFavedLimit(set);
|
2017-08-15 14:34:30 +00:00
|
|
|
}
|
|
|
|
|
2020-06-08 17:24:36 +00:00
|
|
|
void Stickers::moveFavedToFront(StickersSet &set, int index) {
|
2017-08-15 14:34:30 +00:00
|
|
|
Expects(index > 0 && index < set.stickers.size());
|
2020-06-08 17:24:36 +00:00
|
|
|
|
2017-08-15 14:34:30 +00:00
|
|
|
auto document = set.stickers[index];
|
|
|
|
while (index-- != 0) {
|
|
|
|
set.stickers[index + 1] = set.stickers[index];
|
|
|
|
}
|
|
|
|
set.stickers[0] = document;
|
|
|
|
for (auto &list : set.emoji) {
|
|
|
|
auto index = list.indexOf(document);
|
|
|
|
if (index > 0) {
|
|
|
|
while (index-- != 0) {
|
|
|
|
list[index + 1] = list[index];
|
|
|
|
}
|
|
|
|
list[0] = document;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-08 17:24:36 +00:00
|
|
|
void Stickers::setIsFaved(
|
2020-05-28 10:00:51 +00:00
|
|
|
not_null<DocumentData*> document,
|
2020-06-08 17:24:36 +00:00
|
|
|
std::optional<std::vector<not_null<EmojiPtr>>> emojiList) {
|
|
|
|
auto &sets = setsRef();
|
2017-08-02 20:57:49 +00:00
|
|
|
auto it = sets.find(FavedSetId);
|
2017-08-03 11:15:57 +00:00
|
|
|
if (it == sets.end()) {
|
2020-06-08 17:24:36 +00:00
|
|
|
it = sets.emplace(FavedSetId, std::make_unique<StickersSet>(
|
2020-05-28 10:00:51 +00:00
|
|
|
&document->owner(),
|
2018-01-25 12:28:48 +00:00
|
|
|
FavedSetId,
|
2021-08-25 08:15:05 +00:00
|
|
|
uint64(0), // accessHash
|
|
|
|
uint64(0), // hash
|
2018-01-25 12:28:48 +00:00
|
|
|
Lang::Hard::FavedSetTitle(),
|
|
|
|
QString(),
|
|
|
|
0, // count
|
2021-07-09 06:08:58 +00:00
|
|
|
SetFlag::Special,
|
2020-05-28 10:00:51 +00:00
|
|
|
TimeId(0))).first;
|
2017-08-03 11:15:57 +00:00
|
|
|
}
|
2020-05-28 10:00:51 +00:00
|
|
|
const auto set = it->second.get();
|
|
|
|
auto index = set->stickers.indexOf(document);
|
2017-08-03 11:15:57 +00:00
|
|
|
if (index == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (index > 0) {
|
2020-06-08 17:24:36 +00:00
|
|
|
moveFavedToFront(*set, index);
|
2017-08-03 11:15:57 +00:00
|
|
|
} else if (emojiList) {
|
2020-06-08 17:24:36 +00:00
|
|
|
pushFavedToFront(*set, document, *emojiList);
|
|
|
|
} else if (auto list = getEmojiListFromSet(document)) {
|
|
|
|
pushFavedToFront(*set, document, *list);
|
2017-08-03 11:15:57 +00:00
|
|
|
} else {
|
2020-06-08 17:24:36 +00:00
|
|
|
requestSetToPushFaved(document);
|
2017-08-15 14:34:30 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-06-09 16:57:05 +00:00
|
|
|
session().local().writeFavedStickers();
|
2020-06-08 17:24:36 +00:00
|
|
|
notifyUpdated();
|
2021-03-29 22:28:19 +00:00
|
|
|
notifyStickerSetInstalled(FavedSetId);
|
2017-08-15 14:34:30 +00:00
|
|
|
}
|
|
|
|
|
2020-06-08 17:24:36 +00:00
|
|
|
void Stickers::requestSetToPushFaved(not_null<DocumentData*> document) {
|
|
|
|
auto addAnyway = [=](std::vector<not_null<EmojiPtr>> list) {
|
2017-08-03 11:15:57 +00:00
|
|
|
if (list.empty()) {
|
2017-08-15 14:34:30 +00:00
|
|
|
if (auto sticker = document->sticker()) {
|
|
|
|
if (auto emoji = Ui::Emoji::Find(sticker->alt)) {
|
|
|
|
list.push_back(emoji);
|
2017-08-05 12:38:59 +00:00
|
|
|
}
|
2017-08-15 14:34:30 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-08 17:24:36 +00:00
|
|
|
setIsFaved(document, std::move(list));
|
2017-08-15 14:34:30 +00:00
|
|
|
};
|
2020-06-11 16:09:46 +00:00
|
|
|
session().api().request(MTPmessages_GetStickerSet(
|
2021-11-19 19:04:40 +00:00
|
|
|
Data::InputStickerSet(document->sticker()->set),
|
|
|
|
MTP_int(0) // hash
|
2020-06-11 16:09:46 +00:00
|
|
|
)).done([=](const MTPmessages_StickerSet &result) {
|
2021-11-19 19:04:40 +00:00
|
|
|
result.match([&](const MTPDmessages_stickerSet &data) {
|
|
|
|
auto list = std::vector<not_null<EmojiPtr>>();
|
|
|
|
list.reserve(data.vpacks().v.size());
|
|
|
|
for (const auto &mtpPack : data.vpacks().v) {
|
|
|
|
auto &pack = mtpPack.c_stickerPack();
|
|
|
|
for (const auto &documentId : pack.vdocuments().v) {
|
|
|
|
if (documentId.v == document->id) {
|
|
|
|
if (const auto emoji = Ui::Emoji::Find(qs(mtpPack.c_stickerPack().vemoticon()))) {
|
|
|
|
list.emplace_back(emoji);
|
|
|
|
}
|
|
|
|
break;
|
2017-08-03 11:15:57 +00:00
|
|
|
}
|
|
|
|
}
|
2017-08-15 14:34:30 +00:00
|
|
|
}
|
2021-11-19 19:04:40 +00:00
|
|
|
addAnyway(std::move(list));
|
|
|
|
}, [](const MTPDmessages_stickerSetNotModified &) {
|
|
|
|
LOG(("API Error: Unexpected messages.stickerSetNotModified."));
|
|
|
|
});
|
2021-11-26 20:46:53 +00:00
|
|
|
}).fail([=] {
|
2017-08-15 14:34:30 +00:00
|
|
|
// Perhaps this is a deleted sticker pack. Add anyway.
|
|
|
|
addAnyway({});
|
2020-06-11 16:09:46 +00:00
|
|
|
}).send();
|
2017-08-03 11:15:57 +00:00
|
|
|
}
|
|
|
|
|
2020-08-19 14:04:05 +00:00
|
|
|
void Stickers::removeFromRecentSet(not_null<DocumentData*> document) {
|
|
|
|
RemoveFromSet(setsRef(), document, CloudRecentSetId);
|
|
|
|
session().local().writeRecentStickers();
|
|
|
|
notifyRecentUpdated();
|
|
|
|
}
|
|
|
|
|
2020-06-08 17:24:36 +00:00
|
|
|
void Stickers::setIsNotFaved(not_null<DocumentData*> document) {
|
2020-08-19 14:04:05 +00:00
|
|
|
RemoveFromSet(setsRef(), document, FavedSetId);
|
2020-06-09 16:57:05 +00:00
|
|
|
session().local().writeFavedStickers();
|
2020-06-08 17:24:36 +00:00
|
|
|
notifyUpdated();
|
2017-08-03 11:15:57 +00:00
|
|
|
}
|
|
|
|
|
2020-06-08 17:24:36 +00:00
|
|
|
void Stickers::setFaved(not_null<DocumentData*> document, bool faved) {
|
2017-08-03 11:15:57 +00:00
|
|
|
if (faved) {
|
2020-06-08 17:24:36 +00:00
|
|
|
setIsFaved(document);
|
2017-08-03 11:15:57 +00:00
|
|
|
} else {
|
2020-06-08 17:24:36 +00:00
|
|
|
setIsNotFaved(document);
|
2017-08-02 20:57:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-25 08:15:05 +00:00
|
|
|
void Stickers::setsReceived(
|
|
|
|
const QVector<MTPStickerSet> &data,
|
|
|
|
uint64 hash) {
|
|
|
|
setsOrMasksReceived(data, hash, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Stickers::masksReceived(
|
|
|
|
const QVector<MTPStickerSet> &data,
|
|
|
|
uint64 hash) {
|
|
|
|
setsOrMasksReceived(data, hash, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Stickers::setsOrMasksReceived(
|
|
|
|
const QVector<MTPStickerSet> &data,
|
|
|
|
uint64 hash,
|
|
|
|
bool masks) {
|
|
|
|
auto &setsOrder = masks ? maskSetsOrderRef() : setsOrderRef();
|
2017-08-02 20:57:49 +00:00
|
|
|
setsOrder.clear();
|
|
|
|
|
2020-06-08 17:24:36 +00:00
|
|
|
auto &sets = setsRef();
|
2017-08-02 20:57:49 +00:00
|
|
|
QMap<uint64, uint64> setsToRequest;
|
2020-05-28 10:00:51 +00:00
|
|
|
for (auto &[id, set] : sets) {
|
2021-07-09 06:08:58 +00:00
|
|
|
const auto archived = !!(set->flags & SetFlag::Archived);
|
2021-08-25 08:15:05 +00:00
|
|
|
const auto maskset = !!(set->flags & SetFlag::Masks);
|
|
|
|
if (!archived && (masks == maskset)) {
|
2018-01-25 12:28:48 +00:00
|
|
|
// Mark for removing.
|
2021-07-09 06:08:58 +00:00
|
|
|
set->flags &= ~SetFlag::Installed;
|
2020-05-28 10:00:51 +00:00
|
|
|
set->installDate = 0;
|
2017-08-02 20:57:49 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-28 10:00:51 +00:00
|
|
|
for (const auto &setData : data) {
|
2021-03-25 11:05:33 +00:00
|
|
|
if (setData.type() != mtpc_stickerSet) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const auto set = feedSet(setData.c_stickerSet());
|
2021-07-09 06:08:58 +00:00
|
|
|
if (!(set->flags & SetFlag::Archived)
|
|
|
|
|| (set->flags & SetFlag::Official)) {
|
2021-03-25 11:05:33 +00:00
|
|
|
setsOrder.push_back(set->id);
|
|
|
|
if (set->stickers.isEmpty()
|
2021-07-09 06:08:58 +00:00
|
|
|
|| (set->flags & SetFlag::NotLoaded)) {
|
2021-08-25 08:15:05 +00:00
|
|
|
setsToRequest.insert(set->id, set->accessHash);
|
2017-08-02 20:57:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
auto writeRecent = false;
|
2020-06-08 17:24:36 +00:00
|
|
|
auto &recent = getRecentPack();
|
2020-05-31 08:18:24 +00:00
|
|
|
for (auto it = sets.begin(); it != sets.end();) {
|
2020-05-28 10:00:51 +00:00
|
|
|
const auto set = it->second.get();
|
2021-07-09 06:08:58 +00:00
|
|
|
const auto installed = !!(set->flags & SetFlag::Installed);
|
|
|
|
const auto featured = !!(set->flags & SetFlag::Featured);
|
|
|
|
const auto special = !!(set->flags & SetFlag::Special);
|
|
|
|
const auto archived = !!(set->flags & SetFlag::Archived);
|
2017-08-02 20:57:49 +00:00
|
|
|
if (!installed) { // remove not mine sets from recent stickers
|
|
|
|
for (auto i = recent.begin(); i != recent.cend();) {
|
2020-05-28 10:00:51 +00:00
|
|
|
if (set->stickers.indexOf(i->first) >= 0) {
|
2017-08-02 20:57:49 +00:00
|
|
|
i = recent.erase(i);
|
|
|
|
writeRecent = true;
|
|
|
|
} else {
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (installed || featured || special || archived) {
|
|
|
|
++it;
|
|
|
|
} else {
|
|
|
|
it = sets.erase(it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!setsToRequest.isEmpty()) {
|
2020-06-08 17:24:36 +00:00
|
|
|
auto &api = session().api();
|
2017-08-02 20:57:49 +00:00
|
|
|
for (auto i = setsToRequest.cbegin(), e = setsToRequest.cend(); i != e; ++i) {
|
|
|
|
api.scheduleStickerSetRequest(i.key(), i.value());
|
|
|
|
}
|
|
|
|
api.requestStickerSets();
|
|
|
|
}
|
|
|
|
|
2021-08-25 08:15:05 +00:00
|
|
|
if (masks) {
|
2021-03-25 11:05:33 +00:00
|
|
|
session().local().writeInstalledMasks();
|
|
|
|
} else {
|
|
|
|
session().local().writeInstalledStickers();
|
|
|
|
}
|
|
|
|
if (writeRecent) {
|
|
|
|
session().saveSettings();
|
|
|
|
}
|
2017-08-02 20:57:49 +00:00
|
|
|
|
2021-08-25 08:15:05 +00:00
|
|
|
const auto counted = masks
|
|
|
|
? Api::CountMasksHash(&session())
|
|
|
|
: Api::CountStickersHash(&session());
|
2020-06-09 16:57:05 +00:00
|
|
|
if (counted != hash) {
|
2021-08-25 08:15:05 +00:00
|
|
|
LOG(("API Error: received %1 hash %2 while counted hash is %3"
|
|
|
|
).arg(masks ? "masks" : "stickers"
|
2020-06-09 16:57:05 +00:00
|
|
|
).arg(hash
|
|
|
|
).arg(counted));
|
2017-08-02 20:57:49 +00:00
|
|
|
}
|
|
|
|
|
2020-06-08 17:24:36 +00:00
|
|
|
notifyUpdated();
|
2017-08-02 20:57:49 +00:00
|
|
|
}
|
|
|
|
|
2020-06-08 17:24:36 +00:00
|
|
|
void Stickers::setPackAndEmoji(
|
|
|
|
StickersSet &set,
|
|
|
|
StickersPack &&pack,
|
2018-03-07 17:43:26 +00:00
|
|
|
const std::vector<TimeId> &&dates,
|
|
|
|
const QVector<MTPStickerPack> &packs) {
|
2017-08-03 11:15:57 +00:00
|
|
|
set.stickers = std::move(pack);
|
2018-03-07 17:43:26 +00:00
|
|
|
set.dates = std::move(dates);
|
2017-08-03 11:15:57 +00:00
|
|
|
set.emoji.clear();
|
2020-05-28 10:00:51 +00:00
|
|
|
for (const auto &mtpPack : packs) {
|
2017-08-17 09:06:26 +00:00
|
|
|
Assert(mtpPack.type() == mtpc_stickerPack);
|
2017-08-03 11:15:57 +00:00
|
|
|
auto &pack = mtpPack.c_stickerPack();
|
2019-07-05 13:38:38 +00:00
|
|
|
if (auto emoji = Ui::Emoji::Find(qs(pack.vemoticon()))) {
|
2017-08-03 11:15:57 +00:00
|
|
|
emoji = emoji->original();
|
2019-07-05 13:38:38 +00:00
|
|
|
auto &stickers = pack.vdocuments().v;
|
2017-08-03 11:15:57 +00:00
|
|
|
|
2020-06-08 17:24:36 +00:00
|
|
|
auto p = StickersPack();
|
2017-08-03 11:15:57 +00:00
|
|
|
p.reserve(stickers.size());
|
2021-10-19 13:00:21 +00:00
|
|
|
for (auto j = 0, c = int(stickers.size()); j != c; ++j) {
|
2020-06-08 17:24:36 +00:00
|
|
|
auto document = owner().document(stickers[j].v);
|
2017-08-03 11:15:57 +00:00
|
|
|
if (!document || !document->sticker()) continue;
|
|
|
|
|
|
|
|
p.push_back(document);
|
|
|
|
}
|
|
|
|
set.emoji.insert(emoji, p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-08 17:24:36 +00:00
|
|
|
void Stickers::specialSetReceived(
|
2018-01-25 12:28:48 +00:00
|
|
|
uint64 setId,
|
|
|
|
const QString &setTitle,
|
|
|
|
const QVector<MTPDocument> &items,
|
2021-08-25 08:15:05 +00:00
|
|
|
uint64 hash,
|
2018-03-07 17:43:26 +00:00
|
|
|
const QVector<MTPStickerPack> &packs,
|
|
|
|
const QVector<MTPint> &usageDates) {
|
2020-06-08 17:24:36 +00:00
|
|
|
auto &sets = setsRef();
|
2017-08-02 20:57:49 +00:00
|
|
|
auto it = sets.find(setId);
|
|
|
|
|
2018-03-07 17:43:26 +00:00
|
|
|
if (items.isEmpty()) {
|
2017-08-02 20:57:49 +00:00
|
|
|
if (it != sets.cend()) {
|
|
|
|
sets.erase(it);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (it == sets.cend()) {
|
2020-06-08 17:24:36 +00:00
|
|
|
it = sets.emplace(setId, std::make_unique<StickersSet>(
|
|
|
|
&owner(),
|
2018-01-25 12:28:48 +00:00
|
|
|
setId,
|
2021-08-25 08:15:05 +00:00
|
|
|
uint64(0), // accessHash
|
|
|
|
uint64(0), // hash
|
2018-01-25 12:28:48 +00:00
|
|
|
setTitle,
|
|
|
|
QString(),
|
|
|
|
0, // count
|
2021-07-09 06:08:58 +00:00
|
|
|
SetFlag::Special,
|
2020-05-28 10:00:51 +00:00
|
|
|
TimeId(0))).first;
|
2017-08-02 20:57:49 +00:00
|
|
|
} else {
|
2020-05-28 10:00:51 +00:00
|
|
|
it->second->title = setTitle;
|
2017-08-02 20:57:49 +00:00
|
|
|
}
|
2020-05-28 10:00:51 +00:00
|
|
|
const auto set = it->second.get();
|
|
|
|
set->hash = hash;
|
2017-08-02 20:57:49 +00:00
|
|
|
|
2018-03-07 17:43:26 +00:00
|
|
|
auto dates = std::vector<TimeId>();
|
|
|
|
auto dateIndex = 0;
|
2018-03-08 09:57:05 +00:00
|
|
|
auto datesAvailable = (items.size() == usageDates.size())
|
2021-03-25 11:05:33 +00:00
|
|
|
&& ((setId == CloudRecentSetId)
|
|
|
|
|| (setId == CloudRecentAttachedSetId));
|
2018-03-07 17:43:26 +00:00
|
|
|
|
2020-05-28 10:00:51 +00:00
|
|
|
auto customIt = sets.find(CustomSetId);
|
2020-06-08 17:24:36 +00:00
|
|
|
auto pack = StickersPack();
|
2018-03-07 17:43:26 +00:00
|
|
|
pack.reserve(items.size());
|
2019-01-18 12:27:37 +00:00
|
|
|
for (const auto &item : items) {
|
2018-03-08 09:57:05 +00:00
|
|
|
++dateIndex;
|
2020-06-08 17:24:36 +00:00
|
|
|
const auto document = owner().processDocument(item);
|
2018-03-08 09:57:05 +00:00
|
|
|
if (!document->sticker()) {
|
|
|
|
continue;
|
|
|
|
}
|
2017-08-02 20:57:49 +00:00
|
|
|
|
|
|
|
pack.push_back(document);
|
2018-03-08 09:57:05 +00:00
|
|
|
if (datesAvailable) {
|
|
|
|
dates.push_back(TimeId(usageDates[dateIndex - 1].v));
|
|
|
|
}
|
2020-05-28 10:00:51 +00:00
|
|
|
if (customIt != sets.cend()) {
|
|
|
|
const auto custom = customIt->second.get();
|
2017-08-02 20:57:49 +00:00
|
|
|
auto index = custom->stickers.indexOf(document);
|
|
|
|
if (index >= 0) {
|
|
|
|
custom->stickers.removeAt(index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-28 10:00:51 +00:00
|
|
|
if (customIt != sets.cend()
|
|
|
|
&& customIt->second->stickers.isEmpty()) {
|
|
|
|
sets.erase(customIt);
|
|
|
|
customIt = sets.end();
|
2017-08-02 20:57:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auto writeRecent = false;
|
2020-06-08 17:24:36 +00:00
|
|
|
auto &recent = getRecentPack();
|
2017-08-02 20:57:49 +00:00
|
|
|
for (auto i = recent.begin(); i != recent.cend();) {
|
2020-05-28 10:00:51 +00:00
|
|
|
if (set->stickers.indexOf(i->first) >= 0 && pack.indexOf(i->first) < 0) {
|
2017-08-02 20:57:49 +00:00
|
|
|
i = recent.erase(i);
|
|
|
|
writeRecent = true;
|
|
|
|
} else {
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pack.isEmpty()) {
|
|
|
|
sets.erase(it);
|
|
|
|
} else {
|
2020-06-08 17:24:36 +00:00
|
|
|
setPackAndEmoji(*set, std::move(pack), std::move(dates), packs);
|
2017-08-02 20:57:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (writeRecent) {
|
2020-06-18 12:47:09 +00:00
|
|
|
session().saveSettings();
|
2017-08-02 20:57:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (setId) {
|
|
|
|
case CloudRecentSetId: {
|
2020-06-09 16:57:05 +00:00
|
|
|
const auto counted = Api::CountRecentStickersHash(&session());
|
|
|
|
if (counted != hash) {
|
|
|
|
LOG(("API Error: "
|
|
|
|
"received recent stickers hash %1 while counted hash is %2"
|
|
|
|
).arg(hash
|
|
|
|
).arg(counted));
|
2017-08-02 20:57:49 +00:00
|
|
|
}
|
2020-06-09 16:57:05 +00:00
|
|
|
session().local().writeRecentStickers();
|
2017-08-02 20:57:49 +00:00
|
|
|
} break;
|
2021-03-25 11:05:33 +00:00
|
|
|
case CloudRecentAttachedSetId: {
|
|
|
|
const auto counted = Api::CountRecentStickersHash(&session(), true);
|
|
|
|
if (counted != hash) {
|
|
|
|
LOG(("API Error: "
|
|
|
|
"received recent attached stickers hash %1 "
|
|
|
|
"while counted hash is %2"
|
|
|
|
).arg(hash, counted));
|
|
|
|
}
|
|
|
|
session().local().writeRecentMasks();
|
|
|
|
} break;
|
2017-08-02 20:57:49 +00:00
|
|
|
case FavedSetId: {
|
2020-06-09 16:57:05 +00:00
|
|
|
const auto counted = Api::CountFavedStickersHash(&session());
|
|
|
|
if (counted != hash) {
|
|
|
|
LOG(("API Error: "
|
|
|
|
"received faved stickers hash %1 while counted hash is %2"
|
|
|
|
).arg(hash
|
|
|
|
).arg(counted));
|
2017-08-02 20:57:49 +00:00
|
|
|
}
|
2020-06-09 16:57:05 +00:00
|
|
|
session().local().writeFavedStickers();
|
2017-08-02 20:57:49 +00:00
|
|
|
} break;
|
|
|
|
default: Unexpected("setId in SpecialSetReceived()");
|
|
|
|
}
|
|
|
|
|
2020-06-08 17:24:36 +00:00
|
|
|
notifyUpdated();
|
2017-08-02 20:57:49 +00:00
|
|
|
}
|
|
|
|
|
2020-06-08 17:24:36 +00:00
|
|
|
void Stickers::featuredSetsReceived(
|
2020-05-28 10:00:51 +00:00
|
|
|
const QVector<MTPStickerSetCovered> &list,
|
2019-03-22 09:13:39 +00:00
|
|
|
const QVector<MTPlong> &unread,
|
2021-08-25 08:15:05 +00:00
|
|
|
uint64 hash) {
|
2021-03-13 12:12:08 +00:00
|
|
|
auto &&unreadIds = ranges::views::all(
|
2019-03-22 09:13:39 +00:00
|
|
|
unread
|
2021-03-13 12:12:08 +00:00
|
|
|
) | ranges::views::transform([](const MTPlong &id) {
|
2019-03-22 09:13:39 +00:00
|
|
|
return id.v;
|
|
|
|
});
|
|
|
|
const auto unreadMap = base::flat_set<uint64>{
|
|
|
|
unreadIds.begin(),
|
|
|
|
unreadIds.end()
|
|
|
|
};
|
2017-08-02 20:57:49 +00:00
|
|
|
|
2020-06-08 17:24:36 +00:00
|
|
|
auto &setsOrder = featuredSetsOrderRef();
|
2017-08-02 20:57:49 +00:00
|
|
|
setsOrder.clear();
|
|
|
|
|
2020-06-08 17:24:36 +00:00
|
|
|
auto &sets = setsRef();
|
2019-03-22 09:13:39 +00:00
|
|
|
auto setsToRequest = base::flat_map<uint64, uint64>();
|
2020-05-28 10:00:51 +00:00
|
|
|
for (auto &[id, set] : sets) {
|
2019-03-22 09:13:39 +00:00
|
|
|
// Mark for removing.
|
2021-07-09 06:08:58 +00:00
|
|
|
set->flags &= ~SetFlag::Featured;
|
2017-08-02 20:57:49 +00:00
|
|
|
}
|
2020-05-28 10:00:51 +00:00
|
|
|
for (const auto &entry : list) {
|
|
|
|
const auto data = entry.match([&](const auto &data) {
|
|
|
|
return data.vset().match([&](const MTPDstickerSet &data) {
|
|
|
|
return &data;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
auto it = sets.find(data->vid().v);
|
2020-06-08 17:24:36 +00:00
|
|
|
const auto title = getSetTitle(*data);
|
2020-05-28 10:00:51 +00:00
|
|
|
const auto installDate = data->vinstalled_date().value_or_empty();
|
2020-12-13 19:27:51 +00:00
|
|
|
const auto thumbnail = [&] {
|
|
|
|
if (const auto thumbs = data->vthumbs()) {
|
|
|
|
for (const auto &thumb : thumbs->v) {
|
|
|
|
const auto result = Images::FromPhotoSize(
|
|
|
|
&session(),
|
|
|
|
*data,
|
|
|
|
thumb);
|
|
|
|
if (result.location.valid()) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ImageWithLocation();
|
|
|
|
}();
|
2021-07-09 06:08:58 +00:00
|
|
|
const auto flags = SetFlag::Featured
|
|
|
|
| (unreadMap.contains(data->vid().v)
|
|
|
|
? SetFlag::Unread
|
|
|
|
: SetFlag())
|
|
|
|
| ParseStickersSetFlags(*data);
|
2020-05-28 10:00:51 +00:00
|
|
|
if (it == sets.cend()) {
|
2020-06-08 17:24:36 +00:00
|
|
|
it = sets.emplace(data->vid().v, std::make_unique<StickersSet>(
|
|
|
|
&owner(),
|
2020-05-28 10:00:51 +00:00
|
|
|
data->vid().v,
|
|
|
|
data->vaccess_hash().v,
|
2021-08-25 08:15:05 +00:00
|
|
|
data->vhash().v,
|
2020-05-28 10:00:51 +00:00
|
|
|
title,
|
|
|
|
qs(data->vshort_name()),
|
|
|
|
data->vcount().v,
|
2021-07-09 06:08:58 +00:00
|
|
|
flags | SetFlag::NotLoaded,
|
2020-05-28 10:00:51 +00:00
|
|
|
installDate)).first;
|
2020-05-30 18:46:08 +00:00
|
|
|
it->second->setThumbnail(thumbnail);
|
2020-05-28 10:00:51 +00:00
|
|
|
} else {
|
|
|
|
const auto set = it->second.get();
|
2021-08-25 08:15:05 +00:00
|
|
|
set->accessHash = data->vaccess_hash().v;
|
2020-05-28 10:00:51 +00:00
|
|
|
set->title = title;
|
|
|
|
set->shortName = qs(data->vshort_name());
|
2021-07-09 06:08:58 +00:00
|
|
|
set->flags = flags
|
|
|
|
| (set->flags & (SetFlag::NotLoaded | SetFlag::Special));
|
2020-05-28 10:00:51 +00:00
|
|
|
set->installDate = installDate;
|
2020-05-30 18:46:08 +00:00
|
|
|
set->setThumbnail(thumbnail);
|
2020-05-28 10:00:51 +00:00
|
|
|
if (set->count != data->vcount().v || set->hash != data->vhash().v || set->emoji.isEmpty()) {
|
|
|
|
set->count = data->vcount().v;
|
|
|
|
set->hash = data->vhash().v;
|
2021-07-09 06:08:58 +00:00
|
|
|
set->flags |= SetFlag::NotLoaded; // need to request this set
|
2017-08-02 20:57:49 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-28 10:00:51 +00:00
|
|
|
setsOrder.push_back(data->vid().v);
|
|
|
|
if (it->second->stickers.isEmpty()
|
2021-07-09 06:08:58 +00:00
|
|
|
|| (it->second->flags & SetFlag::NotLoaded)) {
|
2020-05-28 10:00:51 +00:00
|
|
|
setsToRequest.emplace(data->vid().v, data->vaccess_hash().v);
|
|
|
|
}
|
2017-08-02 20:57:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auto unreadCount = 0;
|
2020-05-31 08:18:24 +00:00
|
|
|
for (auto it = sets.begin(); it != sets.end();) {
|
2020-05-28 10:00:51 +00:00
|
|
|
const auto set = it->second.get();
|
2021-07-09 06:08:58 +00:00
|
|
|
bool installed = (set->flags & SetFlag::Installed);
|
|
|
|
bool featured = (set->flags & SetFlag::Featured);
|
|
|
|
bool special = (set->flags & SetFlag::Special);
|
|
|
|
bool archived = (set->flags & SetFlag::Archived);
|
2017-08-02 20:57:49 +00:00
|
|
|
if (installed || featured || special || archived) {
|
2021-07-09 06:08:58 +00:00
|
|
|
if (featured && (set->flags & SetFlag::Unread)) {
|
2017-08-02 20:57:49 +00:00
|
|
|
++unreadCount;
|
|
|
|
}
|
|
|
|
++it;
|
|
|
|
} else {
|
|
|
|
it = sets.erase(it);
|
|
|
|
}
|
|
|
|
}
|
2020-06-08 17:24:36 +00:00
|
|
|
setFeaturedSetsUnreadCount(unreadCount);
|
2017-08-02 20:57:49 +00:00
|
|
|
|
2020-06-09 16:57:05 +00:00
|
|
|
const auto counted = Api::CountFeaturedStickersHash(&session());
|
|
|
|
if (counted != hash) {
|
|
|
|
LOG(("API Error: "
|
|
|
|
"received featured stickers hash %1 while counted hash is %2"
|
|
|
|
).arg(hash
|
|
|
|
).arg(counted));
|
2017-08-02 20:57:49 +00:00
|
|
|
}
|
|
|
|
|
2019-03-22 09:13:39 +00:00
|
|
|
if (!setsToRequest.empty()) {
|
2020-06-08 17:24:36 +00:00
|
|
|
auto &api = session().api();
|
2021-03-13 08:26:58 +00:00
|
|
|
for (const auto &[setId, accessHash] : setsToRequest) {
|
2019-03-22 09:13:39 +00:00
|
|
|
api.scheduleStickerSetRequest(setId, accessHash);
|
2017-08-02 20:57:49 +00:00
|
|
|
}
|
|
|
|
api.requestStickerSets();
|
|
|
|
}
|
|
|
|
|
2020-06-09 16:57:05 +00:00
|
|
|
session().local().writeFeaturedStickers();
|
2017-08-02 20:57:49 +00:00
|
|
|
|
2020-06-08 17:24:36 +00:00
|
|
|
notifyUpdated();
|
2017-08-02 20:57:49 +00:00
|
|
|
}
|
|
|
|
|
2021-08-25 08:15:05 +00:00
|
|
|
void Stickers::gifsReceived(const QVector<MTPDocument> &items, uint64 hash) {
|
2020-06-08 17:24:36 +00:00
|
|
|
auto &saved = savedGifsRef();
|
2017-08-02 20:57:49 +00:00
|
|
|
saved.clear();
|
|
|
|
|
|
|
|
saved.reserve(items.size());
|
2019-01-18 12:27:37 +00:00
|
|
|
for (const auto &item : items) {
|
2020-06-08 17:24:36 +00:00
|
|
|
const auto document = owner().processDocument(item);
|
2018-01-17 16:21:01 +00:00
|
|
|
if (!document->isGifv()) {
|
2019-01-18 12:27:37 +00:00
|
|
|
LOG(("API Error: "
|
2020-06-30 16:22:47 +00:00
|
|
|
"bad document returned in Stickers::gifsReceived!"));
|
2017-08-02 20:57:49 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
saved.push_back(document);
|
|
|
|
}
|
2020-06-09 16:57:05 +00:00
|
|
|
const auto counted = Api::CountSavedGifsHash(&session());
|
|
|
|
if (counted != hash) {
|
|
|
|
LOG(("API Error: "
|
|
|
|
"received saved gifs hash %1 while counted hash is %2"
|
|
|
|
).arg(hash
|
|
|
|
).arg(counted));
|
2017-08-02 20:57:49 +00:00
|
|
|
}
|
|
|
|
|
2020-06-09 16:57:05 +00:00
|
|
|
session().local().writeSavedGifs();
|
2017-08-02 20:57:49 +00:00
|
|
|
|
2020-06-08 17:24:36 +00:00
|
|
|
notifySavedGifsUpdated();
|
2017-08-02 20:57:49 +00:00
|
|
|
}
|
|
|
|
|
2020-06-08 17:24:36 +00:00
|
|
|
std::vector<not_null<DocumentData*>> Stickers::getListByEmoji(
|
2018-03-07 21:25:03 +00:00
|
|
|
not_null<EmojiPtr> emoji,
|
|
|
|
uint64 seed) {
|
|
|
|
const auto original = emoji->original();
|
|
|
|
|
|
|
|
struct StickerWithDate {
|
|
|
|
not_null<DocumentData*> document;
|
|
|
|
TimeId date = 0;
|
|
|
|
};
|
|
|
|
auto result = std::vector<StickerWithDate>();
|
2020-06-08 17:24:36 +00:00
|
|
|
auto &sets = setsRef();
|
2018-03-07 21:25:03 +00:00
|
|
|
auto setsToRequest = base::flat_map<uint64, uint64>();
|
|
|
|
|
|
|
|
const auto add = [&](not_null<DocumentData*> document, TimeId date) {
|
|
|
|
if (ranges::find(result, document, [](const StickerWithDate &data) {
|
|
|
|
return data.document;
|
|
|
|
}) == result.end()) {
|
|
|
|
result.push_back({ document, date });
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
constexpr auto kSlice = 65536;
|
|
|
|
const auto CreateSortKey = [&](
|
|
|
|
not_null<DocumentData*> document,
|
|
|
|
int base) {
|
2022-01-19 14:45:51 +00:00
|
|
|
if (document->sticker() && document->sticker()->isAnimated()) {
|
2019-07-04 14:24:22 +00:00
|
|
|
base += kSlice;
|
|
|
|
}
|
2018-03-07 21:25:03 +00:00
|
|
|
return TimeId(base + int((document->id ^ seed) % kSlice));
|
|
|
|
};
|
|
|
|
const auto CreateRecentSortKey = [&](not_null<DocumentData*> document) {
|
2019-07-04 14:24:22 +00:00
|
|
|
return CreateSortKey(document, kSlice * 6);
|
2018-03-07 21:25:03 +00:00
|
|
|
};
|
|
|
|
auto myCounter = 0;
|
2019-07-04 14:24:22 +00:00
|
|
|
const auto CreateMySortKey = [&](not_null<DocumentData*> document) {
|
|
|
|
auto base = kSlice * 6;
|
2022-01-19 14:45:51 +00:00
|
|
|
if (!document->sticker() || !document->sticker()->isAnimated()) {
|
2019-07-04 14:24:22 +00:00
|
|
|
base -= kSlice;
|
|
|
|
}
|
|
|
|
return (base - (++myCounter));
|
2018-03-07 21:25:03 +00:00
|
|
|
};
|
|
|
|
const auto CreateFeaturedSortKey = [&](not_null<DocumentData*> document) {
|
|
|
|
return CreateSortKey(document, kSlice * 2);
|
|
|
|
};
|
|
|
|
const auto CreateOtherSortKey = [&](not_null<DocumentData*> document) {
|
2019-07-04 14:24:22 +00:00
|
|
|
return CreateSortKey(document, 0);
|
|
|
|
};
|
|
|
|
const auto InstallDateAdjusted = [&](
|
|
|
|
TimeId date,
|
|
|
|
not_null<DocumentData*> document) {
|
2022-01-19 14:45:51 +00:00
|
|
|
return (document->sticker() && document->sticker()->isAnimated())
|
2019-07-04 14:24:22 +00:00
|
|
|
? date
|
|
|
|
: date / 2;
|
2018-03-07 21:25:03 +00:00
|
|
|
};
|
|
|
|
const auto InstallDate = [&](not_null<DocumentData*> document) {
|
|
|
|
Expects(document->sticker() != nullptr);
|
|
|
|
|
|
|
|
const auto sticker = document->sticker();
|
2021-07-08 16:42:57 +00:00
|
|
|
if (sticker->set.id) {
|
|
|
|
const auto setIt = sets.find(sticker->set.id);
|
2018-03-07 21:25:03 +00:00
|
|
|
if (setIt != sets.end()) {
|
2020-05-28 10:00:51 +00:00
|
|
|
return InstallDateAdjusted(setIt->second->installDate, document);
|
2018-03-07 21:25:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return TimeId(0);
|
|
|
|
};
|
2017-08-03 11:15:57 +00:00
|
|
|
|
2018-03-07 21:25:03 +00:00
|
|
|
auto recentIt = sets.find(Stickers::CloudRecentSetId);
|
|
|
|
if (recentIt != sets.cend()) {
|
2020-05-28 10:00:51 +00:00
|
|
|
const auto recent = recentIt->second.get();
|
|
|
|
auto i = recent->emoji.constFind(original);
|
|
|
|
if (i != recent->emoji.cend()) {
|
2018-03-07 21:25:03 +00:00
|
|
|
result.reserve(i->size());
|
|
|
|
for (const auto document : *i) {
|
|
|
|
const auto usageDate = [&] {
|
2020-05-28 10:00:51 +00:00
|
|
|
if (recent->dates.empty()) {
|
2018-03-07 21:25:03 +00:00
|
|
|
return TimeId(0);
|
|
|
|
}
|
2020-05-28 10:00:51 +00:00
|
|
|
const auto index = recent->stickers.indexOf(document);
|
2018-03-07 21:25:03 +00:00
|
|
|
if (index < 0) {
|
|
|
|
return TimeId(0);
|
|
|
|
}
|
2020-05-28 10:00:51 +00:00
|
|
|
Assert(index < recent->dates.size());
|
|
|
|
return recent->dates[index];
|
2018-03-07 21:25:03 +00:00
|
|
|
}();
|
|
|
|
const auto date = usageDate
|
|
|
|
? usageDate
|
|
|
|
: InstallDate(document);
|
|
|
|
result.push_back({
|
|
|
|
document,
|
|
|
|
date ? date : CreateRecentSortKey(document) });
|
|
|
|
}
|
2017-08-03 11:15:57 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-08 17:24:36 +00:00
|
|
|
const auto addList = [&](
|
|
|
|
const StickersSetsOrder &order,
|
2021-07-09 06:08:58 +00:00
|
|
|
SetFlag skip) {
|
2018-03-07 13:53:12 +00:00
|
|
|
for (const auto setId : order) {
|
|
|
|
auto it = sets.find(setId);
|
2020-05-28 10:00:51 +00:00
|
|
|
if (it == sets.cend() || (it->second->flags & skip)) {
|
2018-03-07 13:53:12 +00:00
|
|
|
continue;
|
|
|
|
}
|
2020-05-28 10:00:51 +00:00
|
|
|
const auto set = it->second.get();
|
|
|
|
if (set->emoji.isEmpty()) {
|
2021-08-25 08:15:05 +00:00
|
|
|
setsToRequest.emplace(set->id, set->accessHash);
|
2021-07-09 06:08:58 +00:00
|
|
|
set->flags |= SetFlag::NotLoaded;
|
2018-03-07 13:53:12 +00:00
|
|
|
continue;
|
|
|
|
}
|
2020-05-28 10:00:51 +00:00
|
|
|
auto i = set->emoji.constFind(original);
|
|
|
|
if (i == set->emoji.cend()) {
|
2018-03-07 13:53:12 +00:00
|
|
|
continue;
|
|
|
|
}
|
2021-07-09 06:08:58 +00:00
|
|
|
const auto my = (set->flags & SetFlag::Installed);
|
2018-03-07 13:53:12 +00:00
|
|
|
result.reserve(result.size() + i->size());
|
2018-03-07 21:25:03 +00:00
|
|
|
for (const auto document : *i) {
|
2020-05-28 10:00:51 +00:00
|
|
|
const auto installDate = my ? set->installDate : TimeId(0);
|
2018-03-07 21:25:03 +00:00
|
|
|
const auto date = (installDate > 1)
|
2019-07-04 14:24:22 +00:00
|
|
|
? InstallDateAdjusted(installDate, document)
|
2018-03-07 21:25:03 +00:00
|
|
|
: my
|
2019-07-04 14:24:22 +00:00
|
|
|
? CreateMySortKey(document)
|
2018-03-07 21:25:03 +00:00
|
|
|
: CreateFeaturedSortKey(document);
|
|
|
|
add(document, date);
|
2017-08-03 11:15:57 +00:00
|
|
|
}
|
|
|
|
}
|
2018-03-07 13:53:12 +00:00
|
|
|
};
|
|
|
|
|
2021-07-09 06:08:58 +00:00
|
|
|
addList(setsOrder(), SetFlag::Archived);
|
|
|
|
//addList(featuredSetsOrder(), SetFlag::Installed);
|
2018-03-07 13:53:12 +00:00
|
|
|
|
2018-03-07 21:25:03 +00:00
|
|
|
if (!setsToRequest.empty()) {
|
2019-07-04 14:24:22 +00:00
|
|
|
for (const auto &[setId, accessHash] : setsToRequest) {
|
2020-06-08 17:24:36 +00:00
|
|
|
session().api().scheduleStickerSetRequest(setId, accessHash);
|
2017-08-03 11:15:57 +00:00
|
|
|
}
|
2020-06-08 17:24:36 +00:00
|
|
|
session().api().requestStickerSets();
|
2017-08-03 11:15:57 +00:00
|
|
|
}
|
2018-03-07 21:25:03 +00:00
|
|
|
|
2020-06-18 18:04:16 +00:00
|
|
|
if (Core::App().settings().suggestStickersByEmoji()) {
|
2020-06-08 17:24:36 +00:00
|
|
|
const auto others = session().api().stickersByEmoji(original);
|
2018-03-19 16:32:33 +00:00
|
|
|
if (!others) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
result.reserve(result.size() + others->size());
|
|
|
|
for (const auto document : *others) {
|
|
|
|
add(document, CreateOtherSortKey(document));
|
|
|
|
}
|
2018-03-07 21:25:03 +00:00
|
|
|
}
|
|
|
|
|
2021-03-13 18:07:29 +00:00
|
|
|
ranges::actions::sort(
|
2018-03-07 21:25:03 +00:00
|
|
|
result,
|
|
|
|
std::greater<>(),
|
2019-07-04 14:24:22 +00:00
|
|
|
&StickerWithDate::date);
|
2018-03-07 21:25:03 +00:00
|
|
|
|
2021-03-13 12:12:08 +00:00
|
|
|
return ranges::views::all(
|
2018-03-07 21:25:03 +00:00
|
|
|
result
|
2021-03-13 12:12:08 +00:00
|
|
|
) | ranges::views::transform([](const StickerWithDate &data) {
|
2018-03-07 21:25:03 +00:00
|
|
|
return data.document;
|
|
|
|
}) | ranges::to_vector;
|
2017-08-03 11:15:57 +00:00
|
|
|
}
|
|
|
|
|
2020-06-08 17:24:36 +00:00
|
|
|
std::optional<std::vector<not_null<EmojiPtr>>> Stickers::getEmojiListFromSet(
|
2017-08-17 08:31:24 +00:00
|
|
|
not_null<DocumentData*> document) {
|
2017-08-03 11:15:57 +00:00
|
|
|
if (auto sticker = document->sticker()) {
|
|
|
|
auto &inputSet = sticker->set;
|
2021-07-08 16:42:57 +00:00
|
|
|
if (!inputSet.id) {
|
2018-09-21 16:28:46 +00:00
|
|
|
return std::nullopt;
|
2017-08-03 11:15:57 +00:00
|
|
|
}
|
2020-06-08 17:24:36 +00:00
|
|
|
const auto &sets = this->sets();
|
2021-07-08 16:42:57 +00:00
|
|
|
auto it = sets.find(inputSet.id);
|
2017-08-03 11:15:57 +00:00
|
|
|
if (it == sets.cend()) {
|
2018-09-21 16:28:46 +00:00
|
|
|
return std::nullopt;
|
2017-08-03 11:15:57 +00:00
|
|
|
}
|
2020-05-28 10:00:51 +00:00
|
|
|
const auto set = it->second.get();
|
2017-08-17 08:31:24 +00:00
|
|
|
auto result = std::vector<not_null<EmojiPtr>>();
|
2020-05-28 10:00:51 +00:00
|
|
|
for (auto i = set->emoji.cbegin(), e = set->emoji.cend(); i != e; ++i) {
|
2017-08-03 11:15:57 +00:00
|
|
|
if (i->contains(document)) {
|
2019-03-22 09:13:39 +00:00
|
|
|
result.emplace_back(i.key());
|
2017-08-03 11:15:57 +00:00
|
|
|
}
|
|
|
|
}
|
2017-08-15 14:34:30 +00:00
|
|
|
if (result.empty()) {
|
2018-09-21 16:28:46 +00:00
|
|
|
return std::nullopt;
|
2017-08-15 14:34:30 +00:00
|
|
|
}
|
2019-11-12 15:15:34 +00:00
|
|
|
return result;
|
2017-08-03 11:15:57 +00:00
|
|
|
}
|
2018-09-21 16:28:46 +00:00
|
|
|
return std::nullopt;
|
2017-08-03 11:15:57 +00:00
|
|
|
}
|
|
|
|
|
2020-06-08 17:24:36 +00:00
|
|
|
StickersSet *Stickers::feedSet(const MTPDstickerSet &data) {
|
|
|
|
auto &sets = setsRef();
|
2020-05-28 10:00:51 +00:00
|
|
|
auto it = sets.find(data.vid().v);
|
2020-06-08 17:24:36 +00:00
|
|
|
auto title = getSetTitle(data);
|
2021-07-09 06:08:58 +00:00
|
|
|
auto oldFlags = StickersSetFlags(0);
|
2020-12-13 19:27:51 +00:00
|
|
|
const auto thumbnail = [&] {
|
|
|
|
if (const auto thumbs = data.vthumbs()) {
|
|
|
|
for (const auto &thumb : thumbs->v) {
|
|
|
|
const auto result = Images::FromPhotoSize(
|
|
|
|
&session(),
|
|
|
|
data,
|
|
|
|
thumb);
|
|
|
|
if (result.location.valid()) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ImageWithLocation();
|
|
|
|
}();
|
2021-07-09 06:08:58 +00:00
|
|
|
const auto flags = ParseStickersSetFlags(data);
|
2017-08-03 12:52:56 +00:00
|
|
|
if (it == sets.cend()) {
|
2020-06-08 17:24:36 +00:00
|
|
|
it = sets.emplace(data.vid().v, std::make_unique<StickersSet>(
|
|
|
|
&owner(),
|
2020-05-28 10:00:51 +00:00
|
|
|
data.vid().v,
|
|
|
|
data.vaccess_hash().v,
|
2021-08-25 08:15:05 +00:00
|
|
|
data.vhash().v,
|
2018-01-25 12:28:48 +00:00
|
|
|
title,
|
2020-05-28 10:00:51 +00:00
|
|
|
qs(data.vshort_name()),
|
|
|
|
data.vcount().v,
|
2021-07-09 06:08:58 +00:00
|
|
|
flags | SetFlag::NotLoaded,
|
2020-05-28 10:00:51 +00:00
|
|
|
data.vinstalled_date().value_or_empty())).first;
|
2020-05-30 18:46:08 +00:00
|
|
|
it->second->setThumbnail(thumbnail);
|
2017-08-03 12:52:56 +00:00
|
|
|
} else {
|
2020-05-28 10:00:51 +00:00
|
|
|
const auto set = it->second.get();
|
2021-08-25 08:15:05 +00:00
|
|
|
set->accessHash = data.vaccess_hash().v;
|
2020-05-28 10:00:51 +00:00
|
|
|
set->title = title;
|
|
|
|
set->shortName = qs(data.vshort_name());
|
2021-07-09 06:08:58 +00:00
|
|
|
oldFlags = set->flags;
|
|
|
|
const auto clientFlags = set->flags
|
|
|
|
& (SetFlag::Featured
|
|
|
|
| SetFlag::Unread
|
|
|
|
| SetFlag::NotLoaded
|
|
|
|
| SetFlag::Special);
|
|
|
|
set->flags = flags | clientFlags;
|
2020-05-28 10:00:51 +00:00
|
|
|
const auto installDate = data.vinstalled_date();
|
|
|
|
set->installDate = installDate
|
2019-07-10 17:28:33 +00:00
|
|
|
? (installDate->v ? installDate->v : base::unixtime::now())
|
2018-01-25 12:28:48 +00:00
|
|
|
: TimeId(0);
|
2020-05-30 18:46:08 +00:00
|
|
|
it->second->setThumbnail(thumbnail);
|
2020-05-28 10:00:51 +00:00
|
|
|
if (set->count != data.vcount().v
|
|
|
|
|| set->hash != data.vhash().v
|
|
|
|
|| set->emoji.isEmpty()) {
|
|
|
|
// Need to request this data.
|
|
|
|
set->count = data.vcount().v;
|
|
|
|
set->hash = data.vhash().v;
|
2021-07-09 06:08:58 +00:00
|
|
|
set->flags |= SetFlag::NotLoaded;
|
2017-08-03 12:52:56 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-28 10:00:51 +00:00
|
|
|
const auto set = it->second.get();
|
2021-07-09 06:08:58 +00:00
|
|
|
auto changedFlags = (oldFlags ^ set->flags);
|
|
|
|
if (changedFlags & SetFlag::Archived) {
|
|
|
|
const auto masks = !!(set->flags & SetFlag::Masks);
|
2021-03-30 15:51:45 +00:00
|
|
|
auto &archivedOrder = masks
|
|
|
|
? archivedMaskSetsOrderRef()
|
|
|
|
: archivedSetsOrderRef();
|
|
|
|
const auto index = archivedOrder.indexOf(set->id);
|
2021-07-09 06:08:58 +00:00
|
|
|
if (set->flags & SetFlag::Archived) {
|
2017-08-03 12:52:56 +00:00
|
|
|
if (index < 0) {
|
2021-03-30 15:51:45 +00:00
|
|
|
archivedOrder.push_front(set->id);
|
2017-08-03 12:52:56 +00:00
|
|
|
}
|
|
|
|
} else if (index >= 0) {
|
2021-03-30 15:51:45 +00:00
|
|
|
archivedOrder.removeAt(index);
|
2017-08-03 12:52:56 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-28 10:00:51 +00:00
|
|
|
return it->second.get();
|
2017-08-03 12:52:56 +00:00
|
|
|
}
|
|
|
|
|
2021-11-19 19:04:40 +00:00
|
|
|
StickersSet *Stickers::feedSetFull(const MTPDmessages_stickerSet &d) {
|
|
|
|
Expects(d.vset().type() == mtpc_stickerSet);
|
2017-08-03 12:52:56 +00:00
|
|
|
|
2019-07-05 13:38:38 +00:00
|
|
|
const auto &s = d.vset().c_stickerSet();
|
2017-08-03 12:52:56 +00:00
|
|
|
|
2020-06-08 17:24:36 +00:00
|
|
|
auto &sets = setsRef();
|
2020-05-30 18:24:35 +00:00
|
|
|
const auto wasArchived = [&] {
|
|
|
|
auto it = sets.find(s.vid().v);
|
|
|
|
return (it != sets.end())
|
2021-07-09 06:08:58 +00:00
|
|
|
&& (it->second->flags & SetFlag::Archived);
|
2020-05-30 18:24:35 +00:00
|
|
|
}();
|
2019-03-22 09:13:39 +00:00
|
|
|
|
2020-06-08 17:24:36 +00:00
|
|
|
auto set = feedSet(s);
|
2019-03-22 09:13:39 +00:00
|
|
|
|
2021-07-09 06:08:58 +00:00
|
|
|
set->flags &= ~SetFlag::NotLoaded;
|
2019-03-22 09:13:39 +00:00
|
|
|
|
2020-05-30 18:24:35 +00:00
|
|
|
const auto &d_docs = d.vdocuments().v;
|
2020-05-28 10:00:51 +00:00
|
|
|
auto customIt = sets.find(Stickers::CustomSetId);
|
2021-07-08 16:42:57 +00:00
|
|
|
const auto inputSet = set->identifier();
|
2017-08-03 12:52:56 +00:00
|
|
|
|
2020-06-08 17:24:36 +00:00
|
|
|
auto pack = StickersPack();
|
2017-08-03 12:52:56 +00:00
|
|
|
pack.reserve(d_docs.size());
|
2019-01-18 12:27:37 +00:00
|
|
|
for (const auto &item : d_docs) {
|
2020-06-08 17:24:36 +00:00
|
|
|
const auto document = owner().processDocument(item);
|
2019-01-18 12:27:37 +00:00
|
|
|
if (!document->sticker()) continue;
|
2017-08-03 12:52:56 +00:00
|
|
|
|
2019-01-18 12:27:37 +00:00
|
|
|
pack.push_back(document);
|
2021-07-08 16:42:57 +00:00
|
|
|
if (!document->sticker()->set.id) {
|
2019-03-22 09:13:39 +00:00
|
|
|
document->sticker()->set = inputSet;
|
|
|
|
}
|
2020-05-28 10:00:51 +00:00
|
|
|
if (customIt != sets.cend()) {
|
|
|
|
const auto custom = customIt->second.get();
|
2019-01-18 12:27:37 +00:00
|
|
|
const auto index = custom->stickers.indexOf(document);
|
2017-08-03 12:52:56 +00:00
|
|
|
if (index >= 0) {
|
|
|
|
custom->stickers.removeAt(index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-28 10:00:51 +00:00
|
|
|
if (customIt != sets.cend() && customIt->second->stickers.isEmpty()) {
|
|
|
|
sets.erase(customIt);
|
|
|
|
customIt = sets.end();
|
2017-08-03 12:52:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auto writeRecent = false;
|
2020-06-08 17:24:36 +00:00
|
|
|
auto &recent = getRecentPack();
|
2017-08-03 12:52:56 +00:00
|
|
|
for (auto i = recent.begin(); i != recent.cend();) {
|
|
|
|
if (set->stickers.indexOf(i->first) >= 0 && pack.indexOf(i->first) < 0) {
|
|
|
|
i = recent.erase(i);
|
|
|
|
writeRecent = true;
|
|
|
|
} else {
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-09 06:08:58 +00:00
|
|
|
const auto isMasks = !!(set->flags & SetFlag::Masks);
|
2017-08-03 12:52:56 +00:00
|
|
|
if (pack.isEmpty()) {
|
2021-03-25 11:05:33 +00:00
|
|
|
const auto removeIndex = (isMasks
|
|
|
|
? maskSetsOrder()
|
|
|
|
: setsOrder()).indexOf(set->id);
|
|
|
|
if (removeIndex >= 0) {
|
|
|
|
(isMasks
|
|
|
|
? maskSetsOrderRef()
|
|
|
|
: setsOrderRef()).removeAt(removeIndex);
|
|
|
|
}
|
2017-08-03 12:52:56 +00:00
|
|
|
sets.remove(set->id);
|
|
|
|
set = nullptr;
|
|
|
|
} else {
|
|
|
|
set->stickers = pack;
|
|
|
|
set->emoji.clear();
|
2019-07-05 13:38:38 +00:00
|
|
|
auto &v = d.vpacks().v;
|
2021-10-19 13:00:21 +00:00
|
|
|
for (auto i = 0, l = int(v.size()); i != l; ++i) {
|
2017-08-03 12:52:56 +00:00
|
|
|
if (v[i].type() != mtpc_stickerPack) continue;
|
|
|
|
|
|
|
|
auto &pack = v[i].c_stickerPack();
|
2019-07-05 13:38:38 +00:00
|
|
|
if (auto emoji = Ui::Emoji::Find(qs(pack.vemoticon()))) {
|
2017-08-03 12:52:56 +00:00
|
|
|
emoji = emoji->original();
|
2019-07-05 13:38:38 +00:00
|
|
|
auto &stickers = pack.vdocuments().v;
|
2017-08-03 12:52:56 +00:00
|
|
|
|
2020-06-08 17:24:36 +00:00
|
|
|
StickersPack p;
|
2017-08-03 12:52:56 +00:00
|
|
|
p.reserve(stickers.size());
|
2021-10-19 13:00:21 +00:00
|
|
|
for (auto j = 0, c = int(stickers.size()); j != c; ++j) {
|
2020-06-08 17:24:36 +00:00
|
|
|
auto doc = owner().document(stickers[j].v);
|
2017-08-03 12:52:56 +00:00
|
|
|
if (!doc || !doc->sticker()) continue;
|
|
|
|
|
|
|
|
p.push_back(doc);
|
|
|
|
}
|
|
|
|
set->emoji.insert(emoji, p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (writeRecent) {
|
2020-06-18 12:47:09 +00:00
|
|
|
session().saveSettings();
|
2017-08-03 12:52:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (set) {
|
2021-07-09 06:08:58 +00:00
|
|
|
const auto isArchived = !!(set->flags & SetFlag::Archived);
|
2021-03-25 11:05:33 +00:00
|
|
|
if (isMasks) {
|
|
|
|
session().local().writeInstalledMasks();
|
2021-07-09 06:08:58 +00:00
|
|
|
} else if (set->flags & SetFlag::Installed) {
|
2019-03-22 09:13:39 +00:00
|
|
|
if (!isArchived) {
|
2020-06-09 16:57:05 +00:00
|
|
|
session().local().writeInstalledStickers();
|
2017-08-03 12:52:56 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-09 06:08:58 +00:00
|
|
|
if (set->flags & SetFlag::Featured) {
|
2020-06-09 16:57:05 +00:00
|
|
|
session().local().writeFeaturedStickers();
|
2017-08-03 12:52:56 +00:00
|
|
|
}
|
2019-03-22 09:13:39 +00:00
|
|
|
if (wasArchived != isArchived) {
|
2021-03-30 15:51:45 +00:00
|
|
|
if (isMasks) {
|
|
|
|
session().local().writeArchivedMasks();
|
|
|
|
} else {
|
|
|
|
session().local().writeArchivedStickers();
|
|
|
|
}
|
2019-03-22 09:13:39 +00:00
|
|
|
}
|
2017-08-03 12:52:56 +00:00
|
|
|
}
|
|
|
|
|
2020-06-08 17:24:36 +00:00
|
|
|
notifyUpdated();
|
2017-08-03 12:52:56 +00:00
|
|
|
|
|
|
|
return set;
|
|
|
|
}
|
|
|
|
|
2021-11-19 19:04:40 +00:00
|
|
|
void Stickers::newSetReceived(const MTPDmessages_stickerSet &set) {
|
2019-07-05 13:38:38 +00:00
|
|
|
const auto &s = set.vset().c_stickerSet();
|
|
|
|
if (!s.vinstalled_date()) {
|
2019-03-22 09:13:39 +00:00
|
|
|
LOG(("API Error: "
|
|
|
|
"updateNewStickerSet without install_date flag."));
|
|
|
|
return;
|
|
|
|
} else if (s.is_archived()) {
|
|
|
|
LOG(("API Error: "
|
|
|
|
"updateNewStickerSet with archived flag."));
|
|
|
|
return;
|
|
|
|
}
|
2021-03-29 16:36:03 +00:00
|
|
|
auto &order = s.is_masks() ? maskSetsOrderRef() : setsOrderRef();
|
2019-07-05 13:38:38 +00:00
|
|
|
int32 insertAtIndex = 0, currentIndex = order.indexOf(s.vid().v);
|
2019-03-22 09:13:39 +00:00
|
|
|
if (currentIndex != insertAtIndex) {
|
|
|
|
if (currentIndex > 0) {
|
|
|
|
order.removeAt(currentIndex);
|
|
|
|
}
|
2019-07-05 13:38:38 +00:00
|
|
|
order.insert(insertAtIndex, s.vid().v);
|
2019-03-22 09:13:39 +00:00
|
|
|
}
|
|
|
|
|
2021-11-19 19:04:40 +00:00
|
|
|
feedSetFull(set);
|
2019-03-22 09:13:39 +00:00
|
|
|
}
|
|
|
|
|
2020-06-08 17:24:36 +00:00
|
|
|
QString Stickers::getSetTitle(const MTPDstickerSet &s) {
|
2019-07-05 13:38:38 +00:00
|
|
|
auto title = qs(s.vtitle());
|
|
|
|
if ((s.vflags().v & MTPDstickerSet::Flag::f_official) && !title.compare(qstr("Great Minds"), Qt::CaseInsensitive)) {
|
2019-06-19 15:09:03 +00:00
|
|
|
return tr::lng_stickers_default_set(tr::now);
|
2017-08-12 07:17:33 +00:00
|
|
|
}
|
|
|
|
return title;
|
|
|
|
}
|
|
|
|
|
2020-06-09 16:57:05 +00:00
|
|
|
RecentStickerPack &Stickers::getRecentPack() const {
|
2017-12-11 14:45:29 +00:00
|
|
|
if (cRecentStickers().isEmpty() && !cRecentStickersPreload().isEmpty()) {
|
|
|
|
const auto p = cRecentStickersPreload();
|
|
|
|
cSetRecentStickersPreload(RecentStickerPreload());
|
|
|
|
|
|
|
|
auto &recent = cRefRecentStickers();
|
|
|
|
recent.reserve(p.size());
|
|
|
|
for (const auto &preloaded : p) {
|
2020-06-08 17:24:36 +00:00
|
|
|
const auto document = owner().document(preloaded.first);
|
2017-12-11 14:45:29 +00:00
|
|
|
if (!document || !document->sticker()) continue;
|
|
|
|
|
|
|
|
recent.push_back(qMakePair(document, preloaded.second));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return cRefRecentStickers();
|
|
|
|
}
|
|
|
|
|
2016-09-09 15:52:46 +00:00
|
|
|
} // namespace Stickers
|