File indexing completed on 2024-12-22 04:45:14
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 "channelhistoryjob.h" 0008 0009 #include "restapimethod.h" 0010 #include "rocketchatqtrestapi_debug.h" 0011 #include <QJsonDocument> 0012 #include <QJsonObject> 0013 #include <QNetworkReply> 0014 #include <QUrlQuery> 0015 using namespace RocketChatRestApi; 0016 ChannelHistoryJob::ChannelHistoryJob(QObject *parent) 0017 : ChannelGroupBaseJob(parent) 0018 { 0019 } 0020 0021 ChannelHistoryJob::~ChannelHistoryJob() = default; 0022 0023 bool ChannelHistoryJob::start() 0024 { 0025 if (!canStart()) { 0026 deleteLater(); 0027 return false; 0028 } 0029 addStartRestApiInfo("ChannelHistoryJob::start"); 0030 submitGetRequest(); 0031 0032 return true; 0033 } 0034 0035 void ChannelHistoryJob::onGetRequestResponse(const QString &replyErrorString, const QJsonDocument &replyJson) 0036 { 0037 const QJsonObject replyObject = replyJson.object(); 0038 0039 if (replyObject[QLatin1String("success")].toBool()) { 0040 addLoggerInfo(QByteArrayLiteral("ChannelHistoryJob success: ") + replyJson.toJson(QJsonDocument::Indented)); 0041 Q_EMIT channelHistoryDone(replyObject, channelGroupInfo()); 0042 } else { 0043 emitFailedMessage(replyErrorString, replyObject); 0044 addLoggerWarning(QByteArrayLiteral("ChannelHistoryJob problem: ") + replyJson.toJson(QJsonDocument::Indented)); 0045 } 0046 } 0047 0048 ChannelHistoryJob::ChannelHistoryInfo ChannelHistoryJob::channelHistoryInfo() const 0049 { 0050 return mChannelHistoryInfo; 0051 } 0052 0053 void ChannelHistoryJob::setChannelHistoryInfo(const ChannelHistoryInfo &channelHistoryInfo) 0054 { 0055 mChannelHistoryInfo = channelHistoryInfo; 0056 } 0057 0058 bool ChannelHistoryJob::requireHttpAuthentication() const 0059 { 0060 return true; 0061 } 0062 0063 bool ChannelHistoryJob::canStart() const 0064 { 0065 // if (!hasIdentifier()) { 0066 // qCWarning(ROCKETCHATQTRESTAPI_LOG) << "ChannelHistoryJob: RoomId and RoomName are empty"; 0067 // return false; 0068 // } 0069 if (mChannelHistoryInfo.channelType == ChannelHistoryJob::Unknown) { 0070 qCWarning(ROCKETCHATQTRESTAPI_LOG) << "ChannelHistoryJob: Channel type is unknown."; 0071 return false; 0072 } 0073 if (mChannelHistoryInfo.roomId.isEmpty()) { 0074 qCWarning(ROCKETCHATQTRESTAPI_LOG) << "ChannelHistoryJob: roomId is empty"; 0075 return false; 0076 } 0077 if (!RestApiAbstractJob::canStart()) { 0078 return false; 0079 } 0080 return true; 0081 } 0082 0083 QNetworkRequest ChannelHistoryJob::request() const 0084 { 0085 QUrl url; 0086 switch (mChannelHistoryInfo.channelType) { 0087 case Channel: 0088 url = mRestApiMethod->generateUrl(RestApiUtil::RestApiUrlType::ChannelsHistory); 0089 break; 0090 case Groups: 0091 url = mRestApiMethod->generateUrl(RestApiUtil::RestApiUrlType::GroupsHistory); 0092 break; 0093 case Direct: 0094 url = mRestApiMethod->generateUrl(RestApiUtil::RestApiUrlType::ImHistory); 0095 break; 0096 case Unknown: 0097 qCWarning(ROCKETCHATQTRESTAPI_LOG) << "ChannelHistoryJob: Type is not defined"; 0098 break; 0099 } 0100 0101 QUrlQuery queryUrl; 0102 queryUrl.addQueryItem(QStringLiteral("roomId"), mChannelHistoryInfo.roomId); 0103 if (!mChannelHistoryInfo.latestMessage.isEmpty()) { 0104 queryUrl.addQueryItem(QStringLiteral("latest"), mChannelHistoryInfo.latestMessage); 0105 } 0106 if (!mChannelHistoryInfo.oldestMessage.isEmpty()) { 0107 queryUrl.addQueryItem(QStringLiteral("oldest"), mChannelHistoryInfo.oldestMessage); 0108 } 0109 if (mChannelHistoryInfo.offset > 0) { 0110 queryUrl.addQueryItem(QStringLiteral("offset"), QString::number(mChannelHistoryInfo.offset)); 0111 } 0112 if (mChannelHistoryInfo.count > 0) { 0113 queryUrl.addQueryItem(QStringLiteral("count"), QString::number(mChannelHistoryInfo.count)); 0114 } 0115 queryUrl.addQueryItem(QStringLiteral("inclusive"), mChannelHistoryInfo.inclusive ? QStringLiteral("true") : QStringLiteral("false")); 0116 queryUrl.addQueryItem(QStringLiteral("unreads"), mChannelHistoryInfo.unreads ? QStringLiteral("true") : QStringLiteral("false")); 0117 addQueryParameter(queryUrl); 0118 url.setQuery(queryUrl); 0119 0120 QNetworkRequest request(url); 0121 addAuthRawHeader(request); 0122 addRequestAttribute(request); 0123 return request; 0124 } 0125 0126 QDebug operator<<(QDebug d, const RocketChatRestApi::ChannelHistoryJob::ChannelHistoryInfo &t) 0127 { 0128 d << "latestMessage " << t.latestMessage; 0129 d << "oldestMessage " << t.oldestMessage; 0130 d << "channelType " << t.channelType; 0131 d << "offset " << t.offset; 0132 d << "count " << t.count; 0133 d << "inclusive " << t.inclusive; 0134 d << "unreads " << t.unreads; 0135 d << "roomId " << t.roomId; 0136 return d; 0137 } 0138 0139 #include "moc_channelhistoryjob.cpp"