File indexing completed on 2024-05-12 16:28:11

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 "account/abstractaccount.h"
0008 #include "account/accountmanager.h"
0009 #include "abstracttimelinemodel.h"
0010 #include "post.h"
0011 #include <QAbstractListModel>
0012 
0013 /// Model building on top of AbstractTimelineModel, used by MainTimelineModel and ThreadModel for example
0014 /// \see AbstractTimelineModel
0015 class TimelineModel : public AbstractTimelineModel
0016 {
0017     Q_OBJECT
0018     Q_PROPERTY(QString displayName READ displayName NOTIFY nameChanged)
0019     Q_PROPERTY(bool shouldLoadMore MEMBER m_shouldLoadMore WRITE setShouldLoadMore NOTIFY shouldLoadMoreChanged)
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 Q_SIGNALS:
0068     /// Emitted when actionReply is called
0069     void wantReply(AbstractAccount *account, Post *post, const QModelIndex &index);
0070 
0071     /// Emitted when the timeline display name has changed
0072     void nameChanged();
0073 
0074     void shouldLoadMoreChanged();
0075 
0076 protected:
0077     void fetchMore(const QModelIndex &parent) override;
0078     bool canFetchMore(const QModelIndex &parent) const override;
0079     void fetchedTimeline(const QByteArray &array, bool alwaysAppendToEnd = false);
0080 
0081     AccountManager *m_manager = nullptr;
0082 
0083     QList<Post *> m_timeline;
0084 
0085     bool m_shouldLoadMore = true;
0086     friend class TimelineTest;
0087 };