forked from mirror/nheko
parent
d467568a65
commit
6f2bc908ba
@ -0,0 +1 @@ |
||||
#include "Reaction.h" |
@ -0,0 +1,24 @@ |
||||
#pragma once |
||||
|
||||
#include <QObject> |
||||
#include <QString> |
||||
|
||||
struct Reaction |
||||
{ |
||||
Q_GADGET |
||||
Q_PROPERTY(QString key READ key) |
||||
Q_PROPERTY(QString users READ users) |
||||
Q_PROPERTY(QString selfReactedEvent READ selfReactedEvent) |
||||
Q_PROPERTY(int count READ count) |
||||
|
||||
public: |
||||
QString key() const { return key_; } |
||||
QString users() const { return users_; } |
||||
QString selfReactedEvent() const { return selfReactedEvent_; } |
||||
int count() const { return count_; } |
||||
|
||||
QString key_; |
||||
QString users_; |
||||
QString selfReactedEvent_; |
||||
int count_; |
||||
}; |
@ -1,98 +0,0 @@ |
||||
#include "ReactionsModel.h" |
||||
|
||||
#include <Cache.h> |
||||
#include <MatrixClient.h> |
||||
|
||||
QHash<int, QByteArray> |
||||
ReactionsModel::roleNames() const |
||||
{ |
||||
return { |
||||
{Key, "key"}, |
||||
{Count, "counter"}, |
||||
{Users, "users"}, |
||||
{SelfReactedEvent, "selfReactedEvent"}, |
||||
}; |
||||
} |
||||
|
||||
int |
||||
ReactionsModel::rowCount(const QModelIndex &) const |
||||
{ |
||||
return static_cast<int>(reactions.size()); |
||||
} |
||||
|
||||
QVariant |
||||
ReactionsModel::data(const QModelIndex &index, int role) const |
||||
{ |
||||
const int i = index.row(); |
||||
if (i < 0 || i >= static_cast<int>(reactions.size())) |
||||
return {}; |
||||
|
||||
switch (role) { |
||||
case Key: |
||||
return QString::fromStdString(reactions[i].key); |
||||
case Count: |
||||
return static_cast<int>(reactions[i].reactions.size()); |
||||
case Users: { |
||||
QString users; |
||||
bool first = true; |
||||
for (const auto &reaction : reactions[i].reactions) { |
||||
if (!first) |
||||
users += ", "; |
||||
else |
||||
first = false; |
||||
users += QString::fromStdString( |
||||
cache::displayName(room_id_, reaction.second.sender)); |
||||
} |
||||
return users; |
||||
} |
||||
case SelfReactedEvent: |
||||
for (const auto &reaction : reactions[i].reactions) |
||||
if (reaction.second.sender == http::client()->user_id().to_string()) |
||||
return QString::fromStdString(reaction.second.event_id); |
||||
return QStringLiteral(""); |
||||
default: |
||||
return {}; |
||||
} |
||||
} |
||||
|
||||
void |
||||
ReactionsModel::addReaction(const std::string &room_id, |
||||
const mtx::events::RoomEvent<mtx::events::msg::Reaction> &reaction) |
||||
{ |
||||
room_id_ = room_id; |
||||
|
||||
int idx = 0; |
||||
for (auto &storedReactions : reactions) { |
||||
if (storedReactions.key == reaction.content.relates_to.key) { |
||||
storedReactions.reactions[reaction.event_id] = reaction; |
||||
emit dataChanged(index(idx, 0), index(idx, 0)); |
||||
return; |
||||
} |
||||
idx++; |
||||
} |
||||
|
||||
beginInsertRows(QModelIndex(), idx, idx); |
||||
reactions.push_back( |
||||
KeyReaction{reaction.content.relates_to.key, {{reaction.event_id, reaction}}}); |
||||
endInsertRows(); |
||||
} |
||||
|
||||
void |
||||
ReactionsModel::removeReaction(const mtx::events::RoomEvent<mtx::events::msg::Reaction> &reaction) |
||||
{ |
||||
int idx = 0; |
||||
for (auto &storedReactions : reactions) { |
||||
if (storedReactions.key == reaction.content.relates_to.key) { |
||||
storedReactions.reactions.erase(reaction.event_id); |
||||
|
||||
if (storedReactions.reactions.size() == 0) { |
||||
beginRemoveRows(QModelIndex(), idx, idx); |
||||
reactions.erase(reactions.begin() + idx); |
||||
endRemoveRows(); |
||||
} else |
||||
emit dataChanged(index(idx, 0), index(idx, 0)); |
||||
return; |
||||
} |
||||
idx++; |
||||
} |
||||
} |
@ -1,41 +0,0 @@ |
||||
#pragma once |
||||
|
||||
#include <QAbstractListModel> |
||||
#include <QHash> |
||||
|
||||
#include <utility> |
||||
#include <vector> |
||||
|
||||
#include <mtx/events/collections.hpp> |
||||
|
||||
class ReactionsModel : public QAbstractListModel |
||||
{ |
||||
Q_OBJECT |
||||
public: |
||||
explicit ReactionsModel(QObject *parent = nullptr) { Q_UNUSED(parent); } |
||||
enum Roles |
||||
{ |
||||
Key, |
||||
Count, |
||||
Users, |
||||
SelfReactedEvent, |
||||
}; |
||||
|
||||
QHash<int, QByteArray> roleNames() const override; |
||||
int rowCount(const QModelIndex &parent = QModelIndex()) const override; |
||||
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; |
||||
|
||||
public slots: |
||||
void addReaction(const std::string &room_id, |
||||
const mtx::events::RoomEvent<mtx::events::msg::Reaction> &reaction); |
||||
void removeReaction(const mtx::events::RoomEvent<mtx::events::msg::Reaction> &reaction); |
||||
|
||||
private: |
||||
struct KeyReaction |
||||
{ |
||||
std::string key; |
||||
std::map<std::string, mtx::events::RoomEvent<mtx::events::msg::Reaction>> reactions; |
||||
}; |
||||
std::string room_id_; |
||||
std::vector<KeyReaction> reactions; |
||||
}; |
Loading…
Reference in new issue