From ad247ca0f4b34e0fa2e6d9cebcfd223c83efc7ee Mon Sep 17 00:00:00 2001 From: sam Date: Sun, 25 Feb 2024 22:26:24 +0100 Subject: [PATCH] more things --- chat/src/http/api/guilds/create_guild.rs | 8 +++- chat/src/http/ws/mod.rs | 54 +++++++++++++++++++++++- foxchat/src/c2s/event.rs | 9 ++-- foxchat/src/c2s/mod.rs | 3 ++ foxchat/src/model/channel.rs | 2 +- foxchat/src/model/guild.rs | 5 ++- foxchat/src/s2s/dispatch.rs | 6 ++- 7 files changed, 77 insertions(+), 10 deletions(-) diff --git a/chat/src/http/api/guilds/create_guild.rs b/chat/src/http/api/guilds/create_guild.rs index d506707..c98065a 100644 --- a/chat/src/http/api/guilds/create_guild.rs +++ b/chat/src/http/api/guilds/create_guild.rs @@ -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, - } + }]), })) } diff --git a/chat/src/http/ws/mod.rs b/chat/src/http/ws/mod.rs index 1aa2065..a3557e1 100644 --- a/chat/src/http/ws/mod.rs +++ b/chat/src/http/ws/mod.rs @@ -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::(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, + socket_state: Arc>, + user_id: String, +) -> Result { + 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!() +} diff --git a/foxchat/src/c2s/event.rs b/foxchat/src/c2s/event.rs index 6181aba..f68cb28 100644 --- a/foxchat/src/c2s/event.rs +++ b/foxchat/src/c2s/event.rs @@ -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, @@ -19,4 +22,4 @@ pub enum Payload { Identify { token: String, }, -} \ No newline at end of file +} diff --git a/foxchat/src/c2s/mod.rs b/foxchat/src/c2s/mod.rs index e69de29..47b0373 100644 --- a/foxchat/src/c2s/mod.rs +++ b/foxchat/src/c2s/mod.rs @@ -0,0 +1,3 @@ +pub mod event; + +pub use event::Payload; diff --git a/foxchat/src/model/channel.rs b/foxchat/src/model/channel.rs index 65be52a..e5d5c8f 100644 --- a/foxchat/src/model/channel.rs +++ b/foxchat/src/model/channel.rs @@ -8,7 +8,7 @@ pub struct Channel { pub topic: Option, } -#[derive(Serialize, Deserialize, Debug)] +#[derive(Serialize, Deserialize, Debug, Clone)] pub struct PartialChannel { pub id: String, pub name: String, diff --git a/foxchat/src/model/guild.rs b/foxchat/src/model/guild.rs index 11c0159..7518f5e 100644 --- a/foxchat/src/model/guild.rs +++ b/foxchat/src/model/guild.rs @@ -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>, } diff --git a/foxchat/src/s2s/dispatch.rs b/foxchat/src/s2s/dispatch.rs index 3718331..7b30f0d 100644 --- a/foxchat/src/s2s/dispatch.rs +++ b/foxchat/src/s2s/dispatch.rs @@ -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, created_at: DateTime, }, + Ready { + user: User, + guilds: Vec, + } } impl Dispatch {