From 4bf2aa6f01c4b7eb0c1d57e78dee373b63b4272d Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Wed, 4 Nov 2020 15:21:12 -0500 Subject: [PATCH] various cleanups --- backend/src/api/posse.rs | 15 +++++++++++++++ backend/src/api/switch.rs | 18 ++++++++++++++++++ backend/src/main.rs | 2 ++ 3 files changed, 35 insertions(+) diff --git a/backend/src/api/posse.rs b/backend/src/api/posse.rs index b0f967a..123645a 100644 --- a/backend/src/api/posse.rs +++ b/backend/src/api/posse.rs @@ -6,6 +6,7 @@ use crate::{ }; use diesel::prelude::*; use rocket::State; +use rocket_contrib::json::Json; use serde::Deserialize; use std::fmt::Write; @@ -78,6 +79,20 @@ fn posse(item: Item, dw: &DiscordWebhook, tw: &Twitter, ma: &Mastodon) -> WebRes pub static BLOG_FEED_URL: &'static str = "https://christine.website/blog.json"; +#[post("/posse", format = "json", data = "")] +#[instrument(skip(dw, tw, ma), err)] +pub fn notify( + item: Json, + tok: paseto::Token, + dw: State, + tw: State, + ma: State, +) -> Result { + posse(item.into_inner(), &dw, &tw, &ma)?; + + Ok(()) +} + #[post("/blog/refresh")] #[instrument(skip(conn, dw, tw, ma), err)] pub fn refresh_blog( diff --git a/backend/src/api/switch.rs b/backend/src/api/switch.rs index 7da24b9..68f990a 100644 --- a/backend/src/api/switch.rs +++ b/backend/src/api/switch.rs @@ -81,6 +81,24 @@ pub fn current_front(conn: MainDatabase, tok: paseto::Token) -> Result Result { + use schema::{members, switches}; + + let mut front: Vec<(models::Switch, models::Member)> = switches::table + .inner_join(members::table) + .order_by(switches::dsl::started_at.desc()) + .limit(1) + .load(&*conn) + .map_err(Error::Database)?; + + match front.pop() { + Some((_, member)) => Ok(member.cmene), + None => Err(Error::NotFound), + } +} + #[post("/switches/switch", data = "")] #[instrument(skip(conn, sc, pk), err)] pub fn switch( diff --git a/backend/src/main.rs b/backend/src/main.rs index e848942..dbe6d3a 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -53,8 +53,10 @@ fn main() -> Result<()> { .mount( "/api", routes![ + api::posse::notify, api::posse::refresh_blog, api::switch::current_front, + api::switch::current_front_text, api::switch::get, api::switch::list, api::switch::switch,