diff --git a/plume-common/src/utils.rs b/plume-common/src/utils.rs index cae6504d..cbb4db72 100644 --- a/plume-common/src/utils.rs +++ b/plume-common/src/utils.rs @@ -2,10 +2,7 @@ use heck::ToUpperCamelCase; use openssl::rand::rand_bytes; use pulldown_cmark::{html, CodeBlockKind, CowStr, Event, LinkType, Options, Parser, Tag}; use regex_syntax::is_word_character; -use rocket::{ - http::uri::Uri, - response::{Flash, Redirect}, -}; +use rocket::http::uri::Uri; use std::collections::HashSet; use syntect::html::{ClassStyle, ClassedHTMLGenerator}; use syntect::parsing::SyntaxSet; @@ -80,19 +77,6 @@ pub fn iri_percent_encode_seg_char(c: char) -> String { } } -/** -* Redirects to the login page with a given message. -* -* Note that the message should be translated before passed to this function. -*/ -pub fn requires_login>>(message: &str, url: T) -> Flash { - Flash::new( - Redirect::to(format!("/login?m={}", Uri::percent_encode(message))), - "callback", - url.into().to_string(), - ) -} - #[derive(Debug)] enum State { Mention, diff --git a/src/main.rs b/src/main.rs index 4d236661..32d181f2 100755 --- a/src/main.rs +++ b/src/main.rs @@ -33,6 +33,7 @@ init_i18n!( mod api; mod inbox; mod mail; +mod utils; #[macro_use] mod template_utils; mod routes; diff --git a/src/routes/blogs.rs b/src/routes/blogs.rs index 07a8e8f1..c56b5a6e 100644 --- a/src/routes/blogs.rs +++ b/src/routes/blogs.rs @@ -11,6 +11,7 @@ use validator::{Validate, ValidationError, ValidationErrors}; use crate::routes::{errors::ErrorPage, Page, RespondOrRedirect}; use crate::template_utils::{IntoContext, Ructe}; +use crate::utils::requires_login; use plume_common::activity_pub::{ActivityStream, ApRequest}; use plume_common::utils; use plume_models::{ @@ -62,7 +63,7 @@ pub fn new(conn: DbConn, rockets: PlumeRocket, _user: User) -> Ructe { #[get("/blogs/new", rank = 2)] pub fn new_auth(i18n: I18n) -> Flash { - utils::requires_login( + requires_login( &i18n!( i18n.catalog, "To create a new blog, you need to be logged in" diff --git a/src/routes/likes.rs b/src/routes/likes.rs index 267bbe45..25ca68bf 100644 --- a/src/routes/likes.rs +++ b/src/routes/likes.rs @@ -2,8 +2,8 @@ use rocket::response::{Flash, Redirect}; use rocket_i18n::I18n; use crate::routes::errors::ErrorPage; +use crate::utils::requires_login; use plume_common::activity_pub::broadcast; -use plume_common::utils; use plume_models::{ blogs::Blog, db_conn::DbConn, inbox::inbox, likes, posts::Post, timeline::*, users::User, Error, PlumeRocket, CONFIG, @@ -54,7 +54,7 @@ pub fn create( #[post("/~///like", rank = 2)] pub fn create_auth(blog: String, slug: String, i18n: I18n) -> Flash { - utils::requires_login( + requires_login( &i18n!(i18n.catalog, "To like a post, you need to be logged in"), uri!(create: blog = blog, slug = slug), ) diff --git a/src/routes/notifications.rs b/src/routes/notifications.rs index 1b987336..2f473ed0 100644 --- a/src/routes/notifications.rs +++ b/src/routes/notifications.rs @@ -3,7 +3,7 @@ use rocket_i18n::I18n; use crate::routes::{errors::ErrorPage, Page}; use crate::template_utils::{IntoContext, Ructe}; -use plume_common::utils; +use crate::utils::requires_login; use plume_models::{db_conn::DbConn, notifications::Notification, users::User, PlumeRocket}; #[get("/notifications?")] @@ -24,7 +24,7 @@ pub fn notifications( #[get("/notifications?", rank = 2)] pub fn notifications_auth(i18n: I18n, page: Option) -> Flash { - utils::requires_login( + requires_login( &i18n!( i18n.catalog, "To see your notifications, you need to be logged in" diff --git a/src/routes/posts.rs b/src/routes/posts.rs index de0f4541..854a5621 100644 --- a/src/routes/posts.rs +++ b/src/routes/posts.rs @@ -14,8 +14,9 @@ use crate::routes::{ comments::NewCommentForm, errors::ErrorPage, ContentLen, RemoteForm, RespondOrRedirect, }; use crate::template_utils::{IntoContext, Ructe}; +use crate::utils::requires_login; use plume_common::activity_pub::{broadcast, ActivityStream, ApRequest}; -use plume_common::utils; +use plume_common::utils::md_to_html; use plume_models::{ blogs::*, comments::{Comment, CommentTree}, @@ -120,7 +121,7 @@ pub fn activity_details( #[get("/~//new", rank = 2)] pub fn new_auth(blog: String, i18n: I18n) -> Flash { - utils::requires_login( + requires_login( &i18n!( i18n.catalog, "To write a new post, you need to be logged in" @@ -268,7 +269,7 @@ pub fn update( ) .into() } else { - let (content, mentions, hashtags) = utils::md_to_html( + let (content, mentions, hashtags) = md_to_html( form.content.to_string().as_ref(), Some( &Instance::get_local() @@ -452,7 +453,7 @@ pub fn create( .into()); } - let (content, mentions, hashtags) = utils::md_to_html( + let (content, mentions, hashtags) = md_to_html( form.content.to_string().as_ref(), Some( &Instance::get_local() diff --git a/src/routes/reshares.rs b/src/routes/reshares.rs index 67b2b84f..b6d11c8a 100644 --- a/src/routes/reshares.rs +++ b/src/routes/reshares.rs @@ -2,8 +2,8 @@ use rocket::response::{Flash, Redirect}; use rocket_i18n::I18n; use crate::routes::errors::ErrorPage; +use crate::utils::requires_login; use plume_common::activity_pub::broadcast; -use plume_common::utils; use plume_models::{ blogs::Blog, db_conn::DbConn, inbox::inbox, posts::Post, reshares::*, timeline::*, users::User, Error, PlumeRocket, CONFIG, @@ -54,7 +54,7 @@ pub fn create( #[post("/~///reshare", rank = 1)] pub fn create_auth(blog: String, slug: String, i18n: I18n) -> Flash { - utils::requires_login( + requires_login( &i18n!(i18n.catalog, "To reshare a post, you need to be logged in"), uri!(create: blog = blog, slug = slug), ) diff --git a/src/routes/user.rs b/src/routes/user.rs index e7d66de1..ac8371a3 100644 --- a/src/routes/user.rs +++ b/src/routes/user.rs @@ -14,8 +14,9 @@ use crate::routes::{ email_signups::EmailSignupForm, errors::ErrorPage, Page, RemoteForm, RespondOrRedirect, }; use crate::template_utils::{IntoContext, Ructe}; +use crate::utils::requires_login; use plume_common::activity_pub::{broadcast, ActivityStream, ApRequest, Id}; -use plume_common::utils; +use plume_common::utils::md_to_html; use plume_models::{ blogs::Blog, db_conn::DbConn, follows, headers::Headers, inbox::inbox as local_inbox, instance::Instance, medias::Media, posts::Post, reshares::Reshare, safe_string::SafeString, @@ -26,7 +27,7 @@ use plume_models::{ pub fn me(user: Option) -> RespondOrRedirect { match user { Some(user) => Redirect::to(uri!(details: name = user.username)).into(), - None => utils::requires_login("", uri!(me)).into(), + None => requires_login("", uri!(me)).into(), } } @@ -71,7 +72,7 @@ pub fn dashboard(user: User, conn: DbConn, rockets: PlumeRocket) -> Result Flash { - utils::requires_login( + requires_login( &i18n!( i18n.catalog, "To access your dashboard, you need to be logged in" @@ -187,7 +188,7 @@ pub fn follow_not_connected( #[get("/@//follow?local", rank = 2)] pub fn follow_auth(name: String, i18n: I18n) -> Flash { - utils::requires_login( + requires_login( &i18n!( i18n.catalog, "To subscribe to someone, you need to be logged in" @@ -307,7 +308,7 @@ pub fn edit( #[get("/@//edit", rank = 2)] pub fn edit_auth(name: String, i18n: I18n) -> Flash { - utils::requires_login( + requires_login( &i18n!( i18n.catalog, "To edit your profile, you need to be logged in" @@ -338,7 +339,7 @@ pub fn update( user.email = Some(form.email.clone()); user.summary = form.summary.clone(); user.summary_html = SafeString::new( - &utils::md_to_html( + &md_to_html( &form.summary, None, false, diff --git a/src/utils.rs b/src/utils.rs new file mode 100644 index 00000000..b204814c --- /dev/null +++ b/src/utils.rs @@ -0,0 +1,17 @@ +use rocket::{ + http::uri::Uri, + response::{Flash, Redirect}, +}; + +/** +* Redirects to the login page with a given message. +* +* Note that the message should be translated before passed to this function. +*/ +pub fn requires_login>>(message: &str, url: T) -> Flash { + Flash::new( + Redirect::to(format!("/login?m={}", Uri::percent_encode(message))), + "callback", + url.into().to_string(), + ) +}