File indexing completed on 2025-01-12 04:33:55

0001 /*
0002    SPDX-FileCopyrightText: 2018-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "ignoreuserjob.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 IgnoreUserJob::IgnoreUserJob(QObject *parent)
0016     : RestApiAbstractJob(parent)
0017 {
0018 }
0019 
0020 IgnoreUserJob::~IgnoreUserJob() = default;
0021 
0022 bool IgnoreUserJob::requireHttpAuthentication() const
0023 {
0024     return true;
0025 }
0026 
0027 bool IgnoreUserJob::start()
0028 {
0029     if (!canStart()) {
0030         qCWarning(ROCKETCHATQTRESTAPI_LOG) << "Impossible to start ignore user job";
0031         deleteLater();
0032         return false;
0033     }
0034     submitGetRequest();
0035 
0036     addStartRestApiInfo(QByteArrayLiteral("IgnoreUserJob: ignore user starting"));
0037     return true;
0038 }
0039 
0040 void IgnoreUserJob::onGetRequestResponse(const QString &replyErrorString, const QJsonDocument &replyJson)
0041 {
0042     const QJsonObject replyObject = replyJson.object();
0043     if (replyObject[QLatin1String("success")].toBool()) {
0044         addLoggerInfo(QByteArrayLiteral("IgnoreUserJob: success: ") + replyJson.toJson(QJsonDocument::Indented));
0045         Q_EMIT ignoreUserDone(replyObject, mRoomId);
0046     } else {
0047         emitFailedMessage(replyErrorString, replyObject);
0048         addLoggerWarning(QByteArrayLiteral("Problem when we tried to ignore user message: ") + replyJson.toJson(QJsonDocument::Indented));
0049     }
0050 }
0051 
0052 bool IgnoreUserJob::ignore() const
0053 {
0054     return mIgnore;
0055 }
0056 
0057 void IgnoreUserJob::setIgnore(bool ignore)
0058 {
0059     mIgnore = ignore;
0060 }
0061 
0062 QString IgnoreUserJob::ignoreUserId() const
0063 {
0064     return mIgnoreUserId;
0065 }
0066 
0067 void IgnoreUserJob::setIgnoreUserId(const QString &ignoreUserId)
0068 {
0069     mIgnoreUserId = ignoreUserId;
0070 }
0071 
0072 QString IgnoreUserJob::roomId() const
0073 {
0074     return mRoomId;
0075 }
0076 
0077 void IgnoreUserJob::setRoomId(const QString &roomId)
0078 {
0079     mRoomId = roomId;
0080 }
0081 
0082 QNetworkRequest IgnoreUserJob::request() const
0083 {
0084     QUrl url = mRestApiMethod->generateUrl(RestApiUtil::RestApiUrlType::ChatIgnoreUser);
0085     QUrlQuery queryUrl;
0086     queryUrl.addQueryItem(QStringLiteral("rid"), mRoomId);
0087     queryUrl.addQueryItem(QStringLiteral("userId"), mIgnoreUserId);
0088     queryUrl.addQueryItem(QStringLiteral("ignore"), mIgnore ? QStringLiteral("true") : QStringLiteral("false"));
0089     url.setQuery(queryUrl);
0090     QNetworkRequest request(url);
0091     addAuthRawHeader(request);
0092     addRequestAttribute(request, false);
0093 
0094     return request;
0095 }
0096 
0097 bool IgnoreUserJob::canStart() const
0098 {
0099     if (!RestApiAbstractJob::canStart()) {
0100         return false;
0101     }
0102     if (mRoomId.isEmpty()) {
0103         qCWarning(ROCKETCHATQTRESTAPI_LOG) << "RoomId is empty";
0104         return false;
0105     }
0106     if (mIgnoreUserId.isEmpty()) {
0107         qCWarning(ROCKETCHATQTRESTAPI_LOG) << "ignore user id is empty";
0108         return false;
0109     }
0110     return true;
0111 }
0112 
0113 #include "moc_ignoreuserjob.cpp"