Skip to content
Snippets Groups Projects
TimelineModel.h 11.1 KiB
Newer Older
#pragma once

#include <QAbstractListModel>
#include <QColor>
Nicolas Werner's avatar
Nicolas Werner committed
#include <QHash>
#include <QSet>
#include <mtxclient/http/errors.hpp>
#include "CacheCryptoStructs.h"
#include "EventStore.h"
Nicolas Werner's avatar
Nicolas Werner committed
#include "InputBar.h"
Nicolas Werner's avatar
Nicolas Werner committed
#include "ui/UserProfile.h"

namespace mtx::http {
using RequestErr = const std::optional<mtx::http::ClientError> &;
}
Nicolas Werner's avatar
Nicolas Werner committed
namespace mtx::responses {
struct Timeline;
struct Messages;
struct ClaimKeys;
}
Nicolas Werner's avatar
Nicolas Werner committed
struct RelatedInfo;
namespace qml_mtx_events {
Q_NAMESPACE

enum EventType
{
        // Unsupported event
        Unsupported,
        /// m.room_key_request
        KeyRequest,
        /// m.reaction,
        Reaction,
        /// m.room.aliases
        Aliases,
        /// m.room.avatar
        Avatar,
trilene's avatar
trilene committed
        /// m.call.invite
        CallInvite,
        /// m.call.answer
        CallAnswer,
        /// m.call.hangup
        CallHangUp,
        /// m.call.candidates
        CallCandidates,
        /// m.room.canonical_alias
        CanonicalAlias,
        /// m.room.create
        /// m.room.encrypted.
        Encrypted,
        /// m.room.encryption.
        Encryption,
        /// m.room.guest_access
        RoomGuestAccess,
        /// m.room.history_visibility
        RoomHistoryVisibility,
        /// m.room.join_rules
        RoomJoinRules,
        /// m.room.member
        Member,
        /// m.room.name
        Name,
        /// m.room.power_levels
        PowerLevels,
        /// m.room.tombstone
        Tombstone,
        /// m.room.topic
        Topic,
        /// m.room.redaction
        Redaction,
        /// m.room.pinned_events
        PinnedEvents,
        // m.sticker
        Sticker,
        // m.tag
        Tag,
        /// m.room.message
        AudioMessage,
        EmoteMessage,
        FileMessage,
        ImageMessage,
        LocationMessage,
        NoticeMessage,
        TextMessage,
        VideoMessage,
        Redacted,
        UnknownMessage,
        KeyVerificationRequest,
        KeyVerificationStart,
        KeyVerificationMac,
        KeyVerificationAccept,
        KeyVerificationCancel,
        KeyVerificationKey,
        KeyVerificationDone,
        KeyVerificationReady
};
Q_ENUM_NS(EventType)

enum EventState
{
        //! The plaintext message was received by the server.
        Received,
        //! At least one of the participants has read the message.
        Read,
        //! The client sent the message. Not yet received.
        Sent,
        //! When the message is loaded from cache or backfill.
        Empty,
};
Q_ENUM_NS(EventState)
class StateKeeper
{
public:
        StateKeeper(std::function<void()> &&fn)
          : fn_(std::move(fn))
        {}

        ~StateKeeper() { fn_(); }

private:
        std::function<void()> fn_;
};

struct DecryptionResult
{
        //! The decrypted content as a normal plaintext event.
        mtx::events::collections::TimelineEvents event;
        //! Whether or not the decryption was successful.
        bool isDecrypted = false;
};

class TimelineViewManager;

class TimelineModel : public QAbstractListModel
{
        Q_OBJECT
        Q_PROPERTY(
          int currentIndex READ currentIndex WRITE setCurrentIndex NOTIFY currentIndexChanged)
        Q_PROPERTY(std::vector<QString> typingUsers READ typingUsers WRITE updateTypingUsers NOTIFY
                     typingUsersChanged)
Nicolas Werner's avatar
Nicolas Werner committed
        Q_PROPERTY(QString reply READ reply WRITE setReply NOTIFY replyChanged RESET resetReply)
Nicolas Werner's avatar
Nicolas Werner committed
        Q_PROPERTY(QString edit READ edit WRITE setEdit NOTIFY editChanged RESET resetEdit)
Lasath Fernando's avatar
Lasath Fernando committed
        Q_PROPERTY(
          bool paginationInProgress READ paginationInProgress NOTIFY paginationInProgressChanged)
Nicolas Werner's avatar
Nicolas Werner committed
        Q_PROPERTY(QString roomName READ roomName NOTIFY roomNameChanged)
        Q_PROPERTY(QString roomAvatarUrl READ roomAvatarUrl NOTIFY roomAvatarUrlChanged)
        Q_PROPERTY(QString roomTopic READ roomTopic NOTIFY roomTopicChanged)
Nicolas Werner's avatar
Nicolas Werner committed
        Q_PROPERTY(InputBar *input READ input CONSTANT)
        explicit TimelineModel(TimelineViewManager *manager,
                               QString room_id,
                               QObject *parent = nullptr);
                Body,
                FormattedBody,
                PreviousMessageUserId,
                UserId,
                UserName,
                PreviousMessageDay,
                Day,
                Timestamp,
                ThumbnailUrl,
Nicolas Werner's avatar
Nicolas Werner committed
                Filename,
Nicolas Werner's avatar
Nicolas Werner committed
                Filesize,
Nicolas Werner's avatar
Nicolas Werner committed
                MimeType,
                Height,
                Width,
                ProportionalHeight,
                State,
Nicolas Werner's avatar
Nicolas Werner committed
                IsEdited,
                IsEditable,
                IsEncrypted,
                ReplyTo,
                Reactions,
                RoomName,
                RoomTopic,
trilene's avatar
trilene committed
                CallType,
Nicolas Werner's avatar
Nicolas Werner committed
                Dump,
        };

        QHash<int, QByteArray> roleNames() const override;
        int rowCount(const QModelIndex &parent = QModelIndex()) const override;
        QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
        QVariant data(const mtx::events::collections::TimelineEvents &event, int role) const;
        bool canFetchMore(const QModelIndex &) const override;
        void fetchMore(const QModelIndex &) override;

        Q_INVOKABLE QString displayName(QString id) const;
        Q_INVOKABLE QString avatarUrl(QString id) const;
        Q_INVOKABLE QString formatDateSeparator(QDate date) const;
        Q_INVOKABLE QString formatTypingUsers(const std::vector<QString> &users, QColor bg);
        Q_INVOKABLE QString formatMemberEvent(QString id);
        Q_INVOKABLE QString formatJoinRuleEvent(QString id);
        Q_INVOKABLE QString formatHistoryVisibilityEvent(QString id);
        Q_INVOKABLE QString formatGuestAccessEvent(QString id);
        Q_INVOKABLE QString formatPowerLevelEvent(QString id);
        Q_INVOKABLE void viewRawMessage(QString id) const;
        Q_INVOKABLE void viewDecryptedRawMessage(QString id) const;
        Q_INVOKABLE void openUserProfile(QString userid, bool global = false);
Nicolas Werner's avatar
Nicolas Werner committed
        Q_INVOKABLE void editAction(QString id);
        Q_INVOKABLE void replyAction(QString id);
        Q_INVOKABLE void readReceiptsAction(QString id) const;
Nicolas Werner's avatar
Nicolas Werner committed
        Q_INVOKABLE void redactEvent(QString id);
        Q_INVOKABLE int idToIndex(QString id) const;
        Q_INVOKABLE QString indexToId(int index) const;
        Q_INVOKABLE void openMedia(QString eventId);
        Q_INVOKABLE void cacheMedia(QString eventId);
        Q_INVOKABLE bool saveMedia(QString eventId) const;
        void cacheMedia(QString eventId, std::function<void(const QString filename)> callback);
Nicolas Werner's avatar
Nicolas Werner committed
        std::vector<::Reaction> reactions(const std::string &event_id)
        {
                auto list = events.reactions(event_id);
                std::vector<::Reaction> vec;
                for (const auto &r : list)
                        vec.push_back(r.value<Reaction>());
                return vec;
        }

        void updateLastMessage();
Nicolas Werner's avatar
Nicolas Werner committed
        void addEvents(const mtx::responses::Timeline &events);
Nicolas Werner's avatar
Nicolas Werner committed
        void syncState(const mtx::responses::State &state);
        template<class T>
trilene's avatar
trilene committed
        void sendMessageEvent(const T &content, mtx::events::EventType eventType);
Nicolas Werner's avatar
Nicolas Werner committed
        RelatedInfo relatedInfo(QString id);
        void setCurrentIndex(int index);
        int currentIndex() const { return idToIndex(currentId); }
        void markEventsAsRead(const std::vector<QString> &event_ids);
        QVariantMap getDump(QString eventId, QString relatedTo) const;
        void updateTypingUsers(const std::vector<QString> &users)
        {
                if (this->typingUsers_ != users) {
                        this->typingUsers_ = users;
                        emit typingUsersChanged(typingUsers_);
                }
        }
        std::vector<QString> typingUsers() const { return typingUsers_; }
        bool paginationInProgress() const { return m_paginationInProgress; }
Nicolas Werner's avatar
Nicolas Werner committed
        QString reply() const { return reply_; }
        void setReply(QString newReply)
        {
                if (edit_.startsWith('m'))
                        return;

Nicolas Werner's avatar
Nicolas Werner committed
                if (reply_ != newReply) {
                        reply_ = newReply;
                        emit replyChanged(reply_);
                }
        }
        void resetReply()
        {
                if (!reply_.isEmpty()) {
                        reply_ = "";
                        emit replyChanged(reply_);
                }
        }
Nicolas Werner's avatar
Nicolas Werner committed
        QString edit() const { return edit_; }
        void setEdit(QString newEdit);
        void resetEdit();
        void setDecryptDescription(bool decrypt) { decryptDescription = decrypt; }
        void clearTimeline() { events.clearTimeline(); }
        void receivedSessionKey(const std::string &session_key)
        {
                events.receivedSessionKey(session_key);
        }
Nicolas Werner's avatar
Nicolas Werner committed
        QString roomName() const;
        QString roomTopic() const;
Nicolas Werner's avatar
Nicolas Werner committed
        InputBar *input() { return &input_; }
Nicolas Werner's avatar
Nicolas Werner committed
        QString roomAvatarUrl() const;
        QString roomId() const { return room_id_; }
        void addPendingMessage(mtx::events::collections::TimelineEvents event);
        void currentIndexChanged(int index);
Nicolas Werner's avatar
Nicolas Werner committed
        void redactionFailed(QString id);
        void eventRedacted(QString id);
        void mediaCached(QString mxcUrl, QString cacheUrl);
        void newEncryptedImage(mtx::crypto::EncryptedFile encryptionInfo);
        void typingUsersChanged(std::vector<QString> users);
Nicolas Werner's avatar
Nicolas Werner committed
        void replyChanged(QString reply);
Nicolas Werner's avatar
Nicolas Werner committed
        void editChanged(QString reply);
        void paginationInProgressChanged(const bool);
trilene's avatar
trilene committed
        void newCallEvent(const mtx::events::collections::TimelineEvents &event);
Nicolas Werner's avatar
Nicolas Werner committed
        void openProfile(UserProfile *profile);
        void newMessageToSend(mtx::events::collections::TimelineEvents event);
        void addPendingMessageToStore(mtx::events::collections::TimelineEvents event);
        void updateFlowEventId(std::string event_id);

Nicolas Werner's avatar
Nicolas Werner committed
        void roomNameChanged();
        void roomTopicChanged();
        void roomAvatarUrlChanged();

private:
        template<typename T>
        void sendEncryptedMessage(mtx::events::RoomEvent<T> msg, mtx::events::EventType eventType);
        void readEvent(const std::string &id);
        void setPaginationInProgress(const bool paginationInProgress);

        mutable EventStore events;

Lasath Fernando's avatar
Lasath Fernando committed
        bool decryptDescription     = true;
        bool m_paginationInProgress = false;
        QString currentId, currentReadId;
Nicolas Werner's avatar
Nicolas Werner committed
        QString reply_, edit_;
        std::vector<QString> typingUsers_;

        TimelineViewManager *manager_;
Nicolas Werner's avatar
Nicolas Werner committed
        InputBar input_{this};

        friend struct SendMessageVisitor;
trilene's avatar
trilene committed
TimelineModel::sendMessageEvent(const T &content, mtx::events::EventType eventType)
        mtx::events::RoomEvent<T> msgCopy = {};
trilene's avatar
trilene committed
        msgCopy.content                   = content;
        msgCopy.type                      = eventType;
        emit newMessageToSend(msgCopy);