2
0
mirror of https://github.com/telegramdesktop/tdesktop synced 2025-08-31 06:26:18 +00:00

Destroy messages by ttl_period dates.

This commit is contained in:
John Preston
2021-02-02 18:42:26 +04:00
parent 9262b773cb
commit 046a3906c4
6 changed files with 110 additions and 0 deletions

View File

@@ -220,6 +220,7 @@ Session::Session(not_null<Main::Session*> session)
session->serverConfig().pinnedDialogsCountMax.value())
, _contactsList(Dialogs::SortMode::Name)
, _contactsNoChatsList(Dialogs::SortMode::Name)
, _ttlCheckTimer([=] { checkTTLs(); })
, _selfDestructTimer([=] { checkSelfDestructItems(); })
, _sendActionsAnimation([=](crl::time now) {
return sendActionsAnimationCallback(now);
@@ -1959,6 +1960,54 @@ void Session::registerMessage(not_null<HistoryItem*> item) {
list->emplace(itemId, item);
}
void Session::registerMessageTTL(TimeId when, not_null<HistoryItem*> item) {
Expects(when > 0);
auto &list = _ttlMessages[when];
list.emplace(item);
const auto nearest = _ttlMessages.begin()->first;
if (nearest < when && _ttlCheckTimer.isActive()) {
return;
}
scheduleNextTTLs();
}
void Session::scheduleNextTTLs() {
if (_ttlMessages.empty()) {
return;
}
const auto nearest = _ttlMessages.begin()->first;
const auto now = base::unixtime::now();
const auto timeout = (std::max(now, nearest) - now) * crl::time(1000);
_ttlCheckTimer.callOnce(timeout);
}
void Session::unregisterMessageTTL(
TimeId when,
not_null<HistoryItem*> item) {
Expects(when > 0);
const auto i = _ttlMessages.find(when);
if (i == end(_ttlMessages)) {
return;
}
auto &list = i->second;
list.erase(item);
if (list.empty()) {
_ttlMessages.erase(i);
}
}
void Session::checkTTLs() {
_ttlCheckTimer.cancel();
const auto now = base::unixtime::now();
while (!_ttlMessages.empty() && _ttlMessages.begin()->first <= now) {
_ttlMessages.begin()->second.front()->destroy();
}
scheduleNextTTLs();
}
void Session::processMessagesDeleted(
ChannelId channelId,
const QVector<MTPint> &data) {