Simplify community_inbox

This commit is contained in:
Felix 2020-05-14 17:44:01 +02:00
parent 1afb39dc9f
commit c591758c06

View file

@ -7,6 +7,22 @@ pub enum CommunityAcceptedObjects {
Undo(Undo), Undo(Undo),
} }
impl CommunityAcceptedObjects {
fn follow(&self) -> Result<Follow, Error> {
match self {
CommunityAcceptedObjects::Follow(f) => Ok(f.to_owned()),
CommunityAcceptedObjects::Undo(u) => Ok(
u.undo_props
.get_object_base_box()
.to_owned()
.unwrap()
.to_owned()
.into_concrete::<Follow>()?,
),
}
}
}
// TODO Consolidate community and user inboxes into a single shared one // TODO Consolidate community and user inboxes into a single shared one
/// Handler for all incoming activities to community inboxes. /// Handler for all incoming activities to community inboxes.
pub async fn community_inbox( pub async fn community_inbox(
@ -14,7 +30,7 @@ pub async fn community_inbox(
input: web::Json<CommunityAcceptedObjects>, input: web::Json<CommunityAcceptedObjects>,
path: web::Path<String>, path: web::Path<String>,
db: DbPoolParam, db: DbPoolParam,
chat_server: ChatServerParam, _chat_server: ChatServerParam,
) -> Result<HttpResponse, Error> { ) -> Result<HttpResponse, Error> {
let input = input.into_inner(); let input = input.into_inner();
let community_name = path.into_inner(); let community_name = path.into_inner();
@ -22,31 +38,13 @@ pub async fn community_inbox(
"Community {} received activity {:?}", "Community {} received activity {:?}",
&community_name, &input &community_name, &input
); );
match input { let follow = input.follow()?;
CommunityAcceptedObjects::Follow(f) => {
handle_follow(&f, &request, &community_name, db, chat_server)
}
CommunityAcceptedObjects::Undo(u) => {
handle_undo_follow(&u, &request, &community_name, db, chat_server)
}
}
}
/// Handle a follow request from a remote user, adding it to the local database and returning an
/// Accept activity.
fn handle_follow(
follow: &Follow,
request: &HttpRequest,
community_name: &str,
db: DbPoolParam,
_chat_server: ChatServerParam,
) -> Result<HttpResponse, Error> {
let user_uri = follow let user_uri = follow
.follow_props .follow_props
.get_actor_xsd_any_uri() .get_actor_xsd_any_uri()
.unwrap() .unwrap()
.to_string(); .to_string();
let _community_uri = follow let community_uri = follow
.follow_props .follow_props
.get_object_xsd_any_uri() .get_object_xsd_any_uri()
.unwrap() .unwrap()
@ -55,10 +53,24 @@ fn handle_follow(
let conn = db.get()?; let conn = db.get()?;
let user = get_or_fetch_and_upsert_remote_user(&user_uri, &conn)?; let user = get_or_fetch_and_upsert_remote_user(&user_uri, &conn)?;
let community = Community::read_from_name(&conn, &community_name)?; let community = get_or_fetch_and_upsert_remote_community(&community_uri, &conn)?;
verify(&request, &user)?; verify(&request, &user)?;
match input {
CommunityAcceptedObjects::Follow(f) => handle_follow(&f, &user, &community, &conn),
CommunityAcceptedObjects::Undo(u) => handle_undo_follow(&u, &user, &community, &conn),
}
}
/// Handle a follow request from a remote user, adding it to the local database and returning an
/// Accept activity.
fn handle_follow(
follow: &Follow,
user: &User_,
community: &Community,
conn: &PgConnection,
) -> Result<HttpResponse, Error> {
insert_activity(&conn, user.id, &follow, false)?; insert_activity(&conn, user.id, &follow, false)?;
let community_follower_form = CommunityFollowerForm { let community_follower_form = CommunityFollowerForm {
@ -76,39 +88,11 @@ fn handle_follow(
fn handle_undo_follow( fn handle_undo_follow(
undo: &Undo, undo: &Undo,
request: &HttpRequest, user: &User_,
community_name: &str, community: &Community,
db: DbPoolParam, conn: &PgConnection,
_chat_server: ChatServerParam,
) -> Result<HttpResponse, Error> { ) -> Result<HttpResponse, Error> {
let follow = undo insert_activity(&conn, user.id, &undo, false)?;
.undo_props
.get_object_base_box()
.to_owned()
.unwrap()
.to_owned()
.into_concrete::<Follow>()?;
let user_uri = follow
.follow_props
.get_actor_xsd_any_uri()
.unwrap()
.to_string();
let _community_uri = follow
.follow_props
.get_object_xsd_any_uri()
.unwrap()
.to_string();
let conn = db.get()?;
let user = get_or_fetch_and_upsert_remote_user(&user_uri, &conn)?;
let community = Community::read_from_name(&conn, &community_name)?;
verify(&request, &user)?;
insert_activity(&conn, user.id, &follow, false)?;
let community_follower_form = CommunityFollowerForm { let community_follower_form = CommunityFollowerForm {
community_id: community.id, community_id: community.id,