1 /******************************************************************************
2  * Copyright (C) 2019 Kitsune Ral <kitsune-ral@users.sf.net>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
17  */
18 
19 #pragma once
20 
21 #include "roomevent.h"
22 
23 namespace Quotient {
24 
25 struct EventRelation {
26     using reltypeid_t = const char*;
ReplyEventRelation27     static constexpr reltypeid_t Reply() { return "m.in_reply_to"; }
AnnotationEventRelation28     static constexpr reltypeid_t Annotation() { return "m.annotation"; }
ReplacementEventRelation29     static constexpr reltypeid_t Replacement() { return "m.replace"; }
30 
31     QString type;
32     QString eventId;
33     QString key = {}; // Only used for m.annotation for now
34 
replyToEventRelation35     static EventRelation replyTo(QString eventId)
36     {
37         return { Reply(), std::move(eventId) };
38     }
annotateEventRelation39     static EventRelation annotate(QString eventId, QString key)
40     {
41         return { Annotation(), std::move(eventId), std::move(key) };
42     }
replaceEventRelation43     static EventRelation replace(QString eventId)
44     {
45         return { Replacement(), std::move(eventId) };
46     }
47 };
48 template <>
49 struct JsonObjectConverter<EventRelation> {
50     static void dumpTo(QJsonObject& jo, const EventRelation& pod);
51     static void fillFrom(const QJsonObject& jo, EventRelation& pod);
52 };
53 
54 class ReactionEvent : public RoomEvent {
55 public:
56     DEFINE_EVENT_TYPEID("m.reaction", ReactionEvent)
57 
58     explicit ReactionEvent(const EventRelation& value)
59         : RoomEvent(typeId(), matrixTypeId(),
60                     { { QStringLiteral("m.relates_to"), toJson(value) } })
61     {}
62     explicit ReactionEvent(const QJsonObject& obj) : RoomEvent(typeId(), obj) {}
63     EventRelation relation() const
64     {
65         return content<EventRelation>(QStringLiteral("m.relates_to"));
66     }
67 
68 private:
69     EventRelation _relation;
70 };
71 REGISTER_EVENT_TYPE(ReactionEvent)
72 
73 } // namespace Quotient
74