2022-12-09 22:56:51 +00:00
|
|
|
use serde::Deserialize;
|
2022-12-09 21:16:53 +00:00
|
|
|
use serde_json::Value;
|
2022-05-30 22:29:09 +00:00
|
|
|
use tokio_postgres::GenericClient;
|
|
|
|
|
|
|
|
use crate::activitypub::{
|
|
|
|
fetcher::helpers::get_or_import_profile_by_actor_id,
|
2022-07-16 01:49:27 +00:00
|
|
|
identifiers::parse_local_object_id,
|
2022-12-09 22:56:51 +00:00
|
|
|
receiver::deserialize_into_object_id,
|
2022-05-30 22:29:09 +00:00
|
|
|
vocabulary::NOTE,
|
|
|
|
};
|
|
|
|
use crate::config::Config;
|
2022-12-03 22:09:42 +00:00
|
|
|
use crate::database::DatabaseError;
|
2022-12-09 21:16:53 +00:00
|
|
|
use crate::errors::ValidationError;
|
2022-05-30 22:29:09 +00:00
|
|
|
use crate::models::reactions::queries::create_reaction;
|
2022-10-15 12:32:27 +00:00
|
|
|
use crate::models::posts::queries::get_post_by_remote_object_id;
|
2022-05-30 22:29:09 +00:00
|
|
|
use super::HandlerResult;
|
|
|
|
|
2022-12-09 22:56:51 +00:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct Like {
|
|
|
|
id: String,
|
|
|
|
actor: String,
|
|
|
|
#[serde(deserialize_with = "deserialize_into_object_id")]
|
|
|
|
object: String,
|
|
|
|
}
|
|
|
|
|
2022-05-30 22:29:09 +00:00
|
|
|
pub async fn handle_like(
|
|
|
|
config: &Config,
|
|
|
|
db_client: &mut impl GenericClient,
|
2022-12-09 21:16:53 +00:00
|
|
|
activity: Value,
|
2022-05-30 22:29:09 +00:00
|
|
|
) -> HandlerResult {
|
2022-12-09 22:56:51 +00:00
|
|
|
let activity: Like = serde_json::from_value(activity)
|
2022-12-09 21:16:53 +00:00
|
|
|
.map_err(|_| ValidationError("unexpected activity structure"))?;
|
2022-05-30 22:29:09 +00:00
|
|
|
let author = get_or_import_profile_by_actor_id(
|
|
|
|
db_client,
|
|
|
|
&config.instance(),
|
|
|
|
&config.media_dir(),
|
|
|
|
&activity.actor,
|
|
|
|
).await?;
|
2022-10-15 12:32:27 +00:00
|
|
|
let post_id = match parse_local_object_id(
|
|
|
|
&config.instance_url(),
|
2022-12-09 22:56:51 +00:00
|
|
|
&activity.object,
|
2022-10-15 12:32:27 +00:00
|
|
|
) {
|
2022-05-30 22:29:09 +00:00
|
|
|
Ok(post_id) => post_id,
|
|
|
|
Err(_) => {
|
2022-10-15 12:32:27 +00:00
|
|
|
let post = match get_post_by_remote_object_id(
|
|
|
|
db_client,
|
2022-12-09 22:56:51 +00:00
|
|
|
&activity.object,
|
2022-10-15 12:32:27 +00:00
|
|
|
).await {
|
2022-05-30 22:29:09 +00:00
|
|
|
Ok(post) => post,
|
|
|
|
// Ignore like if post is not found locally
|
|
|
|
Err(DatabaseError::NotFound(_)) => return Ok(None),
|
|
|
|
Err(other_error) => return Err(other_error.into()),
|
|
|
|
};
|
|
|
|
post.id
|
|
|
|
},
|
|
|
|
};
|
|
|
|
match create_reaction(
|
|
|
|
db_client,
|
|
|
|
&author.id,
|
|
|
|
&post_id,
|
|
|
|
Some(&activity.id),
|
|
|
|
).await {
|
|
|
|
Ok(_) => (),
|
|
|
|
// Ignore activity if reaction is already saved
|
|
|
|
Err(DatabaseError::AlreadyExists(_)) => return Ok(None),
|
|
|
|
Err(other_error) => return Err(other_error.into()),
|
|
|
|
};
|
|
|
|
Ok(Some(NOTE))
|
|
|
|
}
|