2023-07-31 09:10:26 +02:00
|
|
|
use chrono::{DateTime, Local, NaiveDateTime, Utc};
|
2023-04-18 12:10:11 +02:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use sqlx::{FromRow, SqlitePool};
|
|
|
|
|
|
|
|
#[derive(FromRow, Debug, Serialize, Deserialize)]
|
|
|
|
pub struct Log {
|
|
|
|
pub msg: String,
|
2023-07-31 09:10:26 +02:00
|
|
|
pub created_at: NaiveDateTime,
|
2023-04-18 12:10:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Log {
|
|
|
|
pub async fn create(db: &SqlitePool, msg: String) -> bool {
|
|
|
|
sqlx::query!("INSERT INTO log(msg) VALUES (?)", msg,)
|
|
|
|
.execute(db)
|
|
|
|
.await
|
|
|
|
.is_ok()
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn last(db: &SqlitePool) -> Vec<Log> {
|
|
|
|
sqlx::query_as!(
|
|
|
|
Log,
|
|
|
|
"
|
|
|
|
SELECT msg, created_at
|
|
|
|
FROM log
|
|
|
|
ORDER BY id DESC
|
|
|
|
LIMIT 1000
|
|
|
|
"
|
|
|
|
)
|
|
|
|
.fetch_all(db)
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn generate_feed(db: &SqlitePool) -> String {
|
2023-05-24 09:31:18 +02:00
|
|
|
let mut ret = String::from(
|
2023-05-24 09:36:48 +02:00
|
|
|
r#"<?xml version="1.0" encoding="utf-8"?>
|
2023-05-24 09:39:49 +02:00
|
|
|
<rss version="2.0">
|
|
|
|
<channel>
|
|
|
|
<title>Ruder App Admin Feed</title>
|
|
|
|
<link>app.rudernlinz.at</link>
|
|
|
|
<description>An RSS feed with activities from app.rudernlinz.at</description>"#,
|
2023-05-24 09:31:18 +02:00
|
|
|
);
|
2023-04-18 12:10:11 +02:00
|
|
|
for log in Self::last(db).await {
|
2023-07-31 09:10:26 +02:00
|
|
|
let utc_time: DateTime<Utc> = DateTime::from_utc(log.created_at, Utc);
|
|
|
|
let local_time = utc_time.with_timezone(&Local);
|
2023-05-24 09:21:22 +02:00
|
|
|
ret.push_str("<item><title>");
|
2023-07-31 09:10:26 +02:00
|
|
|
ret.push_str(&format!("({}) {}", local_time, log.msg));
|
2023-05-24 09:21:22 +02:00
|
|
|
ret.push_str("</title></item>");
|
2023-04-18 12:10:11 +02:00
|
|
|
}
|
2023-05-24 09:11:34 +02:00
|
|
|
ret.push_str("</channel>");
|
|
|
|
ret.push_str("</rss>");
|
2023-05-30 14:36:23 +02:00
|
|
|
ret.replace('\n', "")
|
2023-04-18 12:10:11 +02:00
|
|
|
}
|
|
|
|
}
|