feat: backport changes from the #17-automate-onboarding-mk-ii branch
Some checks failed
On_Push / lint_fmt (push) Failing after 38s
On_Push / lint_clippy (push) Failing after 38s
On_Push / build (push) Has been skipped
On_Push / deploy (push) Has been skipped

This commit is contained in:
silver 2024-11-23 22:17:57 +00:00
parent 93359698f0
commit 37ea38f516
Signed by: silver
GPG key ID: 36F93D61BAD3FD7D
4 changed files with 433 additions and 96 deletions

View file

@ -38,6 +38,8 @@ pub struct Config {
// wolves API base for clubs/socs
pub wolves_url: String,
// API key for accessing more general resources
pub wolves_api: String,
}
impl TypeMapKey for Config {
type Value = Arc<RwLock<Config>>;
@ -63,6 +65,7 @@ pub fn get_config() -> Config {
mail_user: "".to_string(),
mail_pass: "".to_string(),
wolves_url: "".to_string(),
wolves_api: "".to_string(),
};
if let Ok(x) = env::var("DATABASE_HOME") {
@ -92,6 +95,9 @@ pub fn get_config() -> Config {
if let Ok(x) = env::var("WOLVES_URL") {
config.wolves_url = x.trim().to_string();
}
if let Ok(x) = env::var("WOLVES_API") {
config.wolves_api = x.trim().to_string();
}
config
}
@ -526,36 +532,8 @@ pub mod get_data {
use super::*;
use crate::set_roles::update_server;
use std::collections::BTreeMap;
use wolves_oxidised::WolvesUser;
#[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,
}
#[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;
@ -569,6 +547,9 @@ pub mod get_data {
};
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,
@ -581,7 +562,7 @@ pub mod get_data {
// 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 => {
@ -627,30 +608,7 @@ pub mod get_data {
.unwrap_or_default()
}
async fn get_wolves_sub(config: &Config, wolves_api: &str) -> Vec<WolvesResultUser> {
if config.wolves_url.is_empty() {
return vec![];
}
// get wolves data
if let Ok(mut res) = surf::post(&config.wolves_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_wolves(db: &Pool<Sqlite>, user: &WolvesResultUser) {
async fn add_users_wolves(db: &Pool<Sqlite>, user: &WolvesUser) {
// expiry
match sqlx::query_as::<_, Wolves>(
"
@ -671,7 +629,7 @@ pub mod get_data {
}
}
}
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: &WolvesUser) {
match sqlx::query_as::<_, ServerMembers>(
"
INSERT OR REPLACE INTO server_members (server, id_wolves, expiry)