File indexing completed on 2024-12-01 13:09:19

0001 /*
0002    SPDX-FileCopyrightText: 2017-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include "libruqolacore_export.h"
0010 #include "roles/roleinfo.h"
0011 #include <QDateTime>
0012 #include <QDebug>
0013 #include <QString>
0014 
0015 class LIBRUQOLACORE_EXPORT User
0016 {
0017     Q_GADGET
0018 public:
0019     enum class PresenceStatus {
0020         PresenceOnline,
0021         PresenceBusy,
0022         PresenceAway,
0023         PresenceOffline,
0024         Unknown,
0025     };
0026     Q_ENUM(PresenceStatus)
0027 
0028     struct LIBRUQOLACORE_EXPORT UserEmailsInfo {
0029         QString email;
0030         bool verified = false;
0031         [[nodiscard]] bool operator==(const UserEmailsInfo &other) const
0032         {
0033             return (email == other.email) && (verified == other.verified);
0034         }
0035 
0036         [[nodiscard]] bool isValid() const
0037         {
0038             return !email.isEmpty();
0039         }
0040     };
0041 
0042     User();
0043     ~User();
0044 
0045     [[nodiscard]] QString name() const;
0046     void setName(const QString &name);
0047 
0048     [[nodiscard]] QString userId() const;
0049     void setUserId(const QString &userId);
0050 
0051     [[nodiscard]] User::PresenceStatus status() const;
0052     void setStatus(User::PresenceStatus status);
0053 
0054     void parseUser(const QJsonObject &json);
0055     [[nodiscard]] QString iconFromStatus() const;
0056 
0057     [[nodiscard]] bool operator==(const User &other) const;
0058     [[nodiscard]] bool operator!=(const User &other) const;
0059 
0060     [[nodiscard]] QString userName() const;
0061     void setUserName(const QString &userName);
0062 
0063     [[nodiscard]] bool isValid() const;
0064 
0065     [[nodiscard]] double utcOffset() const;
0066     void setUtcOffset(double utcOffset);
0067 
0068     [[nodiscard]] QString statusText() const;
0069     void setStatusText(const QString &statusText);
0070 
0071     void parseUserRestApi(const QJsonObject &object, const QVector<RoleInfo> &roleInfo);
0072 
0073     [[nodiscard]] static QVector<User> parseUsersList(const QJsonObject &object, const QVector<RoleInfo> &roleInfo);
0074     void parseUser(const QVariantList &list);
0075 
0076     [[nodiscard]] QStringList roles() const;
0077     void setRoles(const QStringList &roles, const QVector<RoleInfo> &roleInfo);
0078 
0079     [[nodiscard]] QDateTime createdAt() const;
0080     void setCreatedAt(const QDateTime &createdAt);
0081 
0082     [[nodiscard]] QDateTime lastLogin() const;
0083     void setLastLogin(const QDateTime &lastLogin);
0084 
0085     [[nodiscard]] UserEmailsInfo userEmailsInfo() const;
0086     void setUserEmailsInfo(const UserEmailsInfo &userEmailsInfo);
0087 
0088     [[nodiscard]] QStringList i18nRoles() const;
0089 
0090     [[nodiscard]] bool active() const;
0091     void setActive(bool newActive);
0092 
0093     [[nodiscard]] QString generateStatusStr() const;
0094 
0095     [[nodiscard]] bool requirePasswordChange() const;
0096     void setRequirePasswordChange(bool newRequirePasswordChange);
0097 
0098     [[nodiscard]] static QString roleI18n(const QString &roleStr, const QVector<RoleInfo> &roleInfo);
0099 
0100     [[nodiscard]] QString bio() const;
0101     void setBio(const QString &newBio);
0102 
0103     static QJsonObject serialize(const User &user);
0104     static User deserialize(const QJsonObject &o);
0105 
0106 private:
0107     UserEmailsInfo mUserEmailsInfo;
0108     QDateTime mCreatedAt;
0109     QDateTime mLastLogin;
0110 
0111     PresenceStatus mStatus = PresenceStatus::PresenceOffline;
0112     QString mUserId;
0113     QString mName;
0114     QString mUserName;
0115     QString mStatusText;
0116     QString mBio;
0117     QStringList mRoles;
0118     QStringList mI18nRoles;
0119     double mUtcOffset = 0.0;
0120     bool mActive = true;
0121     bool mRequirePasswordChange = false;
0122 };
0123 Q_DECLARE_METATYPE(User)
0124 Q_DECLARE_TYPEINFO(User, Q_MOVABLE_TYPE);
0125 LIBRUQOLACORE_EXPORT QDebug operator<<(QDebug d, const User &t);
0126 LIBRUQOLACORE_EXPORT QDebug operator<<(QDebug d, const User::UserEmailsInfo &t);