feat(api): Add Lysand roles to user accounts

This commit is contained in:
Jesse Wierzbinski 2024-06-11 14:29:59 -10:00
parent 4f2c98390c
commit efe202ea27
No known key found for this signature in database
6 changed files with 76 additions and 1 deletions

View file

@ -44,4 +44,21 @@ Single Sign-On (SSO) settings for the instance. This object contains two fields:
## `/api/v2/instance` ## `/api/v2/instance`
Contains the same extensions as `/api/v1/instance`, except `banner` which uses the normal Mastodon API attribute. Contains the same extensions as `/api/v1/instance`, except `banner` which uses the normal Mastodon API attribute.
## `Account`
(`/api/v1/accounts/:id`, `/api/v1/accounts/verify_credentials`, ...)
An extra attribute has been adding to all returned account objects:
```ts
{
// ...
roles: LysandRoles[];
}
```
### `roles`
An array of roles from [Lysand Roles](./roles.md).

View file

@ -14,6 +14,10 @@ import { RoleToUsers, Roles } from "~/drizzle/schema";
export class Role { export class Role {
private constructor(private role: InferSelectModel<typeof Roles>) {} private constructor(private role: InferSelectModel<typeof Roles>) {}
public static fromRole(role: InferSelectModel<typeof Roles>) {
return new Role(role);
}
public static async fromId(id: string | null): Promise<Role | null> { public static async fromId(id: string | null): Promise<Role | null> {
if (!id) return null; if (!id) return null;

View file

@ -43,6 +43,7 @@ import { type Config, config } from "~/packages/config-manager";
import type { Account as APIAccount } from "~/types/mastodon/account"; import type { Account as APIAccount } from "~/types/mastodon/account";
import type { Mention as APIMention } from "~/types/mastodon/mention"; import type { Mention as APIMention } from "~/types/mastodon/mention";
import type { Note } from "./note"; import type { Note } from "./note";
import { Role } from "./role";
/** /**
* Gives helpers to fetch users from database in a nice format * Gives helpers to fetch users from database in a nice format
@ -602,6 +603,36 @@ export class User {
suspended: false, suspended: false,
discoverable: undefined, discoverable: undefined,
mute_expires_at: undefined, mute_expires_at: undefined,
roles: user.roles
.map((role) => Role.fromRole(role))
.concat(
Role.fromRole({
id: "default",
name: "Default",
permissions: config.permissions.default,
priority: 0,
description: "Default role for all users",
visible: false,
icon: null,
}),
)
.concat(
user.isAdmin
? [
Role.fromRole({
id: "admin",
name: "Admin",
permissions: config.permissions.admin,
priority: 2 ** 31 - 1,
description:
"Default role for all administrators",
visible: false,
icon: null,
}),
]
: [],
)
.map((r) => r.toAPI()),
group: false, group: false,
}; };
} }

View file

@ -83,6 +83,16 @@ describe(meta.route, () => {
limited: false, limited: false,
noindex: false, noindex: false,
suspended: false, suspended: false,
roles: expect.arrayContaining([
expect.objectContaining({
id: "default",
name: "Default",
priority: 0,
description: "Default role for all users",
visible: false,
icon: null,
}),
]),
} satisfies APIAccount); } satisfies APIAccount);
}); });
}); });

View file

@ -1,5 +1,6 @@
import type { Emoji } from "./emoji"; import type { Emoji } from "./emoji";
import type { Field } from "./field"; import type { Field } from "./field";
import type { LysandRole } from "./lysand";
import type { Role } from "./role"; import type { Role } from "./role";
import type { Source } from "./source"; import type { Source } from "./source";
@ -30,5 +31,6 @@ export type Account = {
bot: boolean | null; bot: boolean | null;
source?: Source; source?: Source;
role?: Role; role?: Role;
roles: LysandRole[];
mute_expires_at?: string; mute_expires_at?: string;
}; };

11
types/mastodon/lysand.ts Normal file
View file

@ -0,0 +1,11 @@
import type { RolePermissions } from "~/drizzle/schema";
export type LysandRole = {
id: string;
name: string;
permissions: RolePermissions[];
priority: number;
description: string | null;
visible: boolean;
icon: string | null;
};