2022-12-09 22:49:20 +00:00
|
|
|
use serde::Deserialize;
|
2022-12-09 21:16:53 +00:00
|
|
|
use serde_json::Value;
|
2022-07-16 01:35:10 +00:00
|
|
|
|
2023-02-18 23:52:48 +00:00
|
|
|
use mitra_config::Config;
|
2023-03-30 20:27:17 +00:00
|
|
|
use mitra_models::{
|
|
|
|
database::{DatabaseClient, DatabaseError},
|
2023-04-24 15:35:32 +00:00
|
|
|
notifications::queries::create_subscription_expiration_notification,
|
2023-01-19 20:34:46 +00:00
|
|
|
profiles::queries::get_profile_by_remote_actor_id,
|
|
|
|
relationships::queries::unsubscribe,
|
|
|
|
users::queries::get_user_by_name,
|
2022-07-16 01:35:10 +00:00
|
|
|
};
|
2023-03-30 20:27:17 +00:00
|
|
|
|
2023-04-24 15:35:32 +00:00
|
|
|
use crate::activitypub::{identifiers::parse_local_actor_id, vocabulary::PERSON};
|
2023-03-30 20:27:17 +00:00
|
|
|
use crate::errors::ValidationError;
|
|
|
|
|
2022-10-23 22:18:01 +00:00
|
|
|
use super::{HandlerError, HandlerResult};
|
2022-07-16 01:35:10 +00:00
|
|
|
|
2022-12-09 22:49:20 +00:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct Remove {
|
|
|
|
actor: String,
|
|
|
|
object: String,
|
|
|
|
target: String,
|
|
|
|
}
|
|
|
|
|
2022-07-16 01:35:10 +00:00
|
|
|
pub async fn handle_remove(
|
|
|
|
config: &Config,
|
2023-01-17 23:14:18 +00:00
|
|
|
db_client: &mut impl DatabaseClient,
|
2022-12-09 21:16:53 +00:00
|
|
|
activity: Value,
|
2022-07-16 01:35:10 +00:00
|
|
|
) -> HandlerResult {
|
2022-12-09 22:49:20 +00:00
|
|
|
let activity: Remove = serde_json::from_value(activity)
|
2022-12-09 21:16:53 +00:00
|
|
|
.map_err(|_| ValidationError("unexpected activity structure"))?;
|
2023-04-24 15:35:32 +00:00
|
|
|
let actor_profile = get_profile_by_remote_actor_id(db_client, &activity.actor).await?;
|
2022-10-23 22:18:01 +00:00
|
|
|
let actor = actor_profile.actor_json.ok_or(HandlerError::LocalObject)?;
|
2022-12-09 22:49:20 +00:00
|
|
|
if Some(activity.target) == actor.subscribers {
|
2022-08-03 16:01:08 +00:00
|
|
|
// Removing from subscribers
|
2023-04-24 15:35:32 +00:00
|
|
|
let username = parse_local_actor_id(&config.instance_url(), &activity.object)?;
|
2022-08-03 16:01:08 +00:00
|
|
|
let user = get_user_by_name(db_client, &username).await?;
|
2022-07-16 01:35:10 +00:00
|
|
|
// actor is recipient, user is sender
|
|
|
|
match unsubscribe(db_client, &user.id, &actor_profile.id).await {
|
|
|
|
Ok(_) => {
|
2023-04-24 15:35:32 +00:00
|
|
|
create_subscription_expiration_notification(db_client, &actor_profile.id, &user.id)
|
|
|
|
.await?;
|
2022-07-16 01:35:10 +00:00
|
|
|
return Ok(Some(PERSON));
|
2023-04-24 15:35:32 +00:00
|
|
|
}
|
2022-07-16 01:35:10 +00:00
|
|
|
// Ignore removal if relationship does not exist
|
|
|
|
Err(DatabaseError::NotFound(_)) => return Ok(None),
|
|
|
|
Err(other_error) => return Err(other_error.into()),
|
|
|
|
};
|
|
|
|
};
|
|
|
|
Ok(None)
|
|
|
|
}
|