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

0001 /*
0002    SPDX-FileCopyrightText: 2023-2024 Laurent Montel <montel.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "sendinvitationemailjob.h"
0008 #include "restapimethod.h"
0009 #include "rocketchatqtrestapi_debug.h"
0010 
0011 #include <QJsonArray>
0012 #include <QJsonDocument>
0013 #include <QJsonObject>
0014 #include <QNetworkReply>
0015 using namespace RocketChatRestApi;
0016 SendInvitationEmailJob::SendInvitationEmailJob(QObject *parent)
0017     : RestApiAbstractJob(parent)
0018 {
0019 }
0020 
0021 SendInvitationEmailJob::~SendInvitationEmailJob() = default;
0022 
0023 bool SendInvitationEmailJob::start()
0024 {
0025     if (!canStart()) {
0026         deleteLater();
0027         return false;
0028     }
0029     addStartRestApiInfo("SendInvitationEmailJob::start");
0030     submitPostRequest(json());
0031     return true;
0032 }
0033 
0034 void SendInvitationEmailJob::onPostRequestResponse(const QString &replyErrorString, const QJsonDocument &replyJson)
0035 {
0036     const QJsonObject replyObject = replyJson.object();
0037     if (replyObject[QLatin1String("success")].toBool()) {
0038         addLoggerInfo(QByteArrayLiteral("SendInvitationEmailJob: success: ") + replyJson.toJson(QJsonDocument::Indented));
0039         Q_EMIT sendInvitationEmailsDone();
0040     } else {
0041         emitFailedMessage(replyErrorString, replyObject);
0042         addLoggerWarning(QByteArrayLiteral("SendInvitationEmailJob: Problem: ") + replyJson.toJson(QJsonDocument::Indented));
0043     }
0044 }
0045 
0046 QStringList SendInvitationEmailJob::emails() const
0047 {
0048     return mEmails;
0049 }
0050 
0051 void SendInvitationEmailJob::setEmails(const QStringList &newEmails)
0052 {
0053     mEmails = newEmails;
0054 }
0055 
0056 bool SendInvitationEmailJob::requireHttpAuthentication() const
0057 {
0058     return true;
0059 }
0060 
0061 bool SendInvitationEmailJob::canStart() const
0062 {
0063     if (!RestApiAbstractJob::canStart()) {
0064         return false;
0065     }
0066     if (mEmails.isEmpty()) {
0067         qCWarning(ROCKETCHATQTRESTAPI_LOG) << "Any email defined";
0068         return false;
0069     }
0070     return true;
0071 }
0072 
0073 QNetworkRequest SendInvitationEmailJob::request() const
0074 {
0075     const QUrl url = mRestApiMethod->generateUrl(RestApiUtil::RestApiUrlType::SendInvitationEmails);
0076     QNetworkRequest request(url);
0077     addAuthRawHeader(request);
0078     addRequestAttribute(request);
0079     return request;
0080 }
0081 
0082 QJsonDocument SendInvitationEmailJob::json() const
0083 {
0084     QJsonObject jsonObj;
0085     jsonObj[QLatin1String("emails")] = QJsonArray::fromStringList(mEmails);
0086     const QJsonDocument postData = QJsonDocument(jsonObj);
0087     return postData;
0088 }
0089 
0090 #include "moc_sendinvitationemailjob.cpp"