File: data_forum_icons.cpp

package info (click to toggle)
telegram-desktop 4.6.5%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 53,300 kB
  • sloc: cpp: 605,857; python: 3,978; ansic: 1,636; sh: 965; makefile: 841; objc: 652; javascript: 187; xml: 165
file content (126 lines) | stat: -rw-r--r-- 3,186 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.

For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#include "data/data_forum_icons.h"

#include "main/main_session.h"
#include "data/data_session.h"
#include "data/data_document.h"
#include "data/data_forum.h"
#include "data/data_forum_topic.h"
#include "apiwrap.h"

namespace Data {
namespace {

constexpr auto kRefreshDefaultListEach = 60 * 60 * crl::time(1000);
constexpr auto kRecentRequestTimeout = 10 * crl::time(1000);
constexpr auto kMaxTimeout = 6 * 60 * 60 * crl::time(1000);

} // namespace

ForumIcons::ForumIcons(not_null<Session*> owner)
: _owner(owner)
, _resetUserpicsTimer([=] { resetUserpics(); }) {
}

ForumIcons::~ForumIcons() = default;

Main::Session &ForumIcons::session() const {
	return _owner->session();
}

void ForumIcons::requestDefaultIfUnknown() {
	if (_default.empty()) {
		requestDefault();
	}
}

void ForumIcons::refreshDefault() {
	requestDefault();
}

const std::vector<DocumentId> &ForumIcons::list() const {
	return _default;
}

rpl::producer<> ForumIcons::defaultUpdates() const {
	return _defaultUpdated.events();
}

void ForumIcons::requestDefault() {
	if (_defaultRequestId) {
		return;
	}
	auto &api = _owner->session().api();
	_defaultRequestId = api.request(MTPmessages_GetStickerSet(
		MTP_inputStickerSetEmojiDefaultTopicIcons(),
		MTP_int(0) // hash
	)).done([=](const MTPmessages_StickerSet &result) {
		_defaultRequestId = 0;
		result.match([&](const MTPDmessages_stickerSet &data) {
			updateDefault(data);
		}, [](const MTPDmessages_stickerSetNotModified &) {
			LOG(("API Error: Unexpected messages.stickerSetNotModified."));
		});
	}).fail([=] {
		_defaultRequestId = 0;
	}).send();
}

void ForumIcons::updateDefault(const MTPDmessages_stickerSet &data) {
	const auto &list = data.vdocuments().v;
	_default.clear();
	_default.reserve(list.size());
	for (const auto &sticker : list) {
		_default.push_back(_owner->processDocument(sticker)->id);
	}
	_defaultUpdated.fire({});
}

void ForumIcons::scheduleUserpicsReset(not_null<Forum*> forum) {
	const auto duration = crl::time(st::slideDuration);
	_resetUserpicsWhen[forum] = crl::now() + duration;
	if (!_resetUserpicsTimer.isActive()) {
		_resetUserpicsTimer.callOnce(duration);
	}
}

void ForumIcons::clearUserpicsReset(not_null<Forum*> forum) {
	_resetUserpicsWhen.remove(forum);
}

void ForumIcons::resetUserpics() {
	auto nearest = crl::time();
	auto now = crl::now();
	for (auto i = begin(_resetUserpicsWhen); i != end(_resetUserpicsWhen);) {
		if (i->second > now) {
			if (!nearest || nearest > i->second) {
				nearest = i->second;
			}
			++i;
		} else {
			const auto forum = i->first;
			i = _resetUserpicsWhen.erase(i);
			resetUserpicsFor(forum);
		}
	}
	if (nearest) {
		_resetUserpicsTimer.callOnce(
			std::min(nearest - now, 86400 * crl::time(1000)));
	} else {
		_resetUserpicsTimer.cancel();
	}
}

void ForumIcons::resetUserpicsFor(not_null<Forum*> forum) {
	forum->enumerateTopics([](not_null<ForumTopic*> topic) {
		topic->clearUserpicLoops();
	});
}

} // namespace Data