Merge branch 'add-rss-feed' into 'main'

Add rss feed

See merge request PhilippHofer/rot!1
This commit is contained in:
PhilippHofer 2023-04-18 10:21:57 +00:00
commit 9896ba55b2
11 changed files with 269 additions and 5 deletions

129
Cargo.lock generated
View File

@ -119,6 +119,19 @@ dependencies = [
"num-traits", "num-traits",
] ]
[[package]]
name = "atom_syndication"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca96cb38e3d8236f1573a84bbc55e130bd1ae07df770e36d0cf221ea7a50e36c"
dependencies = [
"chrono",
"derive_builder",
"diligent-date-parser",
"never",
"quick-xml",
]
[[package]] [[package]]
name = "atomic" name = "atomic"
version = "0.5.1" version = "0.5.1"
@ -421,6 +434,72 @@ dependencies = [
"syn 2.0.15", "syn 2.0.15",
] ]
[[package]]
name = "darling"
version = "0.14.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7b750cb3417fd1b327431a470f388520309479ab0bf5e323505daf0290cd3850"
dependencies = [
"darling_core",
"darling_macro",
]
[[package]]
name = "darling_core"
version = "0.14.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "109c1ca6e6b7f82cc233a97004ea8ed7ca123a9af07a8230878fcfda9b158bf0"
dependencies = [
"fnv",
"ident_case",
"proc-macro2",
"quote",
"strsim",
"syn 1.0.109",
]
[[package]]
name = "darling_macro"
version = "0.14.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a4aab4dbc9f7611d8b55048a3a16d2d010c2c8334e46304b40ac1cc14bf3b48e"
dependencies = [
"darling_core",
"quote",
"syn 1.0.109",
]
[[package]]
name = "derive_builder"
version = "0.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8d67778784b508018359cbc8696edb3db78160bab2c2a28ba7f56ef6932997f8"
dependencies = [
"derive_builder_macro",
]
[[package]]
name = "derive_builder_core"
version = "0.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c11bdc11a0c47bc7d37d582b5285da6849c96681023680b906673c5707af7b0f"
dependencies = [
"darling",
"proc-macro2",
"quote",
"syn 1.0.109",
]
[[package]]
name = "derive_builder_macro"
version = "0.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ebcda35c7a396850a55ffeac740804b40ffec779b98fffbb1738f4033f0ee79e"
dependencies = [
"derive_builder_core",
"syn 1.0.109",
]
[[package]] [[package]]
name = "deunicode" name = "deunicode"
version = "0.4.3" version = "0.4.3"
@ -471,6 +550,15 @@ dependencies = [
"subtle", "subtle",
] ]
[[package]]
name = "diligent-date-parser"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f6cf7fe294274a222363f84bcb63cdea762979a0443b4cf1f4f8fd17c86b1182"
dependencies = [
"chrono",
]
[[package]] [[package]]
name = "dotenvy" name = "dotenvy"
version = "0.15.7" version = "0.15.7"
@ -946,6 +1034,12 @@ dependencies = [
"cxx-build", "cxx-build",
] ]
[[package]]
name = "ident_case"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39"
[[package]] [[package]]
name = "idna" name = "idna"
version = "0.3.0" version = "0.3.0"
@ -1232,6 +1326,12 @@ dependencies = [
"version_check", "version_check",
] ]
[[package]]
name = "never"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c96aba5aa877601bb3f6dd6a63a969e1f82e60646e81e71b14496995e9853c91"
[[package]] [[package]]
name = "nom" name = "nom"
version = "7.1.3" version = "7.1.3"
@ -1596,6 +1696,16 @@ version = "0.5.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b45c49fc4f91f35bae654f85ebb3a44d60ac64f11b3166ffa609def390c732d8" checksum = "b45c49fc4f91f35bae654f85ebb3a44d60ac64f11b3166ffa609def390c732d8"
[[package]]
name = "quick-xml"
version = "0.28.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0ce5e73202a820a31f8a0ee32ada5e21029c81fd9e3ebf668a40832e4219d9d1"
dependencies = [
"encoding_rs",
"memchr",
]
[[package]] [[package]]
name = "quote" name = "quote"
version = "1.0.26" version = "1.0.26"
@ -1818,12 +1928,25 @@ dependencies = [
"log", "log",
"rocket", "rocket",
"rocket_dyn_templates", "rocket_dyn_templates",
"rss",
"serde", "serde",
"serde_json", "serde_json",
"sqlx", "sqlx",
"tera", "tera",
] ]
[[package]]
name = "rss"
version = "2.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aa1ec965a5f5ec71e16106b35df21861c4014ace848c6b75720816925552936d"
dependencies = [
"atom_syndication",
"derive_builder",
"never",
"quick-xml",
]
[[package]] [[package]]
name = "rustix" name = "rustix"
version = "0.37.11" version = "0.37.11"
@ -2148,6 +2271,12 @@ dependencies = [
"unicode-normalization", "unicode-normalization",
] ]
[[package]]
name = "strsim"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
[[package]] [[package]]
name = "subtle" name = "subtle"
version = "2.4.1" version = "2.4.1"

View File

@ -16,4 +16,4 @@ serde = { version = "1.0", features = [ "derive" ]}
serde_json = "1.0" serde_json = "1.0"
chrono = { version = "0.4", features = ["serde"]} chrono = { version = "0.4", features = ["serde"]}
tera = { version = "1.18", features = ["date-locale"]} tera = { version = "1.18", features = ["date-locale"]}
rss = "2.0"

View File

@ -5,6 +5,7 @@
- [] delete flag user administration - [] delete flag user administration
- [] max_people = 0 -> Rot hervorheben, dass Ausfahrt abgesagt wurde? - [] max_people = 0 -> Rot hervorheben, dass Ausfahrt abgesagt wurde?
- [] my trips for cox - [] my trips for cox
- [] remove key from src/rest/admin/rss.rs (line 8); at least before putting code somewhere public
# Nice to have # Nice to have
- [] automatically add regular planned trip - [] automatically add regular planned trip

View File

@ -45,3 +45,9 @@ CREATE TABLE IF NOT EXISTS "user_trip" (
FOREIGN KEY(trip_details_id) REFERENCES trip_details(id), FOREIGN KEY(trip_details_id) REFERENCES trip_details(id),
CONSTRAINT unq UNIQUE (user_id, trip_details_id) -- allow user to participate only once for each trip CONSTRAINT unq UNIQUE (user_id, trip_details_id) -- allow user to participate only once for each trip
); );
CREATE TABLE IF NOT EXISTS "log" (
"id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
"msg" text NOT NULL,
"created_at" text NOT NULL DEFAULT CURRENT_TIMESTAMP
);

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}, trip::{Trip, TripWithUser},
}; };
pub mod log;
pub mod planned_event; pub mod planned_event;
pub mod trip; pub mod trip;
pub mod tripdetails; pub mod tripdetails;

View File

@ -1,11 +1,13 @@
use rocket::Route; use rocket::Route;
pub mod planned_event; pub mod planned_event;
pub mod rss;
pub mod user; pub mod user;
pub fn routes() -> Vec<Route> { pub fn routes() -> Vec<Route> {
let mut ret = Vec::new(); let mut ret = Vec::new();
ret.append(&mut user::routes()); ret.append(&mut user::routes());
ret.append(&mut planned_event::routes()); ret.append(&mut planned_event::routes());
ret.append(&mut rss::routes());
ret ret
} }

17
src/rest/admin/rss.rs Normal file
View File

@ -0,0 +1,17 @@
use crate::rest::Log;
use rocket::{get, routes, Route, State};
use sqlx::SqlitePool;
#[get("/rss?<key>")]
async fn index(db: &State<SqlitePool>, key: Option<&str>) -> String {
match key {
Some(key) if key.eq("G9h/f2MFEr408IaB4Yd67/maVSsnAJNjcaZ2Tzl5Vo=") => {
Log::generate_feed(db).await
}
_ => "Not allowed".to_string(),
}
}
pub fn routes() -> Vec<Route> {
routes![index]
}

View File

@ -11,7 +11,10 @@ use rocket_dyn_templates::{context, tera, Template};
use serde_json::json; use serde_json::json;
use sqlx::SqlitePool; use sqlx::SqlitePool;
use crate::model::user::{LoginError, User}; use crate::model::{
log::Log,
user::{LoginError, User},
};
#[get("/")] #[get("/")]
fn index(flash: Option<FlashMessage<'_>>) -> Template { fn index(flash: Option<FlashMessage<'_>>) -> Template {
@ -96,6 +99,8 @@ async fn updatepw(
let user_json: String = format!("{}", json!(user)); let user_json: String = format!("{}", json!(user));
cookies.add_private(Cookie::new("loggedin_user", user_json)); cookies.add_private(Cookie::new("loggedin_user", user_json));
Log::create(db, format!("User {} set her password.", user.name)).await;
Flash::success( Flash::success(
Redirect::to("/"), Redirect::to("/"),
"Passwort erfolgreich gesetzt. Du bist nun eingeloggt.", "Passwort erfolgreich gesetzt. Du bist nun eingeloggt.",

View File

@ -7,6 +7,7 @@ use rocket::{
use sqlx::SqlitePool; use sqlx::SqlitePool;
use crate::model::{ use crate::model::{
log::Log,
trip::{CoxHelpError, Trip, TripDeleteError, TripUpdateError}, trip::{CoxHelpError, Trip, TripDeleteError, TripUpdateError},
tripdetails::TripDetails, tripdetails::TripDetails,
user::CoxUser, user::CoxUser,
@ -36,6 +37,18 @@ async fn create(db: &State<SqlitePool>, data: Form<AddTripForm>, cox: CoxUser) -
//TODO: fix clone() //TODO: fix clone()
Trip::new_own(db, cox.id, trip_details_id).await; Trip::new_own(db, cox.id, trip_details_id).await;
Log::create(
db,
format!(
"Cox {} created trip on {} @ {} for {} rower",
cox.name,
data.day.clone(),
data.planned_starting_time.clone(),
data.max_people,
),
)
.await;
Flash::success(Redirect::to("/"), "Ausfahrt erfolgreich erstellt.") Flash::success(Redirect::to("/"), "Ausfahrt erfolgreich erstellt.")
} }
@ -66,7 +79,17 @@ async fn update(
#[get("/join/<planned_event_id>")] #[get("/join/<planned_event_id>")]
async fn join(db: &State<SqlitePool>, planned_event_id: i64, cox: CoxUser) -> Flash<Redirect> { async fn join(db: &State<SqlitePool>, planned_event_id: i64, cox: CoxUser) -> Flash<Redirect> {
match Trip::new_join(db, cox.id, planned_event_id).await { match Trip::new_join(db, cox.id, planned_event_id).await {
Ok(_) => Flash::success(Redirect::to("/"), "Danke für's helfen!"), Ok(_) => {
Log::create(
db,
format!(
"Cox {} helps at planned_event.id={}",
cox.name, planned_event_id,
),
)
.await;
Flash::success(Redirect::to("/"), "Danke für's helfen!")
}
Err(CoxHelpError::AlreadyRegisteredAsCox) => { Err(CoxHelpError::AlreadyRegisteredAsCox) => {
Flash::error(Redirect::to("/"), "Du hilfst bereits aus!") Flash::error(Redirect::to("/"), "Du hilfst bereits aus!")
} }
@ -80,7 +103,10 @@ async fn join(db: &State<SqlitePool>, planned_event_id: i64, cox: CoxUser) -> Fl
#[get("/remove/trip/<trip_id>")] #[get("/remove/trip/<trip_id>")]
async fn remove_trip(db: &State<SqlitePool>, trip_id: i64, cox: CoxUser) -> Flash<Redirect> { async fn remove_trip(db: &State<SqlitePool>, trip_id: i64, cox: CoxUser) -> Flash<Redirect> {
match Trip::delete(db, cox.id, trip_id).await { match Trip::delete(db, cox.id, trip_id).await {
Ok(_) => Flash::success(Redirect::to("/"), "Erfolgreich abgemeldet!"), Ok(_) => {
Log::create(db, format!("Cox {} deleted trip.id={}", cox.name, trip_id)).await;
Flash::success(Redirect::to("/"), "Erfolgreich gelöscht!")
}
Err(TripDeleteError::SomebodyAlreadyRegistered) => Flash::error( Err(TripDeleteError::SomebodyAlreadyRegistered) => Flash::error(
Redirect::to("/"), Redirect::to("/"),
"Ausfahrt kann nicht gelöscht werden, da bereits jemand registriert ist!", "Ausfahrt kann nicht gelöscht werden, da bereits jemand registriert ist!",
@ -95,6 +121,15 @@ async fn remove_trip(db: &State<SqlitePool>, trip_id: i64, cox: CoxUser) -> Flas
async fn remove(db: &State<SqlitePool>, planned_event_id: i64, cox: CoxUser) -> Flash<Redirect> { async fn remove(db: &State<SqlitePool>, planned_event_id: i64, cox: CoxUser) -> Flash<Redirect> {
Trip::delete_by_planned_event_id(db, cox.id, planned_event_id).await; Trip::delete_by_planned_event_id(db, cox.id, planned_event_id).await;
Log::create(
db,
format!(
"Cox {} deleted registration for planned_event.id={}",
cox.name, planned_event_id
),
)
.await;
Flash::success(Redirect::to("/"), "Erfolgreich abgemeldet!") Flash::success(Redirect::to("/"), "Erfolgreich abgemeldet!")
} }

View File

@ -11,6 +11,7 @@ use rocket_dyn_templates::{tera::Context, Template};
use sqlx::SqlitePool; use sqlx::SqlitePool;
use crate::model::{ use crate::model::{
log::Log,
user::User, user::User,
usertrip::{UserTrip, UserTripError}, usertrip::{UserTrip, UserTripError},
Day, Day,
@ -51,7 +52,17 @@ async fn index(db: &State<SqlitePool>, user: User, flash: Option<FlashMessage<'_
#[get("/join/<trip_details_id>")] #[get("/join/<trip_details_id>")]
async fn join(db: &State<SqlitePool>, trip_details_id: i64, user: User) -> Flash<Redirect> { async fn join(db: &State<SqlitePool>, trip_details_id: i64, user: User) -> Flash<Redirect> {
match UserTrip::create(db, user.id, trip_details_id).await { match UserTrip::create(db, user.id, trip_details_id).await {
Ok(_) => Flash::success(Redirect::to("/"), "Erfolgreich angemeldet!"), Ok(_) => {
Log::create(
db,
format!(
"User {} registered for trip_details.id={}",
user.name, trip_details_id
),
)
.await;
Flash::success(Redirect::to("/"), "Erfolgreich angemeldet!")
}
Err(UserTripError::EventAlreadyFull) => { Err(UserTripError::EventAlreadyFull) => {
Flash::error(Redirect::to("/"), "Event bereits ausgebucht!") Flash::error(Redirect::to("/"), "Event bereits ausgebucht!")
} }
@ -68,6 +79,15 @@ async fn join(db: &State<SqlitePool>, trip_details_id: i64, user: User) -> Flash
async fn remove(db: &State<SqlitePool>, trip_details_id: i64, user: User) -> Flash<Redirect> { async fn remove(db: &State<SqlitePool>, trip_details_id: i64, user: User) -> Flash<Redirect> {
UserTrip::delete(db, user.id, trip_details_id).await; UserTrip::delete(db, user.id, trip_details_id).await;
Log::create(
db,
format!(
"User {} unregistered for trip_details.id={}",
user.name, trip_details_id
),
)
.await;
Flash::success(Redirect::to("/"), "Erfolgreich abgemeldet!") Flash::success(Redirect::to("/"), "Erfolgreich abgemeldet!")
} }