From 25161fc8e953c842549c7401ff1784408d9765f7 Mon Sep 17 00:00:00 2001 From: philipp Date: Sun, 18 Aug 2024 21:30:14 +0200 Subject: [PATCH 1/7] first draft of sending blog post notifications --- Rocket.toml | 1 + src/tera/mod.rs | 31 +++++++++++++++++++++++++++++++ templates/index.html.tera | 14 ++++++++++++++ 3 files changed, 46 insertions(+) diff --git a/Rocket.toml b/Rocket.toml index b9d5057..9f97381 100644 --- a/Rocket.toml +++ b/Rocket.toml @@ -5,3 +5,4 @@ limits = { file = "10 MiB", data-form = "10 MiB"} smtp_pw = "8kIjlLH79Ky6D3jQ" usage_log_path = "./usage.txt" openweathermap_key = "c8dab8f91b5b815d76e9879cbaecd8d5" +wordpress_key = "KYplHPxd6Gtqjdx4Ruik" diff --git a/src/tera/mod.rs b/src/tera/mod.rs index 27e88dc..982a1b7 100644 --- a/src/tera/mod.rs +++ b/src/tera/mod.rs @@ -118,6 +118,35 @@ fn unauthorized_error(req: &Request) -> Redirect { Redirect::to("/auth") } +#[derive(FromForm, Debug)] +struct NewBlogpostForm<'r> { + article_url: &'r str, + pw: &'r str, +} + +#[post("/", data = "")] +async fn new_blogpost( + db: &State, + blogpost: Form>, + config: &State, +) -> String { + if blogpost.pw == &config.wordpress_key { + let member = Role::find_by_name(&db, "Donau Linz").await.unwrap(); + Notification::create_for_role( + db, + &member, + &format!("auf unserer Website!"), + &format!("Neuer Blogpost"), + Some(blogpost.article_url), + None, + ) + .await; + "ACK".into() + } else { + "WRONG pw".into() + } +} + #[catch(403)] //forbidden fn forbidden_error() -> Flash { Flash::error(Redirect::to("/"), "Keine Berechtigung für diese Aktion. Wenn du der Meinung bist, dass du das machen darfst, melde dich bitte bei it@rudernlinz.at.") @@ -187,6 +216,7 @@ pub struct Config { smtp_pw: String, usage_log_path: String, pub openweathermap_key: String, + wordpress_key: String, } pub fn config(rocket: Rocket) -> Rocket { @@ -194,6 +224,7 @@ pub fn config(rocket: Rocket) -> Rocket { .mount("/", routes![index, steering, impressum]) .mount("/auth", auth::routes()) .mount("/wikiauth", routes![wikiauth]) + .mount("/new-blogpost", routes![new_blogpost]) .mount("/log", log::routes()) .mount("/planned", planned::routes()) .mount("/ergo", ergo::routes()) diff --git a/templates/index.html.tera b/templates/index.html.tera index cca630f..a275d0f 100644 --- a/templates/index.html.tera +++ b/templates/index.html.tera @@ -33,6 +33,13 @@
{{ notification.message | safe }}
{% endif %} {% endfor %} From eeab4c167b943e2c43611cdb341db053f292f798 Mon Sep 17 00:00:00 2001 From: philipp Date: Sun, 18 Aug 2024 21:32:34 +0200 Subject: [PATCH 2/7] switch to new pw --- Rocket.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Rocket.toml b/Rocket.toml index 9f97381..4b4856e 100644 --- a/Rocket.toml +++ b/Rocket.toml @@ -5,4 +5,4 @@ limits = { file = "10 MiB", data-form = "10 MiB"} smtp_pw = "8kIjlLH79Ky6D3jQ" usage_log_path = "./usage.txt" openweathermap_key = "c8dab8f91b5b815d76e9879cbaecd8d5" -wordpress_key = "KYplHPxd6Gtqjdx4Ruik" +wordpress_key = "pw-to-allow-sending-notifications" From 6171bb0f85c48157dcf11dbcc3c47a5daefc07bb Mon Sep 17 00:00:00 2001 From: philipp Date: Sun, 18 Aug 2024 21:37:53 +0200 Subject: [PATCH 3/7] comment wordpress changes --- notes.md => wordpress-notes.md | 37 ++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) rename notes.md => wordpress-notes.md (62%) diff --git a/notes.md b/wordpress-notes.md similarity index 62% rename from notes.md rename to wordpress-notes.md index 74ce2fe..8170489 100644 --- a/notes.md +++ b/wordpress-notes.md @@ -71,3 +71,40 @@ remove_action('authenticate', 'wp_authenticate_username_password', 20); add_filter( 'authenticate', 'rot_auth', 10, 3 ); ``` + + +# Wordpress notify rowt on newly published article + +Add the following code to `wp-content/themes/bravada/functions.php`: + +``` +function send_article_url_on_publish($new_status, $old_status, $post) { + // Check if the post is transitioning to 'publish' status + if ($new_status == 'publish' && $old_status != 'publish' && $post->post_type == 'post') { + // Get the URL of the newly published article + $article_url = get_permalink($post->ID); + + // URL to send the GET request to + $api_url = 'https://staging.rudernlinz.at/new-blogpost'; + + // Add the article URL as a query parameter + $request_url = add_query_arg(array( + 'article_url' => urlencode($article_url), + 'pw' => "pw-as-specified-in-rockettoml" + ), $api_url); + + // Send the GET request + $response = wp_remote_post($request_url); + + // Optional: Check if the request was successful + if (is_wp_error($response)) { + error_log('Failed to send GET request: ' . $response->get_error_message()); + } else { + error_log('GET request sent successfully with article URL: ' . $article_url); + } + } +} + +// Hook the function to the 'transition_post_status' action +add_action('transition_post_status', 'send_article_url_on_publish', 10, 3); +``` From 83aa9bc84c79fe03b4740e3820eb082fc90964e2 Mon Sep 17 00:00:00 2001 From: philipp Date: Sun, 18 Aug 2024 21:46:11 +0200 Subject: [PATCH 4/7] allow for article_titles --- Cargo.lock | 1 + Cargo.toml | 1 + src/tera/mod.rs | 3 ++- wordpress-notes.md | 4 +++- 4 files changed, 7 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4aed81e..a459ecd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2271,6 +2271,7 @@ dependencies = [ "sqlx", "tera", "ureq", + "urlencoding", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 39208c5..d75e236 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,6 +28,7 @@ itertools = "0.13" job_scheduler_ng = "2.0" ureq = { version = "2.9", features = ["json"] } regex = "1.10" +urlencoding = "2.1" [target.'cfg(not(windows))'.dependencies] openssl = { version = "0.10", features = [ "vendored" ] } diff --git a/src/tera/mod.rs b/src/tera/mod.rs index 982a1b7..8ef6830 100644 --- a/src/tera/mod.rs +++ b/src/tera/mod.rs @@ -121,6 +121,7 @@ fn unauthorized_error(req: &Request) -> Redirect { #[derive(FromForm, Debug)] struct NewBlogpostForm<'r> { article_url: &'r str, + article_title: &'r str, pw: &'r str, } @@ -135,7 +136,7 @@ async fn new_blogpost( Notification::create_for_role( db, &member, - &format!("auf unserer Website!"), + &urlencoding::decode(blogpost.article_title).expect("UTF-8"), &format!("Neuer Blogpost"), Some(blogpost.article_url), None, diff --git a/wordpress-notes.md b/wordpress-notes.md index 8170489..3eee2b1 100644 --- a/wordpress-notes.md +++ b/wordpress-notes.md @@ -83,6 +83,7 @@ function send_article_url_on_publish($new_status, $old_status, $post) { if ($new_status == 'publish' && $old_status != 'publish' && $post->post_type == 'post') { // Get the URL of the newly published article $article_url = get_permalink($post->ID); + $article_title = get_the_title($post->ID); // URL to send the GET request to $api_url = 'https://staging.rudernlinz.at/new-blogpost'; @@ -90,7 +91,8 @@ function send_article_url_on_publish($new_status, $old_status, $post) { // Add the article URL as a query parameter $request_url = add_query_arg(array( 'article_url' => urlencode($article_url), - 'pw' => "pw-as-specified-in-rockettoml" + 'article_title' => urlencode($article_title), + 'pw' => "pw-as-specified-in-rockettoml" ), $api_url); // Send the GET request From 8efb3aea2ceb9e73b2d81d7616b4e9f759a5410a Mon Sep 17 00:00:00 2001 From: philipp Date: Sun, 18 Aug 2024 21:57:20 +0200 Subject: [PATCH 5/7] push --- wordpress-notes.md | 42 +++++++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/wordpress-notes.md b/wordpress-notes.md index 3eee2b1..413af99 100644 --- a/wordpress-notes.md +++ b/wordpress-notes.md @@ -78,31 +78,43 @@ add_filter( 'authenticate', 'rot_auth', 10, 3 ); Add the following code to `wp-content/themes/bravada/functions.php`: ``` + function send_article_url_on_publish($new_status, $old_status, $post) { // Check if the post is transitioning to 'publish' status if ($new_status == 'publish' && $old_status != 'publish' && $post->post_type == 'post') { // Get the URL of the newly published article $article_url = get_permalink($post->ID); $article_title = get_the_title($post->ID); + + // URL to send the POST request to + $api_url = 'https://app.rudernlinz.at/new-blogpost'; + + // Prepare the data for the POST request + $body = array( + 'article_url' => $article_url, + 'article_title' => $article_title, + 'pw' => "pw-as-specified-in-rockettoml" + ); + + // Prepare the arguments for wp_remote_post + $args = array( + 'body' => $body, + 'timeout' => '5', + 'redirection' => '5', + 'httpversion' => '1.0', + 'blocking' => true, + 'headers' => array(), + 'cookies' => array() + ); - // URL to send the GET request to - $api_url = 'https://staging.rudernlinz.at/new-blogpost'; - - // Add the article URL as a query parameter - $request_url = add_query_arg(array( - 'article_url' => urlencode($article_url), - 'article_title' => urlencode($article_title), - 'pw' => "pw-as-specified-in-rockettoml" - ), $api_url); - - // Send the GET request - $response = wp_remote_post($request_url); - + // Send the POST request + $response = wp_remote_post($api_url, $args); + // Optional: Check if the request was successful if (is_wp_error($response)) { - error_log('Failed to send GET request: ' . $response->get_error_message()); + error_log('Failed to send POST request: ' . $response->get_error_message()); } else { - error_log('GET request sent successfully with article URL: ' . $article_url); + error_log('POST request sent successfully with article URL: ' . $article_url); } } } From 20da86f69efaa9ae649c8e8bb05a083dd60af3b9 Mon Sep 17 00:00:00 2001 From: philipp Date: Sun, 18 Aug 2024 22:21:36 +0200 Subject: [PATCH 6/7] delete notification again, if article is unpublished --- src/model/notification.rs | 7 +++++++ src/tera/mod.rs | 21 +++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/src/model/notification.rs b/src/model/notification.rs index 8919cb9..0dcf45a 100644 --- a/src/model/notification.rs +++ b/src/model/notification.rs @@ -179,6 +179,13 @@ ORDER BY read_at DESC, created_at DESC; .await .unwrap(); } + + pub(crate) async fn delete_by_link(db: &sqlx::Pool, link: &str) { + sqlx::query!("DELETE FROM notification WHERE link=?", link) + .execute(db) + .await + .unwrap(); + } } #[cfg(test)] diff --git a/src/tera/mod.rs b/src/tera/mod.rs index 8ef6830..9094c40 100644 --- a/src/tera/mod.rs +++ b/src/tera/mod.rs @@ -148,6 +148,26 @@ async fn new_blogpost( } } +#[derive(FromForm, Debug)] +struct BlogpostUnpublishedForm<'r> { + article_url: &'r str, + pw: &'r str, +} + +#[post("/", data = "")] +async fn blogpost_unpublished( + db: &State, + blogpost: Form>, + config: &State, +) -> String { + if blogpost.pw == &config.wordpress_key { + Notification::delete_by_link(&db, blogpost.article_url).await; + "ACK".into() + } else { + "WRONG pw".into() + } +} + #[catch(403)] //forbidden fn forbidden_error() -> Flash { Flash::error(Redirect::to("/"), "Keine Berechtigung für diese Aktion. Wenn du der Meinung bist, dass du das machen darfst, melde dich bitte bei it@rudernlinz.at.") @@ -226,6 +246,7 @@ pub fn config(rocket: Rocket) -> Rocket { .mount("/auth", auth::routes()) .mount("/wikiauth", routes![wikiauth]) .mount("/new-blogpost", routes![new_blogpost]) + .mount("/blogpost-unpublished", routes![blogpost_unpublished]) .mount("/log", log::routes()) .mount("/planned", planned::routes()) .mount("/ergo", ergo::routes()) From c68593a67db558fc695fe56de1642ee622d52f50 Mon Sep 17 00:00:00 2001 From: philipp Date: Sun, 18 Aug 2024 22:23:08 +0200 Subject: [PATCH 7/7] proper docs; Fixes #645 --- wordpress-notes.md | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/wordpress-notes.md b/wordpress-notes.md index 413af99..fc13429 100644 --- a/wordpress-notes.md +++ b/wordpress-notes.md @@ -78,7 +78,6 @@ add_filter( 'authenticate', 'rot_auth', 10, 3 ); Add the following code to `wp-content/themes/bravada/functions.php`: ``` - function send_article_url_on_publish($new_status, $old_status, $post) { // Check if the post is transitioning to 'publish' status if ($new_status == 'publish' && $old_status != 'publish' && $post->post_type == 'post') { @@ -93,7 +92,7 @@ function send_article_url_on_publish($new_status, $old_status, $post) { $body = array( 'article_url' => $article_url, 'article_title' => $article_title, - 'pw' => "pw-as-specified-in-rockettoml" + 'pw' => "wordpress_key" ); // Prepare the arguments for wp_remote_post @@ -117,6 +116,39 @@ function send_article_url_on_publish($new_status, $old_status, $post) { error_log('POST request sent successfully with article URL: ' . $article_url); } } + if ($new_status != 'publish' && $old_status == 'publish' && $post->post_type == 'post') { + $article_url = get_permalink($post->ID); + // URL to send the POST request to + $api_url = 'https://app.rudernlinz.at/blogpost-unpublished'; + + // Prepare the data for the POST request + $body = array( + 'article_url' => $article_url, + 'pw' => "wordpress_key" + ); + + // Prepare the arguments for wp_remote_post + $args = array( + 'body' => $body, + 'timeout' => '5', + 'redirection' => '5', + 'httpversion' => '1.0', + 'blocking' => true, + 'headers' => array(), + 'cookies' => array() + ); + + // Send the POST request + $response = wp_remote_post($api_url, $args); + + // Optional: Check if the request was successful + if (is_wp_error($response)) { + error_log('Failed to send POST request: ' . $response->get_error_message()); + } else { + error_log('POST request sent successfully with article URL: ' . $article_url); + } + + } } // Hook the function to the 'transition_post_status' action