File indexing completed on 2024-12-08 04:34:54

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 "abstractaccount.h"
0008 #include "relationship.h"
0009 
0010 #include <QWebSocket>
0011 
0012 class AccountConfig;
0013 
0014 class Account : public AbstractAccount
0015 {
0016     Q_OBJECT
0017 
0018 public:
0019     explicit Account(const QString &instance_uri, QNetworkAccessManager *nam, bool ignoreSslErrors = false, bool admin = true, QObject *parent = nullptr);
0020     explicit Account(AccountConfig *settings, QNetworkAccessManager *nam, QObject *parent = nullptr);
0021     ~Account() override;
0022     void get(const QUrl &url,
0023              bool authenticated,
0024              QObject *parent,
0025              std::function<void(QNetworkReply *)> callback,
0026              std::function<void(QNetworkReply *)> errorCallback = nullptr) override;
0027     void post(const QUrl &url,
0028               const QJsonDocument &doc,
0029               bool authenticated,
0030               QObject *parent,
0031               std::function<void(QNetworkReply *)> callback,
0032               std::function<void(QNetworkReply *)> errorCallback,
0033               QHash<QByteArray, QByteArray> headers = {}) override;
0034     void post(const QUrl &url,
0035               const QUrlQuery &formdata,
0036               bool authenticated,
0037               QObject *parent,
0038               std::function<void(QNetworkReply *)> callback,
0039               std::function<void(QNetworkReply *)> errorCallback) override;
0040     QNetworkReply *post(const QUrl &url, QHttpMultiPart *message, bool authenticated, QObject *parent, std::function<void(QNetworkReply *)> callback) override;
0041     void put(const QUrl &url, const QJsonDocument &doc, bool authenticated, QObject *parent, std::function<void(QNetworkReply *)> callback) override;
0042     void put(const QUrl &url, const QUrlQuery &formdata, bool authenticated, QObject *parent, std::function<void(QNetworkReply *)> callback) override;
0043     void patch(const QUrl &url, QHttpMultiPart *multiPart, bool authenticated, QObject *parent, std::function<void(QNetworkReply *)>) override;
0044     void deleteResource(const QUrl &url, bool authenticated, QObject *parent, std::function<void(QNetworkReply *)> callback) override;
0045     QNetworkReply *upload(const QUrl &filename, std::function<void(QNetworkReply *)> callback) override;
0046 
0047     QWebSocket *streamingSocket(const QString &stream);
0048     QNetworkAccessManager *qnam()
0049     {
0050         return m_qnam;
0051     }
0052 
0053     void writeToSettings() override;
0054 
0055     void buildFromSettings() override;
0056 
0057     void validateToken(bool newAccount = false) override;
0058 
0059     bool hasFollowRequests() const override;
0060 
0061     Q_INVOKABLE void checkForFollowRequests() override;
0062 
0063     Q_INVOKABLE void updatePushNotifications() override;
0064 
0065 private:
0066     void unsubscribePushNotifications();
0067     void subscribePushNotifications();
0068     QUrlQuery buildNotificationFormData();
0069 
0070     bool m_ignoreSslErrors = false;
0071     QNetworkAccessManager *m_qnam;
0072     QMap<QString, QWebSocket *> m_websockets;
0073     bool m_hasPushSubscription = false;
0074 
0075     // common parts for all HTTP request
0076     QNetworkRequest makeRequest(const QUrl &url, bool authenticated) const;
0077     void handleReply(QNetworkReply *reply, std::function<void(QNetworkReply *)> reply_cb, std::function<void(QNetworkReply *)> errorCallback = nullptr) const;
0078 };