2022-05-30 23:08:49 +00:00
|
|
|
use tokio_postgres::GenericClient;
|
|
|
|
|
|
|
|
use crate::activitypub::{
|
|
|
|
activity::Activity,
|
2022-07-15 17:31:02 +00:00
|
|
|
receiver::find_object_id,
|
2022-12-06 23:53:30 +00:00
|
|
|
vocabulary::{ANNOUNCE, FOLLOW, LIKE},
|
2022-05-30 23:08:49 +00:00
|
|
|
};
|
2022-12-06 23:53:30 +00:00
|
|
|
use crate::config::Config;
|
2022-12-03 22:09:42 +00:00
|
|
|
use crate::database::DatabaseError;
|
|
|
|
use crate::errors::ValidationError;
|
2022-05-30 23:08:49 +00:00
|
|
|
use crate::models::posts::queries::{
|
|
|
|
delete_post,
|
2022-10-15 12:32:27 +00:00
|
|
|
get_post_by_remote_object_id,
|
2022-05-30 23:08:49 +00:00
|
|
|
};
|
2022-10-15 12:32:27 +00:00
|
|
|
use crate::models::profiles::queries::get_profile_by_remote_actor_id;
|
2022-05-30 23:08:49 +00:00
|
|
|
use crate::models::reactions::queries::{
|
|
|
|
delete_reaction,
|
2022-10-15 12:32:27 +00:00
|
|
|
get_reaction_by_remote_activity_id,
|
2022-05-30 23:08:49 +00:00
|
|
|
};
|
|
|
|
use super::HandlerResult;
|
2022-12-06 23:53:30 +00:00
|
|
|
use super::undo_follow::handle_undo_follow;
|
2022-05-30 23:08:49 +00:00
|
|
|
|
|
|
|
pub async fn handle_undo(
|
2022-12-06 23:53:30 +00:00
|
|
|
config: &Config,
|
2022-05-30 23:08:49 +00:00
|
|
|
db_client: &mut impl GenericClient,
|
|
|
|
activity: Activity,
|
|
|
|
) -> HandlerResult {
|
2022-12-06 23:53:30 +00:00
|
|
|
if let Some(FOLLOW) = activity.object["type"].as_str() {
|
2022-12-08 22:47:56 +00:00
|
|
|
// Object type is currently required for processing Undo(Follow)
|
|
|
|
// because activity IDs of remote follow requests are not stored.
|
2022-12-06 23:53:30 +00:00
|
|
|
return handle_undo_follow(config, db_client, activity).await
|
|
|
|
};
|
|
|
|
|
2022-10-15 12:32:27 +00:00
|
|
|
let actor_profile = get_profile_by_remote_actor_id(
|
|
|
|
db_client,
|
|
|
|
&activity.actor,
|
|
|
|
).await?;
|
2022-07-15 17:31:02 +00:00
|
|
|
let object_id = find_object_id(&activity.object)?;
|
2022-10-15 12:32:27 +00:00
|
|
|
match get_reaction_by_remote_activity_id(db_client, &object_id).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());
|
|
|
|
};
|
|
|
|
delete_reaction(
|
|
|
|
db_client,
|
|
|
|
&reaction.author_id,
|
|
|
|
&reaction.post_id,
|
|
|
|
).await?;
|
|
|
|
Ok(Some(LIKE))
|
|
|
|
},
|
|
|
|
Err(DatabaseError::NotFound(_)) => {
|
|
|
|
// Undo(Announce)
|
2022-10-15 12:32:27 +00:00
|
|
|
let post = match get_post_by_remote_object_id(
|
|
|
|
db_client,
|
|
|
|
&object_id,
|
|
|
|
).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))
|
|
|
|
},
|
2022-05-30 23:58:11 +00:00
|
|
|
Err(other_error) => Err(other_error.into()),
|
2022-05-30 23:08:49 +00:00
|
|
|
}
|
|
|
|
}
|