File indexing completed on 2024-10-13 07:29:34

0001 // SPDX-FileCopyrightText: 2023 Tobias Fella <tobias.fella@kde.org>
0002 // SPDX-License-Identifier: GPL-2.0-or-later
0003 
0004 #pragma once
0005 
0006 #include <QJsonObject>
0007 #include <QList>
0008 #include <QObject>
0009 #include <QPointer>
0010 #include <QQmlEngine>
0011 #include <QString>
0012 #include <QVariantMap>
0013 
0014 #include <Quotient/csapi/registration.h>
0015 
0016 #include <Quotient/connection.h>
0017 
0018 #include <Quotient/jobs/basejob.h>
0019 #include <Quotient/util.h>
0020 
0021 namespace Quotient
0022 {
0023 class Connection;
0024 class CheckUsernameAvailabilityJob;
0025 }
0026 
0027 class NeoChatRegisterJob : public Quotient::BaseJob
0028 {
0029 public:
0030     explicit NeoChatRegisterJob(const QString &kind = QStringLiteral("user"),
0031                                 const Quotient::Omittable<QJsonObject> &auth = Quotient::none,
0032                                 const QString &username = {},
0033                                 const QString &password = {},
0034                                 const QString &deviceId = {},
0035                                 const QString &initialDeviceDisplayName = {},
0036                                 Quotient::Omittable<bool> inhibitLogin = Quotient::none);
0037 
0038     QString userId() const
0039     {
0040         return loadFromJson<QString>(QStringLiteral("user_id"));
0041     }
0042 
0043     QString accessToken() const
0044     {
0045         return loadFromJson<QString>(QStringLiteral("access_token"));
0046     }
0047 
0048     QString homeServer() const
0049     {
0050         return loadFromJson<QString>(QStringLiteral("home_server"));
0051     }
0052 
0053     QString deviceId() const
0054     {
0055         return loadFromJson<QString>(QStringLiteral("device_id"));
0056     }
0057 };
0058 
0059 class Registration : public QObject
0060 {
0061     Q_OBJECT
0062     QML_ELEMENT
0063     QML_SINGLETON
0064 
0065     Q_PROPERTY(QString homeserver READ homeserver WRITE setHomeserver NOTIFY homeserverChanged)
0066     Q_PROPERTY(QString username READ username WRITE setUsername NOTIFY usernameChanged)
0067     Q_PROPERTY(QString recaptchaSiteKey READ recaptchaSiteKey WRITE setRecaptchaSiteKey NOTIFY recaptchaSiteKeyChanged)
0068     Q_PROPERTY(QString recaptchaResponse READ recaptchaResponse WRITE setRecaptchaResponse NOTIFY recaptchaResponseChanged)
0069     Q_PROPERTY(QString password READ password WRITE setPassword NOTIFY passwordChanged)
0070     Q_PROPERTY(QString email READ email WRITE setEmail NOTIFY emailChanged)
0071     Q_PROPERTY(QString nextStep READ nextStep WRITE setNextStep NOTIFY nextStepChanged)
0072     Q_PROPERTY(QList<QVariantMap> terms READ terms NOTIFY termsChanged)
0073     Q_PROPERTY(Status status READ status NOTIFY statusChanged)
0074     Q_PROPERTY(QString statusString READ statusString NOTIFY statusChanged)
0075 
0076 public:
0077     enum Status {
0078         NoServer,
0079         TestingHomeserver,
0080         InvalidServer,
0081         ServerNoRegistration,
0082         NoUsername,
0083         TestingUsername,
0084         UsernameTaken,
0085         Ready,
0086         Working,
0087     };
0088     Q_ENUM(Status);
0089     static Registration &instance()
0090     {
0091         static Registration _instance;
0092         return _instance;
0093     }
0094     static Registration *create(QQmlEngine *engine, QJSEngine *)
0095     {
0096         engine->setObjectOwnership(&instance(), QQmlEngine::CppOwnership);
0097         return &instance();
0098     }
0099 
0100     Q_INVOKABLE void registerAccount();
0101     Q_INVOKABLE void registerEmail();
0102 
0103     void setRecaptchaSiteKey(const QString &recaptchaSiteKey);
0104     QString recaptchaSiteKey() const;
0105 
0106     void setRecaptchaResponse(const QString &response);
0107     QString recaptchaResponse() const;
0108 
0109     void setHomeserver(const QString &url);
0110     QString homeserver() const;
0111 
0112     QString username() const;
0113     void setUsername(const QString &username);
0114 
0115     QString password() const;
0116     void setPassword(const QString &password);
0117 
0118     [[nodiscard]] QString email() const;
0119     void setEmail(const QString &email);
0120 
0121     QString nextStep() const;
0122     void setNextStep(const QString &nextStep);
0123 
0124     QList<QVariantMap> terms() const;
0125 
0126     Status status() const;
0127     QString statusString() const;
0128 
0129 Q_SIGNALS:
0130     void recaptchaSiteKeyChanged();
0131     void recaptchaResponseChanged();
0132     void homeserverChanged();
0133     void homeserverAvailableChanged();
0134     void testingChanged();
0135     void usernameChanged();
0136     void usernameAvailableChanged();
0137     void testingUsernameChanged();
0138     void flowsChanged();
0139     void termsChanged();
0140     void passwordChanged();
0141     void emailChanged();
0142     void nextStepChanged();
0143     void statusChanged();
0144 
0145 private:
0146     QString m_recaptchaSiteKey;
0147     QString m_recaptchaResponse;
0148     QString m_homeserver;
0149     QString m_username;
0150     QString m_password;
0151     QList<QVariantMap> m_terms;
0152     QString m_email;
0153     Status m_status = NoServer;
0154     QString m_nextStep;
0155     QString m_session;
0156     QString m_sid;
0157     QString m_emailSecret;
0158 
0159     QPointer<Quotient::CheckUsernameAvailabilityJob> m_usernameJob;
0160     QPointer<NeoChatRegisterJob> m_testServerJob;
0161     QList<QList<QString>> m_flows;
0162     QPointer<Quotient::Connection> m_connection;
0163 
0164     void testHomeserver();
0165     void testUsername();
0166     void setStatus(Status status);
0167 
0168     Registration();
0169 };