From f2a2bf2b2345b745e468c0b8f4beaa0427aa13ce Mon Sep 17 00:00:00 2001 From: Kitaiti Makoto Date: Sun, 24 Apr 2022 06:41:21 +0900 Subject: [PATCH] Implement FromId07 for Reshare --- plume-models/src/reshares.rs | 57 ++++++++++++++++++++++++++++++++++-- 1 file changed, 55 insertions(+), 2 deletions(-) diff --git a/plume-models/src/reshares.rs b/plume-models/src/reshares.rs index d21da516..e22fb7e8 100644 --- a/plume-models/src/reshares.rs +++ b/plume-models/src/reshares.rs @@ -3,11 +3,15 @@ use crate::{ timeline::*, users::User, Connection, Error, Result, CONFIG, }; use activitypub::activity::{Announce, Undo}; -use activitystreams::{activity::Announce as Announce07, iri_string::types::IriString, prelude::*}; +use activitystreams::{ + activity::{ActorAndObjectRef, Announce as Announce07}, + iri_string::types::IriString, + prelude::*, +}; use chrono::NaiveDateTime; use diesel::{self, ExpressionMethods, QueryDsl, RunQueryDsl}; use plume_common::activity_pub::{ - inbox::{AsActor, AsObject, AsObject07, FromId}, + inbox::{AsActor, AsObject, AsObject07, FromId, FromId07}, sign::Signer, Id, IntoId, PUBLIC_VISIBILITY, }; @@ -206,6 +210,55 @@ impl FromId for Reshare { } } +impl FromId07 for Reshare { + type Error = Error; + type Object = Announce07; + + fn from_db07(conn: &DbConn, id: &str) -> Result { + Reshare::find_by_ap_url(conn, id) + } + + fn from_activity07(conn: &DbConn, act: Announce07) -> Result { + let res = Reshare::insert( + conn, + NewReshare { + post_id: Post::from_id( + conn, + act.object_field_ref() + .as_single_id() + .ok_or(Error::MissingApProperty)? + .as_str(), + None, + CONFIG.proxy(), + ) + .map_err(|(_, e)| e)? + .id, + user_id: User::from_id( + conn, + act.actor_field_ref() + .as_single_id() + .ok_or(Error::MissingApProperty)? + .as_str(), + None, + CONFIG.proxy(), + ) + .map_err(|(_, e)| e)? + .id, + ap_url: act + .id_unchecked() + .ok_or(Error::MissingApProperty)? + .to_string(), + }, + )?; + res.notify(conn)?; + Ok(res) + } + + fn get_sender07() -> &'static dyn Signer { + Instance::get_local_instance_user().expect("Failed to local instance user") + } +} + impl AsObject for Reshare { type Error = Error; type Output = ();