style: cargo fmt
This commit is contained in:
parent
c21762ac7e
commit
78e87b65ce
23 changed files with 555 additions and 276 deletions
|
@ -1,9 +1,9 @@
|
|||
use actix_web::{post, web, Error, HttpRequest, HttpResponse, Scope};
|
||||
use actix_web::{Error, HttpRequest, HttpResponse, Scope, post, web};
|
||||
use serde::Deserialize;
|
||||
|
||||
mod uuid;
|
||||
|
||||
use crate::{api::v1::auth::check_access_token, structs::Guild, utils::get_auth_header, Data};
|
||||
use crate::{Data, api::v1::auth::check_access_token, structs::Guild, utils::get_auth_header};
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct GuildInfo {
|
||||
|
@ -12,35 +12,42 @@ struct GuildInfo {
|
|||
}
|
||||
|
||||
pub fn web() -> Scope {
|
||||
web::scope("/servers")
|
||||
.service(res)
|
||||
.service(uuid::web())
|
||||
web::scope("/servers").service(res).service(uuid::web())
|
||||
}
|
||||
|
||||
#[post("")]
|
||||
pub async fn res(req: HttpRequest, guild_info: web::Json<GuildInfo>, data: web::Data<Data>) -> Result<HttpResponse, Error> {
|
||||
pub async fn res(
|
||||
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);
|
||||
|
||||
if let Err(error) = auth_header {
|
||||
return Ok(error)
|
||||
return Ok(error);
|
||||
}
|
||||
|
||||
let authorized = check_access_token(auth_header.unwrap(), &data.pool).await;
|
||||
|
||||
if let Err(error) = authorized {
|
||||
return Ok(error)
|
||||
return Ok(error);
|
||||
}
|
||||
|
||||
let uuid = authorized.unwrap();
|
||||
|
||||
let guild = Guild::new(&data.pool, guild_info.name.clone(), guild_info.description.clone(), uuid).await;
|
||||
let guild = Guild::new(
|
||||
&data.pool,
|
||||
guild_info.name.clone(),
|
||||
guild_info.description.clone(),
|
||||
uuid,
|
||||
)
|
||||
.await;
|
||||
|
||||
if let Err(error) = guild {
|
||||
return Ok(error)
|
||||
return Ok(error);
|
||||
}
|
||||
|
||||
Ok(HttpResponse::Ok().json(guild.unwrap()))
|
||||
}
|
||||
|
||||
|
|
|
@ -1,25 +1,34 @@
|
|||
use actix_web::{get, post, web, Error, HttpRequest, HttpResponse};
|
||||
use serde::Deserialize;
|
||||
use crate::{api::v1::auth::check_access_token, structs::{Channel, Member}, utils::get_auth_header, Data};
|
||||
use crate::{
|
||||
Data,
|
||||
api::v1::auth::check_access_token,
|
||||
structs::{Channel, Member},
|
||||
utils::get_auth_header,
|
||||
};
|
||||
use ::uuid::Uuid;
|
||||
use actix_web::{Error, HttpRequest, HttpResponse, get, post, web};
|
||||
use log::error;
|
||||
use serde::Deserialize;
|
||||
|
||||
pub mod uuid;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct ChannelInfo {
|
||||
name: String,
|
||||
description: Option<String>
|
||||
description: Option<String>,
|
||||
}
|
||||
|
||||
#[get("{uuid}/channels")]
|
||||
pub async fn get(req: HttpRequest, path: web::Path<(Uuid,)>, data: web::Data<Data>) -> Result<HttpResponse, Error> {
|
||||
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);
|
||||
|
||||
if let Err(error) = auth_header {
|
||||
return Ok(error)
|
||||
return Ok(error);
|
||||
}
|
||||
|
||||
let guild_uuid = path.into_inner().0;
|
||||
|
@ -27,7 +36,7 @@ pub async fn get(req: HttpRequest, path: web::Path<(Uuid,)>, data: web::Data<Dat
|
|||
let authorized = check_access_token(auth_header.unwrap(), &data.pool).await;
|
||||
|
||||
if let Err(error) = authorized {
|
||||
return Ok(error)
|
||||
return Ok(error);
|
||||
}
|
||||
|
||||
let uuid = authorized.unwrap();
|
||||
|
@ -41,18 +50,22 @@ pub async fn get(req: HttpRequest, path: web::Path<(Uuid,)>, data: web::Data<Dat
|
|||
let cache_result = data.get_cache_key(format!("{}_channels", guild_uuid)).await;
|
||||
|
||||
if let Ok(cache_hit) = cache_result {
|
||||
return Ok(HttpResponse::Ok().content_type("application/json").body(cache_hit))
|
||||
return Ok(HttpResponse::Ok()
|
||||
.content_type("application/json")
|
||||
.body(cache_hit));
|
||||
}
|
||||
|
||||
let channels_result = Channel::fetch_all(&data.pool, guild_uuid).await;
|
||||
|
||||
if let Err(error) = channels_result {
|
||||
return Ok(error)
|
||||
return Ok(error);
|
||||
}
|
||||
|
||||
let channels = channels_result.unwrap();
|
||||
|
||||
let cache_result = data.set_cache_key(format!("{}_channels", guild_uuid), channels.clone(), 1800).await;
|
||||
let cache_result = data
|
||||
.set_cache_key(format!("{}_channels", guild_uuid), channels.clone(), 1800)
|
||||
.await;
|
||||
|
||||
if let Err(error) = cache_result {
|
||||
error!("{}", error);
|
||||
|
@ -63,13 +76,18 @@ pub async fn get(req: HttpRequest, path: web::Path<(Uuid,)>, data: web::Data<Dat
|
|||
}
|
||||
|
||||
#[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> {
|
||||
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);
|
||||
|
||||
if let Err(error) = auth_header {
|
||||
return Ok(error)
|
||||
return Ok(error);
|
||||
}
|
||||
|
||||
let guild_uuid = path.into_inner().0;
|
||||
|
@ -77,7 +95,7 @@ pub async fn create(req: HttpRequest, channel_info: web::Json<ChannelInfo>, path
|
|||
let authorized = check_access_token(auth_header.unwrap(), &data.pool).await;
|
||||
|
||||
if let Err(error) = authorized {
|
||||
return Ok(error)
|
||||
return Ok(error);
|
||||
}
|
||||
|
||||
let uuid = authorized.unwrap();
|
||||
|
@ -90,7 +108,13 @@ pub async fn create(req: HttpRequest, channel_info: web::Json<ChannelInfo>, path
|
|||
|
||||
// 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;
|
||||
let channel = Channel::new(
|
||||
data.clone(),
|
||||
guild_uuid,
|
||||
channel_info.name.clone(),
|
||||
channel_info.description.clone(),
|
||||
)
|
||||
.await;
|
||||
|
||||
if let Err(error) = channel {
|
||||
return Ok(error);
|
||||
|
|
|
@ -1,8 +1,13 @@
|
|||
use actix_web::{get, web, Error, HttpRequest, HttpResponse};
|
||||
use serde::Deserialize;
|
||||
use crate::{api::v1::auth::check_access_token, structs::{Channel, Member}, utils::get_auth_header, Data};
|
||||
use crate::{
|
||||
Data,
|
||||
api::v1::auth::check_access_token,
|
||||
structs::{Channel, Member},
|
||||
utils::get_auth_header,
|
||||
};
|
||||
use ::uuid::Uuid;
|
||||
use actix_web::{Error, HttpRequest, HttpResponse, get, web};
|
||||
use log::error;
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct MessageRequest {
|
||||
|
@ -11,13 +16,18 @@ struct MessageRequest {
|
|||
}
|
||||
|
||||
#[get("{uuid}/channels/{channel_uuid}/messages")]
|
||||
pub async fn get(req: HttpRequest, path: web::Path<(Uuid, Uuid)>, message_request: web::Query<MessageRequest>, data: web::Data<Data>) -> Result<HttpResponse, Error> {
|
||||
pub async fn get(
|
||||
req: HttpRequest,
|
||||
path: web::Path<(Uuid, Uuid)>,
|
||||
message_request: web::Query<MessageRequest>,
|
||||
data: web::Data<Data>,
|
||||
) -> Result<HttpResponse, Error> {
|
||||
let headers = req.headers();
|
||||
|
||||
let auth_header = get_auth_header(headers);
|
||||
|
||||
if let Err(error) = auth_header {
|
||||
return Ok(error)
|
||||
return Ok(error);
|
||||
}
|
||||
|
||||
let (guild_uuid, channel_uuid) = path.into_inner();
|
||||
|
@ -25,7 +35,7 @@ pub async fn get(req: HttpRequest, path: web::Path<(Uuid, Uuid)>, message_reques
|
|||
let authorized = check_access_token(auth_header.unwrap(), &data.pool).await;
|
||||
|
||||
if let Err(error) = authorized {
|
||||
return Ok(error)
|
||||
return Ok(error);
|
||||
}
|
||||
|
||||
let uuid = authorized.unwrap();
|
||||
|
@ -46,23 +56,27 @@ pub async fn get(req: HttpRequest, path: web::Path<(Uuid, Uuid)>, message_reques
|
|||
let channel_result = Channel::fetch_one(&data.pool, guild_uuid, channel_uuid).await;
|
||||
|
||||
if let Err(error) = channel_result {
|
||||
return Ok(error)
|
||||
return Ok(error);
|
||||
}
|
||||
|
||||
|
||||
channel = channel_result.unwrap();
|
||||
|
||||
let cache_result = data.set_cache_key(format!("{}", channel_uuid), channel.clone(), 60).await;
|
||||
|
||||
|
||||
let cache_result = data
|
||||
.set_cache_key(format!("{}", channel_uuid), channel.clone(), 60)
|
||||
.await;
|
||||
|
||||
if let Err(error) = cache_result {
|
||||
error!("{}", error);
|
||||
return Ok(HttpResponse::InternalServerError().finish());
|
||||
}
|
||||
}
|
||||
|
||||
let messages = channel.fetch_messages(&data.pool, message_request.amount, message_request.offset).await;
|
||||
let messages = channel
|
||||
.fetch_messages(&data.pool, message_request.amount, message_request.offset)
|
||||
.await;
|
||||
|
||||
if let Err(error) = messages {
|
||||
return Ok(error)
|
||||
return Ok(error);
|
||||
}
|
||||
|
||||
Ok(HttpResponse::Ok().json(messages.unwrap()))
|
||||
|
|
|
@ -1,19 +1,28 @@
|
|||
pub mod messages;
|
||||
pub mod socket;
|
||||
|
||||
use actix_web::{delete, get, web, Error, HttpRequest, HttpResponse};
|
||||
use crate::{api::v1::auth::check_access_token, structs::{Channel, Member}, utils::get_auth_header, Data};
|
||||
use crate::{
|
||||
Data,
|
||||
api::v1::auth::check_access_token,
|
||||
structs::{Channel, Member},
|
||||
utils::get_auth_header,
|
||||
};
|
||||
use ::uuid::Uuid;
|
||||
use actix_web::{Error, HttpRequest, HttpResponse, delete, get, web};
|
||||
use log::error;
|
||||
|
||||
#[get("{uuid}/channels/{channel_uuid}")]
|
||||
pub async fn get(req: HttpRequest, path: web::Path<(Uuid, Uuid)>, data: web::Data<Data>) -> Result<HttpResponse, Error> {
|
||||
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);
|
||||
|
||||
if let Err(error) = auth_header {
|
||||
return Ok(error)
|
||||
return Ok(error);
|
||||
}
|
||||
|
||||
let (guild_uuid, channel_uuid) = path.into_inner();
|
||||
|
@ -21,7 +30,7 @@ pub async fn get(req: HttpRequest, path: web::Path<(Uuid, Uuid)>, data: web::Dat
|
|||
let authorized = check_access_token(auth_header.unwrap(), &data.pool).await;
|
||||
|
||||
if let Err(error) = authorized {
|
||||
return Ok(error)
|
||||
return Ok(error);
|
||||
}
|
||||
|
||||
let uuid = authorized.unwrap();
|
||||
|
@ -35,18 +44,22 @@ pub async fn get(req: HttpRequest, path: web::Path<(Uuid, Uuid)>, data: web::Dat
|
|||
let cache_result = data.get_cache_key(format!("{}", channel_uuid)).await;
|
||||
|
||||
if let Ok(cache_hit) = cache_result {
|
||||
return Ok(HttpResponse::Ok().content_type("application/json").body(cache_hit))
|
||||
return Ok(HttpResponse::Ok()
|
||||
.content_type("application/json")
|
||||
.body(cache_hit));
|
||||
}
|
||||
|
||||
let channel_result = Channel::fetch_one(&data.pool, guild_uuid, channel_uuid).await;
|
||||
|
||||
if let Err(error) = channel_result {
|
||||
return Ok(error)
|
||||
return Ok(error);
|
||||
}
|
||||
|
||||
let channel = channel_result.unwrap();
|
||||
|
||||
let cache_result = data.set_cache_key(format!("{}", channel_uuid), channel.clone(), 60).await;
|
||||
let cache_result = data
|
||||
.set_cache_key(format!("{}", channel_uuid), channel.clone(), 60)
|
||||
.await;
|
||||
|
||||
if let Err(error) = cache_result {
|
||||
error!("{}", error);
|
||||
|
@ -57,13 +70,17 @@ pub async fn get(req: HttpRequest, path: web::Path<(Uuid, Uuid)>, data: web::Dat
|
|||
}
|
||||
|
||||
#[delete("{uuid}/channels/{channel_uuid}")]
|
||||
pub async fn delete(req: HttpRequest, path: web::Path<(Uuid, Uuid)>, data: web::Data<Data>) -> Result<HttpResponse, Error> {
|
||||
pub async fn delete(
|
||||
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);
|
||||
|
||||
if let Err(error) = auth_header {
|
||||
return Ok(error)
|
||||
return Ok(error);
|
||||
}
|
||||
|
||||
let (guild_uuid, channel_uuid) = path.into_inner();
|
||||
|
@ -71,7 +88,7 @@ pub async fn delete(req: HttpRequest, path: web::Path<(Uuid, Uuid)>, data: web::
|
|||
let authorized = check_access_token(auth_header.unwrap(), &data.pool).await;
|
||||
|
||||
if let Err(error) = authorized {
|
||||
return Ok(error)
|
||||
return Ok(error);
|
||||
}
|
||||
|
||||
let uuid = authorized.unwrap();
|
||||
|
@ -98,7 +115,7 @@ pub async fn delete(req: HttpRequest, path: web::Path<(Uuid, Uuid)>, data: web::
|
|||
let channel_result = Channel::fetch_one(&data.pool, guild_uuid, channel_uuid).await;
|
||||
|
||||
if let Err(error) = channel_result {
|
||||
return Ok(error)
|
||||
return Ok(error);
|
||||
}
|
||||
|
||||
channel = channel_result.unwrap();
|
||||
|
@ -107,9 +124,8 @@ pub async fn delete(req: HttpRequest, path: web::Path<(Uuid, Uuid)>, data: web::
|
|||
let delete_result = channel.delete(&data.pool).await;
|
||||
|
||||
if let Err(error) = delete_result {
|
||||
return Ok(error)
|
||||
return Ok(error);
|
||||
}
|
||||
|
||||
Ok(HttpResponse::Ok().finish())
|
||||
}
|
||||
|
||||
|
|
|
@ -1,13 +1,23 @@
|
|||
use actix_web::{get, rt, web, Error, HttpRequest, HttpResponse};
|
||||
use actix_web::{Error, HttpRequest, HttpResponse, get, rt, web};
|
||||
use actix_ws::AggregatedMessage;
|
||||
use futures_util::StreamExt as _;
|
||||
use uuid::Uuid;
|
||||
use log::error;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::{api::v1::auth::check_access_token, structs::{Channel, Member}, utils::get_auth_header, Data};
|
||||
use crate::{
|
||||
Data,
|
||||
api::v1::auth::check_access_token,
|
||||
structs::{Channel, Member},
|
||||
utils::get_auth_header,
|
||||
};
|
||||
|
||||
#[get("{uuid}/channels/{channel_uuid}/socket")]
|
||||
pub async fn echo(req: HttpRequest, path: web::Path<(Uuid, Uuid)>, stream: web::Payload, data: web::Data<Data>) -> Result<HttpResponse, Error> {
|
||||
pub async fn echo(
|
||||
req: HttpRequest,
|
||||
path: web::Path<(Uuid, Uuid)>,
|
||||
stream: web::Payload,
|
||||
data: web::Data<Data>,
|
||||
) -> Result<HttpResponse, Error> {
|
||||
// Get all headers
|
||||
let headers = req.headers();
|
||||
|
||||
|
@ -15,7 +25,7 @@ pub async fn echo(req: HttpRequest, path: web::Path<(Uuid, Uuid)>, stream: web::
|
|||
let auth_header = get_auth_header(headers);
|
||||
|
||||
if let Err(error) = auth_header {
|
||||
return Ok(error)
|
||||
return Ok(error);
|
||||
}
|
||||
|
||||
// Get uuids from path
|
||||
|
@ -25,7 +35,7 @@ pub async fn echo(req: HttpRequest, path: web::Path<(Uuid, Uuid)>, stream: web::
|
|||
let authorized = check_access_token(auth_header.unwrap(), &data.pool).await;
|
||||
|
||||
if let Err(error) = authorized {
|
||||
return Ok(error)
|
||||
return Ok(error);
|
||||
}
|
||||
|
||||
// Unwrap user uuid from authorization
|
||||
|
@ -50,13 +60,15 @@ pub async fn echo(req: HttpRequest, path: web::Path<(Uuid, Uuid)>, stream: web::
|
|||
let channel_result = Channel::fetch_one(&data.pool, guild_uuid, channel_uuid).await;
|
||||
|
||||
if let Err(error) = channel_result {
|
||||
return Ok(error)
|
||||
return Ok(error);
|
||||
}
|
||||
|
||||
|
||||
channel = channel_result.unwrap();
|
||||
|
||||
let cache_result = data.set_cache_key(format!("{}", channel_uuid), channel.clone(), 60).await;
|
||||
|
||||
|
||||
let cache_result = data
|
||||
.set_cache_key(format!("{}", channel_uuid), channel.clone(), 60)
|
||||
.await;
|
||||
|
||||
if let Err(error) = cache_result {
|
||||
error!("{}", error);
|
||||
return Ok(HttpResponse::InternalServerError().finish());
|
||||
|
@ -74,7 +86,7 @@ pub async fn echo(req: HttpRequest, path: web::Path<(Uuid, Uuid)>, stream: web::
|
|||
|
||||
if let Err(error) = pubsub_result {
|
||||
error!("{}", error);
|
||||
return Ok(HttpResponse::InternalServerError().finish())
|
||||
return Ok(HttpResponse::InternalServerError().finish());
|
||||
}
|
||||
|
||||
let mut session_2 = session_1.clone();
|
||||
|
@ -90,14 +102,25 @@ pub async fn echo(req: HttpRequest, path: web::Path<(Uuid, Uuid)>, stream: web::
|
|||
|
||||
// start task but don't wait for it
|
||||
rt::spawn(async move {
|
||||
let mut conn = data.cache_pool.get_multiplexed_tokio_connection().await.unwrap();
|
||||
let mut conn = data
|
||||
.cache_pool
|
||||
.get_multiplexed_tokio_connection()
|
||||
.await
|
||||
.unwrap();
|
||||
// receive messages from websocket
|
||||
while let Some(msg) = stream.next().await {
|
||||
match msg {
|
||||
Ok(AggregatedMessage::Text(text)) => {
|
||||
// echo text message
|
||||
redis::cmd("PUBLISH").arg(&[channel_uuid.to_string(), text.to_string()]).exec_async(&mut conn).await.unwrap();
|
||||
channel.new_message(&data.pool, uuid, text.to_string()).await.unwrap();
|
||||
redis::cmd("PUBLISH")
|
||||
.arg(&[channel_uuid.to_string(), text.to_string()])
|
||||
.exec_async(&mut conn)
|
||||
.await
|
||||
.unwrap();
|
||||
channel
|
||||
.new_message(&data.pool, uuid, text.to_string())
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
Ok(AggregatedMessage::Binary(bin)) => {
|
||||
|
|
|
@ -1,8 +1,13 @@
|
|||
use actix_web::{get, post, web, Error, HttpRequest, HttpResponse};
|
||||
use actix_web::{Error, HttpRequest, HttpResponse, get, post, web};
|
||||
use serde::Deserialize;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::{api::v1::auth::check_access_token, structs::{Guild, Member}, utils::get_auth_header, Data};
|
||||
use crate::{
|
||||
Data,
|
||||
api::v1::auth::check_access_token,
|
||||
structs::{Guild, Member},
|
||||
utils::get_auth_header,
|
||||
};
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct InviteRequest {
|
||||
|
@ -10,13 +15,17 @@ struct InviteRequest {
|
|||
}
|
||||
|
||||
#[get("{uuid}/invites")]
|
||||
pub async fn get(req: HttpRequest, path: web::Path<(Uuid,)>, data: web::Data<Data>) -> Result<HttpResponse, Error> {
|
||||
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);
|
||||
|
||||
if let Err(error) = auth_header {
|
||||
return Ok(error)
|
||||
return Ok(error);
|
||||
}
|
||||
|
||||
let guild_uuid = path.into_inner().0;
|
||||
|
@ -24,7 +33,7 @@ pub async fn get(req: HttpRequest, path: web::Path<(Uuid,)>, data: web::Data<Dat
|
|||
let authorized = check_access_token(auth_header.unwrap(), &data.pool).await;
|
||||
|
||||
if let Err(error) = authorized {
|
||||
return Ok(error)
|
||||
return Ok(error);
|
||||
}
|
||||
|
||||
let uuid = authorized.unwrap();
|
||||
|
@ -47,19 +56,24 @@ pub async fn get(req: HttpRequest, path: web::Path<(Uuid,)>, data: web::Data<Dat
|
|||
|
||||
if let Err(error) = invites {
|
||||
return Ok(error);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(HttpResponse::Ok().json(invites.unwrap()))
|
||||
}
|
||||
|
||||
#[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> {
|
||||
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);
|
||||
|
||||
if let Err(error) = auth_header {
|
||||
return Ok(error)
|
||||
return Ok(error);
|
||||
}
|
||||
|
||||
let guild_uuid = path.into_inner().0;
|
||||
|
@ -67,7 +81,7 @@ pub async fn create(req: HttpRequest, path: web::Path<(Uuid,)>, invite_request:
|
|||
let authorized = check_access_token(auth_header.unwrap(), &data.pool).await;
|
||||
|
||||
if let Err(error) = authorized {
|
||||
return Ok(error)
|
||||
return Ok(error);
|
||||
}
|
||||
|
||||
let uuid = authorized.unwrap();
|
||||
|
@ -88,7 +102,7 @@ pub async fn create(req: HttpRequest, path: web::Path<(Uuid,)>, invite_request:
|
|||
|
||||
let guild = guild_result.unwrap();
|
||||
|
||||
let custom_id = invite_request.as_ref().map(|ir| ir.custom_id.clone());
|
||||
let custom_id = invite_request.as_ref().map(|ir| ir.custom_id.clone());
|
||||
|
||||
let invite = guild.create_invite(&data.pool, &member, custom_id).await;
|
||||
|
||||
|
|
|
@ -1,11 +1,16 @@
|
|||
use actix_web::{get, web, Error, HttpRequest, HttpResponse, Scope};
|
||||
use actix_web::{Error, HttpRequest, HttpResponse, Scope, get, web};
|
||||
use uuid::Uuid;
|
||||
|
||||
mod channels;
|
||||
mod roles;
|
||||
mod invites;
|
||||
mod roles;
|
||||
|
||||
use crate::{api::v1::auth::check_access_token, structs::{Guild, Member}, utils::get_auth_header, Data};
|
||||
use crate::{
|
||||
Data,
|
||||
api::v1::auth::check_access_token,
|
||||
structs::{Guild, Member},
|
||||
utils::get_auth_header,
|
||||
};
|
||||
|
||||
pub fn web() -> Scope {
|
||||
web::scope("")
|
||||
|
@ -28,13 +33,17 @@ pub fn web() -> Scope {
|
|||
}
|
||||
|
||||
#[get("/{uuid}")]
|
||||
pub async fn res(req: HttpRequest, path: web::Path<(Uuid,)>, data: web::Data<Data>) -> Result<HttpResponse, Error> {
|
||||
pub async fn res(
|
||||
req: HttpRequest,
|
||||
path: web::Path<(Uuid,)>,
|
||||
data: web::Data<Data>,
|
||||
) -> Result<HttpResponse, Error> {
|
||||
let headers = req.headers();
|
||||
|
||||
let auth_header = get_auth_header(headers);
|
||||
|
||||
if let Err(error) = auth_header {
|
||||
return Ok(error)
|
||||
return Ok(error);
|
||||
}
|
||||
|
||||
let guild_uuid = path.into_inner().0;
|
||||
|
@ -42,7 +51,7 @@ pub async fn res(req: HttpRequest, path: web::Path<(Uuid,)>, data: web::Data<Dat
|
|||
let authorized = check_access_token(auth_header.unwrap(), &data.pool).await;
|
||||
|
||||
if let Err(error) = authorized {
|
||||
return Ok(error)
|
||||
return Ok(error);
|
||||
}
|
||||
|
||||
let uuid = authorized.unwrap();
|
||||
|
@ -61,4 +70,3 @@ pub async fn res(req: HttpRequest, path: web::Path<(Uuid,)>, data: web::Data<Dat
|
|||
|
||||
Ok(HttpResponse::Ok().json(guild.unwrap()))
|
||||
}
|
||||
|
||||
|
|
|
@ -1,8 +1,13 @@
|
|||
use actix_web::{get, post, web, Error, HttpRequest, HttpResponse};
|
||||
use serde::Deserialize;
|
||||
use crate::{api::v1::auth::check_access_token, structs::{Member, Role}, utils::get_auth_header, Data};
|
||||
use crate::{
|
||||
Data,
|
||||
api::v1::auth::check_access_token,
|
||||
structs::{Member, Role},
|
||||
utils::get_auth_header,
|
||||
};
|
||||
use ::uuid::Uuid;
|
||||
use actix_web::{Error, HttpRequest, HttpResponse, get, post, web};
|
||||
use log::error;
|
||||
use serde::Deserialize;
|
||||
|
||||
pub mod uuid;
|
||||
|
||||
|
@ -12,13 +17,17 @@ struct RoleInfo {
|
|||
}
|
||||
|
||||
#[get("{uuid}/roles")]
|
||||
pub async fn get(req: HttpRequest, path: web::Path<(Uuid,)>, data: web::Data<Data>) -> Result<HttpResponse, Error> {
|
||||
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);
|
||||
|
||||
if let Err(error) = auth_header {
|
||||
return Ok(error)
|
||||
return Ok(error);
|
||||
}
|
||||
|
||||
let guild_uuid = path.into_inner().0;
|
||||
|
@ -26,7 +35,7 @@ pub async fn get(req: HttpRequest, path: web::Path<(Uuid,)>, data: web::Data<Dat
|
|||
let authorized = check_access_token(auth_header.unwrap(), &data.pool).await;
|
||||
|
||||
if let Err(error) = authorized {
|
||||
return Ok(error)
|
||||
return Ok(error);
|
||||
}
|
||||
|
||||
let uuid = authorized.unwrap();
|
||||
|
@ -40,18 +49,22 @@ pub async fn get(req: HttpRequest, path: web::Path<(Uuid,)>, data: web::Data<Dat
|
|||
let cache_result = data.get_cache_key(format!("{}_roles", guild_uuid)).await;
|
||||
|
||||
if let Ok(cache_hit) = cache_result {
|
||||
return Ok(HttpResponse::Ok().content_type("application/json").body(cache_hit))
|
||||
return Ok(HttpResponse::Ok()
|
||||
.content_type("application/json")
|
||||
.body(cache_hit));
|
||||
}
|
||||
|
||||
let roles_result = Role::fetch_all(&data.pool, guild_uuid).await;
|
||||
|
||||
if let Err(error) = roles_result {
|
||||
return Ok(error)
|
||||
return Ok(error);
|
||||
}
|
||||
|
||||
let roles = roles_result.unwrap();
|
||||
|
||||
let cache_result = data.set_cache_key(format!("{}_roles", guild_uuid), roles.clone(), 1800).await;
|
||||
let cache_result = data
|
||||
.set_cache_key(format!("{}_roles", guild_uuid), roles.clone(), 1800)
|
||||
.await;
|
||||
|
||||
if let Err(error) = cache_result {
|
||||
error!("{}", error);
|
||||
|
@ -62,13 +75,18 @@ pub async fn get(req: HttpRequest, path: web::Path<(Uuid,)>, data: web::Data<Dat
|
|||
}
|
||||
|
||||
#[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> {
|
||||
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);
|
||||
|
||||
if let Err(error) = auth_header {
|
||||
return Ok(error)
|
||||
return Ok(error);
|
||||
}
|
||||
|
||||
let guild_uuid = path.into_inner().0;
|
||||
|
@ -76,7 +94,7 @@ pub async fn create(req: HttpRequest, role_info: web::Json<RoleInfo>, path: web:
|
|||
let authorized = check_access_token(auth_header.unwrap(), &data.pool).await;
|
||||
|
||||
if let Err(error) = authorized {
|
||||
return Ok(error)
|
||||
return Ok(error);
|
||||
}
|
||||
|
||||
let uuid = authorized.unwrap();
|
||||
|
|
|
@ -1,16 +1,25 @@
|
|||
use actix_web::{get, web, Error, HttpRequest, HttpResponse};
|
||||
use crate::{api::v1::auth::check_access_token, structs::{Member, Role}, utils::get_auth_header, Data};
|
||||
use crate::{
|
||||
Data,
|
||||
api::v1::auth::check_access_token,
|
||||
structs::{Member, Role},
|
||||
utils::get_auth_header,
|
||||
};
|
||||
use ::uuid::Uuid;
|
||||
use actix_web::{Error, HttpRequest, HttpResponse, get, web};
|
||||
use log::error;
|
||||
|
||||
#[get("{uuid}/roles/{role_uuid}")]
|
||||
pub async fn get(req: HttpRequest, path: web::Path<(Uuid, Uuid)>, data: web::Data<Data>) -> Result<HttpResponse, Error> {
|
||||
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);
|
||||
|
||||
if let Err(error) = auth_header {
|
||||
return Ok(error)
|
||||
return Ok(error);
|
||||
}
|
||||
|
||||
let (guild_uuid, role_uuid) = path.into_inner();
|
||||
|
@ -18,7 +27,7 @@ pub async fn get(req: HttpRequest, path: web::Path<(Uuid, Uuid)>, data: web::Dat
|
|||
let authorized = check_access_token(auth_header.unwrap(), &data.pool).await;
|
||||
|
||||
if let Err(error) = authorized {
|
||||
return Ok(error)
|
||||
return Ok(error);
|
||||
}
|
||||
|
||||
let uuid = authorized.unwrap();
|
||||
|
@ -32,18 +41,22 @@ pub async fn get(req: HttpRequest, path: web::Path<(Uuid, Uuid)>, data: web::Dat
|
|||
let cache_result = data.get_cache_key(format!("{}", role_uuid)).await;
|
||||
|
||||
if let Ok(cache_hit) = cache_result {
|
||||
return Ok(HttpResponse::Ok().content_type("application/json").body(cache_hit))
|
||||
return Ok(HttpResponse::Ok()
|
||||
.content_type("application/json")
|
||||
.body(cache_hit));
|
||||
}
|
||||
|
||||
let role_result = Role::fetch_one(&data.pool, guild_uuid, role_uuid).await;
|
||||
|
||||
if let Err(error) = role_result {
|
||||
return Ok(error)
|
||||
return Ok(error);
|
||||
}
|
||||
|
||||
let role = role_result.unwrap();
|
||||
|
||||
let cache_result = data.set_cache_key(format!("{}", role_uuid), role.clone(), 60).await;
|
||||
let cache_result = data
|
||||
.set_cache_key(format!("{}", role_uuid), role.clone(), 60)
|
||||
.await;
|
||||
|
||||
if let Err(error) = cache_result {
|
||||
error!("{}", error);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue