File indexing completed on 2024-12-08 10:27:23
0001 // SPDX-FileCopyrightText: 2021 kaniini <https://git.pleroma.social/kaniini> 0002 // SPDX-FileCopyrightText: 2021 Carl Schwan <carl@carlschwan.eu> 0003 // SPDX-License-Identifier: GPL-3.0-only 0004 0005 #pragma once 0006 0007 #include <QFile> 0008 #include <QHttpMultiPart> 0009 #include <QHttpPart> 0010 #include <QImage> 0011 #include <QJsonDocument> 0012 #include <QJsonObject> 0013 #include <QMap> 0014 #include <QObject> 0015 #include <QSettings> 0016 #include <QUrl> 0017 #include <QUrlQuery> 0018 #include <QWebSocket> 0019 #include <QtNetwork/QNetworkAccessManager> 0020 #include <QtNetwork/QNetworkReply> 0021 #include <QtNetwork/QNetworkRequest> 0022 0023 #include "abstractaccount.h" 0024 #include "identity.h" 0025 #include "relationship.h" 0026 #include "timeline/post.h" 0027 0028 class AccountConfig; 0029 0030 class Account : public AbstractAccount 0031 { 0032 Q_OBJECT 0033 0034 public: 0035 explicit Account(const QString &instance_uri, QNetworkAccessManager *nam, bool ignoreSslErrors = false, QObject *parent = nullptr); 0036 explicit Account(const AccountConfig &settings, QNetworkAccessManager *nam, QObject *parent = nullptr); 0037 ~Account() override; 0038 0039 // making API calls 0040 void get(const QUrl &url, 0041 bool authenticated, 0042 QObject *parent, 0043 std::function<void(QNetworkReply *)> callback, 0044 std::function<void(QNetworkReply *)> errorCallback = nullptr) override; 0045 void post(const QUrl &url, 0046 const QJsonDocument &doc, 0047 bool authenticated, 0048 QObject *parent, 0049 std::function<void(QNetworkReply *)> callback, 0050 std::function<void(QNetworkReply *)> errorCallback, 0051 QHash<QByteArray, QByteArray> headers = {}) override; 0052 void post(const QUrl &url, const QUrlQuery &formdata, bool authenticated, QObject *parent, std::function<void(QNetworkReply *)> callback) override; 0053 QNetworkReply *post(const QUrl &url, QHttpMultiPart *message, bool authenticated, QObject *parent, std::function<void(QNetworkReply *)> callback) override; 0054 void put(const QUrl &url, const QJsonDocument &doc, bool authenticated, QObject *parent, std::function<void(QNetworkReply *)> callback) override; 0055 void patch(const QUrl &url, QHttpMultiPart *multiPart, bool authenticated, QObject *parent, std::function<void(QNetworkReply *)>) override; 0056 void deleteResource(const QUrl &url, bool authenticated, QObject *parent, std::function<void(QNetworkReply *)> callback) override; 0057 QNetworkReply *upload(const QUrl &filename, std::function<void(QNetworkReply *)> callback) override; 0058 0059 QWebSocket *streamingSocket(const QString &stream); 0060 QNetworkAccessManager *qnam() 0061 { 0062 return m_qnam; 0063 } 0064 0065 // save/restore. 0066 // writeToSettings assumes a settings object in a parent context 0067 // buildFromSettings assumes a settings object in the object context 0068 void writeToSettings() override; 0069 0070 void buildFromSettings(const AccountConfig &settings) override; 0071 0072 void validateToken() override; 0073 0074 bool hasFollowRequests() const override; 0075 0076 Q_INVOKABLE void checkForFollowRequests() override; 0077 0078 private: 0079 bool m_ignoreSslErrors = false; 0080 QNetworkAccessManager *m_qnam; 0081 QMap<QString, QWebSocket *> m_websockets; 0082 bool m_hasFollowRequests = false; 0083 0084 // common parts for all HTTP request 0085 QNetworkRequest makeRequest(const QUrl &url, bool authenticated) const; 0086 void handleReply(QNetworkReply *reply, std::function<void(QNetworkReply *)> reply_cb, std::function<void(QNetworkReply *)> errorCallback = nullptr) const; 0087 };