fixed room creation not giving membership

This commit is contained in:
2025-12-15 15:02:36 +01:00
parent 0365c7978c
commit 5ec961255d
3 changed files with 27 additions and 5 deletions

View File

@@ -23,3 +23,14 @@ pub async fn room_id_from_uuid(db: &PgPool, room_uuid: Uuid) -> Result<i32, (Sta
// FIX: hmm probably the wrong error here
.map_err(|_| (StatusCode::UNAUTHORIZED, String::from("Wrong token")))
}
pub async fn username_from_uuid(
db: &PgPool,
user_uuid: Uuid,
) -> Result<String, (StatusCode, String)> {
sqlx::query_scalar("SELECT username FROM user_ WHERE uuid = $1")
.bind(user_uuid)
.fetch_one(db)
.await
.map_err(|_| (StatusCode::UNAUTHORIZED, String::from("Wrong token")))
}

View File

@@ -7,12 +7,12 @@ use axum::{
use sqlx::PgPool;
use uuid::Uuid;
use crate::db::user_id_from_uuid;
use crate::db::{user_id_from_uuid, username_from_uuid};
use crate::{auth::verify_jwt, db::room_id_from_uuid};
#[derive(sqlx::FromRow, serde::Serialize, Debug)]
pub struct Message {
pub sender: Uuid,
pub sender: String,
pub message_type: String,
pub content: String,
}
@@ -57,7 +57,7 @@ async fn list_messages(
let messages = sqlx::query_as::<_, Message>(
r#"
SELECT
u.uuid AS sender,
u.username AS sender,
r.uuid AS room,
m.type AS message_type,
m.content
@@ -106,10 +106,12 @@ async fn create_message(
.await
.map_err(|_| (StatusCode::BAD_REQUEST, format!("Could not create message")))?;
let sender_name = username_from_uuid(&db, claims.sub).await?;
Ok((
StatusCode::CREATED,
Json(Message {
sender: claims.sub,
sender: sender_name,
message_type: payload.message_type,
content: payload.content,
}),

View File

@@ -7,8 +7,8 @@ use axum::{
use sqlx::PgPool;
use uuid::Uuid;
use crate::auth::verify_jwt;
use crate::db::user_id_from_uuid;
use crate::{auth::verify_jwt, db::room_id_from_uuid};
#[derive(sqlx::FromRow, serde::Serialize)]
pub struct Room {
@@ -75,6 +75,15 @@ async fn create_room(
.await
.map_err(|_| (StatusCode::BAD_REQUEST, format!("Could not create room")))?;
let room_id = room_id_from_uuid(&db, room_uuid).await?;
sqlx::query("INSERT INTO membership_ (user_id, room) VALUES ($1, $2)")
.bind(user_id)
.bind(room_id)
.execute(&db)
.await
.map_err(|_| (StatusCode::BAD_REQUEST, format!("Could not create room")))?;
Ok((
StatusCode::CREATED,
Json(Room {