aboutsummaryrefslogtreecommitdiff
path: root/lib/events/roommessageevent.h
blob: 7bcda2bade72eea0a8d4dd4d161e7896ff3d3e6e (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// SPDX-FileCopyrightText: 2015 Felix Rohrbach <kde@fxrh.de>
// SPDX-FileCopyrightText: 2016 Kitsune Ral <Kitsune-Ral@users.sf.net>
// SPDX-FileCopyrightText: 2017 Roman Plášil <me@rplasil.name>
// SPDX-License-Identifier: LGPL-2.1-or-later

#pragma once

#include "eventcontent.h"
#include "roomevent.h"

class QFileInfo;

namespace Quotient {
namespace MessageEventContent = EventContent; // Back-compatibility

/**
 * The event class corresponding to m.room.message events
 */
class RoomMessageEvent : public RoomEvent {
    Q_GADGET
    Q_PROPERTY(QString msgType READ rawMsgtype CONSTANT)
    Q_PROPERTY(QString plainBody READ plainBody CONSTANT)
    Q_PROPERTY(QMimeType mimeType READ mimeType STORED false CONSTANT)
    Q_PROPERTY(const EventContent::TypedBase* content READ content CONSTANT)
public:
    DEFINE_EVENT_TYPEID("m.room.message", RoomMessageEvent)

    enum class MsgType {
        Text,
        Emote,
        Notice,
        Image,
        File,
        Location,
        Video,
        Audio,
        Unknown
    };

    RoomMessageEvent(const QString& plainBody, const QString& jsonMsgType,
                     EventContent::TypedBase* content = nullptr);
    explicit RoomMessageEvent(const QString& plainBody,
                              MsgType msgType = MsgType::Text,
                              EventContent::TypedBase* content = nullptr);
#if QT_VERSION_MAJOR < 6
    [[deprecated("Create an EventContent object on the client side"
                 " and pass it to other constructors")]] //
    explicit RoomMessageEvent(const QString& plainBody, const QFileInfo& file,
                              bool asGenericFile = false);
#endif
    explicit RoomMessageEvent(const QJsonObject& obj);

    MsgType msgtype() const;
    QString rawMsgtype() const;
    QString plainBody() const;
    const EventContent::TypedBase* content() const { return _content.data(); }
    template <typename VisitorT>
    void editContent(VisitorT&& visitor)
    {
        visitor(*_content);
        editJson()[ContentKeyL] = assembleContentJson(plainBody(), rawMsgtype(),
                                                      _content.data());
    }
    QMimeType mimeType() const;
    bool hasTextContent() const;
    bool hasFileContent() const;
    bool hasThumbnail() const;
    QString replacedEvent() const;

    static QString rawMsgTypeForUrl(const QUrl& url);
    static QString rawMsgTypeForFile(const QFileInfo& fi);

private:
    QScopedPointer<EventContent::TypedBase> _content;

    // FIXME: should it really be static?
    static QJsonObject assembleContentJson(const QString& plainBody,
                                           const QString& jsonMsgType,
                                           EventContent::TypedBase* content);

    Q_ENUM(MsgType)
};
REGISTER_EVENT_TYPE(RoomMessageEvent)
using MessageEventType = RoomMessageEvent::MsgType;

namespace EventContent {
    // Additional event content types

    struct RelatesTo {
        static constexpr const char* ReplyTypeId() { return "m.in_reply_to"; }
        static constexpr const char* ReplacementTypeId() { return "m.replace"; }
        QString type; // The only supported relation so far
        QString eventId;
    };
    inline RelatesTo replyTo(QString eventId)
    {
        return { RelatesTo::ReplyTypeId(), std::move(eventId) };
    }
    inline RelatesTo replacementOf(QString eventId)
    {
        return { RelatesTo::ReplacementTypeId(), std::move(eventId) };
    }

    /**
     * Rich text content for m.text, m.emote, m.notice
     *
     * Available fields: mimeType, body. The body can be either rich text
     * or plain text, depending on what mimeType specifies.
     */
    class TextContent : public TypedBase {
    public:
        TextContent(QString text, const QString& contentType,
                    Omittable<RelatesTo> relatesTo = none);
        explicit TextContent(const QJsonObject& json);

        QMimeType type() const override { return mimeType; }

        QMimeType mimeType;
        QString body;
        Omittable<RelatesTo> relatesTo;

    protected:
        void fillJson(QJsonObject* json) const override;
    };

    /**
     * Content class for m.location
     *
     * Available fields:
     * - corresponding to the top-level JSON:
     *   - geoUri ("geo_uri" in JSON)
     * - corresponding to the "info" subobject:
     *   - thumbnail.url ("thumbnail_url" in JSON)
     * - corresponding to the "info/thumbnail_info" subobject:
     *   - thumbnail.payloadSize
     *   - thumbnail.mimeType
     *   - thumbnail.imageSize
     */
    class LocationContent : public TypedBase {
    public:
        LocationContent(const QString& geoUri, const Thumbnail& thumbnail = {});
        explicit LocationContent(const QJsonObject& json);

        QMimeType type() const override;

    public:
        QString geoUri;
        Thumbnail thumbnail;

    protected:
        void fillJson(QJsonObject* o) const override;
    };

    /**
     * A base class for info types that include duration: audio and video
     */
    template <typename ContentT>
    class PlayableContent : public ContentT {
    public:
        using ContentT::ContentT;
        PlayableContent(const QJsonObject& json)
            : ContentT(json)
            , duration(ContentT::originalInfoJson["duration"_ls].toInt())
        {}

    protected:
        void fillJson(QJsonObject* json) const override
        {
            ContentT::fillJson(json);
            auto infoJson = json->take("info"_ls).toObject();
            infoJson.insert(QStringLiteral("duration"), duration);
            json->insert(QStringLiteral("info"), infoJson);
        }

    public:
        int duration;
    };

    /**
     * Content class for m.video
     *
     * Available fields:
     * - corresponding to the top-level JSON:
     *   - url
     *   - filename (extension to the CS API spec)
     * - corresponding to the "info" subobject:
     *   - payloadSize ("size" in JSON)
     *   - mimeType ("mimetype" in JSON)
     *   - duration
     *   - imageSize (QSize for a combination of "h" and "w" in JSON)
     *   - thumbnail.url ("thumbnail_url" in JSON)
     * - corresponding to the "info/thumbnail_info" subobject: contents of
     *   thumbnail field, in the same vein as for "info":
     *   - payloadSize
     *   - mimeType
     *   - imageSize
     */
    using VideoContent = PlayableContent<UrlWithThumbnailContent<ImageInfo>>;

    /**
     * Content class for m.audio
     *
     * Available fields:
     * - corresponding to the top-level JSON:
     *   - url
     *   - filename (extension to the CS API spec)
     * - corresponding to the "info" subobject:
     *   - payloadSize ("size" in JSON)
     *   - mimeType ("mimetype" in JSON)
     *   - duration
     */
    using AudioContent = PlayableContent<UrlBasedContent<FileInfo>>;
} // namespace EventContent
} // namespace Quotient