summary refs log tree commit diff
path: root/src/timeline/ReactionsModel.cpp
blob: 1200e2ba518c048c4213f695322809a5ec921da9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#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++;
        }
}