2016-10-02 09:30:28 +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-10-02 09:30:28 +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-10-02 09:30:28 +00:00
|
|
|
*/
|
2016-10-02 13:54:27 +00:00
|
|
|
#include "window/notifications_manager.h"
|
2016-10-02 09:30:28 +00:00
|
|
|
|
|
|
|
#include "platform/platform_notifications_manager.h"
|
2016-10-02 13:54:27 +00:00
|
|
|
#include "window/notifications_manager_default.h"
|
2019-02-13 12:36:59 +00:00
|
|
|
#include "media/audio/media_audio_track.h"
|
|
|
|
#include "media/audio/media_audio.h"
|
2018-01-13 12:45:11 +00:00
|
|
|
#include "history/history.h"
|
2017-12-18 15:44:50 +00:00
|
|
|
#include "history/history_item_components.h"
|
2018-02-12 11:10:40 +00:00
|
|
|
#include "history/feed/history_feed_section.h"
|
2017-04-13 08:27:10 +00:00
|
|
|
#include "lang/lang_keys.h"
|
2018-04-09 17:48:29 +00:00
|
|
|
#include "data/data_session.h"
|
2019-01-04 11:09:48 +00:00
|
|
|
#include "data/data_channel.h"
|
2018-02-12 11:10:40 +00:00
|
|
|
#include "window/window_controller.h"
|
2019-01-21 13:42:21 +00:00
|
|
|
#include "core/application.h"
|
2016-10-02 16:32:46 +00:00
|
|
|
#include "mainwindow.h"
|
2016-10-02 17:06:34 +00:00
|
|
|
#include "mainwidget.h"
|
2017-03-04 19:36:59 +00:00
|
|
|
#include "apiwrap.h"
|
|
|
|
#include "auth_session.h"
|
2016-10-02 09:30:28 +00:00
|
|
|
|
|
|
|
namespace Window {
|
|
|
|
namespace Notifications {
|
2017-12-04 17:46:03 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
// not more than one sound in 500ms from one peer - grouping
|
2019-02-19 06:57:53 +00:00
|
|
|
constexpr auto kMinimalAlertDelay = crl::time(500);
|
2019-03-10 07:42:25 +00:00
|
|
|
constexpr auto kWaitingForAllGroupedDelay = crl::time(1000);
|
2017-12-04 17:46:03 +00:00
|
|
|
|
|
|
|
} // namespace
|
2016-10-02 09:30:28 +00:00
|
|
|
|
2019-01-18 11:26:43 +00:00
|
|
|
System::System(AuthSession *session)
|
|
|
|
: _authSession(session)
|
2019-03-10 07:42:25 +00:00
|
|
|
, _waitTimer([=] { showNext(); })
|
|
|
|
, _waitForAllGroupedTimer([=] { showGrouped(); }) {
|
2017-03-04 19:36:59 +00:00
|
|
|
createManager();
|
|
|
|
|
2019-01-18 11:26:43 +00:00
|
|
|
subscribe(settingsChanged(), [=](ChangeType type) {
|
2017-03-04 19:36:59 +00:00
|
|
|
if (type == ChangeType::DesktopEnabled) {
|
|
|
|
App::wnd()->updateTrayMenu();
|
|
|
|
clearAll();
|
|
|
|
} else if (type == ChangeType::ViewParams) {
|
|
|
|
updateAll();
|
2018-12-04 10:32:06 +00:00
|
|
|
} else if (type == ChangeType::IncludeMuted
|
|
|
|
|| type == ChangeType::CountMessages) {
|
2017-03-04 19:36:59 +00:00
|
|
|
Notify::unreadCounterUpdated();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void System::createManager() {
|
|
|
|
_manager = Platform::Notifications::Create(this);
|
|
|
|
if (!_manager) {
|
|
|
|
_manager = std::make_unique<Default::Manager>(this);
|
|
|
|
}
|
2016-10-02 09:30:28 +00:00
|
|
|
}
|
|
|
|
|
2017-03-04 19:36:59 +00:00
|
|
|
void System::schedule(History *history, HistoryItem *item) {
|
2018-12-17 07:01:30 +00:00
|
|
|
if (App::quitting()
|
|
|
|
|| !history->currentNotification()
|
|
|
|
|| !AuthSession::Exists()) return;
|
2017-03-04 19:36:59 +00:00
|
|
|
|
2018-04-09 17:48:29 +00:00
|
|
|
const auto notifyBy = (!history->peer->isUser() && item->mentionsMe())
|
2018-01-13 12:45:11 +00:00
|
|
|
? item->from().get()
|
|
|
|
: nullptr;
|
2017-03-04 19:36:59 +00:00
|
|
|
|
|
|
|
if (item->isSilent()) {
|
|
|
|
history->popNotification(item);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-09 17:48:29 +00:00
|
|
|
Auth().data().requestNotifySettings(history->peer);
|
|
|
|
if (notifyBy) {
|
|
|
|
Auth().data().requestNotifySettings(notifyBy);
|
|
|
|
}
|
|
|
|
auto haveSetting = !Auth().data().notifyMuteUnknown(history->peer);
|
|
|
|
if (haveSetting && Auth().data().notifyIsMuted(history->peer)) {
|
|
|
|
if (notifyBy) {
|
|
|
|
haveSetting = !Auth().data().notifyMuteUnknown(notifyBy);
|
|
|
|
if (haveSetting) {
|
|
|
|
if (Auth().data().notifyIsMuted(notifyBy)) {
|
|
|
|
history->popNotification(item);
|
|
|
|
return;
|
2017-03-04 19:36:59 +00:00
|
|
|
}
|
|
|
|
}
|
2018-04-09 17:48:29 +00:00
|
|
|
} else {
|
|
|
|
history->popNotification(item);
|
|
|
|
return;
|
2017-03-04 19:36:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!item->notificationReady()) {
|
|
|
|
haveSetting = false;
|
|
|
|
}
|
|
|
|
|
2018-04-09 17:48:29 +00:00
|
|
|
auto delay = item->Has<HistoryMessageForwarded>() ? 500 : 100;
|
|
|
|
auto t = unixtime();
|
2019-02-19 06:57:53 +00:00
|
|
|
auto ms = crl::now();
|
2017-03-04 19:36:59 +00:00
|
|
|
bool isOnline = App::main()->lastWasOnline(), otherNotOld = ((cOtherOnline() * 1000LL) + Global::OnlineCloudTimeout() > t * 1000LL);
|
|
|
|
bool otherLaterThanMe = (cOtherOnline() * 1000LL + (ms - App::main()->lastSetOnline()) > t * 1000LL);
|
|
|
|
if (!isOnline && otherNotOld && otherLaterThanMe) {
|
|
|
|
delay = Global::NotifyCloudDelay();
|
|
|
|
} else if (cOtherOnline() >= t) {
|
|
|
|
delay = Global::NotifyDefaultDelay();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto when = ms + delay;
|
2018-04-09 17:48:29 +00:00
|
|
|
_whenAlerts[history].insert(when, notifyBy);
|
2017-03-04 19:36:59 +00:00
|
|
|
if (Global::DesktopNotify() && !Platform::Notifications::SkipToast()) {
|
|
|
|
auto &whenMap = _whenMaps[history];
|
|
|
|
if (whenMap.constFind(item->id) == whenMap.cend()) {
|
|
|
|
whenMap.insert(item->id, when);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto &addTo = haveSetting ? _waiters : _settingWaiters;
|
|
|
|
auto it = addTo.constFind(history);
|
|
|
|
if (it == addTo.cend() || it->when > when) {
|
2018-04-09 17:48:29 +00:00
|
|
|
addTo.insert(history, Waiter(item->id, when, notifyBy));
|
2017-03-04 19:36:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (haveSetting) {
|
|
|
|
if (!_waitTimer.isActive() || _waitTimer.remainingTime() > delay) {
|
2019-01-18 11:26:43 +00:00
|
|
|
_waitTimer.callOnce(delay);
|
2017-03-04 19:36:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void System::clearAll() {
|
|
|
|
_manager->clearAll();
|
|
|
|
|
|
|
|
for (auto i = _whenMaps.cbegin(), e = _whenMaps.cend(); i != e; ++i) {
|
|
|
|
i.key()->clearNotifications();
|
|
|
|
}
|
|
|
|
_whenMaps.clear();
|
|
|
|
_whenAlerts.clear();
|
|
|
|
_waiters.clear();
|
|
|
|
_settingWaiters.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void System::clearFromHistory(History *history) {
|
|
|
|
_manager->clearFromHistory(history);
|
|
|
|
|
|
|
|
history->clearNotifications();
|
|
|
|
_whenMaps.remove(history);
|
|
|
|
_whenAlerts.remove(history);
|
|
|
|
_waiters.remove(history);
|
|
|
|
_settingWaiters.remove(history);
|
|
|
|
|
2019-01-18 11:26:43 +00:00
|
|
|
_waitTimer.cancel();
|
2017-03-04 19:36:59 +00:00
|
|
|
showNext();
|
|
|
|
}
|
|
|
|
|
|
|
|
void System::clearFromItem(HistoryItem *item) {
|
|
|
|
_manager->clearFromItem(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
void System::clearAllFast() {
|
|
|
|
_manager->clearAllFast();
|
|
|
|
|
|
|
|
_whenMaps.clear();
|
|
|
|
_whenAlerts.clear();
|
|
|
|
_waiters.clear();
|
|
|
|
_settingWaiters.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void System::checkDelayed() {
|
|
|
|
for (auto i = _settingWaiters.begin(); i != _settingWaiters.end();) {
|
2017-12-04 17:46:03 +00:00
|
|
|
const auto history = i.key();
|
|
|
|
const auto peer = history->peer;
|
|
|
|
auto loaded = false;
|
|
|
|
auto muted = false;
|
2018-04-09 17:48:29 +00:00
|
|
|
if (!Auth().data().notifyMuteUnknown(peer)) {
|
|
|
|
if (!Auth().data().notifyIsMuted(peer)) {
|
2017-03-04 19:36:59 +00:00
|
|
|
loaded = true;
|
2018-04-09 17:48:29 +00:00
|
|
|
} else if (const auto from = i.value().notifyBy) {
|
|
|
|
if (!Auth().data().notifyMuteUnknown(from)) {
|
|
|
|
if (!Auth().data().notifyIsMuted(from)) {
|
2017-03-04 19:36:59 +00:00
|
|
|
loaded = true;
|
|
|
|
} else {
|
|
|
|
loaded = muted = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
loaded = muted = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (loaded) {
|
2017-12-04 17:46:03 +00:00
|
|
|
const auto fullId = FullMsgId(
|
|
|
|
history->channelId(),
|
|
|
|
i.value().msg);
|
|
|
|
if (const auto item = App::histItemById(fullId)) {
|
2017-03-04 19:36:59 +00:00
|
|
|
if (!item->notificationReady()) {
|
|
|
|
loaded = false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
muted = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (loaded) {
|
|
|
|
if (!muted) {
|
|
|
|
_waiters.insert(i.key(), i.value());
|
|
|
|
}
|
|
|
|
i = _settingWaiters.erase(i);
|
|
|
|
} else {
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
2019-01-18 11:26:43 +00:00
|
|
|
_waitTimer.cancel();
|
2017-03-04 19:36:59 +00:00
|
|
|
showNext();
|
|
|
|
}
|
|
|
|
|
2019-03-10 07:42:25 +00:00
|
|
|
void System::showGrouped() {
|
|
|
|
if (const auto lastItem = App::histItemById(_lastHistoryItemId)) {
|
|
|
|
_waitForAllGroupedTimer.cancel();
|
|
|
|
_manager->showNotification(lastItem, _lastForwardedCount);
|
|
|
|
_lastForwardedCount = 0;
|
|
|
|
_lastHistoryItemId = FullMsgId();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-04 19:36:59 +00:00
|
|
|
void System::showNext() {
|
|
|
|
if (App::quitting()) return;
|
|
|
|
|
2019-03-10 07:42:25 +00:00
|
|
|
const auto isSameGroup = [=](HistoryItem *item) {
|
|
|
|
if (!_lastHistoryItemId || !item) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (const auto lastItem = App::histItemById(_lastHistoryItemId)) {
|
|
|
|
return (lastItem->groupId() == item->groupId() || lastItem->author() == item->author());
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2019-02-19 06:57:53 +00:00
|
|
|
auto ms = crl::now(), nextAlert = 0LL;
|
2017-03-04 19:36:59 +00:00
|
|
|
bool alert = false;
|
|
|
|
int32 now = unixtime();
|
|
|
|
for (auto i = _whenAlerts.begin(); i != _whenAlerts.end();) {
|
|
|
|
while (!i.value().isEmpty() && i.value().begin().key() <= ms) {
|
2017-12-04 17:46:03 +00:00
|
|
|
const auto peer = i.key()->peer;
|
2018-04-09 17:48:29 +00:00
|
|
|
const auto peerUnknown = Auth().data().notifyMuteUnknown(peer);
|
|
|
|
const auto peerAlert = !peerUnknown
|
|
|
|
&& !Auth().data().notifyIsMuted(peer);
|
2017-12-04 17:46:03 +00:00
|
|
|
const auto from = i.value().begin().value();
|
2018-04-09 17:48:29 +00:00
|
|
|
const auto fromUnknown = (!from
|
|
|
|
|| Auth().data().notifyMuteUnknown(from));
|
|
|
|
const auto fromAlert = !fromUnknown
|
|
|
|
&& !Auth().data().notifyIsMuted(from);
|
2017-12-04 17:46:03 +00:00
|
|
|
if (peerAlert || fromAlert) {
|
2017-03-04 19:36:59 +00:00
|
|
|
alert = true;
|
|
|
|
}
|
2017-12-04 17:46:03 +00:00
|
|
|
while (!i.value().isEmpty()
|
|
|
|
&& i.value().begin().key() <= ms + kMinimalAlertDelay) {
|
|
|
|
i.value().erase(i.value().begin());
|
|
|
|
}
|
2017-03-04 19:36:59 +00:00
|
|
|
}
|
|
|
|
if (i.value().isEmpty()) {
|
|
|
|
i = _whenAlerts.erase(i);
|
|
|
|
} else {
|
|
|
|
if (!nextAlert || nextAlert > i.value().begin().key()) {
|
|
|
|
nextAlert = i.value().begin().key();
|
|
|
|
}
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (alert) {
|
|
|
|
Platform::Notifications::FlashBounce();
|
2017-05-03 13:01:15 +00:00
|
|
|
if (Global::SoundNotify() && !Platform::Notifications::SkipAudio()) {
|
|
|
|
ensureSoundCreated();
|
|
|
|
_soundTrack->playOnce();
|
|
|
|
emit Media::Player::mixer()->suppressAll(_soundTrack->getLengthMs());
|
|
|
|
emit Media::Player::mixer()->faderOnTimer();
|
|
|
|
}
|
2017-03-04 19:36:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (_waiters.isEmpty() || !Global::DesktopNotify() || Platform::Notifications::SkipToast()) {
|
|
|
|
if (nextAlert) {
|
2019-01-18 11:26:43 +00:00
|
|
|
_waitTimer.callOnce(nextAlert - ms);
|
2017-03-04 19:36:59 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
auto next = 0LL;
|
|
|
|
HistoryItem *notifyItem = nullptr;
|
|
|
|
History *notifyHistory = nullptr;
|
|
|
|
for (auto i = _waiters.begin(); i != _waiters.end();) {
|
|
|
|
History *history = i.key();
|
|
|
|
if (history->currentNotification() && history->currentNotification()->id != i.value().msg) {
|
|
|
|
auto j = _whenMaps.find(history);
|
|
|
|
if (j == _whenMaps.end()) {
|
|
|
|
history->clearNotifications();
|
|
|
|
i = _waiters.erase(i);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
do {
|
|
|
|
auto k = j.value().constFind(history->currentNotification()->id);
|
|
|
|
if (k != j.value().cend()) {
|
|
|
|
i.value().msg = k.key();
|
|
|
|
i.value().when = k.value();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
history->skipNotification();
|
|
|
|
} while (history->currentNotification());
|
|
|
|
}
|
|
|
|
if (!history->currentNotification()) {
|
|
|
|
_whenMaps.remove(history);
|
|
|
|
i = _waiters.erase(i);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
auto when = i.value().when;
|
|
|
|
if (!notifyItem || next > when) {
|
|
|
|
next = when;
|
|
|
|
notifyItem = history->currentNotification();
|
|
|
|
notifyHistory = history;
|
|
|
|
}
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
if (notifyItem) {
|
|
|
|
if (next > ms) {
|
|
|
|
if (nextAlert && nextAlert < next) {
|
|
|
|
next = nextAlert;
|
|
|
|
nextAlert = 0;
|
|
|
|
}
|
2019-01-18 11:26:43 +00:00
|
|
|
_waitTimer.callOnce(next - ms);
|
2017-03-04 19:36:59 +00:00
|
|
|
break;
|
|
|
|
} else {
|
2019-03-03 09:32:01 +00:00
|
|
|
const auto isForwarded = notifyItem->Has<HistoryMessageForwarded>();
|
|
|
|
const auto isAlbum = notifyItem->groupId();
|
2017-03-04 19:36:59 +00:00
|
|
|
|
2019-03-03 09:32:01 +00:00
|
|
|
auto groupedItem = (isForwarded || isAlbum) ? notifyItem : nullptr; // forwarded and album notify grouping
|
|
|
|
auto forwardedCount = isForwarded ? 1 : 0;
|
|
|
|
|
|
|
|
const auto history = notifyItem->history();
|
|
|
|
const auto j = _whenMaps.find(history);
|
2017-03-04 19:36:59 +00:00
|
|
|
if (j == _whenMaps.cend()) {
|
|
|
|
history->clearNotifications();
|
|
|
|
} else {
|
2017-04-30 12:44:17 +00:00
|
|
|
auto nextNotify = (HistoryItem*)nullptr;
|
2017-03-04 19:36:59 +00:00
|
|
|
do {
|
|
|
|
history->skipNotification();
|
|
|
|
if (!history->hasNotification()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-03-03 09:32:01 +00:00
|
|
|
j.value().remove((groupedItem ? groupedItem : notifyItem)->id);
|
2017-03-04 19:36:59 +00:00
|
|
|
do {
|
2019-03-03 09:32:01 +00:00
|
|
|
const auto k = j.value().constFind(history->currentNotification()->id);
|
2017-03-04 19:36:59 +00:00
|
|
|
if (k != j.value().cend()) {
|
|
|
|
nextNotify = history->currentNotification();
|
|
|
|
_waiters.insert(notifyHistory, Waiter(k.key(), k.value(), 0));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
history->skipNotification();
|
|
|
|
} while (history->hasNotification());
|
|
|
|
if (nextNotify) {
|
2019-03-03 09:32:01 +00:00
|
|
|
if (groupedItem) {
|
|
|
|
const auto canNextBeGrouped = (isForwarded && nextNotify->Has<HistoryMessageForwarded>())
|
|
|
|
|| (isAlbum && nextNotify->groupId());
|
|
|
|
const auto nextItem = canNextBeGrouped ? nextNotify : nullptr;
|
|
|
|
if (nextItem
|
|
|
|
&& qAbs(int64(nextItem->date()) - int64(groupedItem->date())) < 2) {
|
|
|
|
if (isForwarded
|
|
|
|
&& groupedItem->author() == nextItem->author()) {
|
|
|
|
++forwardedCount;
|
|
|
|
groupedItem = nextItem;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (isAlbum
|
|
|
|
&& groupedItem->groupId() == nextItem->groupId()) {
|
|
|
|
groupedItem = nextItem;
|
|
|
|
continue;
|
|
|
|
}
|
2017-03-04 19:36:59 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-03 09:32:01 +00:00
|
|
|
nextNotify = nullptr;
|
2017-03-04 19:36:59 +00:00
|
|
|
}
|
|
|
|
} while (nextNotify);
|
|
|
|
}
|
|
|
|
|
2019-03-10 07:42:25 +00:00
|
|
|
if (!_lastHistoryItemId && groupedItem) {
|
|
|
|
_lastHistoryItemId = groupedItem->fullId();
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the current notification is grouped.
|
|
|
|
if (isAlbum || isForwarded) {
|
|
|
|
// If the previous notification is grouped
|
|
|
|
// then reset the timer.
|
|
|
|
if (_waitForAllGroupedTimer.isActive()) {
|
|
|
|
_waitForAllGroupedTimer.cancel();
|
|
|
|
// If this is not the same group
|
|
|
|
// then show the previous group immediately.
|
|
|
|
if (!isSameGroup(groupedItem)) {
|
|
|
|
showGrouped();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// We have to wait until all the messages in this group are loaded.
|
|
|
|
_lastForwardedCount += forwardedCount;
|
|
|
|
_lastHistoryItemId = groupedItem->fullId();
|
|
|
|
_waitForAllGroupedTimer.callOnce(kWaitingForAllGroupedDelay);
|
|
|
|
} else {
|
|
|
|
// If the current notification is not grouped
|
|
|
|
// then there is no reason to wait for the timer
|
|
|
|
// to show the previous notification.
|
|
|
|
showGrouped();
|
|
|
|
_manager->showNotification(notifyItem, forwardedCount);
|
|
|
|
}
|
2017-03-04 19:36:59 +00:00
|
|
|
|
|
|
|
if (!history->hasNotification()) {
|
|
|
|
_waiters.remove(history);
|
|
|
|
_whenMaps.remove(history);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (nextAlert) {
|
2019-01-18 11:26:43 +00:00
|
|
|
_waitTimer.callOnce(nextAlert - ms);
|
2016-10-02 09:30:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-03 13:01:15 +00:00
|
|
|
void System::ensureSoundCreated() {
|
|
|
|
if (_soundTrack) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_soundTrack = Media::Audio::Current().createTrack();
|
2018-01-04 10:22:53 +00:00
|
|
|
_soundTrack->fillFromFile(
|
|
|
|
Auth().settings().getSoundPath(qsl("msg_incoming")));
|
2017-05-03 13:01:15 +00:00
|
|
|
}
|
|
|
|
|
2017-03-04 19:36:59 +00:00
|
|
|
void System::updateAll() {
|
|
|
|
_manager->updateAll();
|
2016-10-02 09:30:28 +00:00
|
|
|
}
|
|
|
|
|
2016-10-08 08:38:53 +00:00
|
|
|
Manager::DisplayOptions Manager::getNotificationOptions(HistoryItem *item) {
|
2019-01-21 13:42:21 +00:00
|
|
|
const auto hideEverything = Core::App().locked()
|
2018-06-03 13:30:40 +00:00
|
|
|
|| Global::ScreenIsLocked();
|
2016-10-08 08:38:53 +00:00
|
|
|
|
|
|
|
DisplayOptions result;
|
|
|
|
result.hideNameAndPhoto = hideEverything || (Global::NotifyView() > dbinvShowName);
|
|
|
|
result.hideMessageText = hideEverything || (Global::NotifyView() > dbinvShowPreview);
|
|
|
|
result.hideReplyButton = result.hideMessageText || !item || !item->history()->peer->canWrite();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-10-02 16:32:46 +00:00
|
|
|
void Manager::notificationActivated(PeerId peerId, MsgId msgId) {
|
|
|
|
onBeforeNotificationActivated(peerId, msgId);
|
|
|
|
if (auto window = App::wnd()) {
|
2019-01-18 12:27:37 +00:00
|
|
|
auto history = Auth().data().history(peerId);
|
2016-10-02 16:32:46 +00:00
|
|
|
window->showFromTray();
|
2017-01-01 17:49:44 +00:00
|
|
|
window->reActivateWindow();
|
2019-01-21 13:42:21 +00:00
|
|
|
if (Core::App().locked()) {
|
2016-10-02 16:32:46 +00:00
|
|
|
window->setInnerFocus();
|
2017-03-04 19:36:59 +00:00
|
|
|
system()->clearAll();
|
2016-10-02 16:32:46 +00:00
|
|
|
} else {
|
2018-02-12 11:10:40 +00:00
|
|
|
openNotificationMessage(history, msgId);
|
2016-10-02 16:32:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
onAfterNotificationActivated(peerId, msgId);
|
|
|
|
}
|
|
|
|
|
2018-02-12 11:10:40 +00:00
|
|
|
void Manager::openNotificationMessage(
|
|
|
|
not_null<History*> history,
|
|
|
|
MsgId messageId) {
|
|
|
|
const auto openExactlyMessage = [&] {
|
|
|
|
if (history->peer->isUser()
|
|
|
|
|| history->peer->isChannel()
|
|
|
|
|| !IsServerMsgId(messageId)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const auto item = App::histItemById(history->channelId(), messageId);
|
|
|
|
if (!item || !item->mentionsMe()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}();
|
|
|
|
const auto messageFeed = [&] {
|
|
|
|
if (const auto channel = history->peer->asChannel()) {
|
|
|
|
return channel->feed();
|
|
|
|
}
|
|
|
|
return (Data::Feed*)nullptr;
|
|
|
|
}();
|
|
|
|
if (openExactlyMessage) {
|
|
|
|
Ui::showPeerHistory(history, messageId);
|
|
|
|
} else if (messageFeed) {
|
|
|
|
App::wnd()->controller()->showSection(
|
|
|
|
HistoryFeed::Memento(messageFeed));
|
|
|
|
} else {
|
|
|
|
Ui::showPeerHistory(history, ShowAtUnreadMsgId);
|
|
|
|
}
|
|
|
|
system()->clearFromHistory(history);
|
|
|
|
}
|
|
|
|
|
2017-12-07 13:02:24 +00:00
|
|
|
void Manager::notificationReplied(
|
|
|
|
PeerId peerId,
|
|
|
|
MsgId msgId,
|
2018-05-24 13:03:21 +00:00
|
|
|
const TextWithTags &reply) {
|
2016-10-02 17:06:34 +00:00
|
|
|
if (!peerId) return;
|
|
|
|
|
2019-01-18 12:27:37 +00:00
|
|
|
const auto history = Auth().data().history(peerId);
|
2016-10-02 17:06:34 +00:00
|
|
|
|
2018-06-26 15:34:38 +00:00
|
|
|
auto message = ApiWrap::MessageToSend(history);
|
2018-05-24 13:03:21 +00:00
|
|
|
message.textWithTags = reply;
|
2016-10-02 17:06:34 +00:00
|
|
|
message.replyTo = (msgId > 0 && !history->peer->isUser()) ? msgId : 0;
|
|
|
|
message.clearDraft = false;
|
2018-06-26 15:34:38 +00:00
|
|
|
Auth().api().sendMessage(std::move(message));
|
2016-10-02 17:06:34 +00:00
|
|
|
}
|
|
|
|
|
2016-10-02 13:54:27 +00:00
|
|
|
void NativeManager::doShowNotification(HistoryItem *item, int forwardedCount) {
|
2018-01-14 16:02:25 +00:00
|
|
|
const auto options = getNotificationOptions(item);
|
|
|
|
|
|
|
|
const auto title = options.hideNameAndPhoto ? qsl("Telegram Desktop") : item->history()->peer->name;
|
|
|
|
const auto subtitle = options.hideNameAndPhoto ? QString() : item->notificationHeader();
|
|
|
|
const auto text = options.hideMessageText
|
|
|
|
? lang(lng_notification_preview)
|
|
|
|
: (forwardedCount < 2
|
2019-03-03 09:32:01 +00:00
|
|
|
? (item->groupId() ? lang(lng_in_dlg_album) : item->notificationText())
|
2018-01-14 16:02:25 +00:00
|
|
|
: lng_forward_messages(lt_count, forwardedCount));
|
|
|
|
|
|
|
|
doShowNativeNotification(
|
|
|
|
item->history()->peer,
|
|
|
|
item->id,
|
|
|
|
title,
|
|
|
|
subtitle,
|
|
|
|
text,
|
|
|
|
options.hideNameAndPhoto,
|
|
|
|
options.hideReplyButton);
|
2016-10-02 09:30:28 +00:00
|
|
|
}
|
|
|
|
|
2017-05-03 13:01:15 +00:00
|
|
|
System::~System() = default;
|
|
|
|
|
2016-10-02 09:30:28 +00:00
|
|
|
} // namespace Notifications
|
|
|
|
} // namespace Window
|