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
|
|
|
*/
|
2016-09-10 20:54:59 +00:00
|
|
|
#include "stickers.h"
|
2016-09-09 15:52:46 +00:00
|
|
|
|
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"
|
2016-10-20 16:32:15 +00:00
|
|
|
#include "boxes/stickers_box.h"
|
2017-04-06 14:38:10 +00:00
|
|
|
#include "boxes/confirm_box.h"
|
2017-04-13 08:27:10 +00:00
|
|
|
#include "lang/lang_keys.h"
|
2016-09-09 15:52:46 +00:00
|
|
|
#include "apiwrap.h"
|
2017-03-04 10:23:56 +00:00
|
|
|
#include "storage/localstorage.h"
|
2016-09-09 15:52:46 +00:00
|
|
|
#include "mainwidget.h"
|
2018-01-17 16:21:01 +00:00
|
|
|
#include "auth_session.h"
|
2016-12-23 13:21:01 +00:00
|
|
|
#include "mainwindow.h"
|
|
|
|
#include "ui/toast/toast.h"
|
2018-10-13 17:35:30 +00:00
|
|
|
#include "ui/emoji_config.h"
|
2017-04-06 14:38:10 +00:00
|
|
|
#include "styles/style_chat_helpers.h"
|
2016-09-09 15:52:46 +00:00
|
|
|
|
|
|
|
namespace Stickers {
|
|
|
|
|
2017-08-02 20:57:49 +00:00
|
|
|
void ApplyArchivedResult(const MTPDmessages_stickerSetInstallResultArchive &d) {
|
2017-03-10 19:46:28 +00:00
|
|
|
auto &v = d.vsets.v;
|
2017-11-05 17:07:27 +00:00
|
|
|
auto &order = Auth().data().stickerSetsOrderRef();
|
2017-08-02 20:57:49 +00:00
|
|
|
Order archived;
|
2016-09-09 15:52:46 +00:00
|
|
|
archived.reserve(v.size());
|
|
|
|
QMap<uint64, uint64> setsToRequest;
|
|
|
|
for_const (auto &stickerSet, v) {
|
|
|
|
const MTPDstickerSet *setData = nullptr;
|
|
|
|
switch (stickerSet.type()) {
|
|
|
|
case mtpc_stickerSetCovered: {
|
|
|
|
auto &d = stickerSet.c_stickerSetCovered();
|
|
|
|
if (d.vset.type() == mtpc_stickerSet) {
|
|
|
|
setData = &d.vset.c_stickerSet();
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
case mtpc_stickerSetMultiCovered: {
|
|
|
|
auto &d = stickerSet.c_stickerSetMultiCovered();
|
|
|
|
if (d.vset.type() == mtpc_stickerSet) {
|
|
|
|
setData = &d.vset.c_stickerSet();
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
if (setData) {
|
2017-08-02 20:57:49 +00:00
|
|
|
auto set = FeedSet(*setData);
|
2016-09-09 15:52:46 +00:00
|
|
|
if (set->stickers.isEmpty()) {
|
|
|
|
setsToRequest.insert(set->id, set->access);
|
|
|
|
}
|
|
|
|
auto index = order.indexOf(set->id);
|
|
|
|
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) {
|
2017-08-04 14:54:32 +00:00
|
|
|
Auth().api().scheduleStickerSetRequest(i.key(), i.value());
|
2016-09-09 15:52:46 +00:00
|
|
|
}
|
2017-08-04 14:54:32 +00:00
|
|
|
Auth().api().requestStickerSets();
|
2016-09-09 15:52:46 +00:00
|
|
|
}
|
|
|
|
Local::writeInstalledStickers();
|
|
|
|
Local::writeArchivedStickers();
|
2016-12-23 13:21:01 +00:00
|
|
|
|
|
|
|
Ui::Toast::Config toast;
|
|
|
|
toast.text = lang(lng_stickers_packs_archived);
|
|
|
|
toast.maxWidth = st::stickersToastMaxWidth;
|
|
|
|
toast.padding = st::stickersToastPadding;
|
2017-02-03 20:07:26 +00:00
|
|
|
Ui::Toast::Show(toast);
|
2017-09-15 17:34:41 +00:00
|
|
|
// Ui::show(Box<StickersBox>(archived), LayerOption::KeepOther);
|
2016-09-09 15:52:46 +00:00
|
|
|
|
2018-01-17 18:20:55 +00:00
|
|
|
Auth().data().notifyStickersUpdated();
|
2016-09-09 15:52:46 +00:00
|
|
|
}
|
|
|
|
|
2016-11-22 09:48:13 +00:00
|
|
|
// For testing: Just apply random subset or your sticker sets as archived.
|
2017-08-02 20:57:49 +00:00
|
|
|
bool ApplyArchivedResultFake() {
|
2016-11-22 09:48:13 +00:00
|
|
|
auto sets = QVector<MTPStickerSetCovered>();
|
2017-11-05 17:07:27 +00:00
|
|
|
for (auto &set : Auth().data().stickerSetsRef()) {
|
2018-01-25 12:28:48 +00:00
|
|
|
if ((set.flags & MTPDstickerSet::Flag::f_installed_date)
|
|
|
|
&& !(set.flags & MTPDstickerSet_ClientFlag::f_special)) {
|
2016-11-22 09:48:13 +00:00
|
|
|
if (rand_value<uint32>() % 128 < 64) {
|
2018-01-25 12:28:48 +00:00
|
|
|
auto data = MTP_stickerSet(
|
|
|
|
MTP_flags(set.flags | MTPDstickerSet::Flag::f_archived),
|
|
|
|
MTP_int(set.installDate),
|
|
|
|
MTP_long(set.id),
|
|
|
|
MTP_long(set.access),
|
|
|
|
MTP_string(set.title),
|
|
|
|
MTP_string(set.shortName),
|
|
|
|
MTP_int(set.count),
|
|
|
|
MTP_int(set.hash));
|
|
|
|
sets.push_back(MTP_stickerSetCovered(
|
|
|
|
data,
|
|
|
|
MTP_documentEmpty(MTP_long(0))));
|
2016-11-22 09:48:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-01-25 12:28:48 +00:00
|
|
|
if (sets.size() > 3) {
|
|
|
|
sets = sets.mid(0, 3);
|
|
|
|
}
|
|
|
|
auto fakeResult = MTP_messages_stickerSetInstallResultArchive(
|
|
|
|
MTP_vector<MTPStickerSetCovered>(sets));
|
2017-08-02 20:57:49 +00:00
|
|
|
ApplyArchivedResult(fakeResult.c_messages_stickerSetInstallResultArchive());
|
2016-11-22 09:48:13 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-08-02 20:57:49 +00:00
|
|
|
void InstallLocally(uint64 setId) {
|
2017-11-05 17:07:27 +00:00
|
|
|
auto &sets = Auth().data().stickerSetsRef();
|
2016-09-09 15:52:46 +00:00
|
|
|
auto it = sets.find(setId);
|
|
|
|
if (it == sets.end()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto flags = it->flags;
|
|
|
|
it->flags &= ~(MTPDstickerSet::Flag::f_archived | MTPDstickerSet_ClientFlag::f_unread);
|
2018-01-25 12:28:48 +00:00
|
|
|
it->flags |= MTPDstickerSet::Flag::f_installed_date;
|
|
|
|
it->installDate = unixtime();
|
2016-09-09 15:52:46 +00:00
|
|
|
auto changedFlags = flags ^ it->flags;
|
|
|
|
|
2017-11-05 17:07:27 +00:00
|
|
|
auto &order = Auth().data().stickerSetsOrderRef();
|
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);
|
|
|
|
}
|
|
|
|
|
2017-08-02 20:57:49 +00:00
|
|
|
auto custom = sets.find(CustomSetId);
|
2016-09-09 15:52:46 +00:00
|
|
|
if (custom != sets.cend()) {
|
|
|
|
for_const (auto sticker, it->stickers) {
|
|
|
|
int removeIndex = custom->stickers.indexOf(sticker);
|
|
|
|
if (removeIndex >= 0) custom->stickers.removeAt(removeIndex);
|
|
|
|
}
|
|
|
|
if (custom->stickers.isEmpty()) {
|
|
|
|
sets.erase(custom);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Local::writeInstalledStickers();
|
2018-01-25 12:28:48 +00:00
|
|
|
if (changedFlags & MTPDstickerSet_ClientFlag::f_unread) {
|
|
|
|
Local::writeFeaturedStickers();
|
|
|
|
}
|
2016-09-09 15:52:46 +00:00
|
|
|
if (changedFlags & MTPDstickerSet::Flag::f_archived) {
|
2017-11-05 17:07:27 +00:00
|
|
|
auto index = Auth().data().archivedStickerSetsOrderRef().indexOf(setId);
|
2016-09-09 15:52:46 +00:00
|
|
|
if (index >= 0) {
|
2017-11-05 17:07:27 +00:00
|
|
|
Auth().data().archivedStickerSetsOrderRef().removeAt(index);
|
2016-09-09 15:52:46 +00:00
|
|
|
Local::writeArchivedStickers();
|
|
|
|
}
|
|
|
|
}
|
2018-01-17 18:20:55 +00:00
|
|
|
Auth().data().notifyStickersUpdated();
|
2016-09-09 15:52:46 +00:00
|
|
|
}
|
|
|
|
|
2017-08-02 20:57:49 +00:00
|
|
|
void UndoInstallLocally(uint64 setId) {
|
2017-11-05 17:07:27 +00:00
|
|
|
auto &sets = Auth().data().stickerSetsRef();
|
2016-09-09 15:52:46 +00:00
|
|
|
auto it = sets.find(setId);
|
|
|
|
if (it == sets.end()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-25 12:28:48 +00:00
|
|
|
it->flags &= ~MTPDstickerSet::Flag::f_installed_date;
|
|
|
|
it->installDate = TimeId(0);
|
2016-09-09 15:52:46 +00:00
|
|
|
|
2017-11-05 17:07:27 +00:00
|
|
|
auto &order = Auth().data().stickerSetsOrderRef();
|
2016-09-09 15:52:46 +00:00
|
|
|
int currentIndex = order.indexOf(setId);
|
|
|
|
if (currentIndex >= 0) {
|
|
|
|
order.removeAt(currentIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
Local::writeInstalledStickers();
|
2018-01-17 18:20:55 +00:00
|
|
|
Auth().data().notifyStickersUpdated();
|
2016-09-09 15:52:46 +00:00
|
|
|
|
2017-09-15 17:34:41 +00:00
|
|
|
Ui::show(
|
|
|
|
Box<InformBox>(lang(lng_stickers_not_found)),
|
|
|
|
LayerOption::KeepOther);
|
2016-09-09 15:52:46 +00:00
|
|
|
}
|
|
|
|
|
2018-08-28 11:32:27 +00:00
|
|
|
bool IsFaved(not_null<const DocumentData*> document) {
|
|
|
|
const auto it = Auth().data().stickerSets().constFind(FavedSetId);
|
|
|
|
if (it == Auth().data().stickerSets().cend()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for (const auto sticker : it->stickers) {
|
|
|
|
if (sticker == document) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2017-08-02 20:57:49 +00:00
|
|
|
}
|
|
|
|
|
2017-08-05 11:57:23 +00:00
|
|
|
void CheckFavedLimit(Set &set) {
|
|
|
|
if (set.stickers.size() <= Global::StickersFavedLimit()) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-15 14:34:30 +00:00
|
|
|
void PushFavedToFront(
|
|
|
|
Set &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);
|
|
|
|
}
|
|
|
|
CheckFavedLimit(set);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MoveFavedToFront(Set &set, int index) {
|
|
|
|
Expects(index > 0 && index < set.stickers.size());
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-17 08:31:24 +00:00
|
|
|
void RequestSetToPushFaved(not_null<DocumentData*> document);
|
2017-08-15 14:34:30 +00:00
|
|
|
|
2018-09-21 16:28:46 +00:00
|
|
|
void SetIsFaved(not_null<DocumentData*> document, std::optional<std::vector<not_null<EmojiPtr>>> emojiList = std::nullopt) {
|
2017-11-05 17:07:27 +00:00
|
|
|
auto &sets = Auth().data().stickerSetsRef();
|
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()) {
|
2018-01-25 12:28:48 +00:00
|
|
|
it = sets.insert(FavedSetId, Set(
|
|
|
|
FavedSetId,
|
|
|
|
uint64(0),
|
|
|
|
Lang::Hard::FavedSetTitle(),
|
|
|
|
QString(),
|
|
|
|
0, // count
|
|
|
|
0, // hash
|
|
|
|
MTPDstickerSet_ClientFlag::f_special | 0,
|
|
|
|
TimeId(0)));
|
2017-08-03 11:15:57 +00:00
|
|
|
}
|
|
|
|
auto index = it->stickers.indexOf(document);
|
|
|
|
if (index == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (index > 0) {
|
2017-08-15 14:34:30 +00:00
|
|
|
MoveFavedToFront(*it, index);
|
2017-08-03 11:15:57 +00:00
|
|
|
} else if (emojiList) {
|
2017-08-15 14:34:30 +00:00
|
|
|
PushFavedToFront(*it, document, *emojiList);
|
|
|
|
} else if (auto list = GetEmojiListFromSet(document)) {
|
|
|
|
PushFavedToFront(*it, document, *list);
|
2017-08-03 11:15:57 +00:00
|
|
|
} else {
|
2017-08-15 14:34:30 +00:00
|
|
|
RequestSetToPushFaved(document);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Local::writeFavedStickers();
|
2018-01-17 18:20:55 +00:00
|
|
|
Auth().data().notifyStickersUpdated();
|
2017-09-16 16:53:41 +00:00
|
|
|
Auth().api().stickerSetInstalled(FavedSetId);
|
2017-08-15 14:34:30 +00:00
|
|
|
}
|
|
|
|
|
2017-08-17 08:31:24 +00:00
|
|
|
void RequestSetToPushFaved(not_null<DocumentData*> document) {
|
|
|
|
auto addAnyway = [document](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
|
|
|
}
|
|
|
|
}
|
|
|
|
SetIsFaved(document, std::move(list));
|
|
|
|
};
|
|
|
|
MTP::send(MTPmessages_GetStickerSet(document->sticker()->set), rpcDone([document, addAnyway](const MTPmessages_StickerSet &result) {
|
|
|
|
Expects(result.type() == mtpc_messages_stickerSet);
|
2017-08-17 08:31:24 +00:00
|
|
|
auto list = std::vector<not_null<EmojiPtr>>();
|
2017-08-15 14:34:30 +00:00
|
|
|
auto &d = result.c_messages_stickerSet();
|
|
|
|
list.reserve(d.vpacks.v.size());
|
|
|
|
for_const (auto &mtpPack, d.vpacks.v) {
|
|
|
|
auto &pack = mtpPack.c_stickerPack();
|
|
|
|
for_const (auto &documentId, pack.vdocuments.v) {
|
|
|
|
if (documentId.v == document->id) {
|
|
|
|
if (auto emoji = Ui::Emoji::Find(qs(mtpPack.c_stickerPack().vemoticon))) {
|
|
|
|
list.push_back(emoji);
|
2017-08-03 11:15:57 +00:00
|
|
|
}
|
2017-08-15 14:34:30 +00:00
|
|
|
break;
|
2017-08-03 11:15:57 +00:00
|
|
|
}
|
2017-08-15 14:34:30 +00:00
|
|
|
}
|
2017-08-03 11:15:57 +00:00
|
|
|
}
|
2017-08-15 14:34:30 +00:00
|
|
|
addAnyway(std::move(list));
|
|
|
|
}), rpcFail([addAnyway](const RPCError &error) {
|
|
|
|
if (MTP::isDefaultHandledError(error)) {
|
|
|
|
return false;
|
2017-08-03 11:15:57 +00:00
|
|
|
}
|
2017-08-15 14:34:30 +00:00
|
|
|
// Perhaps this is a deleted sticker pack. Add anyway.
|
|
|
|
addAnyway({});
|
|
|
|
return true;
|
|
|
|
}));
|
2017-08-03 11:15:57 +00:00
|
|
|
}
|
|
|
|
|
2017-08-17 08:31:24 +00:00
|
|
|
void SetIsNotFaved(not_null<DocumentData*> document) {
|
2017-11-05 17:07:27 +00:00
|
|
|
auto &sets = Auth().data().stickerSetsRef();
|
2017-08-03 11:15:57 +00:00
|
|
|
auto it = sets.find(FavedSetId);
|
|
|
|
if (it == sets.end()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto index = it->stickers.indexOf(document);
|
|
|
|
if (index < 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
it->stickers.removeAt(index);
|
|
|
|
for (auto i = it->emoji.begin(); i != it->emoji.end();) {
|
|
|
|
auto index = i->indexOf(document);
|
2017-08-02 20:57:49 +00:00
|
|
|
if (index >= 0) {
|
2017-08-03 11:15:57 +00:00
|
|
|
i->removeAt(index);
|
|
|
|
if (i->empty()) {
|
|
|
|
i = it->emoji.erase(i);
|
|
|
|
continue;
|
2017-08-02 20:57:49 +00:00
|
|
|
}
|
|
|
|
}
|
2017-08-03 11:15:57 +00:00
|
|
|
++i;
|
|
|
|
}
|
|
|
|
if (it->stickers.empty()) {
|
|
|
|
sets.erase(it);
|
|
|
|
}
|
|
|
|
Local::writeFavedStickers();
|
2018-01-17 18:20:55 +00:00
|
|
|
Auth().data().notifyStickersUpdated();
|
2017-08-03 11:15:57 +00:00
|
|
|
}
|
|
|
|
|
2017-08-17 08:31:24 +00:00
|
|
|
void SetFaved(not_null<DocumentData*> document, bool faved) {
|
2017-08-03 11:15:57 +00:00
|
|
|
if (faved) {
|
|
|
|
SetIsFaved(document);
|
|
|
|
} else {
|
|
|
|
SetIsNotFaved(document);
|
2017-08-02 20:57:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetsReceived(const QVector<MTPStickerSet> &data, int32 hash) {
|
2017-11-05 17:07:27 +00:00
|
|
|
auto &setsOrder = Auth().data().stickerSetsOrderRef();
|
2017-08-02 20:57:49 +00:00
|
|
|
setsOrder.clear();
|
|
|
|
|
2017-11-05 17:07:27 +00:00
|
|
|
auto &sets = Auth().data().stickerSetsRef();
|
2017-08-02 20:57:49 +00:00
|
|
|
QMap<uint64, uint64> setsToRequest;
|
|
|
|
for (auto &set : sets) {
|
|
|
|
if (!(set.flags & MTPDstickerSet::Flag::f_archived)) {
|
2018-01-25 12:28:48 +00:00
|
|
|
// Mark for removing.
|
|
|
|
set.flags &= ~MTPDstickerSet::Flag::f_installed_date;
|
|
|
|
set.installDate = 0;
|
2017-08-02 20:57:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for_const (auto &setData, data) {
|
|
|
|
if (setData.type() == mtpc_stickerSet) {
|
|
|
|
auto set = FeedSet(setData.c_stickerSet());
|
|
|
|
if (!(set->flags & MTPDstickerSet::Flag::f_archived) || (set->flags & MTPDstickerSet::Flag::f_official)) {
|
|
|
|
setsOrder.push_back(set->id);
|
|
|
|
if (set->stickers.isEmpty() || (set->flags & MTPDstickerSet_ClientFlag::f_not_loaded)) {
|
|
|
|
setsToRequest.insert(set->id, set->access);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
auto writeRecent = false;
|
2017-12-11 14:45:29 +00:00
|
|
|
auto &recent = GetRecentPack();
|
2017-08-02 20:57:49 +00:00
|
|
|
for (auto it = sets.begin(), e = sets.end(); it != e;) {
|
2018-01-25 12:28:48 +00:00
|
|
|
bool installed = (it->flags & MTPDstickerSet::Flag::f_installed_date);
|
2017-08-02 20:57:49 +00:00
|
|
|
bool featured = (it->flags & MTPDstickerSet_ClientFlag::f_featured);
|
|
|
|
bool special = (it->flags & MTPDstickerSet_ClientFlag::f_special);
|
|
|
|
bool archived = (it->flags & MTPDstickerSet::Flag::f_archived);
|
|
|
|
if (!installed) { // remove not mine sets from recent stickers
|
|
|
|
for (auto i = recent.begin(); i != recent.cend();) {
|
|
|
|
if (it->stickers.indexOf(i->first) >= 0) {
|
|
|
|
i = recent.erase(i);
|
|
|
|
writeRecent = true;
|
|
|
|
} else {
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (installed || featured || special || archived) {
|
|
|
|
++it;
|
|
|
|
} else {
|
|
|
|
it = sets.erase(it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!setsToRequest.isEmpty()) {
|
2017-08-04 14:54:32 +00:00
|
|
|
auto &api = Auth().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();
|
|
|
|
}
|
|
|
|
|
|
|
|
Local::writeInstalledStickers();
|
|
|
|
if (writeRecent) Local::writeUserSettings();
|
|
|
|
|
|
|
|
if (Local::countStickersHash() != hash) {
|
|
|
|
LOG(("API Error: received stickers hash %1 while counted hash is %2").arg(hash).arg(Local::countStickersHash()));
|
|
|
|
}
|
|
|
|
|
2018-01-17 18:20:55 +00:00
|
|
|
Auth().data().notifyStickersUpdated();
|
2017-08-02 20:57:49 +00:00
|
|
|
}
|
|
|
|
|
2018-03-07 17:43:26 +00:00
|
|
|
void SetPackAndEmoji(
|
|
|
|
Set &set,
|
|
|
|
Pack &&pack,
|
|
|
|
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();
|
|
|
|
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();
|
|
|
|
if (auto emoji = Ui::Emoji::Find(qs(pack.vemoticon))) {
|
|
|
|
emoji = emoji->original();
|
|
|
|
auto &stickers = pack.vdocuments.v;
|
|
|
|
|
2017-11-05 17:07:27 +00:00
|
|
|
auto p = Pack();
|
2017-08-03 11:15:57 +00:00
|
|
|
p.reserve(stickers.size());
|
|
|
|
for (auto j = 0, c = stickers.size(); j != c; ++j) {
|
2018-01-17 16:21:01 +00:00
|
|
|
auto document = Auth().data().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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-25 12:28:48 +00:00
|
|
|
void SpecialSetReceived(
|
|
|
|
uint64 setId,
|
|
|
|
const QString &setTitle,
|
|
|
|
const QVector<MTPDocument> &items,
|
|
|
|
int32 hash,
|
2018-03-07 17:43:26 +00:00
|
|
|
const QVector<MTPStickerPack> &packs,
|
|
|
|
const QVector<MTPint> &usageDates) {
|
2017-11-05 17:07:27 +00:00
|
|
|
auto &sets = Auth().data().stickerSetsRef();
|
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()) {
|
2018-01-25 12:28:48 +00:00
|
|
|
it = sets.insert(setId, Set(
|
|
|
|
setId,
|
|
|
|
uint64(0),
|
|
|
|
setTitle,
|
|
|
|
QString(),
|
|
|
|
0, // count
|
|
|
|
0, // hash
|
|
|
|
MTPDstickerSet_ClientFlag::f_special | 0,
|
|
|
|
TimeId(0)));
|
2017-08-02 20:57:49 +00:00
|
|
|
} else {
|
|
|
|
it->title = setTitle;
|
|
|
|
}
|
|
|
|
it->hash = hash;
|
|
|
|
|
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())
|
|
|
|
&& (setId == CloudRecentSetId);
|
2018-03-07 17:43:26 +00:00
|
|
|
|
2017-08-02 20:57:49 +00:00
|
|
|
auto custom = sets.find(CustomSetId);
|
2017-11-05 17:07:27 +00:00
|
|
|
auto pack = Pack();
|
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;
|
2019-01-18 12:27:37 +00:00
|
|
|
const auto document = Auth().data().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));
|
|
|
|
}
|
2017-08-02 20:57:49 +00:00
|
|
|
if (custom != sets.cend()) {
|
|
|
|
auto index = custom->stickers.indexOf(document);
|
|
|
|
if (index >= 0) {
|
|
|
|
custom->stickers.removeAt(index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (custom != sets.cend() && custom->stickers.isEmpty()) {
|
|
|
|
sets.erase(custom);
|
|
|
|
custom = sets.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto writeRecent = false;
|
2017-12-11 14:45:29 +00:00
|
|
|
auto &recent = GetRecentPack();
|
2017-08-02 20:57:49 +00:00
|
|
|
for (auto i = recent.begin(); i != recent.cend();) {
|
|
|
|
if (it->stickers.indexOf(i->first) >= 0 && pack.indexOf(i->first) < 0) {
|
|
|
|
i = recent.erase(i);
|
|
|
|
writeRecent = true;
|
|
|
|
} else {
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pack.isEmpty()) {
|
|
|
|
sets.erase(it);
|
|
|
|
} else {
|
2018-03-07 17:43:26 +00:00
|
|
|
SetPackAndEmoji(*it, std::move(pack), std::move(dates), packs);
|
2017-08-02 20:57:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (writeRecent) {
|
|
|
|
Local::writeUserSettings();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (setId) {
|
|
|
|
case CloudRecentSetId: {
|
|
|
|
if (Local::countRecentStickersHash() != hash) {
|
|
|
|
LOG(("API Error: received recent stickers hash %1 while counted hash is %2").arg(hash).arg(Local::countRecentStickersHash()));
|
|
|
|
}
|
|
|
|
Local::writeRecentStickers();
|
|
|
|
} break;
|
|
|
|
case FavedSetId: {
|
|
|
|
if (Local::countFavedStickersHash() != hash) {
|
|
|
|
LOG(("API Error: received faved stickers hash %1 while counted hash is %2").arg(hash).arg(Local::countFavedStickersHash()));
|
|
|
|
}
|
|
|
|
Local::writeFavedStickers();
|
|
|
|
} break;
|
|
|
|
default: Unexpected("setId in SpecialSetReceived()");
|
|
|
|
}
|
|
|
|
|
2018-01-17 18:20:55 +00:00
|
|
|
Auth().data().notifyStickersUpdated();
|
2017-08-02 20:57:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FeaturedSetsReceived(const QVector<MTPStickerSetCovered> &data, const QVector<MTPlong> &unread, int32 hash) {
|
|
|
|
OrderedSet<uint64> unreadMap;
|
|
|
|
for_const (auto &unreadSetId, unread) {
|
|
|
|
unreadMap.insert(unreadSetId.v);
|
|
|
|
}
|
|
|
|
|
2017-11-05 17:07:27 +00:00
|
|
|
auto &setsOrder = Auth().data().featuredStickerSetsOrderRef();
|
2017-08-02 20:57:49 +00:00
|
|
|
setsOrder.clear();
|
|
|
|
|
2017-11-05 17:07:27 +00:00
|
|
|
auto &sets = Auth().data().stickerSetsRef();
|
2017-08-02 20:57:49 +00:00
|
|
|
QMap<uint64, uint64> setsToRequest;
|
|
|
|
for (auto &set : sets) {
|
|
|
|
set.flags &= ~MTPDstickerSet_ClientFlag::f_featured; // mark for removing
|
|
|
|
}
|
|
|
|
for (int i = 0, l = data.size(); i != l; ++i) {
|
|
|
|
auto &setData = data[i];
|
|
|
|
const MTPDstickerSet *set = nullptr;
|
|
|
|
switch (setData.type()) {
|
|
|
|
case mtpc_stickerSetCovered: {
|
|
|
|
auto &d = setData.c_stickerSetCovered();
|
|
|
|
if (d.vset.type() == mtpc_stickerSet) {
|
|
|
|
set = &d.vset.c_stickerSet();
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
case mtpc_stickerSetMultiCovered: {
|
|
|
|
auto &d = setData.c_stickerSetMultiCovered();
|
|
|
|
if (d.vset.type() == mtpc_stickerSet) {
|
|
|
|
set = &d.vset.c_stickerSet();
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (set) {
|
|
|
|
auto it = sets.find(set->vid.v);
|
2018-01-25 12:28:48 +00:00
|
|
|
const auto title = GetSetTitle(*set);
|
|
|
|
const auto installDate = set->has_installed_date()
|
|
|
|
? set->vinstalled_date.v
|
|
|
|
: TimeId(0);
|
2017-08-02 20:57:49 +00:00
|
|
|
if (it == sets.cend()) {
|
2018-03-07 12:04:05 +00:00
|
|
|
auto setClientFlags = MTPDstickerSet_ClientFlag::f_featured
|
|
|
|
| MTPDstickerSet_ClientFlag::f_not_loaded;
|
2017-08-02 20:57:49 +00:00
|
|
|
if (unreadMap.contains(set->vid.v)) {
|
|
|
|
setClientFlags |= MTPDstickerSet_ClientFlag::f_unread;
|
|
|
|
}
|
2018-01-25 12:28:48 +00:00
|
|
|
it = sets.insert(set->vid.v, Set(
|
|
|
|
set->vid.v,
|
|
|
|
set->vaccess_hash.v,
|
|
|
|
title,
|
|
|
|
qs(set->vshort_name),
|
|
|
|
set->vcount.v,
|
|
|
|
set->vhash.v,
|
|
|
|
set->vflags.v | setClientFlags,
|
|
|
|
installDate));
|
2017-08-02 20:57:49 +00:00
|
|
|
} else {
|
|
|
|
it->access = set->vaccess_hash.v;
|
|
|
|
it->title = title;
|
|
|
|
it->shortName = qs(set->vshort_name);
|
|
|
|
auto clientFlags = it->flags & (MTPDstickerSet_ClientFlag::f_featured | MTPDstickerSet_ClientFlag::f_unread | MTPDstickerSet_ClientFlag::f_not_loaded | MTPDstickerSet_ClientFlag::f_special);
|
|
|
|
it->flags = set->vflags.v | clientFlags;
|
|
|
|
it->flags |= MTPDstickerSet_ClientFlag::f_featured;
|
2018-01-25 12:28:48 +00:00
|
|
|
it->installDate = installDate;
|
2017-08-02 20:57:49 +00:00
|
|
|
if (unreadMap.contains(it->id)) {
|
|
|
|
it->flags |= MTPDstickerSet_ClientFlag::f_unread;
|
|
|
|
} else {
|
|
|
|
it->flags &= ~MTPDstickerSet_ClientFlag::f_unread;
|
|
|
|
}
|
|
|
|
if (it->count != set->vcount.v || it->hash != set->vhash.v || it->emoji.isEmpty()) {
|
|
|
|
it->count = set->vcount.v;
|
|
|
|
it->hash = set->vhash.v;
|
|
|
|
it->flags |= MTPDstickerSet_ClientFlag::f_not_loaded; // need to request this set
|
|
|
|
}
|
|
|
|
}
|
|
|
|
setsOrder.push_back(set->vid.v);
|
|
|
|
if (it->stickers.isEmpty() || (it->flags & MTPDstickerSet_ClientFlag::f_not_loaded)) {
|
|
|
|
setsToRequest.insert(set->vid.v, set->vaccess_hash.v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto unreadCount = 0;
|
|
|
|
for (auto it = sets.begin(), e = sets.end(); it != e;) {
|
2018-01-25 12:28:48 +00:00
|
|
|
bool installed = (it->flags & MTPDstickerSet::Flag::f_installed_date);
|
2017-08-02 20:57:49 +00:00
|
|
|
bool featured = (it->flags & MTPDstickerSet_ClientFlag::f_featured);
|
|
|
|
bool special = (it->flags & MTPDstickerSet_ClientFlag::f_special);
|
|
|
|
bool archived = (it->flags & MTPDstickerSet::Flag::f_archived);
|
|
|
|
if (installed || featured || special || archived) {
|
|
|
|
if (featured && (it->flags & MTPDstickerSet_ClientFlag::f_unread)) {
|
|
|
|
++unreadCount;
|
|
|
|
}
|
|
|
|
++it;
|
|
|
|
} else {
|
|
|
|
it = sets.erase(it);
|
|
|
|
}
|
|
|
|
}
|
2017-11-05 17:07:27 +00:00
|
|
|
Auth().data().setFeaturedStickerSetsUnreadCount(unreadCount);
|
2017-08-02 20:57:49 +00:00
|
|
|
|
|
|
|
if (Local::countFeaturedStickersHash() != hash) {
|
|
|
|
LOG(("API Error: received featured stickers hash %1 while counted hash is %2").arg(hash).arg(Local::countFeaturedStickersHash()));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!setsToRequest.isEmpty()) {
|
2017-08-04 14:54:32 +00:00
|
|
|
auto &api = Auth().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();
|
|
|
|
}
|
|
|
|
|
|
|
|
Local::writeFeaturedStickers();
|
|
|
|
|
2018-01-17 18:20:55 +00:00
|
|
|
Auth().data().notifyStickersUpdated();
|
2017-08-02 20:57:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GifsReceived(const QVector<MTPDocument> &items, int32 hash) {
|
2017-11-05 17:07:27 +00:00
|
|
|
auto &saved = Auth().data().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) {
|
|
|
|
const auto document = Auth().data().processDocument(item);
|
2018-01-17 16:21:01 +00:00
|
|
|
if (!document->isGifv()) {
|
2019-01-18 12:27:37 +00:00
|
|
|
LOG(("API Error: "
|
|
|
|
"bad document returned in HistoryWidget::savedGifsGot!"));
|
2017-08-02 20:57:49 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
saved.push_back(document);
|
|
|
|
}
|
|
|
|
if (Local::countSavedGifsHash() != hash) {
|
|
|
|
LOG(("API Error: received saved gifs hash %1 while counted hash is %2").arg(hash).arg(Local::countSavedGifsHash()));
|
|
|
|
}
|
|
|
|
|
|
|
|
Local::writeSavedGifs();
|
|
|
|
|
2018-01-17 18:20:55 +00:00
|
|
|
Auth().data().notifySavedGifsUpdated();
|
2017-08-02 20:57:49 +00:00
|
|
|
}
|
|
|
|
|
2018-03-07 21:25:03 +00:00
|
|
|
std::vector<not_null<DocumentData*>> GetListByEmoji(
|
|
|
|
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>();
|
2017-11-05 17:07:27 +00:00
|
|
|
auto &sets = Auth().data().stickerSetsRef();
|
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) {
|
|
|
|
return TimeId(base + int((document->id ^ seed) % kSlice));
|
|
|
|
};
|
|
|
|
const auto CreateRecentSortKey = [&](not_null<DocumentData*> document) {
|
|
|
|
return CreateSortKey(document, kSlice * 4);
|
|
|
|
};
|
|
|
|
auto myCounter = 0;
|
|
|
|
const auto CreateMySortKey = [&] {
|
|
|
|
return (kSlice * 4 - (++myCounter));
|
|
|
|
};
|
|
|
|
const auto CreateFeaturedSortKey = [&](not_null<DocumentData*> document) {
|
|
|
|
return CreateSortKey(document, kSlice * 2);
|
|
|
|
};
|
|
|
|
const auto CreateOtherSortKey = [&](not_null<DocumentData*> document) {
|
|
|
|
return CreateSortKey(document, kSlice);
|
|
|
|
};
|
|
|
|
const auto InstallDate = [&](not_null<DocumentData*> document) {
|
|
|
|
Expects(document->sticker() != nullptr);
|
|
|
|
|
|
|
|
const auto sticker = document->sticker();
|
|
|
|
if (sticker->set.type() == mtpc_inputStickerSetID) {
|
|
|
|
const auto setId = sticker->set.c_inputStickerSetID().vid.v;
|
|
|
|
const auto setIt = sets.find(setId);
|
|
|
|
if (setIt != sets.end()) {
|
|
|
|
return setIt->installDate;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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()) {
|
|
|
|
auto i = recentIt->emoji.constFind(original);
|
|
|
|
if (i != recentIt->emoji.cend()) {
|
|
|
|
result.reserve(i->size());
|
|
|
|
for (const auto document : *i) {
|
|
|
|
const auto usageDate = [&] {
|
|
|
|
if (recentIt->dates.empty()) {
|
|
|
|
return TimeId(0);
|
|
|
|
}
|
|
|
|
const auto index = recentIt->stickers.indexOf(document);
|
|
|
|
if (index < 0) {
|
|
|
|
return TimeId(0);
|
|
|
|
}
|
|
|
|
Assert(index < recentIt->dates.size());
|
|
|
|
return recentIt->dates[index];
|
|
|
|
}();
|
|
|
|
const auto date = usageDate
|
|
|
|
? usageDate
|
|
|
|
: InstallDate(document);
|
|
|
|
result.push_back({
|
|
|
|
document,
|
|
|
|
date ? date : CreateRecentSortKey(document) });
|
|
|
|
}
|
2017-08-03 11:15:57 +00:00
|
|
|
}
|
|
|
|
}
|
2018-03-07 13:53:12 +00:00
|
|
|
const auto addList = [&](const Order &order, MTPDstickerSet::Flag skip) {
|
|
|
|
for (const auto setId : order) {
|
|
|
|
auto it = sets.find(setId);
|
|
|
|
if (it == sets.cend() || (it->flags & skip)) {
|
|
|
|
continue;
|
|
|
|
}
|
2017-08-03 11:15:57 +00:00
|
|
|
if (it->emoji.isEmpty()) {
|
2018-03-07 21:25:03 +00:00
|
|
|
setsToRequest.emplace(it->id, it->access);
|
2017-08-03 11:15:57 +00:00
|
|
|
it->flags |= MTPDstickerSet_ClientFlag::f_not_loaded;
|
2018-03-07 13:53:12 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
auto i = it->emoji.constFind(original);
|
|
|
|
if (i == it->emoji.cend()) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-03-07 21:25:03 +00:00
|
|
|
const auto my = (it->flags & MTPDstickerSet::Flag::f_installed_date);
|
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) {
|
|
|
|
const auto installDate = my ? it->installDate : TimeId(0);
|
|
|
|
const auto date = (installDate > 1)
|
|
|
|
? installDate
|
|
|
|
: my
|
|
|
|
? CreateMySortKey()
|
|
|
|
: CreateFeaturedSortKey(document);
|
|
|
|
add(document, date);
|
2017-08-03 11:15:57 +00:00
|
|
|
}
|
|
|
|
}
|
2018-03-07 13:53:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
addList(
|
|
|
|
Auth().data().stickerSetsOrder(),
|
|
|
|
MTPDstickerSet::Flag::f_archived);
|
2018-03-19 16:32:33 +00:00
|
|
|
//addList(
|
|
|
|
// Auth().data().featuredStickerSetsOrder(),
|
|
|
|
// MTPDstickerSet::Flag::f_installed_date);
|
2018-03-07 13:53:12 +00:00
|
|
|
|
2018-03-07 21:25:03 +00:00
|
|
|
if (!setsToRequest.empty()) {
|
|
|
|
for (const auto [setId, accessHash] : setsToRequest) {
|
|
|
|
Auth().api().scheduleStickerSetRequest(setId, accessHash);
|
2017-08-03 11:15:57 +00:00
|
|
|
}
|
2017-08-04 14:54:32 +00:00
|
|
|
Auth().api().requestStickerSets();
|
2017-08-03 11:15:57 +00:00
|
|
|
}
|
2018-03-07 21:25:03 +00:00
|
|
|
|
2018-03-19 16:32:33 +00:00
|
|
|
if (Global::SuggestStickersByEmoji()) {
|
|
|
|
const auto others = Auth().api().stickersByEmoji(original);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
ranges::action::sort(
|
|
|
|
result,
|
|
|
|
std::greater<>(),
|
|
|
|
[](const StickerWithDate &data) { return data.date; });
|
|
|
|
|
|
|
|
return ranges::view::all(
|
|
|
|
result
|
|
|
|
) | ranges::view::transform([](const StickerWithDate &data) {
|
|
|
|
return data.document;
|
|
|
|
}) | ranges::to_vector;
|
2017-08-03 11:15:57 +00:00
|
|
|
}
|
|
|
|
|
2018-09-21 16:28:46 +00:00
|
|
|
std::optional<std::vector<not_null<EmojiPtr>>> 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;
|
|
|
|
if (inputSet.type() != mtpc_inputStickerSetID) {
|
2018-09-21 16:28:46 +00:00
|
|
|
return std::nullopt;
|
2017-08-03 11:15:57 +00:00
|
|
|
}
|
2017-11-05 17:07:27 +00:00
|
|
|
auto &sets = Auth().data().stickerSets();
|
2017-08-03 11:15:57 +00:00
|
|
|
auto it = sets.constFind(inputSet.c_inputStickerSetID().vid.v);
|
|
|
|
if (it == sets.cend()) {
|
2018-09-21 16:28:46 +00:00
|
|
|
return std::nullopt;
|
2017-08-03 11:15:57 +00:00
|
|
|
}
|
2017-08-17 08:31:24 +00:00
|
|
|
auto result = std::vector<not_null<EmojiPtr>>();
|
2017-08-03 11:15:57 +00:00
|
|
|
for (auto i = it->emoji.cbegin(), e = it->emoji.cend(); i != e; ++i) {
|
|
|
|
if (i->contains(document)) {
|
|
|
|
result.push_back(i.key());
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
return std::move(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
|
|
|
}
|
|
|
|
|
2017-08-03 12:52:56 +00:00
|
|
|
Set *FeedSet(const MTPDstickerSet &set) {
|
2017-11-05 17:07:27 +00:00
|
|
|
auto &sets = Auth().data().stickerSetsRef();
|
2017-08-03 12:52:56 +00:00
|
|
|
auto it = sets.find(set.vid.v);
|
2017-08-12 07:17:33 +00:00
|
|
|
auto title = GetSetTitle(set);
|
2017-08-03 12:52:56 +00:00
|
|
|
auto flags = MTPDstickerSet::Flags(0);
|
|
|
|
if (it == sets.cend()) {
|
2018-01-25 12:28:48 +00:00
|
|
|
it = sets.insert(set.vid.v, Stickers::Set(
|
|
|
|
set.vid.v,
|
|
|
|
set.vaccess_hash.v,
|
|
|
|
title,
|
|
|
|
qs(set.vshort_name),
|
|
|
|
set.vcount.v,
|
|
|
|
set.vhash.v,
|
|
|
|
set.vflags.v | MTPDstickerSet_ClientFlag::f_not_loaded,
|
|
|
|
set.has_installed_date() ? set.vinstalled_date.v : TimeId(0)));
|
2017-08-03 12:52:56 +00:00
|
|
|
} else {
|
|
|
|
it->access = set.vaccess_hash.v;
|
|
|
|
it->title = title;
|
|
|
|
it->shortName = qs(set.vshort_name);
|
|
|
|
flags = it->flags;
|
2018-03-02 17:17:33 +00:00
|
|
|
auto clientFlags = it->flags
|
|
|
|
& (MTPDstickerSet_ClientFlag::f_featured
|
|
|
|
| MTPDstickerSet_ClientFlag::f_unread
|
|
|
|
| MTPDstickerSet_ClientFlag::f_not_loaded
|
|
|
|
| MTPDstickerSet_ClientFlag::f_special);
|
2017-08-03 12:52:56 +00:00
|
|
|
it->flags = set.vflags.v | clientFlags;
|
2018-01-25 12:28:48 +00:00
|
|
|
it->installDate = set.has_installed_date()
|
|
|
|
? set.vinstalled_date.v
|
|
|
|
: TimeId(0);
|
|
|
|
if (it->count != set.vcount.v
|
|
|
|
|| it->hash != set.vhash.v
|
|
|
|
|| it->emoji.isEmpty()) {
|
2017-08-03 12:52:56 +00:00
|
|
|
it->count = set.vcount.v;
|
|
|
|
it->hash = set.vhash.v;
|
|
|
|
it->flags |= MTPDstickerSet_ClientFlag::f_not_loaded; // need to request this set
|
|
|
|
}
|
|
|
|
}
|
|
|
|
auto changedFlags = (flags ^ it->flags);
|
|
|
|
if (changedFlags & MTPDstickerSet::Flag::f_archived) {
|
2017-11-05 17:07:27 +00:00
|
|
|
auto index = Auth().data().archivedStickerSetsOrder().indexOf(it->id);
|
2017-08-03 12:52:56 +00:00
|
|
|
if (it->flags & MTPDstickerSet::Flag::f_archived) {
|
|
|
|
if (index < 0) {
|
2017-11-05 17:07:27 +00:00
|
|
|
Auth().data().archivedStickerSetsOrderRef().push_front(it->id);
|
2017-08-03 12:52:56 +00:00
|
|
|
}
|
|
|
|
} else if (index >= 0) {
|
2017-11-05 17:07:27 +00:00
|
|
|
Auth().data().archivedStickerSetsOrderRef().removeAt(index);
|
2017-08-03 12:52:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return &it.value();
|
|
|
|
}
|
|
|
|
|
|
|
|
Set *FeedSetFull(const MTPmessages_StickerSet &data) {
|
|
|
|
Expects(data.type() == mtpc_messages_stickerSet);
|
|
|
|
Expects(data.c_messages_stickerSet().vset.type() == mtpc_stickerSet);
|
|
|
|
auto &d = data.c_messages_stickerSet();
|
|
|
|
auto set = FeedSet(d.vset.c_stickerSet());
|
|
|
|
|
|
|
|
set->flags &= ~MTPDstickerSet_ClientFlag::f_not_loaded;
|
|
|
|
|
2017-11-05 17:07:27 +00:00
|
|
|
auto &sets = Auth().data().stickerSetsRef();
|
2017-08-03 12:52:56 +00:00
|
|
|
auto &d_docs = d.vdocuments.v;
|
|
|
|
auto custom = sets.find(Stickers::CustomSetId);
|
|
|
|
|
2017-11-05 17:07:27 +00:00
|
|
|
auto pack = Pack();
|
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) {
|
|
|
|
const auto document = Auth().data().processDocument(item);
|
|
|
|
if (!document->sticker()) continue;
|
2017-08-03 12:52:56 +00:00
|
|
|
|
2019-01-18 12:27:37 +00:00
|
|
|
pack.push_back(document);
|
2017-08-03 12:52:56 +00:00
|
|
|
if (custom != sets.cend()) {
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (custom != sets.cend() && custom->stickers.isEmpty()) {
|
|
|
|
sets.erase(custom);
|
|
|
|
custom = sets.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto writeRecent = false;
|
2017-12-11 14:45:29 +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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pack.isEmpty()) {
|
2017-11-05 17:07:27 +00:00
|
|
|
int removeIndex = Auth().data().stickerSetsOrder().indexOf(set->id);
|
|
|
|
if (removeIndex >= 0) Auth().data().stickerSetsOrderRef().removeAt(removeIndex);
|
2017-08-03 12:52:56 +00:00
|
|
|
sets.remove(set->id);
|
|
|
|
set = nullptr;
|
|
|
|
} else {
|
|
|
|
set->stickers = pack;
|
|
|
|
set->emoji.clear();
|
|
|
|
auto &v = d.vpacks.v;
|
|
|
|
for (auto i = 0, l = v.size(); i != l; ++i) {
|
|
|
|
if (v[i].type() != mtpc_stickerPack) continue;
|
|
|
|
|
|
|
|
auto &pack = v[i].c_stickerPack();
|
|
|
|
if (auto emoji = Ui::Emoji::Find(qs(pack.vemoticon))) {
|
|
|
|
emoji = emoji->original();
|
|
|
|
auto &stickers = pack.vdocuments.v;
|
|
|
|
|
2017-11-05 17:07:27 +00:00
|
|
|
Pack p;
|
2017-08-03 12:52:56 +00:00
|
|
|
p.reserve(stickers.size());
|
|
|
|
for (auto j = 0, c = stickers.size(); j != c; ++j) {
|
2018-01-17 16:21:01 +00:00
|
|
|
auto doc = Auth().data().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) {
|
|
|
|
Local::writeUserSettings();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (set) {
|
2018-01-25 12:28:48 +00:00
|
|
|
if (set->flags & MTPDstickerSet::Flag::f_installed_date) {
|
2017-08-03 12:52:56 +00:00
|
|
|
if (!(set->flags & MTPDstickerSet::Flag::f_archived)) {
|
|
|
|
Local::writeInstalledStickers();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (set->flags & MTPDstickerSet_ClientFlag::f_featured) {
|
|
|
|
Local::writeFeaturedStickers();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-17 18:20:55 +00:00
|
|
|
Auth().data().notifyStickersUpdated();
|
2017-08-03 12:52:56 +00:00
|
|
|
|
|
|
|
return set;
|
|
|
|
}
|
|
|
|
|
2017-08-12 07:17:33 +00:00
|
|
|
QString GetSetTitle(const MTPDstickerSet &s) {
|
|
|
|
auto title = qs(s.vtitle);
|
|
|
|
if ((s.vflags.v & MTPDstickerSet::Flag::f_official) && !title.compare(qstr("Great Minds"), Qt::CaseInsensitive)) {
|
|
|
|
return lang(lng_stickers_default_set);
|
|
|
|
}
|
|
|
|
return title;
|
|
|
|
}
|
|
|
|
|
2017-12-11 14:45:29 +00:00
|
|
|
RecentStickerPack &GetRecentPack() {
|
|
|
|
if (cRecentStickers().isEmpty() && !cRecentStickersPreload().isEmpty()) {
|
|
|
|
const auto p = cRecentStickersPreload();
|
|
|
|
cSetRecentStickersPreload(RecentStickerPreload());
|
|
|
|
|
|
|
|
auto &recent = cRefRecentStickers();
|
|
|
|
recent.reserve(p.size());
|
|
|
|
for (const auto &preloaded : p) {
|
2018-01-17 16:21:01 +00:00
|
|
|
const auto document = Auth().data().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
|