feat: return guilds in READY event, dispatch MESSAGE_CREATE event

This commit is contained in:
sam 2024-02-26 17:05:48 +01:00
parent f7494034d5
commit 809af7e637
4 changed files with 44 additions and 19 deletions

View file

@ -4,8 +4,10 @@ use axum::{extract::Path, Extension, Json};
use eyre::Result; use eyre::Result;
use foxchat::{ use foxchat::{
http::ApiError, http::ApiError,
model::{http::channel::CreateMessageParams, Message, user::PartialUser}, id::ChannelType,
FoxError, ulid_timestamp, id::ChannelType, Id, model::{http::channel::CreateMessageParams, user::PartialUser, Message},
s2s::Dispatch,
ulid_timestamp, FoxError, Id,
}; };
use crate::{ use crate::{
@ -26,13 +28,28 @@ pub async fn post_messages(
let mut tx = state.pool.begin().await?; let mut tx = state.pool.begin().await?;
let channel = get_channel(&mut *tx, &channel_id).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?; let message = create_message(&mut *tx, &channel.id, &user.id, params).await?;
tx.commit().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 { Ok(Json(Message {
id: message.id.0.clone(), id: message.id.0.clone(),

View file

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

View file

@ -10,7 +10,7 @@ use axum::{
}; };
use eyre::{eyre, Result}; use eyre::{eyre, Result};
use foxchat::{ use foxchat::{
model::User, model::{Guild, User},
s2s::{Dispatch, Payload}, s2s::{Dispatch, Payload},
signature::{parse_date, verify_signature}, signature::{parse_date, verify_signature},
}; };
@ -448,6 +448,23 @@ async fn collect_ready(
.fetch_one(&app_state.pool) .fetch_one(&app_state.pool)
.await?; .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 { Ok(Payload::Dispatch {
event: Dispatch::Ready { event: Dispatch::Ready {
user: User { user: User {
@ -456,7 +473,7 @@ async fn collect_ready(
instance: user.domain, instance: user.domain,
avatar_url: None, avatar_url: None,
}, },
guilds: vec![], guilds,
}, },
recipients: vec![user.remote_user_id], recipients: vec![user.remote_user_id],
}) })

View file

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