use crate::{
check_is_apub_id_valid,
+ objects::FromApub,
ActorType,
- FromApub,
GroupExt,
NoteExt,
PageExt,
use chrono::NaiveDateTime;
use diesel::result::Error::NotFound;
use lemmy_db::{
- comment::{Comment, CommentForm},
+ comment::Comment,
comment_view::CommentView,
- community::{Community, CommunityForm, CommunityModerator, CommunityModeratorForm},
+ community::{Community, CommunityModerator, CommunityModeratorForm},
naive_now,
- post::{Post, PostForm},
+ post::Post,
post_view::PostView,
- user::{UserForm, User_},
+ user::User_,
views::{community_view::CommunityView, user_view::UserViewSafe},
- Crud,
+ ApubObject,
Joinable,
SearchType,
};
response
}
SearchAcceptedObjects::Page(p) => {
- let post_form = PostForm::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)
) -> Result<User_, LemmyError> {
let apub_id_owned = apub_id.to_owned();
let user = blocking(context.pool(), move |conn| {
- User_::read_from_actor_id(conn, apub_id_owned.as_ref())
+ User_::read_from_apub_id(conn, apub_id_owned.as_ref())
})
.await?;
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)
}
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)
}
) -> Result<Community, LemmyError> {
let apub_id_owned = apub_id.to_owned();
let community = blocking(context.pool(), move |conn| {
- Community::read_from_actor_id(conn, apub_id_owned.as_str())
+ Community::read_from_apub_id(conn, apub_id_owned.as_str())
})
.await?;
}
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!())?;
}
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
}
Ok(p) => Ok(p),
Err(NotFound {}) => {
debug!("Fetching and creating remote post: {}", post_ap_id);
- let post =
+ let page =
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)
}
);
let comment =
fetch_remote_object::<NoteExt>(context.client(), comment_ap_id, recursion_counter).await?;
- let comment_form = CommentForm::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()),