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

View file

@ -6,6 +6,7 @@ pub mod delete_note;
pub mod delete_person;
pub mod follow;
pub mod like_note;
pub mod remove_person;
pub mod undo_announce_note;
pub mod undo_follow;
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 QUESTION: &str = "Question";
pub const REJECT: &str = "Reject";
pub const REMOVE: &str = "Remove";
pub const UNDO: &str = "Undo";
pub const UPDATE: &str = "Update";

View file

@ -10,7 +10,10 @@ use web3::{
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::config::{BlockchainConfig, Instance};
use crate::database::{Pool, get_database_client};
@ -31,7 +34,10 @@ use crate::models::subscriptions::queries::{
get_expired_subscriptions,
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::signatures::{
encode_uint256,
@ -217,7 +223,15 @@ pub async fn check_subscriptions(
subscription.recipient_id,
);
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(
db_client,
&subscription.recipient_id,