add rss feed with common actions

This commit is contained in:
2023-04-18 12:10:11 +02:00
parent b50d546a5c
commit a3ac83228c
8 changed files with 138 additions and 4 deletions

48
src/model/log.rs Normal file
View File

@ -0,0 +1,48 @@
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()
}
}

View File

@ -7,6 +7,7 @@ use self::{
trip::{Trip, TripWithUser},
};
pub mod log;
pub mod planned_event;
pub mod trip;
pub mod tripdetails;