File indexing completed on 2024-12-22 04:45:23

0001 /*
0002    SPDX-FileCopyrightText: 2023-2024 Laurent Montel <montel.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "moderationuserreportedmessagesjob.h"
0008 #include "restapimethod.h"
0009 #include "rocketchatqtrestapi_debug.h"
0010 #include <QJsonDocument>
0011 #include <QJsonObject>
0012 #include <QNetworkReply>
0013 #include <QUrlQuery>
0014 using namespace RocketChatRestApi;
0015 ModerationUserReportedMessagesJob::ModerationUserReportedMessagesJob(QObject *parent)
0016     : RestApiAbstractJob(parent)
0017 {
0018 }
0019 
0020 ModerationUserReportedMessagesJob::~ModerationUserReportedMessagesJob() = default;
0021 
0022 bool ModerationUserReportedMessagesJob::requireHttpAuthentication() const
0023 {
0024     return true;
0025 }
0026 
0027 bool ModerationUserReportedMessagesJob::start()
0028 {
0029     if (!canStart()) {
0030         qCWarning(ROCKETCHATQTRESTAPI_LOG) << "Impossible to start get get user reported message job";
0031         deleteLater();
0032         return false;
0033     }
0034     submitGetRequest();
0035 
0036     addStartRestApiInfo(QByteArrayLiteral("ModerationReportJob: get get user reported message starting"));
0037     return true;
0038 }
0039 
0040 void ModerationUserReportedMessagesJob::onGetRequestResponse(const QString &replyErrorString, const QJsonDocument &replyJson)
0041 {
0042     const QJsonObject replyObject = replyJson.object();
0043     if (replyObject[QLatin1String("success")].toBool()) {
0044         addLoggerInfo(QByteArrayLiteral("ModerationReportJob: success: ") + replyJson.toJson(QJsonDocument::Indented));
0045         Q_EMIT moderationUserReportedMessagesDone(replyObject);
0046     } else {
0047         emitFailedMessage(replyErrorString, replyObject);
0048         addLoggerWarning(QByteArrayLiteral("ModerationReportJob: Problem when we tried to get user reported messages : ")
0049                          + replyJson.toJson(QJsonDocument::Indented));
0050     }
0051 }
0052 
0053 QString ModerationUserReportedMessagesJob::reportedMessageFromUserId() const
0054 {
0055     return mReportedMessageFromUserId;
0056 }
0057 
0058 void ModerationUserReportedMessagesJob::setReportedMessageFromUserId(const QString &newReportedMessageFromUserId)
0059 {
0060     mReportedMessageFromUserId = newReportedMessageFromUserId;
0061 }
0062 
0063 QNetworkRequest ModerationUserReportedMessagesJob::request() const
0064 {
0065     QUrl url = mRestApiMethod->generateUrl(RestApiUtil::RestApiUrlType::ModerationUserReportedMessages);
0066     QUrlQuery queryUrl;
0067     queryUrl.addQueryItem(QStringLiteral("userId"), mReportedMessageFromUserId);
0068     url.setQuery(queryUrl);
0069     QNetworkRequest request(url);
0070     addAuthRawHeader(request);
0071     addRequestAttribute(request, false);
0072 
0073     return request;
0074 }
0075 
0076 bool ModerationUserReportedMessagesJob::canStart() const
0077 {
0078     if (!RestApiAbstractJob::canStart()) {
0079         return false;
0080     }
0081     if (mReportedMessageFromUserId.isEmpty()) {
0082         qCWarning(ROCKETCHATQTRESTAPI_LOG) << "MessageId is empty";
0083         return false;
0084     }
0085     return true;
0086 }
0087 
0088 #include "moc_moderationuserreportedmessagesjob.cpp"