File indexing completed on 2024-07-07 14:59:54

0001 /*
0002  * SPDX-FileCopyrightText: 2016 Riccardo Iaconelli <riccardo@kde.org>
0003  * SPDX-FileCopyrightText: 2017-2024 Laurent Montel <montel@kde.org>
0004  *
0005  * SPDX-License-Identifier: LGPL-2.0-or-later
0006  *
0007  */
0008 
0009 #pragma once
0010 
0011 #include "notifications/notificationoptions.h"
0012 #include "roles/roles.h"
0013 #include <QJsonObject>
0014 #include <QObject>
0015 #include <QPointer>
0016 
0017 #include "channelcounterinfo.h"
0018 #include "libruqolacore_export.h"
0019 #include "retentioninfo.h"
0020 #include "teams/teaminfo.h"
0021 #include "utils.h"
0022 
0023 #include <memory>
0024 
0025 class UsersForRoomModel;
0026 class MessagesModel;
0027 class RocketChatAccount;
0028 class LIBRUQOLACORE_EXPORT Room : public QObject
0029 {
0030     Q_OBJECT
0031 public:
0032     explicit Room(RocketChatAccount *account = nullptr, QObject *parent = nullptr);
0033     ~Room() override;
0034 
0035     struct TeamRoomInfo {
0036         QString teamName;
0037         QString teamIdentifier;
0038     };
0039 
0040     // t (can take values "d" , "c" or "p" or "l")
0041     enum class RoomType {
0042         Unknown,
0043         Direct,
0044         Channel,
0045         Private,
0046     };
0047     Q_ENUM(RoomType)
0048 
0049     [[nodiscard]] static QString roomFromRoomType(RoomType type);
0050 
0051     // To be used in ID find: message ID
0052     [[nodiscard]] bool operator==(const Room &other) const;
0053 
0054     // we can't use operator== as it tests only id. We need it for autotest
0055     [[nodiscard]] bool isEqual(const Room &other) const;
0056 
0057     [[nodiscard]] QString roomOwnerUserName() const;
0058     void setRoomCreatorUserName(const QString &userName);
0059 
0060     [[nodiscard]] QString roomCreatorUserId() const;
0061     void setRoomCreatorUserId(const QString &userId);
0062 
0063     [[nodiscard]] QStringList mutedUsers() const;
0064     void setMutedUsers(const QStringList &mutedUsers);
0065 
0066     [[nodiscard]] qint64 jitsiTimeout() const;
0067     void setJitsiTimeout(qint64 jitsiTimeout);
0068 
0069     [[nodiscard]] int unread() const;
0070     void setUnread(int unread);
0071 
0072     [[nodiscard]] bool selected() const;
0073     void setSelected(bool selected);
0074 
0075     /**
0076      * @brief Return room name
0077      *
0078      * @return QString, The name of the room
0079      */
0080     [[nodiscard]] QString name() const;
0081     void setName(const QString &name);
0082 
0083     [[nodiscard]] QString displayRoomName() const;
0084 
0085     [[nodiscard]] QString announcement() const;
0086     void setAnnouncement(const QString &announcement);
0087 
0088     [[nodiscard]] RoomType channelType() const;
0089     void setChannelType(RoomType channelType);
0090 
0091     [[nodiscard]] bool favorite() const;
0092     void setFavorite(bool favorite);
0093 
0094     [[nodiscard]] QString topic() const;
0095     void setTopic(const QString &topic);
0096 
0097     [[nodiscard]] bool readOnly() const;
0098     void setReadOnly(bool readOnly);
0099 
0100     [[nodiscard]] bool open() const;
0101     void setOpen(bool open);
0102 
0103     [[nodiscard]] bool alert() const;
0104     void setAlert(bool alert);
0105 
0106     [[nodiscard]] QString roomId() const;
0107     void setRoomId(const QString &id);
0108 
0109     void setBlocker(bool alert);
0110     [[nodiscard]] bool blocker() const;
0111 
0112     void parseSubscriptionRoom(const QJsonObject &json);
0113     void parseUpdateRoom(const QJsonObject &json);
0114 
0115     /**
0116      * @brief Constructs Room object from QJsonObject (cache)
0117      *
0118      * @param source The Json containing room attributes
0119      * @return Room object, The room constructed from Json
0120      */
0121     static std::unique_ptr<Room> deserialize(const QJsonObject &source);
0122     static void deserialize(Room *r, const QJsonObject &o);
0123 
0124     /**
0125      * @brief Constructs QBytearray from Room object
0126      *
0127      * @param message The Room object
0128      * @return QByteArray, The Json containing room attributes
0129      */
0130     static QByteArray serialize(Room *r, bool toBinary = true);
0131 
0132     [[nodiscard]] UsersForRoomModel *usersModelForRoom() const;
0133 
0134     [[nodiscard]] MessagesModel *messageModel() const;
0135 
0136     [[nodiscard]] QString inputMessage() const;
0137     void setInputMessage(const QString &inputMessage);
0138 
0139     [[nodiscard]] bool archived() const;
0140     void setArchived(bool archived);
0141 
0142     [[nodiscard]] QString description() const;
0143     void setDescription(const QString &description);
0144 
0145     [[nodiscard]] bool encryptedEnabled() const;
0146     [[nodiscard]] bool canBeModify() const;
0147     [[nodiscard]] NotificationOptions notificationOptions() const;
0148     void setNotificationOptions(const NotificationOptions &notificationOptions);
0149 
0150     [[nodiscard]] int userMentions() const;
0151     void setUserMentions(int userMentions);
0152 
0153     void updateSubscriptionRoom(const QJsonObject &json);
0154     [[nodiscard]] qint64 updatedAt() const;
0155     void setUpdatedAt(qint64 updatedAt);
0156 
0157     void parseInsertRoom(const QJsonObject &json);
0158 
0159     [[nodiscard]] qint64 lastSeenAt() const;
0160     void setLastSeenAt(qint64 lastSeenAt);
0161 
0162     [[nodiscard]] qint64 lastMessageAt() const;
0163     void setLastMessageAt(qint64 lastMessageAt);
0164 
0165     [[nodiscard]] bool blocked() const;
0166     void setBlocked(bool blocked);
0167 
0168     [[nodiscard]] QStringList roles() const;
0169     void setRoles(const QStringList &roles);
0170 
0171     [[nodiscard]] QStringList ignoredUsers() const;
0172     void setIgnoredUsers(const QStringList &ignoredUsers);
0173 
0174     [[nodiscard]] bool encrypted() const;
0175     void setEncrypted(bool encrypted);
0176 
0177     [[nodiscard]] bool userIsIgnored(const QString &userId);
0178     [[nodiscard]] QString roomMessageInfo() const;
0179 
0180     [[nodiscard]] QString e2EKey() const;
0181     void setE2EKey(const QString &e2EKey);
0182 
0183     [[nodiscard]] QString e2eKeyId() const;
0184     void setE2eKeyId(const QString &e2eKeyId);
0185 
0186     [[nodiscard]] bool joinCodeRequired() const;
0187     void setJoinCodeRequired(bool joinCodeRequired);
0188 
0189     [[nodiscard]] bool wasInitialized() const;
0190     void setWasInitialized(bool wasInitialized);
0191 
0192     [[nodiscard]] Roles rolesForRooms() const;
0193     void setRolesForRooms(const Roles &rolesForRooms);
0194 
0195     [[nodiscard]] QStringList rolesForUserId(const QString &userId);
0196 
0197     [[nodiscard]] bool canChangeRoles() const;
0198     [[nodiscard]] bool userHasOwnerRole(const QString &userId) const;
0199     [[nodiscard]] bool userHasLeaderRole(const QString &userId) const;
0200     [[nodiscard]] bool userHasModeratorRole(const QString &userId) const;
0201 
0202     void updateRoles(const QJsonObject &obj);
0203 
0204     [[nodiscard]] bool broadcast() const;
0205     void setBroadcast(bool broadcast);
0206 
0207     [[nodiscard]] QString parentRid() const;
0208     void setParentRid(const QString &parentRid);
0209 
0210     [[nodiscard]] QString fName() const;
0211     void setFName(const QString &value);
0212 
0213     [[nodiscard]] QString displayFName() const;
0214     [[nodiscard]] bool isDiscussionRoom() const;
0215 
0216     [[nodiscard]] QString autoTranslateLanguage() const;
0217     void setAutoTranslateLanguage(const QString &autoTranslateLanguage);
0218 
0219     [[nodiscard]] bool autoTranslate() const;
0220     void setAutoTranslate(bool autoTranslate);
0221 
0222     [[nodiscard]] QString directChannelUserId() const;
0223     void setDirectChannelUserId(const QString &userId);
0224 
0225     [[nodiscard]] QStringList displaySystemMessageTypes() const;
0226     void setDisplaySystemMessageTypes(const QStringList &displaySystemMessageTypes);
0227 
0228     [[nodiscard]] ChannelCounterInfo channelCounterInfo() const;
0229     void setChannelCounterInfo(const ChannelCounterInfo &channelCounterInfo);
0230 
0231     void newMessageAdded();
0232 
0233     [[nodiscard]] bool allowToPinMessage() const;
0234     [[nodiscard]] QString avatarETag() const;
0235     void setAvatarETag(const QString &avatarETag);
0236 
0237     [[nodiscard]] Utils::AvatarInfo avatarInfo() const;
0238 
0239     [[nodiscard]] QStringList uids() const;
0240     void setUids(const QStringList &uids);
0241 
0242     [[nodiscard]] QStringList userNames() const;
0243     void setUserNames(const QStringList &userNames);
0244 
0245     [[nodiscard]] QStringList highlightsWord() const;
0246     void setHighlightsWord(const QStringList &highlightsWord);
0247     [[nodiscard]] bool hasPermission(const QString &permission) const;
0248 
0249     [[nodiscard]] QString displayTopic() const;
0250     [[nodiscard]] QString displayAnnouncement() const;
0251 
0252     [[nodiscard]] RetentionInfo retentionInfo() const;
0253     void setRetentionInfo(RetentionInfo retentionInfo);
0254 
0255     [[nodiscard]] static Room::RoomType roomTypeFromString(const QString &type);
0256     [[nodiscard]] TeamInfo teamInfo() const;
0257     void setTeamInfo(const TeamInfo &teamInfo);
0258 
0259     [[nodiscard]] Room::TeamRoomInfo teamRoomInfo() const;
0260     [[nodiscard]] bool roomIsBlocked() const;
0261     [[nodiscard]] bool hideBadgeForMention() const;
0262     [[nodiscard]] bool hideUnreadStatus() const;
0263 
0264     [[nodiscard]] qint64 numberMessages() const;
0265     void setNumberMessages(qint64 newNumberMessages);
0266 
0267 Q_SIGNALS:
0268     void highlightsWordChanged();
0269     void nameChanged();
0270     void fnameChanged();
0271     void announcementChanged();
0272     void topicChanged();
0273     void favoriteChanged();
0274     void alertChanged();
0275     void readOnlyChanged();
0276     void unreadChanged();
0277     void openChanged();
0278     void encryptedChanged();
0279     void encryptionKeyChanged();
0280     void encryptionKeyIdChanged();
0281     void needAttention();
0282 
0283     // Blocker we blocked the channel
0284     void blockerChanged();
0285     // Blocked the channel was blocked
0286     void blockedChanged();
0287 
0288     void archivedChanged();
0289     void descriptionChanged();
0290     void rolesChanged();
0291     void mutedUsersChanged();
0292     void ignoredUsersChanged();
0293     void jitsiTimeoutChanged();
0294     void joinCodeRequiredChanged();
0295     void channelTypeChanged();
0296 
0297     void broadcastChanged();
0298     void parentRidChanged();
0299     void autoTranslateLanguageChanged();
0300     void autoTranslateChanged();
0301     void lastSeenChanged();
0302     void lastMessageAtChanged();
0303 
0304     void directChannelUserIdChanged();
0305     void displaySystemMessageTypesChanged();
0306     void channelCounterInfoChanged();
0307 
0308     void notificationOptionsChanged();
0309     void avatarETagChanged();
0310 
0311     void uidsChanged();
0312     void userNamesChanged();
0313 
0314     void retentionInfoChanged();
0315     void teamInfoChanged();
0316 
0317 private:
0318     Q_DISABLE_COPY(Room)
0319     LIBRUQOLACORE_NO_EXPORT void parseCommonData(const QJsonObject &json);
0320     LIBRUQOLACORE_NO_EXPORT void parseDisplaySystemMessage(const QJsonObject &json);
0321     LIBRUQOLACORE_NO_EXPORT void parseRetentionInfo(const QJsonObject &json);
0322     LIBRUQOLACORE_NO_EXPORT void parseTeamInfo(const QJsonObject &obj);
0323 
0324     // Room Object Fields
0325 
0326     NotificationOptions mNotificationOptions;
0327     // muted - collection of muted users by its usernames
0328     QStringList mMutedUsers;
0329 
0330     QStringList mIgnoredUsers;
0331 
0332     QStringList mHighlightsWord;
0333 
0334     // Roles
0335     QStringList mRoles;
0336 
0337     QStringList mUids;
0338     QStringList mUserNames;
0339 
0340     QString mInputMessage;
0341 
0342     // _id
0343     QString mRoomId;
0344 
0345     // t (can take values "d" , "c" or "p" or "l")
0346     Room::RoomType mChannelType = RoomType::Unknown;
0347 
0348     // Parent Rid when we have a discussion.
0349     QString mParentRid;
0350 
0351     // name
0352     QString mName;
0353 
0354     QString mFName;
0355 
0356     // Announcement
0357     QString mAnnouncement;
0358 
0359     // u
0360     QString mRoomCreatorUserName;
0361     QString mRoomCreateUserId;
0362 
0363     // topic
0364     QString mTopic;
0365 
0366     QString mDescription;
0367 
0368     // jitsiTimeout
0369     qint64 mJitsiTimeout = -1;
0370     qint64 mUpdatedAt = -1;
0371     qint64 mLastSeenAt = -1;
0372     qint64 mLastMessageAt = -1;
0373 
0374     // Encryption Key
0375     QString mE2EKey;
0376     QString mE2eKeyId;
0377 
0378     // Autotranslate
0379     QString mAutotranslateLanguage;
0380 
0381     // AvatarEtag
0382     QString mAvatarETag;
0383 
0384     // Roles In Room
0385     Roles mRolesForRooms;
0386 
0387     // quint64 ?
0388     int mUnread = 0;
0389     int mUserMentions = 0;
0390 
0391     qint64 mNumberMessages = 0;
0392 
0393     // ro - read-only chat or not
0394     bool mReadOnly = false;
0395     bool mSelected = false;
0396     bool mFavorite = false;
0397     // We can hide it or not.
0398     bool mOpen = false;
0399     bool mAlert = false;
0400     bool mBlocker = false;
0401     bool mArchived = false;
0402     bool mBlocked = false;
0403     bool mEncrypted = false;
0404     bool mJoinCodeRequired = false;
0405     bool mWasInitialized = false;
0406     bool mBroadcast = false;
0407     bool mAutoTranslate = false;
0408 
0409     QString mDirectChannelUserId;
0410 
0411     QStringList mDisplaySystemMessageType;
0412     ChannelCounterInfo mChannelCounterInfo;
0413     RetentionInfo mRetentionInfo;
0414     TeamInfo mTeamInfo;
0415     mutable Utils::AvatarInfo mCurrentAvatarInfo;
0416 
0417     UsersForRoomModel *const mUsersModelForRoom;
0418     QPointer<MessagesModel> mMessageModel;
0419     RocketChatAccount *const mRocketChatAccount;
0420 };
0421 
0422 LIBRUQOLACORE_EXPORT QDebug operator<<(QDebug d, const Room &t);