Use Post::update_mentions07() instead of update_mentions()

This commit is contained in:
Kitaiti Makoto 2022-05-02 21:49:00 +09:00
parent c4bb1f771b
commit 39b49c707e
2 changed files with 13 additions and 50 deletions

View File

@ -5,7 +5,6 @@ use crate::{
}; };
use activitypub::{ use activitypub::{
activity::{Create, Delete, Update}, activity::{Create, Delete, Update},
link,
object::{Article, Image, Tombstone}, object::{Article, Image, Tombstone},
CustomObject, CustomObject,
}; };
@ -560,46 +559,6 @@ impl Post {
Ok(act) Ok(act)
} }
pub fn update_mentions(&self, conn: &Connection, mentions: Vec<link::Mention>) -> Result<()> {
let mentions = mentions
.into_iter()
.map(|m| {
(
m.link_props
.href_string()
.ok()
.and_then(|ap_url| User::find_by_ap_url(conn, &ap_url).ok())
.map(|u| u.id),
m,
)
})
.filter_map(|(id, m)| id.map(|id| (m, id)))
.collect::<Vec<_>>();
let old_mentions = Mention::list_for_post(conn, self.id)?;
let old_user_mentioned = old_mentions
.iter()
.map(|m| m.mentioned_id)
.collect::<HashSet<_>>();
for (m, id) in &mentions {
if !old_user_mentioned.contains(id) {
Mention::from_activity(&*conn, m, self.id, true, true)?;
}
}
let new_mentions = mentions
.into_iter()
.map(|(_m, id)| id)
.collect::<HashSet<_>>();
for m in old_mentions
.iter()
.filter(|m| !new_mentions.contains(&m.mentioned_id))
{
m.delete(conn)?;
}
Ok(())
}
pub fn update_mentions07( pub fn update_mentions07(
&self, &self,
conn: &Connection, conn: &Connection,
@ -1194,15 +1153,19 @@ impl AsObject<User, Update07, &DbConn> for PostUpdate {
let mut tags = vec![]; let mut tags = vec![];
let mut hashtags = vec![]; let mut hashtags = vec![];
for tag in mention_tags { for tag in mention_tags {
serde_json::from_value::<link::Mention>(tag.clone()) serde_json::from_value::<link07::Mention>(tag.clone())
.map(|m| mentions.push(m)) .map(|m| mentions.push(m))
.ok(); .ok();
serde_json::from_value::<Hashtag>(tag.clone()) serde_json::from_value::<Hashtag07>(tag.clone())
.map_err(Error::from) .map_err(Error::from)
.and_then(|t| { .and_then(|t| {
let tag_name = t.name_string()?; let tag_name = t.name.as_ref().ok_or(Error::MissingApProperty)?;
if txt_hashtags.remove(&tag_name) { let tag_name_str = tag_name
.as_xsd_string()
.or_else(|| tag_name.as_rdf_lang_string().map(|rls| &*rls.value))
.ok_or(Error::MissingApProperty)?;
if txt_hashtags.remove(tag_name_str) {
hashtags.push(t); hashtags.push(t);
} else { } else {
tags.push(t); tags.push(t);
@ -1211,9 +1174,9 @@ impl AsObject<User, Update07, &DbConn> for PostUpdate {
}) })
.ok(); .ok();
} }
post.update_mentions(conn, mentions)?; post.update_mentions07(conn, mentions)?;
post.update_tags(conn, tags)?; post.update_tags07(conn, tags)?;
post.update_hashtags(conn, hashtags)?; post.update_hashtags07(conn, hashtags)?;
} }
post.update(conn)?; post.update(conn)?;

View File

@ -306,11 +306,11 @@ pub fn update(
post.update(&conn).expect("post::update: update error"); post.update(&conn).expect("post::update: update error");
if post.published { if post.published {
post.update_mentions( post.update_mentions07(
&conn, &conn,
mentions mentions
.into_iter() .into_iter()
.filter_map(|m| Mention::build_activity(&conn, &m).ok()) .filter_map(|m| Mention::build_activity07(&conn, &m).ok())
.collect(), .collect(),
) )
.expect("post::update: mentions error"); .expect("post::update: mentions error");