mirror of
https://github.com/WCBROW01/zblock.git
synced 2025-12-11 20:18:07 -05:00
Remove feeds associated with a channel when it is deleted
This commit is contained in:
23
feed_info.c
23
feed_info.c
@ -207,6 +207,29 @@ zblock_feed_info_err zblock_feed_info_delete_all_guild(PGconn *conn, u64snowflak
|
||||
return result;
|
||||
}
|
||||
|
||||
// deletes all feeds associated with a channel from the database
|
||||
zblock_feed_info_err zblock_feed_info_delete_all_channel(PGconn *conn, u64snowflake channel_id) {
|
||||
if (!conn) return ZBLOCK_FEED_INFO_INVALID_ARGS;
|
||||
|
||||
uint64_t channel_id_be = htobe64(channel_id);
|
||||
const char *const params[] = {(char *) &channel_id_be};
|
||||
const int param_lengths[] = {sizeof(channel_id_be)};
|
||||
const int param_formats[] = {1};
|
||||
PGresult *res = PQexecParams(conn,
|
||||
"DELETE FROM feeds WHERE channel_id = $1::bigint",
|
||||
1, NULL, params, param_lengths, param_formats, 1
|
||||
);
|
||||
|
||||
zblock_feed_info_err result = ZBLOCK_FEED_INFO_OK;
|
||||
if (PQresultStatus(res) != PGRES_COMMAND_OK) {
|
||||
log_error(PQresultErrorMessage(res));
|
||||
result = ZBLOCK_FEED_INFO_DBERROR;
|
||||
}
|
||||
|
||||
PQclear(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;
|
||||
|
||||
@ -63,6 +63,9 @@ zblock_feed_info_err zblock_feed_info_delete(PGconn *conn, const char *url, u64s
|
||||
// deletes all feeds associated with a guild from the database
|
||||
zblock_feed_info_err zblock_feed_info_delete_all_guild(PGconn *conn, u64snowflake guild_id);
|
||||
|
||||
// deletes all feeds associated with a channel from the database
|
||||
zblock_feed_info_err zblock_feed_info_delete_all_channel(PGconn *conn, u64snowflake channel_id);
|
||||
|
||||
// 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);
|
||||
|
||||
|
||||
8
main.c
8
main.c
@ -560,6 +560,13 @@ static void on_guild_delete(struct discord *client, const struct discord_guild *
|
||||
}
|
||||
}
|
||||
|
||||
static void on_channel_delete(struct discord *client, const struct discord_channel *event) {
|
||||
(void) client;
|
||||
if (zblock_feed_info_delete_all_channel(database_conn, event->id)) {
|
||||
log_error("Unable to delete all feeds from channel %" PRIu64 ". You probably want to clean this up.", event->id);
|
||||
}
|
||||
}
|
||||
|
||||
// delay before the first feed retrieval (in ms)
|
||||
#define FEED_TIMER_DELAY 15000
|
||||
|
||||
@ -598,6 +605,7 @@ int main(void) {
|
||||
discord_set_on_ready(client, &on_ready);
|
||||
discord_set_on_interaction_create(client, &on_interaction);
|
||||
discord_set_on_guild_delete(client, &on_guild_delete);
|
||||
discord_set_on_channel_delete(client, &on_channel_delete);
|
||||
|
||||
// register timers
|
||||
discord_timer_interval(client, timer_retrieve_feeds, NULL, NULL, FEED_TIMER_DELAY, FEED_TIMER_INTERVAL, -1);
|
||||
|
||||
Reference in New Issue
Block a user