From 70a669a29c101867c7f988ade43c6cb836e2c706 Mon Sep 17 00:00:00 2001 From: Jesse Wierzbinski Date: Thu, 13 Jun 2024 22:19:15 -1000 Subject: [PATCH] docs(api): :memo: Document API parameter to change username --- docs/api/mastodon.md | 9 ++++++++- .../api/api/v1/accounts/update_credentials/index.ts | 13 +++++++++++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/docs/api/mastodon.md b/docs/api/mastodon.md index fb539fc6..d1047edd 100644 --- a/docs/api/mastodon.md +++ b/docs/api/mastodon.md @@ -61,4 +61,11 @@ An extra attribute has been adding to all returned account objects: ### `roles` -An array of roles from [Lysand Roles](./roles.md). \ No newline at end of file +An array of roles from [Lysand Roles](./roles.md). + +### `/api/v1/accounts/update_credentials` + +The `username` parameter can now (optionally) be set to change the user's handle. + +> [!WARNING] +> Clients should indicate to users that changing their handle will break existing links to their profile. This is reversible, but the old handle will be available for anyone to claim. \ No newline at end of file diff --git a/server/api/api/v1/accounts/update_credentials/index.ts b/server/api/api/v1/accounts/update_credentials/index.ts index a5b7ef59..2b664489 100644 --- a/server/api/api/v1/accounts/update_credentials/index.ts +++ b/server/api/api/v1/accounts/update_credentials/index.ts @@ -3,7 +3,7 @@ import { errorResponse, jsonResponse } from "@/response"; import { sanitizedHtmlStrip } from "@/sanitization"; import { zValidator } from "@hono/zod-validator"; import { config } from "config-manager"; -import { and, eq } from "drizzle-orm"; +import { and, eq, isNull } from "drizzle-orm"; import type { Hono } from "hono"; import ISO6391 from "iso-639-1"; import { MediaBackend } from "media-manager"; @@ -12,7 +12,7 @@ import { getUrl } from "~/database/entities/attachment"; import { parseEmojis } from "~/database/entities/emoji"; import { contentToHtml } from "~/database/entities/status"; import { db } from "~/drizzle/db"; -import { EmojiToUser, RolePermissions } from "~/drizzle/schema"; +import { EmojiToUser, RolePermissions, Users } from "~/drizzle/schema"; import type { Emoji } from "~/packages/database-interface/emoji"; import { User } from "~/packages/database-interface/user"; @@ -191,6 +191,15 @@ export default (app: Hono) => } if (username) { + // Check if username is already taken + const existingUser = await User.fromSql( + and(isNull(Users.instanceId), eq(Users.username, username)), + ); + + if (existingUser) { + return errorResponse("Username is already taken", 400); + } + self.username = username; }