1 /*
2    SPDX-FileCopyrightText: 2020-2021 Laurent Montel <montel@kde.org>
3 
4    SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "userbasejob.h"
8 #include "rocketchatqtrestapi_debug.h"
9 #include <QJsonObject>
10 #include <QUrlQuery>
11 using namespace RocketChatRestApi;
12 
UserBaseJob(QObject * parent)13 UserBaseJob::UserBaseJob(QObject *parent)
14     : RestApiAbstractJob(parent)
15 {
16 }
17 
18 UserBaseJob::~UserBaseJob() = default;
19 
userInfo() const20 UserBaseJob::UserInfo UserBaseJob::userInfo() const
21 {
22     return mUserInfo;
23 }
24 
setUserInfo(const UserInfo & userInfo)25 void UserBaseJob::setUserInfo(const UserInfo &userInfo)
26 {
27     mUserInfo = userInfo;
28 }
29 
hasUserIdentifier() const30 bool UserBaseJob::hasUserIdentifier() const
31 {
32     return !mUserInfo.userIdentifier.isEmpty() && (mUserInfo.userInfoType != UserBaseJob::UserInfoType::Unknown);
33 }
34 
addQueryUrl(QUrl & url) const35 void UserBaseJob::addQueryUrl(QUrl &url) const
36 {
37     QUrlQuery queryUrl;
38     if (mUserInfo.userInfoType == UserBaseJob::UserInfoType::UserName) {
39         queryUrl.addQueryItem(QStringLiteral("username"), mUserInfo.userIdentifier);
40     } else {
41         queryUrl.addQueryItem(QStringLiteral("userId"), mUserInfo.userIdentifier);
42     }
43     url.setQuery(queryUrl);
44 }
45 
identifier() const46 QString UserBaseJob::identifier() const
47 {
48     switch (mUserInfo.userInfoType) {
49     case UserBaseJob::UserInfoType::UserName:
50     case UserBaseJob::UserInfoType::UserId:
51         return mUserInfo.userIdentifier;
52     case UserBaseJob::UserInfoType::Unknown:
53         return {};
54     }
55     return {};
56 }
57 
generateJson(QJsonObject & jsonObj) const58 void UserBaseJob::generateJson(QJsonObject &jsonObj) const
59 {
60     switch (mUserInfo.userInfoType) {
61     case UserBaseJob::UserInfoType::UserName:
62         jsonObj[QStringLiteral("username")] = mUserInfo.userIdentifier;
63         break;
64     case UserBaseJob::UserInfoType::UserId:
65         jsonObj[QStringLiteral("userId")] = mUserInfo.userIdentifier;
66         break;
67     case UserBaseJob::UserInfoType::Unknown:
68         qCWarning(ROCKETCHATQTRESTAPI_LOG) << "UserBaseJob::UserInfoType::Unknown is a bug !";
69         break;
70     }
71 }
72