Compare commits
3 commits
8821287cbe
...
2e4860323e
Author | SHA1 | Date | |
---|---|---|---|
2e4860323e | |||
1aabe9e524 | |||
ef5fc96d67 |
2 changed files with 102 additions and 2 deletions
|
@ -1,7 +1,7 @@
|
|||
use std::str::FromStr;
|
||||
|
||||
use actix_web::{get, web, Error, HttpRequest, HttpResponse};
|
||||
use serde::Serialize;
|
||||
use actix_web::{get, post, web, Error, HttpRequest, HttpResponse};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sqlx::{prelude::FromRow, Pool, Postgres};
|
||||
use crate::{api::v1::auth::check_access_token, utils::get_auth_header, Data};
|
||||
use ::uuid::Uuid;
|
||||
|
@ -97,6 +97,12 @@ impl Channel {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct ChannelInfo {
|
||||
name: String,
|
||||
description: Option<String>
|
||||
}
|
||||
|
||||
#[get("{uuid}/channels")]
|
||||
pub async fn response(req: HttpRequest, path: web::Path<(Uuid,)>, data: web::Data<Data>) -> Result<HttpResponse, Error> {
|
||||
let headers = req.headers();
|
||||
|
@ -152,3 +158,75 @@ pub async fn response(req: HttpRequest, path: web::Path<(Uuid,)>, data: web::Dat
|
|||
|
||||
Ok(HttpResponse::Ok().json(channels))
|
||||
}
|
||||
|
||||
#[post("{uuid}/channels")]
|
||||
pub async fn response_post(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)
|
||||
}
|
||||
|
||||
let guild_uuid = path.into_inner().0;
|
||||
|
||||
let authorized = check_access_token(auth_header.unwrap(), &data.pool).await;
|
||||
|
||||
if let Err(error) = authorized {
|
||||
return Ok(error)
|
||||
}
|
||||
|
||||
let uuid = authorized.unwrap();
|
||||
|
||||
let row: Result<String, sqlx::Error> = sqlx::query_scalar(&format!("SELECT CAST(uuid AS VARCHAR) FROM guild_members WHERE guild_uuid = '{}' AND user_uuid = '{}'", guild_uuid, uuid))
|
||||
.fetch_one(&data.pool)
|
||||
.await;
|
||||
|
||||
if let Err(error) = row {
|
||||
error!("{}", error);
|
||||
|
||||
return Ok(HttpResponse::InternalServerError().finish())
|
||||
}
|
||||
|
||||
// FIXME: Logic to check permissions, should probably be done in utils.rs
|
||||
|
||||
let _member_uuid = Uuid::from_str(&row.unwrap()).unwrap();
|
||||
|
||||
let channel_uuid = Uuid::now_v7();
|
||||
|
||||
let row = sqlx::query(&format!("INSERT INTO channels (uuid, guild_uuid, name, description) VALUES ('{}', '{}', $1, $2)", channel_uuid, guild_uuid))
|
||||
.bind(&channel_info.name)
|
||||
.bind(&channel_info.description)
|
||||
.execute(&data.pool)
|
||||
.await;
|
||||
|
||||
if let Err(error) = row {
|
||||
error!("{}", error);
|
||||
return Ok(HttpResponse::InternalServerError().finish())
|
||||
}
|
||||
|
||||
let channel_result = Channel::fetch_one(&data.pool, guild_uuid, channel_uuid).await;
|
||||
|
||||
if let Err(error) = channel_result {
|
||||
return Ok(error)
|
||||
}
|
||||
|
||||
let channel = channel_result.unwrap();
|
||||
|
||||
let cache_result = data.set_cache_key(channel_uuid.to_string(), channel.clone(), 1800).await;
|
||||
|
||||
if let Err(error) = cache_result {
|
||||
error!("{}", error);
|
||||
return Ok(HttpResponse::InternalServerError().finish());
|
||||
}
|
||||
|
||||
let cache_deletion_result = data.del_cache_key(format!("{}_channels", guild_uuid)).await;
|
||||
|
||||
if let Err(error) = cache_deletion_result {
|
||||
error!("{}", error);
|
||||
return Ok(HttpResponse::InternalServerError().finish());
|
||||
}
|
||||
|
||||
Ok(HttpResponse::Ok().json(channel))
|
||||
}
|
||||
|
|
22
src/utils.rs
22
src/utils.rs
|
@ -6,6 +6,20 @@ use serde::Serialize;
|
|||
|
||||
use crate::Data;
|
||||
|
||||
enum Permissions {
|
||||
SendMessage = 1,
|
||||
CreateChannel = 2,
|
||||
DeleteChannel = 4,
|
||||
ManageChannel = 8,
|
||||
CreateRole = 16,
|
||||
DeleteRole = 32,
|
||||
ManageRole = 64,
|
||||
CreateInvite = 128,
|
||||
ManageInvite = 256,
|
||||
ManageServer = 512,
|
||||
ManageMember = 1024,
|
||||
}
|
||||
|
||||
pub fn get_auth_header(headers: &HeaderMap) -> Result<&str, HttpResponse> {
|
||||
let auth_token = headers.get(actix_web::http::header::AUTHORIZATION);
|
||||
|
||||
|
@ -70,5 +84,13 @@ impl Data {
|
|||
|
||||
redis::cmd("GET").arg(key_encoded).query_async(&mut conn).await
|
||||
}
|
||||
|
||||
pub async fn del_cache_key(&self, key: String) -> Result<(), RedisError> {
|
||||
let mut conn = self.cache_pool.get_multiplexed_tokio_connection().await?;
|
||||
|
||||
let key_encoded = encode(key);
|
||||
|
||||
redis::cmd("DEL").arg(key_encoded).query_async(&mut conn).await
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue