// SPDX-FileCopyrightText: 2015 Felix Rohrbach // SPDX-FileCopyrightText: 2016 Kitsune Ral // SPDX-License-Identifier: LGPL-2.1-or-later #include "basejob.h" #include "connectiondata.h" #include #include #include #include #include #include #include using namespace Quotient; using std::chrono::seconds, std::chrono::milliseconds; using namespace std::chrono_literals; BaseJob::StatusCode BaseJob::Status::fromHttpCode(int httpCode) { // Based on https://en.wikipedia.org/wiki/List_of_HTTP_status_codes if (httpCode / 10 == 41) // 41x errors return httpCode == 410 ? IncorrectRequest : NotFound; switch (httpCode) { case 401: return Unauthorised; // clang-format off case 403: case 407: // clang-format on return ContentAccessError; case 404: return NotFound; // clang-format off case 400: case 405: case 406: case 426: case 428: case 505: // clang-format on case 494: // Unofficial nginx "Request header too large" case 497: // Unofficial nginx "HTTP request sent to HTTPS port" return IncorrectRequest; case 429: return TooManyRequests; case 501: case 510: return RequestNotImplemented; case 511: return NetworkAuthRequired; default: return NetworkError; } } QDebug BaseJob::Status::dumpToLog(QDebug dbg) const { QDebugStateSaver _s(dbg); dbg.noquote().nospace(); if (auto* const k = QMetaEnum::fromType().valueToKey(code)) { const QByteArray b = k; dbg << b.mid(b.lastIndexOf(':')); } else dbg << code; return dbg << ": " << message; } class BaseJob::Private { public: struct JobTimeoutConfig { seconds jobTimeout; seconds nextRetryInterval; }; // Using an idiom from clang-tidy: // http://clang.llvm.org/extra/clang-tidy/checks/modernize-pass-by-value.html Private(HttpVerb v, QByteArray endpoint, const QUrlQuery& q, RequestData&& data, bool nt) : verb(v) , apiEndpoint(std::move(endpoint)) , requestQuery(q) , requestData(std::move(data)) , needsToken(nt) { timer.setSingleShot(true); retryTimer.setSingleShot(true); } ~Private() { if (reply) { if (reply->isRunning()) { reply->abort(); } delete reply; } } void sendRequest(); /*! \brief Parse the response byte array into JSON * * This calls QJsonDocument::fromJson() on rawResponse, converts * the QJsonParseError result to BaseJob::Status and stores the resulting * JSON in jsonResponse. */ Status parseJson(); ConnectionData* connection = nullptr; // Contents for the network request HttpVerb verb; QByteArray apiEndpoint; QHash requestHeaders; QUrlQuery requestQuery; RequestData requestData; bool needsToken; bool inBackground = false; // There's no use of QMimeType here because we don't want to match // content types against the known MIME type hierarchy; and at the same // type QMimeType is of little help with MIME type globs (`text/*` etc.) QByteArrayList expectedContentTypes { "application/json" }; QByteArrayList expectedKeys; // When the QNetworkAccessManager is destroyed it destroys all pending replies. // Using QPointer allows us to know when that happend. QPointer reply; Status status = Unprepared; QByteArray rawResponse; /// Contains a null document in case of non-JSON body (for a successful /// or unsuccessful response); a document with QJsonObject or QJsonArray /// in case of a successful response with JSON payload, as per the API /// definition (including an empty JSON object - QJsonObject{}); /// and QJsonObject in case of an API error. QJsonDocument jsonResponse; QUrl errorUrl; //< May contain a URL to help with some errors LoggingCategory logCat = JOBS; QTimer timer; QTimer retryTimer; static constexpr std::array errorStrategy { { { 90s, 5s }, { 90s, 10s }, { 120s, 30s } } }; int maxRetries = int(errorStrategy.size()); int retriesTaken = 0; [[nodiscard]] const JobTimeoutConfig& getCurrentTimeoutConfig() const { return errorStrategy[std::min(size_t(retriesTaken), errorStrategy.size() - 1)]; } [[nodiscard]] QString dumpRequest() const { // FIXME: use std::array {} when Apple stdlib gets deduction guides for it static const auto verbs = make_array(QStringLiteral("GET"), QStringLiteral("PUT"), QStringLiteral("POST"), QStringLiteral("DELETE")); const auto verbWord = verbs.at(size_t(verb)); return verbWord % ' ' % (reply ? reply->url().toString(QUrl::RemoveQuery) : makeRequestUrl(connection->baseUrl(), apiEndpoint) .toString()); } }; inline bool isHex(QChar c) { return c.isDigit() || (c >= u'A' && c <= u'F') || (c >= u'a' && c <= u'f'); } QByteArray BaseJob::encodeIfParam(const QString& paramPart) { const auto percentIndex = paramPart.indexOf('%'); if (percentIndex != -1 && paramPart.size() > percentIndex + 2 && isHex(paramPart[percentIndex + 1]) && isHex(paramPart[percentIndex + 2])) { qCWarning(JOBS) << "Developers, upfront percent-encoding of job parameters is " "deprecated since libQuotient 0.7; the string involved is" << paramPart; return QUrl(paramPart, QUrl::TolerantMode).toEncoded(); } return QUrl::toPercentEncoding(paramPart); } BaseJob::BaseJob(HttpVerb verb, const QString& name, QByteArray endpoint, bool needsToken) : BaseJob(verb, name, std::move(endpoint), QUrlQuery {}, RequestData {}, needsToken) {} BaseJob::BaseJob(HttpVerb verb, const QString& name, QByteArray endpoint, const QUrlQuery& query, RequestData&& data, bool needsToken) : d(makeImpl(verb, std::move(endpoint), query, std::move(data), needsToken)) { setObjectName(name); connect(&d->timer, &QTimer::timeout, this, &BaseJob::timeout); connect(&d->retryTimer, &QTimer::timeout, this, [this] { qCDebug(d->logCat) << "Retrying" << this; d->connection->submit(this); }); } BaseJob::~BaseJob() { sto
/******************************************************************************
 * Copyright (C) 2015 Felix Rohrbach <kde@fxrh.de>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
 */

#pragma once

#include "eventcontent.h"
#include "stateevent.h"

namespace Quotient {
class MemberEventContent : public EventContent::Base {
public:
    enum MembershipType : unsigned char {
        Invite = 0,
        Join,
        Knock,
        Leave,
        Ban,
        Undefined
    };

    explicit MemberEventContent(MembershipType mt = Join) : membership(mt) {}
    explicit MemberEventContent(const QJsonObject& json);

    MembershipType membership;
    bool isDirect = false;
    Omittable<QString> displayName;
    Omittable<QUrl> avatarUrl;
    QString reason;

protected:
    void fillJson(QJsonObject* o) const override;
};

using MembershipType = MemberEventContent::MembershipType;

class RoomMemberEvent : public StateEvent<MemberEventContent> {
    Q_GADGET
public:
    DEFINE_EVENT_TYPEID("m.room.member", RoomMemberEvent)

    using MembershipType = MemberEventContent::MembershipType;
    Q_ENUM(MembershipType)

    explicit RoomMemberEvent(const QJsonObject& obj) : StateEvent(typeId(), obj)
    {}
    template <typename... ArgTs>
    RoomMemberEvent(const QString& userId, ArgTs&&... contentArgs)
        : StateEvent(typeId(), matrixTypeId(), userId,
                     std::forward<ArgTs>(contentArgs)...)
    {}

    /// A special constructor to create unknown RoomMemberEvents
    /**
     * This is needed in order to use RoomMemberEvent as a "base event
     * class" in cases like GetMembersByRoomJob when RoomMemberEvents
     * (rather than RoomEvents or StateEvents) are resolved from JSON.
     * For such cases loadEvent<> requires an underlying class to be
     * constructible with unknownTypeId() instead of its genuine id.
     * Don't use it directly.
     * \sa GetMembersByRoomJob, loadEvent, unknownTypeId
     */
    RoomMemberEvent(Type type, const QJsonObject& fullJson)
        : StateEvent(type, fullJson)
    {}

    MembershipType membership() const { return content().membership; }
    QString userId() const { return stateKey(); }
    bool isDirect() const { return content().isDirect; }
    Omittable<QString> newDisplayName() const { return content().displayName; }
    Omittable<QUrl> newAvatarUrl() const { return content().avatarUrl; }
    [[deprecated("Use newDisplayName() instead")]] QString displayName() const
    {
        return newDisplayName().value_or(QString());
    }
    [[deprecated("Use newAvatarUrl() instead")]] QUrl avatarUrl() const
    {
        return newAvatarUrl().value_or(QUrl());
    }
    QString reason() const { return content().reason; }
    bool changesMembership() const;
    bool isBan() const;
    bool isUnban() const;
    bool isInvite() const;
    bool isRejectedInvite() const;
    bool isJoin() const;
    bool isLeave() const;
    bool isRename() const;
    bool isAvatarUpdate() const;
};

template <>
class EventFactory<RoomMemberEvent> {
public:
    static event_ptr_tt<RoomMemberEvent> make(const QJsonObject& json,
                                              const QString&)
    {
        return makeEvent<RoomMemberEvent>(json);
    }
};

REGISTER_EVENT_TYPE(RoomMemberEvent)
} // namespace Quotient
// To alleviate that, a stricter condition is applied, that for Abandoned // and to-be-Abandoned jobs the status message will be disregarded entirely. // We could rectify the situation by making d->connection a QPointer<> // (and deriving ConnectionData from QObject, respectively) but it's // a too edge case for the hassle. if (d->status == s) return; if (d->status.code == Abandoned || s.code == Abandoned) s.message.clear(); if (!s.message.isEmpty() && d->connection && !d->connection->accessToken().isEmpty()) s.message.replace(d->connection->accessToken(), "(REDACTED)"); if (!s.good()) qCWarning(d->logCat) << this << "status" << s; d->status = std::move(s); emit statusChanged(d->status); } void BaseJob::setStatus(int code, QString message) { setStatus({ code, std::move(message) }); } void BaseJob::abandon() { beforeAbandon(); d->timer.stop(); d->retryTimer.stop(); // In case abandon() was called between retries setStatus(Abandoned); if (d->reply) d->reply->disconnect(this); emit finished(this); deleteLater(); } void BaseJob::timeout() { setStatus(Timeout, "The job has timed out"); finishJob(); } void BaseJob::setLoggingCategory(LoggingCategory lcf) { d->logCat = lcf; }