Move require_logins from plume-common to plume
This commit is contained in:
parent
8c48abf48e
commit
74254aed4a
@ -2,10 +2,7 @@ use heck::ToUpperCamelCase;
|
|||||||
use openssl::rand::rand_bytes;
|
use openssl::rand::rand_bytes;
|
||||||
use pulldown_cmark::{html, CodeBlockKind, CowStr, Event, LinkType, Options, Parser, Tag};
|
use pulldown_cmark::{html, CodeBlockKind, CowStr, Event, LinkType, Options, Parser, Tag};
|
||||||
use regex_syntax::is_word_character;
|
use regex_syntax::is_word_character;
|
||||||
use rocket::{
|
use rocket::http::uri::Uri;
|
||||||
http::uri::Uri,
|
|
||||||
response::{Flash, Redirect},
|
|
||||||
};
|
|
||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
use syntect::html::{ClassStyle, ClassedHTMLGenerator};
|
use syntect::html::{ClassStyle, ClassedHTMLGenerator};
|
||||||
use syntect::parsing::SyntaxSet;
|
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<T: Into<Uri<'static>>>(message: &str, url: T) -> Flash<Redirect> {
|
|
||||||
Flash::new(
|
|
||||||
Redirect::to(format!("/login?m={}", Uri::percent_encode(message))),
|
|
||||||
"callback",
|
|
||||||
url.into().to_string(),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
enum State {
|
enum State {
|
||||||
Mention,
|
Mention,
|
||||||
|
@ -33,6 +33,7 @@ init_i18n!(
|
|||||||
mod api;
|
mod api;
|
||||||
mod inbox;
|
mod inbox;
|
||||||
mod mail;
|
mod mail;
|
||||||
|
mod utils;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
mod template_utils;
|
mod template_utils;
|
||||||
mod routes;
|
mod routes;
|
||||||
|
@ -11,6 +11,7 @@ use validator::{Validate, ValidationError, ValidationErrors};
|
|||||||
|
|
||||||
use crate::routes::{errors::ErrorPage, Page, RespondOrRedirect};
|
use crate::routes::{errors::ErrorPage, Page, RespondOrRedirect};
|
||||||
use crate::template_utils::{IntoContext, Ructe};
|
use crate::template_utils::{IntoContext, Ructe};
|
||||||
|
use crate::utils::requires_login;
|
||||||
use plume_common::activity_pub::{ActivityStream, ApRequest};
|
use plume_common::activity_pub::{ActivityStream, ApRequest};
|
||||||
use plume_common::utils;
|
use plume_common::utils;
|
||||||
use plume_models::{
|
use plume_models::{
|
||||||
@ -62,7 +63,7 @@ pub fn new(conn: DbConn, rockets: PlumeRocket, _user: User) -> Ructe {
|
|||||||
|
|
||||||
#[get("/blogs/new", rank = 2)]
|
#[get("/blogs/new", rank = 2)]
|
||||||
pub fn new_auth(i18n: I18n) -> Flash<Redirect> {
|
pub fn new_auth(i18n: I18n) -> Flash<Redirect> {
|
||||||
utils::requires_login(
|
requires_login(
|
||||||
&i18n!(
|
&i18n!(
|
||||||
i18n.catalog,
|
i18n.catalog,
|
||||||
"To create a new blog, you need to be logged in"
|
"To create a new blog, you need to be logged in"
|
||||||
|
@ -2,8 +2,8 @@ use rocket::response::{Flash, Redirect};
|
|||||||
use rocket_i18n::I18n;
|
use rocket_i18n::I18n;
|
||||||
|
|
||||||
use crate::routes::errors::ErrorPage;
|
use crate::routes::errors::ErrorPage;
|
||||||
|
use crate::utils::requires_login;
|
||||||
use plume_common::activity_pub::broadcast;
|
use plume_common::activity_pub::broadcast;
|
||||||
use plume_common::utils;
|
|
||||||
use plume_models::{
|
use plume_models::{
|
||||||
blogs::Blog, db_conn::DbConn, inbox::inbox, likes, posts::Post, timeline::*, users::User,
|
blogs::Blog, db_conn::DbConn, inbox::inbox, likes, posts::Post, timeline::*, users::User,
|
||||||
Error, PlumeRocket, CONFIG,
|
Error, PlumeRocket, CONFIG,
|
||||||
@ -54,7 +54,7 @@ pub fn create(
|
|||||||
|
|
||||||
#[post("/~/<blog>/<slug>/like", rank = 2)]
|
#[post("/~/<blog>/<slug>/like", rank = 2)]
|
||||||
pub fn create_auth(blog: String, slug: String, i18n: I18n) -> Flash<Redirect> {
|
pub fn create_auth(blog: String, slug: String, i18n: I18n) -> Flash<Redirect> {
|
||||||
utils::requires_login(
|
requires_login(
|
||||||
&i18n!(i18n.catalog, "To like a post, you need to be logged in"),
|
&i18n!(i18n.catalog, "To like a post, you need to be logged in"),
|
||||||
uri!(create: blog = blog, slug = slug),
|
uri!(create: blog = blog, slug = slug),
|
||||||
)
|
)
|
||||||
|
@ -3,7 +3,7 @@ use rocket_i18n::I18n;
|
|||||||
|
|
||||||
use crate::routes::{errors::ErrorPage, Page};
|
use crate::routes::{errors::ErrorPage, Page};
|
||||||
use crate::template_utils::{IntoContext, Ructe};
|
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};
|
use plume_models::{db_conn::DbConn, notifications::Notification, users::User, PlumeRocket};
|
||||||
|
|
||||||
#[get("/notifications?<page>")]
|
#[get("/notifications?<page>")]
|
||||||
@ -24,7 +24,7 @@ pub fn notifications(
|
|||||||
|
|
||||||
#[get("/notifications?<page>", rank = 2)]
|
#[get("/notifications?<page>", rank = 2)]
|
||||||
pub fn notifications_auth(i18n: I18n, page: Option<Page>) -> Flash<Redirect> {
|
pub fn notifications_auth(i18n: I18n, page: Option<Page>) -> Flash<Redirect> {
|
||||||
utils::requires_login(
|
requires_login(
|
||||||
&i18n!(
|
&i18n!(
|
||||||
i18n.catalog,
|
i18n.catalog,
|
||||||
"To see your notifications, you need to be logged in"
|
"To see your notifications, you need to be logged in"
|
||||||
|
@ -14,8 +14,9 @@ use crate::routes::{
|
|||||||
comments::NewCommentForm, errors::ErrorPage, ContentLen, RemoteForm, RespondOrRedirect,
|
comments::NewCommentForm, errors::ErrorPage, ContentLen, RemoteForm, RespondOrRedirect,
|
||||||
};
|
};
|
||||||
use crate::template_utils::{IntoContext, Ructe};
|
use crate::template_utils::{IntoContext, Ructe};
|
||||||
|
use crate::utils::requires_login;
|
||||||
use plume_common::activity_pub::{broadcast, ActivityStream, ApRequest};
|
use plume_common::activity_pub::{broadcast, ActivityStream, ApRequest};
|
||||||
use plume_common::utils;
|
use plume_common::utils::md_to_html;
|
||||||
use plume_models::{
|
use plume_models::{
|
||||||
blogs::*,
|
blogs::*,
|
||||||
comments::{Comment, CommentTree},
|
comments::{Comment, CommentTree},
|
||||||
@ -120,7 +121,7 @@ pub fn activity_details(
|
|||||||
|
|
||||||
#[get("/~/<blog>/new", rank = 2)]
|
#[get("/~/<blog>/new", rank = 2)]
|
||||||
pub fn new_auth(blog: String, i18n: I18n) -> Flash<Redirect> {
|
pub fn new_auth(blog: String, i18n: I18n) -> Flash<Redirect> {
|
||||||
utils::requires_login(
|
requires_login(
|
||||||
&i18n!(
|
&i18n!(
|
||||||
i18n.catalog,
|
i18n.catalog,
|
||||||
"To write a new post, you need to be logged in"
|
"To write a new post, you need to be logged in"
|
||||||
@ -268,7 +269,7 @@ pub fn update(
|
|||||||
)
|
)
|
||||||
.into()
|
.into()
|
||||||
} else {
|
} else {
|
||||||
let (content, mentions, hashtags) = utils::md_to_html(
|
let (content, mentions, hashtags) = md_to_html(
|
||||||
form.content.to_string().as_ref(),
|
form.content.to_string().as_ref(),
|
||||||
Some(
|
Some(
|
||||||
&Instance::get_local()
|
&Instance::get_local()
|
||||||
@ -452,7 +453,7 @@ pub fn create(
|
|||||||
.into());
|
.into());
|
||||||
}
|
}
|
||||||
|
|
||||||
let (content, mentions, hashtags) = utils::md_to_html(
|
let (content, mentions, hashtags) = md_to_html(
|
||||||
form.content.to_string().as_ref(),
|
form.content.to_string().as_ref(),
|
||||||
Some(
|
Some(
|
||||||
&Instance::get_local()
|
&Instance::get_local()
|
||||||
|
@ -2,8 +2,8 @@ use rocket::response::{Flash, Redirect};
|
|||||||
use rocket_i18n::I18n;
|
use rocket_i18n::I18n;
|
||||||
|
|
||||||
use crate::routes::errors::ErrorPage;
|
use crate::routes::errors::ErrorPage;
|
||||||
|
use crate::utils::requires_login;
|
||||||
use plume_common::activity_pub::broadcast;
|
use plume_common::activity_pub::broadcast;
|
||||||
use plume_common::utils;
|
|
||||||
use plume_models::{
|
use plume_models::{
|
||||||
blogs::Blog, db_conn::DbConn, inbox::inbox, posts::Post, reshares::*, timeline::*, users::User,
|
blogs::Blog, db_conn::DbConn, inbox::inbox, posts::Post, reshares::*, timeline::*, users::User,
|
||||||
Error, PlumeRocket, CONFIG,
|
Error, PlumeRocket, CONFIG,
|
||||||
@ -54,7 +54,7 @@ pub fn create(
|
|||||||
|
|
||||||
#[post("/~/<blog>/<slug>/reshare", rank = 1)]
|
#[post("/~/<blog>/<slug>/reshare", rank = 1)]
|
||||||
pub fn create_auth(blog: String, slug: String, i18n: I18n) -> Flash<Redirect> {
|
pub fn create_auth(blog: String, slug: String, i18n: I18n) -> Flash<Redirect> {
|
||||||
utils::requires_login(
|
requires_login(
|
||||||
&i18n!(i18n.catalog, "To reshare a post, you need to be logged in"),
|
&i18n!(i18n.catalog, "To reshare a post, you need to be logged in"),
|
||||||
uri!(create: blog = blog, slug = slug),
|
uri!(create: blog = blog, slug = slug),
|
||||||
)
|
)
|
||||||
|
@ -14,8 +14,9 @@ use crate::routes::{
|
|||||||
email_signups::EmailSignupForm, errors::ErrorPage, Page, RemoteForm, RespondOrRedirect,
|
email_signups::EmailSignupForm, errors::ErrorPage, Page, RemoteForm, RespondOrRedirect,
|
||||||
};
|
};
|
||||||
use crate::template_utils::{IntoContext, Ructe};
|
use crate::template_utils::{IntoContext, Ructe};
|
||||||
|
use crate::utils::requires_login;
|
||||||
use plume_common::activity_pub::{broadcast, ActivityStream, ApRequest, Id};
|
use plume_common::activity_pub::{broadcast, ActivityStream, ApRequest, Id};
|
||||||
use plume_common::utils;
|
use plume_common::utils::md_to_html;
|
||||||
use plume_models::{
|
use plume_models::{
|
||||||
blogs::Blog, db_conn::DbConn, follows, headers::Headers, inbox::inbox as local_inbox,
|
blogs::Blog, db_conn::DbConn, follows, headers::Headers, inbox::inbox as local_inbox,
|
||||||
instance::Instance, medias::Media, posts::Post, reshares::Reshare, safe_string::SafeString,
|
instance::Instance, medias::Media, posts::Post, reshares::Reshare, safe_string::SafeString,
|
||||||
@ -26,7 +27,7 @@ use plume_models::{
|
|||||||
pub fn me(user: Option<User>) -> RespondOrRedirect {
|
pub fn me(user: Option<User>) -> RespondOrRedirect {
|
||||||
match user {
|
match user {
|
||||||
Some(user) => Redirect::to(uri!(details: name = user.username)).into(),
|
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<Ructe
|
|||||||
|
|
||||||
#[get("/dashboard", rank = 2)]
|
#[get("/dashboard", rank = 2)]
|
||||||
pub fn dashboard_auth(i18n: I18n) -> Flash<Redirect> {
|
pub fn dashboard_auth(i18n: I18n) -> Flash<Redirect> {
|
||||||
utils::requires_login(
|
requires_login(
|
||||||
&i18n!(
|
&i18n!(
|
||||||
i18n.catalog,
|
i18n.catalog,
|
||||||
"To access your dashboard, you need to be logged in"
|
"To access your dashboard, you need to be logged in"
|
||||||
@ -187,7 +188,7 @@ pub fn follow_not_connected(
|
|||||||
|
|
||||||
#[get("/@/<name>/follow?local", rank = 2)]
|
#[get("/@/<name>/follow?local", rank = 2)]
|
||||||
pub fn follow_auth(name: String, i18n: I18n) -> Flash<Redirect> {
|
pub fn follow_auth(name: String, i18n: I18n) -> Flash<Redirect> {
|
||||||
utils::requires_login(
|
requires_login(
|
||||||
&i18n!(
|
&i18n!(
|
||||||
i18n.catalog,
|
i18n.catalog,
|
||||||
"To subscribe to someone, you need to be logged in"
|
"To subscribe to someone, you need to be logged in"
|
||||||
@ -307,7 +308,7 @@ pub fn edit(
|
|||||||
|
|
||||||
#[get("/@/<name>/edit", rank = 2)]
|
#[get("/@/<name>/edit", rank = 2)]
|
||||||
pub fn edit_auth(name: String, i18n: I18n) -> Flash<Redirect> {
|
pub fn edit_auth(name: String, i18n: I18n) -> Flash<Redirect> {
|
||||||
utils::requires_login(
|
requires_login(
|
||||||
&i18n!(
|
&i18n!(
|
||||||
i18n.catalog,
|
i18n.catalog,
|
||||||
"To edit your profile, you need to be logged in"
|
"To edit your profile, you need to be logged in"
|
||||||
@ -338,7 +339,7 @@ pub fn update(
|
|||||||
user.email = Some(form.email.clone());
|
user.email = Some(form.email.clone());
|
||||||
user.summary = form.summary.clone();
|
user.summary = form.summary.clone();
|
||||||
user.summary_html = SafeString::new(
|
user.summary_html = SafeString::new(
|
||||||
&utils::md_to_html(
|
&md_to_html(
|
||||||
&form.summary,
|
&form.summary,
|
||||||
None,
|
None,
|
||||||
false,
|
false,
|
||||||
|
17
src/utils.rs
Normal file
17
src/utils.rs
Normal file
@ -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<T: Into<Uri<'static>>>(message: &str, url: T) -> Flash<Redirect> {
|
||||||
|
Flash::new(
|
||||||
|
Redirect::to(format!("/login?m={}", Uri::percent_encode(message))),
|
||||||
|
"callback",
|
||||||
|
url.into().to_string(),
|
||||||
|
)
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user