aboutsummaryrefslogtreecommitdiff
path: root/lib/csapi/admin.cpp
blob: 3effbbc3edb40829855d72bc9ca74f11e918eeee (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
/******************************************************************************
 * THIS FILE IS GENERATED - ANY EDITS WILL BE OVERWRITTEN
 */

#include "admin.h"

#include "converters.h"

#include <QtCore/QStringBuilder>

using namespace QMatrixClient;

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

namespace QMatrixClient
{
    // Converters

    template <> struct FromJson<GetWhoIsJob::ConnectionInfo>
    {
        GetWhoIsJob::ConnectionInfo operator()(const QJsonValue& jv)
        {
            const auto& _json = jv.toObject();
            GetWhoIsJob::ConnectionInfo result;
            result.ip =
                fromJson<QString>(_json.value("ip"_ls));
            result.lastSeen =
                fromJson<qint64>(_json.value("last_seen"_ls));
            result.userAgent =
                fromJson<QString>(_json.value("user_agent"_ls));

            return result;
        }
    };

    template <> struct FromJson<GetWhoIsJob::SessionInfo>
    {
        GetWhoIsJob::SessionInfo operator()(const QJsonValue& jv)
        {
            const auto& _json = jv.toObject();
            GetWhoIsJob::SessionInfo result;
            result.connections =
                fromJson<QVector<GetWhoIsJob::ConnectionInfo>>(_json.value("connections"_ls));

            return result;
        }
    };

    template <> struct FromJson<GetWhoIsJob::DeviceInfo>
    {
        GetWhoIsJob::DeviceInfo operator()(const QJsonValue& jv)
        {
            const auto& _json = jv.toObject();
            GetWhoIsJob::DeviceInfo result;
            result.sessions =
                fromJson<QVector<GetWhoIsJob::SessionInfo>>(_json.value("sessions"_ls));

            return result;
        }
    };
} // namespace QMatrixClient

class GetWhoIsJob::Private
{
    public:
        QString userId;
        QHash<QString, DeviceInfo> devices;
};

QUrl GetWhoIsJob::makeRequestUrl(QUrl baseUrl, const QString& userId)
{
    return BaseJob::makeRequestUrl(std::move(baseUrl),
            basePath % "/admin/whois/" % userId);
}

static const auto GetWhoIsJobName = QStringLiteral("GetWhoIsJob");

GetWhoIsJob::GetWhoIsJob(const QString& userId)
    : BaseJob(HttpVerb::Get, GetWhoIsJobName,
        basePath % "/admin/whois/" % userId)
    , d(new Private)
{
}

GetWhoIsJob::~GetWhoIsJob() = default;

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

const QHash<QString, GetWhoIsJob::DeviceInfo>& GetWhoIsJob::devices() const
{
    return d->devices;
}

BaseJob::Status GetWhoIsJob::parseJson(const QJsonDocument& data)
{
    auto json = data.object();
    d->userId = fromJson<QString>(json.value("user_id"_ls));
    d->devices = fromJson<QHash<QString, DeviceInfo>>(json.value("devices"_ls));
    return Success;
}