Avoid panics (#392)
- Use `Result` as much as possible - Display errors instead of panicking TODO (maybe in another PR? this one is already quite big): - Find a way to merge Ructe/ErrorPage types, so that we can have routes returning `Result<X, ErrorPage>` instead of panicking when we have an `Error` - Display more details about the error, to make it easier to debug (sorry, this isn't going to be fun to review, the diff is huge, but it is always the same changes)
This commit is contained in:
+57
-75
@@ -15,7 +15,7 @@ use plume_common::activity_pub::{
|
||||
};
|
||||
use schema::follows;
|
||||
use users::User;
|
||||
use {ap_url, Connection, BASE_URL};
|
||||
use {ap_url, Connection, BASE_URL, Error, Result};
|
||||
|
||||
#[derive(Clone, Queryable, Identifiable, Associations)]
|
||||
#[belongs_to(User, foreign_key = "following_id")]
|
||||
@@ -39,37 +39,30 @@ impl Follow {
|
||||
get!(follows);
|
||||
find_by!(follows, find_by_ap_url, ap_url as &str);
|
||||
|
||||
pub fn find(conn: &Connection, from: i32, to: i32) -> Option<Follow> {
|
||||
pub fn find(conn: &Connection, from: i32, to: i32) -> Result<Follow> {
|
||||
follows::table
|
||||
.filter(follows::follower_id.eq(from))
|
||||
.filter(follows::following_id.eq(to))
|
||||
.get_result(conn)
|
||||
.ok()
|
||||
.map_err(Error::from)
|
||||
}
|
||||
|
||||
pub fn to_activity(&self, conn: &Connection) -> FollowAct {
|
||||
let user = User::get(conn, self.follower_id)
|
||||
.expect("Follow::to_activity: actor not found error");
|
||||
let target = User::get(conn, self.following_id)
|
||||
.expect("Follow::to_activity: target not found error");
|
||||
pub fn to_activity(&self, conn: &Connection) -> Result<FollowAct> {
|
||||
let user = User::get(conn, self.follower_id)?;
|
||||
let target = User::get(conn, self.following_id)?;
|
||||
|
||||
let mut act = FollowAct::default();
|
||||
act.follow_props
|
||||
.set_actor_link::<Id>(user.clone().into_id())
|
||||
.expect("Follow::to_activity: actor error");
|
||||
.set_actor_link::<Id>(user.clone().into_id())?;
|
||||
act.follow_props
|
||||
.set_object_link::<Id>(target.clone().into_id())
|
||||
.expect("Follow::to_activity: object error");
|
||||
.set_object_link::<Id>(target.clone().into_id())?;
|
||||
act.object_props
|
||||
.set_id_string(self.ap_url.clone())
|
||||
.expect("Follow::to_activity: id error");
|
||||
.set_id_string(self.ap_url.clone())?;
|
||||
act.object_props
|
||||
.set_to_link(target.into_id())
|
||||
.expect("Follow::to_activity: target error");
|
||||
.set_to_link(target.into_id())?;
|
||||
act.object_props
|
||||
.set_cc_link_vec::<Id>(vec![])
|
||||
.expect("Follow::to_activity: cc error");
|
||||
act
|
||||
.set_cc_link_vec::<Id>(vec![])?;
|
||||
Ok(act)
|
||||
}
|
||||
|
||||
/// from -> The one sending the follow request
|
||||
@@ -81,78 +74,69 @@ impl Follow {
|
||||
follow: FollowAct,
|
||||
from_id: i32,
|
||||
target_id: i32,
|
||||
) -> Follow {
|
||||
) -> Result<Follow> {
|
||||
let res = Follow::insert(
|
||||
conn,
|
||||
NewFollow {
|
||||
follower_id: from_id,
|
||||
following_id: target_id,
|
||||
ap_url: follow.object_props.id_string().expect("Follow::accept_follow: get id error"),
|
||||
ap_url: follow.object_props.id_string()?,
|
||||
},
|
||||
);
|
||||
)?;
|
||||
|
||||
let mut accept = Accept::default();
|
||||
let accept_id = ap_url(&format!("{}/follow/{}/accept", BASE_URL.as_str(), &res.id));
|
||||
accept
|
||||
.object_props
|
||||
.set_id_string(accept_id)
|
||||
.expect("Follow::accept_follow: set id error");
|
||||
.set_id_string(accept_id)?;
|
||||
accept
|
||||
.object_props
|
||||
.set_to_link(from.clone().into_id())
|
||||
.expect("Follow::accept_follow: to error");
|
||||
.set_to_link(from.clone().into_id())?;
|
||||
accept
|
||||
.object_props
|
||||
.set_cc_link_vec::<Id>(vec![])
|
||||
.expect("Follow::accept_follow: cc error");
|
||||
.set_cc_link_vec::<Id>(vec![])?;
|
||||
accept
|
||||
.accept_props
|
||||
.set_actor_link::<Id>(target.clone().into_id())
|
||||
.expect("Follow::accept_follow: actor error");
|
||||
.set_actor_link::<Id>(target.clone().into_id())?;
|
||||
accept
|
||||
.accept_props
|
||||
.set_object_object(follow)
|
||||
.expect("Follow::accept_follow: object error");
|
||||
.set_object_object(follow)?;
|
||||
broadcast(&*target, accept, vec![from.clone()]);
|
||||
res
|
||||
Ok(res)
|
||||
}
|
||||
}
|
||||
|
||||
impl FromActivity<FollowAct, Connection> for Follow {
|
||||
fn from_activity(conn: &Connection, follow: FollowAct, _actor: Id) -> Follow {
|
||||
type Error = Error;
|
||||
|
||||
fn from_activity(conn: &Connection, follow: FollowAct, _actor: Id) -> Result<Follow> {
|
||||
let from_id = follow
|
||||
.follow_props
|
||||
.actor_link::<Id>()
|
||||
.map(|l| l.into())
|
||||
.unwrap_or_else(|_| {
|
||||
follow
|
||||
.follow_props
|
||||
.actor_object::<Person>()
|
||||
.expect("Follow::from_activity: actor not found error")
|
||||
.object_props
|
||||
.id_string()
|
||||
.expect("Follow::from_activity: actor not found error")
|
||||
});
|
||||
.or_else(|_| Ok(follow
|
||||
.follow_props
|
||||
.actor_object::<Person>()?
|
||||
.object_props
|
||||
.id_string()?) as Result<String>)?;
|
||||
let from =
|
||||
User::from_url(conn, &from_id).expect("Follow::from_activity: actor not found error");
|
||||
User::from_url(conn, &from_id)?;
|
||||
match User::from_url(
|
||||
conn,
|
||||
follow
|
||||
.follow_props
|
||||
.object
|
||||
.as_str()
|
||||
.expect("Follow::from_activity: target url parsing error"),
|
||||
.as_str()?,
|
||||
) {
|
||||
Some(user) => Follow::accept_follow(conn, &from, &user, follow, from.id, user.id),
|
||||
None => {
|
||||
Ok(user) => Follow::accept_follow(conn, &from, &user, follow, from.id, user.id),
|
||||
Err(_) => {
|
||||
let blog = Blog::from_url(
|
||||
conn,
|
||||
follow
|
||||
.follow_props
|
||||
.object
|
||||
.as_str()
|
||||
.expect("Follow::from_activity: target url parsing error"),
|
||||
).expect("Follow::from_activity: target not found error");
|
||||
.as_str()?,
|
||||
)?;
|
||||
Follow::accept_follow(conn, &from, &blog, follow, from.id, blog.id)
|
||||
}
|
||||
}
|
||||
@@ -160,7 +144,9 @@ impl FromActivity<FollowAct, Connection> for Follow {
|
||||
}
|
||||
|
||||
impl Notify<Connection> for Follow {
|
||||
fn notify(&self, conn: &Connection) {
|
||||
type Error = Error;
|
||||
|
||||
fn notify(&self, conn: &Connection) -> Result<()> {
|
||||
Notification::insert(
|
||||
conn,
|
||||
NewNotification {
|
||||
@@ -168,47 +154,43 @@ impl Notify<Connection> for Follow {
|
||||
object_id: self.id,
|
||||
user_id: self.following_id,
|
||||
},
|
||||
);
|
||||
).map(|_| ())
|
||||
}
|
||||
}
|
||||
|
||||
impl Deletable<Connection, Undo> for Follow {
|
||||
fn delete(&self, conn: &Connection) -> Undo {
|
||||
type Error = Error;
|
||||
|
||||
fn delete(&self, conn: &Connection) -> Result<Undo> {
|
||||
diesel::delete(self)
|
||||
.execute(conn)
|
||||
.expect("Follow::delete: follow deletion error");
|
||||
.execute(conn)?;
|
||||
|
||||
// delete associated notification if any
|
||||
if let Some(notif) = Notification::find(conn, notification_kind::FOLLOW, self.id) {
|
||||
if let Ok(notif) = Notification::find(conn, notification_kind::FOLLOW, self.id) {
|
||||
diesel::delete(¬if)
|
||||
.execute(conn)
|
||||
.expect("Follow::delete: notification deletion error");
|
||||
.execute(conn)?;
|
||||
}
|
||||
|
||||
let mut undo = Undo::default();
|
||||
undo.undo_props
|
||||
.set_actor_link(
|
||||
User::get(conn, self.follower_id)
|
||||
.expect("Follow::delete: actor error")
|
||||
User::get(conn, self.follower_id)?
|
||||
.into_id(),
|
||||
)
|
||||
.expect("Follow::delete: actor error");
|
||||
)?;
|
||||
undo.object_props
|
||||
.set_id_string(format!("{}/undo", self.ap_url))
|
||||
.expect("Follow::delete: id error");
|
||||
.set_id_string(format!("{}/undo", self.ap_url))?;
|
||||
undo.undo_props
|
||||
.set_object_link::<Id>(self.clone().into_id())
|
||||
.expect("Follow::delete: object error");
|
||||
undo
|
||||
.set_object_link::<Id>(self.clone().into_id())?;
|
||||
Ok(undo)
|
||||
}
|
||||
|
||||
fn delete_id(id: &str, actor_id: &str, conn: &Connection) {
|
||||
if let Some(follow) = Follow::find_by_ap_url(conn, id) {
|
||||
if let Some(user) = User::find_by_ap_url(conn, actor_id) {
|
||||
if user.id == follow.follower_id {
|
||||
follow.delete(conn);
|
||||
}
|
||||
}
|
||||
fn delete_id(id: &str, actor_id: &str, conn: &Connection) -> Result<Undo> {
|
||||
let follow = Follow::find_by_ap_url(conn, id)?;
|
||||
let user = User::find_by_ap_url(conn, actor_id)?;
|
||||
if user.id == follow.follower_id {
|
||||
follow.delete(conn)
|
||||
} else {
|
||||
Err(Error::Unauthorized)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user