feat: add profile fields for local users

DB→domain→API→AP→frontend end-to-end. Fields stored as
JSONB, exposed via PATCH /users/me, serialized as AP
PropertyValue attachment. Editor in federation settings,
display on profile card.
This commit is contained in:
2026-05-29 13:54:25 +02:00
parent 14a869cc8d
commit 805bd9534f
19 changed files with 224 additions and 27 deletions

View File

@@ -12,6 +12,7 @@ tracing = { workspace = true }
async-trait = { workspace = true }
anyhow = { workspace = true }
url = { workspace = true }
serde_json = { workspace = true }
[dev-dependencies]
tokio = { workspace = true, features = ["full"] }

View File

@@ -13,8 +13,9 @@ impl<T> IntoAnyhow<T> for std::result::Result<T, sqlx::Error> {
}
use k_ap::{
ActivityRepository, ActorRepository, ApActorType, ApUser, ApUserRepository, BlockedDomain,
BlocklistRepository, FollowRepository, Follower, FollowerStatus, FollowingStatus, RemoteActor,
ActivityRepository, ActorRepository, ApActorType, ApProfileField, ApUser, ApUserRepository,
BlockedDomain, BlocklistRepository, FollowRepository, Follower, FollowerStatus,
FollowingStatus, RemoteActor,
};
// ── PostgresFederationRepository ─────────────────────────────────────────────
@@ -751,6 +752,7 @@ struct UserRow {
avatar_url: Option<String>,
header_url: Option<String>,
also_known_as: Option<String>,
profile_fields: Option<serde_json::Value>,
}
pub struct PgApUserRepository {
@@ -767,6 +769,19 @@ impl PgApUserRepository {
let profile_url = url::Url::parse(&format!("{}/users/{}", self.base_url, r.id)).ok();
let avatar_url = r.avatar_url.and_then(|u| url::Url::parse(&u).ok());
let banner_url = r.header_url.and_then(|u| url::Url::parse(&u).ok());
let attachment = r
.profile_fields
.and_then(|v| v.as_array().cloned())
.map(|arr| {
arr.into_iter()
.filter_map(|item| {
let name = item.get("name")?.as_str()?.to_string();
let value = item.get("value")?.as_str()?.to_string();
Some(ApProfileField { name, value })
})
.collect()
})
.unwrap_or_default();
ApUser {
id: r.id,
username: r.username,
@@ -776,7 +791,7 @@ impl PgApUserRepository {
banner_url,
also_known_as: r.also_known_as.into_iter().collect(),
profile_url,
attachment: vec![],
attachment,
manually_approves_followers: true,
discoverable: true,
actor_type: ApActorType::default(),
@@ -789,7 +804,7 @@ impl PgApUserRepository {
impl ApUserRepository for PgApUserRepository {
async fn find_by_id(&self, id: uuid::Uuid) -> Result<Option<ApUser>> {
let row = sqlx::query_as::<_, UserRow>(
"SELECT id,username,display_name,bio,avatar_url,header_url,also_known_as FROM users WHERE id=$1 AND local=true",
"SELECT id,username,display_name,bio,avatar_url,header_url,also_known_as,profile_fields FROM users WHERE id=$1 AND local=true",
)
.bind(id)
.fetch_optional(&self.pool)
@@ -800,7 +815,7 @@ impl ApUserRepository for PgApUserRepository {
async fn find_by_username(&self, username: &str) -> Result<Option<ApUser>> {
let row = sqlx::query_as::<_, UserRow>(
"SELECT id,username,display_name,bio,avatar_url,header_url,also_known_as FROM users WHERE username=$1 AND local=true",
"SELECT id,username,display_name,bio,avatar_url,header_url,also_known_as,profile_fields FROM users WHERE username=$1 AND local=true",
)
.bind(username)
.fetch_optional(&self.pool)

View File

@@ -105,6 +105,7 @@ fn row_to_entry(r: FeedRow, viewer: Option<uuid::Uuid>) -> Result<FeedEntry, Dom
avatar_url: r.avatar_url,
header_url: r.header_url,
custom_css: r.custom_css,
profile_fields: vec![],
local: r.author_local,
created_at: r.author_created_at,
updated_at: r.author_updated_at,

View File

@@ -0,0 +1 @@
ALTER TABLE users ADD COLUMN IF NOT EXISTS profile_fields JSONB DEFAULT '[]'::jsonb;

View File

@@ -79,6 +79,7 @@ fn row_to_entry(r: FeedRow, viewer: Option<uuid::Uuid>) -> Result<FeedEntry, Dom
avatar_url: r.avatar_url,
header_url: r.header_url,
custom_css: r.custom_css,
profile_fields: vec![],
local: r.author_local,
created_at: r.author_created_at,
updated_at: r.author_updated_at,

View File

@@ -120,7 +120,7 @@ impl FollowRepository for PgFollowRepository {
.into_domain()?;
let rows = sqlx::query_as::<_, crate::user::UserRow>(
"SELECT u.id,u.username,u.email,u.password_hash,u.display_name,u.bio,u.avatar_url,u.header_url,u.custom_css,u.local,u.ap_id,u.inbox_url,u.created_at,u.updated_at
"SELECT u.id,u.username,u.email,u.password_hash,u.display_name,u.bio,u.avatar_url,u.header_url,u.custom_css,u.profile_fields,u.local,u.created_at,u.updated_at
FROM users u JOIN follows f ON f.follower_id=u.id
WHERE f.following_id=$1 AND f.state='accepted'
ORDER BY f.created_at DESC LIMIT $2 OFFSET $3"
@@ -154,7 +154,7 @@ impl FollowRepository for PgFollowRepository {
.into_domain()?;
let rows = sqlx::query_as::<_, crate::user::UserRow>(
"SELECT u.id,u.username,u.email,u.password_hash,u.display_name,u.bio,u.avatar_url,u.header_url,u.custom_css,u.local,u.ap_id,u.inbox_url,u.created_at,u.updated_at
"SELECT u.id,u.username,u.email,u.password_hash,u.display_name,u.bio,u.avatar_url,u.header_url,u.custom_css,u.profile_fields,u.local,u.created_at,u.updated_at
FROM users u JOIN follows f ON f.following_id=u.id
WHERE f.follower_id=$1 AND f.state='accepted'
ORDER BY f.created_at DESC LIMIT $2 OFFSET $3"
@@ -210,7 +210,7 @@ impl FollowRepository for PgFollowRepository {
let rows = sqlx::query_as::<_, crate::user::UserRow>(
"SELECT u.id, u.username, u.email, u.password_hash, u.display_name, u.bio,
u.avatar_url, u.header_url, u.custom_css, u.local,
u.avatar_url, u.header_url, u.custom_css, u.profile_fields, u.local,
u.created_at, u.updated_at
FROM users u
JOIN follows f1

View File

@@ -54,7 +54,7 @@ impl TopFriendRepository for PgTopFriendRepository {
let rows = sqlx::query_as::<_, TopFriendRow>(
"SELECT tf.user_id AS tf_user_id, tf.friend_id, tf.position,
u.id, u.username, u.email, u.password_hash, u.display_name, u.bio,
u.avatar_url, u.header_url, u.custom_css, u.local,
u.avatar_url, u.header_url, u.custom_css, u.profile_fields, u.local,
u.created_at, u.updated_at
FROM top_friends tf JOIN users u ON u.id=tf.friend_id
WHERE tf.user_id=$1 ORDER BY tf.position",

View File

@@ -31,11 +31,26 @@ pub struct UserRow {
pub avatar_url: Option<String>,
pub header_url: Option<String>,
pub custom_css: Option<String>,
pub profile_fields: Option<serde_json::Value>,
pub local: bool,
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
}
fn parse_profile_fields(v: Option<serde_json::Value>) -> Vec<(String, String)> {
v.and_then(|v| v.as_array().cloned())
.map(|arr| {
arr.into_iter()
.filter_map(|item| {
let name = item.get("name")?.as_str()?.to_string();
let value = item.get("value")?.as_str()?.to_string();
Some((name, value))
})
.collect()
})
.unwrap_or_default()
}
impl From<UserRow> for User {
fn from(r: UserRow) -> Self {
User {
@@ -48,6 +63,7 @@ impl From<UserRow> for User {
avatar_url: r.avatar_url,
header_url: r.header_url,
custom_css: r.custom_css,
profile_fields: parse_profile_fields(r.profile_fields),
local: r.local,
created_at: r.created_at,
updated_at: r.updated_at,
@@ -57,7 +73,7 @@ impl From<UserRow> for User {
pub const USER_SELECT: &str =
"SELECT id,username,email,password_hash,display_name,bio,avatar_url,header_url,\
custom_css,local,created_at,updated_at FROM users";
custom_css,profile_fields,local,created_at,updated_at FROM users";
#[async_trait]
impl UserReader for PgUserRepository {
@@ -222,14 +238,20 @@ impl UserReader for PgUserRepository {
#[async_trait]
impl UserWriter for PgUserRepository {
async fn save(&self, user: &User) -> Result<(), DomainError> {
let profile_fields_json: serde_json::Value = user
.profile_fields
.iter()
.map(|(n, v)| serde_json::json!({"name": n, "value": v}))
.collect();
sqlx::query(
"INSERT INTO users (id,username,email,password_hash,display_name,bio,avatar_url,header_url,custom_css,local,created_at,updated_at)
VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12)
"INSERT INTO users (id,username,email,password_hash,display_name,bio,avatar_url,header_url,custom_css,profile_fields,local,created_at,updated_at)
VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13)
ON CONFLICT(id) DO UPDATE SET
username=EXCLUDED.username, email=EXCLUDED.email,
password_hash=EXCLUDED.password_hash, display_name=EXCLUDED.display_name,
bio=EXCLUDED.bio, avatar_url=EXCLUDED.avatar_url,
header_url=EXCLUDED.header_url, custom_css=EXCLUDED.custom_css,
profile_fields=EXCLUDED.profile_fields,
local=EXCLUDED.local,
updated_at=NOW()"
)
@@ -242,6 +264,7 @@ impl UserWriter for PgUserRepository {
.bind(&user.avatar_url)
.bind(&user.header_url)
.bind(&user.custom_css)
.bind(&profile_fields_json)
.bind(user.local)
.bind(user.created_at)
.bind(user.updated_at)
@@ -267,14 +290,22 @@ impl UserWriter for PgUserRepository {
user_id: &UserId,
input: UpdateProfileInput,
) -> Result<(), DomainError> {
let profile_fields_json: Option<serde_json::Value> =
input.profile_fields.as_ref().map(|fields| {
fields
.iter()
.map(|(n, v)| serde_json::json!({"name": n, "value": v}))
.collect()
});
sqlx::query(
"UPDATE users SET \
display_name = COALESCE($2, display_name), \
bio = COALESCE($3, bio), \
avatar_url = COALESCE($4, avatar_url), \
header_url = COALESCE($5, header_url), \
custom_css = COALESCE($6, custom_css), \
updated_at = NOW() \
display_name = COALESCE($2, display_name), \
bio = COALESCE($3, bio), \
avatar_url = COALESCE($4, avatar_url), \
header_url = COALESCE($5, header_url), \
custom_css = COALESCE($6, custom_css), \
profile_fields = COALESCE($7, profile_fields), \
updated_at = NOW() \
WHERE id = $1",
)
.bind(user_id.as_uuid())
@@ -283,6 +314,7 @@ impl UserWriter for PgUserRepository {
.bind(input.avatar_url)
.bind(input.header_url)
.bind(input.custom_css)
.bind(profile_fields_json)
.execute(&self.pool)
.await
.into_domain()

View File

@@ -47,6 +47,7 @@ pub struct UpdateProfileRequest {
pub avatar_url: Option<String>,
pub header_url: Option<String>,
pub custom_css: Option<String>,
pub profile_fields: Option<Vec<crate::responses::ProfileField>>,
}
#[derive(Deserialize, utoipa::ToSchema)]

View File

@@ -1,5 +1,5 @@
use chrono::{DateTime, Utc};
use serde::Serialize;
use serde::{Deserialize, Serialize};
use uuid::Uuid;
#[derive(Serialize, utoipa::ToSchema)]
@@ -19,6 +19,7 @@ pub struct UserResponse {
pub avatar_url: Option<String>,
pub header_url: Option<String>,
pub custom_css: Option<String>,
pub profile_fields: Vec<ProfileField>,
pub local: bool,
pub is_followed_by_viewer: bool,
#[serde(rename = "joinedAt")]
@@ -105,7 +106,7 @@ pub struct CreatedApiKeyResponse {
pub key: String,
}
#[derive(Serialize, Clone, utoipa::ToSchema)]
#[derive(Serialize, Deserialize, Clone, utoipa::ToSchema)]
#[serde(rename_all = "camelCase")]
pub struct ProfileField {
pub name: String,

View File

@@ -73,6 +73,7 @@ impl ActivityPubRepository for TestApRepo {
avatar_url: None,
header_url: None,
custom_css: None,
profile_fields: vec![],
local: false,
created_at: chrono::Utc::now(),
updated_at: chrono::Utc::now(),

View File

@@ -8,6 +8,7 @@ pub struct UpdateProfileInput {
pub avatar_url: Option<String>,
pub header_url: Option<String>,
pub custom_css: Option<String>,
pub profile_fields: Option<Vec<(String, String)>>,
}
#[derive(Debug, Clone)]
@@ -21,6 +22,7 @@ pub struct User {
pub avatar_url: Option<String>,
pub header_url: Option<String>,
pub custom_css: Option<String>,
pub profile_fields: Vec<(String, String)>,
pub local: bool,
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
@@ -44,6 +46,7 @@ impl User {
avatar_url: None,
header_url: None,
custom_css: None,
profile_fields: vec![],
local: true,
created_at: now,
updated_at: now,

View File

@@ -1,7 +1,7 @@
use crate::{deps_struct, errors::ApiError, extractors::Deps};
use api_types::{
requests::{LoginRequest, RegisterRequest},
responses::{AuthResponse, ErrorResponse, UserResponse},
responses::{AuthResponse, ErrorResponse, ProfileField, UserResponse},
};
use application::use_cases::auth::{login, register, LoginInput, RegisterInput};
use axum::{http::StatusCode, response::IntoResponse, Json};
@@ -23,6 +23,14 @@ pub fn to_user_response(u: &domain::models::user::User) -> UserResponse {
avatar_url: u.avatar_url.clone(),
header_url: u.header_url.clone(),
custom_css: u.custom_css.clone(),
profile_fields: u
.profile_fields
.iter()
.map(|(n, v)| ProfileField {
name: n.clone(),
value: v.clone(),
})
.collect(),
local: u.local,
is_followed_by_viewer: false,
created_at: u.created_at,

View File

@@ -114,6 +114,9 @@ pub async fn patch_profile(
avatar_url: body.avatar_url,
header_url: body.header_url,
custom_css: body.custom_css,
profile_fields: body
.profile_fields
.map(|f| f.into_iter().map(|pf| (pf.name, pf.value)).collect()),
},
)
.await?;
@@ -208,6 +211,7 @@ pub async fn get_users(
avatar_url: u.avatar_url.clone(),
header_url: None,
custom_css: None,
profile_fields: vec![],
local: true,
is_followed_by_viewer: false,
created_at: chrono::Utc::now(),