Compare commits
3 commits
71f0cc14be
...
c79451a851
Author | SHA1 | Date | |
---|---|---|---|
c79451a851 | |||
caee16005d | |||
7ee500bf10 |
2 changed files with 115 additions and 7 deletions
|
@ -9,14 +9,14 @@ use log::error;
|
|||
|
||||
mod uuid;
|
||||
|
||||
#[derive(Serialize, FromRow)]
|
||||
#[derive(Serialize, Clone, FromRow)]
|
||||
struct ChannelPermission {
|
||||
role_uuid: String,
|
||||
permissions: i32
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct Channel {
|
||||
#[derive(Serialize, Clone)]
|
||||
pub struct Channel {
|
||||
uuid: String,
|
||||
name: String,
|
||||
description: Option<String>,
|
||||
|
@ -64,6 +64,37 @@ impl Channel {
|
|||
|
||||
Ok(channels?)
|
||||
}
|
||||
|
||||
pub async fn fetch_one(pool: &Pool<Postgres>, guild_uuid: Uuid, channel_uuid: Uuid) -> Result<Self, HttpResponse> {
|
||||
let row = sqlx::query_as(&format!("SELECT CAST(uuid AS VARCHAR), name, description FROM channels WHERE guild_uuid = '{}' AND uuid = '{}'", guild_uuid, channel_uuid))
|
||||
.fetch_one(pool)
|
||||
.await;
|
||||
|
||||
if let Err(error) = row {
|
||||
error!("{}", error);
|
||||
|
||||
return Err(HttpResponse::InternalServerError().finish())
|
||||
}
|
||||
|
||||
let (uuid, name, description): (String, String, Option<String>) = row.unwrap();
|
||||
|
||||
let row = sqlx::query_as(&format!("SELECT CAST(uuid AS VARCHAR), name, description FROM channels WHERE guild_uuid = '{}' AND uuid = '{}'", guild_uuid, channel_uuid))
|
||||
.fetch_all(pool)
|
||||
.await;
|
||||
|
||||
if let Err(error) = row {
|
||||
error!("{}", error);
|
||||
|
||||
return Err(HttpResponse::InternalServerError().finish())
|
||||
}
|
||||
|
||||
Ok(Self {
|
||||
uuid,
|
||||
name,
|
||||
description,
|
||||
permissions: row.unwrap(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[get("{uuid}/channels")]
|
||||
|
@ -96,14 +127,28 @@ pub async fn response(req: HttpRequest, path: web::Path<(Uuid,)>, data: web::Dat
|
|||
return Ok(HttpResponse::InternalServerError().finish())
|
||||
}
|
||||
|
||||
let member_uuid = Uuid::from_str(&row.unwrap()).unwrap();
|
||||
let _member_uuid = Uuid::from_str(&row.unwrap()).unwrap();
|
||||
|
||||
let cache_result = data.get_cache_key(format!("{}_channels", guild_uuid)).await;
|
||||
|
||||
let channels = Channel::fetch_all(&data.pool, guild_uuid).await;
|
||||
if let Ok(cache_hit) = cache_result {
|
||||
return Ok(HttpResponse::Ok().content_type("application/json").body(cache_hit))
|
||||
}
|
||||
|
||||
if let Err(error) = channels {
|
||||
let channels_result = Channel::fetch_all(&data.pool, guild_uuid).await;
|
||||
|
||||
if let Err(error) = channels_result {
|
||||
return Ok(error)
|
||||
}
|
||||
|
||||
Ok(HttpResponse::Ok().json(channels.unwrap()))
|
||||
let channels = channels_result.unwrap();
|
||||
|
||||
let cache_result = data.set_cache_key(format!("{}_channels", guild_uuid), channels.clone(), 1800).await;
|
||||
|
||||
if let Err(error) = cache_result {
|
||||
error!("{}", error);
|
||||
return Ok(HttpResponse::InternalServerError().finish());
|
||||
}
|
||||
|
||||
Ok(HttpResponse::Ok().json(channels))
|
||||
}
|
||||
|
|
|
@ -1 +1,64 @@
|
|||
mod messages;
|
||||
use std::str::FromStr;
|
||||
|
||||
use actix_web::{get, web, Error, HttpRequest, HttpResponse};
|
||||
use crate::{api::v1::auth::check_access_token, utils::get_auth_header, Data};
|
||||
use ::uuid::Uuid;
|
||||
use log::error;
|
||||
use super::Channel;
|
||||
|
||||
#[get("{uuid}/channels/{channel_uuid}")]
|
||||
pub async fn response(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)
|
||||
}
|
||||
|
||||
let (guild_uuid, channel_uuid) = path.into_inner();
|
||||
|
||||
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())
|
||||
}
|
||||
|
||||
let _member_uuid = Uuid::from_str(&row.unwrap()).unwrap();
|
||||
|
||||
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))
|
||||
}
|
||||
|
||||
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(format!("{}", channel_uuid), channel.clone(), 60).await;
|
||||
|
||||
if let Err(error) = cache_result {
|
||||
error!("{}", error);
|
||||
return Ok(HttpResponse::InternalServerError().finish());
|
||||
}
|
||||
|
||||
Ok(HttpResponse::Ok().json(channel))
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue