diff --git a/spoticord_database/migrations/2024-06-17-090114_initial_setup/down.sql b/spoticord_database/migrations/2024-06-17-090114_initial_setup/down.sql index ac0e43b..c82a495 100644 --- a/spoticord_database/migrations/2024-06-17-090114_initial_setup/down.sql +++ b/spoticord_database/migrations/2024-06-17-090114_initial_setup/down.sql @@ -6,4 +6,5 @@ DROP TABLE "user"; -- Trigger functions +DROP FUNCTION IF EXISTS delete_inactive_accounts(); DROP FUNCTION IF EXISTS update_last_updated_column(); \ No newline at end of file diff --git a/spoticord_database/migrations/2024-06-17-090114_initial_setup/up.sql b/spoticord_database/migrations/2024-06-17-090114_initial_setup/up.sql index aace681..d3c29a7 100644 --- a/spoticord_database/migrations/2024-06-17-090114_initial_setup/up.sql +++ b/spoticord_database/migrations/2024-06-17-090114_initial_setup/up.sql @@ -1,4 +1,4 @@ --- Trigger functions +-- Functions CREATE OR REPLACE FUNCTION update_last_updated_column() RETURNS TRIGGER AS $$ @@ -8,6 +8,13 @@ BEGIN END; $$ LANGUAGE plpgsql; +CREATE OR REPLACE FUNCTION delete_inactive_accounts() RETURNS void AS $$ +BEGIN + DELETE FROM account + WHERE last_updated < NOW() - INTERVAL '2 months'; +END; +$$ LANGUAGE plpgsql; + -- Tables CREATE TABLE "user" ( @@ -20,7 +27,7 @@ CREATE TABLE "link_request" ( user_id TEXT UNIQUE NOT NULL, expires TIMESTAMP NOT NULL, - CONSTRAINT fk_user_id FOREIGN KEY (user_id) REFERENCES "user" (id) + CONSTRAINT fk_user_id FOREIGN KEY (user_id) REFERENCES "user" (id) ON DELETE CASCADE ); CREATE TABLE "account" ( @@ -32,7 +39,7 @@ CREATE TABLE "account" ( expires TIMESTAMP NOT NULL, last_updated TIMESTAMP NOT NULL DEFAULT NOW(), - CONSTRAINT fk_account_user_id FOREIGN KEY (user_id) REFERENCES "user" (id) + CONSTRAINT fk_account_user_id FOREIGN KEY (user_id) REFERENCES "user" (id) ON DELETE CASCADE ); -- Triggers diff --git a/spoticord_database/src/lib.rs b/spoticord_database/src/lib.rs index fc40f2b..14898bd 100644 --- a/spoticord_database/src/lib.rs +++ b/spoticord_database/src/lib.rs @@ -61,16 +61,16 @@ impl Database { Ok(result) } - pub async fn delete_user(&self, user_id: impl AsRef) -> Result<()> { + pub async fn delete_user(&self, user_id: impl AsRef) -> Result { use schema::user::dsl::*; let mut connection = self.0.get().await?; - diesel::delete(user) + let affected = diesel::delete(user) .filter(id.eq(user_id.as_ref())) .execute(&mut connection) .await?; - Ok(()) + Ok(affected) } pub async fn get_or_create_user(&self, user_id: impl AsRef) -> Result { diff --git a/spoticord_session/src/lib.rs b/spoticord_session/src/lib.rs index cafd942..f77c6f3 100644 --- a/spoticord_session/src/lib.rs +++ b/spoticord_session/src/lib.rs @@ -516,6 +516,11 @@ impl songbird::EventHandler for SessionHandle { } EventContext::ClientDisconnect(ClientDisconnect { user_id }) => { + // Ignore disconnects if we're inactive + if !self.active().await.unwrap_or(false) { + return None; + } + match self.owner().await { Ok(id) if id.get() == user_id.0 => { debug!("Owner of session disconnected, stopping playback"); diff --git a/src/commands/core/unlink.rs b/src/commands/core/unlink.rs index e55f791..04859ab 100644 --- a/src/commands/core/unlink.rs +++ b/src/commands/core/unlink.rs @@ -9,7 +9,11 @@ use crate::bot::{Context, FrameworkError}; /// Unlink your Spotify account from Spoticord #[poise::command(slash_command, on_error = on_error)] -pub async fn unlink(ctx: Context<'_>) -> Result<()> { +pub async fn unlink( + ctx: Context<'_>, + + #[description = "Also delete Discord account information"] user_data: Option, +) -> Result<()> { let manager = ctx.data(); let db = manager.database(); let user_id = ctx.author().id.to_string(); @@ -19,7 +23,14 @@ pub async fn unlink(ctx: Context<'_>) -> Result<()> { session.shutdown_player().await; } - if db.delete_account(&user_id).await? == 0 { + let deleted_account = db.delete_account(&user_id).await? != 0; + let deleted_user = if user_data.unwrap_or(false) { + db.delete_user(&user_id).await? != 0 + } else { + false + }; + + if !deleted_account && !deleted_user { ctx.send( CreateReply::default() .embed(