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