File indexing completed on 2025-01-12 04:33:54
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 "getthreadmessagesjob.h" 0008 #include "restapimethod.h" 0009 #include "rocketchatqtrestapi_debug.h" 0010 #include <KLocalizedString> 0011 #include <QJsonDocument> 0012 #include <QJsonObject> 0013 #include <QNetworkReply> 0014 #include <QUrlQuery> 0015 using namespace RocketChatRestApi; 0016 GetThreadMessagesJob::GetThreadMessagesJob(QObject *parent) 0017 : RestApiAbstractJob(parent) 0018 { 0019 } 0020 0021 GetThreadMessagesJob::~GetThreadMessagesJob() = default; 0022 0023 bool GetThreadMessagesJob::requireHttpAuthentication() const 0024 { 0025 return true; 0026 } 0027 0028 bool GetThreadMessagesJob::canStart() const 0029 { 0030 if (!RestApiAbstractJob::canStart()) { 0031 return false; 0032 } 0033 if (mThreadMessageId.isEmpty()) { 0034 qCWarning(ROCKETCHATQTRESTAPI_LOG) << "GetThreadMessagesJob: mThreadMessageId is empty"; 0035 return false; 0036 } 0037 return true; 0038 } 0039 0040 bool GetThreadMessagesJob::start() 0041 { 0042 if (!canStart()) { 0043 qCWarning(ROCKETCHATQTRESTAPI_LOG) << "Impossible to start GetThreadMessagesJob job"; 0044 deleteLater(); 0045 return false; 0046 } 0047 submitGetRequest(); 0048 0049 addStartRestApiInfo(QByteArrayLiteral("GetThreadMessagesJob: Ask threads messages")); 0050 return true; 0051 } 0052 0053 void GetThreadMessagesJob::onGetRequestResponse(const QString &replyErrorString, const QJsonDocument &replyJson) 0054 { 0055 const QJsonObject replyObject = replyJson.object(); 0056 if (replyObject[QLatin1String("success")].toBool()) { 0057 addLoggerInfo(QByteArrayLiteral("GetThreadMessagesJob: success: ") + replyJson.toJson(QJsonDocument::Indented)); 0058 Q_EMIT getThreadMessagesDone(replyObject, mThreadMessageId); 0059 } else { 0060 emitFailedMessage(replyErrorString, replyObject); 0061 addLoggerWarning(QByteArrayLiteral("GetThreadMessagesJob: Problem: ") + replyJson.toJson(QJsonDocument::Indented)); 0062 } 0063 } 0064 0065 QString GetThreadMessagesJob::threadMessageId() const 0066 { 0067 return mThreadMessageId; 0068 } 0069 0070 void GetThreadMessagesJob::setThreadMessageId(const QString &threadMessageId) 0071 { 0072 mThreadMessageId = threadMessageId; 0073 } 0074 0075 QNetworkRequest GetThreadMessagesJob::request() const 0076 { 0077 QUrl url = mRestApiMethod->generateUrl(RestApiUtil::RestApiUrlType::ChatGetThreadMessages); 0078 QUrlQuery queryUrl; 0079 queryUrl.addQueryItem(QStringLiteral("tmid"), mThreadMessageId); 0080 url.setQuery(queryUrl); 0081 0082 QNetworkRequest request(url); 0083 addRequestAttribute(request, false); 0084 0085 addAuthRawHeader(request); 0086 return request; 0087 } 0088 0089 QString GetThreadMessagesJob::errorMessage(const QString &str, const QJsonObject &details) 0090 { 0091 if (str == QLatin1String("error-invalid-message")) { 0092 return i18n("Invalid message: \'%1\'", mThreadMessageId); 0093 } 0094 return RestApiAbstractJob::errorMessage(str, details); 0095 } 0096 0097 #include "moc_getthreadmessagesjob.cpp"