Parcourir la source

Fix JSON typing errors in type field (str -> int)

master
NGnius (Graham) il y a 3 ans
Parent
révision
3ecd599b65
3 fichiers modifiés avec 59 ajouts et 15 suppressions
  1. +4
    -4
      src/auth_tools.rs
  2. +51
    -7
      src/discord.rs
  3. +4
    -4
      src/main.rs

+ 4
- 4
src/auth_tools.rs Voir le fichier

@@ -3,7 +3,7 @@ use rocket::http::{Status};
use rocket::{Request, Data, Outcome};
use std::io::Read;
use ed25519_dalek::{Verifier, Signature};
use crate::discord::Interaction;
use crate::discord::{Interaction, InteractionRaw};

pub struct AuthenticatedInteraction {
signature: String,
@@ -50,15 +50,15 @@ impl FromDataSimple for AuthenticatedInteraction {
println!("Signature validation error");
return Outcome::Failure((Status::Unauthorized, ()))
}
if let Ok(payload) = serde_json::from_str(&string_buf[..string_len]) {
if let Ok(payload) = serde_json::from_str::<InteractionRaw>(&string_buf[..string_len]) {
let auth = AuthenticatedInteraction {
signature: signature.clone(),
timestamp: timestamp.clone(),
interaction: payload,
interaction: payload.interaction(),
};
return Outcome::Success(auth);
}
println!("Invalid json payload");
println!("Invalid json payload {}\n({})", &string_buf[..string_len], serde_json::from_str::<Interaction>(&string_buf[..string_len]).err().unwrap());
return Outcome::Failure((Status::Unauthorized, ()))
}
println!("Invalid body");


+ 51
- 7
src/discord.rs Voir le fichier

@@ -1,5 +1,30 @@
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Clone)]
pub struct InteractionRaw {
#[serde(rename = "type")]
pub type_: usize,
pub token: Option<String>,
pub id: Option<String>,
pub guild_id: Option<String>,
pub channel_id: Option<String>,
}

impl InteractionRaw {
pub fn interaction(&self) -> Interaction {
match self.type_ {
1 => Interaction::Ping {},
2 => Interaction::Command {
token: self.token.clone().unwrap(),
id: self.id.clone().unwrap(),
guild_id: self.guild_id.clone().unwrap(),
channel_id: self.channel_id.clone().unwrap(),
},
_ => Interaction::Invalid {},
}
}
}

#[derive(Serialize, Deserialize, Clone)]
#[serde(tag = "type")]
pub enum Interaction {
@@ -15,6 +40,7 @@ pub enum Interaction {
channel_id: String,
//version: usize,
},
Invalid {},
}

impl Interaction {
@@ -22,29 +48,47 @@ impl Interaction {
match self {
Self::Ping {..} => true,
Self::Command {..} => false,
Self::Invalid {..} => false,
}
}
}

#[derive(Serialize, Deserialize, Clone)]
#[serde(tag = "type")]
pub enum InteractionResponse {
#[serde(rename = "1")]
//#[serde(rename = "1")]
Pong {},
#[serde(rename = "2")]
//#[serde(rename = "2")]
Acknowledge {},
#[serde(rename = "3")]
//#[serde(rename = "3")]
ChannelMessage {
data: Option<InteractionApplicationCommandCallbackData>,
},
#[serde(rename = "4")]
//#[serde(rename = "4")]
ChannelMessageWithSource {
data: Option<InteractionApplicationCommandCallbackData>,
},
#[serde(rename = "5")]
//#[serde(rename = "5")]
AcknowledgeWithSource {},
}

impl InteractionResponse {
pub fn raw(&self) -> InteractionResponseRaw {
match self {
InteractionResponse::Pong { .. } => InteractionResponseRaw {type_: 1, data:None},
InteractionResponse::Acknowledge { .. } => InteractionResponseRaw {type_: 2, data:None},
InteractionResponse::ChannelMessage { data, .. } => InteractionResponseRaw {type_: 3, data: data.clone()},
InteractionResponse::ChannelMessageWithSource { data, .. } => InteractionResponseRaw {type_: 4, data: data.clone()},
InteractionResponse::AcknowledgeWithSource { .. } => InteractionResponseRaw {type_: 5, data:None},
}
}
}

#[derive(Serialize, Deserialize, Clone)]
pub struct InteractionResponseRaw {
#[serde(rename = "type")]
pub type_: usize,
pub data: Option<InteractionApplicationCommandCallbackData>,
}

#[derive(Serialize, Deserialize, Clone)]
pub struct InteractionApplicationCommandCallbackData {
pub tts: bool,


+ 4
- 4
src/main.rs Voir le fichier

@@ -10,7 +10,7 @@ use lazy_static::lazy_static;
use rocket_contrib::json::Json;
use std::sync::RwLock;
use crate::auth_tools::{AuthenticatedInteraction};
use crate::discord::{InteractionResponse, InteractionApplicationCommandCallbackData, ApplicationCommand};
use crate::discord::{InteractionResponse, InteractionApplicationCommandCallbackData, ApplicationCommand, InteractionResponseRaw};
use crate::command_definitions::hello_world;
use std::collections::{HashMap, HashSet};

@@ -23,9 +23,9 @@ lazy_static! {
}

#[post("/", data = "<interaction>")]
fn root_post(interaction: AuthenticatedInteraction) -> Json<InteractionResponse> {
fn root_post(interaction: AuthenticatedInteraction) -> Json<InteractionResponseRaw> {
if interaction.interaction.is_ping() {
return Json(InteractionResponse::Pong {})
return Json(InteractionResponse::Pong{}.raw())
}
let resp = InteractionResponse::ChannelMessage {
data: Some(InteractionApplicationCommandCallbackData {
@@ -34,7 +34,7 @@ fn root_post(interaction: AuthenticatedInteraction) -> Json<InteractionResponse>
allowed_mentions: None,
}),
};
Json(resp)
Json(resp.raw())
}

#[get("/")]


Chargement…
Annuler
Enregistrer