feat: add patch request to channels!
This commit is contained in:
parent
15eb102784
commit
41defc4a25
3 changed files with 178 additions and 8 deletions
|
@ -1,3 +1,5 @@
|
||||||
|
//! `/api/v1/channels/{uuid}` Channel specific endpoints
|
||||||
|
|
||||||
pub mod messages;
|
pub mod messages;
|
||||||
pub mod socket;
|
pub mod socket;
|
||||||
|
|
||||||
|
@ -8,8 +10,9 @@ use crate::{
|
||||||
structs::{Channel, Member},
|
structs::{Channel, Member},
|
||||||
utils::{get_auth_header, global_checks},
|
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 uuid::Uuid;
|
||||||
|
use serde::Deserialize;
|
||||||
|
|
||||||
#[get("/{uuid}")]
|
#[get("/{uuid}")]
|
||||||
pub async fn get(
|
pub async fn get(
|
||||||
|
@ -62,3 +65,80 @@ pub async fn delete(
|
||||||
|
|
||||||
Ok(HttpResponse::Ok().finish())
|
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))
|
||||||
|
}
|
||||||
|
|
||||||
|
|
101
src/structs.rs
101
src/structs.rs
|
@ -24,13 +24,9 @@ use url::Url;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
Conn, Data,
|
error::Error, schema::*, utils::{
|
||||||
error::Error,
|
generate_refresh_token, global_checks, image_check, order_by_is_above, user_uuid_from_identifier, CHANNEL_REGEX, EMAIL_REGEX, PASSWORD_REGEX, USERNAME_REGEX
|
||||||
schema::*,
|
}, Conn, Data
|
||||||
utils::{
|
|
||||||
EMAIL_REGEX, PASSWORD_REGEX, USERNAME_REGEX, generate_refresh_token, global_checks,
|
|
||||||
image_check, order_by_is_above, user_uuid_from_identifier,
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
pub trait HasUuid {
|
pub trait HasUuid {
|
||||||
|
@ -231,6 +227,10 @@ impl Channel {
|
||||||
name: String,
|
name: String,
|
||||||
description: Option<String>,
|
description: Option<String>,
|
||||||
) -> Result<Self, Error> {
|
) -> Result<Self, Error> {
|
||||||
|
if !CHANNEL_REGEX.is_match(&name) {
|
||||||
|
return Err(Error::BadRequest("Channel name is invalid".to_string()))
|
||||||
|
}
|
||||||
|
|
||||||
let mut conn = data.pool.get().await?;
|
let mut conn = data.pool.get().await?;
|
||||||
|
|
||||||
let channel_uuid = Uuid::now_v7();
|
let channel_uuid = Uuid::now_v7();
|
||||||
|
@ -353,6 +353,93 @@ impl Channel {
|
||||||
|
|
||||||
message.build(data).await
|
message.build(data).await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn set_name(&mut self, data: &Data, new_name: String) -> Result<(), Error> {
|
||||||
|
if !CHANNEL_REGEX.is_match(&new_name) {
|
||||||
|
return Err(Error::BadRequest("Channel name is invalid".to_string()))
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut conn = data.pool.get().await?;
|
||||||
|
|
||||||
|
use channels::dsl;
|
||||||
|
update(channels::table)
|
||||||
|
.filter(dsl::uuid.eq(self.uuid))
|
||||||
|
.set(dsl::name.eq(&new_name))
|
||||||
|
.execute(&mut conn)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
self.name = new_name;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn set_description(&mut self, data: &Data, new_description: String) -> Result<(), Error> {
|
||||||
|
let mut conn = data.pool.get().await?;
|
||||||
|
|
||||||
|
use channels::dsl;
|
||||||
|
update(channels::table)
|
||||||
|
.filter(dsl::uuid.eq(self.uuid))
|
||||||
|
.set(dsl::description.eq(&new_description))
|
||||||
|
.execute(&mut conn)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
self.description = Some(new_description);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn move_channel(&mut self, data: &Data, new_is_above: Uuid) -> Result<(), Error> {
|
||||||
|
let mut conn = data.pool.get().await?;
|
||||||
|
|
||||||
|
use channels::dsl;
|
||||||
|
let old_above_uuid: Option<Uuid> = match dsl::channels
|
||||||
|
.filter(dsl::is_above.eq(self.uuid))
|
||||||
|
.select(dsl::uuid)
|
||||||
|
.get_result(&mut conn)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(r) => Ok(Some(r)),
|
||||||
|
Err(e) if e == diesel::result::Error::NotFound => Ok(None),
|
||||||
|
Err(e) => Err(e),
|
||||||
|
}?;
|
||||||
|
|
||||||
|
if let Some(uuid) = old_above_uuid {
|
||||||
|
update(channels::table)
|
||||||
|
.filter(dsl::uuid.eq(uuid))
|
||||||
|
.set(dsl::is_above.eq(None::<Uuid>))
|
||||||
|
.execute(&mut conn)
|
||||||
|
.await?;
|
||||||
|
}
|
||||||
|
|
||||||
|
match update(channels::table)
|
||||||
|
.filter(dsl::is_above.eq(new_is_above))
|
||||||
|
.set(dsl::is_above.eq(self.uuid))
|
||||||
|
.execute(&mut conn)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(r) => Ok(r),
|
||||||
|
Err(e) if e == diesel::result::Error::NotFound => Ok(0),
|
||||||
|
Err(e) => Err(e),
|
||||||
|
}?;
|
||||||
|
|
||||||
|
update(channels::table)
|
||||||
|
.filter(dsl::uuid.eq(self.uuid))
|
||||||
|
.set(dsl::is_above.eq(new_is_above))
|
||||||
|
.execute(&mut conn)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
if let Some(uuid) = old_above_uuid {
|
||||||
|
update(channels::table)
|
||||||
|
.filter(dsl::uuid.eq(uuid))
|
||||||
|
.set(dsl::is_above.eq(self.is_above))
|
||||||
|
.execute(&mut conn)
|
||||||
|
.await?;
|
||||||
|
}
|
||||||
|
|
||||||
|
self.is_above = Some(new_is_above);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Copy)]
|
#[derive(Clone, Copy)]
|
||||||
|
|
|
@ -30,6 +30,9 @@ pub static EMAIL_REGEX: LazyLock<Regex> = LazyLock::new(|| {
|
||||||
pub static USERNAME_REGEX: LazyLock<Regex> =
|
pub static USERNAME_REGEX: LazyLock<Regex> =
|
||||||
LazyLock::new(|| Regex::new(r"^[a-z0-9_.-]+$").unwrap());
|
LazyLock::new(|| Regex::new(r"^[a-z0-9_.-]+$").unwrap());
|
||||||
|
|
||||||
|
pub static CHANNEL_REGEX: LazyLock<Regex> =
|
||||||
|
LazyLock::new(|| Regex::new(r"^[a-z0-9_.-]+$").unwrap());
|
||||||
|
|
||||||
// Password is expected to be hashed using SHA3-384
|
// Password is expected to be hashed using SHA3-384
|
||||||
pub static PASSWORD_REGEX: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"[0-9a-f]{96}").unwrap());
|
pub static PASSWORD_REGEX: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"[0-9a-f]{96}").unwrap());
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue