Refactored and fixed api endpoints

This commit is contained in:
Benjamin Sherriff
2023-12-02 13:52:01 -05:00
parent 046bf51697
commit dc2ff172b0
11 changed files with 238 additions and 182 deletions

View File

@@ -4,7 +4,7 @@ use actix_web::{get, post, web, HttpResponse, ResponseError};
use log::warn;
use serde::{Serialize, Deserialize};
use serenity::model::prelude::{GuildChannel, ChannelType};
use siren::ServiceError;
use siren::{ServiceError, Response};
use crate::{AppState, bot::commands::audio::{play::play_track, join}, storage::guilds::QueryGuild, auth::{JwtAuth, verify_role}};
@@ -22,7 +22,10 @@ async fn get_guilds(data: web::Data<Arc<AppState>>, auth: JwtAuth) -> HttpRespon
message: err.to_string()
})
};
HttpResponse::Ok().json(guilds)
HttpResponse::Ok().json(Response {
data: guilds,
metadata: None
})
}
#[get("/{id}/text")]
@@ -39,7 +42,10 @@ async fn get_text_channels(id: web::Path<String>, data: web::Data<Arc<AppState>>
message: err.to_string()
})
};
HttpResponse::Ok().json(channels)
HttpResponse::Ok().json(Response {
data: channels,
metadata: None
})
}
#[get("/{id}/voice")]
@@ -56,7 +62,10 @@ async fn get_voice_channels(id: web::Path<String>, data: web::Data<Arc<AppState>
message: err.to_string()
})
};
HttpResponse::Ok().json(channels)
HttpResponse::Ok().json(Response {
data: channels,
metadata: None
})
}
#[derive(Serialize, Deserialize)]
@@ -74,7 +83,6 @@ async fn send_message(path: web::Path<(String, String)>, text: web::Json<Channel
let guild_id = match guild_id.parse::<u64>() {
Ok(id) => id,
Err(err) => {
warn!("Could not parse guild id: {:?}", err);
return ResponseError::error_response(&ServiceError {
status: 422,
message: err.to_string()
@@ -84,7 +92,6 @@ async fn send_message(path: web::Path<(String, String)>, text: web::Json<Channel
let channel_id = match channel_id.parse::<u64>() {
Ok(id) => id,
Err(err) => {
warn!("Could not parse channel id: {:?}", err);
return ResponseError::error_response(&ServiceError {
status: 422,
message: err.to_string()
@@ -95,7 +102,6 @@ async fn send_message(path: web::Path<(String, String)>, text: web::Json<Channel
let channels = match channel_results {
Ok(channels) => channels,
Err(err) => {
warn!("Could not get channels: {:?}", err);
return ResponseError::error_response(&ServiceError {
status: 422,
message: err.to_string()
@@ -106,7 +112,6 @@ async fn send_message(path: web::Path<(String, String)>, text: web::Json<Channel
let channel = match channels.iter().find(|c| c.id.0 == channel_id) {
Some(channel) => channel,
None => {
warn!("Could not find channel with id {}", channel_id);
return ResponseError::error_response(&ServiceError {
status: 422,
message: format!("Could not find channel with id {}", channel_id)
@@ -115,7 +120,6 @@ async fn send_message(path: web::Path<(String, String)>, text: web::Json<Channel
};
if let Err(err) = channel.say(&Pin::new(&data.http).get_ref(), &text.message).await {
warn!("Could not send message: {:?}", err);
return ResponseError::error_response(&ServiceError {
status: 422,
message: err.to_string()
@@ -140,14 +144,12 @@ async fn play(path: web::Path<(String, String)>, play_request: web::Json<PlayReq
let guild_id = match guild_id.parse::<u64>() {
Ok(id) => id,
Err(err) => {
warn!("Could not parse guild id: {:?}", err);
return ResponseError::error_response(&ServiceError { status: 422, message: err.to_string() })
}
};
let channel_id = match channel_id.parse::<u64>() {
Ok(id) => id,
Err(err) => {
warn!("Could not parse channel id: {:?}", err);
return ResponseError::error_response(&ServiceError { status: 422, message: err.to_string() })
}
};
@@ -155,14 +157,12 @@ async fn play(path: web::Path<(String, String)>, play_request: web::Json<PlayReq
let guild = match http.get_guild(guild_id).await {
Ok(guild) => guild,
Err(err) => {
warn!("Could not get guild: {:?}", err);
return ResponseError::error_response(&ServiceError { status: 422, message: err.to_string() })
}
};
let channel = match http.get_channel(channel_id).await {
Ok(channel) => channel,
Err(err) => {
warn!("Could not get channel: {:?}", err);
return ResponseError::error_response(&ServiceError { status: 422, message: err.to_string() })
}
};
@@ -174,13 +174,11 @@ async fn play(path: web::Path<(String, String)>, play_request: web::Json<PlayReq
match play_track(Arc::clone(&data.songbird), guild.id, play_request.track_url.to_string()).await {
Ok(_) => HttpResponse::Ok().finish(),
Err(err) => {
warn!("Could not play track: {:?}", err);
return ResponseError::error_response(&err)
}
}
},
Err(err) => {
warn!("Could not join channel: {:?}", err);
return ResponseError::error_response(&ServiceError { status: 500, message: err.to_string() })
}
}
@@ -196,7 +194,6 @@ async fn stop(path: web::Path<String>, data: web::Data<Arc<AppState>>, auth: Jwt
let guild_id = match guild_id.parse::<u64>() {
Ok(id) => id,
Err(err) => {
warn!("Could not parse guild id: {:?}", err);
return ResponseError::error_response(&ServiceError {
status: 422,
message: err.to_string()
@@ -222,7 +219,6 @@ async fn resume(path: web::Path<String>, data: web::Data<Arc<AppState>>, auth: J
let guild_id = match guild_id.parse::<u64>() {
Ok(id) => id,
Err(err) => {
warn!("Could not parse guild id: {:?}", err);
return ResponseError::error_response(&ServiceError {
status: 422,
message: err.to_string()
@@ -233,7 +229,6 @@ async fn resume(path: web::Path<String>, data: web::Data<Arc<AppState>>, auth: J
if let Some(handler_lock) = data.songbird.get(guild_id) {
let handler = handler_lock.lock().await;
if let Err(err) = handler.queue().resume() {
warn!("Could not resume track: {:?}", err);
return ResponseError::error_response(&ServiceError {
status: 422,
message: err.to_string()
@@ -254,7 +249,6 @@ async fn pause(path: web::Path<String>, data: web::Data<Arc<AppState>>, auth: Jw
let guild_id = match guild_id.parse::<u64>() {
Ok(id) => id,
Err(err) => {
warn!("Could not parse guild id: {:?}", err);
return ResponseError::error_response(&ServiceError {
status: 422,
message: err.to_string()
@@ -265,7 +259,6 @@ async fn pause(path: web::Path<String>, data: web::Data<Arc<AppState>>, auth: Jw
if let Some(handler_lock) = data.songbird.get(guild_id) {
let handler = handler_lock.lock().await;
if let Err(err) = handler.queue().pause() {
warn!("Could not pause track: {:?}", err);
return ResponseError::error_response(&ServiceError {
status: 422,
message: err.to_string()
@@ -291,7 +284,6 @@ async fn get_volume(path: web::Path<String>, auth: JwtAuth) -> HttpResponse {
let guild_id = match guild_id.parse::<u64>() {
Ok(id) => id,
Err(err) => {
warn!("Could not parse guild id: {:?}", err);
return ResponseError::error_response(&ServiceError {
status: 422,
message: err.to_string()
@@ -302,7 +294,6 @@ async fn get_volume(path: web::Path<String>, auth: JwtAuth) -> HttpResponse {
let volume = match QueryGuild::get(guild_id as i64) {
Ok(guild) => guild.volume,
Err(err) => {
warn!("Could not get volume: {:?}", err);
return ResponseError::error_response(&ServiceError {
status: 422,
message: err.to_string()
@@ -323,7 +314,6 @@ async fn set_volume(path: web::Path<String>, volume: web::Json::<SetVolume>, dat
let guild_id = match guild_id.parse::<u64>() {
Ok(id) => id,
Err(err) => {
warn!("Could not parse guild id: {:?}", err);
return ResponseError::error_response(&ServiceError {
status: 422,
message: err.to_string()
@@ -337,7 +327,6 @@ async fn set_volume(path: web::Path<String>, volume: web::Json::<SetVolume>, dat
let guild = match http.get_guild(guild_id).await {
Ok(guild) => guild,
Err(err) => {
warn!("Could not get guild: {:?}", err);
return ResponseError::error_response(&ServiceError { status: 422, message: err.to_string() })
}
};
@@ -356,7 +345,6 @@ async fn skip(path: web::Path<String>, data: web::Data<Arc<AppState>>, auth: Jwt
let guild_id = match guild_id.parse::<u64>() {
Ok(id) => id,
Err(err) => {
warn!("Could not parse guild id: {:?}", err);
return ResponseError::error_response(&ServiceError {
status: 422,
message: err.to_string()
@@ -367,7 +355,6 @@ async fn skip(path: web::Path<String>, data: web::Data<Arc<AppState>>, auth: Jwt
if let Some(handler_lock) = data.songbird.get(guild_id) {
let handler = handler_lock.lock().await;
if let Err(err) = handler.queue().skip() {
warn!("Could not skip track: {:?}", err);
return ResponseError::error_response(&ServiceError {
status: 422,
message: err.to_string()

View File

@@ -6,7 +6,7 @@ use serenity::model::Permissions;
use serenity::model::channel::Message;
use serenity::model::prelude::{ChannelType, PermissionOverwrite, PermissionOverwriteType};
use serenity::prelude::*;
use siren::{GetResponse, ServiceError};
use siren::{Response, ServiceError};
pub struct OAI {
pub client: reqwest::Client,
@@ -160,7 +160,7 @@ impl OAI {
Ok(response)
}
async fn get_messages(&self, guild_id: u64, channel_id: u64, author_id: u64) -> Result<GetResponse<Vec<siren::Message>>, ServiceError> {
async fn get_messages(&self, guild_id: u64, channel_id: u64, author_id: u64) -> Result<Response<Vec<siren::Message>>, ServiceError> {
let uri = format!("{}/messages?guild_id={}&channel_id={}&author_id={}&limit={}", self.service_url, guild_id, channel_id, author_id, self.max_context_questions);
let value = self.client
.get(&uri)
@@ -169,7 +169,7 @@ impl OAI {
.json::<Value>()
.await?;
let response = serde_json::from_value::<GetResponse<Vec<siren::Message>>>(value)?;
let response = serde_json::from_value::<Response<Vec<siren::Message>>>(value)?;
Ok(response)
}