aboutsummaryrefslogtreecommitdiff
path: root/lib/events/event.cpp
blob: 3f50734727891c9beee1fa205af1ceb9006a5de1 (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
/******************************************************************************
 * 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 "simplestateevents.h"
#include "roommemberevent.h"
#include "roomavatarevent.h"
#include "typingevent.h"
#include "receiptevent.h"
#include "accountdataevents.h"
#include "directchatevent.h"
#include "redactionevent.h"
#include "logging.h"

#include <QtCore/QJsonDocument>

using namespace QMatrixClient;

event_type_t EventTypeRegistry::nextTypeId()
{
    static event_type_t typeIndex = unknownTypeId();
    return ++typeIndex;
}

Event::Event(Type type, const QJsonObject& json)
    : _type(type), _json(json)
{
    if (!json.contains(ContentKeyL) &&
            !json.value(UnsignedKeyL).toObject().contains(RedactedCauseKeyL))
    {
        qCWarning(EVENTS) << "Event without 'content' node";
        qCWarning(EVENTS) << formatJson << json;
    }
}

Event::Event(Type type, event_mtype_t matrixType, const QJsonObject& contentJson)
    : Event(type, basicEventJson(matrixType, contentJson))
{ }

Event::~Event() = default;

QString Event::matrixType() const
{
    return fullJson()[TypeKeyL].toString();
}

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

const QJsonObject Event::contentJson() const
{
    return fullJson()[ContentKeyL].toObject();
}

const QJsonObject Event::unsignedJson() const
{
    return fullJson()[UnsignedKeyL].toObject();
}

[[gnu::unused]] static auto roomEventTypeInitialised =
        EventTypeRegistry::chainFactories<Event, RoomEvent>();

RoomEvent::RoomEvent(Type type, event_mtype_t matrixType,
                     const QJsonObject& contentJson)
    : Event(type, matrixType, contentJson)
{ }

RoomEvent::RoomEvent(Type type, const QJsonObject& json)
    : Event(type, json)
{
    const auto unsignedData = json[UnsignedKeyL].toObject();
    const auto redaction = unsignedData[RedactedCauseKeyL];
    if (redaction.isObject())
    {
        _redactedBecause = makeEvent<RedactionEvent>(redaction.toObject());
        return;
    }

    _txnId = unsignedData.value("transactionId"_ls).toString();
    if (!_txnId.isEmpty())
        qCDebug(EVENTS) << "Event transactionId:" << _txnId;
}

RoomEvent::~RoomEvent() = default; // Let the smart pointer do its job

QString RoomEvent::id() const
{
    return fullJson()[EventIdKeyL].toString();
}

QDateTime RoomEvent::timestamp() const
{
    return QMatrixClient::fromJson<QDateTime>(fullJson()["origin_server_ts"_ls]);
}

QString RoomEvent::roomId() const
{
    return fullJson()["room_id"_ls].toString();
}

QString RoomEvent::senderId() const
{
    return fullJson()["sender"_ls].toString();
}

QString RoomEvent::redactionReason() const
{
    return isRedacted() ? _redactedBecause->reason() : QString{};
}

void RoomEvent::addId(const QString& newId)
{
    Q_ASSERT(id().isEmpty()); Q_ASSERT(!newId.isEmpty());
    editJson().insert(EventIdKey, newId);
}

[[gnu::unused]] static auto stateEventTypeInitialised =
        EventTypeRegistry::chainFactories<RoomEvent, StateEventBase>();

bool StateEventBase::repeatsState() const
{
    const auto prevContentJson = unsignedJson().value(PrevContentKeyL);
    return fullJson().value(ContentKeyL) == prevContentJson;
}