2022-05-30 22:23:06 +00:00
|
|
|
use tokio_postgres::GenericClient;
|
|
|
|
|
|
|
|
use crate::activitypub::{
|
|
|
|
activity::Activity,
|
|
|
|
fetcher::helpers::{get_or_import_profile_by_actor_id, import_post},
|
2022-07-16 01:49:27 +00:00
|
|
|
identifiers::parse_local_object_id,
|
|
|
|
receiver::find_object_id,
|
2022-09-17 17:29:31 +00:00
|
|
|
vocabulary::{CREATE, LIKE, NOTE, UPDATE},
|
2022-05-30 22:23:06 +00:00
|
|
|
};
|
|
|
|
use crate::config::Config;
|
|
|
|
use crate::errors::DatabaseError;
|
|
|
|
use crate::models::posts::queries::{create_post, get_post_by_object_id};
|
|
|
|
use crate::models::posts::types::PostCreateData;
|
|
|
|
use super::HandlerResult;
|
|
|
|
|
|
|
|
pub async fn handle_announce(
|
|
|
|
config: &Config,
|
|
|
|
db_client: &mut impl GenericClient,
|
|
|
|
activity: Activity,
|
|
|
|
) -> HandlerResult {
|
|
|
|
let repost_object_id = activity.id;
|
|
|
|
match get_post_by_object_id(db_client, &repost_object_id).await {
|
|
|
|
Ok(_) => return Ok(None), // Ignore if repost already exists
|
|
|
|
Err(DatabaseError::NotFound(_)) => (),
|
|
|
|
Err(other_error) => return Err(other_error.into()),
|
|
|
|
};
|
|
|
|
let author = get_or_import_profile_by_actor_id(
|
|
|
|
db_client,
|
|
|
|
&config.instance(),
|
|
|
|
&config.media_dir(),
|
|
|
|
&activity.actor,
|
|
|
|
).await?;
|
2022-09-17 17:29:31 +00:00
|
|
|
if let Some(CREATE) | Some(LIKE) | Some(UPDATE) = activity.object["type"].as_str() {
|
2022-09-13 13:38:02 +00:00
|
|
|
// Ignore Announce(Create) activities from Lemmy
|
|
|
|
return Ok(None);
|
|
|
|
};
|
2022-07-15 17:31:02 +00:00
|
|
|
let object_id = find_object_id(&activity.object)?;
|
2022-07-16 01:49:27 +00:00
|
|
|
let post_id = match parse_local_object_id(&config.instance_url(), &object_id) {
|
2022-05-30 22:23:06 +00:00
|
|
|
Ok(post_id) => post_id,
|
|
|
|
Err(_) => {
|
|
|
|
// Try to get remote post
|
|
|
|
let post = import_post(config, db_client, object_id, None).await?;
|
|
|
|
post.id
|
|
|
|
},
|
|
|
|
};
|
2022-10-04 23:18:01 +00:00
|
|
|
let repost_data = PostCreateData::repost(post_id, Some(repost_object_id));
|
2022-05-30 22:23:06 +00:00
|
|
|
create_post(db_client, &author.id, repost_data).await?;
|
|
|
|
Ok(Some(NOTE))
|
|
|
|
}
|