From 4a0542529bef6b68d373863c1773e79fe050368f Mon Sep 17 00:00:00 2001 From: Martin Rotter Date: Fri, 23 Oct 2020 14:52:43 +0200 Subject: [PATCH] Inoreader plugin now fetches labels assigned to messages as well. --- .../services/abstract/serviceroot.cpp | 5 +++-- .../services/inoreader/inoreaderfeed.cpp | 2 +- .../inoreader/inoreaderserviceroot.cpp | 2 +- .../network/inoreadernetworkfactory.cpp | 19 ++++++++++++++++--- .../network/inoreadernetworkfactory.h | 4 ++-- .../tt-rss/network/ttrssnetworkfactory.cpp | 3 +-- 6 files changed, 24 insertions(+), 11 deletions(-) diff --git a/src/librssguard/services/abstract/serviceroot.cpp b/src/librssguard/services/abstract/serviceroot.cpp index 9202338c5..cb66bd4c3 100644 --- a/src/librssguard/services/abstract/serviceroot.cpp +++ b/src/librssguard/services/abstract/serviceroot.cpp @@ -509,7 +509,7 @@ bool ServiceRoot::loadMessagesForItem(RootItem* item, MessagesModel* model) { else if (item->kind() == RootItem::Kind::Label) { // Show messages with particular label. model->setFilter(QString("Messages.is_deleted = 0 AND Messages.is_pdeleted = 0 AND Messages.account_id = %1 AND " - "(SELECT COUNT(*) FROM LabelsInMessages WHERE account_id = %1 AND message = Messages.custom_id AND label = %2) > 0") + "(SELECT COUNT(*) FROM LabelsInMessages WHERE account_id = %1 AND message = Messages.custom_id AND label = '%2') > 0") .arg(QString::number(accountId()), item->customId())); } else if (item->kind() == RootItem::Kind::Labels) { @@ -534,7 +534,8 @@ bool ServiceRoot::loadMessagesForItem(RootItem* item, MessagesModel* model) { QString urls = textualFeedUrls(children).join(QSL(", ")); - qDebug("Displaying messages from feeds IDs: %s and URLs: %s.", qPrintable(filter_clause), qPrintable(urls)); + qDebugNN << "Displaying messages from feeds IDs:" << QUOTE_W_SPACE(filter_clause) + << "and URLs:" << QUOTE_W_SPACE_DOT(urls); } return true; diff --git a/src/librssguard/services/inoreader/inoreaderfeed.cpp b/src/librssguard/services/inoreader/inoreaderfeed.cpp index 3ba05fa9a..06c417057 100644 --- a/src/librssguard/services/inoreader/inoreaderfeed.cpp +++ b/src/librssguard/services/inoreader/inoreaderfeed.cpp @@ -17,7 +17,7 @@ InoreaderServiceRoot* InoreaderFeed::serviceRoot() const { QList InoreaderFeed::obtainNewMessages(bool* error_during_obtaining) { Feed::Status error = Feed::Status::Normal; - QList messages = serviceRoot()->network()->messages(customId(), error); + QList messages = serviceRoot()->network()->messages(getParentServiceRoot(), customId(), error); setStatus(error); diff --git a/src/librssguard/services/inoreader/inoreaderserviceroot.cpp b/src/librssguard/services/inoreader/inoreaderserviceroot.cpp index 97f9a357c..7c2c8511c 100644 --- a/src/librssguard/services/inoreader/inoreaderserviceroot.cpp +++ b/src/librssguard/services/inoreader/inoreaderserviceroot.cpp @@ -132,7 +132,7 @@ QString InoreaderServiceRoot::additionalTooltip() const { RootItem* InoreaderServiceRoot::obtainNewTreeForSyncIn() const { auto tree = m_network->feedsCategories(true); - if (tree->childCount() > 1) { + if (tree != nullptr && tree->childCount() > 1) { auto* lblroot = new LabelsNode(tree); auto labels = m_network->getLabels(); diff --git a/src/librssguard/services/inoreader/network/inoreadernetworkfactory.cpp b/src/librssguard/services/inoreader/network/inoreadernetworkfactory.cpp index f7e9a6714..62a737741 100644 --- a/src/librssguard/services/inoreader/network/inoreadernetworkfactory.cpp +++ b/src/librssguard/services/inoreader/network/inoreadernetworkfactory.cpp @@ -2,6 +2,7 @@ #include "services/inoreader/network/inoreadernetworkfactory.h" +#include "3rd-party/boolinq/boolinq.h" #include "definitions/definitions.h" #include "gui/dialogs/formmain.h" #include "gui/tabwidget.h" @@ -12,6 +13,7 @@ #include "network-web/silentnetworkaccessmanager.h" #include "network-web/webfactory.h" #include "services/abstract/category.h" +#include "services/abstract/labelsnode.h" #include "services/inoreader/definitions.h" #include "services/inoreader/inoreaderfeed.h" #include "services/inoreader/inoreaderserviceroot.h" @@ -152,7 +154,7 @@ QList InoreaderNetworkFactory::getLabels() { return lbls; } -QList InoreaderNetworkFactory::messages(const QString& stream_id, Feed::Status& error) { +QList InoreaderNetworkFactory::messages(ServiceRoot* root, const QString& stream_id, Feed::Status& error) { Downloader downloader; QEventLoop loop; QString target_url = INOREADER_API_FEED_CONTENTS; @@ -188,7 +190,7 @@ QList InoreaderNetworkFactory::messages(const QString& stream_id, Feed: QString messages_data = downloader.lastOutputData(); error = Feed::Status::Normal; - return decodeMessages(messages_data, stream_id); + return decodeMessages(root, messages_data, stream_id); } } @@ -353,9 +355,10 @@ void InoreaderNetworkFactory::onAuthFailed() { }); } -QList InoreaderNetworkFactory::decodeMessages(const QString& messages_json_data, const QString& stream_id) { +QList InoreaderNetworkFactory::decodeMessages(ServiceRoot* root, const QString& messages_json_data, const QString& stream_id) { QList messages; QJsonArray json = QJsonDocument::fromJson(messages_json_data.toUtf8()).object()["items"].toArray(); + auto active_labels = root->labelsNode() != nullptr ? root->labelsNode()->labels() : QList(); messages.reserve(json.count()); @@ -403,6 +406,16 @@ QList InoreaderNetworkFactory::decodeMessages(const QString& messages_j else if (category.contains(INOREADER_STATE_IMPORTANT)) { message.m_isImportant = category.contains(INOREADER_STATE_IMPORTANT); } + else if (category.contains(QSL("label"))) { + Label* label = boolinq::from(active_labels.begin(), active_labels.end()).firstOrDefault([category](Label* lbl) { + return lbl->customId() == category; + }); + + if (label != nullptr) { + // We found live Label object for our assigned label. + message.m_assignedLabels.append(label); + } + } } message.m_contents = message_obj["summary"].toObject()["content"].toString(); diff --git a/src/librssguard/services/inoreader/network/inoreadernetworkfactory.h b/src/librssguard/services/inoreader/network/inoreadernetworkfactory.h index 30c54649a..1c345d109 100644 --- a/src/librssguard/services/inoreader/network/inoreadernetworkfactory.h +++ b/src/librssguard/services/inoreader/network/inoreadernetworkfactory.h @@ -40,7 +40,7 @@ class InoreaderNetworkFactory : public QObject { QList getLabels(); - QList messages(const QString& stream_id, Feed::Status& error); + QList messages(ServiceRoot* root, const QString& stream_id, Feed::Status& error); void markMessagesRead(RootItem::ReadStatus status, const QStringList& custom_ids, bool async = true); void markMessagesStarred(RootItem::Importance importance, const QStringList& custom_ids, bool async = true); @@ -49,7 +49,7 @@ class InoreaderNetworkFactory : public QObject { void onAuthFailed(); private: - QList decodeMessages(const QString& messages_json_data, const QString& stream_id); + QList decodeMessages(ServiceRoot* root, const QString& messages_json_data, const QString& stream_id); RootItem* decodeFeedCategoriesData(const QString& categories, const QString& feeds, bool obtain_icons); void initializeOauth(); diff --git a/src/librssguard/services/tt-rss/network/ttrssnetworkfactory.cpp b/src/librssguard/services/tt-rss/network/ttrssnetworkfactory.cpp index e5a471996..110e2b4f6 100644 --- a/src/librssguard/services/tt-rss/network/ttrssnetworkfactory.cpp +++ b/src/librssguard/services/tt-rss/network/ttrssnetworkfactory.cpp @@ -676,6 +676,7 @@ TtRssGetHeadlinesResponse::~TtRssGetHeadlinesResponse() = default; QList TtRssGetHeadlinesResponse::messages(ServiceRoot* root) const { QList messages; + auto active_labels = root->labelsNode() != nullptr ? root->labelsNode()->labels() : QList(); for (const QJsonValue& item : m_rawContent["content"].toArray()) { QJsonObject mapped = item.toObject(); @@ -686,8 +687,6 @@ QList TtRssGetHeadlinesResponse::messages(ServiceRoot* root) const { message.m_isImportant = mapped["marked"].toBool(); message.m_contents = mapped["content"].toString(); - auto active_labels = root->labelsNode() != nullptr ? root->labelsNode()->labels() : QList(); - for (const QJsonValue& lbl_val : mapped["labels"].toArray()) { QString lbl_custom_id = QString::number(lbl_val.toArray().at(0).toInt()); Label* label = boolinq::from(active_labels.begin(), active_labels.end()).firstOrDefault([lbl_custom_id](Label* lbl) {