mirror of https://github.com/Nheko-Reborn/nheko
parent
4428388b3f
commit
9a9dbda571
@ -0,0 +1,73 @@ |
||||
#include "PresenceEmitter.h" |
||||
|
||||
#include <QCache> |
||||
#include <Utils.h> |
||||
|
||||
#include "Cache.h" |
||||
|
||||
namespace { |
||||
struct CacheEntry |
||||
{ |
||||
QString status; |
||||
mtx::presence::PresenceState state; |
||||
}; |
||||
} |
||||
|
||||
static QCache<QString, CacheEntry> presences; |
||||
|
||||
static QString |
||||
presenceToStr(mtx::presence::PresenceState state) |
||||
{ |
||||
switch (state) { |
||||
case mtx::presence::PresenceState::offline: |
||||
return QStringLiteral("offline"); |
||||
case mtx::presence::PresenceState::unavailable: |
||||
return QStringLiteral("unavailable"); |
||||
case mtx::presence::PresenceState::online: |
||||
default: |
||||
return QStringLiteral("online"); |
||||
} |
||||
} |
||||
|
||||
static CacheEntry * |
||||
pullPresence(const QString &id) |
||||
{ |
||||
auto p = cache::presence(id.toStdString()); |
||||
auto c = new CacheEntry{ |
||||
utils::replaceEmoji(QString::fromStdString(p.status_msg).toHtmlEscaped()), p.presence}; |
||||
presences.insert(id, c); |
||||
return c; |
||||
} |
||||
|
||||
void |
||||
PresenceEmitter::sync( |
||||
const std::vector<mtx::events::Event<mtx::events::presence::Presence>> &presences_) |
||||
{ |
||||
for (const auto &p : presences_) { |
||||
auto id = QString::fromStdString(p.sender); |
||||
presences.remove(id); |
||||
emit presenceChanged(std::move(id)); |
||||
} |
||||
} |
||||
|
||||
QString |
||||
PresenceEmitter::userPresence(QString id) const |
||||
{ |
||||
if (id.isEmpty()) |
||||
return {}; |
||||
else if (auto p = presences[id]) |
||||
return presenceToStr(p->state); |
||||
else |
||||
return presenceToStr(pullPresence(id)->state); |
||||
} |
||||
|
||||
QString |
||||
PresenceEmitter::userStatus(QString id) const |
||||
{ |
||||
if (id.isEmpty()) |
||||
return {}; |
||||
else if (auto p = presences[id]) |
||||
return p->status; |
||||
else |
||||
return pullPresence(id)->status; |
||||
} |
@ -0,0 +1,26 @@ |
||||
#pragma once |
||||
|
||||
#include <QObject> |
||||
|
||||
#include <vector> |
||||
|
||||
#include <mtx/events.hpp> |
||||
#include <mtx/events/presence.hpp> |
||||
|
||||
class PresenceEmitter : public QObject |
||||
{ |
||||
Q_OBJECT |
||||
|
||||
public: |
||||
PresenceEmitter(QObject *p = nullptr) |
||||
: QObject(p) |
||||
{} |
||||
|
||||
void sync(const std::vector<mtx::events::Event<mtx::events::presence::Presence>> &presences); |
||||
|
||||
Q_INVOKABLE QString userPresence(QString id) const; |
||||
Q_INVOKABLE QString userStatus(QString id) const; |
||||
|
||||
signals: |
||||
void presenceChanged(QString userid); |
||||
}; |
Loading…
Reference in new issue