added roominvites in frontend in a notifications page and added clap to define backend port

This commit is contained in:
2025-12-31 12:14:46 +01:00
parent bd99c82d2d
commit 6bd1921806
5 changed files with 177 additions and 14 deletions

121
Cargo.lock generated
View File

@@ -26,6 +26,56 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "anstream"
version = "0.6.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "43d5b281e737544384e969a5ccad3f1cdd24b48086a0fc1b2a5262a26b8f4f4a"
dependencies = [
"anstyle",
"anstyle-parse",
"anstyle-query",
"anstyle-wincon",
"colorchoice",
"is_terminal_polyfill",
"utf8parse",
]
[[package]]
name = "anstyle"
version = "1.0.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5192cca8006f1fd4f7237516f40fa183bb07f8fbdfedaa0036de5ea9b0b45e78"
[[package]]
name = "anstyle-parse"
version = "0.2.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4e7644824f0aa2c7b9384579234ef10eb7efb6a0deb83f9630a49594dd9c15c2"
dependencies = [
"utf8parse",
]
[[package]]
name = "anstyle-query"
version = "1.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc"
dependencies = [
"windows-sys 0.61.2",
]
[[package]]
name = "anstyle-wincon"
version = "3.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d"
dependencies = [
"anstyle",
"once_cell_polyfill",
"windows-sys 0.61.2",
]
[[package]] [[package]]
name = "anyhow" name = "anyhow"
version = "1.0.100" version = "1.0.100"
@@ -213,6 +263,7 @@ dependencies = [
"argon2", "argon2",
"axum", "axum",
"chrono", "chrono",
"clap",
"dashmap", "dashmap",
"jsonwebtoken", "jsonwebtoken",
"password-hash", "password-hash",
@@ -242,6 +293,52 @@ dependencies = [
"windows-link", "windows-link",
] ]
[[package]]
name = "clap"
version = "4.5.53"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c9e340e012a1bf4935f5282ed1436d1489548e8f72308207ea5df0e23d2d03f8"
dependencies = [
"clap_builder",
"clap_derive",
]
[[package]]
name = "clap_builder"
version = "4.5.53"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d76b5d13eaa18c901fd2f7fca939fefe3a0727a953561fefdf3b2922b8569d00"
dependencies = [
"anstream",
"anstyle",
"clap_lex",
"strsim",
]
[[package]]
name = "clap_derive"
version = "4.5.49"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2a0b5487afeab2deb2ff4e03a807ad1a03ac532ff5a2cee5d86884440c7f7671"
dependencies = [
"heck",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "clap_lex"
version = "0.7.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1d728cc89cf3aee9ff92b05e62b19ee65a02b5702cff7d5a377e32c6ae29d8d"
[[package]]
name = "colorchoice"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b05b61dc5112cbb17e4b6cd61790d9845d13888356391624cbe7e41efeac1e75"
[[package]] [[package]]
name = "concurrent-queue" name = "concurrent-queue"
version = "2.5.0" version = "2.5.0"
@@ -993,6 +1090,12 @@ dependencies = [
"hashbrown 0.16.1", "hashbrown 0.16.1",
] ]
[[package]]
name = "is_terminal_polyfill"
version = "1.70.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695"
[[package]] [[package]]
name = "itoa" name = "itoa"
version = "1.0.15" version = "1.0.15"
@@ -1255,6 +1358,12 @@ version = "1.21.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d" checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d"
[[package]]
name = "once_cell_polyfill"
version = "1.70.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe"
[[package]] [[package]]
name = "openssl" name = "openssl"
version = "0.10.75" version = "0.10.75"
@@ -2087,6 +2196,12 @@ dependencies = [
"unicode-properties", "unicode-properties",
] ]
[[package]]
name = "strsim"
version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
[[package]] [[package]]
name = "subtle" name = "subtle"
version = "2.6.1" version = "2.6.1"
@@ -2539,6 +2654,12 @@ version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be" checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be"
[[package]]
name = "utf8parse"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
[[package]] [[package]]
name = "uuid" name = "uuid"
version = "1.19.0" version = "1.19.0"

View File

@@ -8,6 +8,7 @@ anyhow = "1.0.99"
argon2 = "0.5.3" argon2 = "0.5.3"
axum = { version = "0.8.4", features = ["multipart", "ws"] } axum = { version = "0.8.4", features = ["multipart", "ws"] }
chrono = { version = "0.4.42", features = ["serde"] } chrono = { version = "0.4.42", features = ["serde"] }
clap = { version = "4.5.53", features = ["derive"] }
dashmap = "6.1.0" dashmap = "6.1.0"
jsonwebtoken = "9.3.1" jsonwebtoken = "9.3.1"
password-hash = "0.5.0" password-hash = "0.5.0"

View File

@@ -20,8 +20,7 @@ pub async fn room_id_from_uuid(db: &PgPool, room_uuid: Uuid) -> Result<i32, (Sta
.bind(room_uuid) .bind(room_uuid)
.fetch_one(db) .fetch_one(db)
.await .await
// FIX: hmm probably the wrong error here .map_err(|_| (StatusCode::NOT_FOUND, "Failed to find room".into()))
.map_err(|_| (StatusCode::UNAUTHORIZED, String::from("Wrong token")))
} }
pub async fn username_from_uuid( pub async fn username_from_uuid(
@@ -50,3 +49,14 @@ pub async fn id_from_username(db: &PgPool, username: String) -> Result<i32, (Sta
.await .await
.map_err(|_| (StatusCode::NOT_FOUND, "User not found".into())) .map_err(|_| (StatusCode::NOT_FOUND, "User not found".into()))
} }
pub async fn room_name_from_uuid(
db: &PgPool,
room_uuid: Uuid,
) -> Result<String, (StatusCode, String)> {
sqlx::query_scalar("SELECT name FROM room_ WHERE uuid = $1")
.bind(room_uuid)
.fetch_one(db)
.await
.map_err(|_| (StatusCode::NOT_FOUND, "Failed to find room".into()))
}

View File

@@ -2,17 +2,36 @@ use axum::{
Extension, Router, Extension, Router,
http::{Method, header}, http::{Method, header},
}; };
use std::{env::var, net::SocketAddr, time::Duration}; use clap::Parser;
use std::{net::SocketAddr, time::Duration};
use tower_governor::{GovernorLayer, governor::GovernorConfigBuilder}; use tower_governor::{GovernorLayer, governor::GovernorConfigBuilder};
use tower_http::cors::{Any, CorsLayer}; use tower_http::{
cors::{Any, CorsLayer},
trace::{DefaultMakeSpan, DefaultOnResponse, TraceLayer},
};
use tracing::Level;
mod auth; mod auth;
mod db; mod db;
mod realtime; mod realtime;
mod routes; mod routes;
#[derive(clap::Parser, Debug)]
#[command(author, version, about, long_about = None)]
pub struct Cli {
/// Server port
#[arg(short, long, default_value = "8080")]
port: String,
/// Verbose mode
#[arg(short, long)]
verbose: bool,
}
#[tokio::main] #[tokio::main]
async fn main() -> anyhow::Result<()> { async fn main() -> anyhow::Result<()> {
let cli = Cli::parse();
tracing_subscriber::fmt() tracing_subscriber::fmt()
.with_max_level(tracing::Level::INFO) .with_max_level(tracing::Level::INFO)
.init(); .init();
@@ -45,7 +64,7 @@ async fn main() -> anyhow::Result<()> {
let realtime = realtime::Realtime::new(); let realtime = realtime::Realtime::new();
let app = Router::new() let mut app = Router::new()
.merge(routes::users::routes()) .merge(routes::users::routes())
.merge(routes::rooms::routes()) .merge(routes::rooms::routes())
.merge(routes::messages::routes()) .merge(routes::messages::routes())
@@ -54,14 +73,17 @@ async fn main() -> anyhow::Result<()> {
.layer(Extension(db_pool)) .layer(Extension(db_pool))
.layer(Extension(realtime)) .layer(Extension(realtime))
.layer(GovernorLayer::new(governor_conf)) .layer(GovernorLayer::new(governor_conf))
// .layer(
// TraceLayer::new_for_http()
// .make_span_with(DefaultMakeSpan::new().level(Level::INFO))
// .on_response(DefaultOnResponse::new().level(Level::INFO)),
// )
.layer(cors); .layer(cors);
let port = var("CHATAPP_SERVER_PORT").unwrap_or_else(|_| "8080".to_string()); if cli.verbose {
app = app.layer(
TraceLayer::new_for_http()
.make_span_with(DefaultMakeSpan::new().level(Level::INFO))
.on_response(DefaultOnResponse::new().level(Level::INFO)),
);
}
let port = cli.port;
let addr = format!("0.0.0.0:{port}"); let addr = format!("0.0.0.0:{port}");
let listener = tokio::net::TcpListener::bind(&addr).await.unwrap(); let listener = tokio::net::TcpListener::bind(&addr).await.unwrap();

View File

@@ -7,7 +7,7 @@ use axum::{
use sqlx::{PgPool, Pool, Postgres}; use sqlx::{PgPool, Pool, Postgres};
use uuid::Uuid; use uuid::Uuid;
use crate::db::{id_from_username, user_id_from_uuid, username_from_id}; use crate::db::{id_from_username, room_name_from_uuid, user_id_from_uuid, username_from_id};
use crate::{auth::verify_jwt, db::room_id_from_uuid}; use crate::{auth::verify_jwt, db::room_id_from_uuid};
#[derive(sqlx::FromRow, serde::Serialize)] #[derive(sqlx::FromRow, serde::Serialize)]
@@ -27,6 +27,7 @@ pub struct NewRoomPayload {
#[derive(sqlx::FromRow, serde::Serialize)] #[derive(sqlx::FromRow, serde::Serialize)]
pub struct RoomInvite { pub struct RoomInvite {
pub room_uuid: Uuid, pub room_uuid: Uuid,
pub room_name: String,
pub sender_uuid: Uuid, pub sender_uuid: Uuid,
pub sender_username: String, pub sender_username: String,
} }
@@ -197,7 +198,11 @@ async fn list_invites(
let requests = sqlx::query_as::<_, RoomInvite>( let requests = sqlx::query_as::<_, RoomInvite>(
r#" r#"
SELECT r.uuid AS room_uuid, u.uuid AS sender_uuid, u.username AS sender_username SELECT
r.uuid AS room_uuid,
r.name AS room_name,
u.uuid AS sender_uuid,
u.username AS sender_username
FROM room_invite_ AS i FROM room_invite_ AS i
JOIN user_ u ON u.id = i.sender JOIN user_ u ON u.id = i.sender
JOIN room_ r ON r.id = i.room JOIN room_ r ON r.id = i.room
@@ -207,7 +212,8 @@ async fn list_invites(
.bind(user_id) .bind(user_id)
.fetch_all(&db) .fetch_all(&db)
.await .await
.map_err(|_| { .map_err(|e| {
tracing::error!("{e}");
( (
StatusCode::INTERNAL_SERVER_ERROR, StatusCode::INTERNAL_SERVER_ERROR,
"Could not list room invites".into(), "Could not list room invites".into(),
@@ -267,10 +273,13 @@ async fn send_invite(
tracing::info!("bro"); tracing::info!("bro");
let room_name = room_name_from_uuid(&db, payload.room_uuid).await?;
Ok(( Ok((
StatusCode::CREATED, StatusCode::CREATED,
Json(RoomInvite { Json(RoomInvite {
room_uuid: payload.room_uuid, room_uuid: payload.room_uuid,
room_name,
sender_uuid: claims.sub, sender_uuid: claims.sub,
sender_username: username_from_id(&db, receiver_id).await?, sender_username: username_from_id(&db, receiver_id).await?,
}), }),