Compare commits
No commits in common. "c4fc23ec85f5da1422688279de9848b1e710abf8" and "643f94b5805f616dcf79cb663f76236c99ef685a" have entirely different histories.
c4fc23ec85
...
643f94b580
8 changed files with 9 additions and 217 deletions
|
@ -8,12 +8,6 @@ strip = true
|
||||||
lto = true
|
lto = true
|
||||||
codegen-units = 1
|
codegen-units = 1
|
||||||
|
|
||||||
# Speed up compilation to make dev bearable
|
|
||||||
[profile.dev]
|
|
||||||
debug = 0
|
|
||||||
strip = "debuginfo"
|
|
||||||
codegen-units = 512
|
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-cors = "0.7.1"
|
actix-cors = "0.7.1"
|
||||||
actix-web = "4.11"
|
actix-web = "4.11"
|
||||||
|
@ -38,7 +32,7 @@ futures-util = "0.3.31"
|
||||||
bunny-api-tokio = "0.3.0"
|
bunny-api-tokio = "0.3.0"
|
||||||
bindet = "0.3.2"
|
bindet = "0.3.2"
|
||||||
deadpool = "0.12"
|
deadpool = "0.12"
|
||||||
diesel = { version = "2.2", features = ["uuid", "chrono"], default-features = false }
|
diesel = { version = "2.2", features = ["uuid", "chrono"] }
|
||||||
diesel-async = { version = "0.5", features = ["deadpool", "postgres", "async-connection-wrapper"] }
|
diesel-async = { version = "0.5", features = ["deadpool", "postgres", "async-connection-wrapper"] }
|
||||||
diesel_migrations = { version = "2.2.0", features = ["postgres"] }
|
diesel_migrations = { version = "2.2.0", features = ["postgres"] }
|
||||||
thiserror = "2.0.12"
|
thiserror = "2.0.12"
|
||||||
|
|
|
@ -1,2 +0,0 @@
|
||||||
-- This file should undo anything in `up.sql`
|
|
||||||
ALTER TABLE users DROP COLUMN about;
|
|
|
@ -1,2 +0,0 @@
|
||||||
-- Your SQL goes here
|
|
||||||
ALTER TABLE users ADD COLUMN about VARCHAR(200) DEFAULT NULL;
|
|
|
@ -1,5 +1,3 @@
|
||||||
//! `/api/v1/channels/{uuid}` Channel specific endpoints
|
|
||||||
|
|
||||||
pub mod messages;
|
pub mod messages;
|
||||||
pub mod socket;
|
pub mod socket;
|
||||||
|
|
||||||
|
@ -10,9 +8,8 @@ use crate::{
|
||||||
structs::{Channel, Member},
|
structs::{Channel, Member},
|
||||||
utils::{get_auth_header, global_checks},
|
utils::{get_auth_header, global_checks},
|
||||||
};
|
};
|
||||||
use actix_web::{delete, get, patch, web, HttpRequest, HttpResponse};
|
use actix_web::{HttpRequest, HttpResponse, delete, get, web};
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
use serde::Deserialize;
|
|
||||||
|
|
||||||
#[get("/{uuid}")]
|
#[get("/{uuid}")]
|
||||||
pub async fn get(
|
pub async fn get(
|
||||||
|
@ -65,80 +62,3 @@ 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))
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
|
@ -41,7 +41,6 @@ struct NewInfo {
|
||||||
//password: Option<String>, will probably be handled through a reset password link
|
//password: Option<String>, will probably be handled through a reset password link
|
||||||
email: Option<String>,
|
email: Option<String>,
|
||||||
pronouns: Option<String>,
|
pronouns: Option<String>,
|
||||||
about: Option<String>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, MultipartForm)]
|
#[derive(Debug, MultipartForm)]
|
||||||
|
@ -103,9 +102,5 @@ pub async fn update(
|
||||||
me.set_pronouns(&data, pronouns.clone()).await?;
|
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())
|
Ok(HttpResponse::Ok().finish())
|
||||||
}
|
}
|
||||||
|
|
|
@ -146,8 +146,6 @@ diesel::table! {
|
||||||
avatar -> Nullable<Varchar>,
|
avatar -> Nullable<Varchar>,
|
||||||
#[max_length = 32]
|
#[max_length = 32]
|
||||||
pronouns -> Nullable<Varchar>,
|
pronouns -> Nullable<Varchar>,
|
||||||
#[max_length = 200]
|
|
||||||
about -> Nullable<Varchar>,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
122
src/structs.rs
122
src/structs.rs
|
@ -24,9 +24,13 @@ use url::Url;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
error::Error, schema::*, utils::{
|
Conn, Data,
|
||||||
generate_refresh_token, global_checks, image_check, order_by_is_above, user_uuid_from_identifier, CHANNEL_REGEX, EMAIL_REGEX, PASSWORD_REGEX, USERNAME_REGEX
|
error::Error,
|
||||||
}, Conn, Data
|
schema::*,
|
||||||
|
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 {
|
||||||
|
@ -227,10 +231,6 @@ 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,93 +353,6 @@ 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)]
|
||||||
|
@ -953,7 +866,6 @@ pub struct User {
|
||||||
display_name: Option<String>,
|
display_name: Option<String>,
|
||||||
avatar: Option<String>,
|
avatar: Option<String>,
|
||||||
pronouns: Option<String>,
|
pronouns: Option<String>,
|
||||||
about: Option<String>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl User {
|
impl User {
|
||||||
|
@ -1005,7 +917,6 @@ pub struct Me {
|
||||||
display_name: Option<String>,
|
display_name: Option<String>,
|
||||||
avatar: Option<String>,
|
avatar: Option<String>,
|
||||||
pronouns: Option<String>,
|
pronouns: Option<String>,
|
||||||
about: Option<String>,
|
|
||||||
email: String,
|
email: String,
|
||||||
pub email_verified: bool,
|
pub email_verified: bool,
|
||||||
}
|
}
|
||||||
|
@ -1200,25 +1111,6 @@ impl Me {
|
||||||
|
|
||||||
Ok(())
|
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)]
|
#[derive(Deserialize)]
|
||||||
|
|
|
@ -30,9 +30,6 @@ 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