aboutsummaryrefslogtreecommitdiff
path: root/plugins/Matrix.hpp
blob: 6a876d03fdaabc99cef16df6679d6101d2e772d6 (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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
#pragma once

#include "../include/FileAnalyzer.hpp"
#include "Plugin.hpp"
#include "Page.hpp"
#include <SFML/Graphics/Color.hpp>
#include <unordered_map>
#include <set>
#include <mutex>
#include <rapidjson/fwd.h>

namespace QuickMedia {
    struct RoomData;

    struct UserInfo {
        friend struct RoomData;

        std::string user_id;
        std::string display_name;
        std::string avatar_url;
        sf::Color display_name_color;
    private:
        std::string read_marker_event_id;
    };

    enum class MessageType {
        TEXT,
        IMAGE,
        VIDEO,
        AUDIO,
        FILE,
        REDACTION
    };

    enum class RelatedEventType {
        NONE,
        REPLY,
        EDIT,
        REDACTION
    };

    struct Message {
        std::shared_ptr<UserInfo> user;
        std::string event_id;
        std::string body;
        std::string url;
        std::string thumbnail_url;
        std::string related_event_id;
        sf::Vector2i thumbnail_size; // Set to {0, 0} if not specified
        RelatedEventType related_event_type;
        bool mentions_me = false;
        time_t timestamp = 0; // In milliseconds
        MessageType type;
    };

    struct RoomData {
        std::shared_ptr<UserInfo> get_user_by_id(const std::string &user_id);
        void add_user(std::shared_ptr<UserInfo> user);

        void set_user_read_marker(std::shared_ptr<UserInfo> &user, const std::string &event_id);
        std::string get_user_read_marker(std::shared_ptr<UserInfo> &user);

        // Ignores duplicates
        void prepend_messages_reverse(const std::vector<std::shared_ptr<Message>> &new_messages);
        // Ignores duplicates
        void append_messages(const std::vector<std::shared_ptr<Message>> &new_messages);

        std::shared_ptr<Message> get_message_by_id(const std::string &id);

        std::vector<std::shared_ptr<UserInfo>> get_users_excluding_me(const std::string &my_user_id);

        void acquire_room_lock();
        void release_room_lock();

        const std::vector<std::shared_ptr<Message>>& get_messages_thread_unsafe() const;
        const std::vector<std::string>& get_pinned_events_unsafe() const;

        bool has_prev_batch();
        void set_prev_batch(const std::string &new_prev_batch);
        std::string get_prev_batch();

        bool has_name();
        void set_name(const std::string &new_name);
        // TODO: Remove this
        std::string get_name();

        bool has_avatar_url();
        void set_avatar_url(const std::string &new_avatar_url);
        std::string get_avatar_url();

        void set_pinned_events(std::vector<std::string> new_pinned_events);
        std::set<std::string>& get_tags_unsafe();

        void clear_data();

        std::string id;
        bool initial_fetch_finished = false;

        // These 4 variables are set by QuickMedia, not the matrix plugin
        bool last_message_read = true;
        time_t last_read_message_timestamp = 0;
        bool has_unread_mention = false;
        void *userdata = nullptr; // Pointer to BodyItem. Note: this has to be valid as long as the room is valid

        // These are messages fetched with |Matrix::get_message_by_id|. Needed to show replies, when replying to old message not part of /sync.
        // The value is nullptr if the message is fetched and cached but the event if referenced an invalid message.
        // TODO: Verify if replied to messages are also part of /sync; then this is not needed.
        std::unordered_map<std::string, std::shared_ptr<Message>> fetched_messages_by_event_id;

        size_t messages_read_index = 0;
        bool pinned_events_updated = false;

        size_t index;
    private:
        std::mutex user_mutex;
        std::mutex room_mutex;

        std::string name;
        std::string avatar_url;
        std::string prev_batch;

        // Each room has its own list of user data, even if multiple rooms has the same user
        // because users can have different display names and avatars in different rooms.
        std::unordered_map<std::string, std::shared_ptr<UserInfo>> user_info_by_user_id;
        std::vector<std::shared_ptr<Message>> messages;
        std::unordered_map<std::string, std::shared_ptr<Message>> message_by_event_id;
        std::vector<std::string> pinned_events;
        std::set<std::string> tags;
    };

    struct Invite {
        std::string room_name;
        std::string room_avatar_url;
        std::shared_ptr<UserInfo> invited_by;
        time_t timestamp = 0; // In milliseconds
        bool new_invite = false;
    };

    enum class MessageDirection {
        BEFORE,
        AFTER
    };

    struct UploadInfo {
        ContentType content_type;
        size_t file_size;
        std::optional<Dimensions> dimensions;
        std::optional<double> duration_seconds;
        std::string content_uri;
    };

    using Messages = std::vector<std::shared_ptr<Message>>;

    struct SyncData {
        Messages messages;
        std::optional<std::vector<std::string>> pinned_events;
        std::optional<std::vector<std::string>> tags;
    };

    using Rooms = std::vector<RoomData*>;

    bool message_contains_user_mention(const std::string &msg, const std::string &username);

    enum class MatrixPageType {
        ROOM_LIST,
        CHAT
    };

    enum class LeaveType {
        LEAVE,
        KICKED,
        BANNED
    };

    class MatrixDelegate {
    public:
        virtual ~MatrixDelegate() = default;

        virtual void join_room(RoomData *room) = 0;
        virtual void leave_room(RoomData *room, LeaveType leave_type, const std::string &reason) = 0;

        // Note: calling |room| methods inside this function is not allowed
        virtual void room_add_tag(RoomData *room, const std::string &tag) = 0;
        // Note: calling |room| methods inside this function is not allowed
        virtual void room_remove_tag(RoomData *room, const std::string &tag) = 0;
        virtual void room_add_new_messages(RoomData *room, const Messages &messages, bool is_initial_sync) = 0;

        virtual void add_invite(const std::string &room_id, const Invite &invite) = 0;
        virtual void remove_invite(const std::string &room_id) = 0;

        virtual void update(MatrixPageType page_type) { (void)page_type; }
    };

    class Matrix;
    class MatrixRoomsPage;
    class MatrixRoomTagsPage;
    class MatrixInvitesPage;
    class MatrixChatPage;

    class MatrixQuickMedia : public MatrixDelegate {
    public:
        MatrixQuickMedia(Program *program, Matrix *matrix, MatrixRoomsPage *rooms_page, MatrixRoomTagsPage *room_tags_page, MatrixInvitesPage *invites_page);

        void join_room(RoomData *room) override;
        void leave_room(RoomData *room, LeaveType leave_type, const std::string &reason) override;
        void room_add_tag(RoomData *room, const std::string &tag) override;
        void room_remove_tag(RoomData *room, const std::string &tag) override;
        void room_add_new_messages(RoomData *room, const Messages &messages, bool is_initial_sync) override;

        void add_invite(const std::string &room_id, const Invite &invite) override;
        void remove_invite(const std::string &room_id) override;

        void update(MatrixPageType page_type) override;

        Program *program;
        Matrix *matrix;
        MatrixRoomsPage *rooms_page;
        MatrixRoomTagsPage *room_tags_page;
        MatrixInvitesPage *invites_page;
    private:
        void update_pending_room_messages(MatrixPageType page_type);
    private:
        struct RoomMessagesData {
            Messages messages;
            bool is_initial_sync;
        };

        std::map<RoomData*, std::shared_ptr<BodyItem>> room_body_item_by_room;
        std::map<RoomData*, RoomMessagesData> pending_room_messages;
        std::mutex pending_room_messages_mutex;
    };

    class MatrixRoomsPage : public Page {
    public:
        MatrixRoomsPage(Program *program, Body *body, std::string title, MatrixRoomTagsPage *room_tags_page = nullptr);
        ~MatrixRoomsPage() override;

        const char* get_title() const override { return title.c_str(); }
        PluginResult submit(const std::string &title, const std::string &url, std::vector<Tab> &result_tabs) override;

        void update() override;
        void add_body_item(std::shared_ptr<BodyItem> body_item);

        void move_room_to_top(RoomData *room);
        void remove_body_item_by_room_id(const std::string &room_id);

        void set_current_chat_page(MatrixChatPage *chat_page);

        MatrixQuickMedia *matrix_delegate = nullptr;
    private:
        std::mutex mutex;
        std::vector<std::shared_ptr<BodyItem>> room_body_items;
        std::vector<std::string> pending_remove_body_items;
        Body *body;
        std::string title;
        MatrixRoomTagsPage *room_tags_page;
        MatrixChatPage *current_chat_page;
    };

    class MatrixRoomTagsPage : public Page {
    public:
        MatrixRoomTagsPage(Program *program, Body *body) : Page(program), body(body) {}
        const char* get_title() const override { return "Tags"; }
        PluginResult submit(const std::string &title, const std::string &url, std::vector<Tab> &result_tabs) override;

        void update() override;
        void add_room_body_item_to_tag(std::shared_ptr<BodyItem> body_item, const std::string &tag);
        void remove_room_body_item_from_tag(std::shared_ptr<BodyItem> body_item, const std::string &tag);

        void move_room_to_top(RoomData *room);
        void remove_body_item_by_room_id(const std::string &room_id);

        void set_current_rooms_page(MatrixRoomsPage *rooms_page);

        MatrixQuickMedia *matrix_delegate = nullptr;
    private:
        struct TagData {
            std::shared_ptr<BodyItem> tag_item;
            std::vector<std::shared_ptr<BodyItem>> room_body_items;
        };

        std::recursive_mutex mutex;
        Body *body;
        std::map<std::string, TagData> tag_body_items_by_name;
        std::map<std::string, std::vector<std::shared_ptr<BodyItem>>> add_room_body_items_by_tags;
        std::map<std::string, std::vector<std::shared_ptr<BodyItem>>> remove_room_body_items_by_tags;
        MatrixRoomsPage *current_rooms_page = nullptr;
    };

    class MatrixInvitesPage : public Page {
    public:
        MatrixInvitesPage(Program *program, Matrix *matrix, Body *body);

        const char* get_title() const override { return title.c_str(); }
        PluginResult submit(const std::string &title, const std::string &url, std::vector<Tab> &result_tabs) override;

        void update() override;
        void add_body_item(std::shared_ptr<BodyItem> body_item);
        void remove_body_item_by_room_id(const std::string &room_id);
    private:
        Matrix *matrix;
        std::mutex mutex;
        std::vector<std::shared_ptr<BodyItem>> body_items;
        std::vector<std::string> pending_remove_body_items;
        Body *body;
        std::string title = "Invites (0)";
        size_t prev_invite_count = 0;
    };

    class MatrixInviteDetailsPage : public Page {
    public:
        MatrixInviteDetailsPage(Program *program, Matrix *matrix, MatrixInvitesPage *invites_page, std::string room_id, std::string title) :
            Page(program), matrix(matrix), invites_page(invites_page), room_id(std::move(room_id)), title(std::move(title)) {}
        const char* get_title() const override { return title.c_str(); }
        PluginResult submit(const std::string &title, const std::string &url, std::vector<Tab> &result_tabs) override;

        Matrix *matrix;
        MatrixInvitesPage *invites_page;
        const std::string room_id;
        const std::string title;
    };

    // Dummy, only play one video. TODO: Play all videos in room, as related videos?
    class MatrixVideoPage : public Page {
    public:
        MatrixVideoPage(Program *program) : Page(program) {}
        const char* get_title() const override { return ""; }
        PluginResult submit(const std::string &title, const std::string &url, std::vector<Tab> &result_tabs) override {
            (void)title;
            (void)url;
            (void)result_tabs;
            return PluginResult::ERR;
        }
        PageTypez get_type() const override { return PageTypez::VIDEO; }
    };

    class MatrixChatPage : public Page {
    public:
        MatrixChatPage(Program *program, std::string room_id, MatrixRoomsPage *rooms_page);
        ~MatrixChatPage() override;

        const char* get_title() const override { return ""; }
        PluginResult submit(const std::string &title, const std::string &url, std::vector<Tab> &result_tabs) override {
            (void)title;
            (void)url;
            (void)result_tabs;
            return PluginResult::ERR;
        }
        PageTypez get_type() const override { return PageTypez::CHAT; }
        void update() override;

        const std::string room_id;
        MatrixQuickMedia *matrix_delegate = nullptr;
        MatrixRoomsPage *rooms_page = nullptr;
    };

    class Matrix {
    public:
        void start_sync(MatrixDelegate *delegate);
        void stop_sync();
        bool is_initial_sync_finished() const;
        void get_room_sync_data(RoomData *room, SyncData &sync_data);

        void get_all_synced_room_messages(RoomData *room, Messages &messages);
        void get_all_pinned_events(RoomData *room, std::vector<std::string> &events);
        PluginResult get_previous_room_messages(RoomData *room, Messages &messages);

        // |url| should only be set when uploading media.
        // TODO: Make api better.
        PluginResult post_message(RoomData *room, const std::string &body, const std::optional<UploadInfo> &file_info, const std::optional<UploadInfo> &thumbnail_info, const std::string &msgtype = "");
        // |relates_to| is from |BodyItem.userdata| and is of type |Message*|
        PluginResult post_reply(RoomData *room, const std::string &body, void *relates_to);
        // |relates_to| is from |BodyItem.userdata| and is of type |Message*|
        PluginResult post_edit(RoomData *room, const std::string &body, void *relates_to);
        
        PluginResult post_file(RoomData *room, const std::string &filepath, std::string &err_msg);
        PluginResult login(const std::string &username, const std::string &password, const std::string &homeserver, std::string &err_msg);
        PluginResult logout();

        // |message| is from |BodyItem.userdata| and is of type |Message*|
        PluginResult delete_message(RoomData *room, void *message, std::string &err_msg);

        PluginResult load_and_verify_cached_session();

        PluginResult on_start_typing(RoomData *room);
        PluginResult on_stop_typing(RoomData *room);

        PluginResult set_read_marker(RoomData *room, const Message *message);

        PluginResult join_room(const std::string &room_id);
        PluginResult leave_room(const std::string &room_id);

        // |message| is from |BodyItem.userdata| and is of type |Message*|
        bool was_message_posted_by_me(void *message);

        std::string message_get_author_displayname(Message *message) const;

        // Cached
        PluginResult get_config(int *upload_size);

        std::shared_ptr<UserInfo> get_me(RoomData *room);

        // Returns nullptr if message cant be found. Note: cached
        std::shared_ptr<Message> get_message_by_id(RoomData *room, const std::string &event_id);

        RoomData* get_room_by_id(const std::string &id);

        bool use_tor = false;
    private:
        PluginResult parse_sync_response(const rapidjson::Document &root, MatrixDelegate *delegate);
        PluginResult parse_sync_account_data(const rapidjson::Value &account_data_json, std::optional<std::set<std::string>> &dm_rooms);
        PluginResult parse_sync_room_data(const rapidjson::Value &rooms_json, MatrixDelegate *delegate);
        PluginResult get_previous_room_messages(RoomData *room_data);
        void events_add_user_info(const rapidjson::Value &events_json, RoomData *room_data);
        void events_add_user_read_markers(const rapidjson::Value &events_json, RoomData *room_data);
        void events_add_messages(const rapidjson::Value &events_json, RoomData *room_data, MessageDirection message_dir, MatrixDelegate *delegate, bool has_unread_notifications);
        void events_set_room_name(const rapidjson::Value &events_json, RoomData *room_data);
        void events_add_pinned_events(const rapidjson::Value &events_json, RoomData *room_data);
        void events_add_room_to_tags(const rapidjson::Value &events_json, RoomData *room_data, MatrixDelegate *delegate);
        void add_invites(const rapidjson::Value &invite_json, MatrixDelegate *delegate);
        void remove_rooms(const rapidjson::Value &leave_json, MatrixDelegate *delegate);
        std::shared_ptr<Message> parse_message_event(const rapidjson::Value &event_item_json, RoomData *room_data);
        PluginResult upload_file(RoomData *room, const std::string &filepath, UploadInfo &file_info, UploadInfo &thumbnail_info, std::string &err_msg);
        void add_room(std::unique_ptr<RoomData> room);
        void remove_room(const std::string &room_id);
        void set_invite(const std::string &room_id, Invite invite);
        // Returns true if an invite for |room_id| exists
        bool remove_invite(const std::string &room_id);
        DownloadResult download_json(rapidjson::Document &result, const std::string &url, std::vector<CommandArg> additional_args, bool use_browser_useragent = false, std::string *err_msg = nullptr) const;
    private:
        std::vector<std::unique_ptr<RoomData>> rooms;
        std::unordered_map<std::string, size_t> room_data_by_id; // value is an index into |rooms|
        std::recursive_mutex room_data_mutex;
        std::string user_id;
        std::string username;
        std::string access_token;
        std::string homeserver;
        std::optional<int> upload_limit;
        std::string next_batch;

        std::unordered_map<std::string, Invite> invites;
        std::mutex invite_mutex;

        std::thread sync_thread;
        bool sync_running = false;
        MatrixDelegate *delegate = nullptr;
    };
}