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-04-25 13:49:35 +00:00
|
|
|
use fedimovies_config::Config;
|
|
|
|
use fedimovies_models::{
|
2023-04-24 15:35:32 +00:00
|
|
|
database::DatabaseClient, profiles::queries::get_profile_by_remote_actor_id,
|
|
|
|
relationships::queries::subscribe_opt, users::queries::get_user_by_name,
|
2023-03-30 20:27:17 +00:00
|
|
|
};
|
2023-02-18 23:52:48 +00:00
|
|
|
|
2022-10-23 22:18:01 +00:00
|
|
|
use super::{HandlerError, HandlerResult};
|
2023-04-24 15:35:32 +00:00
|
|
|
use crate::activitypub::{identifiers::parse_local_actor_id, vocabulary::PERSON};
|
|
|
|
use crate::errors::ValidationError;
|
2022-07-16 01:35:10 +00:00
|
|
|
|
2022-12-09 22:49:20 +00:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct Add {
|
|
|
|
actor: String,
|
|
|
|
object: String,
|
|
|
|
target: String,
|
|
|
|
}
|
|
|
|
|
2022-07-16 01:35:10 +00:00
|
|
|
pub async fn handle_add(
|
|
|
|
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 {
|
2023-04-26 10:55:42 +00:00
|
|
|
let activity: Add = serde_json::from_value(activity.clone())
|
|
|
|
.map_err(|_| ValidationError(format!("unexpected Add activity structure: {}", activity)))?;
|
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
|
|
|
// Adding to 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
|
|
|
subscribe_opt(db_client, &user.id, &actor_profile.id).await?;
|
|
|
|
return Ok(Some(PERSON));
|
|
|
|
};
|
|
|
|
Ok(None)
|
|
|
|
}
|