rowt/src/model/log.rs

49 lines
1.2 KiB
Rust
Raw Normal View History

2023-04-18 12:10:11 +02:00
use rss::{ChannelBuilder, Item};
use serde::{Deserialize, Serialize};
use sqlx::{FromRow, SqlitePool};
#[derive(FromRow, Debug, Serialize, Deserialize)]
pub struct Log {
pub msg: String,
pub created_at: String,
}
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 {
let mut channel = ChannelBuilder::default()
.title("Ruder App Admin Feed")
.description("An RSS feed with activities from app.rudernlinz.at")
.build();
let mut items: Vec<Item> = vec![];
for log in Self::last(db).await {
let mut item = Item::default();
item.set_title(format!("({}) {}", log.created_at, log.msg));
items.append(&mut vec![item]);
}
channel.set_items(items);
channel.to_string()
}
}