2019-10-26 01:14:49 +02:00
|
|
|
#![feature(proc_macro_hygiene, decl_macro)]
|
2019-10-26 02:19:33 +02:00
|
|
|
#![recursion_limit="1024"]
|
2019-10-26 01:14:49 +02:00
|
|
|
|
|
|
|
use std::sync::Mutex;
|
|
|
|
#[macro_use] extern crate rocket;
|
|
|
|
use rocket::{State, response::content};
|
|
|
|
use typed_html::{html, text, dom::DOMTree};
|
|
|
|
use diesel::{Connection, pg::PgConnection, prelude::*};
|
2019-10-26 02:19:33 +02:00
|
|
|
use chrono::{offset::Local, NaiveDate};
|
2019-10-26 01:14:49 +02:00
|
|
|
|
|
|
|
use libticker::{
|
|
|
|
config::{Config, CalendarOptions},
|
|
|
|
schema::{self, events::dsl::events},
|
|
|
|
model::{Calendar, Event},
|
|
|
|
ics::{Object, Timestamp, GetValue},
|
|
|
|
};
|
|
|
|
|
|
|
|
fn fix_url(s: &str) -> std::borrow::Cow<str> {
|
|
|
|
if s.starts_with("http:") || s.starts_with("https:") {
|
|
|
|
s.into()
|
|
|
|
} else {
|
|
|
|
format!("http://{}", s).into()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-26 02:19:33 +02:00
|
|
|
struct DayEvents<'e> {
|
|
|
|
date: NaiveDate,
|
|
|
|
events: &'e [Event],
|
|
|
|
}
|
|
|
|
|
|
|
|
/// assumes pre-sorted input
|
|
|
|
fn group_by_day(es: &[Event]) -> Vec<DayEvents> {
|
|
|
|
let mut results = vec![];
|
|
|
|
|
|
|
|
let mut prev_date = None;
|
|
|
|
let mut date_start = 0;
|
|
|
|
for (i, event) in es.iter().enumerate() {
|
|
|
|
if prev_date.is_some() && prev_date != Some(event.dtstart.date()) {
|
|
|
|
if i > date_start {
|
|
|
|
results.push(DayEvents {
|
|
|
|
date: prev_date.unwrap().clone(),
|
|
|
|
events: &es[date_start..i],
|
|
|
|
});
|
|
|
|
date_start = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
prev_date = Some(event.dtstart.date());
|
|
|
|
}
|
|
|
|
|
|
|
|
results
|
|
|
|
}
|
|
|
|
|
2019-10-26 01:14:49 +02:00
|
|
|
#[get("/")]
|
|
|
|
fn index(db: State<Mutex<PgConnection>>) -> content::Html<String> {
|
|
|
|
let db = db.lock().unwrap();
|
2019-10-26 01:57:31 +02:00
|
|
|
let today = Local::today().naive_local().and_hms(0, 0, 0);
|
2019-10-26 01:14:49 +02:00
|
|
|
let es = events
|
2019-10-26 01:57:31 +02:00
|
|
|
.filter(schema::events::dtstart.ge(&today))
|
2019-10-26 01:14:49 +02:00
|
|
|
.order_by(schema::events::dtstart.asc())
|
|
|
|
.then_order_by(schema::events::dtend.desc())
|
|
|
|
.load::<Event>(&*db)
|
|
|
|
.unwrap();
|
2019-10-26 02:19:33 +02:00
|
|
|
let days = group_by_day(&es);
|
|
|
|
|
2019-10-26 01:14:49 +02:00
|
|
|
let doc: DOMTree<String> = html!(
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<title>"Ticker"</title>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<h1>"Ticker"</h1>
|
|
|
|
|
2019-10-26 02:19:33 +02:00
|
|
|
{ days.iter().map(|day| html!(<div>
|
|
|
|
<nav><h2>{ text!("{}", &day.date) }</h2></nav>
|
|
|
|
|
|
|
|
{ day.events.iter().map(|e| html!(
|
|
|
|
<article class="event">
|
|
|
|
{ match &e.url {
|
|
|
|
None => html!(
|
|
|
|
<h3>{ text!("{}", &e.summary) }</h3>
|
|
|
|
),
|
|
|
|
Some(url) => html!(
|
|
|
|
<h3>
|
|
|
|
<a href={ fix_url(url) }>
|
|
|
|
{ text!("{}", &e.summary) }
|
|
|
|
</a>
|
|
|
|
</h3>
|
|
|
|
),
|
|
|
|
} }
|
2019-10-26 01:14:49 +02:00
|
|
|
|
2019-10-26 02:19:33 +02:00
|
|
|
<p class="dtstart">{ text!("{}", &e.dtstart) }</p>
|
|
|
|
{ e.location.as_ref().map(|location| html!(
|
|
|
|
<p>
|
|
|
|
{ text!("{}", location) }
|
|
|
|
</p>
|
|
|
|
)) }
|
|
|
|
</article>
|
|
|
|
)) }
|
|
|
|
</div>)) }
|
2019-10-26 01:14:49 +02:00
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
);
|
|
|
|
content::Html(doc.to_string())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let config = Config::read_yaml_file("config.yaml");
|
|
|
|
let db = PgConnection::establish(&config.db_url)
|
|
|
|
.expect("DB");
|
|
|
|
|
|
|
|
rocket::ignite()
|
|
|
|
.manage(Mutex::new(db))
|
|
|
|
.mount("/", routes![
|
|
|
|
index,
|
|
|
|
])
|
|
|
|
.launch();
|
|
|
|
}
|