aboutsummaryrefslogtreecommitdiff
path: root/events/event.h
blob: 7db1410071d8846cd5d97292ce9e687f5edee200 (plain)
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/******************************************************************************
 * Copyright (C) 2015 Felix Rohrbach <kde@fxrh.de>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#pragma once

#include <QtCore/QString>
#include <QtCore/QDateTime>
#include <QtCore/QJsonObject>
#include <QtCore/QJsonArray>

#include "util.h"

namespace QMatrixClient
{
    class Event
    {
            Q_GADGET
        public:
            enum class Type
            {
                RoomMessage, RoomName, RoomAliases, RoomCanonicalAlias,
                RoomMember, RoomTopic, Typing, Receipt, Unknown
            };

            explicit Event(Type type) : _type(type) { }
            Event(Type type, const QJsonObject& rep);
            Event(const Event&) = delete;

            Type type() const { return _type; }
            QByteArray originalJson() const;
            QJsonObject originalJsonObject() const;

            // According to the CS API spec, every event also has
            // a "content" object; but since its structure is different for
            // different types, we're implementing it per-event type
            // (and in most cases it will be a combination of other fields
            // instead of "content" field).

            static Event* fromJson(const QJsonObject& obj);

        protected:
            static QDateTime toTimestamp(const QJsonValue& v);
            static QStringList toStringList(const QJsonValue& v);

            const QJsonObject contentJson() const;

        private:
            Type _type;
            QJsonObject _originalJson;

            REGISTER_ENUM(Type)
            Q_PROPERTY(Type type READ type CONSTANT)
            Q_PROPERTY(QJsonObject contentJson READ contentJson CONSTANT)
    };
    using EventType = Event::Type;
    template <typename EventT>
    using EventsBatch = std::vector<EventT*>;
    using Events = EventsBatch<Event>;

    template <typename BaseEventT>
    BaseEventT* makeEvent(const QJsonObject& obj)
    {
        if (auto e = BaseEventT::fromJson(obj))
            return e;

        return new BaseEventT(EventType::Unknown, obj);
    }

    template <typename BaseEventT = Event,
              typename BatchT = EventsBatch<BaseEventT> >
    BatchT makeEvents(const QJsonArray& objs)
    {
        BatchT evs;
        // The below line accommodates the difference in size types of
        // STL and Qt containers.
        evs.reserve(static_cast<typename BatchT::size_type>(objs.size()));
        for (auto obj: objs)
            evs.push_back(makeEvent<BaseEventT>(obj.toObject()));
        return evs;
    }

    class RoomEvent : public Event
    {
            Q_GADGET
            Q_PROPERTY(QString id READ id)
            Q_PROPERTY(QDateTime timestamp READ timestamp CONSTANT)
            Q_PROPERTY(QString roomId READ roomId CONSTANT)
            Q_PROPERTY(QString senderId READ senderId CONSTANT)
            Q_PROPERTY(QString transactionId READ transactionId CONSTANT)
        public:
            explicit RoomEvent(Type type) : Event(type) { }
            RoomEvent(Type type, const QJsonObject& rep);

            const QString& id() const { return _id; }
            const QDateTime& timestamp() const { return _serverTimestamp; }
            const QString& roomId() const { return _roomId; }
            const QString& senderId() const { return _senderId; }
            const QString& transactionId() const { return _txnId; }

            /**
             * Sets the transaction id for locally created events. This should be
             * done before the event is exposed to any code using the respective
             * Q_PROPERTY.
             *
             * \param txnId - transaction id, normally obtained from
             * Connection::generateTxnId()
             */
            void setTransactionId(const QString& txnId) { _txnId = txnId; }

            /**
             * Sets event id for locally created events
             *
             * When a new event is created locally, it has no server id yet.
             * This function allows to add the id once the confirmation from
             * the server is received. There should be no id set previously
             * in the event. It's the responsibility of the code calling addId()
             * to notify clients that use Q_PROPERTY(id) about its change
             */
            void addId(const QString& id);

            // "Static override" of the one in Event
            static RoomEvent* fromJson(const QJsonObject& obj);

        private:
            QString _id;
            QDateTime _serverTimestamp;
            QString _roomId;
            QString _senderId;
            QString _txnId;
    };
    using RoomEvents = EventsBatch<RoomEvent>;
}  // namespace QMatrixClient
Q_DECLARE_OPAQUE_POINTER(QMatrixClient::Event*)
Q_DECLARE_METATYPE(QMatrixClient::Event*)
Q_DECLARE_OPAQUE_POINTER(QMatrixClient::RoomEvent*)
Q_DECLARE_METATYPE(QMatrixClient::RoomEvent*)