structural refactor and codebase improvements

This commit is contained in:
2026-08-09 14:58:14 +02:00
parent 22b1dd3f56
commit c9715baab8
247 changed files with 11515 additions and 3063 deletions

View File

@@ -9,77 +9,58 @@ use uuid::Uuid;
use crate::{
csrf::CsrfToken,
errors::ApiError,
extractors::{AdminApiUser, AuthenticatedUser, RequiredCookieUser},
forms::{
ActorUrlForm, BlockDomainForm, FollowForm, FollowerActionForm, RemoveDomainForm,
UnfollowForm,
},
extractors::{AuthenticatedUser, RequiredCookieUser},
forms::{FollowForm, FollowerActionForm, UnfollowForm},
render::render_page,
state::AppState,
};
#[cfg(feature = "federation")]
use crate::{
extractors::AdminApiUser,
forms::{ActorUrlForm, BlockDomainForm, RemoveDomainForm},
};
use api_types::{
ActorListResponse, ActorUrlRequest, AddBlockedDomainRequest, BlockedActorResponse,
BlockedDomainResponse, FollowRequest, RemoteActorDto,
};
use application::social::deps::{SocialCommandDeps, SocialQueryDeps};
use domain::value_objects::{FollowTarget, SocialActor, SocialIdentity};
use template_askama::{
BlockedActorsTemplate, BlockedDomainsTemplate, FollowersTemplate, FollowingTemplate,
RemoteActorData,
ActorListResponse, ActorUrlRequest, FollowRelationResponse, FollowRequest,
PendingCountResponse, RemoteActorDto,
};
#[cfg(feature = "federation")]
use api_types::{AddBlockedDomainRequest, BlockedActorResponse, BlockedDomainResponse};
use domain::value_objects::{FollowTarget, InstanceIdentity, SocialActor, SocialIdentity};
#[cfg(feature = "federation")]
use template_askama::{BlockedActorsTemplate, BlockedDomainsTemplate};
use template_askama::{FollowersTemplate, FollowingTemplate, RemoteActorData};
use super::helpers::{build_page_context, encode_error};
impl From<&AppState> for SocialCommandDeps {
fn from(state: &AppState) -> Self {
Self {
social_command: state.app_ctx.repos.social_command.clone(),
social_query: state.app_ctx.repos.social_query_unified.clone(),
event_publisher: state.app_ctx.services.event_publisher.clone(),
}
}
}
impl From<&AppState> for SocialQueryDeps {
fn from(state: &AppState) -> Self {
Self {
social_query: state.app_ctx.repos.social_query_unified.clone(),
}
}
}
fn ap_to_domain(e: anyhow::Error) -> domain::errors::DomainError {
tracing::error!("ActivityPub error: {:?}", e);
domain::errors::DomainError::InfrastructureError(e.to_string())
}
fn actor_url(identity: &SocialIdentity) -> String {
match identity {
SocialIdentity::Remote { actor_url } => actor_url.clone(),
SocialIdentity::Local(uid) => format!("local:{}", uid.value()),
}
}
fn social_actor_to_dto(actor: SocialActor) -> RemoteActorDto {
fn social_actor_to_dto(actor: SocialActor, instance: &InstanceIdentity) -> RemoteActorDto {
RemoteActorDto {
url: actor_url(&actor.identity),
handle: actor.handle,
display_name: actor.display_name,
}
}
fn social_actor_to_blocked_dto(actor: SocialActor) -> BlockedActorResponse {
BlockedActorResponse {
url: actor_url(&actor.identity),
url: instance.actor_url_of(&actor.identity),
user_id: match &actor.identity {
SocialIdentity::Local(uid) => Some(uid.value()),
SocialIdentity::Remote { .. } => None,
},
handle: actor.handle,
display_name: actor.display_name,
avatar_url: actor.avatar_url,
}
}
fn social_actor_to_template(actor: SocialActor) -> RemoteActorData {
#[cfg(feature = "federation")]
fn social_actor_to_blocked_dto(
actor: SocialActor,
instance: &InstanceIdentity,
) -> BlockedActorResponse {
BlockedActorResponse {
url: instance.actor_url_of(&actor.identity),
handle: actor.handle,
display_name: actor.display_name,
avatar_url: actor.avatar_url,
}
}
fn social_actor_to_template(actor: SocialActor, instance: &InstanceIdentity) -> RemoteActorData {
RemoteActorData {
url: actor_url(&actor.identity),
url: instance.actor_url_of(&actor.identity),
handle: actor.handle,
display_name: actor.display_name,
avatar_url: actor.avatar_url,
@@ -88,6 +69,7 @@ fn social_actor_to_template(actor: SocialActor) -> RemoteActorData {
// ── API ──────────────────────────────────────────────────────────────────────
#[cfg(feature = "federation")]
#[utoipa::path(
get, path = "/api/v1/admin/blocked-domains",
responses(
@@ -101,13 +83,7 @@ pub async fn get_blocked_domains_admin(
State(state): State<AppState>,
_admin: AdminApiUser,
) -> Result<Json<Vec<BlockedDomainResponse>>, ApiError> {
let domains = state
.app_ctx
.services
.ap_service
.get_blocked_domains()
.await
.map_err(ap_to_domain)?;
let domains = state.app_ctx.ap_blocklist.get_blocked_domains().await?;
Ok(Json(
domains
.into_iter()
@@ -120,6 +96,7 @@ pub async fn get_blocked_domains_admin(
))
}
#[cfg(feature = "federation")]
#[utoipa::path(
post, path = "/api/v1/admin/blocked-domains",
request_body = AddBlockedDomainRequest,
@@ -137,14 +114,13 @@ pub async fn add_blocked_domain_admin(
) -> Result<impl IntoResponse, ApiError> {
state
.app_ctx
.services
.ap_service
.ap_blocklist
.add_blocked_domain(&body.domain, body.reason.as_deref())
.await
.map_err(ap_to_domain)?;
.await?;
Ok(StatusCode::CREATED)
}
#[cfg(feature = "federation")]
#[utoipa::path(
delete, path = "/api/v1/admin/blocked-domains/{domain}",
params(("domain" = String, Path, description = "Domain to unblock")),
@@ -162,14 +138,13 @@ pub async fn remove_blocked_domain_admin(
) -> Result<impl IntoResponse, ApiError> {
state
.app_ctx
.services
.ap_service
.ap_blocklist
.remove_blocked_domain(&domain)
.await
.map_err(ap_to_domain)?;
.await?;
Ok(StatusCode::NO_CONTENT)
}
#[cfg(feature = "federation")]
#[utoipa::path(
post, path = "/api/v1/social/block",
request_body = ActorUrlRequest,
@@ -184,18 +159,19 @@ pub async fn block_actor_api(
user: AuthenticatedUser,
axum::Json(body): axum::Json<ActorUrlRequest>,
) -> Result<impl IntoResponse, ApiError> {
let deps = SocialCommandDeps::from(&state);
let instance = state.app_ctx.instance.clone();
application::social::execute::execute_command(
&deps,
&state.app_ctx.deps.social.command,
application::social::commands::SocialCmd::Block {
blocker_id: user.0.value(),
target: SocialIdentity::from_actor_url(&body.actor_url, &state.app_ctx.config.base_url),
target: instance.identify(&body.actor_url),
},
)
.await?;
Ok(StatusCode::NO_CONTENT)
}
#[cfg(feature = "federation")]
#[utoipa::path(
post, path = "/api/v1/social/unblock",
request_body = ActorUrlRequest,
@@ -210,18 +186,19 @@ pub async fn unblock_actor_api(
user: AuthenticatedUser,
axum::Json(body): axum::Json<ActorUrlRequest>,
) -> Result<impl IntoResponse, ApiError> {
let deps = SocialCommandDeps::from(&state);
let instance = state.app_ctx.instance.clone();
application::social::execute::execute_command(
&deps,
&state.app_ctx.deps.social.command,
application::social::commands::SocialCmd::Unblock {
blocker_id: user.0.value(),
target: SocialIdentity::from_actor_url(&body.actor_url, &state.app_ctx.config.base_url),
target: instance.identify(&body.actor_url),
},
)
.await?;
Ok(StatusCode::NO_CONTENT)
}
#[cfg(feature = "federation")]
#[utoipa::path(
get, path = "/api/v1/social/blocked",
responses(
@@ -234,18 +211,14 @@ pub async fn get_blocked_actors_api(
State(state): State<AppState>,
user: AuthenticatedUser,
) -> Result<Json<Vec<BlockedActorResponse>>, ApiError> {
let deps = SocialQueryDeps::from(&state);
let identities = application::social::execute::execute_query(
&deps,
application::social::queries::SocialQry::GetBlocked {
user_id: user.0.value(),
},
)
.await?;
let instance = state.app_ctx.instance.clone();
let identities =
application::social::get_blocked::execute(&state.app_ctx.deps.social.query, user.0.value())
.await?;
Ok(Json(
identities
.into_iter()
.map(social_actor_to_blocked_dto)
.map(|a| social_actor_to_blocked_dto(a, &instance))
.collect(),
))
}
@@ -262,16 +235,17 @@ pub async fn get_following(
State(state): State<AppState>,
user: AuthenticatedUser,
) -> Result<Json<ActorListResponse>, ApiError> {
let deps = SocialQueryDeps::from(&state);
let identities = application::social::execute::execute_query(
&deps,
application::social::queries::SocialQry::GetFollowing {
user_id: user.0.value(),
},
let instance = state.app_ctx.instance.clone();
let identities = application::social::get_following::execute(
&state.app_ctx.deps.social.query,
user.0.value(),
)
.await?;
Ok(Json(ActorListResponse {
actors: identities.into_iter().map(social_actor_to_dto).collect(),
actors: identities
.into_iter()
.map(|a| social_actor_to_dto(a, &instance))
.collect(),
}))
}
@@ -287,16 +261,17 @@ pub async fn get_followers(
State(state): State<AppState>,
user: AuthenticatedUser,
) -> Result<Json<ActorListResponse>, ApiError> {
let deps = SocialQueryDeps::from(&state);
let identities = application::social::execute::execute_query(
&deps,
application::social::queries::SocialQry::GetFollowers {
user_id: user.0.value(),
},
let instance = state.app_ctx.instance.clone();
let identities = application::social::get_followers::execute(
&state.app_ctx.deps.social.query,
user.0.value(),
)
.await?;
Ok(Json(ActorListResponse {
actors: identities.into_iter().map(social_actor_to_dto).collect(),
actors: identities
.into_iter()
.map(|a| social_actor_to_dto(a, &instance))
.collect(),
}))
}
@@ -305,14 +280,15 @@ pub async fn get_user_following(
_user: AuthenticatedUser,
Path(user_id): Path<Uuid>,
) -> Result<Json<ActorListResponse>, ApiError> {
let deps = SocialQueryDeps::from(&state);
let identities = application::social::execute::execute_query(
&deps,
application::social::queries::SocialQry::GetFollowing { user_id },
)
.await?;
let instance = state.app_ctx.instance.clone();
let identities =
application::social::get_following::execute(&state.app_ctx.deps.social.query, user_id)
.await?;
Ok(Json(ActorListResponse {
actors: identities.into_iter().map(social_actor_to_dto).collect(),
actors: identities
.into_iter()
.map(|a| social_actor_to_dto(a, &instance))
.collect(),
}))
}
@@ -321,14 +297,15 @@ pub async fn get_user_followers(
_user: AuthenticatedUser,
Path(user_id): Path<Uuid>,
) -> Result<Json<ActorListResponse>, ApiError> {
let deps = SocialQueryDeps::from(&state);
let identities = application::social::execute::execute_query(
&deps,
application::social::queries::SocialQry::GetFollowers { user_id },
)
.await?;
let instance = state.app_ctx.instance.clone();
let identities =
application::social::get_followers::execute(&state.app_ctx.deps.social.query, user_id)
.await?;
Ok(Json(ActorListResponse {
actors: identities.into_iter().map(social_actor_to_dto).collect(),
actors: identities
.into_iter()
.map(|a| social_actor_to_dto(a, &instance))
.collect(),
}))
}
@@ -346,9 +323,8 @@ pub async fn follow(
user: AuthenticatedUser,
Json(body): Json<FollowRequest>,
) -> Result<impl IntoResponse, ApiError> {
let deps = SocialCommandDeps::from(&state);
application::social::execute::execute_command(
&deps,
&state.app_ctx.deps.social.command,
application::social::commands::SocialCmd::Follow {
follower_id: user.0.value(),
target: FollowTarget::Handle(body.handle),
@@ -372,12 +348,12 @@ pub async fn unfollow(
user: AuthenticatedUser,
Json(body): Json<ActorUrlRequest>,
) -> Result<impl IntoResponse, ApiError> {
let deps = SocialCommandDeps::from(&state);
let instance = state.app_ctx.instance.clone();
application::social::execute::execute_command(
&deps,
&state.app_ctx.deps.social.command,
application::social::commands::SocialCmd::Unfollow {
follower_id: user.0.value(),
target: SocialIdentity::from_actor_url(&body.actor_url, &state.app_ctx.config.base_url),
target: instance.identify(&body.actor_url),
},
)
.await?;
@@ -398,15 +374,12 @@ pub async fn accept_follower(
user: AuthenticatedUser,
Json(body): Json<ActorUrlRequest>,
) -> Result<impl IntoResponse, ApiError> {
let deps = SocialCommandDeps::from(&state);
let instance = state.app_ctx.instance.clone();
application::social::execute::execute_command(
&deps,
&state.app_ctx.deps.social.command,
application::social::commands::SocialCmd::AcceptFollow {
owner_id: user.0.value(),
requester: SocialIdentity::from_actor_url(
&body.actor_url,
&state.app_ctx.config.base_url,
),
requester: instance.identify(&body.actor_url),
},
)
.await?;
@@ -427,15 +400,12 @@ pub async fn reject_follower(
user: AuthenticatedUser,
Json(body): Json<ActorUrlRequest>,
) -> Result<impl IntoResponse, ApiError> {
let deps = SocialCommandDeps::from(&state);
let instance = state.app_ctx.instance.clone();
application::social::execute::execute_command(
&deps,
&state.app_ctx.deps.social.command,
application::social::commands::SocialCmd::RejectFollow {
owner_id: user.0.value(),
requester: SocialIdentity::from_actor_url(
&body.actor_url,
&state.app_ctx.config.base_url,
),
requester: instance.identify(&body.actor_url),
},
)
.await?;
@@ -456,15 +426,12 @@ pub async fn remove_follower(
user: AuthenticatedUser,
Json(body): Json<ActorUrlRequest>,
) -> Result<impl IntoResponse, ApiError> {
let deps = SocialCommandDeps::from(&state);
let instance = state.app_ctx.instance.clone();
application::social::execute::execute_command(
&deps,
&state.app_ctx.deps.social.command,
application::social::commands::SocialCmd::RemoveFollower {
owner_id: user.0.value(),
follower: SocialIdentity::from_actor_url(
&body.actor_url,
&state.app_ctx.config.base_url,
),
follower: instance.identify(&body.actor_url),
},
)
.await?;
@@ -483,22 +450,101 @@ pub async fn get_pending_followers(
State(state): State<AppState>,
user: AuthenticatedUser,
) -> Result<Json<ActorListResponse>, ApiError> {
let deps = SocialQueryDeps::from(&state);
let identities = application::social::execute::execute_query(
&deps,
application::social::queries::SocialQry::GetPending {
user_id: user.0.value(),
},
let instance = state.app_ctx.instance.clone();
let identities = application::social::get_pending_followers::execute(
&state.app_ctx.deps.social.query,
user.0.value(),
)
.await?;
Ok(Json(ActorListResponse {
actors: identities.into_iter().map(social_actor_to_dto).collect(),
actors: identities
.into_iter()
.map(|a| social_actor_to_dto(a, &instance))
.collect(),
}))
}
#[utoipa::path(
get, path = "/api/v1/social/followers/pending/count",
responses(
(status = 200, body = PendingCountResponse),
(status = 401, description = "Unauthorized"),
),
security(("bearer_auth" = []))
)]
pub async fn get_pending_follower_count(
State(state): State<AppState>,
user: AuthenticatedUser,
) -> Result<Json<PendingCountResponse>, ApiError> {
let count = application::social::count_pending_followers::execute(
&state.app_ctx.deps.social.query,
user.0.value(),
)
.await?;
Ok(Json(PendingCountResponse { count }))
}
#[utoipa::path(
get, path = "/api/v1/social/following/pending",
responses(
(status = 200, body = ActorListResponse),
(status = 401, description = "Unauthorized"),
),
security(("bearer_auth" = []))
)]
pub async fn get_pending_following(
State(state): State<AppState>,
user: AuthenticatedUser,
) -> Result<Json<ActorListResponse>, ApiError> {
let instance = state.app_ctx.instance.clone();
let actors = application::social::get_pending_following::execute(
&state.app_ctx.deps.social.query,
user.0.value(),
)
.await?;
Ok(Json(ActorListResponse {
actors: actors
.into_iter()
.map(|a| social_actor_to_dto(a, &instance))
.collect(),
}))
}
#[derive(serde::Deserialize)]
pub struct RelationshipQuery {
pub actor_url: String,
}
#[utoipa::path(
get, path = "/api/v1/social/relationship",
params(("actor_url" = String, Query, description = "Canonical actor URL of the target")),
responses(
(status = 200, body = FollowRelationResponse),
(status = 401, description = "Unauthorized"),
),
security(("bearer_auth" = []))
)]
pub async fn get_relationship(
State(state): State<AppState>,
user: AuthenticatedUser,
Query(q): Query<RelationshipQuery>,
) -> Result<Json<FollowRelationResponse>, ApiError> {
let target = state.app_ctx.instance.identify(&q.actor_url);
let rel = application::social::get_relation::execute(
&state.app_ctx.deps.social.query,
user.0.value(),
target,
)
.await?;
Ok(Json(FollowRelationResponse {
following: rel.following.into(),
followed_by: rel.followed_by.into(),
}))
}
// ── HTML ─────────────────────────────────────────────────────────────────────
pub async fn follow_remote_user(
pub async fn follow_user(
RequiredCookieUser(user_id): RequiredCookieUser,
State(state): State<AppState>,
Path(profile_user_uuid): Path<Uuid>,
@@ -518,9 +564,8 @@ pub async fn follow_remote_user(
.unwrap_or(&format!("/users/{}", profile_user_uuid))
.to_string();
let deps = SocialCommandDeps::from(&state);
match application::social::execute::execute_command(
&deps,
&state.app_ctx.deps.social.command,
application::social::commands::SocialCmd::Follow {
follower_id: user_id.value(),
target: FollowTarget::Handle(form.handle),
@@ -542,7 +587,7 @@ pub async fn follow_remote_user(
}
}
pub async fn unfollow_remote_user(
pub async fn unfollow_user(
RequiredCookieUser(user_id): RequiredCookieUser,
State(state): State<AppState>,
Path(profile_user_uuid): Path<Uuid>,
@@ -555,12 +600,12 @@ pub async fn unfollow_remote_user(
if crate::csrf::mismatch(&csrf, &form.csrf_token) {
return StatusCode::FORBIDDEN.into_response();
}
let deps = SocialCommandDeps::from(&state);
let instance = state.app_ctx.instance.clone();
match application::social::execute::execute_command(
&deps,
&state.app_ctx.deps.social.command,
application::social::commands::SocialCmd::Unfollow {
follower_id: user_id.value(),
target: SocialIdentity::from_actor_url(&form.actor_url, &state.app_ctx.config.base_url),
target: instance.identify(&form.actor_url),
},
)
.await
@@ -592,15 +637,12 @@ pub async fn accept_follower_html(
if crate::csrf::mismatch(&csrf, &form.csrf_token) {
return StatusCode::FORBIDDEN.into_response();
}
let deps = SocialCommandDeps::from(&state);
let instance = state.app_ctx.instance.clone();
match application::social::execute::execute_command(
&deps,
&state.app_ctx.deps.social.command,
application::social::commands::SocialCmd::AcceptFollow {
owner_id: user_id.value(),
requester: SocialIdentity::from_actor_url(
&form.actor_url,
&state.app_ctx.config.base_url,
),
requester: instance.identify(&form.actor_url),
},
)
.await
@@ -626,15 +668,12 @@ pub async fn reject_follower_html(
if crate::csrf::mismatch(&csrf, &form.csrf_token) {
return StatusCode::FORBIDDEN.into_response();
}
let deps = SocialCommandDeps::from(&state);
let instance = state.app_ctx.instance.clone();
match application::social::execute::execute_command(
&deps,
&state.app_ctx.deps.social.command,
application::social::commands::SocialCmd::RejectFollow {
owner_id: user_id.value(),
requester: SocialIdentity::from_actor_url(
&form.actor_url,
&state.app_ctx.config.base_url,
),
requester: instance.identify(&form.actor_url),
},
)
.await
@@ -647,6 +686,7 @@ pub async fn reject_follower_html(
}
}
#[cfg(feature = "federation")]
pub async fn get_followers_collection(
State(state): State<AppState>,
Path(user_id): Path<Uuid>,
@@ -661,8 +701,7 @@ pub async fn get_followers_collection(
let page = params.get("page").and_then(|p| p.parse::<u32>().ok());
return match state
.app_ctx
.services
.ap_service
.ap_document
.followers_collection_json(user_id, page)
.await
{
@@ -674,12 +713,16 @@ pub async fn get_followers_collection(
json,
)
.into_response(),
Err(_) => StatusCode::NOT_FOUND.into_response(),
Err(e) => {
tracing::error!("followers_collection_json error: {:?}", e);
StatusCode::INTERNAL_SERVER_ERROR.into_response()
}
};
}
axum::response::Redirect::to(&format!("/users/{}/followers-list", user_id)).into_response()
}
#[cfg(feature = "federation")]
pub async fn get_following_collection(
State(state): State<AppState>,
Path(user_id): Path<Uuid>,
@@ -694,8 +737,7 @@ pub async fn get_following_collection(
let page = params.get("page").and_then(|p| p.parse::<u32>().ok());
return match state
.app_ctx
.services
.ap_service
.ap_document
.following_collection_json(user_id, page)
.await
{
@@ -707,7 +749,10 @@ pub async fn get_following_collection(
json,
)
.into_response(),
Err(_) => StatusCode::NOT_FOUND.into_response(),
Err(e) => {
tracing::error!("following_collection_json error: {:?}", e);
StatusCode::INTERNAL_SERVER_ERROR.into_response()
}
};
}
axum::response::Redirect::to(&format!("/users/{}/following-list", user_id)).into_response()
@@ -729,24 +774,39 @@ pub async fn get_following_page(
"{}/users/{}/following-list",
state.app_ctx.config.base_url, profile_user_uuid
);
let deps = SocialQueryDeps::from(&state);
match application::social::execute::execute_query(
&deps,
application::social::queries::SocialQry::GetFollowing {
user_id: user_id.value(),
},
let instance = state.app_ctx.instance.clone();
match application::social::get_following::execute(
&state.app_ctx.deps.social.query,
user_id.value(),
)
.await
{
Ok(following) => {
let actors: Vec<RemoteActorData> = following
.into_iter()
.map(social_actor_to_template)
.map(|a| social_actor_to_template(a, &instance))
.collect();
let pending_actors: Vec<RemoteActorData> =
match application::social::get_pending_following::execute(
&state.app_ctx.deps.social.query,
user_id.value(),
)
.await
{
Ok(pending) => pending
.into_iter()
.map(|a| social_actor_to_template(a, &instance))
.collect(),
Err(e) => {
tracing::error!("get_pending_following error: {:?}", e);
Vec::new()
}
};
render_page(FollowingTemplate {
ctx,
user_id: profile_user_uuid,
actors,
pending_actors,
error: params.error,
})
.into_response()
@@ -778,19 +838,17 @@ pub async fn get_followers_page(
"{}/users/{}/followers-list",
state.app_ctx.config.base_url, profile_user_uuid
);
let deps = SocialQueryDeps::from(&state);
match application::social::execute::execute_query(
&deps,
application::social::queries::SocialQry::GetFollowers {
user_id: user_id.value(),
},
let instance = state.app_ctx.instance.clone();
match application::social::get_followers::execute(
&state.app_ctx.deps.social.query,
user_id.value(),
)
.await
{
Ok(followers) => {
let actors: Vec<RemoteActorData> = followers
.into_iter()
.map(social_actor_to_template)
.map(|a| social_actor_to_template(a, &instance))
.collect();
render_page(FollowersTemplate {
ctx,
@@ -824,15 +882,12 @@ pub async fn remove_follower_html(
if crate::csrf::mismatch(&csrf, &form.csrf_token) {
return StatusCode::FORBIDDEN.into_response();
}
let deps = SocialCommandDeps::from(&state);
let instance = state.app_ctx.instance.clone();
match application::social::execute::execute_command(
&deps,
&state.app_ctx.deps.social.command,
application::social::commands::SocialCmd::RemoveFollower {
owner_id: user_id.value(),
follower: SocialIdentity::from_actor_url(
&form.actor_url,
&state.app_ctx.config.base_url,
),
follower: instance.identify(&form.actor_url),
},
)
.await
@@ -851,6 +906,7 @@ pub async fn remove_follower_html(
}
}
#[cfg(feature = "federation")]
pub async fn get_blocked_domains_page(
crate::extractors::AdminUser(user_id): crate::extractors::AdminUser,
State(state): State<AppState>,
@@ -859,13 +915,7 @@ pub async fn get_blocked_domains_page(
let mut ctx = build_page_context(&state, Some(user_id), csrf.0).await;
ctx.page_title = "Blocked Domains — Movies Diary".to_string();
ctx.canonical_url = format!("{}/admin/blocked-domains", state.app_ctx.config.base_url);
match state
.app_ctx
.services
.ap_service
.get_blocked_domains()
.await
{
match state.app_ctx.ap_blocklist.get_blocked_domains().await {
Ok(domains) => {
let entries: Vec<template_askama::BlockedDomainEntry> = domains
.into_iter()
@@ -892,6 +942,7 @@ pub async fn get_blocked_domains_page(
}
}
#[cfg(feature = "federation")]
pub async fn post_blocked_domain(
crate::extractors::AdminUser(_): crate::extractors::AdminUser,
State(state): State<AppState>,
@@ -904,8 +955,7 @@ pub async fn post_blocked_domain(
let reason = form.reason.as_deref().filter(|s| !s.trim().is_empty());
match state
.app_ctx
.services
.ap_service
.ap_blocklist
.add_blocked_domain(&form.domain, reason)
.await
{
@@ -917,6 +967,7 @@ pub async fn post_blocked_domain(
}
}
#[cfg(feature = "federation")]
pub async fn post_remove_blocked_domain(
crate::extractors::AdminUser(_): crate::extractors::AdminUser,
State(state): State<AppState>,
@@ -928,8 +979,7 @@ pub async fn post_remove_blocked_domain(
}
match state
.app_ctx
.services
.ap_service
.ap_blocklist
.remove_blocked_domain(&form.domain)
.await
{
@@ -941,6 +991,7 @@ pub async fn post_remove_blocked_domain(
}
}
#[cfg(feature = "federation")]
pub async fn get_blocked_actors_page(
RequiredCookieUser(user_id): RequiredCookieUser,
State(state): State<AppState>,
@@ -949,12 +1000,10 @@ pub async fn get_blocked_actors_page(
let mut ctx = build_page_context(&state, Some(user_id.clone()), csrf.0).await;
ctx.page_title = "Blocked Users — Movies Diary".to_string();
ctx.canonical_url = format!("{}/social/blocked", state.app_ctx.config.base_url);
let deps = SocialQueryDeps::from(&state);
match application::social::execute::execute_query(
&deps,
application::social::queries::SocialQry::GetBlocked {
user_id: user_id.value(),
},
let instance = state.app_ctx.instance.clone();
match application::social::get_blocked::execute(
&state.app_ctx.deps.social.query,
user_id.value(),
)
.await
{
@@ -962,7 +1011,7 @@ pub async fn get_blocked_actors_page(
let entries: Vec<template_askama::BlockedActorEntry> = blocked
.into_iter()
.map(|a| template_askama::BlockedActorEntry {
url: actor_url(&a.identity),
url: instance.actor_url_of(&a.identity),
handle: a.handle,
display_name: a.display_name,
avatar_url: a.avatar_url,
@@ -985,6 +1034,7 @@ pub async fn get_blocked_actors_page(
}
}
#[cfg(feature = "federation")]
pub async fn post_block_actor_html(
RequiredCookieUser(user_id): RequiredCookieUser,
State(state): State<AppState>,
@@ -994,12 +1044,12 @@ pub async fn post_block_actor_html(
if crate::csrf::mismatch(&csrf, &form.csrf_token) {
return StatusCode::FORBIDDEN.into_response();
}
let deps = SocialCommandDeps::from(&state);
let instance = state.app_ctx.instance.clone();
match application::social::execute::execute_command(
&deps,
&state.app_ctx.deps.social.command,
application::social::commands::SocialCmd::Block {
blocker_id: user_id.value(),
target: SocialIdentity::from_actor_url(&form.actor_url, &state.app_ctx.config.base_url),
target: instance.identify(&form.actor_url),
},
)
.await
@@ -1012,6 +1062,7 @@ pub async fn post_block_actor_html(
}
}
#[cfg(feature = "federation")]
pub async fn post_unblock_actor(
RequiredCookieUser(user_id): RequiredCookieUser,
State(state): State<AppState>,
@@ -1021,12 +1072,12 @@ pub async fn post_unblock_actor(
if crate::csrf::mismatch(&csrf, &form.csrf_token) {
return StatusCode::FORBIDDEN.into_response();
}
let deps = SocialCommandDeps::from(&state);
let instance = state.app_ctx.instance.clone();
match application::social::execute::execute_command(
&deps,
&state.app_ctx.deps.social.command,
application::social::commands::SocialCmd::Unblock {
blocker_id: user_id.value(),
target: SocialIdentity::from_actor_url(&form.actor_url, &state.app_ctx.config.base_url),
target: instance.identify(&form.actor_url),
},
)
.await
@@ -1038,3 +1089,110 @@ pub async fn post_unblock_actor(
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use domain::value_objects::{InstanceIdentity, SocialActor, SocialIdentity, UserId};
fn local_actor(uid: &UserId) -> SocialActor {
SocialActor {
identity: SocialIdentity::Local(uid.clone()),
handle: "@gabriel@md.example".into(),
display_name: None,
avatar_url: None,
}
}
/// The bug: a local actor used to serialize as `local:{uuid}`, which
/// `identify` cannot parse, so every POST echoing this url back resolved
/// to Remote and was routed to ActivityPub.
#[test]
fn local_actor_dto_url_round_trips_back_to_the_same_identity() {
let instance = InstanceIdentity::new("https://md.example");
let uid = UserId::from_uuid(uuid::Uuid::new_v4());
let dto = social_actor_to_dto(local_actor(&uid), &instance);
assert!(!dto.url.starts_with("local:"), "url was {}", dto.url);
assert_eq!(
instance.identify(&dto.url),
SocialIdentity::Local(uid),
"a local actor's dto url must parse back as Local"
);
}
#[cfg(feature = "federation")]
#[test]
fn local_blocked_dto_url_round_trips_back_to_the_same_identity() {
let instance = InstanceIdentity::new("https://md.example");
let uid = UserId::from_uuid(uuid::Uuid::new_v4());
let dto = social_actor_to_blocked_dto(local_actor(&uid), &instance);
assert!(!dto.url.starts_with("local:"), "url was {}", dto.url);
assert_eq!(instance.identify(&dto.url), SocialIdentity::Local(uid));
}
#[test]
fn local_template_url_round_trips_back_to_the_same_identity() {
let instance = InstanceIdentity::new("https://md.example");
let uid = UserId::from_uuid(uuid::Uuid::new_v4());
let data = social_actor_to_template(local_actor(&uid), &instance);
assert!(!data.url.starts_with("local:"), "url was {}", data.url);
assert_eq!(instance.identify(&data.url), SocialIdentity::Local(uid));
}
#[test]
fn remote_actor_dto_url_is_passed_through_unchanged() {
let instance = InstanceIdentity::new("https://md.example");
let actor = SocialActor {
identity: SocialIdentity::Remote {
actor_url: "https://other.example/users/bob".into(),
},
handle: "@bob@other.example".into(),
display_name: None,
avatar_url: None,
};
let dto = social_actor_to_dto(actor, &instance);
assert_eq!(dto.url, "https://other.example/users/bob");
}
#[test]
fn local_actor_dto_carries_user_id_and_avatar() {
let instance = InstanceIdentity::new("https://md.example");
let uid = UserId::from_uuid(uuid::Uuid::new_v4());
let mut actor = local_actor(&uid);
actor.avatar_url = Some("https://md.example/images/a.webp".into());
let dto = social_actor_to_dto(actor, &instance);
assert_eq!(
dto.user_id,
Some(uid.value()),
"local actors must carry user_id for internal links"
);
assert_eq!(
dto.avatar_url.as_deref(),
Some("https://md.example/images/a.webp")
);
}
#[test]
fn remote_actor_dto_has_no_user_id() {
let instance = InstanceIdentity::new("https://md.example");
let actor = SocialActor {
identity: SocialIdentity::Remote {
actor_url: "https://other.example/users/bob".into(),
},
handle: "@bob@other.example".into(),
display_name: None,
avatar_url: None,
};
assert_eq!(social_actor_to_dto(actor, &instance).user_id, None);
}
}