style: move servers to guilds
This commit is contained in:
parent
1543a2f485
commit
e4d9a1b5af
9 changed files with 3 additions and 3 deletions
140
src/api/v1/guilds/mod.rs
Normal file
140
src/api/v1/guilds/mod.rs
Normal file
|
@ -0,0 +1,140 @@
|
|||
//! `/api/v1/servers` Guild related endpoints
|
||||
|
||||
use actix_web::{HttpRequest, HttpResponse, Scope, get, post, web};
|
||||
use serde::Deserialize;
|
||||
|
||||
mod uuid;
|
||||
|
||||
use crate::{
|
||||
api::v1::auth::check_access_token, error::Error, structs::{Guild, StartAmountQuery}, utils::{get_auth_header, global_checks}, Data
|
||||
};
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct GuildInfo {
|
||||
name: String,
|
||||
}
|
||||
|
||||
pub fn web() -> Scope {
|
||||
web::scope("/guilds")
|
||||
.service(post)
|
||||
.service(get)
|
||||
.service(uuid::web())
|
||||
}
|
||||
|
||||
/// `POST /api/v1/servers` Creates a new guild
|
||||
///
|
||||
/// requires auth: yes
|
||||
///
|
||||
/// ### Request Example
|
||||
/// ```
|
||||
/// json!({
|
||||
/// "name": "My new server!"
|
||||
/// });
|
||||
/// ```
|
||||
///
|
||||
/// ### Response Example
|
||||
/// ```
|
||||
/// json!({
|
||||
/// "uuid": "383d2afa-082f-4dd3-9050-ca6ed91487b6",
|
||||
/// "name": "My new server!",
|
||||
/// "description": null,
|
||||
/// "icon": null,
|
||||
/// "owner_uuid": "155d2291-fb23-46bd-a656-ae7c5d8218e6",
|
||||
/// "roles": [],
|
||||
/// "member_count": 1
|
||||
/// });
|
||||
/// ```
|
||||
/// NOTE: UUIDs in this response are made using `uuidgen`, UUIDs made by the actual backend will be UUIDv7 and have extractable timestamps
|
||||
#[post("")]
|
||||
pub async fn post(
|
||||
req: HttpRequest,
|
||||
guild_info: web::Json<GuildInfo>,
|
||||
data: web::Data<Data>,
|
||||
) -> Result<HttpResponse, Error> {
|
||||
let headers = req.headers();
|
||||
|
||||
let auth_header = get_auth_header(headers)?;
|
||||
|
||||
let mut conn = data.pool.get().await?;
|
||||
|
||||
let uuid = check_access_token(auth_header, &mut conn).await?;
|
||||
|
||||
let guild = Guild::new(
|
||||
&mut conn,
|
||||
guild_info.name.clone(),
|
||||
uuid,
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(HttpResponse::Ok().json(guild))
|
||||
}
|
||||
|
||||
/// `GET /api/v1/servers` Fetches all guilds
|
||||
///
|
||||
/// requires auth: yes
|
||||
///
|
||||
/// requires admin: yes
|
||||
///
|
||||
/// ### Response Example
|
||||
/// ```
|
||||
/// json!([
|
||||
/// {
|
||||
/// "uuid": "383d2afa-082f-4dd3-9050-ca6ed91487b6",
|
||||
/// "name": "My new server!",
|
||||
/// "description": null,
|
||||
/// "icon": null,
|
||||
/// "owner_uuid": "155d2291-fb23-46bd-a656-ae7c5d8218e6",
|
||||
/// "roles": [],
|
||||
/// "member_count": 1
|
||||
/// },
|
||||
/// {
|
||||
/// "uuid": "5ba61ec7-5f97-43e1-89a5-d4693c155612",
|
||||
/// "name": "My first server!",
|
||||
/// "description": "This is a cool and nullable description!",
|
||||
/// "icon": "https://nullable-url/path/to/icon.png",
|
||||
/// "owner_uuid": "155d2291-fb23-46bd-a656-ae7c5d8218e6",
|
||||
/// "roles": [
|
||||
/// {
|
||||
/// "uuid": "be0e4da4-cf73-4f45-98f8-bb1c73d1ab8b",
|
||||
/// "guild_uuid": "5ba61ec7-5f97-43e1-89a5-d4693c155612",
|
||||
/// "name": "Cool people",
|
||||
/// "color": 15650773,
|
||||
/// "is_above": c7432f1c-f4ad-4ad3-8216-51388b6abb5b,
|
||||
/// "permissions": 0
|
||||
/// }
|
||||
/// {
|
||||
/// "uuid": "c7432f1c-f4ad-4ad3-8216-51388b6abb5b",
|
||||
/// "guild_uuid": "5ba61ec7-5f97-43e1-89a5-d4693c155612",
|
||||
/// "name": "Equally cool people",
|
||||
/// "color": 16777215,
|
||||
/// "is_above": null,
|
||||
/// "permissions": 0
|
||||
/// }
|
||||
/// ],
|
||||
/// "member_count": 20
|
||||
/// }
|
||||
/// ]);
|
||||
/// ```
|
||||
/// NOTE: UUIDs in this response are made using `uuidgen`, UUIDs made by the actual backend will be UUIDv7 and have extractable timestamps
|
||||
#[get("")]
|
||||
pub async fn get(
|
||||
req: HttpRequest,
|
||||
request_query: web::Query<StartAmountQuery>,
|
||||
data: web::Data<Data>,
|
||||
) -> Result<HttpResponse, Error> {
|
||||
let headers = req.headers();
|
||||
|
||||
let auth_header = get_auth_header(headers)?;
|
||||
|
||||
let start = request_query.start.unwrap_or(0);
|
||||
|
||||
let amount = request_query.amount.unwrap_or(10);
|
||||
|
||||
let uuid = check_access_token(auth_header, &mut data.pool.get().await?).await?;
|
||||
|
||||
global_checks(&data, uuid).await?;
|
||||
|
||||
let guilds = Guild::fetch_amount(&data.pool, start, amount).await?;
|
||||
|
||||
Ok(HttpResponse::Ok().json(guilds))
|
||||
}
|
86
src/api/v1/guilds/uuid/channels.rs
Normal file
86
src/api/v1/guilds/uuid/channels.rs
Normal file
|
@ -0,0 +1,86 @@
|
|||
use crate::{
|
||||
api::v1::auth::check_access_token, error::Error, structs::{Channel, Member}, utils::{get_auth_header, global_checks, order_by_is_above}, Data
|
||||
};
|
||||
use ::uuid::Uuid;
|
||||
use actix_web::{HttpRequest, HttpResponse, get, post, web};
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct ChannelInfo {
|
||||
name: String,
|
||||
description: Option<String>,
|
||||
}
|
||||
|
||||
#[get("{uuid}/channels")]
|
||||
pub async fn get(
|
||||
req: HttpRequest,
|
||||
path: web::Path<(Uuid,)>,
|
||||
data: web::Data<Data>,
|
||||
) -> Result<HttpResponse, Error> {
|
||||
let headers = req.headers();
|
||||
|
||||
let auth_header = get_auth_header(headers)?;
|
||||
|
||||
let guild_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?;
|
||||
|
||||
Member::fetch_one(&mut conn, uuid, guild_uuid).await?;
|
||||
|
||||
if let Ok(cache_hit) = data.get_cache_key(format!("{}_channels", guild_uuid)).await {
|
||||
return Ok(HttpResponse::Ok()
|
||||
.content_type("application/json")
|
||||
.body(cache_hit));
|
||||
}
|
||||
|
||||
let channels = Channel::fetch_all(&data.pool, guild_uuid).await?;
|
||||
|
||||
let channels_ordered = order_by_is_above(channels).await?;
|
||||
|
||||
data.set_cache_key(
|
||||
format!("{}_channels", guild_uuid),
|
||||
channels_ordered.clone(),
|
||||
1800,
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(HttpResponse::Ok().json(channels_ordered))
|
||||
}
|
||||
|
||||
#[post("{uuid}/channels")]
|
||||
pub async fn create(
|
||||
req: HttpRequest,
|
||||
channel_info: web::Json<ChannelInfo>,
|
||||
path: web::Path<(Uuid,)>,
|
||||
data: web::Data<Data>,
|
||||
) -> Result<HttpResponse, Error> {
|
||||
let headers = req.headers();
|
||||
|
||||
let auth_header = get_auth_header(headers)?;
|
||||
|
||||
let guild_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?;
|
||||
|
||||
Member::fetch_one(&mut conn, uuid, guild_uuid).await?;
|
||||
|
||||
// FIXME: Logic to check permissions, should probably be done in utils.rs
|
||||
|
||||
let channel = Channel::new(
|
||||
data.clone(),
|
||||
guild_uuid,
|
||||
channel_info.name.clone(),
|
||||
channel_info.description.clone(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(HttpResponse::Ok().json(channel))
|
||||
}
|
54
src/api/v1/guilds/uuid/icon.rs
Normal file
54
src/api/v1/guilds/uuid/icon.rs
Normal file
|
@ -0,0 +1,54 @@
|
|||
//! `/api/v1/servers/{uuid}/icon` icon related endpoints, will probably be replaced by a multipart post to above endpoint
|
||||
|
||||
use actix_web::{HttpRequest, HttpResponse, put, web};
|
||||
use futures_util::StreamExt as _;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::{
|
||||
api::v1::auth::check_access_token, error::Error, structs::{Guild, Member}, utils::{get_auth_header, global_checks}, Data
|
||||
};
|
||||
|
||||
/// `PUT /api/v1/servers/{uuid}/icon` Icon upload
|
||||
///
|
||||
/// requires auth: no
|
||||
///
|
||||
/// put request expects a file and nothing else
|
||||
#[put("{uuid}/icon")]
|
||||
pub async fn upload(
|
||||
req: HttpRequest,
|
||||
path: web::Path<(Uuid,)>,
|
||||
mut payload: web::Payload,
|
||||
data: web::Data<Data>,
|
||||
) -> Result<HttpResponse, Error> {
|
||||
let headers = req.headers();
|
||||
|
||||
let auth_header = get_auth_header(headers)?;
|
||||
|
||||
let guild_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?;
|
||||
|
||||
Member::fetch_one(&mut conn, uuid, guild_uuid).await?;
|
||||
|
||||
let mut guild = Guild::fetch_one(&mut conn, guild_uuid).await?;
|
||||
|
||||
let mut bytes = web::BytesMut::new();
|
||||
while let Some(item) = payload.next().await {
|
||||
bytes.extend_from_slice(&item?);
|
||||
}
|
||||
|
||||
guild
|
||||
.set_icon(
|
||||
&data.bunny_cdn,
|
||||
&mut conn,
|
||||
data.config.bunny.cdn_url.clone(),
|
||||
bytes,
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(HttpResponse::Ok().finish())
|
||||
}
|
0
src/api/v1/guilds/uuid/invites/id.rs
Normal file
0
src/api/v1/guilds/uuid/invites/id.rs
Normal file
69
src/api/v1/guilds/uuid/invites/mod.rs
Normal file
69
src/api/v1/guilds/uuid/invites/mod.rs
Normal file
|
@ -0,0 +1,69 @@
|
|||
use actix_web::{HttpRequest, HttpResponse, get, post, web};
|
||||
use serde::Deserialize;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::{
|
||||
api::v1::auth::check_access_token, error::Error, structs::{Guild, Member}, utils::{get_auth_header, global_checks}, Data
|
||||
};
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct InviteRequest {
|
||||
custom_id: String,
|
||||
}
|
||||
|
||||
#[get("{uuid}/invites")]
|
||||
pub async fn get(
|
||||
req: HttpRequest,
|
||||
path: web::Path<(Uuid,)>,
|
||||
data: web::Data<Data>,
|
||||
) -> Result<HttpResponse, Error> {
|
||||
let headers = req.headers();
|
||||
|
||||
let auth_header = get_auth_header(headers)?;
|
||||
|
||||
let guild_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?;
|
||||
|
||||
Member::fetch_one(&mut conn, uuid, guild_uuid).await?;
|
||||
|
||||
let guild = Guild::fetch_one(&mut conn, guild_uuid).await?;
|
||||
|
||||
let invites = guild.get_invites(&mut conn).await?;
|
||||
|
||||
Ok(HttpResponse::Ok().json(invites))
|
||||
}
|
||||
|
||||
#[post("{uuid}/invites")]
|
||||
pub async fn create(
|
||||
req: HttpRequest,
|
||||
path: web::Path<(Uuid,)>,
|
||||
invite_request: web::Json<Option<InviteRequest>>,
|
||||
data: web::Data<Data>,
|
||||
) -> Result<HttpResponse, Error> {
|
||||
let headers = req.headers();
|
||||
|
||||
let auth_header = get_auth_header(headers)?;
|
||||
|
||||
let guild_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 member = Member::fetch_one(&mut conn, uuid, guild_uuid).await?;
|
||||
|
||||
let guild = Guild::fetch_one(&mut conn, guild_uuid).await?;
|
||||
|
||||
let custom_id = invite_request.as_ref().map(|ir| ir.custom_id.clone());
|
||||
|
||||
let invite = guild.create_invite(&mut conn, &member, custom_id).await?;
|
||||
|
||||
Ok(HttpResponse::Ok().json(invite))
|
||||
}
|
89
src/api/v1/guilds/uuid/mod.rs
Normal file
89
src/api/v1/guilds/uuid/mod.rs
Normal file
|
@ -0,0 +1,89 @@
|
|||
//! `/api/v1/servers/{uuid}` Specific server endpoints
|
||||
|
||||
use actix_web::{HttpRequest, HttpResponse, Scope, get, web};
|
||||
use uuid::Uuid;
|
||||
|
||||
mod channels;
|
||||
mod icon;
|
||||
mod invites;
|
||||
mod roles;
|
||||
|
||||
use crate::{
|
||||
api::v1::auth::check_access_token, error::Error, structs::{Guild, Member}, utils::{get_auth_header, global_checks}, Data
|
||||
};
|
||||
|
||||
pub fn web() -> Scope {
|
||||
web::scope("")
|
||||
// Servers
|
||||
.service(get)
|
||||
// Channels
|
||||
.service(channels::get)
|
||||
.service(channels::create)
|
||||
// Roles
|
||||
.service(roles::get)
|
||||
.service(roles::create)
|
||||
.service(roles::uuid::get)
|
||||
// Invites
|
||||
.service(invites::get)
|
||||
.service(invites::create)
|
||||
// Icon
|
||||
.service(icon::upload)
|
||||
}
|
||||
|
||||
/// `GET /api/v1/servers/{uuid}` DESCRIPTION
|
||||
///
|
||||
/// requires auth: yes
|
||||
///
|
||||
/// ### Response Example
|
||||
/// ```
|
||||
/// json!({
|
||||
/// "uuid": "5ba61ec7-5f97-43e1-89a5-d4693c155612",
|
||||
/// "name": "My first server!",
|
||||
/// "description": "This is a cool and nullable description!",
|
||||
/// "icon": "https://nullable-url/path/to/icon.png",
|
||||
/// "owner_uuid": "155d2291-fb23-46bd-a656-ae7c5d8218e6",
|
||||
/// "roles": [
|
||||
/// {
|
||||
/// "uuid": "be0e4da4-cf73-4f45-98f8-bb1c73d1ab8b",
|
||||
/// "guild_uuid": "5ba61ec7-5f97-43e1-89a5-d4693c155612",
|
||||
/// "name": "Cool people",
|
||||
/// "color": 15650773,
|
||||
/// "is_above": c7432f1c-f4ad-4ad3-8216-51388b6abb5b,
|
||||
/// "permissions": 0
|
||||
/// }
|
||||
/// {
|
||||
/// "uuid": "c7432f1c-f4ad-4ad3-8216-51388b6abb5b",
|
||||
/// "guild_uuid": "5ba61ec7-5f97-43e1-89a5-d4693c155612",
|
||||
/// "name": "Equally cool people",
|
||||
/// "color": 16777215,
|
||||
/// "is_above": null,
|
||||
/// "permissions": 0
|
||||
/// }
|
||||
/// ],
|
||||
/// "member_count": 20
|
||||
/// });
|
||||
/// ```
|
||||
#[get("/{uuid}")]
|
||||
pub async fn get(
|
||||
req: HttpRequest,
|
||||
path: web::Path<(Uuid,)>,
|
||||
data: web::Data<Data>,
|
||||
) -> Result<HttpResponse, Error> {
|
||||
let headers = req.headers();
|
||||
|
||||
let auth_header = get_auth_header(headers)?;
|
||||
|
||||
let guild_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?;
|
||||
|
||||
Member::fetch_one(&mut conn, uuid, guild_uuid).await?;
|
||||
|
||||
let guild = Guild::fetch_one(&mut conn, guild_uuid).await?;
|
||||
|
||||
Ok(HttpResponse::Ok().json(guild))
|
||||
}
|
76
src/api/v1/guilds/uuid/roles/mod.rs
Normal file
76
src/api/v1/guilds/uuid/roles/mod.rs
Normal file
|
@ -0,0 +1,76 @@
|
|||
use ::uuid::Uuid;
|
||||
use actix_web::{HttpRequest, HttpResponse, get, post, web};
|
||||
use serde::Deserialize;
|
||||
|
||||
use crate::{
|
||||
api::v1::auth::check_access_token, error::Error, structs::{Member, Role}, utils::{get_auth_header, global_checks, order_by_is_above}, Data
|
||||
};
|
||||
|
||||
pub mod uuid;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct RoleInfo {
|
||||
name: String,
|
||||
}
|
||||
|
||||
#[get("{uuid}/roles")]
|
||||
pub async fn get(
|
||||
req: HttpRequest,
|
||||
path: web::Path<(Uuid,)>,
|
||||
data: web::Data<Data>,
|
||||
) -> Result<HttpResponse, Error> {
|
||||
let headers = req.headers();
|
||||
|
||||
let auth_header = get_auth_header(headers)?;
|
||||
|
||||
let guild_uuid = path.into_inner().0;
|
||||
|
||||
let mut conn = data.pool.get().await?;
|
||||
|
||||
let uuid = check_access_token(auth_header, &mut conn).await?;
|
||||
|
||||
Member::fetch_one(&mut conn, uuid, guild_uuid).await?;
|
||||
|
||||
if let Ok(cache_hit) = data.get_cache_key(format!("{}_roles", guild_uuid)).await {
|
||||
return Ok(HttpResponse::Ok()
|
||||
.content_type("application/json")
|
||||
.body(cache_hit));
|
||||
}
|
||||
|
||||
let roles = Role::fetch_all(&mut conn, guild_uuid).await?;
|
||||
|
||||
let roles_ordered = order_by_is_above(roles).await?;
|
||||
|
||||
data.set_cache_key(format!("{}_roles", guild_uuid), roles_ordered.clone(), 1800)
|
||||
.await?;
|
||||
|
||||
Ok(HttpResponse::Ok().json(roles_ordered))
|
||||
}
|
||||
|
||||
#[post("{uuid}/roles")]
|
||||
pub async fn create(
|
||||
req: HttpRequest,
|
||||
role_info: web::Json<RoleInfo>,
|
||||
path: web::Path<(Uuid,)>,
|
||||
data: web::Data<Data>,
|
||||
) -> Result<HttpResponse, Error> {
|
||||
let headers = req.headers();
|
||||
|
||||
let auth_header = get_auth_header(headers)?;
|
||||
|
||||
let guild_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?;
|
||||
|
||||
Member::fetch_one(&mut conn, uuid, guild_uuid).await?;
|
||||
|
||||
// FIXME: Logic to check permissions, should probably be done in utils.rs
|
||||
|
||||
let role = Role::new(&mut conn, guild_uuid, role_info.name.clone()).await?;
|
||||
|
||||
Ok(HttpResponse::Ok().json(role))
|
||||
}
|
39
src/api/v1/guilds/uuid/roles/uuid.rs
Normal file
39
src/api/v1/guilds/uuid/roles/uuid.rs
Normal file
|
@ -0,0 +1,39 @@
|
|||
use crate::{
|
||||
api::v1::auth::check_access_token, error::Error, structs::{Member, Role}, utils::{get_auth_header, global_checks}, Data
|
||||
};
|
||||
use ::uuid::Uuid;
|
||||
use actix_web::{HttpRequest, HttpResponse, get, web};
|
||||
|
||||
#[get("{uuid}/roles/{role_uuid}")]
|
||||
pub async fn get(
|
||||
req: HttpRequest,
|
||||
path: web::Path<(Uuid, Uuid)>,
|
||||
data: web::Data<Data>,
|
||||
) -> Result<HttpResponse, Error> {
|
||||
let headers = req.headers();
|
||||
|
||||
let auth_header = get_auth_header(headers)?;
|
||||
|
||||
let (guild_uuid, role_uuid) = path.into_inner();
|
||||
|
||||
let mut conn = data.pool.get().await?;
|
||||
|
||||
let uuid = check_access_token(auth_header, &mut conn).await?;
|
||||
|
||||
global_checks(&data, uuid).await?;
|
||||
|
||||
Member::fetch_one(&mut conn, uuid, guild_uuid).await?;
|
||||
|
||||
if let Ok(cache_hit) = data.get_cache_key(format!("{}", role_uuid)).await {
|
||||
return Ok(HttpResponse::Ok()
|
||||
.content_type("application/json")
|
||||
.body(cache_hit));
|
||||
}
|
||||
|
||||
let role = Role::fetch_one(&mut conn, role_uuid).await?;
|
||||
|
||||
data.set_cache_key(format!("{}", role_uuid), role.clone(), 60)
|
||||
.await?;
|
||||
|
||||
Ok(HttpResponse::Ok().json(role))
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue