File indexing completed on 2024-12-22 04:45:19
0001 /* 0002 SPDX-FileCopyrightText: 2019-2024 Laurent Montel <montel@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include "opendmjob.h" 0008 0009 #include "restapimethod.h" 0010 #include "rocketchatqtrestapi_debug.h" 0011 #include <QJsonDocument> 0012 #include <QJsonObject> 0013 #include <QNetworkReply> 0014 using namespace RocketChatRestApi; 0015 OpenDmJob::OpenDmJob(QObject *parent) 0016 : RestApiAbstractJob(parent) 0017 { 0018 } 0019 0020 OpenDmJob::~OpenDmJob() = default; 0021 0022 bool OpenDmJob::start() 0023 { 0024 if (!canStart()) { 0025 deleteLater(); 0026 return false; 0027 } 0028 addStartRestApiInfo("OpenDmJob::start"); 0029 submitPostRequest(json()); 0030 0031 return true; 0032 } 0033 0034 void OpenDmJob::onPostRequestResponse(const QString &replyErrorString, const QJsonDocument &replyJson) 0035 { 0036 const QJsonObject replyObject = replyJson.object(); 0037 0038 if (replyObject[QLatin1String("success")].toBool()) { 0039 addLoggerInfo(QByteArrayLiteral("Create direct message success: ") + replyJson.toJson(QJsonDocument::Indented)); 0040 qDebug() << " replyJson " << replyJson; 0041 Q_EMIT openDmDone(); 0042 } else { 0043 emitFailedMessage(replyErrorString, replyObject); 0044 addLoggerWarning(QByteArrayLiteral("Create direct message Problem: ") + replyJson.toJson(QJsonDocument::Indented)); 0045 } 0046 } 0047 0048 QString OpenDmJob::directUserId() const 0049 { 0050 return mDirectUserId; 0051 } 0052 0053 void OpenDmJob::setDirectUserId(const QString &userId) 0054 { 0055 mDirectUserId = userId; 0056 } 0057 0058 bool OpenDmJob::requireHttpAuthentication() const 0059 { 0060 return true; 0061 } 0062 0063 bool OpenDmJob::canStart() const 0064 { 0065 if (mDirectUserId.isEmpty()) { 0066 qCWarning(ROCKETCHATQTRESTAPI_LOG) << "OpenDmJob: mDirectUserId is empty"; 0067 return false; 0068 } 0069 if (!RestApiAbstractJob::canStart()) { 0070 return false; 0071 } 0072 return true; 0073 } 0074 0075 QJsonDocument OpenDmJob::json() const 0076 { 0077 QJsonObject jsonObj; 0078 jsonObj[QLatin1String("roomId")] = mDirectUserId; 0079 const QJsonDocument postData = QJsonDocument(jsonObj); 0080 return postData; 0081 } 0082 0083 QNetworkRequest OpenDmJob::request() const 0084 { 0085 const QUrl url = mRestApiMethod->generateUrl(RestApiUtil::RestApiUrlType::ImOpen); 0086 QNetworkRequest request(url); 0087 addAuthRawHeader(request); 0088 addRequestAttribute(request); 0089 return request; 0090 } 0091 0092 #include "moc_opendmjob.cpp"