From 32cd91cfb97a662ebe25284456a59625b876f88d Mon Sep 17 00:00:00 2001 From: Kitaiti Makoto Date: Mon, 21 Mar 2022 09:52:22 +0900 Subject: [PATCH] Implement Mention::from_activity07() --- plume-models/src/mentions.rs | 43 ++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/plume-models/src/mentions.rs b/plume-models/src/mentions.rs index d6d027d9..0d2fd415 100644 --- a/plume-models/src/mentions.rs +++ b/plume-models/src/mentions.rs @@ -138,6 +138,49 @@ impl Mention { } } + pub fn from_activity07( + conn: &Connection, + ment: &link07::Mention, + inside: i32, + in_post: bool, + notify: bool, + ) -> Result { + let ap_url = ment.href().ok_or(Error::NotFound)?.as_str(); + let mentioned = User::find_by_ap_url(conn, ap_url)?; + + if in_post { + Post::get(conn, inside).and_then(|post| { + let res = Mention::insert( + conn, + NewMention { + mentioned_id: mentioned.id, + post_id: Some(post.id), + comment_id: None, + }, + )?; + if notify { + res.notify(conn)?; + } + Ok(res) + }) + } else { + Comment::get(conn, inside).and_then(|comment| { + let res = Mention::insert( + conn, + NewMention { + mentioned_id: mentioned.id, + post_id: None, + comment_id: Some(comment.id), + }, + )?; + if notify { + res.notify(conn)?; + } + Ok(res) + }) + } + } + pub fn delete(&self, conn: &Connection) -> Result<()> { //find related notifications and delete them if let Ok(n) = Notification::find(conn, notification_kind::MENTION, self.id) {