feat: add delete account message
This commit is contained in:
parent
e602194f85
commit
9a3ba3f0a5
|
@ -0,0 +1,39 @@
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use anyhow::{Context, Result};
|
||||||
|
use tokio::sync::RwLock;
|
||||||
|
|
||||||
|
use crate::{ClientState, ErrorResponse, State, WsStream};
|
||||||
|
use crate::types::protocol::{DeleteAccountRequest, DeleteAccountResponse};
|
||||||
|
|
||||||
|
pub async fn delete_account(state: Arc<RwLock<State>>, client_state: Arc<RwLock<ClientState>>, conn: &mut WsStream, number: u32, _req: DeleteAccountRequest) -> Result<()> {
|
||||||
|
let id = match client_state.read().await.lodestone_id() {
|
||||||
|
Some(id) => id,
|
||||||
|
None => return crate::util::send(conn, number, ErrorResponse::new(None, "no Lodestone ID? this is a bug")).await,
|
||||||
|
};
|
||||||
|
let lodestone_id = id as i64;
|
||||||
|
|
||||||
|
let channels = sqlx::query!(
|
||||||
|
// language=sqlite
|
||||||
|
"select count(*) as count from user_channels where lodestone_id = ?",
|
||||||
|
lodestone_id,
|
||||||
|
)
|
||||||
|
.fetch_one(&state.read().await.db)
|
||||||
|
.await
|
||||||
|
.context("could not get channel count")?;
|
||||||
|
|
||||||
|
if channels.count > 0 {
|
||||||
|
return crate::util::send(conn, number, ErrorResponse::new(None, "leave all linkshells first")).await;
|
||||||
|
}
|
||||||
|
|
||||||
|
sqlx::query!(
|
||||||
|
// language=sqlite
|
||||||
|
"delete from users where lodestone_id = ?",
|
||||||
|
lodestone_id,
|
||||||
|
)
|
||||||
|
.execute(&state.read().await.db)
|
||||||
|
.await
|
||||||
|
.context("could not delete user")?;
|
||||||
|
|
||||||
|
crate::util::send(conn, number, DeleteAccountResponse {}).await
|
||||||
|
}
|
|
@ -2,6 +2,7 @@ pub use self::{
|
||||||
allow_invites::*,
|
allow_invites::*,
|
||||||
authenticate::*,
|
authenticate::*,
|
||||||
create::*,
|
create::*,
|
||||||
|
delete_account::*,
|
||||||
disband::*,
|
disband::*,
|
||||||
invite::*,
|
invite::*,
|
||||||
join::*,
|
join::*,
|
||||||
|
@ -22,6 +23,7 @@ pub use self::{
|
||||||
pub mod allow_invites;
|
pub mod allow_invites;
|
||||||
pub mod authenticate;
|
pub mod authenticate;
|
||||||
pub mod create;
|
pub mod create;
|
||||||
|
pub mod delete_account;
|
||||||
pub mod disband;
|
pub mod disband;
|
||||||
pub mod invite;
|
pub mod invite;
|
||||||
pub mod join;
|
pub mod join;
|
||||||
|
|
|
@ -427,6 +427,9 @@ async fn client_loop(state: Arc<RwLock<State>>, mut conn: WsStream) -> Result<()
|
||||||
RequestKind::AllowInvites(req) if logged_in => {
|
RequestKind::AllowInvites(req) if logged_in => {
|
||||||
crate::handlers::allow_invites(Arc::clone(&state), Arc::clone(&client_state), &mut conn, msg.number, req).await?;
|
crate::handlers::allow_invites(Arc::clone(&state), Arc::clone(&client_state), &mut conn, msg.number, req).await?;
|
||||||
}
|
}
|
||||||
|
RequestKind::DeleteAccount(req) if logged_in => {
|
||||||
|
crate::handlers::delete_account(Arc::clone(&state), Arc::clone(&client_state), &mut conn, msg.number, req).await?;
|
||||||
|
}
|
||||||
_ if !logged_in => {
|
_ if !logged_in => {
|
||||||
util::send(&mut conn, msg.number, ErrorResponse::new(None, "not logged in")).await?;
|
util::send(&mut conn, msg.number, ErrorResponse::new(None, "not logged in")).await?;
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,6 +29,7 @@ pub enum RequestKind {
|
||||||
Secrets(SecretsRequest),
|
Secrets(SecretsRequest),
|
||||||
SendSecrets(SendSecretsRequest),
|
SendSecrets(SendSecretsRequest),
|
||||||
AllowInvites(AllowInvitesRequest),
|
AllowInvites(AllowInvitesRequest),
|
||||||
|
DeleteAccount(DeleteAccountRequest),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
@ -63,6 +64,7 @@ pub enum ResponseKind {
|
||||||
SendSecrets(SendSecretsResponse),
|
SendSecrets(SendSecretsResponse),
|
||||||
Announce(AnnounceResponse),
|
Announce(AnnounceResponse),
|
||||||
AllowInvites(AllowInvitesResponse),
|
AllowInvites(AllowInvitesResponse),
|
||||||
|
DeleteAccount(DeleteAccountResponse),
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! request_container {
|
macro_rules! request_container {
|
||||||
|
@ -93,6 +95,7 @@ request_container!(PublicKey, PublicKeyRequest);
|
||||||
request_container!(Secrets, SecretsRequest);
|
request_container!(Secrets, SecretsRequest);
|
||||||
request_container!(SendSecrets, SendSecretsRequest);
|
request_container!(SendSecrets, SendSecretsRequest);
|
||||||
request_container!(AllowInvites, AllowInvitesRequest);
|
request_container!(AllowInvites, AllowInvitesRequest);
|
||||||
|
request_container!(DeleteAccount, DeleteAccountRequest);
|
||||||
|
|
||||||
macro_rules! response_container {
|
macro_rules! response_container {
|
||||||
($name:ident, $response:ty) => {
|
($name:ident, $response:ty) => {
|
||||||
|
@ -127,3 +130,4 @@ response_container!(Secrets, SecretsResponse);
|
||||||
response_container!(SendSecrets, SendSecretsResponse);
|
response_container!(SendSecrets, SendSecretsResponse);
|
||||||
response_container!(Announce, AnnounceResponse);
|
response_container!(Announce, AnnounceResponse);
|
||||||
response_container!(AllowInvites, AllowInvitesResponse);
|
response_container!(AllowInvites, AllowInvitesResponse);
|
||||||
|
response_container!(DeleteAccount, DeleteAccountResponse);
|
||||||
|
|
|
@ -0,0 +1,9 @@
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
pub struct DeleteAccountRequest {
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
pub struct DeleteAccountResponse {
|
||||||
|
}
|
|
@ -1,8 +1,34 @@
|
||||||
|
pub use self::{
|
||||||
|
allow_invites::*,
|
||||||
|
announce::*,
|
||||||
|
authenticate::*,
|
||||||
|
container::*,
|
||||||
|
create::*,
|
||||||
|
delete_account::*,
|
||||||
|
disband::*,
|
||||||
|
error::*,
|
||||||
|
invite::*,
|
||||||
|
join::*,
|
||||||
|
kick::*,
|
||||||
|
leave::*,
|
||||||
|
list::*,
|
||||||
|
member_change::*,
|
||||||
|
message::*,
|
||||||
|
ping::*,
|
||||||
|
promote::*,
|
||||||
|
public_key::*,
|
||||||
|
register::*,
|
||||||
|
secrets::*,
|
||||||
|
update::*,
|
||||||
|
version::*,
|
||||||
|
};
|
||||||
|
|
||||||
pub mod allow_invites;
|
pub mod allow_invites;
|
||||||
pub mod announce;
|
pub mod announce;
|
||||||
pub mod authenticate;
|
pub mod authenticate;
|
||||||
pub mod container;
|
pub mod container;
|
||||||
pub mod create;
|
pub mod create;
|
||||||
|
pub mod delete_account;
|
||||||
pub mod disband;
|
pub mod disband;
|
||||||
pub mod error;
|
pub mod error;
|
||||||
pub mod invite;
|
pub mod invite;
|
||||||
|
@ -22,26 +48,3 @@ pub mod version;
|
||||||
|
|
||||||
pub mod channel;
|
pub mod channel;
|
||||||
|
|
||||||
pub use self::{
|
|
||||||
allow_invites::*,
|
|
||||||
announce::*,
|
|
||||||
authenticate::*,
|
|
||||||
container::*,
|
|
||||||
create::*,
|
|
||||||
disband::*,
|
|
||||||
error::*,
|
|
||||||
invite::*,
|
|
||||||
join::*,
|
|
||||||
kick::*,
|
|
||||||
leave::*,
|
|
||||||
list::*,
|
|
||||||
member_change::*,
|
|
||||||
message::*,
|
|
||||||
ping::*,
|
|
||||||
promote::*,
|
|
||||||
public_key::*,
|
|
||||||
register::*,
|
|
||||||
secrets::*,
|
|
||||||
update::*,
|
|
||||||
version::*,
|
|
||||||
};
|
|
||||||
|
|
Loading…
Reference in New Issue