File indexing completed on 2024-05-19 08:52:50

0001 /*
0002 
0003  * SPDX-FileCopyrightText: 2016 Riccardo Iaconelli <riccardo@kde.org>
0004  *
0005  * SPDX-License-Identifier: LGPL-2.0-or-later
0006  *
0007  */
0008 
0009 #pragma once
0010 
0011 #include "libruqolacore_export.h"
0012 #include "messages/message.h"
0013 #include <QAbstractListModel>
0014 #include <QPointer>
0015 
0016 class RocketChatAccount;
0017 class LoadRecentHistoryManager;
0018 class Room;
0019 class LIBRUQOLACORE_EXPORT MessagesModel : public QAbstractListModel
0020 {
0021     Q_OBJECT
0022 public:
0023     struct LIBRUQOLACORE_EXPORT AttachmentAndUrlPreviewVisibility {
0024         bool show = false;
0025         QString ElementId;
0026     };
0027     enum MessageRoles {
0028         Username = Qt::UserRole + 1,
0029         MessagePointer,
0030         OriginalMessage,
0031         MessageConvertedText,
0032         Timestamp,
0033         DateTimeUtc,
0034         UserId,
0035         SystemMessageType,
0036         MessageId,
0037         RoomId,
0038         UpdatedAt,
0039         EditedAt,
0040         EditedByUserName,
0041         EditedByUserId,
0042         EditedToolTip,
0043         Alias,
0044         Avatar,
0045         Groupable,
0046         ParseUrls,
0047         MessageType,
0048         Attachments,
0049         Urls,
0050         Date,
0051         DateDiffersFromPrevious,
0052         CanEditMessage,
0053         CanDeleteMessage,
0054         Starred,
0055         UsernameUrl,
0056         Roles,
0057         Reactions,
0058         Ignored,
0059         Pinned,
0060         DiscussionCount,
0061         DiscussionRoomId,
0062         DiscussionLastMessage,
0063         ThreadCount,
0064         ThreadLastMessage,
0065         ThreadMessageId,
0066         ThreadMessagePreview,
0067         ThreadMessage,
0068         ThreadMessageFollowed,
0069         ShowTranslatedMessage,
0070         DisplayAttachment,
0071         DisplayLastSeenMessage,
0072         DisplayUrlPreview,
0073         Emoji,
0074         AvatarInfo,
0075         PendingMessage,
0076         ShowIgnoredMessage,
0077         MessageInEditMode,
0078         DirectChannels,
0079         HoverHighLight,
0080         LocalTranslation,
0081         OriginalMessageOrAttachmentDescription,
0082         GoToMessageBackgroundColor,
0083         LastMessageRoles = GoToMessageBackgroundColor,
0084     };
0085     Q_ENUM(MessageRoles)
0086 
0087     explicit MessagesModel(const QString &roomID = QStringLiteral("no_room"),
0088                            RocketChatAccount *account = nullptr,
0089                            Room *room = nullptr,
0090                            QObject *parent = nullptr);
0091     ~MessagesModel() override;
0092 
0093     /**
0094      * @brief Adds a number of messages to the model
0095      *
0096      * @param messages The messages to be added
0097      */
0098     void addMessages(const QVector<Message> &messages, bool insertListMessages = false);
0099 
0100     /**
0101      * @brief returns number of messages in the model
0102      *
0103      * @param parent, it is void
0104      * @return int, The number of messages in QVector mAllMessages
0105      */
0106     [[nodiscard]] int rowCount(const QModelIndex &parent = QModelIndex()) const override;
0107 
0108     [[nodiscard]] QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
0109     bool setData(const QModelIndex &index, const QVariant &value, int role) override;
0110 
0111     /**
0112      * @brief Returns last timestamp of last message in QVector mAllMessages
0113      *
0114      * @return qint64 The last timestamp
0115      */
0116     [[nodiscard]] qint64 lastTimestamp() const;
0117 
0118     void deleteMessage(const QString &messageId);
0119 
0120     [[nodiscard]] qint64 generateNewStartTimeStamp(qint64 lastTimeStamp);
0121 
0122     void setRoomId(const QString &roomId);
0123     [[nodiscard]] bool isEmpty() const;
0124 
0125     void clear();
0126 
0127     void changeShowOriginalMessage(const QString &messageId, bool showOriginal);
0128 
0129     [[nodiscard]] QString roomId() const;
0130 
0131     void activate();
0132     void deactivate();
0133 
0134     [[nodiscard]] Message findLastMessageBefore(const QString &messageId, const std::function<bool(const Message &)> &predicate) const;
0135     [[nodiscard]] Message findNextMessageAfter(const QString &messageId, const std::function<bool(const Message &)> &predicate) const;
0136     [[nodiscard]] Message findMessageById(const QString &messageId) const;
0137     [[nodiscard]] QModelIndex indexForMessage(const QString &messageId) const;
0138 
0139     [[nodiscard]] QString messageIdFromIndex(int rowIndex);
0140 
0141     [[nodiscard]] QString searchText() const;
0142     void setSearchText(const QString &searchText);
0143 
0144     [[nodiscard]] Message threadMessage(const QString &threadMessageId) const;
0145 
0146 private:
0147     LIBRUQOLACORE_NO_EXPORT void slotFileDownloaded(const QString &filePath, const QUrl &cacheImageUrl);
0148     /**
0149      * @brief Adds a message to the model
0150      *
0151      * @param message The message to be added
0152      */
0153     LIBRUQOLACORE_NO_EXPORT void addMessage(const Message &message);
0154 
0155     LIBRUQOLACORE_NO_EXPORT void refresh();
0156     [[nodiscard]] LIBRUQOLACORE_NO_EXPORT bool threadMessageFollowed(const QString &threadMessageId) const;
0157     [[nodiscard]] LIBRUQOLACORE_NO_EXPORT QStringList roomRoles(const QString &userId) const;
0158     [[nodiscard]] LIBRUQOLACORE_NO_EXPORT QString convertMessageText(const Message &message,
0159                                                                      const QString &userName,
0160                                                                      const QStringList &highlightWords,
0161                                                                      const QString &searchedText) const;
0162     [[nodiscard]] LIBRUQOLACORE_NO_EXPORT QString threadMessagePreview(const QString &threadMessageId) const;
0163     [[nodiscard]] LIBRUQOLACORE_NO_EXPORT QVector<Message>::iterator findMessage(const QString &messageId);
0164     [[nodiscard]] LIBRUQOLACORE_NO_EXPORT QVector<Message>::const_iterator findMessage(const QString &messageId) const;
0165     [[nodiscard]] LIBRUQOLACORE_NO_EXPORT QString convertedText(const Message &message, const QString &searchedText) const;
0166 
0167     QString mSearchText;
0168     QString mRoomId;
0169     QVector<Message> mAllMessages;
0170     RocketChatAccount *mRocketChatAccount = nullptr;
0171     QPointer<Room> mRoom;
0172     std::unique_ptr<LoadRecentHistoryManager> mLoadRecentHistoryManager;
0173 };
0174 Q_DECLARE_METATYPE(MessagesModel::AttachmentAndUrlPreviewVisibility)
0175 Q_DECLARE_TYPEINFO(MessagesModel::AttachmentAndUrlPreviewVisibility, Q_RELOCATABLE_TYPE);