Move require_logins from plume-common to plume

This commit is contained in:
Kitaiti Makoto 2022-01-12 04:18:13 +09:00
parent 8c48abf48e
commit 74254aed4a
9 changed files with 39 additions and 34 deletions

View File

@ -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<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)]
enum State {
Mention,

View File

@ -33,6 +33,7 @@ init_i18n!(
mod api;
mod inbox;
mod mail;
mod utils;
#[macro_use]
mod template_utils;
mod routes;

View File

@ -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<Redirect> {
utils::requires_login(
requires_login(
&i18n!(
i18n.catalog,
"To create a new blog, you need to be logged in"

View File

@ -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("/~/<blog>/<slug>/like", rank = 2)]
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"),
uri!(create: blog = blog, slug = slug),
)

View File

@ -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?<page>")]
@ -24,7 +24,7 @@ pub fn notifications(
#[get("/notifications?<page>", rank = 2)]
pub fn notifications_auth(i18n: I18n, page: Option<Page>) -> Flash<Redirect> {
utils::requires_login(
requires_login(
&i18n!(
i18n.catalog,
"To see your notifications, you need to be logged in"

View File

@ -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("/~/<blog>/new", rank = 2)]
pub fn new_auth(blog: String, i18n: I18n) -> Flash<Redirect> {
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()

View File

@ -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("/~/<blog>/<slug>/reshare", rank = 1)]
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"),
uri!(create: blog = blog, slug = slug),
)

View File

@ -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<User>) -> 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<Ructe
#[get("/dashboard", rank = 2)]
pub fn dashboard_auth(i18n: I18n) -> Flash<Redirect> {
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("/@/<name>/follow?local", rank = 2)]
pub fn follow_auth(name: String, i18n: I18n) -> Flash<Redirect> {
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("/@/<name>/edit", rank = 2)]
pub fn edit_auth(name: String, i18n: I18n) -> Flash<Redirect> {
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,

17
src/utils.rs Normal file
View 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(),
)
}