From 8f976be998694004d321c20abdaac544abd91618 Mon Sep 17 00:00:00 2001 From: Kitaiti Makoto Date: Sun, 1 May 2022 09:56:16 +0900 Subject: [PATCH] Implement AsObject07 for PostUpdate --- plume-models/src/posts.rs | 72 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/plume-models/src/posts.rs b/plume-models/src/posts.rs index e1dee790..375f3a7f 100644 --- a/plume-models/src/posts.rs +++ b/plume-models/src/posts.rs @@ -1432,6 +1432,78 @@ impl AsObject for PostUpdate { } } +impl AsObject07 for PostUpdate { + type Error = Error; + type Output = (); + + fn activity07(self, conn: &DbConn, actor: User, _id: &str) -> Result<()> { + let mut post = + Post::from_id(conn, &self.ap_url, None, CONFIG.proxy()).map_err(|(_, e)| e)?; + + if !post.is_author(conn, actor.id)? { + // TODO: maybe the author was added in the meantime + return Err(Error::Unauthorized); + } + + if let Some(title) = self.title { + post.slug = Post::slug(&title).to_string(); + post.title = title; + } + + if let Some(content) = self.content { + post.content = SafeString::new(&content); + } + + if let Some(subtitle) = self.subtitle { + post.subtitle = subtitle; + } + + post.cover_id = self.cover; + + if let Some(source) = self.source { + post.source = source; + } + + if let Some(license) = self.license { + post.license = license; + } + + let mut txt_hashtags = md_to_html(&post.source, None, false, None) + .2 + .into_iter() + .collect::>(); + if let Some(serde_json::Value::Array(mention_tags)) = self.tags { + let mut mentions = vec![]; + let mut tags = vec![]; + let mut hashtags = vec![]; + for tag in mention_tags { + serde_json::from_value::(tag.clone()) + .map(|m| mentions.push(m)) + .ok(); + + serde_json::from_value::(tag.clone()) + .map_err(Error::from) + .and_then(|t| { + let tag_name = t.name_string()?; + if txt_hashtags.remove(&tag_name) { + hashtags.push(t); + } else { + tags.push(t); + } + Ok(()) + }) + .ok(); + } + post.update_mentions(conn, mentions)?; + post.update_tags(conn, tags)?; + post.update_hashtags(conn, hashtags)?; + } + + post.update(conn)?; + Ok(()) + } +} + impl IntoId for Post { fn into_id(self) -> Id { Id::new(self.ap_url)