feat: add patch request to channels!

This commit is contained in:
Radical 2025-06-01 22:10:37 +02:00
parent 15eb102784
commit 41defc4a25
3 changed files with 178 additions and 8 deletions

View file

@ -1,3 +1,5 @@
//! `/api/v1/channels/{uuid}` Channel specific endpoints
pub mod messages;
pub mod socket;
@ -8,8 +10,9 @@ use crate::{
structs::{Channel, Member},
utils::{get_auth_header, global_checks},
};
use actix_web::{HttpRequest, HttpResponse, delete, get, web};
use actix_web::{delete, get, patch, web, HttpRequest, HttpResponse};
use uuid::Uuid;
use serde::Deserialize;
#[get("/{uuid}")]
pub async fn get(
@ -62,3 +65,80 @@ pub async fn delete(
Ok(HttpResponse::Ok().finish())
}
#[derive(Deserialize)]
struct NewInfo {
name: Option<String>,
description: Option<String>,
is_above: Option<String>,
}
/// `PATCH /api/v1/channels/{uuid}` Returns user with the given UUID
///
/// requires auth: yes
///
/// requires relation: yes
///
/// ### Request Example
/// All fields are optional and can be nulled/dropped if only changing 1 value
/// ```
/// json!({
/// "name": "gaming-chat",
/// "description": "Gaming related topics.",
/// "is_above": "398f6d7b-752c-4348-9771-fe6024adbfb1"
/// });
/// ```
///
/// ### Response Example
/// ```
/// json!({
/// uuid: "cdcac171-5add-4f88-9559-3a247c8bba2c",
/// guild_uuid: "383d2afa-082f-4dd3-9050-ca6ed91487b6",
/// name: "gaming-chat",
/// description: "Gaming related topics.",
/// is_above: "398f6d7b-752c-4348-9771-fe6024adbfb1",
/// permissions: {
/// role_uuid: "79cc0806-0f37-4a06-a468-6639c4311a2d",
/// permissions: 0
/// }
/// });
/// ```
/// NOTE: UUIDs in this response are made using `uuidgen`, UUIDs made by the actual backend will be UUIDv7 and have extractable timestamps
#[patch("/{uuid}")]
pub async fn patch(
req: HttpRequest,
path: web::Path<(Uuid,)>,
new_info: web::Json<NewInfo>,
data: web::Data<Data>,
) -> Result<HttpResponse, Error> {
let headers = req.headers();
let auth_header = get_auth_header(headers)?;
let channel_uuid = path.into_inner().0;
let mut conn = data.pool.get().await?;
let uuid = check_access_token(auth_header, &mut conn).await?;
global_checks(&data, uuid).await?;
let mut channel = Channel::fetch_one(&data, channel_uuid).await?;
Member::check_membership(&mut conn, uuid, channel.guild_uuid).await?;
if let Some(new_name) = &new_info.name {
channel.set_name(&data, new_name.to_string()).await?;
}
if let Some(new_description) = &new_info.description {
channel.set_description(&data, new_description.to_string()).await?;
}
if let Some(new_is_above) = &new_info.is_above {
channel.set_description(&data, new_is_above.to_string()).await?;
}
Ok(HttpResponse::Ok().json(channel))
}