File indexing completed on 2024-12-22 04:45:26
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 "markroomasunreadjob.h" 0008 #include "restapimethod.h" 0009 #include "rocketchatqtrestapi_debug.h" 0010 0011 #include <KLocalizedString> 0012 #include <QJsonDocument> 0013 #include <QJsonObject> 0014 #include <QNetworkReply> 0015 using namespace RocketChatRestApi; 0016 MarkRoomAsUnReadJob::MarkRoomAsUnReadJob(QObject *parent) 0017 : RestApiAbstractJob(parent) 0018 { 0019 } 0020 0021 MarkRoomAsUnReadJob::~MarkRoomAsUnReadJob() = default; 0022 0023 bool MarkRoomAsUnReadJob::start() 0024 { 0025 if (!canStart()) { 0026 deleteLater(); 0027 return false; 0028 } 0029 addStartRestApiInfo("MarkRoomAsUnReadJob::start"); 0030 submitPostRequest(json()); 0031 0032 return true; 0033 } 0034 0035 void MarkRoomAsUnReadJob::onPostRequestResponse(const QString &replyErrorString, const QJsonDocument &replyJson) 0036 { 0037 const QJsonObject replyObject = replyJson.object(); 0038 0039 if (replyObject[QLatin1String("success")].toBool()) { 0040 addLoggerInfo(QByteArrayLiteral("MarkRoomAsUnReadJob: success: ") + replyJson.toJson(QJsonDocument::Indented)); 0041 Q_EMIT markAsUnReadDone(); 0042 } else { 0043 emitFailedMessage(replyErrorString, replyObject); 0044 addLoggerWarning(QByteArrayLiteral("MarkRoomAsUnReadJob: Problem: ") + replyJson.toJson(QJsonDocument::Indented)); 0045 } 0046 } 0047 0048 QString MarkRoomAsUnReadJob::objectId() const 0049 { 0050 return mObjectId; 0051 } 0052 0053 void MarkRoomAsUnReadJob::setObjectId(const QString &objectId) 0054 { 0055 mObjectId = objectId; 0056 } 0057 0058 QString MarkRoomAsUnReadJob::errorMessage(const QString &str, const QJsonObject &detail) 0059 { 0060 if (str == QLatin1String("error-action-not-allowed")) { 0061 return i18n("Unread message not authorized."); 0062 } else if (str == QLatin1String("error-no-message-for-unread")) { 0063 return i18n("There are no messages to mark unread."); 0064 } 0065 return RestApiAbstractJob::errorMessage(str, detail); 0066 } 0067 0068 MarkRoomAsUnReadJob::MarkAsUnReadObject MarkRoomAsUnReadJob::unReadObject() const 0069 { 0070 return mUnReadObject; 0071 } 0072 0073 void MarkRoomAsUnReadJob::setUnReadObject(MarkAsUnReadObject type) 0074 { 0075 mUnReadObject = type; 0076 } 0077 0078 bool MarkRoomAsUnReadJob::requireHttpAuthentication() const 0079 { 0080 return true; 0081 } 0082 0083 bool MarkRoomAsUnReadJob::canStart() const 0084 { 0085 if (!RestApiAbstractJob::canStart()) { 0086 return false; 0087 } 0088 if (mUnReadObject == Unknown) { 0089 qCWarning(ROCKETCHATQTRESTAPI_LOG) << "MarkRoomAsUnReadJob: object type undefined"; 0090 return false; 0091 } 0092 0093 if (mObjectId.isEmpty()) { 0094 qCWarning(ROCKETCHATQTRESTAPI_LOG) << "MarkRoomAsUnReadJob: mRoomId is empty"; 0095 return false; 0096 } 0097 return true; 0098 } 0099 0100 QNetworkRequest MarkRoomAsUnReadJob::request() const 0101 { 0102 const QUrl url = mRestApiMethod->generateUrl(RestApiUtil::RestApiUrlType::SubscriptionsUnRead); 0103 QNetworkRequest request(url); 0104 addAuthRawHeader(request); 0105 addRequestAttribute(request); 0106 return request; 0107 } 0108 0109 QJsonDocument MarkRoomAsUnReadJob::json() const 0110 { 0111 QJsonObject jsonObj; 0112 if (mUnReadObject == Room) { 0113 jsonObj[QLatin1String("roomId")] = mObjectId; 0114 } else if (mUnReadObject == FromMessage) { 0115 QJsonObject fromMessage; 0116 fromMessage[QLatin1String("_id")] = mObjectId; 0117 jsonObj[QLatin1String("firstUnreadMessage")] = fromMessage; 0118 } 0119 0120 const QJsonDocument postData = QJsonDocument(jsonObj); 0121 return postData; 0122 } 0123 0124 #include "moc_markroomasunreadjob.cpp"