summary refs log tree commit diff
path: root/include/RoomState.h
blob: 0e91410c5315f850d3fe45f14088b8300aea787a (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
 * nheko Copyright (C) 2017  Konstantinos Sideris <siderisk@auth.gr>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#pragma once

#include <QJsonDocument>
#include <QPixmap>
#include <QUrl>

#include <mtx.hpp>

class RoomState
{
public:
        RoomState();
        RoomState(const mtx::responses::Timeline &timeline);
        RoomState(const mtx::responses::State &state);

        // Calculate room data that are not immediatly accessible. Like room name and
        // avatar.
        //
        // e.g If the room is 1-on-1 name and avatar should be extracted from a user.
        void resolveName();
        void resolveAvatar();
        void parse(const nlohmann::json &object);

        QUrl getAvatar() const { return avatar_; };
        QString getName() const { return name_; };
        QString getTopic() const
        {
                return QString::fromStdString(topic.content.topic).simplified();
        };

        void removeLeaveMemberships();
        void update(const RoomState &state);

        template<class Collection>
        void updateFromEvents(const std::vector<Collection> &collection);

        std::string serialize() const;

        // The latest state events.
        mtx::events::StateEvent<mtx::events::state::Aliases> aliases;
        mtx::events::StateEvent<mtx::events::state::Avatar> avatar;
        mtx::events::StateEvent<mtx::events::state::CanonicalAlias> canonical_alias;
        mtx::events::StateEvent<mtx::events::state::Create> create;
        mtx::events::StateEvent<mtx::events::state::HistoryVisibility> history_visibility;
        mtx::events::StateEvent<mtx::events::state::JoinRules> join_rules;
        mtx::events::StateEvent<mtx::events::state::Name> name;
        mtx::events::StateEvent<mtx::events::state::PowerLevels> power_levels;
        mtx::events::StateEvent<mtx::events::state::Topic> topic;

        // Contains the m.room.member events for all the joined users.
        using UserID = std::string;
        std::map<UserID, mtx::events::StateEvent<mtx::events::state::Member>> memberships;

private:
        QUrl avatar_;
        QString name_;

        // It defines the user whose avatar is used for the room. If the room has an
        // avatar event this should be empty.
        QString userAvatar_;
};

template<class Collection>
void
RoomState::updateFromEvents(const std::vector<Collection> &collection)
{
        using Aliases           = mtx::events::StateEvent<mtx::events::state::Aliases>;
        using Avatar            = mtx::events::StateEvent<mtx::events::state::Avatar>;
        using CanonicalAlias    = mtx::events::StateEvent<mtx::events::state::CanonicalAlias>;
        using Create            = mtx::events::StateEvent<mtx::events::state::Create>;
        using HistoryVisibility = mtx::events::StateEvent<mtx::events::state::HistoryVisibility>;
        using JoinRules         = mtx::events::StateEvent<mtx::events::state::JoinRules>;
        using Member            = mtx::events::StateEvent<mtx::events::state::Member>;
        using Name              = mtx::events::StateEvent<mtx::events::state::Name>;
        using PowerLevels       = mtx::events::StateEvent<mtx::events::state::PowerLevels>;
        using Topic             = mtx::events::StateEvent<mtx::events::state::Topic>;

        for (const auto &event : collection) {
                if (mpark::holds_alternative<Aliases>(event)) {
                        this->aliases = mpark::get<Aliases>(event);
                } else if (mpark::holds_alternative<Avatar>(event)) {
                        this->avatar = mpark::get<Avatar>(event);
                } else if (mpark::holds_alternative<CanonicalAlias>(event)) {
                        this->canonical_alias = mpark::get<CanonicalAlias>(event);
                } else if (mpark::holds_alternative<Create>(event)) {
                        this->create = mpark::get<Create>(event);
                } else if (mpark::holds_alternative<HistoryVisibility>(event)) {
                        this->history_visibility = mpark::get<HistoryVisibility>(event);
                } else if (mpark::holds_alternative<JoinRules>(event)) {
                        this->join_rules = mpark::get<JoinRules>(event);
                } else if (mpark::holds_alternative<Name>(event)) {
                        this->name = mpark::get<Name>(event);
                } else if (mpark::holds_alternative<Member>(event)) {
                        auto membership = mpark::get<Member>(event);
                        this->memberships.emplace(membership.state_key, membership);
                } else if (mpark::holds_alternative<PowerLevels>(event)) {
                        this->power_levels = mpark::get<PowerLevels>(event);
                } else if (mpark::holds_alternative<Topic>(event)) {
                        this->topic = mpark::get<Topic>(event);
                }
        }
}