docs(api): 📝 Document API parameter to change username

This commit is contained in:
Jesse Wierzbinski 2024-06-13 22:19:15 -10:00
parent cfa0ab4ac9
commit 70a669a29c
No known key found for this signature in database
2 changed files with 19 additions and 3 deletions

View file

@ -62,3 +62,10 @@ An extra attribute has been adding to all returned account objects:
### `roles`
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.

View file

@ -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;
}