Debian Codegraph @

src/notifications/Manager.cpp

raw symbols SCIP
1 // SPDX-FileCopyrightText: Nheko Contributors
2 //
3 // SPDX-License-Identifier: GPL-3.0-or-later
4
5 #include "notifications/Manager.h"
6
7 #include "Cache.h"
8 #include "Cache_p.h"
9 #include "EventAccessors.h"
10 #include "UserSettingsPage.h"
11 #include "Utils.h"
12
13 bool
14 NotificationsManager::allowShowingImages(const mtx::responses::Notification &notification)
15 {
16 auto show = UserSettings::instance()->showImage();
17
18 switch (show) {
19 case UserSettings::ShowImage::Always:
20 return true;
21 case UserSettings::ShowImage::OnlyPrivate: {
22 auto accessRules = cache::client()
23 ->getStateEvent<mtx::events::state::JoinRules>(notification.room_id)
24 .value_or(mtx::events::StateEvent<mtx::events::state::JoinRules>{})
25 .content;
26
27 return accessRules.join_rule != mtx::events::state::JoinRule::Public;
28 }
29 case UserSettings::ShowImage::Never:
30 default:
31 return false;
32 }
33 }
34
35 QString
36 NotificationsManager::getMessageTemplate(const mtx::responses::Notification &notification)
37 {
38 const auto sender =
39 cache::displayName(QString::fromStdString(notification.room_id),
40 QString::fromStdString(mtx::accessors::sender(notification.event)));
41
42 // TODO: decrypt this message if the decryption setting is on in the UserSettings
43 if (auto msg = std::get_if<mtx::events::EncryptedEvent<mtx::events::msg::Encrypted>>(
44 &notification.event);
45 msg != nullptr) {
46 return tr("%1 sent an encrypted message").arg(sender);
47 }
48
49 bool containsSpoiler =
50 mtx::accessors::formatted_body(notification.event).find("<span data-mx-spoiler") !=
51 std::string::npos;
52
53 if (containsSpoiler) {
54 // Because we skip the %2 here, this might cause a warning in some cases.
55 if (mtx::accessors::msg_type(notification.event) == mtx::events::MessageType::Emote) {
56 return QStringLiteral("* %1 spoils something.").arg(sender);
57 } else if (utils::isReply(notification.event)) {
58 return tr("%1 replied with a spoiler.",
59 "Format a reply in a notification. %1 is the sender.")
60 .arg(sender);
61 } else {
62 return QStringLiteral("%1 sent a spoiler.").arg(sender);
63 }
64 } else {
65 if (mtx::accessors::msg_type(notification.event) == mtx::events::MessageType::Emote) {
66 return QStringLiteral("* %1 %2").arg(sender);
67 } else if (utils::isReply(notification.event)) {
68 return tr("%1 replied: %2",
69 "Format a reply in a notification. %1 is the sender, %2 the message")
70 .arg(sender);
71 } else {
72 return QStringLiteral("%1: %2").arg(sender);
73 }
74 }
75 }
76
77 void
78 NotificationsManager::removeNotifications(const QString &roomId_,
79 const std::vector<QString> &eventIds)
80 {
81 std::string room_id = roomId_.toStdString();
82
83 std::uint64_t markerPos = 0;
84 for (const auto &e : eventIds) {
85 markerPos = std::max(markerPos, cache::getEventIndex(room_id, e.toStdString()).value_or(0));
86 }
87
88 for (const auto &[roomId, eventId] : std::as_const(this->notificationIds)) {
89 if (roomId != roomId_)
90 continue;
91 auto idx = cache::getEventIndex(room_id, eventId.toStdString());
92 if (!idx || markerPos >= idx) {
93 removeNotification(roomId, eventId);
94 }
95 }
96 }
97
98 #include "moc_Manager.cpp"