public profile editing

This commit is contained in:
Moon.eth
2026-07-29 19:51:59 +09:00
parent 8f0d47b850
commit 17f385c1c3
9 changed files with 795 additions and 14 deletions
+58 -10
View File
@@ -86,6 +86,63 @@ export function verifyCredentials(api: ApiClient): Promise<CredentialAccount> {
return api.get<CredentialAccount>('/api/v1/accounts/verify_credentials')
}
export interface PublicProfileUpdate {
displayName?: string
note?: string
fields?: Array<{ name: string; value: string }>
avatar?: File | '' | undefined
header?: File | '' | undefined
background?: File | '' | undefined
avatarDescription?: string
headerDescription?: string
bot?: boolean
actorType?: 'Person' | 'Service' | 'Group'
birthday?: string
showBirthday?: boolean
}
/** Update only public-facing account profile data through the shared API. */
export async function updatePublicProfile(
api: ApiClient,
update: PublicProfileUpdate,
): Promise<CredentialAccount> {
const form = new FormData()
if (update.displayName !== undefined) form.set('display_name', update.displayName)
if (update.note !== undefined) form.set('note', update.note)
if (update.avatar !== undefined) form.set('avatar', update.avatar)
if (update.header !== undefined) form.set('header', update.header)
if (update.background !== undefined) form.set('pleroma_background_image', update.background)
if (update.avatarDescription !== undefined) {
form.set('avatar_description', update.avatarDescription)
}
if (update.headerDescription !== undefined) {
form.set('header_description', update.headerDescription)
}
if (update.bot !== undefined) form.set('bot', String(update.bot))
if (update.actorType !== undefined) form.set('actor_type', update.actorType)
if (update.birthday !== undefined) form.set('birthday', update.birthday)
if (update.showBirthday !== undefined) form.set('show_birthday', String(update.showBirthday))
// An omitted collection means "leave fields unchanged". To explicitly clear
// every field, submit one empty row; Pleroma and Mastodon both discard it
// while still recognizing that fields_attributes was present.
const submittedFields =
update.fields === undefined
? undefined
: update.fields.length > 0
? update.fields
: [{ name: '', value: '' }]
submittedFields?.forEach((field, index) => {
form.append(`fields_attributes[${index}][name]`, field.name)
form.append(`fields_attributes[${index}][value]`, field.value)
})
const { data } = await api.raw<CredentialAccount>('/api/v1/accounts/update_credentials', {
method: 'PATCH',
form,
})
return data
}
/**
* Replace the signed-in account's profile fields while leaving every other
* credential untouched. Mastodon and Pleroma both accept this indexed
@@ -95,16 +152,7 @@ export async function updateProfileFields(
api: ApiClient,
fields: Array<{ name: string; value: string }>,
): Promise<CredentialAccount> {
const form = new FormData()
fields.forEach((field, index) => {
form.append(`fields_attributes[${index}][name]`, field.name)
form.append(`fields_attributes[${index}][value]`, field.value)
})
const { data } = await api.raw<CredentialAccount>('/api/v1/accounts/update_credentials', {
method: 'PATCH',
form,
})
return data
return updatePublicProfile(api, { fields })
}
export function fetchAccount(api: ApiClient, id: string): Promise<Account> {