Send Remove(Person) activity when subscription expires

This commit is contained in:
silverpill 2022-07-16 00:27:31 +00:00
parent 0f5a2edf7f
commit 0a8e06995b
5 changed files with 60 additions and 13 deletions

View file

@ -5,14 +5,14 @@ use crate::activitypub::{
constants::AP_CONTEXT, constants::AP_CONTEXT,
deliverer::OutgoingActivity, deliverer::OutgoingActivity,
identifiers::{local_actor_id, local_object_id, LocalActorCollection}, identifiers::{local_actor_id, local_object_id, LocalActorCollection},
vocabulary::ADD, vocabulary::{ADD, REMOVE},
}; };
use crate::config::Instance; use crate::config::Instance;
use crate::models::users::types::User; use crate::models::users::types::User;
use crate::utils::id::new_uuid; use crate::utils::id::new_uuid;
#[derive(Serialize)] #[derive(Serialize)]
pub struct AddPerson { pub struct AddOrRemovePerson {
#[serde(rename = "@context")] #[serde(rename = "@context")]
context: String, context: String,
@ -27,19 +27,21 @@ pub struct AddPerson {
to: Vec<String>, to: Vec<String>,
} }
fn build_add_person( fn build_update_collection(
instance_url: &str, instance_url: &str,
sender_username: &str, sender_username: &str,
person_id: &str, person_id: &str,
collection: LocalActorCollection, collection: LocalActorCollection,
) -> AddPerson { remove: bool,
) -> AddOrRemovePerson {
let actor_id = local_actor_id(instance_url, sender_username); let actor_id = local_actor_id(instance_url, sender_username);
let activity_id = local_object_id(instance_url, &new_uuid()); let activity_id = local_object_id(instance_url, &new_uuid());
let activity_type = if remove { REMOVE } else { ADD };
let collection_id = collection.of(&actor_id); let collection_id = collection.of(&actor_id);
AddPerson { AddOrRemovePerson {
context: AP_CONTEXT.to_string(), context: AP_CONTEXT.to_string(),
id: activity_id, id: activity_id,
activity_type: ADD.to_string(), activity_type: activity_type.to_string(),
actor: actor_id, actor: actor_id,
object: person_id.to_string(), object: person_id.to_string(),
target: collection_id, target: collection_id,
@ -47,17 +49,19 @@ fn build_add_person(
} }
} }
pub fn prepare_add_person( pub fn prepare_update_collection(
instance: &Instance, instance: &Instance,
sender: &User, sender: &User,
person: &Actor, person: &Actor,
collection: LocalActorCollection, collection: LocalActorCollection,
) -> OutgoingActivity<AddPerson> { remove: bool,
let activity = build_add_person( ) -> OutgoingActivity<AddOrRemovePerson> {
let activity = build_update_collection(
&instance.url(), &instance.url(),
&sender.profile.username, &sender.profile.username,
&person.id, &person.id,
collection, collection,
remove,
); );
let recipients = vec![person.clone()]; let recipients = vec![person.clone()];
OutgoingActivity { OutgoingActivity {
@ -68,6 +72,15 @@ pub fn prepare_add_person(
} }
} }
pub fn prepare_add_person(
instance: &Instance,
sender: &User,
person: &Actor,
collection: LocalActorCollection,
) -> OutgoingActivity<AddOrRemovePerson> {
prepare_update_collection(instance, sender, person, collection, false)
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
@ -79,11 +92,12 @@ mod tests {
let sender_username = "local"; let sender_username = "local";
let person_id = "https://test.remote/actor/test"; let person_id = "https://test.remote/actor/test";
let collection = LocalActorCollection::Subscribers; let collection = LocalActorCollection::Subscribers;
let activity = build_add_person( let activity = build_update_collection(
INSTANCE_URL, INSTANCE_URL,
sender_username, sender_username,
person_id, person_id,
collection, collection,
false,
); );
assert_eq!(activity.activity_type, "Add"); assert_eq!(activity.activity_type, "Add");

View file

@ -6,6 +6,7 @@ pub mod delete_note;
pub mod delete_person; pub mod delete_person;
pub mod follow; pub mod follow;
pub mod like_note; pub mod like_note;
pub mod remove_person;
pub mod undo_announce_note; pub mod undo_announce_note;
pub mod undo_follow; pub mod undo_follow;
pub mod undo_like_note; pub mod undo_like_note;

View file

@ -0,0 +1,17 @@
use crate::activitypub::{
actor::Actor,
deliverer::OutgoingActivity,
identifiers::LocalActorCollection,
};
use crate::config::Instance;
use crate::models::users::types::User;
use super::add_person::{prepare_update_collection, AddOrRemovePerson};
pub fn prepare_remove_person(
instance: &Instance,
sender: &User,
person: &Actor,
collection: LocalActorCollection,
) -> OutgoingActivity<AddOrRemovePerson> {
prepare_update_collection(instance, sender, person, collection, true)
}

View file

@ -11,6 +11,7 @@ pub const FOLLOW: &str = "Follow";
pub const LIKE: &str = "Like"; pub const LIKE: &str = "Like";
pub const QUESTION: &str = "Question"; pub const QUESTION: &str = "Question";
pub const REJECT: &str = "Reject"; pub const REJECT: &str = "Reject";
pub const REMOVE: &str = "Remove";
pub const UNDO: &str = "Undo"; pub const UNDO: &str = "Undo";
pub const UPDATE: &str = "Update"; pub const UPDATE: &str = "Update";

View file

@ -10,7 +10,10 @@ use web3::{
types::{BlockId, BlockNumber, FilterBuilder, U256}, types::{BlockId, BlockNumber, FilterBuilder, U256},
}; };
use crate::activitypub::builders::add_person::prepare_add_person; use crate::activitypub::builders::{
add_person::prepare_add_person,
remove_person::prepare_remove_person,
};
use crate::activitypub::identifiers::LocalActorCollection; use crate::activitypub::identifiers::LocalActorCollection;
use crate::config::{BlockchainConfig, Instance}; use crate::config::{BlockchainConfig, Instance};
use crate::database::{Pool, get_database_client}; use crate::database::{Pool, get_database_client};
@ -31,7 +34,10 @@ use crate::models::subscriptions::queries::{
get_expired_subscriptions, get_expired_subscriptions,
get_subscription_by_participants, get_subscription_by_participants,
}; };
use crate::models::users::queries::get_user_by_wallet_address; use crate::models::users::queries::{
get_user_by_id,
get_user_by_wallet_address,
};
use super::errors::EthereumError; use super::errors::EthereumError;
use super::signatures::{ use super::signatures::{
encode_uint256, encode_uint256,
@ -217,7 +223,15 @@ pub async fn check_subscriptions(
subscription.recipient_id, subscription.recipient_id,
); );
let sender = get_profile_by_id(db_client, &subscription.sender_id).await?; let sender = get_profile_by_id(db_client, &subscription.sender_id).await?;
if sender.is_local() { if let Some(ref remote_sender) = sender.actor_json {
let recipient = get_user_by_id(db_client, &subscription.recipient_id).await?;
prepare_remove_person(
instance,
&recipient,
remote_sender,
LocalActorCollection::Subscribers,
).spawn_deliver();
} else {
create_subscription_expiration_notification( create_subscription_expiration_notification(
db_client, db_client,
&subscription.recipient_id, &subscription.recipient_id,