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

0001 /*
0002    SPDX-FileCopyrightText: 2020-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "usersautocompletejob.h"
0008 #include "restapimethod.h"
0009 #include "rocketchatqtrestapi_debug.h"
0010 #include <QJsonDocument>
0011 #include <QJsonObject>
0012 #include <QNetworkReply>
0013 #include <QUrlQuery>
0014 using namespace RocketChatRestApi;
0015 UsersAutocompleteJob::UsersAutocompleteJob(QObject *parent)
0016     : RestApiAbstractJob(parent)
0017 {
0018 }
0019 
0020 UsersAutocompleteJob::~UsersAutocompleteJob() = default;
0021 
0022 bool UsersAutocompleteJob::requireHttpAuthentication() const
0023 {
0024     return true;
0025 }
0026 
0027 bool UsersAutocompleteJob::start()
0028 {
0029     if (!canStart()) {
0030         qCWarning(ROCKETCHATQTRESTAPI_LOG) << "Impossible to start UsersAutocompleteJob job";
0031         deleteLater();
0032         return false;
0033     }
0034     submitGetRequest();
0035     addStartRestApiInfo(QByteArrayLiteral("UsersAutocompleteJob: Ask info about me"));
0036     return true;
0037 }
0038 
0039 void UsersAutocompleteJob::onGetRequestResponse(const QString &replyErrorString, const QJsonDocument &replyJson)
0040 {
0041     const QJsonObject replyObject = replyJson.object();
0042     if (replyObject[QLatin1String("success")].toBool()) {
0043         addLoggerInfo(QByteArrayLiteral("UsersAutocompleteJob: success: ") + replyJson.toJson(QJsonDocument::Indented));
0044         Q_EMIT usersAutocompleteDone(replyObject);
0045     } else {
0046         emitFailedMessage(replyErrorString, replyObject);
0047         addLoggerWarning(QByteArrayLiteral("UsersAutocompleteJob: Problem: ") + replyJson.toJson(QJsonDocument::Indented));
0048     }
0049 }
0050 
0051 UsersAutocompleteJob::UsersAutocompleterInfo UsersAutocompleteJob::usersCompleterInfo() const
0052 {
0053     return mUsersCompleterInfo;
0054 }
0055 
0056 void UsersAutocompleteJob::setUsersCompleterInfo(const UsersAutocompleterInfo &usersCompleterInfo)
0057 {
0058     mUsersCompleterInfo = usersCompleterInfo;
0059 }
0060 
0061 QNetworkRequest UsersAutocompleteJob::request() const
0062 {
0063     QUrl url = mRestApiMethod->generateUrl(RestApiUtil::RestApiUrlType::UsersAutocomplete);
0064     QUrlQuery queryUrl;
0065     // TODO add support for exception.
0066     // const QString val = QStringLiteral("{\"term\": \"%1\", \"exception\": \"%2\"}").arg(mUsersCompleterInfo.pattern).arg(mUsersCompleterInfo.exception);
0067     const QString val = QStringLiteral("{\"term\": \"%1\"}").arg(mUsersCompleterInfo.pattern);
0068     queryUrl.addQueryItem(QStringLiteral("selector"), val);
0069     url.setQuery(queryUrl);
0070     QNetworkRequest request(url);
0071     addAuthRawHeader(request);
0072     addRequestAttribute(request, false);
0073 
0074     return request;
0075 }
0076 
0077 bool UsersAutocompleteJob::canStart() const
0078 {
0079     if (!mUsersCompleterInfo.isValid()) {
0080         qCWarning(ROCKETCHATQTRESTAPI_LOG) << "UsersAutocompleteJob: selector is empty";
0081         return false;
0082     }
0083     if (!RestApiAbstractJob::canStart()) {
0084         return false;
0085     }
0086     return true;
0087 }
0088 
0089 bool UsersAutocompleteJob::UsersAutocompleterInfo::isValid() const
0090 {
0091     return !pattern.isEmpty();
0092 }
0093 
0094 #include "moc_usersautocompletejob.cpp"