feat: make identity server store user instance/guild IDs

This commit is contained in:
sam 2024-03-04 15:59:07 +01:00
parent 42abd70184
commit fd77dd01fa
Signed by: sam
GPG key ID: B4EF20DDE721CAA1
19 changed files with 245 additions and 29 deletions

View file

@ -1,3 +1,7 @@
mod rest;
pub use rest::post_rest_event;
use std::sync::Arc;
use axum::{

38
chat/src/fed/rest.rs Normal file
View file

@ -0,0 +1,38 @@
use std::sync::Arc;
use eyre::Result;
use foxchat::{fed, s2s::http::RestEvent};
use tracing::{debug, error};
use crate::{app_state::AppState, model::identity_instance::IdentityInstance};
/// Posts an event to a remote instance's inbox.
pub async fn post_rest_event(
state: Arc<AppState>,
instance: &IdentityInstance,
event: RestEvent,
) -> Result<()> {
debug!("Sending {:?} event to {}'s inbox", &event, &instance.domain);
match fed::post::<RestEvent, ()>(
&state.private_key,
&state.config.domain,
&instance.domain,
"/_fox/ident/inbox",
None,
&event,
)
.await
{
Ok(_) => Ok(()),
Err(e) => {
error!(
"Error sending {:?} event to {}'s inbox: {}",
&event, &instance.domain, e
);
return Err(e);
}
}
}

View file

@ -4,13 +4,17 @@ use axum::{Extension, Json};
use foxchat::{
http::ApiError,
model::{channel::PartialChannel, http::guild::CreateGuildParams, Guild},
s2s::http::RestEvent,
FoxError,
};
use crate::{
app_state::AppState,
db::{channel::create_channel, guild::{create_guild, join_guild}},
fed::FoxRequestData,
db::{
channel::create_channel,
guild::{create_guild, join_guild},
},
fed::{post_rest_event, FoxRequestData},
model::user::User,
};
@ -27,9 +31,20 @@ pub async fn post_guilds(
let channel = create_channel(&mut *tx, &guild.id, "general", None).await?;
join_guild(&mut *tx, &guild.id, &user.id).await?;
tx.commit().await?;
// Send an event to the user's instance that they joined a guild
post_rest_event(
state,
&request.instance,
RestEvent::GuildJoin {
guild_id: guild.id.0.clone(),
user_id: user.remote_user_id.clone(),
},
)
.await
.ok();
Ok(Json(Guild {
id: guild.id.0,
name: guild.name,

View file

@ -3,6 +3,7 @@ use std::sync::Arc;
use axum::{Extension, Json};
use eyre::Context;
use foxchat::{
error::ToFoxError,
fed,
http::ApiError,
s2s::http::{HelloRequest, HelloResponse, NodeResponse},
@ -30,7 +31,8 @@ pub async fn post_hello(
"/_fox/ident/node",
None,
)
.await?;
.await?
.to_fox_error()?;
let public_key =
RsaPublicKey::from_pkcs1_pem(&node.public_key).wrap_err("parsing remote public key")?;

View file

@ -1,7 +1,7 @@
use std::sync::Arc;
use eyre::Result;
use foxchat::{fed, model::User as HttpUser, Id, id::UserType};
use foxchat::{error::ToFoxError, fed, id::UserType, model::User as HttpUser, Id};
use ulid::Ulid;
use crate::app_state::AppState;
@ -48,7 +48,8 @@ impl User {
&format!("/_fox/ident/users/{}", remote_id),
None,
)
.await?;
.await?
.to_fox_error()?;
let user = sqlx::query!(
"insert into users (id, instance_id, remote_user_id, username, avatar) values ($1, $2, $3, $4, $5) returning *",