parent
6b6c047b68
commit
a8c0969a71
@ -1,35 +1,34 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use poise::serenity_prelude::Http;
|
||||
use rocket::{http::{CookieJar, Status}, State};
|
||||
use rocket::serde::json::Json;
|
||||
use rocket::{http::CookieJar, State};
|
||||
use rocket::response::stream::{Event, EventStream};
|
||||
|
||||
use crate::{cookies::user::USER_ID_COOKIE, models::Settings};
|
||||
|
||||
// TODO: Incrementally send guilds
|
||||
#[get("/get_guilds")]
|
||||
pub async fn get_guilds(
|
||||
cookies: &CookieJar<'_>,
|
||||
settings: &State<Settings>,
|
||||
http: &State<Http>,
|
||||
) -> Result<Json<HashMap<u64, bool>>, Status> {
|
||||
pub async fn get_guilds<'a>(
|
||||
cookies: &'a CookieJar<'_>,
|
||||
settings: &'a State<Settings>,
|
||||
http: &'a State<Http>,
|
||||
) -> EventStream![Event + 'a] {
|
||||
// EventStream![] is shorthand for EventStream[Event],
|
||||
// but we need to pass in a lifetime parameter.
|
||||
EventStream! {
|
||||
// TODO: Proper error handling
|
||||
let user_id: u64 = match cookies.get_private(USER_ID_COOKIE) {
|
||||
Some(id) => match id.value().parse() {
|
||||
Ok(id) => id,
|
||||
Err(_) => return Err(Status::BadRequest),
|
||||
Err(_) => panic!("BadRequest"),
|
||||
// Err(_) => return Err(Status::BadRequest),
|
||||
},
|
||||
None => return Err(Status::Unauthorized),
|
||||
None => panic!("Unauthorized"),
|
||||
// None => return Err(Status::Unauthorized),
|
||||
};
|
||||
Ok(Json({
|
||||
let mut guild_statuses = HashMap::with_capacity(settings.guilds.len());
|
||||
for guild in &settings.guilds {
|
||||
guild_statuses.insert(guild.id, {
|
||||
if guild.hidden {
|
||||
continue;
|
||||
}
|
||||
http.get_member(guild.id, user_id).await.is_ok()
|
||||
});
|
||||
yield Event::data(format!("{},{}", guild.id, http.get_member(guild.id, user_id).await.is_ok()));
|
||||
}
|
||||
}
|
||||
guild_statuses
|
||||
}))
|
||||
}
|
||||
|
Loading…
Reference in new issue