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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
|
/*
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/notify/data_notify_settings.h"
#include "apiwrap.h"
#include "api/api_ringtones.h"
#include "base/unixtime.h"
#include "core/application.h"
#include "data/data_changes.h"
#include "data/data_channel.h"
#include "data/data_chat.h"
#include "data/data_document.h"
#include "data/data_file_origin.h"
#include "data/data_peer.h"
#include "data/data_forum.h"
#include "data/data_forum_topic.h"
#include "data/data_session.h"
#include "data/data_user.h"
#include "main/main_session.h"
#include "history/history.h"
#include "main/main_session.h"
#include "window/notifications_manager.h"
namespace Data {
namespace {
constexpr auto kMaxNotifyCheckDelay = 24 * 3600 * crl::time(1000);
[[nodiscard]] bool MutedFromUntil(TimeId until, crl::time *changesIn) {
const auto now = base::unixtime::now();
const auto result = (until > now) ? (until - now) : 0;
if (changesIn) {
*changesIn = (result > 0)
? std::min(result * crl::time(1000), kMaxNotifyCheckDelay)
: kMaxNotifyCheckDelay;
}
return (result > 0);
}
} // namespace
NotifySettings::NotifySettings(not_null<Session*> owner)
: _owner(owner)
, _unmuteByFinishedTimer([=] { unmuteByFinished(); }) {
}
void NotifySettings::request(not_null<PeerData*> peer) {
if (peer->notify().settingsUnknown()) {
peer->session().api().requestNotifySettings(
MTP_inputNotifyPeer(peer->input));
}
if (defaultSettings(peer).settingsUnknown()) {
peer->session().api().requestNotifySettings(peer->isUser()
? MTP_inputNotifyUsers()
: (peer->isChat() || peer->isMegagroup())
? MTP_inputNotifyChats()
: MTP_inputNotifyBroadcasts());
}
}
void NotifySettings::request(not_null<Data::Thread*> thread) {
if (const auto topic = thread->asTopic()) {
if (topic->notify().settingsUnknown()) {
topic->session().api().requestNotifySettings(
MTP_inputNotifyForumTopic(
topic->channel()->input,
MTP_int(topic->rootId())));
}
}
request(thread->peer());
}
void NotifySettings::apply(
const MTPNotifyPeer ¬ifyPeer,
const MTPPeerNotifySettings &settings) {
notifyPeer.match([&](const MTPDnotifyUsers &) {
apply(DefaultNotify::User, settings);
}, [&](const MTPDnotifyChats &) {
apply(DefaultNotify::Group, settings);
}, [&](const MTPDnotifyBroadcasts &) {
apply(DefaultNotify::Broadcast, settings);
}, [&](const MTPDnotifyPeer &data) {
apply(peerFromMTP(data.vpeer()), settings);
}, [&](const MTPDnotifyForumTopic &data) {
apply(peerFromMTP(data.vpeer()), data.vtop_msg_id().v, settings);
});
}
void NotifySettings::apply(
const MTPInputNotifyPeer ¬ifyPeer,
const MTPPeerNotifySettings &settings) {
const auto peerFromInput = [&](const MTPInputPeer &peer) {
return peer.match([&](const MTPDinputPeerSelf &) {
return _owner->session().userPeerId();
}, [](const MTPDinputPeerUser &data) {
return peerFromUser(data.vuser_id());
}, [](const MTPDinputPeerChat &data) {
return peerFromChat(data.vchat_id());
}, [](const MTPDinputPeerChannel &data) {
return peerFromChannel(data.vchannel_id());
}, [](const MTPDinputPeerUserFromMessage &data) -> PeerId {
Unexpected("From message peer in NotifySettings::apply.");
}, [](const MTPDinputPeerChannelFromMessage &data) -> PeerId {
Unexpected("From message peer in NotifySettings::apply.");
}, [](const MTPDinputPeerEmpty &) -> PeerId {
Unexpected("Empty peer in NotifySettings::apply.");
});
};
notifyPeer.match([&](const MTPDinputNotifyUsers &) {
apply(DefaultNotify::User, settings);
}, [&](const MTPDinputNotifyChats &) {
apply(DefaultNotify::Group, settings);
}, [&](const MTPDinputNotifyBroadcasts &) {
apply(DefaultNotify::Broadcast, settings);
}, [&](const MTPDinputNotifyPeer &data) {
apply(peerFromInput(data.vpeer()), settings);
}, [&](const MTPDinputNotifyForumTopic &data) {
apply(peerFromInput(data.vpeer()), data.vtop_msg_id().v, settings);
});
}
void NotifySettings::apply(
DefaultNotify type,
const MTPPeerNotifySettings &settings) {
if (defaultValue(type).settings.change(settings)) {
updateLocal(type);
Core::App().notifications().checkDelayed();
}
}
void NotifySettings::apply(
PeerId peerId,
const MTPPeerNotifySettings &settings) {
if (const auto peer = _owner->peerLoaded(peerId)) {
apply(peer, settings);
}
}
void NotifySettings::apply(
not_null<PeerData*> peer,
const MTPPeerNotifySettings &settings) {
if (peer->notify().change(settings)) {
updateLocal(peer);
Core::App().notifications().checkDelayed();
}
}
void NotifySettings::apply(
PeerId peerId,
MsgId topicRootId,
const MTPPeerNotifySettings &settings) {
if (const auto peer = _owner->peerLoaded(peerId)) {
if (const auto topic = peer->forumTopicFor(topicRootId)) {
apply(topic, settings);
}
}
}
void NotifySettings::apply(
not_null<Data::ForumTopic*> topic,
const MTPPeerNotifySettings &settings) {
if (topic->notify().change(settings)) {
updateLocal(topic);
Core::App().notifications().checkDelayed();
}
}
void NotifySettings::update(
not_null<Data::Thread*> thread,
Data::MuteValue muteForSeconds,
std::optional<bool> silentPosts,
std::optional<NotifySound> sound) {
if (thread->notify().change(muteForSeconds, silentPosts, sound)) {
updateLocal(thread);
thread->session().api().updateNotifySettingsDelayed(thread);
}
}
void NotifySettings::resetToDefault(not_null<Data::Thread*> thread) {
const auto empty = MTP_peerNotifySettings(
MTP_flags(0),
MTPBool(),
MTPBool(),
MTPint(),
MTPNotificationSound(),
MTPNotificationSound(),
MTPNotificationSound());
if (thread->notify().change(empty)) {
updateLocal(thread);
thread->session().api().updateNotifySettingsDelayed(thread);
}
}
void NotifySettings::update(
not_null<PeerData*> peer,
Data::MuteValue muteForSeconds,
std::optional<bool> silentPosts,
std::optional<NotifySound> sound) {
if (peer->notify().change(muteForSeconds, silentPosts, sound)) {
updateLocal(peer);
peer->session().api().updateNotifySettingsDelayed(peer);
}
}
void NotifySettings::resetToDefault(not_null<PeerData*> peer) {
const auto empty = MTP_peerNotifySettings(
MTP_flags(0),
MTPBool(),
MTPBool(),
MTPint(),
MTPNotificationSound(),
MTPNotificationSound(),
MTPNotificationSound());
if (peer->notify().change(empty)) {
updateLocal(peer);
peer->session().api().updateNotifySettingsDelayed(peer);
}
}
void NotifySettings::forumParentMuteUpdated(not_null<Data::Forum*> forum) {
forum->enumerateTopics([&](not_null<Data::ForumTopic*> topic) {
if (!topic->notify().settingsUnknown()) {
updateLocal(topic);
}
});
}
auto NotifySettings::defaultValue(DefaultNotify type)
-> DefaultValue & {
const auto index = static_cast<int>(type);
Assert(index >= 0 && index < base::array_size(_defaultValues));
return _defaultValues[index];
}
auto NotifySettings::defaultValue(DefaultNotify type) const
-> const DefaultValue & {
const auto index = static_cast<int>(type);
Assert(index >= 0 && index < base::array_size(_defaultValues));
return _defaultValues[index];
}
const PeerNotifySettings &NotifySettings::defaultSettings(
not_null<const PeerData*> peer) const {
return defaultSettings(peer->isUser()
? DefaultNotify::User
: (peer->isChat() || peer->isMegagroup())
? DefaultNotify::Group
: DefaultNotify::Broadcast);
}
const PeerNotifySettings &NotifySettings::defaultSettings(
DefaultNotify type) const {
return defaultValue(type).settings;
}
void NotifySettings::defaultUpdate(
DefaultNotify type,
Data::MuteValue muteForSeconds,
std::optional<bool> silentPosts,
std::optional<NotifySound> sound) {
auto &settings = defaultValue(type).settings;
if (settings.change(muteForSeconds, silentPosts, sound)) {
updateLocal(type);
_owner->session().api().updateNotifySettingsDelayed(type);
}
}
void NotifySettings::updateLocal(not_null<Data::Thread*> thread) {
const auto topic = thread->asTopic();
if (!topic) {
return updateLocal(thread->peer());
}
auto changesIn = crl::time(0);
const auto muted = isMuted(topic, &changesIn);
topic->setMuted(muted);
if (muted) {
auto &lifetime = _mutedTopics.emplace(
topic,
rpl::lifetime()).first->second;
topic->destroyed() | rpl::start_with_next([=] {
_mutedTopics.erase(topic);
}, lifetime);
unmuteByFinishedDelayed(changesIn);
Core::App().notifications().clearIncomingFromTopic(topic);
} else {
_mutedTopics.erase(topic);
}
cacheSound(topic->notify().sound());
}
void NotifySettings::updateLocal(not_null<PeerData*> peer) {
const auto history = _owner->historyLoaded(peer->id);
auto changesIn = crl::time(0);
const auto muted = isMuted(peer, &changesIn);
const auto changeInHistory = history && (history->muted() != muted);
if (changeInHistory) {
history->setMuted(muted);
// Notification already sent.
} else {
peer->session().changes().peerUpdated(
peer,
PeerUpdate::Flag::Notifications);
}
if (muted) {
_mutedPeers.emplace(peer);
unmuteByFinishedDelayed(changesIn);
if (history) {
Core::App().notifications().clearIncomingFromHistory(history);
}
} else {
_mutedPeers.erase(peer);
}
cacheSound(peer->notify().sound());
}
void NotifySettings::cacheSound(DocumentId id) {
cacheSound(_owner->document(id));
}
void NotifySettings::cacheSound(not_null<DocumentData*> document) {
if (document->isNull()) {
return;
}
const auto view = document->createMediaView();
_ringtones.views.emplace(document->id, view);
document->forceToCache(true);
document->save(Data::FileOriginRingtones(), QString());
}
void NotifySettings::cacheSound(const std::optional<NotifySound> &sound) {
if (!sound || !sound->id) {
return;
} else if (const auto doc = _owner->document(sound->id); !doc->isNull()) {
cacheSound(doc);
return;
}
_ringtones.pendingIds.push_back(sound->id);
if (_ringtones.pendingLifetime) {
return;
}
// Not requested yet.
_owner->session().api().ringtones().listUpdates(
) | rpl::start_with_next([=] {
for (const auto id : base::take(_ringtones.pendingIds)) {
cacheSound(id);
}
_ringtones.pendingLifetime.destroy();
}, _ringtones.pendingLifetime);
_owner->session().api().ringtones().requestList();
}
void NotifySettings::updateLocal(DefaultNotify type) {
defaultValue(type).updates.fire({});
const auto goodForUpdate = [&](
not_null<const PeerData*> peer,
const PeerNotifySettings &settings) {
auto &peers = peer->notify();
return !peers.settingsUnknown()
&& ((!peers.muteUntil() && settings.muteUntil())
|| (!peers.silentPosts() && settings.silentPosts())
|| (!peers.sound() && settings.sound()));
};
const auto callback = [&](not_null<PeerData*> peer) {
if (goodForUpdate(peer, defaultSettings(type))) {
updateLocal(peer);
}
};
switch (type) {
case DefaultNotify::User: _owner->enumerateUsers(callback); break;
case DefaultNotify::Group: _owner->enumerateGroups(callback); break;
case DefaultNotify::Broadcast:
_owner->enumerateBroadcasts(callback);
break;
}
cacheSound(defaultValue(type).settings.sound());
}
std::shared_ptr<DocumentMedia> NotifySettings::lookupRingtone(
DocumentId id) const {
if (!id) {
return nullptr;
}
const auto it = _ringtones.views.find(id);
return (it == end(_ringtones.views)) ? nullptr : it->second;
}
void NotifySettings::unmuteByFinishedDelayed(crl::time delay) {
accumulate_min(delay, kMaxNotifyCheckDelay);
if (!_unmuteByFinishedTimer.isActive()
|| _unmuteByFinishedTimer.remainingTime() > delay) {
_unmuteByFinishedTimer.callOnce(delay);
}
}
void NotifySettings::unmuteByFinished() {
auto changesInMin = crl::time(0);
for (auto i = begin(_mutedPeers); i != end(_mutedPeers);) {
const auto history = _owner->historyLoaded((*i)->id);
auto changesIn = crl::time(0);
const auto muted = isMuted(*i, &changesIn);
if (history) {
history->setMuted(muted);
}
if (muted) {
if (!changesInMin || changesInMin > changesIn) {
changesInMin = changesIn;
}
++i;
} else {
i = _mutedPeers.erase(i);
}
}
for (auto i = begin(_mutedTopics); i != end(_mutedTopics);) {
auto changesIn = crl::time(0);
const auto topic = i->first;
const auto muted = isMuted(topic, &changesIn);
topic->setMuted(muted);
if (muted) {
if (!changesInMin || changesInMin > changesIn) {
changesInMin = changesIn;
}
++i;
} else {
i = _mutedTopics.erase(i);
}
}
if (changesInMin) {
unmuteByFinishedDelayed(changesInMin);
}
}
bool NotifySettings::isMuted(
not_null<const Data::Thread*> thread,
crl::time *changesIn) const {
const auto topic = thread->asTopic();
const auto until = topic ? topic->notify().muteUntil() : std::nullopt;
return until
? MutedFromUntil(*until, changesIn)
: isMuted(thread->peer(), changesIn);
}
bool NotifySettings::isMuted(not_null<const Data::Thread*> thread) const {
return isMuted(thread, nullptr);
}
NotifySound NotifySettings::sound(
not_null<const Data::Thread*> thread) const {
const auto topic = thread->asTopic();
const auto sound = topic ? topic->notify().sound() : std::nullopt;
return sound ? *sound : this->sound(thread->peer());
}
bool NotifySettings::muteUnknown(
not_null<const Data::Thread*> thread) const {
const auto topic = thread->asTopic();
return (topic && topic->notify().settingsUnknown())
|| ((!topic || !topic->notify().muteUntil().has_value())
&& muteUnknown(thread->peer()));
}
bool NotifySettings::soundUnknown(
not_null<const Data::Thread*> thread) const {
const auto topic = thread->asTopic();
return (topic && topic->notify().settingsUnknown())
|| ((!topic || !topic->notify().sound().has_value())
&& soundUnknown(topic->channel()));
}
bool NotifySettings::isMuted(
not_null<const PeerData*> peer,
crl::time *changesIn) const {
if (const auto until = peer->notify().muteUntil()) {
return MutedFromUntil(*until, changesIn);
} else if (const auto until = defaultSettings(peer).muteUntil()) {
return MutedFromUntil(*until, changesIn);
}
return true;
}
bool NotifySettings::isMuted(not_null<const PeerData*> peer) const {
return isMuted(peer, nullptr);
}
bool NotifySettings::silentPosts(not_null<const PeerData*> peer) const {
if (const auto silent = peer->notify().silentPosts()) {
return *silent;
} else if (const auto silent = defaultSettings(peer).silentPosts()) {
return *silent;
}
return false;
}
NotifySound NotifySettings::sound(not_null<const PeerData*> peer) const {
// Explicitly ignore a notify sound for Saved Messages
// to follow the global notify sound.
if (const auto sound = peer->notify().sound(); !peer->isSelf() && sound) {
return *sound;
} else if (const auto sound = defaultSettings(peer).sound()) {
return *sound;
}
return {};
}
bool NotifySettings::muteUnknown(not_null<const PeerData*> peer) const {
return peer->notify().settingsUnknown()
|| (!peer->notify().muteUntil().has_value()
&& defaultSettings(peer).settingsUnknown());
}
bool NotifySettings::silentPostsUnknown(
not_null<const PeerData*> peer) const {
return peer->notify().settingsUnknown()
|| (!peer->notify().silentPosts().has_value()
&& defaultSettings(peer).settingsUnknown());
}
bool NotifySettings::soundUnknown(
not_null<const PeerData*> peer) const {
return peer->notify().settingsUnknown()
|| (!peer->notify().sound().has_value()
&& defaultSettings(peer).settingsUnknown());
}
bool NotifySettings::settingsUnknown(not_null<const PeerData*> peer) const {
return muteUnknown(peer)
|| silentPostsUnknown(peer)
|| soundUnknown(peer);
}
bool NotifySettings::settingsUnknown(
not_null<const Data::Thread*> thread) const {
const auto topic = thread->asTopic();
return muteUnknown(thread)
|| soundUnknown(thread)
|| (!topic && silentPostsUnknown(thread->peer()));
}
rpl::producer<> NotifySettings::defaultUpdates(DefaultNotify type) const {
return defaultValue(type).updates.events();
}
rpl::producer<> NotifySettings::defaultUpdates(
not_null<const PeerData*> peer) const {
return defaultUpdates(peer->isUser()
? DefaultNotify::User
: (peer->isChat() || peer->isMegagroup())
? DefaultNotify::Group
: DefaultNotify::Broadcast);
}
} // namespace Data
|