Add better data removal, fix small benign bug

main
DaXcess 2024-08-13 11:31:54 +02:00
parent 576e2ef563
commit ef86e1e2d3
No known key found for this signature in database
GPG Key ID: CF78CC72F0FD5EAD
5 changed files with 32 additions and 8 deletions

View File

@ -6,4 +6,5 @@ DROP TABLE "user";
-- Trigger functions -- Trigger functions
DROP FUNCTION IF EXISTS delete_inactive_accounts();
DROP FUNCTION IF EXISTS update_last_updated_column(); DROP FUNCTION IF EXISTS update_last_updated_column();

View File

@ -1,4 +1,4 @@
-- Trigger functions -- Functions
CREATE OR REPLACE FUNCTION update_last_updated_column() CREATE OR REPLACE FUNCTION update_last_updated_column()
RETURNS TRIGGER AS $$ RETURNS TRIGGER AS $$
@ -8,6 +8,13 @@ BEGIN
END; END;
$$ LANGUAGE plpgsql; $$ 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 -- Tables
CREATE TABLE "user" ( CREATE TABLE "user" (
@ -20,7 +27,7 @@ CREATE TABLE "link_request" (
user_id TEXT UNIQUE NOT NULL, user_id TEXT UNIQUE NOT NULL,
expires TIMESTAMP 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" ( CREATE TABLE "account" (
@ -32,7 +39,7 @@ CREATE TABLE "account" (
expires TIMESTAMP NOT NULL, expires TIMESTAMP NOT NULL,
last_updated TIMESTAMP NOT NULL DEFAULT NOW(), 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 -- Triggers

View File

@ -61,16 +61,16 @@ impl Database {
Ok(result) Ok(result)
} }
pub async fn delete_user(&self, user_id: impl AsRef<str>) -> Result<()> { pub async fn delete_user(&self, user_id: impl AsRef<str>) -> Result<usize> {
use schema::user::dsl::*; use schema::user::dsl::*;
let mut connection = self.0.get().await?; let mut connection = self.0.get().await?;
diesel::delete(user) let affected = diesel::delete(user)
.filter(id.eq(user_id.as_ref())) .filter(id.eq(user_id.as_ref()))
.execute(&mut connection) .execute(&mut connection)
.await?; .await?;
Ok(()) Ok(affected)
} }
pub async fn get_or_create_user(&self, user_id: impl AsRef<str>) -> Result<User> { pub async fn get_or_create_user(&self, user_id: impl AsRef<str>) -> Result<User> {

View File

@ -516,6 +516,11 @@ impl songbird::EventHandler for SessionHandle {
} }
EventContext::ClientDisconnect(ClientDisconnect { user_id }) => { EventContext::ClientDisconnect(ClientDisconnect { user_id }) => {
// Ignore disconnects if we're inactive
if !self.active().await.unwrap_or(false) {
return None;
}
match self.owner().await { match self.owner().await {
Ok(id) if id.get() == user_id.0 => { Ok(id) if id.get() == user_id.0 => {
debug!("Owner of session disconnected, stopping playback"); debug!("Owner of session disconnected, stopping playback");

View File

@ -9,7 +9,11 @@ use crate::bot::{Context, FrameworkError};
/// Unlink your Spotify account from Spoticord /// Unlink your Spotify account from Spoticord
#[poise::command(slash_command, on_error = on_error)] #[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<bool>,
) -> Result<()> {
let manager = ctx.data(); let manager = ctx.data();
let db = manager.database(); let db = manager.database();
let user_id = ctx.author().id.to_string(); let user_id = ctx.author().id.to_string();
@ -19,7 +23,14 @@ pub async fn unlink(ctx: Context<'_>) -> Result<()> {
session.shutdown_player().await; 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( ctx.send(
CreateReply::default() CreateReply::default()
.embed( .embed(