Use the ApRequest guard for routes that need it + Fix a few issues with its impl

Also fixes some Rocket warnings!
This commit is contained in:
Bat 2018-07-11 17:30:01 +02:00
parent 8e47219d82
commit b95e384ed7
8 changed files with 177 additions and 22 deletions

View File

@ -60,22 +60,25 @@ impl<'r, O: Object> Responder<'r> for ActivityStream<O> {
}
}
#[derive(Clone)]
pub struct ApRequest;
impl<'a, 'r> FromRequest<'a, 'r> for ApRequest {
type Error = ();
fn from_request(request: &'a Request<'r>) -> Outcome<Self, (Status, Self::Error), ()> {
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(()))
}
}

View File

@ -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&#x27;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 ""

View File

@ -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 ""

View File

@ -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 ""

View File

@ -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ę"

View File

@ -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<User>) -> Template {
})
}
#[get("/~/<name>", format = "application/activity+json", rank = 1)]
fn activity_details(name: String, conn: DbConn) -> ActivityStream<CustomGroup> {
#[get("/~/<name>", rank = 1)]
fn activity_details(name: String, conn: DbConn, _ap: ApRequest) -> ActivityStream<CustomGroup> {
let blog = Blog::find_local(&*conn, name).unwrap();
ActivityStream::new(blog.into_activity(&*conn))
}

View File

@ -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<User>
})
}
#[get("/~/<blog>/<slug>", rank = 3, format = "application/activity+json")]
fn activity_details(blog: String, slug: String, conn: DbConn) -> ActivityStream<Article> {
#[get("/~/<blog>/<slug>", rank = 3)]
fn activity_details(blog: String, slug: String, conn: DbConn, _ap: ApRequest) -> ActivityStream<Article> {
let blog = Blog::find_by_fqn(&*conn, blog).unwrap();
let post = Post::find_by_slug(&*conn, slug, blog.id).unwrap();

View File

@ -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<User>) -> Template {
})
}
#[get("/@/<name>", format = "application/activity+json", rank = 1)]
fn activity_details(name: String, conn: DbConn) -> ActivityStream<CustomPerson> {
#[get("/@/<name>", rank = 1)]
fn activity_details(name: String, conn: DbConn, _ap: ApRequest) -> ActivityStream<CustomPerson> {
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("/@/<name>/followers", format = "application/activity+json")]
fn ap_followers(name: String, conn: DbConn) -> ActivityStream<OrderedCollection> {
#[get("/@/<name>/followers")]
fn ap_followers(name: String, conn: DbConn, _ap: ApRequest) -> ActivityStream<OrderedCollection> {
let user = User::find_local(&*conn, name).unwrap();
let followers = user.get_followers(&*conn).into_iter().map(|f| Id::new(f.ap_url)).collect::<Vec<Id>>();