From 59da275f0e9c65ef4e047af01d3e6ecc86ed4f2e Mon Sep 17 00:00:00 2001 From: Bart De Vries Date: Thu, 8 Apr 2021 11:12:16 +0200 Subject: [PATCH] Completely fix author update on feed refresh/add --- src/datamanager.cpp | 6 +++++- src/feed.cpp | 31 +++++++++++++++++++++++-------- src/feed.h | 2 ++ src/fetcher.cpp | 12 +++++++++++- 4 files changed, 41 insertions(+), 10 deletions(-) diff --git a/src/datamanager.cpp b/src/datamanager.cpp index 4f083e81..d7e60171 100644 --- a/src/datamanager.cpp +++ b/src/datamanager.cpp @@ -25,6 +25,7 @@ DataManager::DataManager() m_feeds[url]->setLink(link); m_feeds[url]->setDescription(description); m_feeds[url]->setLastUpdated(lastUpdated); + m_feeds[url]->retrieveAuthors(); // TODO: signal feedmodel: Q_EMIT dataChanged(createIndex(i, 0), createIndex(i, 0)); }); connect(&Fetcher::instance(), &Fetcher::entryAdded, this, [this](const QString &feedurl, const QString &id) { @@ -212,6 +213,10 @@ void DataManager::removeFeed(const int &index) void DataManager::addFeed(const QString &url) { + // This method will add the relevant internal data structures, and then add + // a preliminary entry into the database. Those details (as well as entries, + // authors and enclosures) will be updated by calling Fetcher::fetch() which + // will trigger a full update of the feed and all related items. qDebug() << "Adding feed"; if (feedExists(url)) { qDebug() << "Feed already exists"; @@ -240,7 +245,6 @@ void DataManager::addFeed(const QString &url) Q_EMIT feedAdded(urlFromInput.toString()); Fetcher::instance().fetch(urlFromInput.toString()); - // TODO: fetch authors? } Entry* DataManager::getQueueEntry(int const &index) const diff --git a/src/feed.cpp b/src/feed.cpp index d5714101..aac6ae1e 100644 --- a/src/feed.cpp +++ b/src/feed.cpp @@ -90,14 +90,6 @@ Feed::Feed(QString const feedurl) if (!query.next()) qWarning() << "Failed to load feed" << feedurl; - QSqlQuery authorQuery; - authorQuery.prepare(QStringLiteral("SELECT * FROM Authors WHERE id='' AND feed=:feed")); - authorQuery.bindValue(QStringLiteral(":feed"), feedurl); - Database::instance().execute(authorQuery); - while (authorQuery.next()) { - m_authors += new Author(authorQuery.value(QStringLiteral("name")).toString(), authorQuery.value(QStringLiteral("email")).toString(), authorQuery.value(QStringLiteral("uri")).toString(), nullptr); - } - m_subscribed.setSecsSinceEpoch(query.value(QStringLiteral("subscribed")).toInt()); m_lastUpdated.setSecsSinceEpoch(query.value(QStringLiteral("lastUpdated")).toInt()); @@ -114,6 +106,8 @@ Feed::Feed(QString const feedurl) m_errorId = 0; m_errorString = QLatin1String(""); + retrieveAuthors(); + connect(&Fetcher::instance(), &Fetcher::startedFetchingFeed, this, [this](const QString &url) { if (url == m_url) { m_errorId = 0; @@ -150,6 +144,23 @@ Feed::~Feed() { } +void Feed::retrieveAuthors() +{ + qDebug() << "Start retrieving authors for" << m_name; + for (int i=0; i < m_authors.count(); i++) { + delete m_authors[i]; + } + m_authors.clear(); + QSqlQuery authorQuery; + authorQuery.prepare(QStringLiteral("SELECT * FROM Authors WHERE id='' AND feed=:feed")); + authorQuery.bindValue(QStringLiteral(":feed"), m_url); + Database::instance().execute(authorQuery); + while (authorQuery.next()) { + m_authors += new Author(authorQuery.value(QStringLiteral("name")).toString(), authorQuery.value(QStringLiteral("email")).toString(), authorQuery.value(QStringLiteral("uri")).toString(), nullptr); + } + Q_EMIT authorsChanged(m_authors); +} + QString Feed::url() const { return m_url; @@ -261,6 +272,10 @@ void Feed::setDescription(const QString &description) void Feed::setAuthors(const QVector &authors) { + for (auto& author : m_authors) { + delete author; + } + m_authors.clear(); m_authors = authors; Q_EMIT authorsChanged(m_authors); } diff --git a/src/feed.h b/src/feed.h index 9d28e368..7b10c210 100644 --- a/src/feed.h +++ b/src/feed.h @@ -43,6 +43,8 @@ public: ~Feed(); + void retrieveAuthors(); + QString url() const; QString name() const; QString image() const; diff --git a/src/fetcher.cpp b/src/fetcher.cpp index 58a31e6e..f0b7361b 100644 --- a/src/fetcher.cpp +++ b/src/fetcher.cpp @@ -193,7 +193,17 @@ void Fetcher::processEntry(Syndication::ItemPtr entry, const QString &url) void Fetcher::processAuthor(const QString &url, const QString &entryId, const QString &authorName, const QString &authorUri, const QString &authorEmail) { QSqlQuery query; - query.prepare(QStringLiteral("INSERT INTO Authors VALUES(:feed, :id, :name, :uri, :email);")); + query.prepare(QStringLiteral("SELECT COUNT (id) FROM Authors WHERE feed=:feed AND id=:id;")); + query.bindValue(QStringLiteral(":feed"), url); + query.bindValue(QStringLiteral(":id"), entryId); + Database::instance().execute(query); + query.next(); + + if (query.value(0).toInt() != 0) + query.prepare(QStringLiteral("UPDATE Authors SET feed=:feed, id=:id, name=:name, uri=:uri, email=:email WHERE feed=:feed AND id=:id;")); + else + query.prepare(QStringLiteral("INSERT INTO Authors VALUES(:feed, :id, :name, :uri, :email);")); + query.bindValue(QStringLiteral(":feed"), url); query.bindValue(QStringLiteral(":id"), entryId); query.bindValue(QStringLiteral(":name"), authorName);