From 8ebd35c3b627e7c7ad6fb14a74540e66f06067fc Mon Sep 17 00:00:00 2001 From: Martin Rotter Date: Fri, 14 Jan 2022 10:26:30 +0100 Subject: [PATCH] ttrss: system label PUBLISHED is now fetched and can be assigned/removed to/from articles --- src/librssguard/services/tt-rss/definitions.h | 7 ++-- .../services/tt-rss/ttrssnetworkfactory.cpp | 25 +++++++++++++ .../services/tt-rss/ttrssserviceroot.cpp | 35 +++++++++++++++++-- 3 files changed, 63 insertions(+), 4 deletions(-) diff --git a/src/librssguard/services/tt-rss/definitions.h b/src/librssguard/services/tt-rss/definitions.h index af78a25ad..124092c88 100644 --- a/src/librssguard/services/tt-rss/definitions.h +++ b/src/librssguard/services/tt-rss/definitions.h @@ -18,8 +18,8 @@ #define TTRSS_MAX_MESSAGES 200 // General return status codes. -#define TTRSS_API_STATUS_OK 0 -#define TTRSS_API_STATUS_ERR 1 +#define TTRSS_API_STATUS_OK 0 +#define TTRSS_API_STATUS_ERR 1 #define TTRSS_CONTENT_NOT_LOADED -1 // Login. @@ -29,6 +29,9 @@ // Get feed tree. #define TTRSS_GFT_TYPE_CATEGORY "category" +// Special feeds. +#define TTRSS_FEED_PUBLISHED_ID -2 + // Subscribe to feed. #define STF_UNKNOWN -1 #define STF_EXISTS 0 diff --git a/src/librssguard/services/tt-rss/ttrssnetworkfactory.cpp b/src/librssguard/services/tt-rss/ttrssnetworkfactory.cpp index b10bda856..b869079a4 100644 --- a/src/librssguard/services/tt-rss/ttrssnetworkfactory.cpp +++ b/src/librssguard/services/tt-rss/ttrssnetworkfactory.cpp @@ -276,6 +276,7 @@ TtRssGetHeadlinesResponse TtRssNetworkFactory::getHeadlines(int feed_id, int lim json[QSL("show_content")] = show_content; json[QSL("include_attachments")] = include_attachments; json[QSL("sanitize")] = sanitize; + const int timeout = qApp->settings()->value(GROUP(Feeds), SETTING(Feeds::UpdateTimeout)).toInt(); QByteArray result_raw; QList> headers; @@ -786,6 +787,9 @@ QList TtRssGetHeadlinesResponse::messages(ServiceRoot* root) const { QList messages; auto active_labels = root->labelsNode() != nullptr ? root->labelsNode()->labels() : QList(); auto json_msgs = m_rawContent[QSL("content")].toArray(); + auto* published_lbl = boolinq::from(active_labels).firstOrDefault([](const Label* lbl) { + return lbl->customNumericId() == TTRSS_FEED_PUBLISHED_ID; + }); for (const QJsonValue& item : qAsConst(json_msgs)) { QJsonObject mapped = item.toObject(); @@ -797,6 +801,11 @@ QList TtRssGetHeadlinesResponse::messages(ServiceRoot* root) const { message.m_contents = mapped[QSL("content")].toString(); message.m_rawContents = QJsonDocument(mapped).toJson(QJsonDocument::JsonFormat::Compact); + if (published_lbl != nullptr && mapped[QSL("published")].toBool()) { + // Article is published, set label. + message.m_assignedLabels.append(published_lbl); + } + auto json_labels = mapped[QSL("labels")].toArray(); for (const QJsonValue& lbl_val : qAsConst(json_labels)) { @@ -903,6 +912,22 @@ QList TtRssGetLabelsResponse::labels() const { QList labels; auto json_labels = m_rawContent[QSL("content")].toArray(); + // Add "Published" label. + // + // NOTE: In TT-RSS there is a problem with "published" feature: + // 1. If user has article in existing feed, he can mark it as "published" and in + // that case, the "published" behaves more like a label. + // 2. If user uses feature "shareToPublished", he essentially creates new textual + // note, which is then assigned to "Published feed" but can be also assigned label from 1). + // + // This label solves situation 1). 2) is solved in other way (creating static system feed). + QString published_caption = QObject::tr("[SYSTEM] Published articles"); + auto* published_lbl = new Label(published_caption, TextFactory::generateColorFromText(published_caption)); + + published_lbl->setKeepOnTop(true); + published_lbl->setCustomId(QString::number(TTRSS_FEED_PUBLISHED_ID)); + labels.append(published_lbl); + for (const QJsonValue& lbl_val : qAsConst(json_labels)) { QJsonObject lbl_obj = lbl_val.toObject(); Label* lbl = new Label(lbl_obj[QSL("caption")].toString(), QColor(lbl_obj[QSL("fg_color")].toString())); diff --git a/src/librssguard/services/tt-rss/ttrssserviceroot.cpp b/src/librssguard/services/tt-rss/ttrssserviceroot.cpp index a15653822..5f79374a2 100644 --- a/src/librssguard/services/tt-rss/ttrssserviceroot.cpp +++ b/src/librssguard/services/tt-rss/ttrssserviceroot.cpp @@ -2,6 +2,7 @@ #include "services/tt-rss/ttrssserviceroot.h" +#include "3rd-party/boolinq/boolinq.h" #include "database/databasequeries.h" #include "exceptions/feedfetchexception.h" #include "miscellaneous/application.h" @@ -41,6 +42,14 @@ void TtRssServiceRoot::start(bool freshly_activated) { if (!freshly_activated) { DatabaseQueries::loadFromDatabase(this); loadCacheFromFile(); + + auto lbls = m_labelsNode->labels(); + + boolinq::from(lbls).for_each([](Label* lbl) { + if (lbl->customNumericId() == TTRSS_FEED_PUBLISHED_ID) { + lbl->setKeepOnTop(true); + } + }); } updateTitle(); @@ -159,7 +168,18 @@ void TtRssServiceRoot::saveAllCachedData(bool ignore_errors) { QStringList messages = k.value(); if (!messages.isEmpty()) { - auto res = network()->setArticleLabel(messages, label_custom_id, true, networkProxy()); + TtRssResponse res; + + if (label_custom_id.toInt() == TTRSS_FEED_PUBLISHED_ID) { + // "published" label must be added in other method. + res = network()->updateArticles(messages, + UpdateArticle::OperatingField::Published, + UpdateArticle::Mode::SetToTrue, + networkProxy()); + } + else { + res = network()->setArticleLabel(messages, label_custom_id, true, networkProxy()); + } if (!ignore_errors && (network()->lastError() != QNetworkReply::NetworkError::NoError || res.hasError())) { addLabelsAssignmentsToCache(messages, label_custom_id, true); @@ -176,7 +196,18 @@ void TtRssServiceRoot::saveAllCachedData(bool ignore_errors) { QStringList messages = l.value(); if (!messages.isEmpty()) { - auto res = network()->setArticleLabel(messages, label_custom_id, false, networkProxy()); + TtRssResponse res; + + if (label_custom_id.toInt() == TTRSS_FEED_PUBLISHED_ID) { + // "published" label must be removed in other method. + res = network()->updateArticles(messages, + UpdateArticle::OperatingField::Published, + UpdateArticle::Mode::SetToFalse, + networkProxy()); + } + else { + res = network()->setArticleLabel(messages, label_custom_id, false, networkProxy()); + } if (!ignore_errors && (network()->lastError() != QNetworkReply::NetworkError::NoError || res.hasError())) { addLabelsAssignmentsToCache(messages, label_custom_id, false);