feat: return guilds in READY event, dispatch MESSAGE_CREATE event
This commit is contained in:
parent
f7494034d5
commit
809af7e637
4 changed files with 44 additions and 19 deletions
|
@ -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(),
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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],
|
||||
})
|
||||
|
|
|
@ -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<String>,
|
||||
pub channels: Option<Vec<PartialChannel>>,
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue