Merge branch '#5_wolves-api' into 'main'
feat: modified the flake to use the new api Closes #5 See merge request compsoc1/skynet/discord-bot!3
This commit is contained in:
commit
b228c4a061
5 changed files with 69 additions and 160 deletions
22
Cargo.lock
generated
22
Cargo.lock
generated
|
@ -540,27 +540,6 @@ dependencies = [
|
||||||
"subtle",
|
"subtle",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "csv"
|
|
||||||
version = "1.2.2"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "626ae34994d3d8d668f4269922248239db4ae42d538b14c398b74a52208e8086"
|
|
||||||
dependencies = [
|
|
||||||
"csv-core",
|
|
||||||
"itoa",
|
|
||||||
"ryu",
|
|
||||||
"serde",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "csv-core"
|
|
||||||
version = "0.1.10"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "2b2466559f260f48ad25fe6317b3c8dac77b5bdb5763ac7d9d6103530663bc90"
|
|
||||||
dependencies = [
|
|
||||||
"memchr",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "ctr"
|
name = "ctr"
|
||||||
version = "0.6.0"
|
version = "0.6.0"
|
||||||
|
@ -2418,7 +2397,6 @@ name = "skynet_discord_bot"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"chrono",
|
"chrono",
|
||||||
"csv",
|
|
||||||
"dotenvy",
|
"dotenvy",
|
||||||
"lettre",
|
"lettre",
|
||||||
"maud",
|
"maud",
|
||||||
|
|
|
@ -29,9 +29,6 @@ rand = "0.8.5"
|
||||||
# fancy time stuff
|
# fancy time stuff
|
||||||
chrono = "0.4.26"
|
chrono = "0.4.26"
|
||||||
|
|
||||||
# handlign teh csv export from wolves
|
|
||||||
csv = "1.2"
|
|
||||||
|
|
||||||
# for email
|
# for email
|
||||||
lettre = "0.10.4"
|
lettre = "0.10.4"
|
||||||
maud = "0.25.0"
|
maud = "0.25.0"
|
||||||
|
|
|
@ -50,7 +50,6 @@
|
||||||
SKYNET_SERVER = cfg.discord.server;
|
SKYNET_SERVER = cfg.discord.server;
|
||||||
HOME = cfg.home;
|
HOME = cfg.home;
|
||||||
DATABASE = "database.db";
|
DATABASE = "database.db";
|
||||||
CSV = "wolves.csv";
|
|
||||||
};
|
};
|
||||||
|
|
||||||
service_name = script: lib.strings.sanitizeDerivationName("${cfg.user}@${script}");
|
service_name = script: lib.strings.sanitizeDerivationName("${cfg.user}@${script}");
|
||||||
|
@ -71,6 +70,7 @@
|
||||||
"${cfg.env.ldap}"
|
"${cfg.env.ldap}"
|
||||||
"${cfg.env.discord}"
|
"${cfg.env.discord}"
|
||||||
"${cfg.env.mail}"
|
"${cfg.env.mail}"
|
||||||
|
"${cfg.env.wolves}"
|
||||||
];
|
];
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
@ -113,6 +113,10 @@
|
||||||
type = types.str;
|
type = types.str;
|
||||||
description = "ENV file with EMAIL_SMTP, EMAIL_USER, EMAIL_PASS";
|
description = "ENV file with EMAIL_SMTP, EMAIL_USER, EMAIL_PASS";
|
||||||
};
|
};
|
||||||
|
wolves = mkOption rec {
|
||||||
|
type = types.str;
|
||||||
|
description = "Mail details, has WOLVES_URL, WOLVES_KEY";
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
discord = {
|
discord = {
|
||||||
|
@ -178,12 +182,14 @@
|
||||||
"${cfg.env.ldap}"
|
"${cfg.env.ldap}"
|
||||||
"${cfg.env.discord}"
|
"${cfg.env.discord}"
|
||||||
"${cfg.env.mail}"
|
"${cfg.env.mail}"
|
||||||
|
"${cfg.env.wolves}"
|
||||||
];
|
];
|
||||||
};
|
};
|
||||||
restartTriggers = [
|
restartTriggers = [
|
||||||
"${cfg.env.ldap}"
|
"${cfg.env.ldap}"
|
||||||
"${cfg.env.discord}"
|
"${cfg.env.discord}"
|
||||||
"${cfg.env.mail}"
|
"${cfg.env.mail}"
|
||||||
|
"${cfg.env.wolves}"
|
||||||
];
|
];
|
||||||
};
|
};
|
||||||
} // serviceGenerator scripts;
|
} // serviceGenerator scripts;
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use skynet_discord_bot::{db_init, get_config, get_server_config_bulk, Config, ServerMembers, Servers, Wolves};
|
use skynet_discord_bot::{db_init, get_config, get_server_config_bulk, Config, ServerMembers, Servers, Wolves};
|
||||||
|
|
||||||
use serde::Deserialize;
|
use serde::{Deserialize, Serialize};
|
||||||
use serenity::model::id::GuildId;
|
use serenity::model::id::GuildId;
|
||||||
use sqlx::{Pool, Sqlite};
|
use sqlx::{Pool, Sqlite};
|
||||||
|
|
||||||
|
@ -13,149 +13,75 @@ async fn main() {
|
||||||
};
|
};
|
||||||
|
|
||||||
// handle wolves api here
|
// handle wolves api here
|
||||||
get_wolves_csv(&db, &config).await;
|
get_wolves(&db, &config).await;
|
||||||
// handle wolves api here
|
|
||||||
get_wolves(&db).await;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get_wolves_csv(db: &Pool<Sqlite>, config: &Config) {
|
#[derive(Deserialize, Serialize, Debug)]
|
||||||
if let Ok(accounts) = get_csv(config) {
|
struct WolvesResultUser {
|
||||||
for account in accounts {
|
committee: String,
|
||||||
add_users_wolves_csv(db, &config.skynet_server, &account).await;
|
wolves_id: String,
|
||||||
}
|
first_name: String,
|
||||||
}
|
last_name: String,
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, serde::Deserialize)]
|
|
||||||
struct RecordCSV {
|
|
||||||
#[serde(rename = "MemID")]
|
|
||||||
mem_id: String,
|
|
||||||
#[serde(rename = "Contact Email")]
|
|
||||||
email: String,
|
email: String,
|
||||||
#[serde(rename = "Expiry")]
|
student_id: Option<String>,
|
||||||
|
note: Option<String>,
|
||||||
expiry: String,
|
expiry: String,
|
||||||
|
requested: String,
|
||||||
|
approved: String,
|
||||||
|
sitename: String,
|
||||||
|
domain: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<&RecordCSV> for Wolves {
|
#[derive(Deserialize, Serialize, Debug)]
|
||||||
fn from(input: &RecordCSV) -> Self {
|
|
||||||
Self {
|
|
||||||
id_wolves: input.mem_id.to_owned(),
|
|
||||||
email: input.email.to_owned(),
|
|
||||||
discord: None,
|
|
||||||
minecraft: None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<&RecordCSV> for ServerMembers {
|
|
||||||
fn from(input: &RecordCSV) -> Self {
|
|
||||||
Self {
|
|
||||||
server: Default::default(),
|
|
||||||
id_wolves: input.mem_id.to_owned(),
|
|
||||||
expiry: input.expiry.to_owned(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct Csv {
|
|
||||||
wolves: Wolves,
|
|
||||||
server_members: ServerMembers,
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_csv(config: &Config) -> Result<Vec<Csv>, Box<dyn std::error::Error>> {
|
|
||||||
let mut result = vec![];
|
|
||||||
|
|
||||||
let csv = format!("{}/{}", &config.home, &config.csv);
|
|
||||||
if let Ok(mut rdr) = csv::Reader::from_path(csv) {
|
|
||||||
for r in rdr.deserialize() {
|
|
||||||
// Notice that we need to provide a type hint for automatic
|
|
||||||
// deserialization.
|
|
||||||
let record: RecordCSV = r?;
|
|
||||||
if record.mem_id.is_empty() {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut tmp = ServerMembers::from(&record);
|
|
||||||
tmp.server = config.skynet_server;
|
|
||||||
result.push(Csv {
|
|
||||||
wolves: Wolves::from(&record),
|
|
||||||
server_members: tmp,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(result)
|
|
||||||
}
|
|
||||||
async fn add_users_wolves_csv(db: &Pool<Sqlite>, server: &GuildId, user: &Csv) {
|
|
||||||
match sqlx::query_as::<_, Wolves>(
|
|
||||||
"
|
|
||||||
INSERT INTO wolves (id_wolves, email)
|
|
||||||
VALUES ($1, $2)
|
|
||||||
ON CONFLICT(id_wolves) DO UPDATE SET email = $2
|
|
||||||
",
|
|
||||||
)
|
|
||||||
.bind(&user.wolves.id_wolves)
|
|
||||||
.bind(&user.wolves.email)
|
|
||||||
.fetch_optional(db)
|
|
||||||
.await
|
|
||||||
{
|
|
||||||
Ok(_) => {}
|
|
||||||
Err(e) => {
|
|
||||||
println!("Failure to insert into {} {:?}", server.as_u64(), user.wolves);
|
|
||||||
println!("{:?}", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
match sqlx::query_as::<_, ServerMembers>(
|
|
||||||
"
|
|
||||||
INSERT OR REPLACE INTO server_members (server, id_wolves, expiry)
|
|
||||||
VALUES (?1, ?2, ?3)
|
|
||||||
",
|
|
||||||
)
|
|
||||||
.bind(*server.as_u64() as i64)
|
|
||||||
.bind(&user.server_members.id_wolves)
|
|
||||||
.bind(&user.server_members.expiry)
|
|
||||||
.fetch_optional(db)
|
|
||||||
.await
|
|
||||||
{
|
|
||||||
Ok(_) => {}
|
|
||||||
Err(e) => {
|
|
||||||
println!("Failure to insert into {} {:?}", server.as_u64(), user.server_members);
|
|
||||||
println!("{:?}", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
|
||||||
pub struct SkynetResult {
|
|
||||||
discord: String,
|
|
||||||
id_member: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
|
||||||
struct WolvesResult {
|
struct WolvesResult {
|
||||||
|
success: i8,
|
||||||
|
result: Vec<WolvesResultUser>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize, Serialize, Debug)]
|
||||||
|
struct WolvesResultLocal {
|
||||||
pub id_wolves: String,
|
pub id_wolves: String,
|
||||||
pub email: String,
|
pub email: String,
|
||||||
pub expiry: String,
|
pub expiry: String,
|
||||||
}
|
}
|
||||||
async fn get_wolves(db: &Pool<Sqlite>) {
|
async fn get_wolves(db: &Pool<Sqlite>, config: &Config) {
|
||||||
for server_config in get_server_config_bulk(db).await {
|
for server_config in get_server_config_bulk(db).await {
|
||||||
let Servers {
|
let Servers {
|
||||||
server,
|
server,
|
||||||
//wolves_api,
|
wolves_api,
|
||||||
..
|
..
|
||||||
} = server_config;
|
} = server_config;
|
||||||
|
|
||||||
// get the data here
|
for user in get_wolves_sub(config, &wolves_api).await {
|
||||||
let result: Vec<WolvesResult> = vec![];
|
|
||||||
|
|
||||||
for user in result {
|
|
||||||
add_users_wolves(db, &server, &user).await;
|
add_users_wolves(db, &server, &user).await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn add_users_wolves(db: &Pool<Sqlite>, server: &GuildId, user: &WolvesResult) {
|
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>, server: &GuildId, user: &WolvesResultUser) {
|
||||||
// expiry
|
// expiry
|
||||||
match sqlx::query_as::<_, Wolves>(
|
match sqlx::query_as::<_, Wolves>(
|
||||||
"
|
"
|
||||||
|
@ -164,7 +90,7 @@ async fn add_users_wolves(db: &Pool<Sqlite>, server: &GuildId, user: &WolvesResu
|
||||||
ON CONFLICT(id_wolves) DO UPDATE SET email = $2
|
ON CONFLICT(id_wolves) DO UPDATE SET email = $2
|
||||||
",
|
",
|
||||||
)
|
)
|
||||||
.bind(&user.id_wolves)
|
.bind(&user.wolves_id)
|
||||||
.bind(&user.email)
|
.bind(&user.email)
|
||||||
.fetch_optional(db)
|
.fetch_optional(db)
|
||||||
.await
|
.await
|
||||||
|
@ -183,7 +109,7 @@ async fn add_users_wolves(db: &Pool<Sqlite>, server: &GuildId, user: &WolvesResu
|
||||||
",
|
",
|
||||||
)
|
)
|
||||||
.bind(*server.as_u64() as i64)
|
.bind(*server.as_u64() as i64)
|
||||||
.bind(&user.id_wolves)
|
.bind(&user.wolves_id)
|
||||||
.bind(&user.expiry)
|
.bind(&user.expiry)
|
||||||
.fetch_optional(db)
|
.fetch_optional(db)
|
||||||
.await
|
.await
|
||||||
|
|
22
src/lib.rs
22
src/lib.rs
|
@ -23,7 +23,6 @@ pub struct Config {
|
||||||
pub ldap_api: String,
|
pub ldap_api: String,
|
||||||
pub home: String,
|
pub home: String,
|
||||||
pub database: String,
|
pub database: String,
|
||||||
pub csv: String,
|
|
||||||
|
|
||||||
pub auth: String,
|
pub auth: String,
|
||||||
pub discord_token: String,
|
pub discord_token: String,
|
||||||
|
@ -31,6 +30,8 @@ pub struct Config {
|
||||||
pub mail_smtp: String,
|
pub mail_smtp: String,
|
||||||
pub mail_user: String,
|
pub mail_user: String,
|
||||||
pub mail_pass: String,
|
pub mail_pass: String,
|
||||||
|
|
||||||
|
pub wolves_url: String,
|
||||||
}
|
}
|
||||||
impl TypeMapKey for Config {
|
impl TypeMapKey for Config {
|
||||||
type Value = Arc<RwLock<Config>>;
|
type Value = Arc<RwLock<Config>>;
|
||||||
|
@ -53,11 +54,11 @@ pub fn get_config() -> Config {
|
||||||
|
|
||||||
home: ".".to_string(),
|
home: ".".to_string(),
|
||||||
database: "database.db".to_string(),
|
database: "database.db".to_string(),
|
||||||
csv: "wolves.csv".to_string(),
|
|
||||||
|
|
||||||
mail_smtp: "".to_string(),
|
mail_smtp: "".to_string(),
|
||||||
mail_user: "".to_string(),
|
mail_user: "".to_string(),
|
||||||
mail_pass: "".to_string(),
|
mail_pass: "".to_string(),
|
||||||
|
wolves_url: "".to_string(),
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Ok(x) = env::var("LDAP_API") {
|
if let Ok(x) = env::var("LDAP_API") {
|
||||||
|
@ -72,9 +73,6 @@ pub fn get_config() -> Config {
|
||||||
if let Ok(x) = env::var("DATABASE") {
|
if let Ok(x) = env::var("DATABASE") {
|
||||||
config.database = x.trim().to_string();
|
config.database = x.trim().to_string();
|
||||||
}
|
}
|
||||||
if let Ok(x) = env::var("CSV") {
|
|
||||||
config.csv = x.trim().to_string();
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Ok(x) = env::var("LDAP_DISCORD_AUTH") {
|
if let Ok(x) = env::var("LDAP_DISCORD_AUTH") {
|
||||||
config.auth = x.trim().to_string();
|
config.auth = x.trim().to_string();
|
||||||
|
@ -94,6 +92,10 @@ pub fn get_config() -> Config {
|
||||||
config.mail_pass = x.trim().to_string();
|
config.mail_pass = x.trim().to_string();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if let Ok(x) = env::var("WOLVES_URL") {
|
||||||
|
config.wolves_url = x.trim().to_string();
|
||||||
|
}
|
||||||
|
|
||||||
config
|
config
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -104,7 +106,7 @@ fn str_to_num<T: FromStr + Default>(x: &str) -> T {
|
||||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||||
pub struct ServerMembers {
|
pub struct ServerMembers {
|
||||||
pub server: GuildId,
|
pub server: GuildId,
|
||||||
pub id_wolves: String,
|
pub id_wolves: i64,
|
||||||
pub expiry: String,
|
pub expiry: String,
|
||||||
}
|
}
|
||||||
impl<'r> FromRow<'r, SqliteRow> for ServerMembers {
|
impl<'r> FromRow<'r, SqliteRow> for ServerMembers {
|
||||||
|
@ -123,7 +125,7 @@ impl<'r> FromRow<'r, SqliteRow> for ServerMembers {
|
||||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||||
pub struct ServerMembersWolves {
|
pub struct ServerMembersWolves {
|
||||||
pub server: GuildId,
|
pub server: GuildId,
|
||||||
pub id_wolves: String,
|
pub id_wolves: i64,
|
||||||
pub expiry: String,
|
pub expiry: String,
|
||||||
pub email: String,
|
pub email: String,
|
||||||
pub discord: Option<UserId>,
|
pub discord: Option<UserId>,
|
||||||
|
@ -158,7 +160,7 @@ impl<'r> FromRow<'r, SqliteRow> for ServerMembersWolves {
|
||||||
|
|
||||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||||
pub struct Wolves {
|
pub struct Wolves {
|
||||||
pub id_wolves: String,
|
pub id_wolves: i64,
|
||||||
pub email: String,
|
pub email: String,
|
||||||
pub discord: Option<UserId>,
|
pub discord: Option<UserId>,
|
||||||
pub minecraft: Option<String>,
|
pub minecraft: Option<String>,
|
||||||
|
@ -268,7 +270,7 @@ pub async fn db_init(config: &Config) -> Result<Pool<Sqlite>, Error> {
|
||||||
|
|
||||||
sqlx::query(
|
sqlx::query(
|
||||||
"CREATE TABLE IF NOT EXISTS wolves (
|
"CREATE TABLE IF NOT EXISTS wolves (
|
||||||
id_wolves text PRIMARY KEY,
|
id_wolves integer PRIMARY KEY,
|
||||||
email text not null,
|
email text not null,
|
||||||
discord integer,
|
discord integer,
|
||||||
minecraft text
|
minecraft text
|
||||||
|
@ -297,7 +299,7 @@ pub async fn db_init(config: &Config) -> Result<Pool<Sqlite>, Error> {
|
||||||
sqlx::query(
|
sqlx::query(
|
||||||
"CREATE TABLE IF NOT EXISTS server_members (
|
"CREATE TABLE IF NOT EXISTS server_members (
|
||||||
server integer not null,
|
server integer not null,
|
||||||
id_wolves text not null,
|
id_wolves integer not null,
|
||||||
expiry text not null,
|
expiry text not null,
|
||||||
PRIMARY KEY(server,id_wolves),
|
PRIMARY KEY(server,id_wolves),
|
||||||
FOREIGN KEY (id_wolves) REFERENCES wolves (id_wolves)
|
FOREIGN KEY (id_wolves) REFERENCES wolves (id_wolves)
|
||||||
|
|
Loading…
Reference in a new issue