aboutsummaryrefslogtreecommitdiff
path: root/plugins/Matrix.hpp
blob: 50f8c6b9e8c523d66d92014e20bc54a7821f9cab (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
#pragma once

#include "Plugin.hpp"
#include <unordered_map>
#include <json/value.h>

namespace QuickMedia {
    struct UserInfo {
        std::string display_name;
        std::string avatar_url;
    };

    enum class MessageType {
        TEXT,
        IMAGE,
        VIDEO
    };

    struct Message {
        // Index into |RoomData.user_info|
        size_t user_id;
        std::string body;
        std::string url;
        std::string thumbnail_url;
        MessageType type;
    };

    struct MessageInfo {
        int size = 0;
        int w = 0;
        int h = 0;
        const char* mimetype = nullptr;
    };

    struct RoomData {
        // 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.
        // The value is an index to |user_info|.
        std::unordered_map<std::string, size_t> user_info_by_user_id;
        std::vector<UserInfo> user_info;
        std::vector<Message> messages;
        std::string name;
        std::string prev_batch;
        bool initial_fetch_finished = false;
    };

    enum class MessageDirection {
        BEFORE,
        AFTER
    };

    class Matrix : public Plugin {
    public:
        Matrix();
        bool search_is_filter() override { return true; }
        bool search_suggestions_has_thumbnails() const override { return true; }
        bool search_results_has_thumbnails() const override { return false; }
        int get_search_delay() const override { return 0; }
        bool search_suggestion_is_search() const override { return true; }
        Page get_page_after_search() const override { return Page::EXIT; }
        PluginResult get_cached_sync(BodyItems &result_items);
        PluginResult sync();
        PluginResult get_joined_rooms(BodyItems &result_items);
        // Note: the number of items returned in |result_items| may not be the number of new messages because many messages can be combined
        // into one if one user sends multiple messages. The number of messages is returned in |num_new_messages|.
        PluginResult get_room_messages(const std::string &room_id, size_t start_index, BodyItems &result_items, size_t &num_new_messages);
        SearchResult search(const std::string &text, BodyItems &result_items) override;

        // |url| should only be set when uploading media.
        // TODO: Make api better.
        PluginResult post_message(const std::string &room_id, const std::string &body, const std::string &url = "", MessageType msgtype = MessageType::TEXT, MessageInfo *info = nullptr);
        PluginResult post_file(const std::string &room_id, const std::string &filepath);
        PluginResult login(const std::string &username, const std::string &password, const std::string &homeserver, std::string &err_msg);

        PluginResult load_and_verify_cached_session();
    private:
        PluginResult sync_response_to_body_items(const Json::Value &root);
        PluginResult load_initial_room_data(const std::string &room_id, RoomData *room_data);
        void events_add_user_info(const Json::Value &events_json, RoomData *room_data);
        void events_add_messages(const Json::Value &events_json, RoomData *room_data, MessageDirection message_dir);
        void events_set_room_name(const Json::Value &events_json, RoomData *room_data);
    private:
        std::unordered_map<std::string, std::unique_ptr<RoomData>> room_data_by_id;
        std::string user_id;
        std::string access_token;
        std::string homeserver;
        std::string next_batch;
    };
}