aboutsummaryrefslogtreecommitdiff
path: root/lib/csapi/registration.cpp
blob: 1e239d2a9e9a1af63e1012f4b0bcda9ff7fbc830 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/******************************************************************************
 * THIS FILE IS GENERATED - ANY EDITS WILL BE OVERWRITTEN
 */

#include "registration.h"

#include "converters.h"

#include <QtCore/QStringBuilder>

using namespace QMatrixClient;

static const auto basePath = QStringLiteral("/_matrix/client/r0");

class RegisterJob::Private
{
    public:
        QString userId;
        QString accessToken;
        QString homeServer;
        QString deviceId;
};

BaseJob::Query queryToRegister(const QString& kind)
{
    BaseJob::Query _q;
    if (!kind.isEmpty())
        _q.addQueryItem("kind", kind);
    return _q;
}

RegisterJob::RegisterJob(const QString& kind, const QJsonObject& auth, bool bindEmail, const QString& username, const QString& password, const QString& deviceId, const QString& initialDeviceDisplayName)
    : BaseJob(HttpVerb::Post, "RegisterJob",
        basePath % "/register",
        queryToRegister(kind),
        {}, false)
    , d(new Private)
{
    QJsonObject _data;
    addToJson<IfNotEmpty>(_data, "auth", auth);
    addToJson<IfNotEmpty>(_data, "bind_email", bindEmail);
    addToJson<IfNotEmpty>(_data, "username", username);
    addToJson<IfNotEmpty>(_data, "password", password);
    addToJson<IfNotEmpty>(_data, "device_id", deviceId);
    addToJson<IfNotEmpty>(_data, "initial_device_display_name", initialDeviceDisplayName);
    setRequestData(_data);
}

RegisterJob::~RegisterJob() = default;

const QString& RegisterJob::userId() const
{
    return d->userId;
}

const QString& RegisterJob::accessToken() const
{
    return d->accessToken;
}

const QString& RegisterJob::homeServer() const
{
    return d->homeServer;
}

const QString& RegisterJob::deviceId() const
{
    return d->deviceId;
}

BaseJob::Status RegisterJob::parseJson(const QJsonDocument& data)
{
    auto json = data.object();
    d->userId = fromJson<QString>(json.value("user_id"));
    d->accessToken = fromJson<QString>(json.value("access_token"));
    d->homeServer = fromJson<QString>(json.value("home_server"));
    d->deviceId = fromJson<QString>(json.value("device_id"));
    return Success;
}

RequestTokenToRegisterJob::RequestTokenToRegisterJob(const QString& clientSecret, const QString& email, int sendAttempt, const QString& idServer)
    : BaseJob(HttpVerb::Post, "RequestTokenToRegisterJob",
        basePath % "/register/email/requestToken", false)
{
    QJsonObject _data;
    addToJson<IfNotEmpty>(_data, "id_server", idServer);
    addToJson<>(_data, "client_secret", clientSecret);
    addToJson<>(_data, "email", email);
    addToJson<>(_data, "send_attempt", sendAttempt);
    setRequestData(_data);
}

ChangePasswordJob::ChangePasswordJob(const QString& newPassword, const QJsonObject& auth)
    : BaseJob(HttpVerb::Post, "ChangePasswordJob",
        basePath % "/account/password")
{
    QJsonObject _data;
    addToJson<>(_data, "new_password", newPassword);
    addToJson<IfNotEmpty>(_data, "auth", auth);
    setRequestData(_data);
}

QUrl RequestTokenToResetPasswordJob::makeRequestUrl(QUrl baseUrl)
{
    return BaseJob::makeRequestUrl(std::move(baseUrl),
            basePath % "/account/password/email/requestToken");
}

RequestTokenToResetPasswordJob::RequestTokenToResetPasswordJob()
    : BaseJob(HttpVerb::Post, "RequestTokenToResetPasswordJob",
        basePath % "/account/password/email/requestToken", false)
{
}

DeactivateAccountJob::DeactivateAccountJob(const QJsonObject& auth)
    : BaseJob(HttpVerb::Post, "DeactivateAccountJob",
        basePath % "/account/deactivate")
{
    QJsonObject _data;
    addToJson<IfNotEmpty>(_data, "auth", auth);
    setRequestData(_data);
}

class CheckUsernameAvailabilityJob::Private
{
    public:
        bool available;
};

BaseJob::Query queryToCheckUsernameAvailability(const QString& username)
{
    BaseJob::Query _q;
    _q.addQueryItem("username", username);
    return _q;
}

QUrl CheckUsernameAvailabilityJob::makeRequestUrl(QUrl baseUrl, const QString& username)
{
    return BaseJob::makeRequestUrl(std::move(baseUrl),
            basePath % "/register/available",
            queryToCheckUsernameAvailability(username));
}

CheckUsernameAvailabilityJob::CheckUsernameAvailabilityJob(const QString& username)
    : BaseJob(HttpVerb::Get, "CheckUsernameAvailabilityJob",
        basePath % "/register/available",
        queryToCheckUsernameAvailability(username),
        {}, false)
    , d(new Private)
{
}

CheckUsernameAvailabilityJob::~CheckUsernameAvailabilityJob() = default;

bool CheckUsernameAvailabilityJob::available() const
{
    return d->available;
}

BaseJob::Status CheckUsernameAvailabilityJob::parseJson(const QJsonDocument& data)
{
    auto json = data.object();
    d->available = fromJson<bool>(json.value("available"));
    return Success;
}