@ -1,7 +1,7 @@
use crate ::{
check_is_apub_id_valid ,
objects ::FromApub ,
ActorType ,
FromApub ,
GroupExt ,
NoteExt ,
PageExt ,
@ -13,16 +13,16 @@ use anyhow::{anyhow, Context};
use chrono ::NaiveDateTime ;
use diesel ::result ::Error ::NotFound ;
use lemmy_db ::{
comment ::{ Comment , CommentForm } ,
comment ::Comment ,
comment_view ::CommentView ,
community ::{ Community , Community Form, Community Moderator, CommunityModeratorForm } ,
community ::{ Community , Community Moderator, CommunityModeratorForm } ,
community_view ::CommunityView ,
naive_now ,
post ::{ Post , PostForm } ,
post ::Post ,
post_view ::PostView ,
user ::{ UserForm , User_ } ,
user ::User_ ,
user_view ::UserView ,
Crud ,
ApubObject ,
Joinable ,
SearchType ,
} ;
@ -184,22 +184,16 @@ pub async fn search_by_apub_id(
response
}
SearchAcceptedObjects ::Page ( p ) = > {
let p ost_form = Post Form ::from_apub ( & p , context , Some ( query_url ) , recursion_counter ) . await ? ;
let p = Post ::from_apub ( & p , context , query_url , recursion_counter ) . await ? ;
let p = blocking ( context . pool ( ) , move | conn | Post ::upsert ( conn , & post_form ) ) . await ? ? ;
response . posts =
vec! [ blocking ( context . pool ( ) , move | conn | PostView ::read ( conn , p . id , None ) ) . await ? ? ] ;
response
}
SearchAcceptedObjects ::Comment ( c ) = > {
let comment_form =
CommentForm ::from_apub ( & c , context , Some ( query_url ) , recursion_counter ) . await ? ;
let c = Comment ::from_apub ( & c , context , query_url , recursion_counter ) . await ? ;
let c = blocking ( context . pool ( ) , move | conn | {
Comment ::upsert ( conn , & comment_form )
} )
. await ? ? ;
response . comments = vec! [
blocking ( context . pool ( ) , move | conn | {
CommentView ::read ( conn , c . id , None )
@ -243,7 +237,7 @@ pub(crate) async fn get_or_fetch_and_upsert_user(
) -> Result < User_ , LemmyError > {
let apub_id_owned = apub_id . to_owned ( ) ;
let user = blocking ( context . pool ( ) , move | conn | {
User_ ::read_from_a ctor _id( conn , apub_id_owned . as_ref ( ) )
User_ ::read_from_a pub _id( conn , apub_id_owned . as_ref ( ) )
} )
. await ? ;
@ -258,15 +252,13 @@ pub(crate) async fn get_or_fetch_and_upsert_user(
return Ok ( u ) ;
}
let mut uf = UserForm ::from_apub (
& person ? ,
context ,
Some ( apub_id . to_owned ( ) ) ,
recursion_counter ,
)
. await ? ;
uf . last_refreshed_at = Some ( naive_now ( ) ) ;
let user = blocking ( context . pool ( ) , move | conn | User_ ::update ( conn , u . id , & uf ) ) . await ? ? ;
let user = User_ ::from_apub ( & person ? , context , apub_id . to_owned ( ) , recursion_counter ) . await ? ;
let user_id = user . id ;
blocking ( context . pool ( ) , move | conn | {
User_ ::mark_as_updated ( conn , user_id )
} )
. await ? ? ;
Ok ( user )
}
@ -276,14 +268,7 @@ pub(crate) async fn get_or_fetch_and_upsert_user(
let person =
fetch_remote_object ::< PersonExt > ( context . client ( ) , apub_id , recursion_counter ) . await ? ;
let uf = UserForm ::from_apub (
& person ,
context ,
Some ( apub_id . to_owned ( ) ) ,
recursion_counter ,
)
. await ? ;
let user = blocking ( context . pool ( ) , move | conn | User_ ::upsert ( conn , & uf ) ) . await ? ? ;
let user = User_ ::from_apub ( & person , context , apub_id . to_owned ( ) , recursion_counter ) . await ? ;
Ok ( user )
}
@ -318,7 +303,7 @@ pub(crate) async fn get_or_fetch_and_upsert_community(
) -> Result < Community , LemmyError > {
let apub_id_owned = apub_id . to_owned ( ) ;
let community = blocking ( context . pool ( ) , move | conn | {
Community ::read_from_a ctor _id( conn , apub_id_owned . as_str ( ) )
Community ::read_from_a pub _id( conn , apub_id_owned . as_str ( ) )
} )
. await ? ;
@ -354,9 +339,8 @@ async fn fetch_remote_community(
}
let group = group ? ;
let cf =
CommunityForm ::from_apub ( & group , context , Some ( apub_id . to_owned ( ) ) , recursion_counter ) . await ? ;
let community = blocking ( context . pool ( ) , move | conn | Community ::upsert ( conn , & cf ) ) . await ? ? ;
let community =
Community ::from_apub ( & group , context , apub_id . to_owned ( ) , recursion_counter ) . await ? ;
// Also add the community moderators too
let attributed_to = group . inner . attributed_to ( ) . context ( location_info ! ( ) ) ? ;
@ -406,23 +390,13 @@ async fn fetch_remote_community(
}
for o in outbox_items {
let page = PageExt ::from_any_base ( o ) ? . context ( location_info ! ( ) ) ? ;
let page_id = page . id_unchecked ( ) . context ( location_info ! ( ) ) ? ;
// The post creator may be from a blocked instance,
// if it errors, then continue
let post = match PostForm ::from_apub ( & page , context , None , recursion_counter ) . await {
Ok ( post ) = > post ,
Err ( _ ) = > continue ,
} ;
let post_ap_id = post . ap_id . as_ref ( ) . context ( location_info ! ( ) ) ? . clone ( ) ;
// Check whether the post already exists in the local db
let existing = blocking ( context . pool ( ) , move | conn | {
Post ::read_from_apub_id ( conn , & post_ap_id )
} )
. await ? ;
match existing {
Ok ( e ) = > blocking ( context . pool ( ) , move | conn | Post ::update ( conn , e . id , & post ) ) . await ? ? ,
Err ( _ ) = > blocking ( context . pool ( ) , move | conn | Post ::upsert ( conn , & post ) ) . await ? ? ,
} ;
// The post creator may be from a blocked instance, if it errors, then skip it
if check_is_apub_id_valid ( page_id ) . is_err ( ) {
continue ;
}
Post ::from_apub ( & page , context , page_id . to_owned ( ) , recursion_counter ) . await ? ;
// TODO: we need to send a websocket update here
}
@ -448,17 +422,9 @@ pub(crate) async fn get_or_fetch_and_insert_post(
Ok ( p ) = > Ok ( p ) ,
Err ( NotFound { } ) = > {
debug ! ( "Fetching and creating remote post: {}" , post_ap_id ) ;
let p ost =
let p age =
fetch_remote_object ::< PageExt > ( context . client ( ) , post_ap_id , recursion_counter ) . await ? ;
let post_form = PostForm ::from_apub (
& post ,
context ,
Some ( post_ap_id . to_owned ( ) ) ,
recursion_counter ,
)
. await ? ;
let post = blocking ( context . pool ( ) , move | conn | Post ::upsert ( conn , & post_form ) ) . await ? ? ;
let post = Post ::from_apub ( & page , context , post_ap_id . to_owned ( ) , recursion_counter ) . await ? ;
Ok ( post )
}
@ -490,25 +456,20 @@ pub(crate) async fn get_or_fetch_and_insert_comment(
) ;
let comment =
fetch_remote_object ::< NoteExt > ( context . client ( ) , comment_ap_id , recursion_counter ) . await ? ;
let comment _form = Comment Form ::from_apub (
let comment = Comment ::from_apub (
& comment ,
context ,
Some ( comment_ap_id . to_owned ( ) ) ,
comment_ap_id . to_owned ( ) ,
recursion_counter ,
)
. await ? ;
let post_id = comment _form . post_id ;
let post_id = comment . post_id ;
let post = blocking ( context . pool ( ) , move | conn | Post ::read ( conn , post_id ) ) . await ? ? ;
if post . locked {
return Err ( anyhow ! ( "Post is locked" ) . into ( ) ) ;
}
let comment = blocking ( context . pool ( ) , move | conn | {
Comment ::upsert ( conn , & comment_form )
} )
. await ? ? ;
Ok ( comment )
}
Err ( e ) = > Err ( e . into ( ) ) ,