feat: coped in code from discord bot

This commit is contained in:
silver 2024-11-23 01:02:43 +00:00
parent 82d909693d
commit 17af5069b3
Signed by: silver
GPG key ID: 36F93D61BAD3FD7D
7 changed files with 1600 additions and 0 deletions

1450
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -4,3 +4,9 @@ version = "0.1.0"
edition = "2021"
[dependencies]
# for making teh requests
reqwest = { version = "0.12", features = ["json"] }
# parsing teh results
serde_json = { version = "1.0", features = ["raw_value"] }
serde = { version = "1.0.215", features = ["derive"] }

46
src/endpoints/get_cns.rs Normal file
View file

@ -0,0 +1,46 @@
use serde::{Deserialize, Serialize};
// This is what Wolves returns to us
#[derive(Deserialize, Serialize, Debug)]
struct WolvesResult {
success: i8,
result: Vec<WolvesResultCNS>,
}
// this is teh actual data we care about
#[derive(Deserialize, Serialize, Debug)]
struct WolvesResultCNS {
id: String,
name: String,
// Link to their page such as https://ulwolves.ie/society/computer
link: String,
// array of Committee members member_id's
committee: Vec<String>,
}
async fn get_committees(config: &Config) -> Vec<WolvesResultCNS> {
if config.wolves_url.is_empty() {
return vec![];
}
// TODO: Change teh stored env value to teh base domain
// TODO: this address may change
let url = format!("{}/get_cns", &config.wolves_url);
// get wolves data
if let Ok(mut res) = surf::post(&url).header("X-AM-Identity", &config.wolves_api).await {
if let Ok(WolvesResult {
success,
result,
}) = res.body_json().await
{
if success != 1 {
return vec![];
}
return result;
}
}
vec![]
}

View file

@ -0,0 +1,42 @@
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug)]
#[serde(untagged)]
pub enum WolvesResultUserResult {
B(bool),
S(String),
}
#[derive(Deserialize, Serialize, Debug)]
struct WolvesResultUser {
success: i64,
result: WolvesResultUserResult,
}
async fn get_user(wolves_url_base: &str, api_key: &str ,email: &str) -> Option<i64> {
let url = format!("{}/get_id_from_email", wolves_url_base);
match reqwest::Client::new()
.post(&url)
.form(&[("email", email)])
.header("X-AM-Identity", api_key)
.send()
.await
{
Ok(x) => {
if let Ok(y) = x.json::<WolvesResultUser>().await {
// this is the only time we will get a positive response, the None at the end catches everything else
if let WolvesResultUserResult::S(z) = y.result {
if let Ok(id) = z.parse::<i64>() {
return Some(id);
}
}
}
}
Err(e) => {
dbg!(e);
}
}
None
}

View file

@ -0,0 +1,51 @@
use serde::{Deserialize, Serialize};
#[derive(Deserialize, Serialize, Debug)]
struct WolvesResult {
success: i8,
result: Vec<WolvesResultUser>,
}
#[derive(Deserialize, Serialize, Debug)]
struct WolvesResultUser {
// TODO: Might be worth trying to get this replaced with the club/soc ID?
committee: String,
member_id: String,
first_name: String,
last_name: String,
contact_email: String,
opt_in_email: String,
student_id: Option<String>,
note: Option<String>,
expiry: String,
requested: String,
approved: String,
sitename: String,
domain: String,
}
async fn get_wolves_sub(wolves_url_base: &str, wolves_api: &str) -> Vec<WolvesResultUser> {
if wolves_url_base.is_empty() {
return vec![];
}
let url = format!("{}/get_members", wolves_url_base);
// get wolves data
if let Ok(mut res) = surf::post(&url).header("X-AM-Identity", wolves_api).await {
if let Ok(WolvesResult {
success,
result,
}) = res.body_json().await
{
if success != 1 {
return vec![];
}
return result;
}
}
vec![]
}

3
src/endpoints/mod.rs Normal file
View file

@ -0,0 +1,3 @@
mod get_members;
mod get_cns;
mod get_id_from_email;

View file

@ -1,3 +1,5 @@
mod endpoints;
pub fn add(left: u64, right: u64) -> u64 {
left + right
}