- async fn from_apub(
- page: Page,
- context: &LemmyContext,
- request_counter: &mut i32,
- ) -> Result<ApubPost, LemmyError> {
- let creator = page
- .attributed_to
- .dereference(context, context.client(), request_counter)
- .await?;
- let community = page.extract_community(context, request_counter).await?;
-
- let url = if let Some(attachment) = page.attachment.first() {
- Some(attachment.href.clone())
- } else {
- page.url
- };
- let thumbnail_url: Option<Url> = page.image.map(|i| i.url);
- let (metadata_res, pictrs_thumbnail) = if let Some(url) = &url {
- fetch_site_data(context.client(), &context.settings(), Some(url)).await
- } else {
- (None, thumbnail_url)
- };
- let (embed_title, embed_description, embed_html) = metadata_res
- .map(|u| (u.title, u.description, u.html))
- .unwrap_or((None, None, None));
- let body_slurs_removed = read_from_string_or_source_opt(&page.content, &page.source)
- .map(|s| remove_slurs(&s, &context.settings().slur_regex()));
-
- let form = PostForm {
- name: page.name.clone(),
- url: url.map(Into::into),
- body: body_slurs_removed,
- creator_id: creator.id,
- community_id: community.id,
- removed: None,
- locked: page.comments_enabled.map(|e| !e),
- published: page.published.map(|u| u.naive_local()),
- updated: page.updated.map(|u| u.naive_local()),
- deleted: None,
- nsfw: page.sensitive,
- stickied: page.stickied,
- embed_title,
- embed_description,
- embed_html,
- thumbnail_url: pictrs_thumbnail.map(|u| u.into()),
- ap_id: Some(page.id.clone().into()),
- local: Some(false),
- };
+ async fn from_json(page: Page, context: &Data<Self::DataType>) -> Result<ApubPost, LemmyError> {
+ let creator = page.creator()?.dereference(context).await?;
+ let community = page.community(context).await?;
+ if community.posting_restricted_to_mods {
+ is_mod_or_admin(&mut context.pool(), creator.id, community.id).await?;
+ }
+ let mut name = page
+ .name
+ .clone()
+ .or_else(|| {
+ page
+ .content
+ .clone()
+ .as_ref()
+ .and_then(|c| parse_html(c).lines().next().map(ToString::to_string))
+ })
+ .ok_or_else(|| anyhow!("Object must have name or content"))?;
+ if name.chars().count() > MAX_TITLE_LENGTH {
+ name = name.chars().take(MAX_TITLE_LENGTH).collect();
+ }