Compare commits
3 commits
643f94b580
...
c4fc23ec85
Author | SHA1 | Date | |
---|---|---|---|
c4fc23ec85 | |||
41defc4a25 | |||
15eb102784 |
8 changed files with 217 additions and 9 deletions
|
@ -8,6 +8,12 @@ strip = true
|
|||
lto = true
|
||||
codegen-units = 1
|
||||
|
||||
# Speed up compilation to make dev bearable
|
||||
[profile.dev]
|
||||
debug = 0
|
||||
strip = "debuginfo"
|
||||
codegen-units = 512
|
||||
|
||||
[dependencies]
|
||||
actix-cors = "0.7.1"
|
||||
actix-web = "4.11"
|
||||
|
@ -32,7 +38,7 @@ futures-util = "0.3.31"
|
|||
bunny-api-tokio = "0.3.0"
|
||||
bindet = "0.3.2"
|
||||
deadpool = "0.12"
|
||||
diesel = { version = "2.2", features = ["uuid", "chrono"] }
|
||||
diesel = { version = "2.2", features = ["uuid", "chrono"], default-features = false }
|
||||
diesel-async = { version = "0.5", features = ["deadpool", "postgres", "async-connection-wrapper"] }
|
||||
diesel_migrations = { version = "2.2.0", features = ["postgres"] }
|
||||
thiserror = "2.0.12"
|
||||
|
|
2
migrations/2025-06-01-143713_add_about_to_users/down.sql
Normal file
2
migrations/2025-06-01-143713_add_about_to_users/down.sql
Normal file
|
@ -0,0 +1,2 @@
|
|||
-- This file should undo anything in `up.sql`
|
||||
ALTER TABLE users DROP COLUMN about;
|
2
migrations/2025-06-01-143713_add_about_to_users/up.sql
Normal file
2
migrations/2025-06-01-143713_add_about_to_users/up.sql
Normal file
|
@ -0,0 +1,2 @@
|
|||
-- Your SQL goes here
|
||||
ALTER TABLE users ADD COLUMN about VARCHAR(200) DEFAULT NULL;
|
|
@ -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))
|
||||
}
|
||||
|
||||
|
|
|
@ -41,6 +41,7 @@ struct NewInfo {
|
|||
//password: Option<String>, will probably be handled through a reset password link
|
||||
email: Option<String>,
|
||||
pronouns: Option<String>,
|
||||
about: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, MultipartForm)]
|
||||
|
@ -102,5 +103,9 @@ pub async fn update(
|
|||
me.set_pronouns(&data, pronouns.clone()).await?;
|
||||
}
|
||||
|
||||
if let Some(about) = &form.json.about {
|
||||
me.set_about(&data, about.clone()).await?;
|
||||
}
|
||||
|
||||
Ok(HttpResponse::Ok().finish())
|
||||
}
|
||||
|
|
|
@ -146,6 +146,8 @@ diesel::table! {
|
|||
avatar -> Nullable<Varchar>,
|
||||
#[max_length = 32]
|
||||
pronouns -> Nullable<Varchar>,
|
||||
#[max_length = 200]
|
||||
about -> Nullable<Varchar>,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
122
src/structs.rs
122
src/structs.rs
|
@ -24,13 +24,9 @@ use url::Url;
|
|||
use uuid::Uuid;
|
||||
|
||||
use crate::{
|
||||
Conn, Data,
|
||||
error::Error,
|
||||
schema::*,
|
||||
utils::{
|
||||
EMAIL_REGEX, PASSWORD_REGEX, USERNAME_REGEX, generate_refresh_token, global_checks,
|
||||
image_check, order_by_is_above, user_uuid_from_identifier,
|
||||
},
|
||||
error::Error, schema::*, utils::{
|
||||
generate_refresh_token, global_checks, image_check, order_by_is_above, user_uuid_from_identifier, CHANNEL_REGEX, EMAIL_REGEX, PASSWORD_REGEX, USERNAME_REGEX
|
||||
}, Conn, Data
|
||||
};
|
||||
|
||||
pub trait HasUuid {
|
||||
|
@ -231,6 +227,10 @@ impl Channel {
|
|||
name: String,
|
||||
description: Option<String>,
|
||||
) -> 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 channel_uuid = Uuid::now_v7();
|
||||
|
@ -353,6 +353,93 @@ impl Channel {
|
|||
|
||||
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)]
|
||||
|
@ -866,6 +953,7 @@ pub struct User {
|
|||
display_name: Option<String>,
|
||||
avatar: Option<String>,
|
||||
pronouns: Option<String>,
|
||||
about: Option<String>,
|
||||
}
|
||||
|
||||
impl User {
|
||||
|
@ -917,6 +1005,7 @@ pub struct Me {
|
|||
display_name: Option<String>,
|
||||
avatar: Option<String>,
|
||||
pronouns: Option<String>,
|
||||
about: Option<String>,
|
||||
email: String,
|
||||
pub email_verified: bool,
|
||||
}
|
||||
|
@ -1111,6 +1200,25 @@ impl Me {
|
|||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn set_about(&mut self, data: &Data, new_about: String) -> Result<(), Error> {
|
||||
let mut conn = data.pool.get().await?;
|
||||
|
||||
use users::dsl;
|
||||
update(users::table)
|
||||
.filter(dsl::uuid.eq(self.uuid))
|
||||
.set((
|
||||
dsl::about.eq(new_about.as_str()),
|
||||
))
|
||||
.execute(&mut conn)
|
||||
.await?;
|
||||
|
||||
if data.get_cache_key(self.uuid.to_string()).await.is_ok() {
|
||||
data.del_cache_key(self.uuid.to_string()).await?
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
|
|
|
@ -30,6 +30,9 @@ pub static EMAIL_REGEX: LazyLock<Regex> = LazyLock::new(|| {
|
|||
pub static USERNAME_REGEX: LazyLock<Regex> =
|
||||
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
|
||||
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