File indexing completed on 2024-04-28 16:13:21

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 <QDateTime>
0008 #include <QImage>
0009 #include <QJsonObject>
0010 #include <QNetworkReply>
0011 #include <QObject>
0012 #include <QQmlListProperty>
0013 #include <QString>
0014 #include <QUrl>
0015 
0016 #include <QJsonObject>
0017 #include <memory>
0018 #include <optional>
0019 
0020 #include "poll.h"
0021 
0022 class Post;
0023 class Identity;
0024 class AbstractAccount;
0025 
0026 class Application
0027 {
0028     Q_GADGET
0029     Q_PROPERTY(QString name READ name)
0030     Q_PROPERTY(QUrl website READ website)
0031 
0032 public:
0033     Application() = default;
0034     explicit Application(QJsonObject application);
0035 
0036     QString name() const;
0037     QUrl website() const;
0038 
0039 private:
0040     QJsonObject m_application;
0041 };
0042 
0043 class Card
0044 {
0045     Q_GADGET
0046     Q_PROPERTY(QString authorName READ authorName)
0047     Q_PROPERTY(QString authorUrl READ authorUrl)
0048     Q_PROPERTY(QString blurhash READ blurhash)
0049     Q_PROPERTY(QString description READ description)
0050     Q_PROPERTY(QString embedUrl READ embedUrl)
0051     Q_PROPERTY(int width READ width)
0052     Q_PROPERTY(int height READ height)
0053     Q_PROPERTY(QString html READ html)
0054     Q_PROPERTY(QString image READ image)
0055     Q_PROPERTY(QString providerName READ providerName)
0056     Q_PROPERTY(QString providerUrl READ providerUrl)
0057     Q_PROPERTY(QString title READ title)
0058     Q_PROPERTY(QUrl url READ url)
0059 
0060 public:
0061     Card() = default;
0062     explicit Card(QJsonObject card);
0063 
0064     QString authorName() const;
0065     QString authorUrl() const;
0066     QString blurhash() const;
0067     QString description() const;
0068     QString embedUrl() const;
0069     int width() const;
0070     int height() const;
0071     QString html() const;
0072     QString image() const;
0073     QString providerName() const;
0074     QString providerUrl() const;
0075     QString title() const;
0076     QUrl url() const;
0077 
0078 private:
0079     QJsonObject m_card;
0080 };
0081 
0082 /// Post's attachment object.
0083 /// TODO make it possible to fetch the images with a Qml image provider.
0084 /// TODO use getter and setter
0085 class Attachment : public QObject
0086 {
0087     Q_OBJECT
0088 
0089     Q_PROPERTY(QString id MEMBER m_id CONSTANT)
0090     Q_PROPERTY(AttachmentType attachmentType MEMBER m_type CONSTANT)
0091     Q_PROPERTY(int type READ isVideo CONSTANT)
0092     Q_PROPERTY(QString previewUrl MEMBER m_preview_url CONSTANT)
0093     Q_PROPERTY(QString source MEMBER m_url CONSTANT)
0094     Q_PROPERTY(QString remoteUrl MEMBER m_remote_url CONSTANT)
0095     Q_PROPERTY(QString caption READ description CONSTANT)
0096     Q_PROPERTY(QString tempSource READ tempSource CONSTANT)
0097     Q_PROPERTY(int sourceWidth MEMBER m_sourceWidth CONSTANT)
0098     Q_PROPERTY(int sourceHeight MEMBER m_sourceHeight CONSTANT)
0099 
0100 public:
0101     explicit Attachment(QObject *parent = nullptr);
0102     explicit Attachment(const QJsonObject &object, QObject *parent = nullptr);
0103 
0104     enum AttachmentType {
0105         Unknown,
0106         Image,
0107         GifV,
0108         Video,
0109     };
0110     Q_ENUM(AttachmentType);
0111 
0112     Post *m_parent = nullptr;
0113 
0114     QString m_id;
0115     AttachmentType m_type = AttachmentType::Unknown;
0116     QString m_preview_url;
0117     QString m_url;
0118     QString m_remote_url;
0119     int m_sourceWidth = -1;
0120     int m_sourceHeight = -1;
0121 
0122     QString id() const;
0123 
0124     void setDescription(const QString &description);
0125     QString description() const;
0126 
0127     /// Used exclusively in Maximize component to tell it whether or not an attachment is a video
0128     int isVideo() const;
0129 
0130     QString tempSource() const;
0131 
0132 private:
0133     void fromJson(const QJsonObject &object);
0134 
0135     QString m_description;
0136     QString m_blurhash;
0137 };
0138 
0139 class Notification
0140 {
0141     Q_GADGET
0142 
0143 public:
0144     Notification() = default;
0145     explicit Notification(AbstractAccount *account, const QJsonObject &obj, QObject *parent = nullptr);
0146 
0147     enum Type {
0148         Mention,
0149         Follow,
0150         Repeat,
0151         Favorite,
0152         Poll,
0153         FollowRequest,
0154         Update,
0155     };
0156     Q_ENUM(Type);
0157 
0158     int id() const;
0159     AbstractAccount *account() const;
0160     Type type() const;
0161     Post *post() const;
0162     std::shared_ptr<Identity> identity() const;
0163 
0164 private:
0165     int m_id = 0;
0166 
0167     AbstractAccount *m_account = nullptr;
0168     Post *m_post = nullptr;
0169     Type m_type = Type::Favorite;
0170     std::shared_ptr<Identity> m_identity;
0171 
0172     Post *createPost(AbstractAccount *account, const QJsonObject &obj, QObject *parent);
0173 };
0174 
0175 class Post : public QObject
0176 {
0177     Q_OBJECT
0178     Q_PROPERTY(QString spoilerText READ spoilerText WRITE setSpoilerText NOTIFY spoilerTextChanged)
0179     Q_PROPERTY(QString content READ content WRITE setContent NOTIFY contentChanged)
0180     Q_PROPERTY(QString contentType READ contentType WRITE setContentType NOTIFY contentTypeChanged)
0181     Q_PROPERTY(bool sensitive READ sensitive WRITE setSensitive NOTIFY sensitiveChanged)
0182     Q_PROPERTY(Visibility visibility READ visibility WRITE setVisibility NOTIFY visibilityChanged)
0183     Q_PROPERTY(QString language READ language WRITE setLanguage NOTIFY languageChanged)
0184     Q_PROPERTY(QString inReplyTo READ inReplyTo WRITE setInReplyTo NOTIFY inReplyToChanged)
0185     Q_PROPERTY(QStringList mentions READ mentions WRITE setMentions NOTIFY mentionsChanged)
0186     Q_PROPERTY(Poll *poll READ poll NOTIFY pollChanged)
0187     Q_PROPERTY(QStringList filters READ filters CONSTANT)
0188     Q_PROPERTY(Identity *authorIdentity READ getAuthorIdentity CONSTANT)
0189     Q_PROPERTY(QQmlListProperty<Attachment> attachments READ attachmentList CONSTANT)
0190     Q_PROPERTY(QString relativeTime READ relativeTime CONSTANT)
0191     Q_PROPERTY(QString absoluteTime READ absoluteTime CONSTANT)
0192     Q_PROPERTY(Card *card READ getCard CONSTANT)
0193     Q_PROPERTY(QString type READ type CONSTANT)
0194 
0195 public:
0196     Post() = delete;
0197     Post(const Post &) = delete;
0198     explicit Post(AbstractAccount *account, QObject *parent = nullptr);
0199     Post(AbstractAccount *account, QJsonObject obj, QObject *parent = nullptr);
0200 
0201     enum Visibility {
0202         Public,
0203         Unlisted,
0204         Private,
0205         Direct,
0206     };
0207     Q_ENUM(Visibility)
0208 
0209     AbstractAccount *m_parent;
0210 
0211     QString type() const;
0212     void fromJson(QJsonObject obj);
0213 
0214     Identity *getAuthorIdentity() const;
0215     std::shared_ptr<Identity> authorIdentity() const;
0216     std::shared_ptr<Identity> boostIdentity() const;
0217     std::shared_ptr<Identity> replyIdentity() const;
0218     bool boosted() const;
0219 
0220     /// Returns the post id of the status itself
0221     QString postId() const;
0222     QString originalPostId() const;
0223     QUrl url() const;
0224 
0225     /// Returns the spoiler text (subject) of the status
0226     QString spoilerText() const;
0227     void setSpoilerText(const QString &spoilerText);
0228 
0229     QString content() const;
0230     void setContent(const QString &content);
0231     QString contentType() const;
0232     void setContentType(const QString &contentType);
0233     bool sensitive() const;
0234     void setSensitive(bool isSensitive);
0235     Visibility visibility() const;
0236     void setVisibility(Visibility visibility);
0237     QString language() const;
0238     void setLanguage(const QString &language);
0239     QString inReplyTo() const;
0240     void setInReplyTo(const QString &inReplyTo);
0241     QStringList mentions() const;
0242     void setMentions(const QStringList &mentions);
0243     std::optional<Card> card() const;
0244     Card *getCard() const;
0245     void setCard(std::optional<Card> card);
0246     std::optional<Application> application() const;
0247     void setApplication(std::optional<Application> application);
0248     void setPollJson(const QJsonObject &object);
0249     Poll *poll() const;
0250     QStringList filters() const;
0251 
0252     void addAttachment(const QJsonObject &attachment);
0253 
0254     /// Returns the published/creation time of this status.
0255     QDateTime publishedAt() const;
0256 
0257     /// Returns a locale-aware relative time.
0258     QString relativeTime() const;
0259 
0260     // Returns absolute locale-aware time
0261     QString absoluteTime() const;
0262 
0263     /// Returns whether the user favorited this status.
0264     bool favourited() const;
0265     /// Set this post as favourited.
0266     void setFavourited(bool favourited);
0267     /// Returns whether the user reblogged this status.
0268     bool reblogged() const;
0269     /// Set this post as reblogged.
0270     void setReblogged(bool reblogged);
0271     /// Returns whether the user muted this status.
0272     bool muted() const;
0273     /// Set this post as muted.
0274     void setMuted(bool muted);
0275     /// Returns whether the user bookmarked this status.
0276     bool bookmarked() const;
0277     /// Set this post as bookmarked.
0278     void setBookmarked(bool bookmarked);
0279     /// Returns whether the user pinned this status.
0280     bool pinned() const;
0281     /// Set this status as pinned
0282     void setPinned(bool pinned);
0283     /// Returns whether the user filtered this status.
0284     bool filtered() const;
0285 
0286     /// Returns the number of time this status has been boosted.
0287     int reblogsCount() const;
0288     /// Returns the number of time this status has been favorited.
0289     int favouritesCount() const;
0290     /// Returns the number of time this status has been replied.
0291     int repliesCount() const;
0292 
0293     /// Returns whether this status is empty
0294     bool isEmpty() const;
0295 
0296     Q_INVOKABLE void addAttachments(const QJsonArray &attachments);
0297     void setDirtyAttachment();
0298     void updateAttachment(Attachment *a);
0299     QList<Attachment *> attachments() const;
0300     QQmlListProperty<Attachment> attachmentList() const;
0301     bool attachmentsVisible() const;
0302     void setAttachmentsVisible(bool attachmentsVisible);
0303 
0304     bool hidden() const
0305     {
0306         return m_hidden;
0307     }
0308 
0309 Q_SIGNALS:
0310     void spoilerTextChanged();
0311     void contentChanged();
0312     void contentTypeChanged();
0313     void sensitiveChanged();
0314     void visibilityChanged();
0315     void languageChanged();
0316     void inReplyToChanged();
0317     void mentionsChanged();
0318     void attachmentUploaded();
0319     void pollChanged();
0320     void replyIdentityChanged();
0321 
0322 private:
0323     bool m_attachments_visible = true;
0324     QDateTime m_publishedAt;
0325     QString m_postId;
0326     QString m_originalPostId;
0327     QUrl m_url;
0328     QString m_content;
0329     QString m_spoilerText;
0330     QString m_author;
0331     QString m_reply_to_author;
0332     QString m_content_type;
0333     QStringList m_mentions;
0334     QString m_language;
0335 
0336     QString m_replyTargetId;
0337     QStringList m_filters;
0338     std::optional<Card> m_card;
0339     std::optional<Application> m_application;
0340     std::shared_ptr<Identity> m_authorIdentity;
0341     QList<Attachment *> m_attachments;
0342     QQmlListProperty<Attachment> m_attachmentList;
0343     std::unique_ptr<Poll> m_poll;
0344 
0345     bool m_sensitive = false;
0346     Visibility m_visibility;
0347 
0348     bool m_boosted = false;
0349     std::shared_ptr<Identity> m_boostIdentity;
0350 
0351     std::shared_ptr<Identity> m_replyIdentity;
0352 
0353     bool m_favourited = false;
0354     bool m_reblogged = false;
0355     bool m_muted = false;
0356     bool m_bookmarked = false;
0357     bool m_filtered = false;
0358     bool m_hidden = false;
0359     bool m_pinned = false;
0360 
0361     int m_reblogsCount = 0;
0362     int m_favouritesCount = 0;
0363     int m_repliesCount = 0;
0364 };
0365 
0366 Q_DECLARE_METATYPE(Application)
0367 Q_DECLARE_METATYPE(Card)
0368 Q_DECLARE_METATYPE(Notification)