From 20da86f69efaa9ae649c8e8bb05a083dd60af3b9 Mon Sep 17 00:00:00 2001 From: philipp Date: Sun, 18 Aug 2024 22:21:36 +0200 Subject: [PATCH 1/2] 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 2/2] 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