From b95e384ed7215255c4074ad84e4657256260c08e Mon Sep 17 00:00:00 2001 From: Bat Date: Wed, 11 Jul 2018 17:30:01 +0200 Subject: [PATCH] Use the ApRequest guard for routes that need it + Fix a few issues with its impl Also fixes some Rocket warnings! --- plume-common/src/activity_pub/mod.rs | 15 +++++--- po/de.po | 55 +++++++++++++++++++++++++--- po/en.po | 33 +++++++++++++++++ po/fr.po | 37 +++++++++++++++++++ po/pl.po | 37 +++++++++++++++++++ src/routes/blogs.rs | 6 +-- src/routes/posts.rs | 6 +-- src/routes/user.rs | 10 ++--- 8 files changed, 177 insertions(+), 22 deletions(-) diff --git a/plume-common/src/activity_pub/mod.rs b/plume-common/src/activity_pub/mod.rs index b77d6eb..bd23d3a 100644 --- a/plume-common/src/activity_pub/mod.rs +++ b/plume-common/src/activity_pub/mod.rs @@ -60,22 +60,25 @@ impl<'r, O: Object> Responder<'r> for ActivityStream { } } +#[derive(Clone)] pub struct ApRequest; impl<'a, 'r> FromRequest<'a, 'r> for ApRequest { type Error = (); fn from_request(request: &'a Request<'r>) -> Outcome { - request.headers().get_one("Content-Type").map(|header| header.split(",").map(|ct| match ct { - "application/ld+json; profile=\"w3.org/ns/activitystreams\"" | - "application/ld+json;profile=\"w3.org/ns/activitystreams\"" | + request.headers().get_one("Accept").map(|header| header.split(",").map(|ct| match ct.trim() { + // bool for Forward: true if found a valid Content-Type for Plume first (HTML), false otherwise + "application/ld+json; profile=\"https://w3.org/ns/activitystreams\"" | + "application/ld+json;profile=\"https://w3.org/ns/activitystreams\"" | "application/activity+json" | "application/ld+json" => Outcome::Success(ApRequest), - _ => Outcome::Forward(()) - }).fold(Outcome::Forward(()), |out, ct| if out.is_success() { + "text/html" => Outcome::Forward(true), + _ => Outcome::Forward(false) + }).fold(Outcome::Forward(false), |out, ct| if out.is_success() || (out.is_forward() && out.clone().forwarded().unwrap()) { out } else { ct - })).unwrap_or(Outcome::Forward(())) + }).map_forward(|_| ())).unwrap_or(Outcome::Forward(())) } } diff --git a/po/de.po b/po/de.po index be7c428..ec86afe 100644 --- a/po/de.po +++ b/po/de.po @@ -47,7 +47,9 @@ msgid "Something broke on our side." msgstr "Bei dir ist etwas schief gegangen." msgid "Sorry about that. If you think this is a bug, please report it." -msgstr "Entschuldige. Wenn du denkst einen Bug gefunden zu haben, kannst du diesen gerne melden." +msgstr "" +"Entschuldige. Wenn du denkst einen Bug gefunden zu haben, kannst du diesen " +"gerne melden." msgid "Configuration" msgstr "Konfiguration" @@ -67,8 +69,11 @@ msgstr "Willkommen auf {{ instance_name | escape }}" msgid "Notifications" msgstr "Benachrichtigungen" -msgid "Written by {{ link_1 }}{{ url }}{{ link_2 }}{{ name | escape }}{{ link_3 }}" -msgstr "Geschrieben von {{ link_1 }}{{ url }}{{ link_2 }}{{ name | escape }}{{ link_3 }}" +msgid "" +"Written by {{ link_1 }}{{ url }}{{ link_2 }}{{ name | escape }}{{ link_3 }}" +msgstr "" +"Geschrieben von {{ link_1 }}{{ url }}{{ link_2 }}{{ name | escape }}" +"{{ link_3 }}" msgid "This article is under the {{ license }} license." msgstr "Dieser Artikel steht unter der {{ license }} Lizenz." @@ -132,7 +137,9 @@ msgid "Your Blogs" msgstr "Deine Blogs" msgid "You don't have any blog yet. Create your own, or ask to join one." -msgstr "Du hast bisher keinen Blog. Erstelle deinen eigenen oder tritt einem Blog bei." +msgstr "" +"Du hast bisher keinen Blog. Erstelle deinen eigenen oder tritt einem Blog " +"bei." msgid "Start a new blog" msgstr "Starte einen neuen Blog" @@ -251,7 +258,8 @@ msgid "You need to be logged in order to edit your profile" msgstr "Du musst eingeloggt sein, um dein Profil zu editieren" msgid "By {{ link_1 }}{{ link_2 }}{{ link_3 }}{{ name | escape }}{{ link_4 }}" -msgstr "Von {{ link_1 }}{{ link_2 }}{{ link_3 }}{{ name | escape }}{{ link_4 }}" +msgstr "" +"Von {{ link_1 }}{{ link_2 }}{{ link_3 }}{{ name | escape }}{{ link_4 }}" msgid "{{ data }} reshared your article" msgstr "{{ data }} hat deinen Artikel reshared" @@ -282,3 +290,40 @@ msgstr "{{ data }} hat dich erwähnt." msgid "Your comment" msgstr "Dein Kommentar" + +msgid "Unknown error" +msgstr "" + +#, fuzzy +msgid "Invalid name" +msgstr "Nutzername oder Passwort ungültig" + +msgid "A blog with the same name already exists." +msgstr "" + +#, fuzzy +msgid "Your comment can't be empty" +msgstr "Dein Kommentar" + +msgid "A post with the same title already exists." +msgstr "" + +msgid "We need an email or a username to identify you" +msgstr "" + +msgid "Your password should be at least 8 characters long" +msgstr "" + +#, fuzzy +msgid "Passwords are not matching" +msgstr "Passwort Wiederholung" + +#, fuzzy +msgid "Username can't be empty" +msgstr "Nutzername oder E-Mail" + +msgid "Invalid email" +msgstr "" + +msgid "Password should be at least 8 characters long" +msgstr "" diff --git a/po/en.po b/po/en.po index 8f35e7d..7e3192f 100644 --- a/po/en.po +++ b/po/en.po @@ -288,3 +288,36 @@ msgstr "" msgid "Your comment" msgstr "" + +msgid "Unknown error" +msgstr "" + +msgid "Invalid name" +msgstr "" + +msgid "A blog with the same name already exists." +msgstr "" + +msgid "Your comment can't be empty" +msgstr "" + +msgid "A post with the same title already exists." +msgstr "" + +msgid "We need an email or a username to identify you" +msgstr "" + +msgid "Your password should be at least 8 characters long" +msgstr "" + +msgid "Passwords are not matching" +msgstr "" + +msgid "Username can't be empty" +msgstr "" + +msgid "Invalid email" +msgstr "" + +msgid "Password should be at least 8 characters long" +msgstr "" diff --git a/po/fr.po b/po/fr.po index b256a81..9dc3ef8 100644 --- a/po/fr.po +++ b/po/fr.po @@ -291,3 +291,40 @@ msgstr "" #, fuzzy msgid "Your comment" msgstr "Envoyer le commentaire" + +msgid "Unknown error" +msgstr "" + +#, fuzzy +msgid "Invalid name" +msgstr "Nom d'utilisateur ou mot de passe invalide" + +msgid "A blog with the same name already exists." +msgstr "" + +#, fuzzy +msgid "Your comment can't be empty" +msgstr "Envoyer le commentaire" + +msgid "A post with the same title already exists." +msgstr "" + +msgid "We need an email or a username to identify you" +msgstr "" + +msgid "Your password should be at least 8 characters long" +msgstr "" + +#, fuzzy +msgid "Passwords are not matching" +msgstr "Confirmation du mot de passe" + +#, fuzzy +msgid "Username can't be empty" +msgstr "Nom d'utilisateur ou email" + +msgid "Invalid email" +msgstr "" + +msgid "Password should be at least 8 characters long" +msgstr "" diff --git a/po/pl.po b/po/pl.po index 48f2167..c639647 100644 --- a/po/pl.po +++ b/po/pl.po @@ -295,5 +295,42 @@ msgstr "{{ data }} wspomniał o Tobie." msgid "Your comment" msgstr "Twój komentarz" +msgid "Unknown error" +msgstr "" + +#, fuzzy +msgid "Invalid name" +msgstr "Nieprawidłowa nazwa użytkownika lub hasło" + +msgid "A blog with the same name already exists." +msgstr "" + +#, fuzzy +msgid "Your comment can't be empty" +msgstr "Twój komentarz" + +msgid "A post with the same title already exists." +msgstr "" + +msgid "We need an email or a username to identify you" +msgstr "" + +msgid "Your password should be at least 8 characters long" +msgstr "" + +#, fuzzy +msgid "Passwords are not matching" +msgstr "Potwierdzenie hasła" + +#, fuzzy +msgid "Username can't be empty" +msgstr "Nazwa użytkownika lub adres e-mail" + +msgid "Invalid email" +msgstr "" + +msgid "Password should be at least 8 characters long" +msgstr "" + #~ msgid "Logowanie" #~ msgstr "Zaloguj się" diff --git a/src/routes/blogs.rs b/src/routes/blogs.rs index 9a6fcb8..1a2b010 100644 --- a/src/routes/blogs.rs +++ b/src/routes/blogs.rs @@ -8,7 +8,7 @@ use serde_json; use std::{collections::HashMap, borrow::Cow}; use validator::{Validate, ValidationError, ValidationErrors}; -use plume_common::activity_pub::ActivityStream; +use plume_common::activity_pub::{ActivityStream, ApRequest}; use plume_common::utils; use plume_models::{ blog_authors::*, @@ -33,8 +33,8 @@ fn details(name: String, conn: DbConn, user: Option) -> Template { }) } -#[get("/~/", format = "application/activity+json", rank = 1)] -fn activity_details(name: String, conn: DbConn) -> ActivityStream { +#[get("/~/", rank = 1)] +fn activity_details(name: String, conn: DbConn, _ap: ApRequest) -> ActivityStream { let blog = Blog::find_local(&*conn, name).unwrap(); ActivityStream::new(blog.into_activity(&*conn)) } diff --git a/src/routes/posts.rs b/src/routes/posts.rs index 3005104..95d9886 100644 --- a/src/routes/posts.rs +++ b/src/routes/posts.rs @@ -7,7 +7,7 @@ use serde_json; use std::{collections::HashMap, borrow::Cow}; use validator::{Validate, ValidationError, ValidationErrors}; -use plume_common::activity_pub::{broadcast, ActivityStream}; +use plume_common::activity_pub::{broadcast, ActivityStream, ApRequest}; use plume_common::utils; use plume_models::{ blogs::*, @@ -55,8 +55,8 @@ fn details_response(blog: String, slug: String, conn: DbConn, user: Option }) } -#[get("/~//", rank = 3, format = "application/activity+json")] -fn activity_details(blog: String, slug: String, conn: DbConn) -> ActivityStream
{ +#[get("/~//", rank = 3)] +fn activity_details(blog: String, slug: String, conn: DbConn, _ap: ApRequest) -> ActivityStream
{ let blog = Blog::find_by_fqn(&*conn, blog).unwrap(); let post = Post::find_by_slug(&*conn, slug, blog.id).unwrap(); diff --git a/src/routes/user.rs b/src/routes/user.rs index 1b0f42e..8281a70 100644 --- a/src/routes/user.rs +++ b/src/routes/user.rs @@ -10,7 +10,7 @@ use serde_json; use validator::{Validate, ValidationError}; use plume_common::activity_pub::{ - ActivityStream, broadcast, Id, IntoId, + ActivityStream, broadcast, Id, IntoId, ApRequest, inbox::{Notify} }; use plume_common::utils; @@ -112,8 +112,8 @@ fn followers(name: String, conn: DbConn, account: Option) -> Template { }) } -#[get("/@/", format = "application/activity+json", rank = 1)] -fn activity_details(name: String, conn: DbConn) -> ActivityStream { +#[get("/@/", rank = 1)] +fn activity_details(name: String, conn: DbConn, _ap: ApRequest) -> ActivityStream { let user = User::find_local(&*conn, name).unwrap(); ActivityStream::new(user.into_activity(&*conn)) } @@ -222,8 +222,8 @@ fn inbox(name: String, conn: DbConn, data: String) -> String { } } -#[get("/@//followers", format = "application/activity+json")] -fn ap_followers(name: String, conn: DbConn) -> ActivityStream { +#[get("/@//followers")] +fn ap_followers(name: String, conn: DbConn, _ap: ApRequest) -> ActivityStream { let user = User::find_local(&*conn, name).unwrap(); let followers = user.get_followers(&*conn).into_iter().map(|f| Id::new(f.ap_url)).collect::>();