Add unit tests for main model parts (#310)
Add tests for following models: - Blog - Instance - Media - User
This commit is contained in:
committed by
Baptiste Gelez
parent
0b9727ed28
commit
8a4702df92
+98
-62
@@ -13,76 +13,112 @@ use activitypub::{
|
||||
use failure::Error;
|
||||
use serde_json;
|
||||
|
||||
use plume_common::activity_pub::{Id, inbox::{Deletable, FromActivity, InboxError}};
|
||||
use plume_common::activity_pub::{
|
||||
inbox::{Deletable, FromActivity, InboxError},
|
||||
Id,
|
||||
};
|
||||
use plume_models::{
|
||||
Connection,
|
||||
comments::Comment,
|
||||
follows::Follow,
|
||||
instance::Instance,
|
||||
likes,
|
||||
reshares::Reshare,
|
||||
posts::Post,
|
||||
users::User
|
||||
comments::Comment, follows::Follow, instance::Instance, likes, posts::Post, reshares::Reshare,
|
||||
users::User, Connection,
|
||||
};
|
||||
|
||||
pub trait Inbox {
|
||||
fn received(&self, conn: &Connection, act: serde_json::Value) -> Result<(), Error> {
|
||||
let actor_id = Id::new(act["actor"].as_str().unwrap_or_else(|| act["actor"]["id"].as_str().expect("Inbox::received: actor_id missing error")));
|
||||
let actor_id = Id::new(act["actor"].as_str().unwrap_or_else(|| {
|
||||
act["actor"]["id"]
|
||||
.as_str()
|
||||
.expect("Inbox::received: actor_id missing error")
|
||||
}));
|
||||
match act["type"].as_str() {
|
||||
Some(t) => {
|
||||
match t {
|
||||
"Announce" => {
|
||||
Reshare::from_activity(conn, serde_json::from_value(act.clone())?, actor_id);
|
||||
Ok(())
|
||||
},
|
||||
"Create" => {
|
||||
let act: Create = serde_json::from_value(act.clone())?;
|
||||
if Post::try_from_activity(conn, act.clone()) || Comment::try_from_activity(conn, act) {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(InboxError::InvalidType)?
|
||||
}
|
||||
},
|
||||
"Delete" => {
|
||||
let act: Delete = serde_json::from_value(act.clone())?;
|
||||
Post::delete_id(act.delete_props.object_object::<Tombstone>()?.object_props.id_string()?, actor_id.into(), conn);
|
||||
Ok(())
|
||||
},
|
||||
"Follow" => {
|
||||
Follow::from_activity(conn, serde_json::from_value(act.clone())?, actor_id);
|
||||
Ok(())
|
||||
},
|
||||
"Like" => {
|
||||
likes::Like::from_activity(conn, serde_json::from_value(act.clone())?, actor_id);
|
||||
Ok(())
|
||||
},
|
||||
"Undo" => {
|
||||
let act: Undo = serde_json::from_value(act.clone())?;
|
||||
match act.undo_props.object["type"].as_str().expect("Inbox::received: undo without original type error") {
|
||||
"Like" => {
|
||||
likes::Like::delete_id(act.undo_props.object_object::<Like>()?.object_props.id_string()?, actor_id.into(), conn);
|
||||
Ok(())
|
||||
},
|
||||
"Announce" => {
|
||||
Reshare::delete_id(act.undo_props.object_object::<Announce>()?.object_props.id_string()?, actor_id.into(), conn);
|
||||
Ok(())
|
||||
},
|
||||
"Follow" => {
|
||||
Follow::delete_id(act.undo_props.object_object::<FollowAct>()?.object_props.id_string()?, actor_id.into(), conn);
|
||||
Ok(())
|
||||
}
|
||||
_ => Err(InboxError::CantUndo)?
|
||||
}
|
||||
}
|
||||
"Update" => {
|
||||
let act: Update = serde_json::from_value(act.clone())?;
|
||||
Post::handle_update(conn, act.update_props.object_object()?);
|
||||
Ok(())
|
||||
}
|
||||
_ => Err(InboxError::InvalidType)?
|
||||
Some(t) => match t {
|
||||
"Announce" => {
|
||||
Reshare::from_activity(conn, serde_json::from_value(act.clone())?, actor_id);
|
||||
Ok(())
|
||||
}
|
||||
"Create" => {
|
||||
let act: Create = serde_json::from_value(act.clone())?;
|
||||
if Post::try_from_activity(conn, act.clone())
|
||||
|| Comment::try_from_activity(conn, act)
|
||||
{
|
||||
Ok(())
|
||||
} else {
|
||||
Err(InboxError::InvalidType)?
|
||||
}
|
||||
}
|
||||
"Delete" => {
|
||||
let act: Delete = serde_json::from_value(act.clone())?;
|
||||
Post::delete_id(
|
||||
act.delete_props
|
||||
.object_object::<Tombstone>()?
|
||||
.object_props
|
||||
.id_string()?,
|
||||
actor_id.into(),
|
||||
conn,
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
"Follow" => {
|
||||
Follow::from_activity(conn, serde_json::from_value(act.clone())?, actor_id);
|
||||
Ok(())
|
||||
}
|
||||
"Like" => {
|
||||
likes::Like::from_activity(
|
||||
conn,
|
||||
serde_json::from_value(act.clone())?,
|
||||
actor_id,
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
"Undo" => {
|
||||
let act: Undo = serde_json::from_value(act.clone())?;
|
||||
match act.undo_props.object["type"]
|
||||
.as_str()
|
||||
.expect("Inbox::received: undo without original type error")
|
||||
{
|
||||
"Like" => {
|
||||
likes::Like::delete_id(
|
||||
act.undo_props
|
||||
.object_object::<Like>()?
|
||||
.object_props
|
||||
.id_string()?,
|
||||
actor_id.into(),
|
||||
conn,
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
"Announce" => {
|
||||
Reshare::delete_id(
|
||||
act.undo_props
|
||||
.object_object::<Announce>()?
|
||||
.object_props
|
||||
.id_string()?,
|
||||
actor_id.into(),
|
||||
conn,
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
"Follow" => {
|
||||
Follow::delete_id(
|
||||
act.undo_props
|
||||
.object_object::<FollowAct>()?
|
||||
.object_props
|
||||
.id_string()?,
|
||||
actor_id.into(),
|
||||
conn,
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
_ => Err(InboxError::CantUndo)?,
|
||||
}
|
||||
}
|
||||
"Update" => {
|
||||
let act: Update = serde_json::from_value(act.clone())?;
|
||||
Post::handle_update(conn, act.update_props.object_object()?);
|
||||
Ok(())
|
||||
}
|
||||
_ => Err(InboxError::InvalidType)?,
|
||||
},
|
||||
None => Err(InboxError::NoType)?
|
||||
None => Err(InboxError::NoType)?,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+239
-121
@@ -1,36 +1,27 @@
|
||||
use activitypub::{
|
||||
activity::Create,
|
||||
collection::OrderedCollection,
|
||||
object::Article
|
||||
};
|
||||
use activitypub::{activity::Create, collection::OrderedCollection, object::Article};
|
||||
use atom_syndication::{Entry, FeedBuilder};
|
||||
use rocket::{
|
||||
http::{ContentType, Cookies},
|
||||
request::LenientForm,
|
||||
response::{Content, Flash, Redirect, status},
|
||||
http::{ContentType, Cookies}
|
||||
response::{status, Content, Flash, Redirect},
|
||||
};
|
||||
use rocket_contrib::Template;
|
||||
use serde_json;
|
||||
use validator::{Validate, ValidationError};
|
||||
use workerpool::thunk::*;
|
||||
|
||||
use inbox::Inbox;
|
||||
use plume_common::activity_pub::{
|
||||
ActivityStream, broadcast, Id, IntoId, ApRequest,
|
||||
inbox::{FromActivity, Notify, Deletable},
|
||||
sign::{Signable, verify_http_headers}
|
||||
broadcast,
|
||||
inbox::{Deletable, FromActivity, Notify},
|
||||
sign::{verify_http_headers, Signable},
|
||||
ActivityStream, ApRequest, Id, IntoId,
|
||||
};
|
||||
use plume_common::utils;
|
||||
use plume_models::{
|
||||
blogs::Blog,
|
||||
db_conn::DbConn,
|
||||
follows,
|
||||
headers::Headers,
|
||||
instance::Instance,
|
||||
posts::Post,
|
||||
reshares::Reshare,
|
||||
users::*
|
||||
blogs::Blog, db_conn::DbConn, follows, headers::Headers, instance::Instance, posts::Post,
|
||||
reshares::Reshare, users::*,
|
||||
};
|
||||
use inbox::Inbox;
|
||||
use routes::Page;
|
||||
use Worker;
|
||||
|
||||
@@ -38,57 +29,84 @@ use Worker;
|
||||
fn me(user: Option<User>) -> Result<Redirect, Flash<Redirect>> {
|
||||
match user {
|
||||
Some(user) => Ok(Redirect::to(uri!(details: name = user.username))),
|
||||
None => Err(utils::requires_login("", uri!(me).into()))
|
||||
None => Err(utils::requires_login("", uri!(me).into())),
|
||||
}
|
||||
}
|
||||
|
||||
#[get("/@/<name>", rank = 2)]
|
||||
fn details(name: String, conn: DbConn, account: Option<User>, worker: Worker, fecth_articles_conn: DbConn, fecth_followers_conn: DbConn, update_conn: DbConn) -> Template {
|
||||
may_fail!(account.map(|a| a.to_json(&*conn)), User::find_by_fqn(&*conn, name), "Couldn't find requested user", |user| {
|
||||
let recents = Post::get_recents_for_author(&*conn, &user, 6);
|
||||
let reshares = Reshare::get_recents_for_author(&*conn, &user, 6);
|
||||
let user_id = user.id.clone();
|
||||
let n_followers = user.get_followers(&*conn).len();
|
||||
fn details(
|
||||
name: String,
|
||||
conn: DbConn,
|
||||
account: Option<User>,
|
||||
worker: Worker,
|
||||
fecth_articles_conn: DbConn,
|
||||
fecth_followers_conn: DbConn,
|
||||
update_conn: DbConn,
|
||||
) -> Template {
|
||||
may_fail!(
|
||||
account.map(|a| a.to_json(&*conn)),
|
||||
User::find_by_fqn(&*conn, name),
|
||||
"Couldn't find requested user",
|
||||
|user| {
|
||||
let recents = Post::get_recents_for_author(&*conn, &user, 6);
|
||||
let reshares = Reshare::get_recents_for_author(&*conn, &user, 6);
|
||||
let user_id = user.id.clone();
|
||||
let n_followers = user.get_followers(&*conn).len();
|
||||
|
||||
if !user.get_instance(&*conn).local {
|
||||
// Fetch new articles
|
||||
let user_clone = user.clone();
|
||||
worker.execute(Thunk::of(move || {
|
||||
for create_act in user_clone.fetch_outbox::<Create>() {
|
||||
match create_act.create_props.object_object::<Article>() {
|
||||
Ok(article) => {
|
||||
Post::from_activity(&*fecth_articles_conn, article, user_clone.clone().into_id());
|
||||
println!("Fetched article from remote user");
|
||||
}
|
||||
Err(e) => println!("Error while fetching articles in background: {:?}", e)
|
||||
}
|
||||
}
|
||||
}));
|
||||
|
||||
// Fetch followers
|
||||
let user_clone = user.clone();
|
||||
worker.execute(Thunk::of(move || {
|
||||
for user_id in user_clone.fetch_followers_ids() {
|
||||
let follower = User::find_by_ap_url(&*fecth_followers_conn, user_id.clone())
|
||||
.unwrap_or_else(|| User::fetch_from_url(&*fecth_followers_conn, user_id).expect("user::details: Couldn't fetch follower"));
|
||||
follows::Follow::insert(&*fecth_followers_conn, follows::NewFollow {
|
||||
follower_id: follower.id,
|
||||
following_id: user_clone.id,
|
||||
ap_url: format!("{}/follow/{}", follower.ap_url, user_clone.ap_url),
|
||||
});
|
||||
}
|
||||
}));
|
||||
|
||||
// Update profile information if needed
|
||||
let user_clone = user.clone();
|
||||
if user.needs_update() {
|
||||
if !user.get_instance(&*conn).local {
|
||||
// Fetch new articles
|
||||
let user_clone = user.clone();
|
||||
worker.execute(Thunk::of(move || {
|
||||
user_clone.refetch(&*update_conn);
|
||||
}))
|
||||
}
|
||||
}
|
||||
for create_act in user_clone.fetch_outbox::<Create>() {
|
||||
match create_act.create_props.object_object::<Article>() {
|
||||
Ok(article) => {
|
||||
Post::from_activity(
|
||||
&*fecth_articles_conn,
|
||||
article,
|
||||
user_clone.clone().into_id(),
|
||||
);
|
||||
println!("Fetched article from remote user");
|
||||
}
|
||||
Err(e) => {
|
||||
println!("Error while fetching articles in background: {:?}", e)
|
||||
}
|
||||
}
|
||||
}
|
||||
}));
|
||||
|
||||
Template::render("users/details", json!({
|
||||
// Fetch followers
|
||||
let user_clone = user.clone();
|
||||
worker.execute(Thunk::of(move || {
|
||||
for user_id in user_clone.fetch_followers_ids() {
|
||||
let follower =
|
||||
User::find_by_ap_url(&*fecth_followers_conn, user_id.clone())
|
||||
.unwrap_or_else(|| {
|
||||
User::fetch_from_url(&*fecth_followers_conn, user_id)
|
||||
.expect("user::details: Couldn't fetch follower")
|
||||
});
|
||||
follows::Follow::insert(
|
||||
&*fecth_followers_conn,
|
||||
follows::NewFollow {
|
||||
follower_id: follower.id,
|
||||
following_id: user_clone.id,
|
||||
ap_url: format!("{}/follow/{}", follower.ap_url, user_clone.ap_url),
|
||||
},
|
||||
);
|
||||
}
|
||||
}));
|
||||
|
||||
// Update profile information if needed
|
||||
let user_clone = user.clone();
|
||||
if user.needs_update() {
|
||||
worker.execute(Thunk::of(move || {
|
||||
user_clone.refetch(&*update_conn);
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
Template::render(
|
||||
"users/details",
|
||||
json!({
|
||||
"user": user.to_json(&*conn),
|
||||
"instance_url": user.get_instance(&*conn).public_domain,
|
||||
"is_remote": user.instance_id != Instance::local_id(&*conn),
|
||||
@@ -98,25 +116,30 @@ fn details(name: String, conn: DbConn, account: Option<User>, worker: Worker, fe
|
||||
"reshares": reshares.into_iter().map(|r| r.get_post(&*conn).unwrap().to_json(&*conn)).collect::<Vec<serde_json::Value>>(),
|
||||
"is_self": account.map(|a| a.id == user_id).unwrap_or(false),
|
||||
"n_followers": n_followers
|
||||
}))
|
||||
})
|
||||
}),
|
||||
)
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
#[get("/dashboard")]
|
||||
fn dashboard(user: User, conn: DbConn) -> Template {
|
||||
let blogs = Blog::find_for_author(&*conn, user.id);
|
||||
Template::render("users/dashboard", json!({
|
||||
let blogs = Blog::find_for_author(&*conn, &user);
|
||||
Template::render(
|
||||
"users/dashboard",
|
||||
json!({
|
||||
"account": user.to_json(&*conn),
|
||||
"blogs": blogs,
|
||||
"drafts": Post::drafts_by_author(&*conn, &user).into_iter().map(|a| a.to_json(&*conn)).collect::<Vec<serde_json::Value>>(),
|
||||
}))
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
#[get("/dashboard", rank = 2)]
|
||||
fn dashboard_auth() -> Flash<Redirect> {
|
||||
utils::requires_login(
|
||||
"You need to be logged in order to access your dashboard",
|
||||
uri!(dashboard).into()
|
||||
uri!(dashboard).into(),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -125,13 +148,18 @@ fn follow(name: String, conn: DbConn, user: User, worker: Worker) -> Option<Redi
|
||||
let target = User::find_by_fqn(&*conn, name.clone())?;
|
||||
if let Some(follow) = follows::Follow::find(&*conn, user.id, target.id) {
|
||||
let delete_act = follow.delete(&*conn);
|
||||
worker.execute(Thunk::of(move || broadcast(&user, delete_act, vec![target])));
|
||||
worker.execute(Thunk::of(move || {
|
||||
broadcast(&user, delete_act, vec![target])
|
||||
}));
|
||||
} else {
|
||||
let f = follows::Follow::insert(&*conn, follows::NewFollow {
|
||||
follower_id: user.id,
|
||||
following_id: target.id,
|
||||
ap_url: format!("{}/follow/{}", user.ap_url, target.ap_url),
|
||||
});
|
||||
let f = follows::Follow::insert(
|
||||
&*conn,
|
||||
follows::NewFollow {
|
||||
follower_id: user.id,
|
||||
following_id: target.id,
|
||||
ap_url: format!("{}/follow/{}", user.ap_url, target.ap_url),
|
||||
},
|
||||
);
|
||||
f.notify(&*conn);
|
||||
|
||||
let act = f.into_activity(&*conn);
|
||||
@@ -144,17 +172,23 @@ fn follow(name: String, conn: DbConn, user: User, worker: Worker) -> Option<Redi
|
||||
fn follow_auth(name: String) -> Flash<Redirect> {
|
||||
utils::requires_login(
|
||||
"You need to be logged in order to follow someone",
|
||||
uri!(follow: name = name).into()
|
||||
uri!(follow: name = name).into(),
|
||||
)
|
||||
}
|
||||
|
||||
#[get("/@/<name>/followers?<page>")]
|
||||
fn followers_paginated(name: String, conn: DbConn, account: Option<User>, page: Page) -> Template {
|
||||
may_fail!(account.map(|a| a.to_json(&*conn)), User::find_by_fqn(&*conn, name.clone()), "Couldn't find requested user", |user| {
|
||||
let user_id = user.id.clone();
|
||||
let followers_count = user.get_followers(&*conn).len();
|
||||
may_fail!(
|
||||
account.map(|a| a.to_json(&*conn)),
|
||||
User::find_by_fqn(&*conn, name.clone()),
|
||||
"Couldn't find requested user",
|
||||
|user| {
|
||||
let user_id = user.id.clone();
|
||||
let followers_count = user.get_followers(&*conn).len();
|
||||
|
||||
Template::render("users/followers", json!({
|
||||
Template::render(
|
||||
"users/followers",
|
||||
json!({
|
||||
"user": user.to_json(&*conn),
|
||||
"instance_url": user.get_instance(&*conn).public_domain,
|
||||
"is_remote": user.instance_id != Instance::local_id(&*conn),
|
||||
@@ -165,8 +199,10 @@ fn followers_paginated(name: String, conn: DbConn, account: Option<User>, page:
|
||||
"n_followers": followers_count,
|
||||
"page": page.page,
|
||||
"n_pages": Page::total(followers_count as i32)
|
||||
}))
|
||||
})
|
||||
}),
|
||||
)
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
#[get("/@/<name>/followers", rank = 2)]
|
||||
@@ -174,29 +210,38 @@ fn followers(name: String, conn: DbConn, account: Option<User>) -> Template {
|
||||
followers_paginated(name, conn, account, Page::first())
|
||||
}
|
||||
|
||||
|
||||
#[get("/@/<name>", rank = 1)]
|
||||
fn activity_details(name: String, conn: DbConn, _ap: ApRequest) -> Option<ActivityStream<CustomPerson>> {
|
||||
fn activity_details(
|
||||
name: String,
|
||||
conn: DbConn,
|
||||
_ap: ApRequest,
|
||||
) -> Option<ActivityStream<CustomPerson>> {
|
||||
let user = User::find_local(&*conn, name)?;
|
||||
Some(ActivityStream::new(user.into_activity(&*conn)))
|
||||
}
|
||||
|
||||
#[get("/users/new")]
|
||||
fn new(user: Option<User>, conn: DbConn) -> Template {
|
||||
Template::render("users/new", json!({
|
||||
Template::render(
|
||||
"users/new",
|
||||
json!({
|
||||
"enabled": Instance::get_local(&*conn).map(|i| i.open_registrations).unwrap_or(true),
|
||||
"account": user.map(|u| u.to_json(&*conn)),
|
||||
"errors": null,
|
||||
"form": null
|
||||
}))
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
#[get("/@/<name>/edit")]
|
||||
fn edit(name: String, user: User, conn: DbConn) -> Option<Template> {
|
||||
if user.username == name && !name.contains("@") {
|
||||
Some(Template::render("users/edit", json!({
|
||||
Some(Template::render(
|
||||
"users/edit",
|
||||
json!({
|
||||
"account": user.to_json(&*conn)
|
||||
})))
|
||||
}),
|
||||
))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
@@ -206,7 +251,7 @@ fn edit(name: String, user: User, conn: DbConn) -> Option<Template> {
|
||||
fn edit_auth(name: String) -> Flash<Redirect> {
|
||||
utils::requires_login(
|
||||
"You need to be logged in order to edit your profile",
|
||||
uri!(edit: name = name).into()
|
||||
uri!(edit: name = name).into(),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -219,10 +264,22 @@ struct UpdateUserForm {
|
||||
|
||||
#[put("/@/<_name>/edit", data = "<data>")]
|
||||
fn update(_name: String, conn: DbConn, user: User, data: LenientForm<UpdateUserForm>) -> Redirect {
|
||||
user.update(&*conn,
|
||||
data.get().display_name.clone().unwrap_or(user.display_name.to_string()).to_string(),
|
||||
data.get().email.clone().unwrap_or(user.email.clone().unwrap()).to_string(),
|
||||
data.get().summary.clone().unwrap_or(user.summary.to_string())
|
||||
user.update(
|
||||
&*conn,
|
||||
data.get()
|
||||
.display_name
|
||||
.clone()
|
||||
.unwrap_or(user.display_name.to_string())
|
||||
.to_string(),
|
||||
data.get()
|
||||
.email
|
||||
.clone()
|
||||
.unwrap_or(user.email.clone().unwrap())
|
||||
.to_string(),
|
||||
data.get()
|
||||
.summary
|
||||
.clone()
|
||||
.unwrap_or(user.summary.to_string()),
|
||||
);
|
||||
Redirect::to(uri!(me))
|
||||
}
|
||||
@@ -233,7 +290,9 @@ fn delete(name: String, conn: DbConn, user: User, mut cookies: Cookies) -> Optio
|
||||
if user.id == account.id {
|
||||
account.delete(&*conn);
|
||||
|
||||
cookies.get_private(AUTH_COOKIE).map(|cookie| cookies.remove_private(cookie));
|
||||
cookies
|
||||
.get_private(AUTH_COOKIE)
|
||||
.map(|cookie| cookies.remove_private(cookie));
|
||||
|
||||
Some(Redirect::to(uri!(super::instance::index)))
|
||||
} else {
|
||||
@@ -242,16 +301,32 @@ fn delete(name: String, conn: DbConn, user: User, mut cookies: Cookies) -> Optio
|
||||
}
|
||||
|
||||
#[derive(FromForm, Serialize, Validate)]
|
||||
#[validate(schema(function = "passwords_match", skip_on_field_errors = "false", message = "Passwords are not matching"))]
|
||||
#[validate(
|
||||
schema(
|
||||
function = "passwords_match",
|
||||
skip_on_field_errors = "false",
|
||||
message = "Passwords are not matching"
|
||||
)
|
||||
)]
|
||||
struct NewUserForm {
|
||||
#[validate(length(min = "1", message = "Username can't be empty"))]
|
||||
username: String,
|
||||
#[validate(email(message = "Invalid email"))]
|
||||
email: String,
|
||||
#[validate(length(min = "8", message = "Password should be at least 8 characters long"))]
|
||||
#[validate(
|
||||
length(
|
||||
min = "8",
|
||||
message = "Password should be at least 8 characters long"
|
||||
)
|
||||
)]
|
||||
password: String,
|
||||
#[validate(length(min = "8", message = "Password should be at least 8 characters long"))]
|
||||
password_confirmation: String
|
||||
#[validate(
|
||||
length(
|
||||
min = "8",
|
||||
message = "Password should be at least 8 characters long"
|
||||
)
|
||||
)]
|
||||
password_confirmation: String,
|
||||
}
|
||||
|
||||
fn passwords_match(form: &NewUserForm) -> Result<(), ValidationError> {
|
||||
@@ -264,29 +339,37 @@ fn passwords_match(form: &NewUserForm) -> Result<(), ValidationError> {
|
||||
|
||||
#[post("/users/new", data = "<data>")]
|
||||
fn create(conn: DbConn, data: LenientForm<NewUserForm>) -> Result<Redirect, Template> {
|
||||
if !Instance::get_local(&*conn).map(|i| i.open_registrations).unwrap_or(true) {
|
||||
if !Instance::get_local(&*conn)
|
||||
.map(|i| i.open_registrations)
|
||||
.unwrap_or(true)
|
||||
{
|
||||
return Ok(Redirect::to(uri!(new))); // Actually, it is an error
|
||||
}
|
||||
|
||||
let form = data.get();
|
||||
form.validate()
|
||||
.map(|_| {
|
||||
NewUser::new_local(
|
||||
NewUser::new_local(
|
||||
&*conn,
|
||||
form.username.to_string(),
|
||||
form.username.to_string(),
|
||||
false,
|
||||
String::from(""),
|
||||
form.email.to_string(),
|
||||
User::hash_pass(form.password.to_string())
|
||||
User::hash_pass(form.password.to_string()),
|
||||
).update_boxes(&*conn);
|
||||
Redirect::to(uri!(super::session::new))
|
||||
})
|
||||
.map_err(|e| Template::render("users/new", json!({
|
||||
.map_err(|e| {
|
||||
Template::render(
|
||||
"users/new",
|
||||
json!({
|
||||
"enabled": Instance::get_local(&*conn).map(|i| i.open_registrations).unwrap_or(true),
|
||||
"errors": e.inner(),
|
||||
"form": form
|
||||
})))
|
||||
}),
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
#[get("/@/<name>/outbox")]
|
||||
@@ -296,18 +379,32 @@ fn outbox(name: String, conn: DbConn) -> Option<ActivityStream<OrderedCollection
|
||||
}
|
||||
|
||||
#[post("/@/<name>/inbox", data = "<data>")]
|
||||
fn inbox(name: String, conn: DbConn, data: String, headers: Headers) -> Result<String, Option<status::BadRequest<&'static str>>> {
|
||||
fn inbox(
|
||||
name: String,
|
||||
conn: DbConn,
|
||||
data: String,
|
||||
headers: Headers,
|
||||
) -> Result<String, Option<status::BadRequest<&'static str>>> {
|
||||
let user = User::find_local(&*conn, name).ok_or(None)?;
|
||||
let act: serde_json::Value = serde_json::from_str(&data[..]).expect("user::inbox: deserialization error");
|
||||
let act: serde_json::Value =
|
||||
serde_json::from_str(&data[..]).expect("user::inbox: deserialization error");
|
||||
|
||||
let activity = act.clone();
|
||||
let actor_id = activity["actor"].as_str()
|
||||
.or_else(|| activity["actor"]["id"].as_str()).ok_or(Some(status::BadRequest(Some("Missing actor id for activity"))))?;
|
||||
let actor_id = activity["actor"]
|
||||
.as_str()
|
||||
.or_else(|| activity["actor"]["id"].as_str())
|
||||
.ok_or(Some(status::BadRequest(Some(
|
||||
"Missing actor id for activity",
|
||||
))))?;
|
||||
|
||||
let actor = User::from_url(&conn, actor_id.to_owned()).expect("user::inbox: user error");
|
||||
if !verify_http_headers(&actor, headers.0.clone(), data).is_secure() &&
|
||||
!act.clone().verify(&actor) {
|
||||
println!("Rejected invalid activity supposedly from {}, with headers {:?}", actor.username, headers.0);
|
||||
if !verify_http_headers(&actor, headers.0.clone(), data).is_secure()
|
||||
&& !act.clone().verify(&actor)
|
||||
{
|
||||
println!(
|
||||
"Rejected invalid activity supposedly from {}, with headers {:?}",
|
||||
actor.username, headers.0
|
||||
);
|
||||
return Err(Some(status::BadRequest(Some("Invalid signature"))));
|
||||
}
|
||||
|
||||
@@ -324,14 +421,28 @@ fn inbox(name: String, conn: DbConn, data: String, headers: Headers) -> Result<S
|
||||
}
|
||||
|
||||
#[get("/@/<name>/followers")]
|
||||
fn ap_followers(name: String, conn: DbConn, _ap: ApRequest) -> Option<ActivityStream<OrderedCollection>> {
|
||||
fn ap_followers(
|
||||
name: String,
|
||||
conn: DbConn,
|
||||
_ap: ApRequest,
|
||||
) -> Option<ActivityStream<OrderedCollection>> {
|
||||
let user = User::find_local(&*conn, name)?;
|
||||
let followers = user.get_followers(&*conn).into_iter().map(|f| Id::new(f.ap_url)).collect::<Vec<Id>>();
|
||||
let followers = user
|
||||
.get_followers(&*conn)
|
||||
.into_iter()
|
||||
.map(|f| Id::new(f.ap_url))
|
||||
.collect::<Vec<Id>>();
|
||||
|
||||
let mut coll = OrderedCollection::default();
|
||||
coll.object_props.set_id_string(user.followers_endpoint).expect("user::ap_followers: id error");
|
||||
coll.collection_props.set_total_items_u64(followers.len() as u64).expect("user::ap_followers: totalItems error");
|
||||
coll.collection_props.set_items_link_vec(followers).expect("user::ap_followers items error");
|
||||
coll.object_props
|
||||
.set_id_string(user.followers_endpoint)
|
||||
.expect("user::ap_followers: id error");
|
||||
coll.collection_props
|
||||
.set_total_items_u64(followers.len() as u64)
|
||||
.expect("user::ap_followers: totalItems error");
|
||||
coll.collection_props
|
||||
.set_items_link_vec(followers)
|
||||
.expect("user::ap_followers items error");
|
||||
Some(ActivityStream::new(coll))
|
||||
}
|
||||
|
||||
@@ -340,12 +451,19 @@ fn atom_feed(name: String, conn: DbConn) -> Option<Content<String>> {
|
||||
let author = User::find_by_fqn(&*conn, name.clone())?;
|
||||
let feed = FeedBuilder::default()
|
||||
.title(author.display_name.clone())
|
||||
.id(Instance::get_local(&*conn).unwrap().compute_box("~", name, "atom.xml"))
|
||||
.entries(Post::get_recents_for_author(&*conn, &author, 15)
|
||||
.into_iter()
|
||||
.map(|p| super::post_to_atom(p, &*conn))
|
||||
.collect::<Vec<Entry>>())
|
||||
.id(Instance::get_local(&*conn)
|
||||
.unwrap()
|
||||
.compute_box("~", name, "atom.xml"))
|
||||
.entries(
|
||||
Post::get_recents_for_author(&*conn, &author, 15)
|
||||
.into_iter()
|
||||
.map(|p| super::post_to_atom(p, &*conn))
|
||||
.collect::<Vec<Entry>>(),
|
||||
)
|
||||
.build()
|
||||
.expect("user::atom_feed: Error building Atom feed");
|
||||
Some(Content(ContentType::new("application", "atom+xml"), feed.to_string()))
|
||||
Some(Content(
|
||||
ContentType::new("application", "atom+xml"),
|
||||
feed.to_string(),
|
||||
))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user