ttrss: system label PUBLISHED is now fetched and can be assigned/removed to/from articles

This commit is contained in:
Martin Rotter 2022-01-14 10:26:30 +01:00
parent dc78cce665
commit 8ebd35c3b6
3 changed files with 63 additions and 4 deletions

@ -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

@ -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<QPair<QByteArray, QByteArray>> headers;
@ -786,6 +787,9 @@ QList<Message> TtRssGetHeadlinesResponse::messages(ServiceRoot* root) const {
QList<Message> messages;
auto active_labels = root->labelsNode() != nullptr ? root->labelsNode()->labels() : QList<Label*>();
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<Message> 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<RootItem*> TtRssGetLabelsResponse::labels() const {
QList<RootItem*> 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()));

@ -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<Category, TtRssFeed>(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);