2022-12-10 01:00:31 +00:00
|
|
|
use serde::Deserialize;
|
2022-12-09 21:16:53 +00:00
|
|
|
use serde_json::Value;
|
2022-05-30 23:08:49 +00:00
|
|
|
|
2023-04-25 13:49:35 +00:00
|
|
|
use fedimovies_config::Config;
|
|
|
|
use fedimovies_models::{
|
2023-03-30 20:27:17 +00:00
|
|
|
database::{DatabaseClient, DatabaseError},
|
2023-04-24 15:35:32 +00:00
|
|
|
posts::queries::{delete_post, get_post_by_remote_object_id},
|
|
|
|
profiles::queries::{get_profile_by_acct, get_profile_by_remote_actor_id},
|
|
|
|
reactions::queries::{delete_reaction, get_reaction_by_remote_activity_id},
|
|
|
|
relationships::queries::{get_follow_request_by_activity_id, unfollow},
|
2022-05-30 23:08:49 +00:00
|
|
|
};
|
2023-03-30 20:27:17 +00:00
|
|
|
|
|
|
|
use crate::activitypub::{
|
|
|
|
identifiers::parse_local_actor_id,
|
|
|
|
receiver::{deserialize_into_object_id, find_object_id},
|
|
|
|
vocabulary::{ANNOUNCE, FOLLOW, LIKE},
|
|
|
|
};
|
|
|
|
use crate::errors::ValidationError;
|
|
|
|
|
2022-05-30 23:08:49 +00:00
|
|
|
use super::HandlerResult;
|
2022-12-09 20:15:24 +00:00
|
|
|
|
2022-12-10 01:00:31 +00:00
|
|
|
#[derive(Deserialize)]
|
2022-12-13 01:12:42 +00:00
|
|
|
struct UndoFollow {
|
2022-12-10 01:00:31 +00:00
|
|
|
actor: String,
|
|
|
|
object: Value,
|
|
|
|
}
|
|
|
|
|
2022-12-09 20:15:24 +00:00
|
|
|
async fn handle_undo_follow(
|
|
|
|
config: &Config,
|
2023-01-17 23:14:18 +00:00
|
|
|
db_client: &mut impl DatabaseClient,
|
2022-12-13 01:12:42 +00:00
|
|
|
activity: Value,
|
2022-12-09 20:15:24 +00:00
|
|
|
) -> HandlerResult {
|
2022-12-13 01:12:42 +00:00
|
|
|
let activity: UndoFollow = serde_json::from_value(activity)
|
|
|
|
.map_err(|_| ValidationError("unexpected activity structure"))?;
|
2023-04-24 15:35:32 +00:00
|
|
|
let source_profile = get_profile_by_remote_actor_id(db_client, &activity.actor).await?;
|
2022-12-09 20:15:24 +00:00
|
|
|
let target_actor_id = find_object_id(&activity.object["object"])?;
|
2023-04-24 15:35:32 +00:00
|
|
|
let target_username = parse_local_actor_id(&config.instance_url(), &target_actor_id)?;
|
2022-12-09 20:15:24 +00:00
|
|
|
// acct equals username if profile is local
|
|
|
|
let target_profile = get_profile_by_acct(db_client, &target_username).await?;
|
|
|
|
match unfollow(db_client, &source_profile.id, &target_profile.id).await {
|
|
|
|
Ok(_) => (),
|
|
|
|
// Ignore Undo if relationship doesn't exist
|
|
|
|
Err(DatabaseError::NotFound(_)) => return Ok(None),
|
|
|
|
Err(other_error) => return Err(other_error.into()),
|
|
|
|
};
|
|
|
|
Ok(Some(FOLLOW))
|
|
|
|
}
|
2022-05-30 23:08:49 +00:00
|
|
|
|
2022-12-13 01:12:42 +00:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct Undo {
|
|
|
|
actor: String,
|
|
|
|
#[serde(deserialize_with = "deserialize_into_object_id")]
|
|
|
|
object: String,
|
|
|
|
}
|
|
|
|
|
2022-05-30 23:08:49 +00:00
|
|
|
pub async fn handle_undo(
|
2022-12-06 23:53:30 +00:00
|
|
|
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-05-30 23:08:49 +00:00
|
|
|
) -> HandlerResult {
|
2022-12-13 01:12:42 +00:00
|
|
|
if let Some(FOLLOW) = activity["object"]["type"].as_str() {
|
|
|
|
// Undo() with nested follow activity
|
|
|
|
return handle_undo_follow(config, db_client, activity).await;
|
2022-12-06 23:53:30 +00:00
|
|
|
};
|
|
|
|
|
2022-12-13 01:12:42 +00:00
|
|
|
let activity: Undo = serde_json::from_value(activity)
|
|
|
|
.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-12-13 01:12:42 +00:00
|
|
|
|
|
|
|
match get_follow_request_by_activity_id(db_client, &activity.object).await {
|
|
|
|
Ok(follow_request) => {
|
|
|
|
// Undo(Follow)
|
2023-04-11 21:30:34 +00:00
|
|
|
if follow_request.source_id != actor_profile.id {
|
|
|
|
return Err(ValidationError("actor is not a follower").into());
|
|
|
|
};
|
2022-12-13 01:12:42 +00:00
|
|
|
unfollow(
|
|
|
|
db_client,
|
|
|
|
&follow_request.source_id,
|
|
|
|
&follow_request.target_id,
|
2023-04-24 15:35:32 +00:00
|
|
|
)
|
|
|
|
.await?;
|
2022-12-13 01:12:42 +00:00
|
|
|
return Ok(Some(FOLLOW));
|
2023-04-24 15:35:32 +00:00
|
|
|
}
|
2022-12-13 01:12:42 +00:00
|
|
|
Err(DatabaseError::NotFound(_)) => (), // try other object types
|
|
|
|
Err(other_error) => return Err(other_error.into()),
|
|
|
|
};
|
|
|
|
|
|
|
|
match get_reaction_by_remote_activity_id(db_client, &activity.object).await {
|
2022-05-30 23:08:49 +00:00
|
|
|
Ok(reaction) => {
|
|
|
|
// Undo(Like)
|
|
|
|
if reaction.author_id != actor_profile.id {
|
|
|
|
return Err(ValidationError("actor is not an author").into());
|
|
|
|
};
|
2023-04-24 15:35:32 +00:00
|
|
|
delete_reaction(db_client, &reaction.author_id, &reaction.post_id).await?;
|
2022-05-30 23:08:49 +00:00
|
|
|
Ok(Some(LIKE))
|
2023-04-24 15:35:32 +00:00
|
|
|
}
|
2022-05-30 23:08:49 +00:00
|
|
|
Err(DatabaseError::NotFound(_)) => {
|
|
|
|
// Undo(Announce)
|
2023-04-24 15:35:32 +00:00
|
|
|
let post = match get_post_by_remote_object_id(db_client, &activity.object).await {
|
2022-05-30 23:08:49 +00:00
|
|
|
Ok(post) => post,
|
|
|
|
// Ignore undo if neither reaction nor repost is found
|
|
|
|
Err(DatabaseError::NotFound(_)) => return Ok(None),
|
|
|
|
Err(other_error) => return Err(other_error.into()),
|
|
|
|
};
|
|
|
|
if post.author.id != actor_profile.id {
|
|
|
|
return Err(ValidationError("actor is not an author").into());
|
|
|
|
};
|
|
|
|
match post.repost_of_id {
|
|
|
|
// Ignore returned data because reposts don't have attached files
|
|
|
|
Some(_) => delete_post(db_client, &post.id).await?,
|
|
|
|
// Can't undo regular post
|
|
|
|
None => return Err(ValidationError("object is not a repost").into()),
|
|
|
|
};
|
|
|
|
Ok(Some(ANNOUNCE))
|
2023-04-24 15:35:32 +00:00
|
|
|
}
|
2022-05-30 23:58:11 +00:00
|
|
|
Err(other_error) => Err(other_error.into()),
|
2022-05-30 23:08:49 +00:00
|
|
|
}
|
|
|
|
}
|