diff --git a/feed_info.c b/feed_info.c index d14e1b9..cbbad7a 100644 --- a/feed_info.c +++ b/feed_info.c @@ -126,21 +126,10 @@ zblock_feed_info_err zblock_feed_info_exists(PGconn *conn, const char *url, u64s return ZBLOCK_FEED_INFO_OK; } -// Insert new feed into the database +// Insert new feed into the database. This function assumes that you have already verified that it has not been added previously. zblock_feed_info_err zblock_feed_info_insert(PGconn *conn, zblock_feed_info *feed) { if (!conn || !feed) return ZBLOCK_FEED_INFO_INVALID_ARGS; - // check if the feed already exists - { - int feed_exists; - zblock_feed_info_err exists_error = zblock_feed_info_exists(conn, feed->url, feed->channel_id, &feed_exists); - if (exists_error) { - return exists_error; - } else if (feed_exists) { - return ZBLOCK_FEED_INFO_EXISTS; - } - } - uint64_t channel_id_be = htobe64(feed->channel_id); uint64_t guild_id_be = htobe64(feed->guild_id); const char *const insert_params[] = {feed->url, feed->last_pubDate, (char *) &channel_id_be, feed->title, (char *) &guild_id_be}; diff --git a/feed_info.h b/feed_info.h index 6712b65..a5e7c6f 100644 --- a/feed_info.h +++ b/feed_info.h @@ -54,7 +54,7 @@ zblock_feed_info_err zblock_feed_info_retrieve_list_item(PGconn *conn, zblock_fe // check if the feed currently exists. the result is in the exists pointer. zblock_feed_info_err zblock_feed_info_exists(PGconn *conn, const char *url, u64snowflake channel_id, int *exists); -// Insert new feed into the database +// Insert new feed into the database. This function assumes that you have already verified that it has not been added previously. zblock_feed_info_err zblock_feed_info_insert(PGconn *conn, zblock_feed_info *feed); // deletes feed from the database diff --git a/main.c b/main.c index c068e6f..7646303 100644 --- a/main.c +++ b/main.c @@ -225,6 +225,19 @@ static void bot_command_add(struct discord *client, const struct discord_interac feed.url = event->data->options->array[0].value; feed.channel_id = event->channel_id; feed.guild_id = event->guild_id; + + // check if the feed already exists + { + int feed_exists; + zblock_feed_info_err exists_error = zblock_feed_info_exists(database_conn, feed.url, feed.channel_id, &feed_exists); + if (exists_error) { + snprintf(msg, sizeof(msg), "Error adding feed: %s", zblock_feed_info_strerror(exists_error)); + goto send_msg; + } else if (feed_exists) { + snprintf(msg, sizeof(msg), "Error adding feed: It has already been added to this channel"); + goto send_msg; + } + } mrss_t *mrss_feed; mrss_error_t mrss_error = mrss_parse_url(feed.url, &mrss_feed);