Completely fix author update on feed refresh/add

This commit is contained in:
Bart De Vries 2021-04-08 11:12:16 +02:00
parent df9dd521f3
commit 59da275f0e
4 changed files with 41 additions and 10 deletions

View File

@ -25,6 +25,7 @@ DataManager::DataManager()
m_feeds[url]->setLink(link); m_feeds[url]->setLink(link);
m_feeds[url]->setDescription(description); m_feeds[url]->setDescription(description);
m_feeds[url]->setLastUpdated(lastUpdated); m_feeds[url]->setLastUpdated(lastUpdated);
m_feeds[url]->retrieveAuthors();
// TODO: signal feedmodel: Q_EMIT dataChanged(createIndex(i, 0), createIndex(i, 0)); // 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) { 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) 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"; qDebug() << "Adding feed";
if (feedExists(url)) { if (feedExists(url)) {
qDebug() << "Feed already exists"; qDebug() << "Feed already exists";
@ -240,7 +245,6 @@ void DataManager::addFeed(const QString &url)
Q_EMIT feedAdded(urlFromInput.toString()); Q_EMIT feedAdded(urlFromInput.toString());
Fetcher::instance().fetch(urlFromInput.toString()); Fetcher::instance().fetch(urlFromInput.toString());
// TODO: fetch authors?
} }
Entry* DataManager::getQueueEntry(int const &index) const Entry* DataManager::getQueueEntry(int const &index) const

View File

@ -90,14 +90,6 @@ Feed::Feed(QString const feedurl)
if (!query.next()) if (!query.next())
qWarning() << "Failed to load feed" << feedurl; 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_subscribed.setSecsSinceEpoch(query.value(QStringLiteral("subscribed")).toInt());
m_lastUpdated.setSecsSinceEpoch(query.value(QStringLiteral("lastUpdated")).toInt()); m_lastUpdated.setSecsSinceEpoch(query.value(QStringLiteral("lastUpdated")).toInt());
@ -114,6 +106,8 @@ Feed::Feed(QString const feedurl)
m_errorId = 0; m_errorId = 0;
m_errorString = QLatin1String(""); m_errorString = QLatin1String("");
retrieveAuthors();
connect(&Fetcher::instance(), &Fetcher::startedFetchingFeed, this, [this](const QString &url) { connect(&Fetcher::instance(), &Fetcher::startedFetchingFeed, this, [this](const QString &url) {
if (url == m_url) { if (url == m_url) {
m_errorId = 0; 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 QString Feed::url() const
{ {
return m_url; return m_url;
@ -261,6 +272,10 @@ void Feed::setDescription(const QString &description)
void Feed::setAuthors(const QVector<Author *> &authors) void Feed::setAuthors(const QVector<Author *> &authors)
{ {
for (auto& author : m_authors) {
delete author;
}
m_authors.clear();
m_authors = authors; m_authors = authors;
Q_EMIT authorsChanged(m_authors); Q_EMIT authorsChanged(m_authors);
} }

View File

@ -43,6 +43,8 @@ public:
~Feed(); ~Feed();
void retrieveAuthors();
QString url() const; QString url() const;
QString name() const; QString name() const;
QString image() const; QString image() const;

View File

@ -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) void Fetcher::processAuthor(const QString &url, const QString &entryId, const QString &authorName, const QString &authorUri, const QString &authorEmail)
{ {
QSqlQuery query; QSqlQuery query;
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.prepare(QStringLiteral("INSERT INTO Authors VALUES(:feed, :id, :name, :uri, :email);"));
query.bindValue(QStringLiteral(":feed"), url); query.bindValue(QStringLiteral(":feed"), url);
query.bindValue(QStringLiteral(":id"), entryId); query.bindValue(QStringLiteral(":id"), entryId);
query.bindValue(QStringLiteral(":name"), authorName); query.bindValue(QStringLiteral(":name"), authorName);