1// SPDX-FileCopyrightText: 2016 Kitsune Ral <Kitsune-Ral@users.sf.net>
2// SPDX-License-Identifier: LGPL-2.1-or-later
3
4#include "event.h"
5
6#include "../logging_categories_p.h"
7#include "callevents.h"
8#include "stateevent.h"
9
10#include <QtCore/QJsonDocument>
11
12using namespace Quotient;
13
14QString EventTypeRegistry::getMatrixType(event_type_t typeId) { return typeId; }
15
16void AbstractEventMetaType::addDerived(const AbstractEventMetaType* newType)
17{
18 if (const auto existing =
19 std::find_if(first: derivedTypes.cbegin(), last: derivedTypes.cend(),
20 pred: [&newType](const AbstractEventMetaType* t) {
21 return t->matrixId == newType->matrixId;
22 });
23 existing != derivedTypes.cend())
24 {
25 if (*existing == newType)
26 return;
27 // Two different metatype objects claim the same Matrix type id; this
28 // is not normal, so give as much information as possible to diagnose
29 if ((*existing)->className == newType->className) {
30 qCritical(catFunc: EVENTS)
31 << newType->className << "claims" << newType->matrixId
32 << "repeatedly; check that it's exported across translation "
33 "units or shared objects";
34 Q_ASSERT(false); // That situation is plain wrong
35 return; // So maybe std::terminate() even?
36 }
37 qWarning(catFunc: EVENTS).nospace()
38 << newType->matrixId << " is already mapped to "
39 << (*existing)->className << " before " << newType->className
40 << "; unless the two have different isValid() conditions, the "
41 "latter class will never be used";
42 }
43 derivedTypes.emplace_back(args&: newType);
44 qDebug(catFunc: EVENTS).nospace()
45 << newType->matrixId << " -> " << newType->className << "; "
46 << derivedTypes.size() << " derived type(s) registered for "
47 << className;
48}
49
50Event::Event(const QJsonObject& json)
51 : _json(json)
52{
53 if (!json.contains(key: ContentKey)
54 && !json.value(key: UnsignedKey).toObject().contains(key: RedactedCauseKey)) {
55 qCWarning(EVENTS) << "Event without 'content' node";
56 qCWarning(EVENTS) << formatJson << json;
57 }
58}
59
60Event::~Event() = default;
61
62QString Event::matrixType() const { return fullJson()[TypeKey].toString(); }
63
64QByteArray Event::originalJson() const { return QJsonDocument(_json).toJson(); }
65
66const QJsonObject Event::contentJson() const
67{
68 return fullJson()[ContentKey].toObject();
69}
70
71const QJsonObject Event::unsignedJson() const
72{
73 return fullJson()[UnsignedKey].toObject();
74}
75
76bool Event::isStateEvent() const { return is<StateEvent>(); }
77
78bool Event::isCallEvent() const { return is<CallEvent>(); }
79
80void Event::dumpTo(QDebug dbg) const
81{
82 dbg << QJsonDocument(contentJson()).toJson(format: QJsonDocument::Compact);
83}
84