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

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 "channeljoinjob.h"
0008 
0009 #include "restapimethod.h"
0010 #include "rocketchatqtrestapi_debug.h"
0011 #include <KLocalizedString>
0012 #include <QJsonDocument>
0013 #include <QJsonObject>
0014 #include <QNetworkReply>
0015 
0016 using namespace RocketChatRestApi;
0017 ChannelJoinJob::ChannelJoinJob(QObject *parent)
0018     : ChannelGroupBaseJob(parent)
0019 {
0020 }
0021 
0022 ChannelJoinJob::~ChannelJoinJob() = default;
0023 
0024 bool ChannelJoinJob::start()
0025 {
0026     if (!canStart()) {
0027         deleteLater();
0028         return false;
0029     }
0030     addStartRestApiInfo("ChannelJoinJob::start");
0031     submitPostRequest(json());
0032 
0033     return true;
0034 }
0035 
0036 void ChannelJoinJob::onPostRequestResponse(const QString &replyErrorString, const QJsonDocument &replyJson)
0037 {
0038     const QJsonObject replyObject = replyJson.object();
0039 
0040     if (replyObject[QLatin1String("success")].toBool()) {
0041         addLoggerInfo(QByteArrayLiteral("ChannelJoinJob success: ") + replyJson.toJson(QJsonDocument::Indented));
0042         Q_EMIT setChannelJoinDone(channelGroupInfo());
0043     } else {
0044         emitFailedMessage(replyErrorString, replyObject);
0045         addLoggerWarning(QByteArrayLiteral("ChannelJoinJob problem: ") + replyJson.toJson(QJsonDocument::Indented));
0046         // Invalid password
0047         const QString errorType = replyObject[QLatin1String("errorType")].toString();
0048         if (errorType == QLatin1String("error-code-invalid")) {
0049             Q_EMIT missingChannelPassword(channelGroupInfo());
0050         } else if (errorType == QLatin1String("error-room-archived")) {
0051             Q_EMIT openArchivedRoom(channelGroupInfo());
0052         }
0053     }
0054 }
0055 
0056 QString ChannelJoinJob::joinCode() const
0057 {
0058     return mJoinCode;
0059 }
0060 
0061 void ChannelJoinJob::setJoinCode(const QString &joinCode)
0062 {
0063     mJoinCode = joinCode;
0064 }
0065 
0066 bool ChannelJoinJob::requireHttpAuthentication() const
0067 {
0068     return true;
0069 }
0070 
0071 bool ChannelJoinJob::canStart() const
0072 {
0073     if (!RestApiAbstractJob::canStart()) {
0074         return false;
0075     }
0076     if (!hasIdentifier()) {
0077         qCWarning(ROCKETCHATQTRESTAPI_LOG) << "ChannelJoinJob: RoomId and RoomName are empty";
0078         return false;
0079     }
0080     return true;
0081 }
0082 
0083 QJsonDocument ChannelJoinJob::json() const
0084 {
0085     QJsonObject jsonObj;
0086     generateJson(jsonObj);
0087     if (!mJoinCode.isEmpty()) {
0088         jsonObj[QLatin1String("joinCode")] = mJoinCode;
0089     }
0090 
0091     const QJsonDocument postData = QJsonDocument(jsonObj);
0092     return postData;
0093 }
0094 
0095 QNetworkRequest ChannelJoinJob::request() const
0096 {
0097     const QUrl url = mRestApiMethod->generateUrl(RestApiUtil::RestApiUrlType::ChannelsJoin);
0098     QNetworkRequest request(url);
0099     addAuthRawHeader(request);
0100     addRequestAttribute(request);
0101     return request;
0102 }
0103 
0104 QString ChannelJoinJob::errorMessage(const QString &str, const QJsonObject &detail)
0105 {
0106     if (str == QLatin1String("error-room-not-found")) {
0107         return i18n("The required \'%1\' param provided does not match any channel", channelGroupInfo().identifier);
0108     } else if (str == QLatin1String("error-code-invalid")) {
0109         return i18n("The room required a password.");
0110     }
0111     return ChannelGroupBaseJob::errorMessage(str, detail);
0112 }
0113 
0114 #include "moc_channeljoinjob.cpp"