diff --git a/chat/src/http/api/channels/messages.rs b/chat/src/http/api/channels/messages.rs index 03de706..3f77869 100644 --- a/chat/src/http/api/channels/messages.rs +++ b/chat/src/http/api/channels/messages.rs @@ -4,8 +4,10 @@ use axum::{extract::Path, Extension, Json}; use eyre::Result; use foxchat::{ http::ApiError, - model::{http::channel::CreateMessageParams, Message, user::PartialUser}, - FoxError, ulid_timestamp, id::ChannelType, Id, + id::ChannelType, + model::{http::channel::CreateMessageParams, user::PartialUser, Message}, + s2s::Dispatch, + ulid_timestamp, FoxError, Id, }; use crate::{ @@ -26,13 +28,28 @@ pub async fn post_messages( let mut tx = state.pool.begin().await?; let channel = get_channel(&mut *tx, &channel_id).await?; - let _guild = get_guild(&mut *tx, &channel.guild_id, &user.id).await?; + let guild = get_guild(&mut *tx, &channel.guild_id, &user.id).await?; let message = create_message(&mut *tx, &channel.id, &user.id, params).await?; tx.commit().await?; - // TODO: dispatch message create event + // Dispatch message create event + // Not being able to send the event (when there are no subscribers) isn't an error, so we just ignore the error entirely. + state.broadcast.send(Dispatch::message_create( + Message { + id: message.id.0.clone(), + channel_id: channel_id.0.clone(), + author: PartialUser { + id: user.id.0.clone(), + username: user.username.clone(), + instance: request.instance.domain.clone(), + }, + content: Some(message.content.clone()), + created_at: ulid_timestamp(&message.id.0), + }, + guild.id, + )).ok(); Ok(Json(Message { id: message.id.0.clone(), diff --git a/chat/src/http/api/guilds/create_guild.rs b/chat/src/http/api/guilds/create_guild.rs index c98065a..c23a9b4 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::{channel::PartialChannel, http::guild::CreateGuildParams, user::PartialUser, Guild}, + model::{channel::PartialChannel, http::guild::CreateGuildParams, Guild}, FoxError, }; @@ -33,15 +33,7 @@ pub async fn post_guilds( Ok(Json(Guild { id: guild.id.0, name: guild.name, - owner: PartialUser { - id: user.id.0, - username: user.username, - instance: user.instance.domain, - }, - default_channel: PartialChannel { - id: channel.id.0.clone(), - name: channel.name.clone(), - }, + owner_ids: vec![user.id.0], 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 e38eb4c..9466d93 100644 --- a/chat/src/http/ws/mod.rs +++ b/chat/src/http/ws/mod.rs @@ -10,7 +10,7 @@ use axum::{ }; use eyre::{eyre, Result}; use foxchat::{ - model::User, + model::{Guild, User}, s2s::{Dispatch, Payload}, signature::{parse_date, verify_signature}, }; @@ -448,6 +448,23 @@ async fn collect_ready( .fetch_one(&app_state.pool) .await?; + // TODO: also return channels + let guilds = sqlx::query!( + r#"SELECT g.* FROM guilds g JOIN guilds_users gu ON gu.guild_id = g.id + WHERE gu.user_id = $1"#, + user_id + ) + .fetch_all(&app_state.pool) + .await? + .iter() + .map(|r| Guild { + id: r.id.clone(), + name: r.name.clone(), + owner_ids: vec![r.owner_id.clone()], + channels: None, + }) + .collect(); + Ok(Payload::Dispatch { event: Dispatch::Ready { user: User { @@ -456,7 +473,7 @@ async fn collect_ready( instance: user.domain, avatar_url: None, }, - guilds: vec![], + guilds, }, recipients: vec![user.remote_user_id], }) diff --git a/foxchat/src/model/guild.rs b/foxchat/src/model/guild.rs index 7518f5e..84b141e 100644 --- a/foxchat/src/model/guild.rs +++ b/foxchat/src/model/guild.rs @@ -1,12 +1,11 @@ use serde::{Serialize, Deserialize}; -use super::{channel::PartialChannel, user::PartialUser}; +use super::channel::PartialChannel; #[derive(Serialize, Deserialize, Debug, Clone)] pub struct Guild { pub id: String, pub name: String, - pub owner: PartialUser, - pub default_channel: PartialChannel, + pub owner_ids: Vec, pub channels: Option>, }