feat: switched over to using a library to interact with teh wolves API
This commit is contained in:
parent
77a7b7b81d
commit
ca55a78447
4 changed files with 85 additions and 255 deletions
|
@ -52,32 +52,13 @@ pub mod cns {
|
|||
use crate::common::set_roles::normal::update_server;
|
||||
use crate::common::wolves::{add_users_wolves, WolvesResultUserMin};
|
||||
use crate::Config;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serenity::client::Context;
|
||||
use serenity::model::id::GuildId;
|
||||
use sqlx::{Pool, Sqlite};
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
#[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,
|
||||
}
|
||||
|
||||
impl From<&WolvesResultUser> for WolvesResultUserMin {
|
||||
fn from(value: &WolvesResultUser) -> Self {
|
||||
impl From<&wolves_oxidised::WolvesUser> for WolvesResultUserMin {
|
||||
fn from(value: &wolves_oxidised::WolvesUser) -> Self {
|
||||
Self {
|
||||
member_id: value.member_id.to_owned(),
|
||||
contact_email: value.contact_email.to_owned(),
|
||||
|
@ -85,18 +66,6 @@ pub mod cns {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize, Debug)]
|
||||
struct WolvesResult {
|
||||
success: i8,
|
||||
result: Vec<WolvesResultUser>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize, Debug)]
|
||||
struct WolvesResultLocal {
|
||||
pub id_wolves: String,
|
||||
pub email: String,
|
||||
pub expiry: String,
|
||||
}
|
||||
pub async fn get_wolves(ctx: &Context) {
|
||||
let db_lock = {
|
||||
let data_read = ctx.data.read().await;
|
||||
|
@ -110,9 +79,13 @@ pub mod cns {
|
|||
};
|
||||
let config = config_lock.read().await;
|
||||
|
||||
// set up teh client
|
||||
let wolves = wolves_oxidised::Client::new(&config.wolves_url, Some(&config.wolves_api));
|
||||
|
||||
for server_config in get_server_config_bulk(&db).await {
|
||||
let Servers {
|
||||
server,
|
||||
// this is the unique api key for each club/soc
|
||||
wolves_api,
|
||||
..
|
||||
} = &server_config;
|
||||
|
@ -122,7 +95,7 @@ pub mod cns {
|
|||
|
||||
// list of users that need to be updated for this server
|
||||
let mut user_to_update = vec![];
|
||||
for user in get_wolves_sub(&config, wolves_api).await {
|
||||
for user in wolves.get_members(wolves_api).await {
|
||||
let id = user.member_id.parse::<u64>().unwrap_or_default();
|
||||
match existing.get(&(id as i64)) {
|
||||
None => {
|
||||
|
@ -168,32 +141,7 @@ pub mod cns {
|
|||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
async fn get_wolves_sub(config: &Config, wolves_api: &str) -> Vec<WolvesResultUser> {
|
||||
if config.wolves_url.is_empty() {
|
||||
return vec![];
|
||||
}
|
||||
|
||||
let url = format!("{}/get_members", &config.wolves_url);
|
||||
|
||||
// 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![]
|
||||
}
|
||||
|
||||
async fn add_users_server_members(db: &Pool<Sqlite>, server: &GuildId, user: &WolvesResultUser) {
|
||||
async fn add_users_server_members(db: &Pool<Sqlite>, server: &GuildId, user: &wolves_oxidised::WolvesUser) {
|
||||
match sqlx::query_as::<_, ServerMembers>(
|
||||
"
|
||||
INSERT OR REPLACE INTO server_members (server, id_wolves, expiry)
|
||||
|
@ -221,28 +169,9 @@ pub mod cns {
|
|||
pub mod committees {
|
||||
use crate::common::database::DataBase;
|
||||
use crate::Config;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serenity::client::Context;
|
||||
use sqlx::{Pool, Sqlite};
|
||||
|
||||
// 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>,
|
||||
}
|
||||
|
||||
// Database entry for it
|
||||
#[derive(Debug, Clone, sqlx::FromRow)]
|
||||
pub struct Committees {
|
||||
|
@ -253,8 +182,8 @@ pub mod committees {
|
|||
pub committee: Vec<i64>,
|
||||
}
|
||||
|
||||
impl From<WolvesResultCNS> for Committees {
|
||||
fn from(value: WolvesResultCNS) -> Self {
|
||||
impl From<wolves_oxidised::WolvesCNS> for Committees {
|
||||
fn from(value: wolves_oxidised::WolvesCNS) -> Self {
|
||||
Self {
|
||||
id: value.id.parse().unwrap_or(0),
|
||||
name: value.name,
|
||||
|
@ -277,40 +206,14 @@ pub mod committees {
|
|||
};
|
||||
let config = config_lock.read().await;
|
||||
|
||||
let wolves = wolves_oxidised::Client::new(&config.wolves_url, Some(&config.wolves_api));
|
||||
// request data from wolves
|
||||
for committee_wolves in get_committees(&config).await {
|
||||
for committee_wolves in wolves.get_committees().await {
|
||||
let committee = Committees::from(committee_wolves);
|
||||
add_committee(&db, &committee).await;
|
||||
}
|
||||
}
|
||||
|
||||
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![]
|
||||
}
|
||||
|
||||
async fn add_committee(db: &Pool<Sqlite>, committee: &Committees) {
|
||||
match sqlx::query_as::<_, Committees>(
|
||||
"
|
||||
|
@ -334,100 +237,3 @@ pub mod committees {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
get the data for an individual user
|
||||
*/
|
||||
pub mod individual {
|
||||
use crate::common::database::DataBase;
|
||||
use crate::common::wolves::{add_users_wolves, WolvesResultUserMin};
|
||||
use crate::Config;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serenity::client::Context;
|
||||
|
||||
#[derive(Deserialize, Serialize, Debug)]
|
||||
struct WolvesResultUser {
|
||||
// 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,
|
||||
}
|
||||
|
||||
impl From<&WolvesResultUser> for WolvesResultUserMin {
|
||||
fn from(value: &WolvesResultUser) -> Self {
|
||||
Self {
|
||||
member_id: value.member_id.to_owned(),
|
||||
contact_email: value.contact_email.to_owned(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize, Debug)]
|
||||
struct WolvesResult {
|
||||
success: i8,
|
||||
result: WolvesResultUser,
|
||||
}
|
||||
|
||||
pub async fn get_user(ctx: &Context, email: &str) -> bool {
|
||||
let db_lock = {
|
||||
let data_read = ctx.data.read().await;
|
||||
data_read.get::<DataBase>().expect("Expected Database in TypeMap.").clone()
|
||||
};
|
||||
let db = db_lock.read().await;
|
||||
|
||||
let config_lock = {
|
||||
let data_read = ctx.data.read().await;
|
||||
data_read.get::<Config>().expect("Expected Config in TypeMap.").clone()
|
||||
};
|
||||
let config = config_lock.read().await;
|
||||
|
||||
// TODO: proper api key management
|
||||
let api_key = "";
|
||||
// request data from wolves
|
||||
match get_user_sub(&config, api_key, email).await {
|
||||
None => false,
|
||||
// if exists save it and return true
|
||||
Some(user) => {
|
||||
// add to db
|
||||
add_users_wolves(&db, &WolvesResultUserMin::from(&user)).await;
|
||||
|
||||
true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn get_user_sub(config: &Config, wolves_api: &str, _email: &str) -> Option<WolvesResultUser> {
|
||||
if config.wolves_url.is_empty() {
|
||||
return None;
|
||||
}
|
||||
|
||||
// TODO: Change teh stored env value to teh base domain
|
||||
let url = format!("{}/get_member", &config.wolves_url);
|
||||
|
||||
// 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 None;
|
||||
}
|
||||
|
||||
return Some(result);
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue