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

0001 // SPDX-FileCopyrightText: 2022 Carl Schwan <carlschwan@kde.org>
0002 // SPDX-License-Identifier: LGPL-2.1-or-later
0003 
0004 #include "tagstimelinemodel.h"
0005 
0006 TagsTimelineModel::TagsTimelineModel(QObject *parent)
0007     : TimelineModel(parent)
0008 {
0009     init();
0010 }
0011 
0012 TagsTimelineModel::~TagsTimelineModel() = default;
0013 
0014 QString TagsTimelineModel::hashtag() const
0015 {
0016     return m_hashtag;
0017 }
0018 
0019 void TagsTimelineModel::setHashtag(const QString &hashtag)
0020 {
0021     if (hashtag == m_hashtag) {
0022         return;
0023     }
0024     m_hashtag = hashtag;
0025     Q_EMIT hashtagChanged();
0026     fillTimeline({});
0027 }
0028 
0029 QString TagsTimelineModel::displayName() const
0030 {
0031     return QLatin1Char('#') + m_hashtag;
0032 }
0033 
0034 void TagsTimelineModel::fillTimeline(const QString &fromId)
0035 {
0036     if (m_hashtag.isEmpty()) {
0037         return;
0038     }
0039     QUrlQuery q;
0040     if (!fromId.isEmpty()) {
0041         q.addQueryItem(QStringLiteral("max_id"), fromId);
0042     }
0043     auto uri = m_account->apiUrl(QStringLiteral("/api/v1/timelines/tag/%1").arg(m_hashtag));
0044     uri.setQuery(q);
0045     const auto account = m_account;
0046     const auto hashtag = m_hashtag;
0047 
0048     auto handleError = [this](QNetworkReply *reply) {
0049         Q_UNUSED(reply)
0050         setLoading(false);
0051     };
0052 
0053     setLoading(true);
0054     m_account->get(
0055         uri,
0056         true,
0057         this,
0058         [this, account, hashtag, uri](QNetworkReply *reply) {
0059             if (account != m_account || m_hashtag != hashtag) {
0060                 // Receiving request for an old query
0061                 setLoading(false);
0062                 return;
0063             }
0064 
0065             fetchedTimeline(reply->readAll());
0066             setLoading(false);
0067         },
0068         handleError);
0069 }
0070 
0071 void TagsTimelineModel::reset()
0072 {
0073     beginResetModel();
0074     qDeleteAll(m_timeline);
0075     m_timeline.clear();
0076     endResetModel();
0077 }
0078 
0079 #include "moc_tagstimelinemodel.cpp"