Support admin accounts endpoint (first pass)

This commit is contained in:
Alex Gleason 2024-01-05 15:35:55 -06:00
parent 714391b807
commit 5bd03bdcaa
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
5 changed files with 59 additions and 2 deletions

View File

@ -35,6 +35,7 @@ import {
updateCredentialsController,
verifyCredentialsController,
} from './controllers/api/accounts.ts';
import { adminAccountsController } from './controllers/api/admin.ts';
import { appCredentialsController, createAppController } from './controllers/api/apps.ts';
import { blocksController } from './controllers/api/blocks.ts';
import { bookmarksController } from './controllers/api/bookmarks.ts';
@ -185,6 +186,7 @@ app.get('/api/v1/favourites', requirePubkey, favouritesController);
app.get('/api/v1/bookmarks', requirePubkey, bookmarksController);
app.get('/api/v1/blocks', requirePubkey, blocksController);
app.get('/api/v1/admin/accounts', adminAccountsController);
app.post('/api/v1/pleroma/admin/config', requireRole('admin'), updateConfigController);
// Not (yet) implemented.

View File

@ -0,0 +1,23 @@
import { type AppController } from '@/app.ts';
import { Conf } from '@/config.ts';
import { eventsDB } from '@/storages.ts';
import { renderAdminAccount } from '@/views/mastodon/admin-accounts.ts';
const adminAccountsController: AppController = async (c) => {
const events = await eventsDB.getEvents([{ kinds: [30361], authors: [Conf.pubkey], limit: 20 }]);
const pubkeys = events.map((event) => event.tags.find(([name]) => name === 'd')?.[1]!);
const authors = await eventsDB.getEvents([{ kinds: [0], ids: pubkeys, limit: pubkeys.length }]);
for (const event of events) {
const d = event.tags.find(([name]) => name === 'd')?.[1];
event.d_author = authors.find((author) => author.pubkey === d);
}
return c.json(
await Promise.all(
events.map((event) => renderAdminAccount(event)),
),
);
};
export { adminAccountsController };

View File

@ -29,6 +29,8 @@ interface DittoEvent<K extends number = number> extends Event<K> {
author?: DittoEvent<0>;
author_stats?: AuthorStats;
event_stats?: EventStats;
d_author?: DittoEvent<0>;
user?: DittoEvent<30361>;
}
/** Storage interface for Nostr events. */

View File

@ -12,7 +12,7 @@ interface ToAccountOpts {
}
async function renderAccount(
event: Omit<NonNullable<DittoEvent['author']>, 'id' | 'sig'>,
event: Omit<DittoEvent<0>, 'id' | 'sig'>,
opts: ToAccountOpts = {},
) {
const { withSource = false } = opts;
@ -39,7 +39,7 @@ async function renderAccount(
avatar: picture,
avatar_static: picture,
bot: false,
created_at: nostrDate(event.created_at).toISOString(),
created_at: user ? user.inserted_at.toISOString() : nostrDate(event.created_at).toISOString(),
discoverable: true,
display_name: name,
emojis: renderEmojis(event),

View File

@ -0,0 +1,30 @@
import { DittoEvent } from '@/storages/types.ts';
import { nostrDate } from '@/utils.ts';
import { accountFromPubkey, renderAccount } from './accounts.ts';
async function renderAdminAccount(event: DittoEvent<30361>) {
const d = event.tags.find(([name]) => name === 'd')?.[1]!;
const account = event.d_author ? await renderAccount({ ...event.d_author, user: event }) : await accountFromPubkey(d);
return {
id: account.id,
username: event.tags.find(([name]) => name === 'name')?.[1]!,
domain: account.acct.split('@')[1] || null,
created_at: nostrDate(event.created_at).toISOString(),
email: '',
ip: null,
ips: [],
locale: '',
invite_request: null,
role: event.tags.find(([name]) => name === 'role')?.[1] || 'user',
confirmed: true,
approved: true,
disabled: false,
silenced: false,
suspended: false,
account,
};
}
export { renderAdminAccount };