[main.c] Complete functionality for add command

This commit is contained in:
2024-11-22 22:18:05 -05:00
parent 082a185ba3
commit d2863ec148

55
main.c
View File

@ -64,8 +64,9 @@ static void timer_retrieve_feeds(struct discord *client, struct discord_timer *t
// all this SQL stuff should *really* be extracted somewhere else // all this SQL stuff should *really* be extracted somewhere else
// maybe make a function where you can do a lookup with a quantity and offset // maybe make a function where you can do a lookup with a quantity and offset
PGresult *database_res = PQexec(database_conn, "SELECT url, last_pubDate, channel_id from feeds"); PGresult *database_res = PQexec(database_conn, "SELECT url, last_pubDate, channel_id from feeds");
if (PQresultStatus(database_res) != PGRES_COMMAND_OK) { if (PQresultStatus(database_res) != PGRES_TUPLES_OK) {
log_error("Unable to retrieve feed list: %s", PQerrorMessage(database_conn)); log_error("Unable to retrieve feed list: %s", PQresultErrorMessage(select_res));
PQclear(database_res);
return; return;
} }
@ -162,9 +163,9 @@ static void timer_retrieve_feeds(struct discord *client, struct discord_timer *t
"UPDATE feeds SET last_pubDate = $1 WHERE url = $2 AND channel_id = $3", "UPDATE feeds SET last_pubDate = $1 WHERE url = $2 AND channel_id = $3",
3, NULL, update_params, NULL, NULL, 0 3, NULL, update_params, NULL, NULL, 0
); );
ExecStatusType update_status = PQresultStatus(update_res);
if (update_status != PGRES_COMMAND_OK) { if (PQresultStatus(update_res) != PGRES_COMMAND_OK) {
log_error("Failed to update pubDate: %s", PQresStatus(update_status)); // cry log_error("Failed to update pubDate: %s", PQresultErrorMessage(update_res)); // cry
} }
PQclear(update_res); PQclear(update_res);
} }
@ -210,32 +211,53 @@ static void bot_command_add(struct discord *client, const struct discord_interac
zblock_feed_info feed; zblock_feed_info feed;
feed.url = event->data->options->array[0].value; feed.url = event->data->options->array[0].value;
if (!feed.url) { feed.channel_id = event->channel_id;
snprintf(msg, sizeof(msg), "Error adding feed: %s", strerror(errno)); feed.guild_id = event->guild_id;
goto send_msg;
// check if it already exists
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 select_params[] = {feed.url, channel_id_str};
PGresult *select_res = PQexecParams(database_conn, "SELECT * from feeds WHERE url = $1 AND channel_id = $2", 2, NULL, select_params, NULL, NULL, 0);
if (PQresultStatus(select_res) != PGRES_TUPLES_OK) {
snprintf(msg, sizeof(msg), "Error adding feed: %s", PQresultErrorMessage(select_res));
PQclear(select_res);
} }
mrss_t *mrss_feed = NULL; if (PQntuples(select_res) > 0) {
if(mrss_parse_url(feed.url, &mrss_feed)) { PQclear(select_res)
// error here figure this out snprintf(msg, sizeof(msg), "Error adding feed: it has already been added to this channel");
goto send_msg;
} else {
PQclear(select_res); // don't actually do anything with the contents of the query
}
mrss_t *mrss_feed;
mrss_error_t mrss_error = mrss_parse_url(feed.url, &mrss_feed);
if (mrss_error) {
snprintf(msg, sizeof(msg), "Error adding feed: %s", mrss_strerror(mrss_error));
goto send_msg; goto send_msg;
} }
feed.title = mrss_feed->title; feed.title = mrss_feed->title;
feed.last_pubDate = mrss_feed->item->pubDate; feed.last_pubDate = mrss_feed->item->pubDate;
feed.guild_id = event->guild_id;
feed.channel_id = event->channel_id;
zblock_feed_info_err feed_error = zblock_feed_info_insert(database_conn, &feed); PGresult *insert_res = PQexecParams(database_conn,
if (feed_error) { "INSERT INTO feeds (url, last_pubDate, channel_id, title, guild_id) VALUES ($1, $2, $3, $4, $5)",
feed.url, feed.last_pubDate, channel_id_str, feed.title, guild_id_str,
5, NULL, select_params, NULL, NULL, 0
);
if (PQresultStatus(insert_res) != PGRES_COMMAND_OK) {
// write error message // write error message
snprintf(msg, sizeof(msg), "Error adding feed: %s", zblock_feed_info_strerror(feed_error)); snprintf(msg, sizeof(msg), "Error adding feed: %s", zblock_feed_info_strerror(feed_error));
} else { } else {
// write the confirmation message // write the confirmation message
snprintf(msg, sizeof(msg), "The following feed has been successfully added to this channel:\n`%s`", feed.url); snprintf(msg, sizeof(msg), "The following feed has been successfully added to this channel:\n`%s`", feed.url);
} }
PQclear(insert_res);
mrss_free(mrss_feed); mrss_free(mrss_feed);
send_msg: send_msg:
@ -362,6 +384,7 @@ int main(void) {
discord_set_on_interaction_create(client, &on_interaction); discord_set_on_interaction_create(client, &on_interaction);
discord_run(client); discord_run(client);
PQfinish(database_conn);
cleanup: cleanup:
discord_cleanup(client); discord_cleanup(client);
ccord_global_cleanup(); ccord_global_cleanup();