File indexing completed on 2024-12-22 04:45:22
0001 /* 0002 SPDX-FileCopyrightText: 2020-2024 Laurent Montel <montel@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include "findorcreateinvitejob.h" 0008 #include "restapimethod.h" 0009 #include "rocketchatqtrestapi_debug.h" 0010 0011 #include <KLocalizedString> 0012 0013 #include <QJsonDocument> 0014 #include <QJsonObject> 0015 #include <QNetworkReply> 0016 using namespace RocketChatRestApi; 0017 FindOrCreateInviteJob::FindOrCreateInviteJob(QObject *parent) 0018 : RestApiAbstractJob(parent) 0019 { 0020 } 0021 0022 FindOrCreateInviteJob::~FindOrCreateInviteJob() = default; 0023 0024 bool FindOrCreateInviteJob::start() 0025 { 0026 if (!canStart()) { 0027 deleteLater(); 0028 return false; 0029 } 0030 addStartRestApiInfo("FindOrCreateInviteJob::start"); 0031 submitPostRequest(json()); 0032 0033 return true; 0034 } 0035 0036 void FindOrCreateInviteJob::onPostRequestResponse(const QString &replyErrorString, const QJsonDocument &replyJson) 0037 { 0038 const QJsonObject replyObject = replyJson.object(); 0039 0040 if (replyObject[QLatin1String("success")].toBool()) { 0041 addLoggerInfo(QByteArrayLiteral("FindOrCreateInviteJob success: ") + replyJson.toJson(QJsonDocument::Indented)); 0042 Q_EMIT findOrCreateInviteDone(parseResult(replyObject)); 0043 } else { 0044 emitFailedMessage(replyErrorString, replyObject); 0045 addLoggerWarning(QByteArrayLiteral("FindOrCreateInviteJob: Problem: ") + replyJson.toJson(QJsonDocument::Indented)); 0046 } 0047 } 0048 0049 FindOrCreateInviteJob::InviteUsersInfo FindOrCreateInviteJob::parseResult(const QJsonObject &replyObject) 0050 { 0051 FindOrCreateInviteJob::InviteUsersInfo info; 0052 info.url = QUrl(replyObject[QLatin1String("url")].toString()); 0053 info.userId = replyObject[QLatin1String("userId")].toString(); 0054 info.roomId = replyObject[QLatin1String("rid")].toString(); 0055 info.expireDateTime = QDateTime::fromString(replyObject[QLatin1String("expires")].toString(), Qt::ISODate).toString(); 0056 info.maxUses = replyObject[QLatin1String("maxUses")].toInt(); 0057 return info; 0058 } 0059 0060 int FindOrCreateInviteJob::numberOfDays() const 0061 { 0062 return mNumberOfDays; 0063 } 0064 0065 void FindOrCreateInviteJob::setNumberOfDays(int numberOfDays) 0066 { 0067 mNumberOfDays = numberOfDays; 0068 } 0069 0070 int FindOrCreateInviteJob::maxUses() const 0071 { 0072 return mMaxUses; 0073 } 0074 0075 void FindOrCreateInviteJob::setMaxUses(int maxUses) 0076 { 0077 mMaxUses = maxUses; 0078 } 0079 0080 QString FindOrCreateInviteJob::roomId() const 0081 { 0082 return mRoomId; 0083 } 0084 0085 void FindOrCreateInviteJob::setRoomId(const QString &roomId) 0086 { 0087 mRoomId = roomId; 0088 } 0089 0090 bool FindOrCreateInviteJob::requireHttpAuthentication() const 0091 { 0092 return true; 0093 } 0094 0095 bool FindOrCreateInviteJob::canStart() const 0096 { 0097 if (!RestApiAbstractJob::canStart()) { 0098 return false; 0099 } 0100 if (mRoomId.isEmpty()) { 0101 qCWarning(ROCKETCHATQTRESTAPI_LOG) << "RoomId is Empty. Impossible to start FindOrCreateInviteJob"; 0102 return false; 0103 } 0104 if (mMaxUses < 0) { 0105 qCWarning(ROCKETCHATQTRESTAPI_LOG) << "MaxUses is negative it's a bug. Impossible to start FindOrCreateInviteJob"; 0106 return false; 0107 } 0108 if (mNumberOfDays < 0) { 0109 qCWarning(ROCKETCHATQTRESTAPI_LOG) << "Number of days is negative. Impossible to start FindOrCreateInviteJob"; 0110 return false; 0111 } 0112 return true; 0113 } 0114 0115 QNetworkRequest FindOrCreateInviteJob::request() const 0116 { 0117 const QUrl url = mRestApiMethod->generateUrl(RestApiUtil::RestApiUrlType::FindOrCreateInvite); 0118 QNetworkRequest request(url); 0119 addAuthRawHeader(request); 0120 addRequestAttribute(request); 0121 return request; 0122 } 0123 0124 QJsonDocument FindOrCreateInviteJob::json() const 0125 { 0126 QJsonObject jsonObj; 0127 jsonObj[QLatin1String("rid")] = mRoomId; 0128 jsonObj[QLatin1String("days")] = mNumberOfDays; 0129 jsonObj[QLatin1String("maxUses")] = mMaxUses; 0130 const QJsonDocument postData = QJsonDocument(jsonObj); 0131 return postData; 0132 } 0133 0134 QString FindOrCreateInviteJob::errorMessage(const QString &str, const QJsonObject &details) 0135 { 0136 if (str == QLatin1String("not_authorized")) { 0137 return i18n("Generate link is not authorized in this channel."); 0138 } 0139 return RestApiAbstractJob::errorMessage(str, details); 0140 } 0141 0142 QDebug operator<<(QDebug d, const FindOrCreateInviteJob::InviteUsersInfo &t) 0143 { 0144 d << " url: " << t.url; 0145 d << " roomId: " << t.roomId; 0146 d << " userId: " << t.userId; 0147 d << " expireDateTime: " << t.expireDateTime; 0148 d << " maxUses: " << t.maxUses; 0149 return d; 0150 } 0151 0152 bool FindOrCreateInviteJob::InviteUsersInfo::operator==(const FindOrCreateInviteJob::InviteUsersInfo &other) const 0153 { 0154 return other.url == url && other.roomId == roomId && other.userId == userId && other.expireDateTime == expireDateTime && other.maxUses == maxUses; 0155 } 0156 0157 #include "moc_findorcreateinvitejob.cpp"