aboutsummaryrefslogtreecommitdiff
path: root/events/event.cpp
blob: bd7e1b03833432f71e36f146288491ee11caa0de (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
/******************************************************************************
 * 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
 */

#include "event.h"

#include "roommessageevent.h"
#include "roomnameevent.h"
#include "roomaliasesevent.h"
#include "roomcanonicalaliasevent.h"
#include "roommemberevent.h"
#include "roomtopicevent.h"
#include "typingevent.h"
#include "receiptevent.h"
#include "unknownevent.h"
#include "logging.h"

#include <QtCore/QJsonDocument>

using namespace QMatrixClient;

Event::Event(Type type, const QJsonObject& rep)
    : _type(type), _originalJson(rep)
{
    if (!rep.contains("content"))
    {
        qCWarning(EVENTS) << "Event without 'content' node";
        qCWarning(EVENTS) << formatJson << rep;
    }
}

QByteArray Event::originalJson() const
{
    return QJsonDocument(_originalJson).toJson();
}

QDateTime Event::toTimestamp(const QJsonValue& v)
{
    Q_ASSERT(v.isDouble());
    return QDateTime::fromMSecsSinceEpoch(
            static_cast<long long int>(v.toDouble()), Qt::UTC);
}

QStringList Event::toStringList(const QJsonValue& v)
{
    Q_ASSERT(v.isArray());

    QStringList l;
    for( const QJsonValue& e : v.toArray() )
        l.push_back(e.toString());
    return l;
}

const QJsonObject Event::contentJson() const
{
    return _originalJson["content"].toObject();
}

template <typename EventT>
EventT* make(const QJsonObject& o)
{
    return new EventT(o);
}

Event* Event::fromJson(const QJsonObject& obj)
{
    // Check more specific event types first
    if (auto e = RoomEvent::fromJson(obj))
        return e;

    return dispatch<Event*>(obj).to(obj["type"].toString(),
            "m.typing", make<TypingEvent>,
            "m.receipt", make<ReceiptEvent>,
            /* Insert new event types (except room events) BEFORE this line */
            nullptr
            );
}

RoomEvent::RoomEvent(Type type, const QJsonObject& rep)
    : Event(type, rep), _id(rep["event_id"].toString())
    , _serverTimestamp(toTimestamp(rep["origin_server_ts"]))
    , _roomId(rep["room_id"].toString())
    , _senderId(rep["sender"].toString())
{
    if (_id.isEmpty())
    {
        qCWarning(EVENTS) << "Can't find event_id in a room event";
        qCWarning(EVENTS) << formatJson << rep;
    }
    if (!rep.contains("origin_server_ts"))
    {
        qCWarning(EVENTS) << "Event: can't find server timestamp in a room event";
        qCWarning(EVENTS) << formatJson << rep;
    }
    if (_senderId.isEmpty())
    {
        qCWarning(EVENTS) << "user_id not found in a room event";
        qCWarning(EVENTS) << formatJson << rep;
    }
}

RoomEvent* RoomEvent::fromJson(const QJsonObject& obj)
{
    return dispatch<RoomEvent*>(obj).to(obj["type"].toString(),
            "m.room.message", make<RoomMessageEvent>,
            "m.room.name", make<RoomNameEvent>,
            "m.room.aliases", make<RoomAliasesEvent>,
            "m.room.canonical_alias", make<RoomCanonicalAliasEvent>,
            "m.room.member", make<RoomMemberEvent>,
            "m.room.topic", make<RoomTopicEvent>,
            /* Insert new ROOM event types BEFORE this line */
            nullptr
        );
}