Merge pull request 'prepare to remove old log, in favor of activities' (#1013) from acitvities-adaption into main
Reviewed-on: #1013
This commit was merged in pull request #1013.
	This commit is contained in:
		@@ -1,7 +1,7 @@
 | 
			
		||||
use std::ops::DerefMut;
 | 
			
		||||
 | 
			
		||||
use super::{role::Role, user::User};
 | 
			
		||||
use chrono::{DateTime, Local, NaiveDateTime, TimeZone, Utc};
 | 
			
		||||
use chrono::{DateTime, Duration, Local, NaiveDateTime, TimeZone, Utc};
 | 
			
		||||
use serde::{Deserialize, Serialize};
 | 
			
		||||
use sqlx::{FromRow, Sqlite, SqlitePool, Transaction};
 | 
			
		||||
 | 
			
		||||
@@ -46,6 +46,15 @@ impl ActivityBuilder {
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    #[must_use]
 | 
			
		||||
    pub fn keep_until_days(self, days: i64) -> Self {
 | 
			
		||||
        let now = Utc::now().naive_utc();
 | 
			
		||||
        Self {
 | 
			
		||||
            keep_until: Some(now + Duration::days(days)),
 | 
			
		||||
            ..self
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    pub async fn save(self, db: &SqlitePool) {
 | 
			
		||||
        Activity::create(db, &self.text, &self.relevant_for, self.keep_until).await;
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
@@ -1,74 +1,16 @@
 | 
			
		||||
use std::ops::DerefMut;
 | 
			
		||||
use super::activity::ActivityBuilder;
 | 
			
		||||
use sqlx::{Sqlite, SqlitePool, Transaction};
 | 
			
		||||
 | 
			
		||||
use chrono::{DateTime, Local, NaiveDateTime, TimeZone, Utc};
 | 
			
		||||
use serde::{Deserialize, Serialize};
 | 
			
		||||
use sqlx::{FromRow, Sqlite, SqlitePool, Transaction};
 | 
			
		||||
 | 
			
		||||
#[derive(FromRow, Debug, Serialize, Deserialize)]
 | 
			
		||||
pub struct Log {
 | 
			
		||||
    pub msg: String,
 | 
			
		||||
    pub created_at: NaiveDateTime,
 | 
			
		||||
}
 | 
			
		||||
pub struct Log {}
 | 
			
		||||
 | 
			
		||||
// TODO: remove and convert to proper acitvities
 | 
			
		||||
impl Log {
 | 
			
		||||
    pub async fn create(db: &SqlitePool, msg: String) -> bool {
 | 
			
		||||
        sqlx::query!("INSERT INTO log(msg) VALUES (?)", msg,)
 | 
			
		||||
            .execute(db)
 | 
			
		||||
            .await
 | 
			
		||||
            .is_ok()
 | 
			
		||||
        ActivityBuilder::new(&msg).save(db).await;
 | 
			
		||||
        true
 | 
			
		||||
    }
 | 
			
		||||
    pub async fn create_with_tx(db: &mut Transaction<'_, Sqlite>, msg: String) -> bool {
 | 
			
		||||
        sqlx::query!("INSERT INTO log(msg) VALUES (?)", msg,)
 | 
			
		||||
            .execute(db.deref_mut())
 | 
			
		||||
            .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 ret = String::from(
 | 
			
		||||
            r#"<?xml version="1.0" encoding="utf-8"?>
 | 
			
		||||
<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>"#,
 | 
			
		||||
        );
 | 
			
		||||
        for log in Self::last(db).await {
 | 
			
		||||
            let utc_time: DateTime<Utc> = Utc::from_utc_datetime(&Utc, &log.created_at);
 | 
			
		||||
            let local_time = utc_time.with_timezone(&Local);
 | 
			
		||||
            ret.push_str("<item><title>");
 | 
			
		||||
            ret.push_str(&format!("({}) {}", local_time, log.msg));
 | 
			
		||||
            ret.push_str("</title></item>");
 | 
			
		||||
        }
 | 
			
		||||
        ret.push_str("</channel>");
 | 
			
		||||
        ret.push_str("</rss>");
 | 
			
		||||
        ret.replace('\n', "")
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    pub async fn show(db: &SqlitePool) -> String {
 | 
			
		||||
        let mut ret = String::new();
 | 
			
		||||
 | 
			
		||||
        for log in Self::last(db).await {
 | 
			
		||||
            let utc_time: DateTime<Utc> = Utc::from_utc_datetime(&Utc, &log.created_at);
 | 
			
		||||
            let local_time = utc_time.with_timezone(&Local);
 | 
			
		||||
            ret.push_str(&format!("- {} - {}\n", local_time, log.msg));
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        ret
 | 
			
		||||
        ActivityBuilder::new(&msg).save_tx(db).await;
 | 
			
		||||
        true
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -3,10 +3,7 @@ use rocket::{form::Form, get, post, routes, FromForm, Route, State};
 | 
			
		||||
use rocket_dyn_templates::{context, Template};
 | 
			
		||||
use sqlx::SqlitePool;
 | 
			
		||||
 | 
			
		||||
use crate::{
 | 
			
		||||
    model::{activity::Activity, log::Log, role::Role, user::AdminUser},
 | 
			
		||||
    tera::Config,
 | 
			
		||||
};
 | 
			
		||||
use crate::model::{activity::Activity, role::Role, user::AdminUser};
 | 
			
		||||
 | 
			
		||||
pub mod boat;
 | 
			
		||||
pub mod event;
 | 
			
		||||
@@ -16,20 +13,6 @@ pub mod role;
 | 
			
		||||
pub mod schnupper;
 | 
			
		||||
pub mod user;
 | 
			
		||||
 | 
			
		||||
#[get("/rss?<key>")]
 | 
			
		||||
async fn rss(db: &State<SqlitePool>, key: &str, config: &State<Config>) -> String {
 | 
			
		||||
    if key.eq(&config.rss_key) {
 | 
			
		||||
        Log::generate_feed(db).await
 | 
			
		||||
    } else {
 | 
			
		||||
        "Not allowed".into()
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[get("/rss/old", rank = 2)]
 | 
			
		||||
async fn show_rss(db: &State<SqlitePool>, _admin: AdminUser) -> String {
 | 
			
		||||
    Log::show(db).await
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[get("/rss", rank = 2)]
 | 
			
		||||
async fn show_activities(db: &State<SqlitePool>, _admin: AdminUser) -> String {
 | 
			
		||||
    Activity::show(db).await
 | 
			
		||||
@@ -88,12 +71,6 @@ pub fn routes() -> Vec<Route> {
 | 
			
		||||
    ret.append(&mut mail::routes());
 | 
			
		||||
    ret.append(&mut event::routes());
 | 
			
		||||
    ret.append(&mut role::routes());
 | 
			
		||||
    ret.append(&mut routes![
 | 
			
		||||
        rss,
 | 
			
		||||
        show_rss,
 | 
			
		||||
        show_activities,
 | 
			
		||||
        show_list,
 | 
			
		||||
        list
 | 
			
		||||
    ]);
 | 
			
		||||
    ret.append(&mut routes![show_activities, show_list, list]);
 | 
			
		||||
    ret
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user