From ca6d37a1cfda7ff0aaac3bcbd2c2cbca48b675e7 Mon Sep 17 00:00:00 2001 From: Will Brown Date: Sat, 23 Nov 2024 15:33:10 -0500 Subject: [PATCH] Add zblock_feed_info_update --- feed_info.c | 24 ++++++++++++++++++++++++ feed_info.h | 3 +++ main.c | 17 ++--------------- 3 files changed, 29 insertions(+), 15 deletions(-) diff --git a/feed_info.c b/feed_info.c index 70f8f52..97befac 100644 --- a/feed_info.c +++ b/feed_info.c @@ -95,3 +95,27 @@ zblock_feed_info_err zblock_feed_info_insert(PGconn *conn, zblock_feed_info *fee PQclear(insert_res); return result; } + +// updates the last_pubDate field of a given feed in the database +zblock_feed_info_err zblock_feed_info_update(PGconn *conn, zblock_feed_info_minimal *feed) { + if (!conn || !feed) return ZBLOCK_FEED_INFO_INVALID_ARGS; + + char channel_id_str[21]; // hold a 64-bit int in decimal form + snprintf(channel_id_str, sizeof(channel_id_str), "%ld", feed->channel_id); + + const char *const update_params[] = {feed->last_pubDate, feed->url, channel_id_str}; + // save the updated pubDate to disk once that's implemented + PGresult *update_res = PQexecParams(conn, + "UPDATE feeds SET last_pubDate = $1 WHERE url = $2 AND channel_id = $3", + 3, NULL, update_params, NULL, NULL, 0 + ); + + zblock_feed_info_err result = ZBLOCK_FEED_INFO_OK; + if (PQresultStatus(update_res) != PGRES_COMMAND_OK) { + log_error("Failed to update feed: %s", PQresultErrorMessage(update_res)); + result = ZBLOCK_FEED_INFO_DBERROR; + } + + PQclear(update_res); + return result; +} diff --git a/feed_info.h b/feed_info.h index 0aaf539..7167e48 100644 --- a/feed_info.h +++ b/feed_info.h @@ -42,4 +42,7 @@ zblock_feed_info_err zblock_feed_info_exists(PGconn *conn, const char *url, u64s // Insert new feed into the database zblock_feed_info_err zblock_feed_info_insert(PGconn *conn, zblock_feed_info *feed); +// updates the last_pubDate field of a given feed in the database +zblock_feed_info_err zblock_feed_info_update(PGconn *conn, zblock_feed_info_minimal *feed); + #endif diff --git a/main.c b/main.c index f92d25b..150343b 100644 --- a/main.c +++ b/main.c @@ -155,21 +155,8 @@ static void timer_retrieve_feeds(struct discord *client, struct discord_timer *t } if (update_pubDate) { - char *current_pubDate = mrss_feed->item->pubDate; - char channel_id_str[21]; // hold a 64-bit int in decimal form - snprintf(channel_id_str, sizeof(channel_id_str), "%ld", feed_buffer->info.channel_id); - - const char *const update_params[] = {current_pubDate, feed_buffer->info.url, channel_id_str}; - // save the updated pubDate to disk once that's implemented - PGresult *update_res = PQexecParams(database_conn, - "UPDATE feeds SET last_pubDate = $1 WHERE url = $2 AND channel_id = $3", - 3, NULL, update_params, NULL, NULL, 0 - ); - - if (PQresultStatus(update_res) != PGRES_COMMAND_OK) { - log_error("Failed to update pubDate: %s", PQresultErrorMessage(update_res)); // cry - } - PQclear(update_res); + feed_buffer->info.url = mrss_feed->item->pubDate; + zblock_feed_info_update(database_conn, &feed_buffer->info); } // done with our feed!