more things

This commit is contained in:
sam 2024-02-25 22:26:24 +01:00
parent b2c3a521e9
commit ad247ca0f4
7 changed files with 77 additions and 10 deletions

View file

@ -3,7 +3,7 @@ use std::sync::Arc;
use axum::{Extension, Json};
use foxchat::{
http::ApiError,
model::{http::guild::CreateGuildParams, user::PartialUser, Guild, channel::PartialChannel},
model::{channel::PartialChannel, http::guild::CreateGuildParams, user::PartialUser, Guild},
FoxError,
};
@ -39,8 +39,12 @@ pub async fn post_guilds(
instance: user.instance.domain,
},
default_channel: PartialChannel {
id: channel.id.0.clone(),
name: channel.name.clone(),
},
channels: Some(vec![PartialChannel {
id: channel.id.0,
name: channel.name,
}
}]),
}))
}

View file

@ -8,7 +8,7 @@ use axum::{
response::Response,
Extension,
};
use eyre::Result;
use eyre::{eyre, Error, Result};
use foxchat::{
s2s::{Dispatch, Payload},
signature::{parse_date, verify_signature},
@ -169,6 +169,28 @@ async fn read(
return;
};
let Ok(msg) = msg.to_text() else {
tx.send(Payload::Error {
message: "Invalid message".into(),
})
.await
.ok();
return;
};
let Ok(msg) = serde_json::from_str::<Payload>(msg) else {
tx.send(Payload::Error {
message: "Invalid message".into(),
})
.await
.ok();
return;
};
match msg {
Payload::Connect { user_id } => {}
}
// TODO: handle incoming payloads
}
}
@ -317,5 +339,35 @@ async fn filter_events(
}
return Ok((false, vec![]));
}
Dispatch::Ready { user, guilds: _ } => {
let user = sqlx::query!(
"SELECT remote_user_id FROM users WHERE id = $1",
user.id.clone()
)
.fetch_one(&app_state.pool)
.await?;
return Ok((true, vec![user.remote_user_id]));
}
}
}
async fn collect_ready(
app_state: Arc<AppState>,
socket_state: Arc<RwLock<SocketState>>,
user_id: String,
) -> Result<Payload> {
let Some(instance) = &socket_state.read().await.instance else {
return Err(eyre!("instance was None when it shouldn't be"));
};
let user = sqlx::query!(
"SELECT * FROM users WHERE instance_id = $1 AND remote_user_id = $2",
instance.id,
user_id
)
.fetch_one(&app_state.pool)
.await?;
todo!()
}

View file

@ -1,13 +1,16 @@
use serde::{Deserialize, Serialize};
use crate::s2s::Dispatch;
#[derive(Debug, Serialize, Deserialize)]
#[serde(tag = "t", content = "d", rename_all = "SCREAMING_SNAKE_CASE")]
pub enum Payload {
#[serde(rename = "D")]
Dispatch {
#[serde(rename = "e")]
event: String,
event: Dispatch,
#[serde(rename = "s")]
server: String,
server_id: String,
},
Error {
message: String,

View file

@ -0,0 +1,3 @@
pub mod event;
pub use event::Payload;

View file

@ -8,7 +8,7 @@ pub struct Channel {
pub topic: Option<String>,
}
#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct PartialChannel {
pub id: String,
pub name: String,

View file

@ -1,11 +1,12 @@
use serde::{Serialize, Deserialize};
use super::{user::PartialUser, channel::PartialChannel};
use super::{channel::PartialChannel, user::PartialUser};
#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Guild {
pub id: String,
pub name: String,
pub owner: PartialUser,
pub default_channel: PartialChannel,
pub channels: Option<Vec<PartialChannel>>,
}

View file

@ -3,7 +3,7 @@ use serde::{Deserialize, Serialize};
use crate::{
id::GuildType,
model::{user::PartialUser, Message},
model::{user::PartialUser, Guild, Message, User},
Id,
};
@ -18,6 +18,10 @@ pub enum Dispatch {
content: Option<String>,
created_at: DateTime<Utc>,
},
Ready {
user: User,
guilds: Vec<Guild>,
}
}
impl Dispatch {