Debian Codegraph @

src/timeline/PresenceEmitter.cpp

raw symbols SCIP
1 // SPDX-FileCopyrightText: Nheko Contributors
2 //
3 // SPDX-License-Identifier: GPL-3.0-or-later
4
5 #include "PresenceEmitter.h"
6
7 #include <QCache>
8 #include <Utils.h>
9
10 #include "Cache.h"
11
12 namespace {
13 struct CacheEntry
14 {
15 QString status;
16 mtx::presence::PresenceState state;
17 };
18 }
19
20 static QCache<QString, CacheEntry> presences;
21
22 static QString
23 presenceToStr(mtx::presence::PresenceState state)
24 {
25 switch (state) {
26 case mtx::presence::PresenceState::offline:
27 return QStringLiteral("offline");
28 case mtx::presence::PresenceState::unavailable:
29 return QStringLiteral("unavailable");
30 case mtx::presence::PresenceState::online:
31 default:
32 return QStringLiteral("online");
33 }
34 }
35
36 static CacheEntry *
37 pullPresence(const QString &id)
38 {
39 auto p = cache::presence(id.toStdString());
40
41 auto statusMsg = QString::fromStdString(p.status_msg);
42 if (statusMsg.size() > 255) {
43 statusMsg.truncate(255);
44 statusMsg.append(u'…');
45 }
46
47 auto c = new CacheEntry{utils::replaceEmoji(std::move(statusMsg).toHtmlEscaped()), p.presence};
48 presences.insert(id, c);
49 return c;
50 }
51
52 void
53 PresenceEmitter::sync(
54 const std::vector<mtx::events::Event<mtx::events::presence::Presence>> &presences_)
55 {
56 for (const auto &p : presences_) {
57 auto id = QString::fromStdString(p.sender);
58 presences.remove(id);
59 emit presenceChanged(std::move(id));
60 }
61 }
62
63 QString
64 PresenceEmitter::userPresence(QString id) const
65 {
66 if (id.isEmpty())
67 return {};
68 else if (auto p = presences[id])
69 return presenceToStr(p->state);
70 else
71 return presenceToStr(pullPresence(id)->state);
72 }
73
74 QString
75 PresenceEmitter::userStatus(QString id) const
76 {
77 if (id.isEmpty())
78 return {};
79 else if (auto p = presences[id])
80 return p->status;
81 else
82 return pullPresence(id)->status;
83 }
84
85 #include "moc_PresenceEmitter.cpp"