feat: implement unread notification count and enhance user listing with pagination
Some checks failed
lint / lint (push) Has been cancelled
test / unit (push) Has been cancelled
test / integration (push) Has been cancelled
lint / lint (pull_request) Failing after 9m33s
test / unit (pull_request) Successful in 16m24s
test / integration (pull_request) Failing after 16m52s
Some checks failed
lint / lint (push) Has been cancelled
test / unit (push) Has been cancelled
test / integration (push) Has been cancelled
lint / lint (pull_request) Failing after 9m33s
test / unit (pull_request) Successful in 16m24s
test / integration (pull_request) Failing after 16m52s
This commit is contained in:
@@ -6,12 +6,14 @@ use api_types::{
|
||||
requests::PaginationQuery,
|
||||
responses::{ActorConnectionPageResponse, ActorConnectionResponse},
|
||||
};
|
||||
use application::use_cases::feed::get_user_feed;
|
||||
use application::use_cases::federation_management::{
|
||||
get_actor_connections_page, get_remote_actor_posts,
|
||||
};
|
||||
use axum::{
|
||||
extract::{Path, Query, State},
|
||||
Json,
|
||||
};
|
||||
use domain::{events::DomainEvent, models::feed::PageParams};
|
||||
use domain::models::feed::PageParams;
|
||||
|
||||
pub async fn remote_actor_posts_handler(
|
||||
State(s): State<AppState>,
|
||||
@@ -19,53 +21,20 @@ pub async fn remote_actor_posts_handler(
|
||||
Query(q): Query<PaginationQuery>,
|
||||
OptionalAuthUser(viewer): OptionalAuthUser,
|
||||
) -> Result<Json<serde_json::Value>, ApiError> {
|
||||
tracing::info!(%handle, "remote_actor_posts: looking up actor");
|
||||
let actor = s.federation.lookup_actor(&handle).await?;
|
||||
tracing::info!(actor_url = %actor.url, has_outbox = actor.outbox_url.is_some(), "remote_actor_posts: actor found");
|
||||
|
||||
let ap_url = url::Url::parse(&actor.url).map_err(|e| ApiError::BadRequest(e.to_string()))?;
|
||||
|
||||
let author_id = match s.ap_repo.find_remote_actor_id(&ap_url).await? {
|
||||
Some(id) => {
|
||||
tracing::info!(?id, "remote_actor_posts: actor already interned");
|
||||
id
|
||||
}
|
||||
None => {
|
||||
tracing::info!("remote_actor_posts: interning actor");
|
||||
let id = s.ap_repo.intern_remote_actor(&ap_url).await?;
|
||||
tracing::info!(?id, "remote_actor_posts: actor interned");
|
||||
id
|
||||
}
|
||||
};
|
||||
|
||||
let page = PageParams {
|
||||
page: q.page(),
|
||||
per_page: q.per_page(),
|
||||
};
|
||||
let result = get_user_feed(&*s.feed, &author_id, page, viewer.as_ref()).await?;
|
||||
tracing::info!(
|
||||
post_count = result.items.len(),
|
||||
"remote_actor_posts: cached posts fetched"
|
||||
);
|
||||
|
||||
match &actor.outbox_url {
|
||||
Some(outbox_url) => {
|
||||
tracing::info!(%outbox_url, "remote_actor_posts: publishing FetchRemoteActorPosts");
|
||||
match s
|
||||
.events
|
||||
.publish(&DomainEvent::FetchRemoteActorPosts {
|
||||
actor_ap_url: actor.url.clone(),
|
||||
outbox_url: outbox_url.clone(),
|
||||
})
|
||||
.await
|
||||
{
|
||||
Ok(_) => tracing::info!("remote_actor_posts: event published"),
|
||||
Err(e) => tracing::warn!("remote_actor_posts: event publish failed: {e}"),
|
||||
}
|
||||
}
|
||||
None => tracing::warn!("remote_actor_posts: actor has no outbox_url, skipping fetch"),
|
||||
}
|
||||
|
||||
let result = get_remote_actor_posts(
|
||||
&*s.federation,
|
||||
&*s.ap_repo,
|
||||
&*s.feed,
|
||||
&*s.events,
|
||||
&handle,
|
||||
page,
|
||||
viewer.as_ref(),
|
||||
)
|
||||
.await?;
|
||||
Ok(Json(serde_json::json!({
|
||||
"total": result.total,
|
||||
"page": result.page,
|
||||
@@ -74,8 +43,6 @@ pub async fn remote_actor_posts_handler(
|
||||
})))
|
||||
}
|
||||
|
||||
const CACHE_TTL_SECS: i64 = 3600;
|
||||
|
||||
pub async fn actor_followers_handler(
|
||||
State(s): State<AppState>,
|
||||
Path(handle): Path<String>,
|
||||
@@ -98,46 +65,15 @@ async fn actor_connections_handler(
|
||||
connection_type: &str,
|
||||
page: u32,
|
||||
) -> Result<Json<ActorConnectionPageResponse>, ApiError> {
|
||||
const PAGE_SIZE: usize = 20;
|
||||
|
||||
let actor = s.federation.lookup_actor(&handle).await?;
|
||||
|
||||
let collection_url = match connection_type {
|
||||
"followers" => actor
|
||||
.followers_url
|
||||
.ok_or_else(|| ApiError::BadRequest("actor has no followers URL".into()))?,
|
||||
_ => actor
|
||||
.following_url
|
||||
.ok_or_else(|| ApiError::BadRequest("actor has no following URL".into()))?,
|
||||
};
|
||||
|
||||
let items = s
|
||||
.remote_actor_connections
|
||||
.list_connections(&actor.url, connection_type, page)
|
||||
.await?;
|
||||
|
||||
let stale = match s
|
||||
.remote_actor_connections
|
||||
.connection_page_age(&actor.url, connection_type, page)
|
||||
.await?
|
||||
{
|
||||
None => true,
|
||||
Some(age) => chrono::Utc::now().signed_duration_since(age).num_seconds() > CACHE_TTL_SECS,
|
||||
};
|
||||
|
||||
if stale {
|
||||
let _ = s
|
||||
.events
|
||||
.publish(&DomainEvent::FetchActorConnections {
|
||||
actor_ap_url: actor.url.clone(),
|
||||
collection_url,
|
||||
connection_type: connection_type.to_string(),
|
||||
page,
|
||||
})
|
||||
.await;
|
||||
}
|
||||
|
||||
let has_more = items.len() >= PAGE_SIZE;
|
||||
let (items, has_more) = get_actor_connections_page(
|
||||
&*s.federation,
|
||||
&*s.remote_actor_connections,
|
||||
&*s.events,
|
||||
&handle,
|
||||
connection_type,
|
||||
page,
|
||||
)
|
||||
.await?;
|
||||
Ok(Json(ActorConnectionPageResponse {
|
||||
items: items
|
||||
.into_iter()
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
use crate::{errors::ApiError, extractors::AuthUser, state::AppState};
|
||||
use api_types::responses::RemoteActorResponse;
|
||||
use api_types::responses::{ProfileField, RemoteActorResponse};
|
||||
use application::use_cases::federation_management::{
|
||||
accept_follow_request, list_pending_requests, list_remote_followers, list_remote_following,
|
||||
reject_follow_request,
|
||||
reject_follow_request, remove_remote_following,
|
||||
};
|
||||
use axum::{extract::State, http::StatusCode, Json};
|
||||
use serde::Deserialize;
|
||||
@@ -29,7 +29,11 @@ fn to_response(a: domain::models::remote_actor::RemoteActor) -> RemoteActorRespo
|
||||
outbox_url: a.outbox_url,
|
||||
followers_url: a.followers_url,
|
||||
following_url: a.following_url,
|
||||
attachment: vec![],
|
||||
attachment: a
|
||||
.attachment
|
||||
.into_iter()
|
||||
.map(|(name, value)| ProfileField { name, value })
|
||||
.collect(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -80,7 +84,7 @@ pub async fn delete_following(
|
||||
AuthUser(uid): AuthUser,
|
||||
Json(body): Json<HandleBody>,
|
||||
) -> Result<StatusCode, ApiError> {
|
||||
application::use_cases::social::unfollow_actor(
|
||||
remove_remote_following(
|
||||
&*s.follows,
|
||||
&*s.users,
|
||||
&*s.federation,
|
||||
|
||||
@@ -10,7 +10,7 @@ use application::use_cases::feed::{
|
||||
get_by_tag, get_followers, get_following, get_home_feed,
|
||||
get_popular_tags as uc_get_popular_tags, get_public_feed, get_user_feed,
|
||||
};
|
||||
use application::use_cases::profile::get_user_by_username;
|
||||
use application::use_cases::profile::{get_user_by_id_or_username, get_user_by_username};
|
||||
use application::use_cases::search::{search_thoughts, search_users};
|
||||
use axum::{
|
||||
extract::{Path, Query, State},
|
||||
@@ -19,17 +19,6 @@ use axum::{
|
||||
Json,
|
||||
};
|
||||
use domain::models::feed::PageParams;
|
||||
use domain::value_objects::UserId;
|
||||
|
||||
fn visibility_as_str(v: &domain::models::thought::Visibility) -> &'static str {
|
||||
use domain::models::thought::Visibility;
|
||||
match v {
|
||||
Visibility::Public => "public",
|
||||
Visibility::Followers => "followers",
|
||||
Visibility::Unlisted => "unlisted",
|
||||
Visibility::Direct => "direct",
|
||||
}
|
||||
}
|
||||
|
||||
pub fn to_thought_response(e: &domain::models::feed::FeedEntry) -> ThoughtResponse {
|
||||
ThoughtResponse {
|
||||
@@ -38,7 +27,7 @@ pub fn to_thought_response(e: &domain::models::feed::FeedEntry) -> ThoughtRespon
|
||||
author: to_user_response(&e.author),
|
||||
in_reply_to_id: e.thought.in_reply_to_id.as_ref().map(|id| id.as_uuid()),
|
||||
in_reply_to_url: e.thought.in_reply_to_url.clone(),
|
||||
visibility: visibility_as_str(&e.thought.visibility).to_string(),
|
||||
visibility: e.thought.visibility.as_str().to_string(),
|
||||
content_warning: e.thought.content_warning.clone(),
|
||||
sensitive: e.thought.sensitive,
|
||||
like_count: e.like_count,
|
||||
@@ -175,7 +164,8 @@ pub async fn get_following_handler(
|
||||
.unwrap_or("");
|
||||
|
||||
if accept.contains("application/activity+json") {
|
||||
let user_id = resolve_user_id(&s, ¶m).await?;
|
||||
let user = get_user_by_id_or_username(&*s.users, ¶m).await?;
|
||||
let user_id = user.id;
|
||||
let page = q.page().try_into().ok();
|
||||
let json = s
|
||||
.federation
|
||||
@@ -209,7 +199,8 @@ pub async fn get_followers_handler(
|
||||
.unwrap_or("");
|
||||
|
||||
if accept.contains("application/activity+json") {
|
||||
let user_id = resolve_user_id(&s, ¶m).await?;
|
||||
let user = get_user_by_id_or_username(&*s.users, ¶m).await?;
|
||||
let user_id = user.id;
|
||||
let page = q.page().try_into().ok();
|
||||
let json = s
|
||||
.federation
|
||||
@@ -231,18 +222,6 @@ pub async fn get_followers_handler(
|
||||
.into_response())
|
||||
}
|
||||
|
||||
async fn resolve_user_id(s: &AppState, param: &str) -> Result<UserId, ApiError> {
|
||||
if let Ok(uuid) = uuid::Uuid::parse_str(param) {
|
||||
s.users
|
||||
.find_by_id(&UserId::from_uuid(uuid))
|
||||
.await?
|
||||
.map(|u| u.id)
|
||||
.ok_or_else(|| ApiError::from(domain::errors::DomainError::NotFound))
|
||||
} else {
|
||||
Ok(get_user_by_username(&*s.users, param).await?.id)
|
||||
}
|
||||
}
|
||||
|
||||
#[utoipa::path(
|
||||
get, path = "/users/{username}/thoughts",
|
||||
params(
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
use crate::{errors::ApiError, extractors::AuthUser, state::AppState};
|
||||
use api_types::requests::NotificationUpdateRequest;
|
||||
use application::use_cases::notifications::{
|
||||
list_notifications as uc_list_notifications, mark_all_notifications_read,
|
||||
mark_notification_read as uc_mark_notification_read,
|
||||
count_unread_notifications, list_notifications as uc_list_notifications,
|
||||
mark_all_notifications_read, mark_notification_read as uc_mark_notification_read,
|
||||
};
|
||||
use axum::{
|
||||
extract::{Path, State},
|
||||
@@ -22,9 +22,10 @@ pub async fn list_notifications(
|
||||
per_page: 20,
|
||||
};
|
||||
let result = uc_list_notifications(&*s.notifications, &uid, page).await?;
|
||||
let unread = count_unread_notifications(&*s.notifications, &uid).await?;
|
||||
Ok(Json(serde_json::json!({
|
||||
"total": result.total,
|
||||
"unread": result.items.iter().filter(|n| !n.read).count()
|
||||
"unread": unread
|
||||
})))
|
||||
}
|
||||
|
||||
@@ -35,9 +36,13 @@ pub async fn mark_notification_read(
|
||||
Path(id): Path<Uuid>,
|
||||
Json(body): Json<NotificationUpdateRequest>,
|
||||
) -> Result<StatusCode, ApiError> {
|
||||
if body.read {
|
||||
uc_mark_notification_read(&*s.notifications, &NotificationId::from_uuid(id), &uid).await?;
|
||||
}
|
||||
uc_mark_notification_read(
|
||||
&*s.notifications,
|
||||
&NotificationId::from_uuid(id),
|
||||
&uid,
|
||||
body.read,
|
||||
)
|
||||
.await?;
|
||||
Ok(StatusCode::NO_CONTENT)
|
||||
}
|
||||
|
||||
@@ -47,9 +52,7 @@ pub async fn mark_all_read(
|
||||
AuthUser(uid): AuthUser,
|
||||
Json(body): Json<NotificationUpdateRequest>,
|
||||
) -> Result<StatusCode, ApiError> {
|
||||
if body.read {
|
||||
mark_all_notifications_read(&*s.notifications, &uid).await?;
|
||||
}
|
||||
mark_all_notifications_read(&*s.notifications, &uid, body.read).await?;
|
||||
Ok(StatusCode::NO_CONTENT)
|
||||
}
|
||||
|
||||
|
||||
@@ -96,8 +96,7 @@ pub async fn post_block(
|
||||
AuthUser(uid): AuthUser,
|
||||
Path(username): Path<String>,
|
||||
) -> Result<StatusCode, ApiError> {
|
||||
let target = get_user_by_username(&*s.users, &username).await?;
|
||||
block_user(&*s.blocks, &*s.events, &uid, &target.id).await?;
|
||||
block_by_username(&*s.blocks, &*s.users, &*s.events, &uid, &username).await?;
|
||||
Ok(StatusCode::NO_CONTENT)
|
||||
}
|
||||
#[utoipa::path(delete, path = "/users/{username}/block", params(("username" = String, Path, description = "Username")), responses((status = 204, description = "Unblocked")), security(("bearer_auth" = [])))]
|
||||
@@ -106,8 +105,7 @@ pub async fn delete_block(
|
||||
AuthUser(uid): AuthUser,
|
||||
Path(username): Path<String>,
|
||||
) -> Result<StatusCode, ApiError> {
|
||||
let target = get_user_by_username(&*s.users, &username).await?;
|
||||
unblock_user(&*s.blocks, &*s.events, &uid, &target.id).await?;
|
||||
unblock_by_username(&*s.blocks, &*s.users, &*s.events, &uid, &username).await?;
|
||||
Ok(StatusCode::NO_CONTENT)
|
||||
}
|
||||
#[utoipa::path(put, path = "/users/me/top-friends", request_body = SetTopFriendsRequest, responses((status = 204, description = "Top friends updated")), security(("bearer_auth" = [])))]
|
||||
|
||||
@@ -20,16 +20,6 @@ use axum::{
|
||||
use domain::value_objects::ThoughtId;
|
||||
use uuid::Uuid;
|
||||
|
||||
fn visibility_as_str(v: &domain::models::thought::Visibility) -> &'static str {
|
||||
use domain::models::thought::Visibility;
|
||||
match v {
|
||||
Visibility::Public => "public",
|
||||
Visibility::Followers => "followers",
|
||||
Visibility::Unlisted => "unlisted",
|
||||
Visibility::Direct => "direct",
|
||||
}
|
||||
}
|
||||
|
||||
fn thought_to_json(
|
||||
t: &domain::models::thought::Thought,
|
||||
author: &domain::models::user::User,
|
||||
@@ -42,7 +32,7 @@ fn thought_to_json(
|
||||
"content": t.content.as_str(),
|
||||
"author": to_user_response(author),
|
||||
"replyToId": t.in_reply_to_id.as_ref().map(|x| x.as_uuid()),
|
||||
"visibility": visibility_as_str(&t.visibility),
|
||||
"visibility": t.visibility.as_str(),
|
||||
"contentWarning": t.content_warning,
|
||||
"sensitive": t.sensitive,
|
||||
"likeCount": like_count,
|
||||
|
||||
@@ -8,7 +8,7 @@ use api_types::{
|
||||
requests::{PaginationQuery, UpdateProfileRequest},
|
||||
responses::{ErrorResponse, ProfileField, RemoteActorResponse, UserResponse},
|
||||
};
|
||||
use application::use_cases::feed::list_users;
|
||||
use application::use_cases::feed::list_users_paginated;
|
||||
use application::use_cases::profile::{
|
||||
get_user as fetch_user, get_user_by_id_or_username, update_profile,
|
||||
};
|
||||
@@ -146,13 +146,10 @@ pub async fn get_users(
|
||||
})));
|
||||
}
|
||||
|
||||
let all = list_users(&*s.users).await?;
|
||||
let total = all.len() as i64;
|
||||
let start = ((page - 1) * per_page) as usize;
|
||||
let items: Vec<_> = all
|
||||
.into_iter()
|
||||
.skip(start)
|
||||
.take(per_page as usize)
|
||||
let result = list_users_paginated(&*s.users, page_params).await?;
|
||||
let items: Vec<_> = result
|
||||
.items
|
||||
.iter()
|
||||
.map(|u| {
|
||||
serde_json::json!({
|
||||
"id": u.id.as_uuid(),
|
||||
@@ -169,7 +166,7 @@ pub async fn get_users(
|
||||
})
|
||||
.collect();
|
||||
Ok(Json(serde_json::json!({
|
||||
"items": items, "total": total, "page": page, "per_page": per_page
|
||||
"items": items, "total": result.total, "page": result.page, "per_page": result.per_page
|
||||
})))
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user