From 0b19cb38183929d3f242cfd11c34426d9302697c Mon Sep 17 00:00:00 2001 From: cel 🌸 Date: Tue, 8 Apr 2025 11:29:31 +0100 Subject: fix(filamento): check if nick and avatar were actually changed --- filamento/examples/example.rs | 8 ++++---- filamento/src/db.rs | 22 +++++++++++++--------- 2 files changed, 17 insertions(+), 13 deletions(-) (limited to 'filamento') diff --git a/filamento/examples/example.rs b/filamento/examples/example.rs index 12ede0d..8ebfde0 100644 --- a/filamento/examples/example.rs +++ b/filamento/examples/example.rs @@ -52,10 +52,10 @@ async fn main() { client.connect().await.unwrap(); tokio::time::sleep(Duration::from_secs(5)).await; info!("changing nick"); - // client - // .change_nick(Some("britney".to_string())) - // .await - // .unwrap(); + client + .change_nick(Some("britney".to_string())) + .await + .unwrap(); info!("sending message"); client .send_message( diff --git a/filamento/src/db.rs b/filamento/src/db.rs index cdc7520..51d99e6 100644 --- a/filamento/src/db.rs +++ b/filamento/src/db.rs @@ -78,10 +78,11 @@ impl Db { /// returns whether or not the nickname was updated pub(crate) async fn delete_user_nick(&self, jid: JID) -> Result { if sqlx::query!( - "insert into users (jid, nick) values (?, ?) on conflict do update set nick = ?", + "insert into users (jid, nick) values (?, ?) on conflict do update set nick = ? where nick is not ?", jid, None::, None::, + None::, ) .execute(&self.db) .await? @@ -96,17 +97,18 @@ impl Db { /// returns whether or not the nickname was updated pub(crate) async fn upsert_user_nick(&self, jid: JID, nick: String) -> Result { - if sqlx::query!( - "insert into users (jid, nick) values (?, ?) on conflict do update set nick = ?", + let rows_affected = sqlx::query!( + "insert into users (jid, nick) values (?, ?) on conflict do update set nick = ? where nick is not ?", jid, nick, + nick, nick ) .execute(&self.db) .await? - .rows_affected() - > 0 - { + .rows_affected(); + tracing::debug!("rows affected: {}", rows_affected); + if rows_affected > 0 { Ok(true) } else { Ok(false) @@ -129,10 +131,11 @@ impl Db { .map(|row: AvatarRow| row.avatar) .unwrap_or(None); if sqlx::query!( - "insert into users (jid, avatar) values (?, ?) on conflict do update set avatar = ?", + "insert into users (jid, avatar) values (?, ?) on conflict do update set avatar = ? where avatar is not ?", jid, None::, None::, + None::, ) .execute(&self.db) .await? @@ -162,10 +165,11 @@ impl Db { .map(|row: AvatarRow| row.avatar) .unwrap_or(None); if sqlx::query!( - "insert into users (jid, avatar) values (?, ?) on conflict do update set avatar = ?", + "insert into users (jid, avatar) values (?, ?) on conflict do update set avatar = ? where avatar is not ?", jid, avatar, - avatar + avatar, + avatar, ) .execute(&self.db) .await? -- cgit