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

0001 /*
0002    SPDX-FileCopyrightText: 2021-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "channelsconverttoteamjob.h"
0008 #include "restapimethod.h"
0009 #include "rocketchatqtrestapi_debug.h"
0010 
0011 #include <QJsonDocument>
0012 #include <QJsonObject>
0013 #include <QNetworkReply>
0014 
0015 #include <KLocalizedString>
0016 
0017 using namespace RocketChatRestApi;
0018 ChannelsConvertToTeamJob::ChannelsConvertToTeamJob(QObject *parent)
0019     : RestApiAbstractJob(parent)
0020 {
0021 }
0022 
0023 ChannelsConvertToTeamJob::~ChannelsConvertToTeamJob() = default;
0024 
0025 bool ChannelsConvertToTeamJob::start()
0026 {
0027     if (!canStart()) {
0028         deleteLater();
0029         return false;
0030     }
0031     addStartRestApiInfo("ChannelsConvertToTeamJob::start");
0032     submitPostRequest(json());
0033 
0034     return true;
0035 }
0036 
0037 void ChannelsConvertToTeamJob::onPostRequestResponse(const QString &replyErrorString, const QJsonDocument &replyJson)
0038 {
0039     const QJsonObject replyObject = replyJson.object();
0040 
0041     if (replyObject[QLatin1String("success")].toBool()) {
0042         addLoggerInfo(QByteArrayLiteral("ChannelsConvertToTeamJob success: ") + replyJson.toJson(QJsonDocument::Indented));
0043         Q_EMIT channelConvertToTeamDone(replyObject);
0044     } else {
0045         emitFailedMessage(replyErrorString, replyObject);
0046         addLoggerWarning(QByteArrayLiteral("ChannelsConvertToTeamJob: Problem: ") + replyJson.toJson(QJsonDocument::Indented));
0047     }
0048 }
0049 
0050 QString ChannelsConvertToTeamJob::channelId() const
0051 {
0052     return mChannelId;
0053 }
0054 
0055 void ChannelsConvertToTeamJob::setChannelId(const QString &channelId)
0056 {
0057     mChannelId = channelId;
0058 }
0059 
0060 bool ChannelsConvertToTeamJob::requireHttpAuthentication() const
0061 {
0062     return true;
0063 }
0064 
0065 bool ChannelsConvertToTeamJob::canStart() const
0066 {
0067     if (!RestApiAbstractJob::canStart()) {
0068         return false;
0069     }
0070     if (mChannelId.isEmpty()) {
0071         qCWarning(ROCKETCHATQTRESTAPI_LOG) << "ChannelsConvertToTeamJob: mTeamId is empty";
0072         return false;
0073     }
0074     return true;
0075 }
0076 
0077 QNetworkRequest ChannelsConvertToTeamJob::request() const
0078 {
0079     const QUrl url = mRestApiMethod->generateUrl(RestApiUtil::RestApiUrlType::ChannelsConvertToTeam);
0080     QNetworkRequest request(url);
0081     addAuthRawHeader(request);
0082     addRequestAttribute(request);
0083     return request;
0084 }
0085 
0086 QJsonDocument ChannelsConvertToTeamJob::json() const
0087 {
0088     QJsonObject jsonObj;
0089     jsonObj[QLatin1String("channelId")] = mChannelId;
0090     const QJsonDocument postData = QJsonDocument(jsonObj);
0091     return postData;
0092 }
0093 
0094 QString ChannelsConvertToTeamJob::generateErrorMessage(const QString &errorStr) const
0095 {
0096     if (errorStr == QLatin1String("unauthorized")) {
0097         return i18n("Channel can not convert to Team.");
0098     } else if (errorStr == QLatin1String("team-name-already-exists")) {
0099         return i18n("Team already exists.");
0100     }
0101     return RestApiAbstractJob::generateErrorMessage(errorStr);
0102 }
0103 
0104 #include "moc_channelsconverttoteamjob.cpp"