style: clippy fixes and linter formatting
This commit is contained in:
@@ -1,25 +1,34 @@
|
||||
use activitypub_federation::{activity_sending::SendActivityTask, fetch::object_id::ObjectId, protocol::context::WithContext};
|
||||
use activitypub_federation::{
|
||||
activity_sending::SendActivityTask, fetch::object_id::ObjectId, protocol::context::WithContext,
|
||||
};
|
||||
use url::Url;
|
||||
|
||||
use crate::{
|
||||
activities::CreateActivity,
|
||||
actors::get_local_actor,
|
||||
federation::ApFederationConfig,
|
||||
};
|
||||
use crate::{activities::CreateActivity, actors::get_local_actor, federation::ApFederationConfig};
|
||||
|
||||
use super::{ActivityPubService, delivery::send_with_retry};
|
||||
|
||||
impl ActivityPubService {
|
||||
pub async fn backfill_outbox(&self, outbox_url: &str, actor_url: &str) -> anyhow::Result<()> {
|
||||
let client = reqwest::Client::builder()
|
||||
.timeout(std::time::Duration::from_secs(super::HTTP_FETCH_TIMEOUT_SECS))
|
||||
.timeout(std::time::Duration::from_secs(
|
||||
super::HTTP_FETCH_TIMEOUT_SECS,
|
||||
))
|
||||
.build()?;
|
||||
let data = self.federation_config.to_request_data();
|
||||
let actor = url::Url::parse(actor_url)?;
|
||||
let root: serde_json::Value = client.get(outbox_url).header("Accept", "application/activity+json").send().await?.json().await?;
|
||||
let root: serde_json::Value = client
|
||||
.get(outbox_url)
|
||||
.header("Accept", "application/activity+json")
|
||||
.send()
|
||||
.await?
|
||||
.json()
|
||||
.await?;
|
||||
let first = match root.get("first").and_then(|v| v.as_str()) {
|
||||
Some(url) => url.to_string(),
|
||||
None => { tracing::debug!(outbox = %outbox_url, "outbox has no first page"); return Ok(()); }
|
||||
None => {
|
||||
tracing::debug!(outbox = %outbox_url, "outbox has no first page");
|
||||
return Ok(());
|
||||
}
|
||||
};
|
||||
let mut current_url = first;
|
||||
let mut visited = std::collections::HashSet::new();
|
||||
@@ -28,16 +37,40 @@ impl ActivityPubService {
|
||||
tracing::warn!(url = %current_url, "backfill: loop detected, stopping");
|
||||
break;
|
||||
}
|
||||
let page: serde_json::Value = match client.get(¤t_url).header("Accept", "application/activity+json").send().await {
|
||||
Ok(resp) => match resp.json().await { Ok(v) => v, Err(e) => { tracing::error!(error = %e, "backfill: failed to parse page JSON"); break; } },
|
||||
Err(e) => { tracing::error!(error = %e, "backfill: HTTP request failed"); break; }
|
||||
let page: serde_json::Value = match client
|
||||
.get(¤t_url)
|
||||
.header("Accept", "application/activity+json")
|
||||
.send()
|
||||
.await
|
||||
{
|
||||
Ok(resp) => match resp.json().await {
|
||||
Ok(v) => v,
|
||||
Err(e) => {
|
||||
tracing::error!(error = %e, "backfill: failed to parse page JSON");
|
||||
break;
|
||||
}
|
||||
},
|
||||
Err(e) => {
|
||||
tracing::error!(error = %e, "backfill: HTTP request failed");
|
||||
break;
|
||||
}
|
||||
};
|
||||
if let Some(items) = page.get("orderedItems").and_then(|v| v.as_array()) {
|
||||
for item in items {
|
||||
let activity_type = item.get("type").and_then(|v| v.as_str()).unwrap_or("");
|
||||
if activity_type != "Create" && activity_type != "Add" { continue; }
|
||||
let Some(object) = item.get("object").filter(|o| o.is_object()).cloned() else { continue };
|
||||
let Some(ap_id) = object.get("id").and_then(|v| v.as_str()).and_then(|s| url::Url::parse(s).ok()) else { continue };
|
||||
if activity_type != "Create" && activity_type != "Add" {
|
||||
continue;
|
||||
}
|
||||
let Some(object) = item.get("object").filter(|o| o.is_object()).cloned() else {
|
||||
continue;
|
||||
};
|
||||
let Some(ap_id) = object
|
||||
.get("id")
|
||||
.and_then(|v| v.as_str())
|
||||
.and_then(|s| url::Url::parse(s).ok())
|
||||
else {
|
||||
continue;
|
||||
};
|
||||
if let Err(e) = data.object_handler.on_create(&ap_id, &actor, object).await {
|
||||
tracing::warn!(ap_id = %ap_id, error = %e, "backfill: failed to process item");
|
||||
}
|
||||
@@ -60,7 +93,16 @@ impl ActivityPubService {
|
||||
let max_attempts = self.delivery_max_attempts;
|
||||
let initial_delay = self.delivery_initial_delay_secs;
|
||||
tokio::spawn(async move {
|
||||
if let Err(e) = ActivityPubService::run_backfill(config, base_url, owner_user_id, follower_inbox_url, max_attempts, initial_delay).await {
|
||||
if let Err(e) = ActivityPubService::run_backfill(
|
||||
config,
|
||||
base_url,
|
||||
owner_user_id,
|
||||
follower_inbox_url,
|
||||
max_attempts,
|
||||
initial_delay,
|
||||
)
|
||||
.await
|
||||
{
|
||||
tracing::warn!(error = %e, "backfill: task failed");
|
||||
}
|
||||
});
|
||||
@@ -76,7 +118,9 @@ impl ActivityPubService {
|
||||
) -> anyhow::Result<()> {
|
||||
const BATCH_SIZE: usize = 20;
|
||||
let data = config.to_request_data();
|
||||
let local_actor = get_local_actor(owner_user_id, &data).await.map_err(|e| anyhow::anyhow!("{e}"))?;
|
||||
let local_actor = get_local_actor(owner_user_id, &data)
|
||||
.await
|
||||
.map_err(|e| anyhow::anyhow!("{e}"))?;
|
||||
let inbox = Url::parse(&follower_inbox_url)?;
|
||||
|
||||
// Cursor-based pagination via get_local_objects_page (newest-first).
|
||||
@@ -105,18 +149,27 @@ impl ActivityPubService {
|
||||
uuid::Uuid::new_v5(&uuid::Uuid::NAMESPACE_URL, ap_id.as_str().as_bytes())
|
||||
))?;
|
||||
let create = CreateActivity {
|
||||
id: create_id, kind: Default::default(),
|
||||
id: create_id,
|
||||
kind: Default::default(),
|
||||
actor: ObjectId::from(local_actor.ap_id.clone()),
|
||||
object: object_json.clone(), to: vec![], cc: vec![], bto: vec![], bcc: vec![],
|
||||
object: object_json.clone(),
|
||||
to: vec![],
|
||||
cc: vec![],
|
||||
bto: vec![],
|
||||
bcc: vec![],
|
||||
};
|
||||
let sends = SendActivityTask::prepare(
|
||||
&WithContext::new_default(create),
|
||||
&local_actor,
|
||||
vec![inbox.clone()],
|
||||
&data,
|
||||
).await?;
|
||||
)
|
||||
.await?;
|
||||
total += 1;
|
||||
if send_with_retry(sends, &data, max_attempts, initial_delay).await.is_empty() {
|
||||
if send_with_retry(sends, &data, max_attempts, initial_delay)
|
||||
.await
|
||||
.is_empty()
|
||||
{
|
||||
success_count += 1;
|
||||
} else {
|
||||
failure_count += 1;
|
||||
@@ -127,7 +180,10 @@ impl ActivityPubService {
|
||||
break;
|
||||
}
|
||||
|
||||
tokio::time::sleep(std::time::Duration::from_millis(super::BATCH_FETCH_SLEEP_MS)).await;
|
||||
tokio::time::sleep(std::time::Duration::from_millis(
|
||||
super::BATCH_FETCH_SLEEP_MS,
|
||||
))
|
||||
.await;
|
||||
}
|
||||
|
||||
tracing::info!(
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
use activitypub_federation::{fetch::object_id::ObjectId, protocol::context::WithContext, traits::Object};
|
||||
use activitypub_federation::{
|
||||
fetch::object_id::ObjectId, protocol::context::WithContext, traits::Object,
|
||||
};
|
||||
use url::Url;
|
||||
|
||||
use crate::{
|
||||
activities::{
|
||||
AddActivity, AnnounceActivity, CreateActivity, DeleteActivity,
|
||||
MoveActivity, UndoActivity, UpdateActivity,
|
||||
AddActivity, AnnounceActivity, CreateActivity, DeleteActivity, MoveActivity, UndoActivity,
|
||||
UpdateActivity,
|
||||
},
|
||||
actors::get_local_actor,
|
||||
urls::activity_url,
|
||||
@@ -22,10 +24,18 @@ impl ActivityPubService {
|
||||
let announce_id = url::Url::parse(&format!(
|
||||
"{}/activities/announce/{}",
|
||||
self.base_url,
|
||||
uuid::Uuid::new_v5(&uuid::Uuid::NAMESPACE_URL, format!("{}/{}", local_user_id, object_ap_id).as_bytes()),
|
||||
)).map_err(|e| anyhow::anyhow!("{e}"))?;
|
||||
uuid::Uuid::new_v5(
|
||||
&uuid::Uuid::NAMESPACE_URL,
|
||||
format!("{}/{}", local_user_id, object_ap_id).as_bytes()
|
||||
),
|
||||
))
|
||||
.map_err(|e| anyhow::anyhow!("{e}"))?;
|
||||
let data = self.federation_config.to_request_data();
|
||||
let Some((local_actor, inboxes)) = self.accepted_follower_inboxes(&data, local_user_id).await? else { return Ok(()); };
|
||||
let Some((local_actor, inboxes)) =
|
||||
self.accepted_follower_inboxes(&data, local_user_id).await?
|
||||
else {
|
||||
return Ok(());
|
||||
};
|
||||
let announce = AnnounceActivity {
|
||||
id: announce_id,
|
||||
kind: Default::default(),
|
||||
@@ -35,8 +45,11 @@ impl ActivityPubService {
|
||||
to: vec![crate::urls::AS_PUBLIC.to_string()],
|
||||
cc: vec![local_actor.followers_url.to_string()],
|
||||
};
|
||||
let (json, sends, inboxes) = self.prepare_broadcast(&data, &local_actor, inboxes, announce).await?;
|
||||
self.dispatch_deliveries(&data, &local_actor, inboxes, sends, json).await
|
||||
let (json, sends, inboxes) = self
|
||||
.prepare_broadcast(&data, &local_actor, inboxes, announce)
|
||||
.await?;
|
||||
self.dispatch_deliveries(&data, &local_actor, inboxes, sends, json)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn broadcast_undo_announce_to_followers(
|
||||
@@ -47,19 +60,30 @@ impl ActivityPubService {
|
||||
let announce_id = url::Url::parse(&format!(
|
||||
"{}/activities/announce/{}",
|
||||
self.base_url,
|
||||
uuid::Uuid::new_v5(&uuid::Uuid::NAMESPACE_URL, format!("{}/{}", local_user_id, object_ap_id).as_bytes()),
|
||||
)).map_err(|e| anyhow::anyhow!("{e}"))?;
|
||||
uuid::Uuid::new_v5(
|
||||
&uuid::Uuid::NAMESPACE_URL,
|
||||
format!("{}/{}", local_user_id, object_ap_id).as_bytes()
|
||||
),
|
||||
))
|
||||
.map_err(|e| anyhow::anyhow!("{e}"))?;
|
||||
let undo_id = activity_url(&self.base_url).map_err(|e| anyhow::anyhow!("{e}"))?;
|
||||
let data = self.federation_config.to_request_data();
|
||||
let Some((local_actor, inboxes)) = self.accepted_follower_inboxes(&data, local_user_id).await? else { return Ok(()); };
|
||||
let Some((local_actor, inboxes)) =
|
||||
self.accepted_follower_inboxes(&data, local_user_id).await?
|
||||
else {
|
||||
return Ok(());
|
||||
};
|
||||
let undo = UndoActivity {
|
||||
id: undo_id,
|
||||
kind: Default::default(),
|
||||
actor: ObjectId::from(local_actor.ap_id.clone()),
|
||||
object: serde_json::json!({"type":"Announce","id":announce_id.to_string(),"actor":local_actor.ap_id.to_string(),"object":object_ap_id.to_string()}),
|
||||
};
|
||||
let (json, sends, inboxes) = self.prepare_broadcast(&data, &local_actor, inboxes, undo).await?;
|
||||
self.dispatch_deliveries(&data, &local_actor, inboxes, sends, json).await
|
||||
let (json, sends, inboxes) = self
|
||||
.prepare_broadcast(&data, &local_actor, inboxes, undo)
|
||||
.await?;
|
||||
self.dispatch_deliveries(&data, &local_actor, inboxes, sends, json)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn broadcast_like_to_inbox(
|
||||
@@ -69,11 +93,16 @@ impl ActivityPubService {
|
||||
author_inbox_url: url::Url,
|
||||
) -> anyhow::Result<()> {
|
||||
let data = self.federation_config.to_request_data();
|
||||
let local_actor = get_local_actor(liker_user_id, &data).await.map_err(|e| anyhow::anyhow!("{e}"))?;
|
||||
let local_actor = get_local_actor(liker_user_id, &data)
|
||||
.await
|
||||
.map_err(|e| anyhow::anyhow!("{e}"))?;
|
||||
let like_id = url::Url::parse(&format!(
|
||||
"{}/activities/like/{}",
|
||||
self.base_url,
|
||||
uuid::Uuid::new_v5(&uuid::Uuid::NAMESPACE_URL, format!("{}/{}", liker_user_id, object_ap_id).as_bytes()),
|
||||
uuid::Uuid::new_v5(
|
||||
&uuid::Uuid::NAMESPACE_URL,
|
||||
format!("{}/{}", liker_user_id, object_ap_id).as_bytes()
|
||||
),
|
||||
))?;
|
||||
let like = crate::activities::LikeActivity {
|
||||
id: like_id,
|
||||
@@ -81,8 +110,11 @@ impl ActivityPubService {
|
||||
actor: ObjectId::from(local_actor.ap_id.clone()),
|
||||
object: object_ap_id,
|
||||
};
|
||||
let (json, sends, inboxes) = self.prepare_broadcast(&data, &local_actor, vec![author_inbox_url], like).await?;
|
||||
self.dispatch_deliveries(&data, &local_actor, inboxes, sends, json).await
|
||||
let (json, sends, inboxes) = self
|
||||
.prepare_broadcast(&data, &local_actor, vec![author_inbox_url], like)
|
||||
.await?;
|
||||
self.dispatch_deliveries(&data, &local_actor, inboxes, sends, json)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn broadcast_undo_like_to_inbox(
|
||||
@@ -92,11 +124,16 @@ impl ActivityPubService {
|
||||
author_inbox_url: url::Url,
|
||||
) -> anyhow::Result<()> {
|
||||
let data = self.federation_config.to_request_data();
|
||||
let local_actor = get_local_actor(liker_user_id, &data).await.map_err(|e| anyhow::anyhow!("{e}"))?;
|
||||
let local_actor = get_local_actor(liker_user_id, &data)
|
||||
.await
|
||||
.map_err(|e| anyhow::anyhow!("{e}"))?;
|
||||
let like_id = url::Url::parse(&format!(
|
||||
"{}/activities/like/{}",
|
||||
self.base_url,
|
||||
uuid::Uuid::new_v5(&uuid::Uuid::NAMESPACE_URL, format!("{}/{}", liker_user_id, object_ap_id).as_bytes()),
|
||||
uuid::Uuid::new_v5(
|
||||
&uuid::Uuid::NAMESPACE_URL,
|
||||
format!("{}/{}", liker_user_id, object_ap_id).as_bytes()
|
||||
),
|
||||
))?;
|
||||
let undo_id = activity_url(&self.base_url).map_err(|e| anyhow::anyhow!("{e}"))?;
|
||||
let undo = UndoActivity {
|
||||
@@ -105,8 +142,11 @@ impl ActivityPubService {
|
||||
actor: ObjectId::from(local_actor.ap_id.clone()),
|
||||
object: serde_json::json!({"type":"Like","id":like_id.to_string(),"actor":local_actor.ap_id.to_string(),"object":object_ap_id.to_string()}),
|
||||
};
|
||||
let (json, sends, inboxes) = self.prepare_broadcast(&data, &local_actor, vec![author_inbox_url], undo).await?;
|
||||
self.dispatch_deliveries(&data, &local_actor, inboxes, sends, json).await
|
||||
let (json, sends, inboxes) = self
|
||||
.prepare_broadcast(&data, &local_actor, vec![author_inbox_url], undo)
|
||||
.await?;
|
||||
self.dispatch_deliveries(&data, &local_actor, inboxes, sends, json)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn broadcast_delete_to_followers(
|
||||
@@ -115,7 +155,11 @@ impl ActivityPubService {
|
||||
ap_id: Url,
|
||||
) -> anyhow::Result<()> {
|
||||
let data = self.federation_config.to_request_data();
|
||||
let Some((local_actor, inboxes)) = self.accepted_follower_inboxes(&data, local_user_id).await? else { return Ok(()); };
|
||||
let Some((local_actor, inboxes)) =
|
||||
self.accepted_follower_inboxes(&data, local_user_id).await?
|
||||
else {
|
||||
return Ok(());
|
||||
};
|
||||
let delete = DeleteActivity {
|
||||
id: activity_url(&self.base_url).map_err(|e| anyhow::anyhow!("{e}"))?,
|
||||
kind: Default::default(),
|
||||
@@ -124,8 +168,11 @@ impl ActivityPubService {
|
||||
to: vec![crate::urls::AS_PUBLIC.to_string()],
|
||||
cc: vec![local_actor.followers_url.to_string()],
|
||||
};
|
||||
let (json, sends, inboxes) = self.prepare_broadcast(&data, &local_actor, inboxes, delete).await?;
|
||||
self.dispatch_deliveries(&data, &local_actor, inboxes, sends, json).await
|
||||
let (json, sends, inboxes) = self
|
||||
.prepare_broadcast(&data, &local_actor, inboxes, delete)
|
||||
.await?;
|
||||
self.dispatch_deliveries(&data, &local_actor, inboxes, sends, json)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn broadcast_add_to_followers(
|
||||
@@ -135,7 +182,11 @@ impl ActivityPubService {
|
||||
object: serde_json::Value,
|
||||
) -> anyhow::Result<()> {
|
||||
let data = self.federation_config.to_request_data();
|
||||
let Some((local_actor, inboxes)) = self.accepted_follower_inboxes(&data, local_user_id).await? else { return Ok(()); };
|
||||
let Some((local_actor, inboxes)) =
|
||||
self.accepted_follower_inboxes(&data, local_user_id).await?
|
||||
else {
|
||||
return Ok(());
|
||||
};
|
||||
let add = AddActivity {
|
||||
id: ap_id,
|
||||
kind: Default::default(),
|
||||
@@ -144,8 +195,11 @@ impl ActivityPubService {
|
||||
to: vec![crate::urls::AS_PUBLIC.to_string()],
|
||||
cc: vec![local_actor.followers_url.to_string()],
|
||||
};
|
||||
let (json, sends, inboxes) = self.prepare_broadcast(&data, &local_actor, inboxes, add).await?;
|
||||
self.dispatch_deliveries(&data, &local_actor, inboxes, sends, json).await
|
||||
let (json, sends, inboxes) = self
|
||||
.prepare_broadcast(&data, &local_actor, inboxes, add)
|
||||
.await?;
|
||||
self.dispatch_deliveries(&data, &local_actor, inboxes, sends, json)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn broadcast_undo_add_to_followers(
|
||||
@@ -154,15 +208,22 @@ impl ActivityPubService {
|
||||
watchlist_entry_ap_id: Url,
|
||||
) -> anyhow::Result<()> {
|
||||
let data = self.federation_config.to_request_data();
|
||||
let Some((local_actor, inboxes)) = self.accepted_follower_inboxes(&data, local_user_id).await? else { return Ok(()); };
|
||||
let Some((local_actor, inboxes)) =
|
||||
self.accepted_follower_inboxes(&data, local_user_id).await?
|
||||
else {
|
||||
return Ok(());
|
||||
};
|
||||
let undo = UndoActivity {
|
||||
id: activity_url(&self.base_url).map_err(|e| anyhow::anyhow!("{e}"))?,
|
||||
kind: Default::default(),
|
||||
actor: ObjectId::from(local_actor.ap_id.clone()),
|
||||
object: serde_json::json!({"type":"Add","id":watchlist_entry_ap_id.as_str(),"object":{"id":watchlist_entry_ap_id.as_str()}}),
|
||||
};
|
||||
let (json, sends, inboxes) = self.prepare_broadcast(&data, &local_actor, inboxes, undo).await?;
|
||||
self.dispatch_deliveries(&data, &local_actor, inboxes, sends, json).await
|
||||
let (json, sends, inboxes) = self
|
||||
.prepare_broadcast(&data, &local_actor, inboxes, undo)
|
||||
.await?;
|
||||
self.dispatch_deliveries(&data, &local_actor, inboxes, sends, json)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn broadcast_create_note(
|
||||
@@ -175,13 +236,18 @@ impl ActivityPubService {
|
||||
return Ok(());
|
||||
}
|
||||
let data = self.federation_config.to_request_data();
|
||||
let Some((local_actor, inboxes)) = self.accepted_follower_inboxes(&data, local_user_id).await? else { return Ok(()); };
|
||||
let Some((local_actor, inboxes)) =
|
||||
self.accepted_follower_inboxes(&data, local_user_id).await?
|
||||
else {
|
||||
return Ok(());
|
||||
};
|
||||
let note_id_str = note["id"].as_str().unwrap_or("");
|
||||
let create_id = Url::parse(&format!(
|
||||
"{}/activities/create/{}",
|
||||
self.base_url,
|
||||
uuid::Uuid::new_v5(&uuid::Uuid::NAMESPACE_URL, note_id_str.as_bytes())
|
||||
)).map_err(|e| anyhow::anyhow!("{e}"))?;
|
||||
))
|
||||
.map_err(|e| anyhow::anyhow!("{e}"))?;
|
||||
let (to, cc) = visibility_addressing(visibility, &local_actor.followers_url);
|
||||
let create = CreateActivity {
|
||||
id: create_id,
|
||||
@@ -193,8 +259,11 @@ impl ActivityPubService {
|
||||
bto: vec![],
|
||||
bcc: vec![],
|
||||
};
|
||||
let (json, sends, inboxes) = self.prepare_broadcast(&data, &local_actor, inboxes, create).await?;
|
||||
self.dispatch_deliveries(&data, &local_actor, inboxes, sends, json).await
|
||||
let (json, sends, inboxes) = self
|
||||
.prepare_broadcast(&data, &local_actor, inboxes, create)
|
||||
.await?;
|
||||
self.dispatch_deliveries(&data, &local_actor, inboxes, sends, json)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn broadcast_update_note(
|
||||
@@ -207,7 +276,11 @@ impl ActivityPubService {
|
||||
return Ok(());
|
||||
}
|
||||
let data = self.federation_config.to_request_data();
|
||||
let Some((local_actor, inboxes)) = self.accepted_follower_inboxes(&data, local_user_id).await? else { return Ok(()); };
|
||||
let Some((local_actor, inboxes)) =
|
||||
self.accepted_follower_inboxes(&data, local_user_id).await?
|
||||
else {
|
||||
return Ok(());
|
||||
};
|
||||
let (to, cc) = visibility_addressing(visibility, &local_actor.followers_url);
|
||||
let update = crate::activities::UpdateActivity {
|
||||
id: activity_url(&self.base_url).map_err(|e| anyhow::anyhow!("{e}"))?,
|
||||
@@ -217,16 +290,30 @@ impl ActivityPubService {
|
||||
to,
|
||||
cc,
|
||||
};
|
||||
let (json, sends, inboxes) = self.prepare_broadcast(&data, &local_actor, inboxes, update).await?;
|
||||
self.dispatch_deliveries(&data, &local_actor, inboxes, sends, json).await
|
||||
let (json, sends, inboxes) = self
|
||||
.prepare_broadcast(&data, &local_actor, inboxes, update)
|
||||
.await?;
|
||||
self.dispatch_deliveries(&data, &local_actor, inboxes, sends, json)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn broadcast_actor_update(&self, user_id: uuid::Uuid) -> anyhow::Result<()> {
|
||||
let data = self.federation_config.to_request_data();
|
||||
let local_actor = get_local_actor(user_id, &data).await.map_err(|e| anyhow::anyhow!("{e}"))?;
|
||||
let person = local_actor.clone().into_json(&data).await.map_err(|e| anyhow::anyhow!("{e}"))?;
|
||||
let person_json = serde_json::to_value(WithContext::new(person, crate::urls::actor_ap_context()))?;
|
||||
let update_id = Url::parse(&format!("{}/activities/update/{}", self.base_url, uuid::Uuid::new_v4()))?;
|
||||
let local_actor = get_local_actor(user_id, &data)
|
||||
.await
|
||||
.map_err(|e| anyhow::anyhow!("{e}"))?;
|
||||
let person = local_actor
|
||||
.clone()
|
||||
.into_json(&data)
|
||||
.await
|
||||
.map_err(|e| anyhow::anyhow!("{e}"))?;
|
||||
let person_json =
|
||||
serde_json::to_value(WithContext::new(person, crate::urls::actor_ap_context()))?;
|
||||
let update_id = Url::parse(&format!(
|
||||
"{}/activities/update/{}",
|
||||
self.base_url,
|
||||
uuid::Uuid::new_v4()
|
||||
))?;
|
||||
let update = UpdateActivity {
|
||||
id: update_id,
|
||||
kind: Default::default(),
|
||||
@@ -240,8 +327,11 @@ impl ActivityPubService {
|
||||
return Ok(());
|
||||
};
|
||||
tracing::info!(%user_id, inbox_count = inboxes.len(), "broadcasting actor update");
|
||||
let (json, sends, inboxes) = self.prepare_broadcast(&data, &local_actor, inboxes, update).await?;
|
||||
self.dispatch_deliveries(&data, &local_actor, inboxes, sends, json).await
|
||||
let (json, sends, inboxes) = self
|
||||
.prepare_broadcast(&data, &local_actor, inboxes, update)
|
||||
.await?;
|
||||
self.dispatch_deliveries(&data, &local_actor, inboxes, sends, json)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn broadcast_move(
|
||||
@@ -250,7 +340,9 @@ impl ActivityPubService {
|
||||
new_actor_url: url::Url,
|
||||
) -> anyhow::Result<()> {
|
||||
let data = self.federation_config.to_request_data();
|
||||
let local_actor = get_local_actor(user_id, &data).await.map_err(|e| anyhow::anyhow!("{e}"))?;
|
||||
let local_actor = get_local_actor(user_id, &data)
|
||||
.await
|
||||
.map_err(|e| anyhow::anyhow!("{e}"))?;
|
||||
let Some((_, inboxes)) = self.accepted_follower_inboxes(&data, user_id).await? else {
|
||||
tracing::info!(%user_id, "broadcast_move: no accepted followers");
|
||||
return Ok(());
|
||||
@@ -262,8 +354,11 @@ impl ActivityPubService {
|
||||
object: local_actor.ap_id.clone(),
|
||||
target: new_actor_url.clone(),
|
||||
};
|
||||
let (json, sends, inboxes) = self.prepare_broadcast(&data, &local_actor, inboxes, move_activity).await?;
|
||||
self.dispatch_deliveries(&data, &local_actor, inboxes, sends, json).await?;
|
||||
let (json, sends, inboxes) = self
|
||||
.prepare_broadcast(&data, &local_actor, inboxes, move_activity)
|
||||
.await?;
|
||||
self.dispatch_deliveries(&data, &local_actor, inboxes, sends, json)
|
||||
.await?;
|
||||
tracing::info!(%user_id, target = %new_actor_url, "broadcast_move: dispatched");
|
||||
Ok(())
|
||||
}
|
||||
@@ -280,10 +375,7 @@ pub(super) fn visibility_addressing(
|
||||
vec![crate::urls::AS_PUBLIC.to_string()],
|
||||
vec![followers_url.to_string()],
|
||||
),
|
||||
ApVisibility::FollowersOnly => (
|
||||
vec![followers_url.to_string()],
|
||||
vec![],
|
||||
),
|
||||
ApVisibility::FollowersOnly => (vec![followers_url.to_string()], vec![]),
|
||||
ApVisibility::Private => (vec![], vec![]),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -57,13 +57,23 @@ impl Activity for RawActivity {
|
||||
type DataType = FederationData;
|
||||
type Error = Error;
|
||||
|
||||
fn id(&self) -> &Url { &self.id }
|
||||
fn actor(&self) -> &Url { &self.actor_url }
|
||||
fn id(&self) -> &Url {
|
||||
&self.id
|
||||
}
|
||||
fn actor(&self) -> &Url {
|
||||
&self.actor_url
|
||||
}
|
||||
|
||||
async fn verify(&self, _data: &activitypub_federation::config::Data<Self::DataType>) -> Result<(), Self::Error> {
|
||||
async fn verify(
|
||||
&self,
|
||||
_data: &activitypub_federation::config::Data<Self::DataType>,
|
||||
) -> Result<(), Self::Error> {
|
||||
Ok(())
|
||||
}
|
||||
async fn receive(self, _data: &activitypub_federation::config::Data<Self::DataType>) -> Result<(), Self::Error> {
|
||||
async fn receive(
|
||||
self,
|
||||
_data: &activitypub_federation::config::Data<Self::DataType>,
|
||||
) -> Result<(), Self::Error> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -96,8 +106,7 @@ impl ActivityPubService {
|
||||
let max_attempts = self.delivery_max_attempts;
|
||||
let initial_delay = self.delivery_initial_delay_secs;
|
||||
tokio::spawn(async move {
|
||||
let failures =
|
||||
send_with_retry(sends, &data, max_attempts, initial_delay).await;
|
||||
let failures = send_with_retry(sends, &data, max_attempts, initial_delay).await;
|
||||
if !failures.is_empty() {
|
||||
tracing::warn!(count = failures.len(), "some deliveries failed permanently");
|
||||
}
|
||||
@@ -128,9 +137,12 @@ impl ActivityPubService {
|
||||
.and_then(|v| v.as_str())
|
||||
.and_then(|s| Url::parse(s).ok())
|
||||
.unwrap_or_else(|| actor.ap_id.clone());
|
||||
let raw = RawActivity { id, actor_url, value: activity.clone() };
|
||||
let sends =
|
||||
SendActivityTask::prepare(&raw, &actor, vec![inbox.clone()], &data).await?;
|
||||
let raw = RawActivity {
|
||||
id,
|
||||
actor_url,
|
||||
value: activity.clone(),
|
||||
};
|
||||
let sends = SendActivityTask::prepare(&raw, &actor, vec![inbox.clone()], &data).await?;
|
||||
let failures = send_with_retry(
|
||||
sends,
|
||||
&data,
|
||||
@@ -172,7 +184,8 @@ impl ActivityPubService {
|
||||
where
|
||||
A: Activity + Serialize + Debug + Send + Sync,
|
||||
{
|
||||
let with_ctx = activitypub_federation::protocol::context::WithContext::new_default(activity);
|
||||
let with_ctx =
|
||||
activitypub_federation::protocol::context::WithContext::new_default(activity);
|
||||
let activity_json = serde_json::to_value(&with_ctx)?;
|
||||
let sends =
|
||||
SendActivityTask::prepare(&with_ctx, local_actor, inboxes.clone(), data).await?;
|
||||
|
||||
@@ -20,112 +20,228 @@ impl ActivityPubService {
|
||||
return self.follow_local(local_user_id, parts[0], &data).await;
|
||||
}
|
||||
let remote_actor = self.webfinger_https(handle, &data).await?;
|
||||
let local_actor = get_local_actor(local_user_id, &data).await.map_err(|e| anyhow::anyhow!("{e}"))?;
|
||||
let local_actor = get_local_actor(local_user_id, &data)
|
||||
.await
|
||||
.map_err(|e| anyhow::anyhow!("{e}"))?;
|
||||
let follow_id = activity_url(&self.base_url).map_err(|e| anyhow::anyhow!("{e}"))?;
|
||||
let follow_id_str = follow_id.to_string();
|
||||
let remote = RemoteActor {
|
||||
url: remote_actor.ap_id.to_string(),
|
||||
handle: format!("{}@{}", remote_actor.username, remote_actor.ap_id.host_str().unwrap_or("")),
|
||||
handle: format!(
|
||||
"{}@{}",
|
||||
remote_actor.username,
|
||||
remote_actor.ap_id.host_str().unwrap_or("")
|
||||
),
|
||||
inbox_url: remote_actor.inbox_url.to_string(),
|
||||
shared_inbox_url: remote_actor.shared_inbox_url.as_ref().map(|u| u.to_string()),
|
||||
shared_inbox_url: remote_actor
|
||||
.shared_inbox_url
|
||||
.as_ref()
|
||||
.map(|u| u.to_string()),
|
||||
display_name: Some(remote_actor.username.clone()),
|
||||
avatar_url: remote_actor.avatar_url.as_ref().map(|u| u.to_string()),
|
||||
outbox_url: Some(remote_actor.outbox_url.to_string()),
|
||||
};
|
||||
// Save BEFORE delivering — prevents lost state on process restart.
|
||||
data.follow_repo.add_following(local_user_id, remote, &follow_id_str).await?;
|
||||
data.follow_repo
|
||||
.add_following(local_user_id, remote, &follow_id_str)
|
||||
.await?;
|
||||
let follow = FollowActivity {
|
||||
id: Url::parse(&follow_id_str)?,
|
||||
kind: Default::default(),
|
||||
actor: ObjectId::from(local_actor.ap_id.clone()),
|
||||
object: ObjectId::from(remote_actor.ap_id.clone()),
|
||||
};
|
||||
let (json, sends, inboxes) = self.prepare_broadcast(&data, &local_actor, vec![remote_actor.inbox()], follow).await?;
|
||||
self.dispatch_deliveries(&data, &local_actor, inboxes, sends, json).await
|
||||
let (json, sends, inboxes) = self
|
||||
.prepare_broadcast(&data, &local_actor, vec![remote_actor.inbox()], follow)
|
||||
.await?;
|
||||
self.dispatch_deliveries(&data, &local_actor, inboxes, sends, json)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn unfollow(&self, local_user_id: uuid::Uuid, actor_url_str: &str) -> anyhow::Result<()> {
|
||||
pub async fn unfollow(
|
||||
&self,
|
||||
local_user_id: uuid::Uuid,
|
||||
actor_url_str: &str,
|
||||
) -> anyhow::Result<()> {
|
||||
let data = self.federation_config.to_request_data();
|
||||
if actor_url_str.starts_with(&self.base_url) {
|
||||
return self.unfollow_local(local_user_id, actor_url_str, &data).await;
|
||||
return self
|
||||
.unfollow_local(local_user_id, actor_url_str, &data)
|
||||
.await;
|
||||
}
|
||||
let remote = data.actor_repo.get_remote_actor(actor_url_str).await?
|
||||
let remote = data
|
||||
.actor_repo
|
||||
.get_remote_actor(actor_url_str)
|
||||
.await?
|
||||
.ok_or_else(|| anyhow::anyhow!("remote actor not found: {}", actor_url_str))?;
|
||||
let local_actor = get_local_actor(local_user_id, &data).await.map_err(|e| anyhow::anyhow!("{e}"))?;
|
||||
let local_actor = get_local_actor(local_user_id, &data)
|
||||
.await
|
||||
.map_err(|e| anyhow::anyhow!("{e}"))?;
|
||||
let remote_ap_id = Url::parse(actor_url_str)?;
|
||||
let inbox = Url::parse(&remote.inbox_url)?;
|
||||
let follow_id = data.follow_repo.get_follow_activity_id(local_user_id, actor_url_str).await?
|
||||
let follow_id = data
|
||||
.follow_repo
|
||||
.get_follow_activity_id(local_user_id, actor_url_str)
|
||||
.await?
|
||||
.and_then(|id| Url::parse(&id).ok())
|
||||
.unwrap_or_else(|| activity_url(&self.base_url).unwrap_or_else(|_| remote_ap_id.clone()));
|
||||
let follow = FollowActivity { id: follow_id, kind: Default::default(), actor: ObjectId::from(local_actor.ap_id.clone()), object: ObjectId::from(remote_ap_id) };
|
||||
.unwrap_or_else(|| {
|
||||
activity_url(&self.base_url).unwrap_or_else(|_| remote_ap_id.clone())
|
||||
});
|
||||
let follow = FollowActivity {
|
||||
id: follow_id,
|
||||
kind: Default::default(),
|
||||
actor: ObjectId::from(local_actor.ap_id.clone()),
|
||||
object: ObjectId::from(remote_ap_id),
|
||||
};
|
||||
let undo = UndoActivity {
|
||||
id: activity_url(&self.base_url).map_err(|e| anyhow::anyhow!("{e}"))?,
|
||||
kind: Default::default(),
|
||||
actor: ObjectId::from(local_actor.ap_id.clone()),
|
||||
object: serde_json::to_value(&follow).map_err(|e| anyhow::anyhow!("{e}"))?,
|
||||
};
|
||||
let (json, sends, inboxes) = self.prepare_broadcast(&data, &local_actor, vec![inbox], undo).await?;
|
||||
self.dispatch_deliveries(&data, &local_actor, inboxes, sends, json).await?;
|
||||
data.follow_repo.remove_following(local_user_id, actor_url_str).await?;
|
||||
data.object_handler.on_actor_removed(&Url::parse(actor_url_str)?).await?;
|
||||
let (json, sends, inboxes) = self
|
||||
.prepare_broadcast(&data, &local_actor, vec![inbox], undo)
|
||||
.await?;
|
||||
self.dispatch_deliveries(&data, &local_actor, inboxes, sends, json)
|
||||
.await?;
|
||||
data.follow_repo
|
||||
.remove_following(local_user_id, actor_url_str)
|
||||
.await?;
|
||||
data.object_handler
|
||||
.on_actor_removed(&Url::parse(actor_url_str)?)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn accept_follower(&self, local_user_id: uuid::Uuid, remote_actor_url: &str) -> anyhow::Result<()> {
|
||||
pub async fn accept_follower(
|
||||
&self,
|
||||
local_user_id: uuid::Uuid,
|
||||
remote_actor_url: &str,
|
||||
) -> anyhow::Result<()> {
|
||||
let data = self.federation_config.to_request_data();
|
||||
let local_actor = get_local_actor(local_user_id, &data).await.map_err(|e| anyhow::anyhow!("{e}"))?;
|
||||
let remote_actor = data.actor_repo.get_remote_actor(remote_actor_url).await?
|
||||
let local_actor = get_local_actor(local_user_id, &data)
|
||||
.await
|
||||
.map_err(|e| anyhow::anyhow!("{e}"))?;
|
||||
let remote_actor = data
|
||||
.actor_repo
|
||||
.get_remote_actor(remote_actor_url)
|
||||
.await?
|
||||
.ok_or_else(|| anyhow::anyhow!("remote actor not found"))?;
|
||||
let follow_id_str = data.follow_repo.get_follower_follow_activity_id(local_user_id, remote_actor_url).await?
|
||||
.ok_or_else(|| anyhow::anyhow!("follow activity id not found for {}", remote_actor_url))?;
|
||||
let follow = FollowActivity { id: Url::parse(&follow_id_str)?, kind: Default::default(), actor: ObjectId::from(Url::parse(remote_actor_url)?), object: ObjectId::from(local_actor.ap_id.clone()) };
|
||||
let accept = AcceptActivity { id: activity_url(&self.base_url).map_err(|e| anyhow::anyhow!("{e}"))?, kind: Default::default(), actor: ObjectId::from(local_actor.ap_id.clone()), object: follow };
|
||||
data.follow_repo.update_follower_status(local_user_id, remote_actor_url, FollowerStatus::Accepted).await?;
|
||||
let follow_id_str = data
|
||||
.follow_repo
|
||||
.get_follower_follow_activity_id(local_user_id, remote_actor_url)
|
||||
.await?
|
||||
.ok_or_else(|| {
|
||||
anyhow::anyhow!("follow activity id not found for {}", remote_actor_url)
|
||||
})?;
|
||||
let follow = FollowActivity {
|
||||
id: Url::parse(&follow_id_str)?,
|
||||
kind: Default::default(),
|
||||
actor: ObjectId::from(Url::parse(remote_actor_url)?),
|
||||
object: ObjectId::from(local_actor.ap_id.clone()),
|
||||
};
|
||||
let accept = AcceptActivity {
|
||||
id: activity_url(&self.base_url).map_err(|e| anyhow::anyhow!("{e}"))?,
|
||||
kind: Default::default(),
|
||||
actor: ObjectId::from(local_actor.ap_id.clone()),
|
||||
object: follow,
|
||||
};
|
||||
data.follow_repo
|
||||
.update_follower_status(local_user_id, remote_actor_url, FollowerStatus::Accepted)
|
||||
.await?;
|
||||
let inbox = Url::parse(&remote_actor.inbox_url)?;
|
||||
let (json, sends, inboxes) = self.prepare_broadcast(&data, &local_actor, vec![inbox], accept).await?;
|
||||
self.dispatch_deliveries(&data, &local_actor, inboxes, sends, json).await?;
|
||||
let target_inbox = remote_actor.shared_inbox_url.clone().unwrap_or_else(|| remote_actor.inbox_url.clone());
|
||||
let (json, sends, inboxes) = self
|
||||
.prepare_broadcast(&data, &local_actor, vec![inbox], accept)
|
||||
.await?;
|
||||
self.dispatch_deliveries(&data, &local_actor, inboxes, sends, json)
|
||||
.await?;
|
||||
let target_inbox = remote_actor
|
||||
.shared_inbox_url
|
||||
.clone()
|
||||
.unwrap_or_else(|| remote_actor.inbox_url.clone());
|
||||
self.spawn_backfill(local_user_id, target_inbox);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn reject_follower(&self, local_user_id: uuid::Uuid, remote_actor_url: &str) -> anyhow::Result<()> {
|
||||
pub async fn reject_follower(
|
||||
&self,
|
||||
local_user_id: uuid::Uuid,
|
||||
remote_actor_url: &str,
|
||||
) -> anyhow::Result<()> {
|
||||
let data = self.federation_config.to_request_data();
|
||||
let local_actor = get_local_actor(local_user_id, &data).await.map_err(|e| anyhow::anyhow!("{e}"))?;
|
||||
let remote_actor = data.actor_repo.get_remote_actor(remote_actor_url).await?
|
||||
let local_actor = get_local_actor(local_user_id, &data)
|
||||
.await
|
||||
.map_err(|e| anyhow::anyhow!("{e}"))?;
|
||||
let remote_actor = data
|
||||
.actor_repo
|
||||
.get_remote_actor(remote_actor_url)
|
||||
.await?
|
||||
.ok_or_else(|| anyhow::anyhow!("remote actor not found"))?;
|
||||
let follow = FollowActivity { id: activity_url(&self.base_url).map_err(|e| anyhow::anyhow!("{e}"))?, kind: Default::default(), actor: ObjectId::from(Url::parse(remote_actor_url)?), object: ObjectId::from(local_actor.ap_id.clone()) };
|
||||
let reject = RejectActivity { id: activity_url(&self.base_url).map_err(|e| anyhow::anyhow!("{e}"))?, kind: Default::default(), actor: ObjectId::from(local_actor.ap_id.clone()), object: follow };
|
||||
let follow = FollowActivity {
|
||||
id: activity_url(&self.base_url).map_err(|e| anyhow::anyhow!("{e}"))?,
|
||||
kind: Default::default(),
|
||||
actor: ObjectId::from(Url::parse(remote_actor_url)?),
|
||||
object: ObjectId::from(local_actor.ap_id.clone()),
|
||||
};
|
||||
let reject = RejectActivity {
|
||||
id: activity_url(&self.base_url).map_err(|e| anyhow::anyhow!("{e}"))?,
|
||||
kind: Default::default(),
|
||||
actor: ObjectId::from(local_actor.ap_id.clone()),
|
||||
object: follow,
|
||||
};
|
||||
let inbox = Url::parse(&remote_actor.inbox_url)?;
|
||||
let (json, sends, inboxes) = self.prepare_broadcast(&data, &local_actor, vec![inbox], reject).await?;
|
||||
self.dispatch_deliveries(&data, &local_actor, inboxes, sends, json).await?;
|
||||
data.follow_repo.remove_follower(local_user_id, remote_actor_url).await?;
|
||||
let (json, sends, inboxes) = self
|
||||
.prepare_broadcast(&data, &local_actor, vec![inbox], reject)
|
||||
.await?;
|
||||
self.dispatch_deliveries(&data, &local_actor, inboxes, sends, json)
|
||||
.await?;
|
||||
data.follow_repo
|
||||
.remove_follower(local_user_id, remote_actor_url)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn get_pending_followers(&self, local_user_id: uuid::Uuid) -> anyhow::Result<Vec<RemoteActor>> {
|
||||
pub async fn get_pending_followers(
|
||||
&self,
|
||||
local_user_id: uuid::Uuid,
|
||||
) -> anyhow::Result<Vec<RemoteActor>> {
|
||||
let data = self.federation_config.to_request_data();
|
||||
data.follow_repo.get_pending_followers(local_user_id).await
|
||||
}
|
||||
|
||||
pub async fn get_accepted_followers(&self, local_user_id: uuid::Uuid) -> anyhow::Result<Vec<RemoteActor>> {
|
||||
pub async fn get_accepted_followers(
|
||||
&self,
|
||||
local_user_id: uuid::Uuid,
|
||||
) -> anyhow::Result<Vec<RemoteActor>> {
|
||||
let data = self.federation_config.to_request_data();
|
||||
Ok(data.follow_repo.get_followers(local_user_id).await?
|
||||
Ok(data
|
||||
.follow_repo
|
||||
.get_followers(local_user_id)
|
||||
.await?
|
||||
.into_iter()
|
||||
.filter(|f| f.status == FollowerStatus::Accepted)
|
||||
.map(|f| f.actor)
|
||||
.collect())
|
||||
}
|
||||
|
||||
pub async fn count_accepted_followers(&self, local_user_id: uuid::Uuid) -> anyhow::Result<usize> {
|
||||
pub async fn count_accepted_followers(
|
||||
&self,
|
||||
local_user_id: uuid::Uuid,
|
||||
) -> anyhow::Result<usize> {
|
||||
let data = self.federation_config.to_request_data();
|
||||
Ok(data.follow_repo.get_followers(local_user_id).await?
|
||||
Ok(data
|
||||
.follow_repo
|
||||
.get_followers(local_user_id)
|
||||
.await?
|
||||
.into_iter()
|
||||
.filter(|f| f.status == FollowerStatus::Accepted)
|
||||
.count())
|
||||
}
|
||||
|
||||
pub async fn get_following(&self, local_user_id: uuid::Uuid) -> anyhow::Result<Vec<RemoteActor>> {
|
||||
pub async fn get_following(
|
||||
&self,
|
||||
local_user_id: uuid::Uuid,
|
||||
) -> anyhow::Result<Vec<RemoteActor>> {
|
||||
let data = self.federation_config.to_request_data();
|
||||
data.follow_repo.get_following(local_user_id).await
|
||||
}
|
||||
@@ -135,17 +251,37 @@ impl ActivityPubService {
|
||||
data.follow_repo.count_following(local_user_id).await
|
||||
}
|
||||
|
||||
pub async fn remove_follower(&self, local_user_id: uuid::Uuid, actor_url: &str) -> anyhow::Result<()> {
|
||||
pub async fn remove_follower(
|
||||
&self,
|
||||
local_user_id: uuid::Uuid,
|
||||
actor_url: &str,
|
||||
) -> anyhow::Result<()> {
|
||||
let data = self.federation_config.to_request_data();
|
||||
data.follow_repo.remove_follower(local_user_id, actor_url).await
|
||||
data.follow_repo
|
||||
.remove_follower(local_user_id, actor_url)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn block_actor(&self, local_user_id: uuid::Uuid, actor_url: &str) -> anyhow::Result<()> {
|
||||
pub async fn block_actor(
|
||||
&self,
|
||||
local_user_id: uuid::Uuid,
|
||||
actor_url: &str,
|
||||
) -> anyhow::Result<()> {
|
||||
let data = self.federation_config.to_request_data();
|
||||
data.blocklist_repo.add_blocked_actor(local_user_id, actor_url).await?;
|
||||
let _ = data.follow_repo.remove_follower(local_user_id, actor_url).await;
|
||||
let _ = data.follow_repo.remove_following(local_user_id, actor_url).await;
|
||||
let local_actor = get_local_actor(local_user_id, &data).await.map_err(|e| anyhow::anyhow!("{e}"))?;
|
||||
data.blocklist_repo
|
||||
.add_blocked_actor(local_user_id, actor_url)
|
||||
.await?;
|
||||
let _ = data
|
||||
.follow_repo
|
||||
.remove_follower(local_user_id, actor_url)
|
||||
.await;
|
||||
let _ = data
|
||||
.follow_repo
|
||||
.remove_following(local_user_id, actor_url)
|
||||
.await;
|
||||
let local_actor = get_local_actor(local_user_id, &data)
|
||||
.await
|
||||
.map_err(|e| anyhow::anyhow!("{e}"))?;
|
||||
if let Ok(Some(remote_actor)) = data.actor_repo.get_remote_actor(actor_url).await {
|
||||
let block = crate::activities::BlockActivity {
|
||||
id: activity_url(&self.base_url).map_err(|e| anyhow::anyhow!("{e}"))?,
|
||||
@@ -154,25 +290,48 @@ impl ActivityPubService {
|
||||
object: Url::parse(actor_url)?,
|
||||
};
|
||||
let inbox = Url::parse(&remote_actor.inbox_url)?;
|
||||
let (json, sends, inboxes) = self.prepare_broadcast(&data, &local_actor, vec![inbox], block).await?;
|
||||
self.dispatch_deliveries(&data, &local_actor, inboxes, sends, json).await?;
|
||||
let (json, sends, inboxes) = self
|
||||
.prepare_broadcast(&data, &local_actor, vec![inbox], block)
|
||||
.await?;
|
||||
self.dispatch_deliveries(&data, &local_actor, inboxes, sends, json)
|
||||
.await?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn unblock_actor(&self, local_user_id: uuid::Uuid, actor_url: &str) -> anyhow::Result<()> {
|
||||
pub async fn unblock_actor(
|
||||
&self,
|
||||
local_user_id: uuid::Uuid,
|
||||
actor_url: &str,
|
||||
) -> anyhow::Result<()> {
|
||||
let data = self.federation_config.to_request_data();
|
||||
data.blocklist_repo.remove_blocked_actor(local_user_id, actor_url).await
|
||||
data.blocklist_repo
|
||||
.remove_blocked_actor(local_user_id, actor_url)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn get_blocked_actors(&self, local_user_id: uuid::Uuid) -> anyhow::Result<Vec<RemoteActor>> {
|
||||
pub async fn get_blocked_actors(
|
||||
&self,
|
||||
local_user_id: uuid::Uuid,
|
||||
) -> anyhow::Result<Vec<RemoteActor>> {
|
||||
let data = self.federation_config.to_request_data();
|
||||
let actor_urls = data.blocklist_repo.get_blocked_actors(local_user_id).await?;
|
||||
let actor_urls = data
|
||||
.blocklist_repo
|
||||
.get_blocked_actors(local_user_id)
|
||||
.await?;
|
||||
let mut actors = Vec::new();
|
||||
for url in actor_urls {
|
||||
let actor = match data.actor_repo.get_remote_actor(&url).await {
|
||||
Ok(Some(a)) => a,
|
||||
_ => RemoteActor { url: url.clone(), handle: url.clone(), inbox_url: url.clone(), shared_inbox_url: None, display_name: None, avatar_url: None, outbox_url: None },
|
||||
_ => RemoteActor {
|
||||
url: url.clone(),
|
||||
handle: url.clone(),
|
||||
inbox_url: url.clone(),
|
||||
shared_inbox_url: None,
|
||||
display_name: None,
|
||||
avatar_url: None,
|
||||
outbox_url: None,
|
||||
},
|
||||
};
|
||||
actors.push(actor);
|
||||
}
|
||||
@@ -185,15 +344,27 @@ impl ActivityPubService {
|
||||
target_username: &str,
|
||||
data: &activitypub_federation::config::Data<FederationData>,
|
||||
) -> anyhow::Result<()> {
|
||||
let target = data.user_repo.find_by_username(target_username).await?
|
||||
let target = data
|
||||
.user_repo
|
||||
.find_by_username(target_username)
|
||||
.await?
|
||||
.ok_or_else(|| anyhow::anyhow!("user not found: {}", target_username))?;
|
||||
if target.id == local_user_id {
|
||||
return Err(anyhow::anyhow!("cannot follow yourself"));
|
||||
}
|
||||
let follower_actor_url = crate::urls::actor_url(&self.base_url, local_user_id).to_string();
|
||||
let target_actor_url = crate::urls::actor_url(&self.base_url, target.id);
|
||||
let follow_id = activity_url(&self.base_url).map_err(|e| anyhow::anyhow!("{e}"))?.to_string();
|
||||
data.follow_repo.add_follower(target.id, &follower_actor_url, FollowerStatus::Accepted, &follow_id).await?;
|
||||
let follow_id = activity_url(&self.base_url)
|
||||
.map_err(|e| anyhow::anyhow!("{e}"))?
|
||||
.to_string();
|
||||
data.follow_repo
|
||||
.add_follower(
|
||||
target.id,
|
||||
&follower_actor_url,
|
||||
FollowerStatus::Accepted,
|
||||
&follow_id,
|
||||
)
|
||||
.await?;
|
||||
let target_as_remote = RemoteActor {
|
||||
url: target_actor_url.to_string(),
|
||||
handle: format!("{}@{}", target.username, data.domain),
|
||||
@@ -203,8 +374,16 @@ impl ActivityPubService {
|
||||
avatar_url: None,
|
||||
outbox_url: None,
|
||||
};
|
||||
data.follow_repo.add_following(local_user_id, target_as_remote, &follow_id).await?;
|
||||
data.follow_repo.update_following_status(local_user_id, target_actor_url.as_ref(), FollowingStatus::Accepted).await?;
|
||||
data.follow_repo
|
||||
.add_following(local_user_id, target_as_remote, &follow_id)
|
||||
.await?;
|
||||
data.follow_repo
|
||||
.update_following_status(
|
||||
local_user_id,
|
||||
target_actor_url.as_ref(),
|
||||
FollowingStatus::Accepted,
|
||||
)
|
||||
.await?;
|
||||
tracing::info!(follower = %local_user_id, followee = %target.id, "local follow");
|
||||
Ok(())
|
||||
}
|
||||
@@ -219,8 +398,12 @@ impl ActivityPubService {
|
||||
let target_user_id = crate::urls::extract_user_id_from_url(&target_url)
|
||||
.ok_or_else(|| anyhow::anyhow!("invalid local actor URL: {}", target_actor_url))?;
|
||||
let local_actor_url = crate::urls::actor_url(&self.base_url, local_user_id).to_string();
|
||||
data.follow_repo.remove_follower(target_user_id, &local_actor_url).await?;
|
||||
data.follow_repo.remove_following(local_user_id, target_actor_url).await?;
|
||||
data.follow_repo
|
||||
.remove_follower(target_user_id, &local_actor_url)
|
||||
.await?;
|
||||
data.follow_repo
|
||||
.remove_following(local_user_id, target_actor_url)
|
||||
.await?;
|
||||
tracing::info!(follower = %local_user_id, followee = %target_user_id, "local unfollow");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use activitypub_federation::{
|
||||
protocol::context::WithContext,
|
||||
traits::Object,
|
||||
};
|
||||
use activitypub_federation::{protocol::context::WithContext, traits::Object};
|
||||
use axum::{Router, extract::DefaultBodyLimit, routing::get, routing::post};
|
||||
use url::Url;
|
||||
|
||||
@@ -18,10 +15,8 @@ use crate::{
|
||||
nodeinfo::{nodeinfo_handler, nodeinfo_well_known_handler},
|
||||
outbox::outbox_handler,
|
||||
repository::{
|
||||
ActivityRepository, ActorRepository, BlockedDomain, BlocklistRepository,
|
||||
FollowRepository, FollowerStatus, FollowingStatus, RemoteActor,
|
||||
ActivityRepository, ActorRepository, BlockedDomain, BlocklistRepository, FollowRepository,
|
||||
},
|
||||
urls::activity_url,
|
||||
user::ApUserRepository,
|
||||
webfinger::webfinger_handler,
|
||||
};
|
||||
@@ -67,54 +62,91 @@ pub struct ActivityPubServiceBuilder {
|
||||
|
||||
impl ActivityPubServiceBuilder {
|
||||
pub fn activity_repo(mut self, v: Arc<dyn ActivityRepository>) -> Self {
|
||||
self.activity_repo = Some(v); self
|
||||
self.activity_repo = Some(v);
|
||||
self
|
||||
}
|
||||
pub fn follow_repo(mut self, v: Arc<dyn FollowRepository>) -> Self {
|
||||
self.follow_repo = Some(v); self
|
||||
self.follow_repo = Some(v);
|
||||
self
|
||||
}
|
||||
pub fn actor_repo(mut self, v: Arc<dyn ActorRepository>) -> Self {
|
||||
self.actor_repo = Some(v); self
|
||||
self.actor_repo = Some(v);
|
||||
self
|
||||
}
|
||||
pub fn blocklist_repo(mut self, v: Arc<dyn BlocklistRepository>) -> Self {
|
||||
self.blocklist_repo = Some(v); self
|
||||
self.blocklist_repo = Some(v);
|
||||
self
|
||||
}
|
||||
pub fn user_repo(mut self, v: Arc<dyn ApUserRepository>) -> Self {
|
||||
self.user_repo = Some(v); self
|
||||
self.user_repo = Some(v);
|
||||
self
|
||||
}
|
||||
pub fn content_reader(mut self, v: Arc<dyn ApContentReader>) -> Self {
|
||||
self.content_reader = Some(v); self
|
||||
self.content_reader = Some(v);
|
||||
self
|
||||
}
|
||||
pub fn object_handler(mut self, v: Arc<dyn ApObjectHandler>) -> Self {
|
||||
self.object_handler = Some(v); self
|
||||
self.object_handler = Some(v);
|
||||
self
|
||||
}
|
||||
pub fn allow_registration(mut self, v: bool) -> Self {
|
||||
self.allow_registration = v;
|
||||
self
|
||||
}
|
||||
pub fn software_name(mut self, v: impl Into<String>) -> Self {
|
||||
self.software_name = v.into();
|
||||
self
|
||||
}
|
||||
pub fn debug(mut self, v: bool) -> Self {
|
||||
self.debug = v;
|
||||
self
|
||||
}
|
||||
pub fn allow_registration(mut self, v: bool) -> Self { self.allow_registration = v; self }
|
||||
pub fn software_name(mut self, v: impl Into<String>) -> Self { self.software_name = v.into(); self }
|
||||
pub fn debug(mut self, v: bool) -> Self { self.debug = v; self }
|
||||
pub fn event_publisher(mut self, v: Arc<dyn crate::data::EventPublisher>) -> Self {
|
||||
self.event_publisher = Some(v); self
|
||||
self.event_publisher = Some(v);
|
||||
self
|
||||
}
|
||||
pub fn delivery_max_attempts(mut self, v: u32) -> Self {
|
||||
self.delivery_max_attempts = v;
|
||||
self
|
||||
}
|
||||
pub fn delivery_initial_delay_secs(mut self, v: u64) -> Self {
|
||||
self.delivery_initial_delay_secs = v;
|
||||
self
|
||||
}
|
||||
pub fn delivery_max_attempts(mut self, v: u32) -> Self { self.delivery_max_attempts = v; self }
|
||||
pub fn delivery_initial_delay_secs(mut self, v: u64) -> Self { self.delivery_initial_delay_secs = v; self }
|
||||
|
||||
pub async fn build(self) -> anyhow::Result<ActivityPubService> {
|
||||
let activity_repo = self.activity_repo
|
||||
let activity_repo = self
|
||||
.activity_repo
|
||||
.ok_or_else(|| anyhow::anyhow!("activity_repo required — call .activity_repo(arc)"))?;
|
||||
let follow_repo = self.follow_repo
|
||||
let follow_repo = self
|
||||
.follow_repo
|
||||
.ok_or_else(|| anyhow::anyhow!("follow_repo required — call .follow_repo(arc)"))?;
|
||||
let actor_repo = self.actor_repo
|
||||
let actor_repo = self
|
||||
.actor_repo
|
||||
.ok_or_else(|| anyhow::anyhow!("actor_repo required — call .actor_repo(arc)"))?;
|
||||
let blocklist_repo = self.blocklist_repo
|
||||
.ok_or_else(|| anyhow::anyhow!("blocklist_repo required — call .blocklist_repo(arc)"))?;
|
||||
let user_repo = self.user_repo
|
||||
let blocklist_repo = self.blocklist_repo.ok_or_else(|| {
|
||||
anyhow::anyhow!("blocklist_repo required — call .blocklist_repo(arc)")
|
||||
})?;
|
||||
let user_repo = self
|
||||
.user_repo
|
||||
.ok_or_else(|| anyhow::anyhow!("user_repo required — call .user_repo(arc)"))?;
|
||||
let content_reader = self.content_reader
|
||||
.ok_or_else(|| anyhow::anyhow!("content_reader required — call .content_reader(arc)"))?;
|
||||
let object_handler = self.object_handler
|
||||
.ok_or_else(|| anyhow::anyhow!("object_handler required — call .object_handler(arc)"))?;
|
||||
let content_reader = self.content_reader.ok_or_else(|| {
|
||||
anyhow::anyhow!("content_reader required — call .content_reader(arc)")
|
||||
})?;
|
||||
let object_handler = self.object_handler.ok_or_else(|| {
|
||||
anyhow::anyhow!("object_handler required — call .object_handler(arc)")
|
||||
})?;
|
||||
let data = FederationData::new(
|
||||
activity_repo, follow_repo, actor_repo, blocklist_repo,
|
||||
user_repo, content_reader, object_handler,
|
||||
self.base_url.clone(), self.allow_registration, self.software_name,
|
||||
activity_repo,
|
||||
follow_repo,
|
||||
actor_repo,
|
||||
blocklist_repo,
|
||||
user_repo,
|
||||
content_reader,
|
||||
object_handler,
|
||||
self.base_url.clone(),
|
||||
self.allow_registration,
|
||||
self.software_name,
|
||||
self.event_publisher,
|
||||
);
|
||||
let federation_config = ApFederationConfig::new(data, self.debug).await?;
|
||||
@@ -147,9 +179,15 @@ impl ActivityPubService {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn federation_config(&self) -> &ApFederationConfig { &self.federation_config }
|
||||
pub fn request_data(&self) -> activitypub_federation::config::Data<FederationData> { self.federation_config.to_request_data() }
|
||||
pub fn base_url(&self) -> &str { &self.base_url }
|
||||
pub fn federation_config(&self) -> &ApFederationConfig {
|
||||
&self.federation_config
|
||||
}
|
||||
pub fn request_data(&self) -> activitypub_federation::config::Data<FederationData> {
|
||||
self.federation_config.to_request_data()
|
||||
}
|
||||
pub fn base_url(&self) -> &str {
|
||||
&self.base_url
|
||||
}
|
||||
|
||||
/// Returns the ActivityPub router. Inbox routes enforce a 1 MB body limit.
|
||||
pub fn router<S>(&self) -> Router<S>
|
||||
@@ -160,9 +198,15 @@ impl ActivityPubService {
|
||||
.route("/.well-known/nodeinfo", get(nodeinfo_well_known_handler))
|
||||
.route("/nodeinfo/2.0", get(nodeinfo_handler))
|
||||
.route("/.well-known/webfinger", get(webfinger_handler))
|
||||
.route("/inbox", post(inbox_handler).layer(DefaultBodyLimit::max(1024 * 1024)))
|
||||
.route(
|
||||
"/inbox",
|
||||
post(inbox_handler).layer(DefaultBodyLimit::max(1024 * 1024)),
|
||||
)
|
||||
.route("/users/{id}", get(actor_handler))
|
||||
.route("/users/{id}/inbox", post(inbox_handler).layer(DefaultBodyLimit::max(1024 * 1024)))
|
||||
.route(
|
||||
"/users/{id}/inbox",
|
||||
post(inbox_handler).layer(DefaultBodyLimit::max(1024 * 1024)),
|
||||
)
|
||||
.route("/users/{id}/outbox", get(outbox_handler))
|
||||
.route("/users/{id}/followers", get(followers_handler))
|
||||
.route("/users/{id}/following", get(following_handler))
|
||||
@@ -172,12 +216,24 @@ impl ActivityPubService {
|
||||
pub async fn actor_json(&self, user_id_str: &str) -> anyhow::Result<String> {
|
||||
let uuid = uuid::Uuid::parse_str(user_id_str)?;
|
||||
let data = self.federation_config.to_request_data();
|
||||
let actor = get_local_actor(uuid, &data).await.map_err(|e| anyhow::anyhow!("{e}"))?;
|
||||
let person = actor.into_json(&data).await.map_err(|e| anyhow::anyhow!("{e}"))?;
|
||||
Ok(serde_json::to_string(&WithContext::new(person, crate::urls::actor_ap_context()))?)
|
||||
let actor = get_local_actor(uuid, &data)
|
||||
.await
|
||||
.map_err(|e| anyhow::anyhow!("{e}"))?;
|
||||
let person = actor
|
||||
.into_json(&data)
|
||||
.await
|
||||
.map_err(|e| anyhow::anyhow!("{e}"))?;
|
||||
Ok(serde_json::to_string(&WithContext::new(
|
||||
person,
|
||||
crate::urls::actor_ap_context(),
|
||||
))?)
|
||||
}
|
||||
|
||||
pub async fn followers_collection_json(&self, user_id: uuid::Uuid, page: Option<u32>) -> anyhow::Result<String> {
|
||||
pub async fn followers_collection_json(
|
||||
&self,
|
||||
user_id: uuid::Uuid,
|
||||
page: Option<u32>,
|
||||
) -> anyhow::Result<String> {
|
||||
const AP_CONTEXT: &str = "https://www.w3.org/ns/activitystreams";
|
||||
const PAGE_SIZE: usize = 20;
|
||||
let data = self.federation_config.to_request_data();
|
||||
@@ -186,11 +242,16 @@ impl ActivityPubService {
|
||||
let obj = if let Some(p) = page {
|
||||
let p = p.max(1);
|
||||
let offset = (p.saturating_sub(1) as usize) * PAGE_SIZE;
|
||||
let followers = data.follow_repo.get_followers_page(user_id, offset as u32, PAGE_SIZE).await?;
|
||||
let followers = data
|
||||
.follow_repo
|
||||
.get_followers_page(user_id, offset as u32, PAGE_SIZE)
|
||||
.await?;
|
||||
let has_next = offset + followers.len() < total;
|
||||
let items: Vec<String> = followers.into_iter().map(|f| f.actor.url).collect();
|
||||
let mut obj = serde_json::json!({"@context":AP_CONTEXT,"type":"OrderedCollectionPage","id":format!("{}?page={}",collection_id,p),"partOf":collection_id,"totalItems":total,"orderedItems":items});
|
||||
if has_next { obj["next"] = serde_json::json!(format!("{}?page={}",collection_id,p+1)); }
|
||||
if has_next {
|
||||
obj["next"] = serde_json::json!(format!("{}?page={}", collection_id, p + 1));
|
||||
}
|
||||
obj
|
||||
} else {
|
||||
serde_json::json!({"@context":AP_CONTEXT,"type":"OrderedCollection","id":collection_id,"totalItems":total,"first":format!("{}?page=1",collection_id)})
|
||||
@@ -198,7 +259,11 @@ impl ActivityPubService {
|
||||
Ok(serde_json::to_string(&obj)?)
|
||||
}
|
||||
|
||||
pub async fn following_collection_json(&self, user_id: uuid::Uuid, page: Option<u32>) -> anyhow::Result<String> {
|
||||
pub async fn following_collection_json(
|
||||
&self,
|
||||
user_id: uuid::Uuid,
|
||||
page: Option<u32>,
|
||||
) -> anyhow::Result<String> {
|
||||
const AP_CONTEXT: &str = "https://www.w3.org/ns/activitystreams";
|
||||
const PAGE_SIZE: usize = 20;
|
||||
let data = self.federation_config.to_request_data();
|
||||
@@ -207,11 +272,16 @@ impl ActivityPubService {
|
||||
let obj = if let Some(p) = page {
|
||||
let p = p.max(1);
|
||||
let offset = (p.saturating_sub(1) as usize) * PAGE_SIZE;
|
||||
let following = data.follow_repo.get_following_page(user_id, offset as u32, PAGE_SIZE).await?;
|
||||
let following = data
|
||||
.follow_repo
|
||||
.get_following_page(user_id, offset as u32, PAGE_SIZE)
|
||||
.await?;
|
||||
let has_next = offset + following.len() < total;
|
||||
let items: Vec<String> = following.into_iter().map(|a| a.url).collect();
|
||||
let mut obj = serde_json::json!({"@context":AP_CONTEXT,"type":"OrderedCollectionPage","id":format!("{}?page={}",collection_id,p),"partOf":collection_id,"totalItems":total,"orderedItems":items});
|
||||
if has_next { obj["next"] = serde_json::json!(format!("{}?page={}",collection_id,p+1)); }
|
||||
if has_next {
|
||||
obj["next"] = serde_json::json!(format!("{}?page={}", collection_id, p + 1));
|
||||
}
|
||||
obj
|
||||
} else {
|
||||
serde_json::json!({"@context":AP_CONTEXT,"type":"OrderedCollection","id":collection_id,"totalItems":total,"first":format!("{}?page=1",collection_id)})
|
||||
@@ -219,35 +289,67 @@ impl ActivityPubService {
|
||||
Ok(serde_json::to_string(&obj)?)
|
||||
}
|
||||
|
||||
pub async fn mark_follower_accepted(&self, user_id: uuid::Uuid, actor_url: &str) -> anyhow::Result<()> {
|
||||
pub async fn mark_follower_accepted(
|
||||
&self,
|
||||
user_id: uuid::Uuid,
|
||||
actor_url: &str,
|
||||
) -> anyhow::Result<()> {
|
||||
let data = self.federation_config.to_request_data();
|
||||
data.follow_repo.update_follower_status(user_id, actor_url, crate::repository::FollowerStatus::Accepted).await.map_err(|e| anyhow::anyhow!("{e}"))
|
||||
data.follow_repo
|
||||
.update_follower_status(
|
||||
user_id,
|
||||
actor_url,
|
||||
crate::repository::FollowerStatus::Accepted,
|
||||
)
|
||||
.await
|
||||
.map_err(|e| anyhow::anyhow!("{e}"))
|
||||
}
|
||||
|
||||
pub async fn mark_follower_rejected(&self, user_id: uuid::Uuid, actor_url: &str) -> anyhow::Result<()> {
|
||||
pub async fn mark_follower_rejected(
|
||||
&self,
|
||||
user_id: uuid::Uuid,
|
||||
actor_url: &str,
|
||||
) -> anyhow::Result<()> {
|
||||
let data = self.federation_config.to_request_data();
|
||||
data.follow_repo.remove_follower(user_id, actor_url).await.map_err(|e| anyhow::anyhow!("{e}"))
|
||||
data.follow_repo
|
||||
.remove_follower(user_id, actor_url)
|
||||
.await
|
||||
.map_err(|e| anyhow::anyhow!("{e}"))
|
||||
}
|
||||
|
||||
pub async fn lookup_actor_by_handle(&self, handle: &str) -> anyhow::Result<crate::user::LookedUpActor> {
|
||||
pub async fn lookup_actor_by_handle(
|
||||
&self,
|
||||
handle: &str,
|
||||
) -> anyhow::Result<crate::user::LookedUpActor> {
|
||||
tracing::info!(handle, "looking up remote actor");
|
||||
let data = self.federation_config.to_request_data();
|
||||
let actor = self.webfinger_https(handle, &data).await
|
||||
let actor = self
|
||||
.webfinger_https(handle, &data)
|
||||
.await
|
||||
.inspect_err(|e| tracing::warn!(handle, error = %e, "actor lookup failed"))?;
|
||||
let domain = actor.ap_id.host_str().unwrap_or("").to_string();
|
||||
tracing::info!(handle = format!("{}@{}", actor.username, domain), ap_url = %actor.ap_id, "remote actor resolved");
|
||||
Ok(crate::user::LookedUpActor {
|
||||
handle: format!("{}@{}", actor.username, domain),
|
||||
display_name: actor.display_name, bio: actor.bio,
|
||||
avatar_url: actor.avatar_url, banner_url: actor.banner_url,
|
||||
ap_url: actor.ap_id, outbox_url: Some(actor.outbox_url),
|
||||
followers_url: Some(actor.followers_url), following_url: Some(actor.following_url),
|
||||
also_known_as: actor.also_known_as, profile_url: actor.profile_url,
|
||||
display_name: actor.display_name,
|
||||
bio: actor.bio,
|
||||
avatar_url: actor.avatar_url,
|
||||
banner_url: actor.banner_url,
|
||||
ap_url: actor.ap_id,
|
||||
outbox_url: Some(actor.outbox_url),
|
||||
followers_url: Some(actor.followers_url),
|
||||
following_url: Some(actor.following_url),
|
||||
also_known_as: actor.also_known_as,
|
||||
profile_url: actor.profile_url,
|
||||
attachment: actor.attachment,
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn add_blocked_domain(&self, domain: &str, reason: Option<&str>) -> anyhow::Result<()> {
|
||||
pub async fn add_blocked_domain(
|
||||
&self,
|
||||
domain: &str,
|
||||
reason: Option<&str>,
|
||||
) -> anyhow::Result<()> {
|
||||
let data = self.federation_config.to_request_data();
|
||||
data.blocklist_repo.add_blocked_domain(domain, reason).await
|
||||
}
|
||||
@@ -269,13 +371,22 @@ impl ActivityPubService {
|
||||
data: &activitypub_federation::config::Data<FederationData>,
|
||||
local_user_id: uuid::Uuid,
|
||||
) -> anyhow::Result<Option<(DbActor, Vec<Url>)>> {
|
||||
let local_actor = get_local_actor(local_user_id, data).await.map_err(|e| anyhow::anyhow!("{e}"))?;
|
||||
let inbox_strs = data.follow_repo.get_accepted_follower_inboxes(local_user_id).await?;
|
||||
if inbox_strs.is_empty() { return Ok(None); }
|
||||
let local_actor = get_local_actor(local_user_id, data)
|
||||
.await
|
||||
.map_err(|e| anyhow::anyhow!("{e}"))?;
|
||||
let inbox_strs = data
|
||||
.follow_repo
|
||||
.get_accepted_follower_inboxes(local_user_id)
|
||||
.await?;
|
||||
if inbox_strs.is_empty() {
|
||||
return Ok(None);
|
||||
}
|
||||
let inboxes: Vec<Url> = inbox_strs.into_iter().filter_map(|s| {
|
||||
Url::parse(&s).map_err(|e| tracing::warn!(inbox = %s, error = %e, "skipping unparseable inbox URL")).ok()
|
||||
}).collect();
|
||||
if inboxes.is_empty() { return Ok(None); }
|
||||
if inboxes.is_empty() {
|
||||
return Ok(None);
|
||||
}
|
||||
Ok(Some((local_actor, inboxes)))
|
||||
}
|
||||
|
||||
@@ -285,20 +396,39 @@ impl ActivityPubService {
|
||||
data: &activitypub_federation::config::Data<FederationData>,
|
||||
) -> anyhow::Result<DbActor> {
|
||||
let normalized = handle.trim_start_matches('@');
|
||||
let at = normalized.rfind('@').ok_or_else(|| anyhow::anyhow!("handle must be user@domain"))?;
|
||||
let at = normalized
|
||||
.rfind('@')
|
||||
.ok_or_else(|| anyhow::anyhow!("handle must be user@domain"))?;
|
||||
let (user, domain_str) = (&normalized[..at], &normalized[at + 1..]);
|
||||
let wf_url = format!("https://{}/.well-known/webfinger?resource=acct:{}@{}", domain_str, user, domain_str);
|
||||
let wf_url = format!(
|
||||
"https://{}/.well-known/webfinger?resource=acct:{}@{}",
|
||||
domain_str, user, domain_str
|
||||
);
|
||||
tracing::debug!(handle, wf_url, "resolving webfinger");
|
||||
let wf: serde_json::Value = reqwest::Client::new().get(&wf_url)
|
||||
let wf: serde_json::Value = reqwest::Client::new()
|
||||
.get(&wf_url)
|
||||
.header("Accept", "application/jrd+json, application/json")
|
||||
.send().await?.json().await?;
|
||||
let self_href = wf["links"].as_array()
|
||||
.and_then(|links| links.iter().find(|l| l["rel"].as_str() == Some("self") && l["type"].as_str() == Some("application/activity+json")))
|
||||
.send()
|
||||
.await?
|
||||
.json()
|
||||
.await?;
|
||||
let self_href = wf["links"]
|
||||
.as_array()
|
||||
.and_then(|links| {
|
||||
links.iter().find(|l| {
|
||||
l["rel"].as_str() == Some("self")
|
||||
&& l["type"].as_str() == Some("application/activity+json")
|
||||
})
|
||||
})
|
||||
.and_then(|l| l["href"].as_str())
|
||||
.ok_or_else(|| anyhow::anyhow!("no self link in WebFinger response"))?.to_owned();
|
||||
.ok_or_else(|| anyhow::anyhow!("no self link in WebFinger response"))?
|
||||
.to_owned();
|
||||
tracing::debug!(handle, self_href, "webfinger resolved, fetching actor");
|
||||
let actor: DbActor = activitypub_federation::fetch::object_id::ObjectId::from(url::Url::parse(&self_href)?)
|
||||
.dereference(data).await.map_err(|e| anyhow::anyhow!("{e}"))?;
|
||||
let actor: DbActor =
|
||||
activitypub_federation::fetch::object_id::ObjectId::from(url::Url::parse(&self_href)?)
|
||||
.dereference(data)
|
||||
.await
|
||||
.map_err(|e| anyhow::anyhow!("{e}"))?;
|
||||
Ok(actor)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user