File indexing completed on 2024-05-12 05:04:17

0001 // SPDX-FileCopyrightText: 2021 kaniini <https://git.pleroma.social/kaniini>
0002 // SPDX-FileCopyrightText: 2021 Carl Schwan <carlschwan@kde.org>
0003 // SPDX-License-Identifier: GPL-3.0-only
0004 
0005 #pragma once
0006 
0007 #include "abstractaccount.h"
0008 #include "abstracttimelinemodel.h"
0009 
0010 /// Model building on top of AbstractTimelineModel, used by MainTimelineModel and ThreadModel for example
0011 /// \see AbstractTimelineModel
0012 class TimelineModel : public AbstractTimelineModel
0013 {
0014     Q_OBJECT
0015 
0016     Q_PROPERTY(QString displayName READ displayName NOTIFY nameChanged)
0017     Q_PROPERTY(bool shouldLoadMore MEMBER m_shouldLoadMore WRITE setShouldLoadMore NOTIFY shouldLoadMoreChanged)
0018     Q_PROPERTY(bool showReplies MEMBER m_showReplies NOTIFY showRepliesChanged)
0019     Q_PROPERTY(bool showBoosts MEMBER m_showBoosts NOTIFY showBoostsChanged)
0020 
0021 public:
0022     explicit TimelineModel(QObject *parent = nullptr);
0023 
0024     int rowCount(const QModelIndex &parent) const override;
0025     QVariant data(const QModelIndex &index, int role) const override;
0026 
0027     /// Start filling the timeline starting at \p fromId
0028     virtual void fillTimeline(const QString &fromId = {}) = 0;
0029 
0030     /// Get a translated label for the timeline (e.g. "Home")
0031     virtual QString displayName() const = 0;
0032 
0033     /// Handle an incoming streaming event
0034     virtual void handleEvent(AbstractAccount::StreamingEventType eventType, const QByteArray &payload);
0035 
0036     /// Initialize and start filling the timeline
0037     void init();
0038 
0039     void setShouldLoadMore(bool shouldLoadMore);
0040 
0041 public Q_SLOTS:
0042     /// Reply to the post at \p index
0043     /// \see wantReply
0044     void actionReply(const QModelIndex &index);
0045 
0046     /// Favorite the post at \p index
0047     void actionFavorite(const QModelIndex &index);
0048 
0049     /// Boost the post at \p index
0050     void actionRepeat(const QModelIndex &index);
0051 
0052     /// Vote on the post at \p index
0053     void actionVote(const QModelIndex &index, const QList<int> &choices);
0054 
0055     /// Bookmark the post at \p index
0056     void actionBookmark(const QModelIndex &index);
0057 
0058     /// Delete & re-draft the post at \p index
0059     /// \see postSourceReady
0060     void actionRedraft(const QModelIndex &index, bool isEdit);
0061 
0062     /// Delete the post at \p index
0063     void actionDelete(const QModelIndex &index);
0064 
0065     /// Pin the post at \p index
0066     void actionPin(const QModelIndex &index);
0067 
0068     /// Reset the timeline posts, and any additional state
0069     virtual void reset() = 0;
0070 
0071 Q_SIGNALS:
0072     /// Emitted when actionReply is called
0073     void wantReply(AbstractAccount *account, Post *post, const QModelIndex &index);
0074 
0075     /// Emitted when the timeline display name has changed
0076     void nameChanged();
0077 
0078     void shouldLoadMoreChanged();
0079 
0080     void showRepliesChanged();
0081     void showBoostsChanged();
0082 
0083 protected:
0084     void fetchMore(const QModelIndex &parent) override;
0085     bool canFetchMore(const QModelIndex &parent) const override;
0086     void fetchedTimeline(const QByteArray &array, bool alwaysAppendToEnd = false);
0087 
0088     AccountManager *m_manager = nullptr;
0089 
0090     QList<Post *> m_timeline;
0091 
0092     bool m_shouldLoadMore = true;
0093     bool m_showReplies = true;
0094     bool m_showBoosts = true;
0095     friend class TimelineTest;
0096 };