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

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