Refactor transmute, fix account in status

This commit is contained in:
Alex Gleason 2023-03-05 00:16:05 -06:00
parent 7da1fed11e
commit cde34bffd8
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
3 changed files with 44 additions and 32 deletions

View File

@ -1,6 +1,5 @@
import { LOCAL_DOMAIN } from '../config.ts';
import { fetchUser } from '../client.ts'; import { fetchUser } from '../client.ts';
import { MetaContent, metaContentSchema } from '../schema.ts'; import { toAccount } from '../transmute.ts';
import { getKeys } from '../utils.ts'; import { getKeys } from '../utils.ts';
import type { Context } from '@/deps.ts'; import type { Context } from '@/deps.ts';
@ -11,32 +10,9 @@ async function credentialsController(c: Context) {
if (keys) { if (keys) {
const { pubkey } = keys; const { pubkey } = keys;
const event = await fetchUser(pubkey); const event = await fetchUser(pubkey);
const parsed = metaContentSchema.safeParse(JSON.parse(event?.content || '')); if (event) {
const content: MetaContent = parsed.success ? parsed.data : {}; return c.json(toAccount(event));
const { host, origin } = new URL(LOCAL_DOMAIN); }
return c.json({
id: pubkey,
acct: pubkey,
avatar: content.picture,
avatar_static: content.picture,
bot: false,
created_at: event ? new Date(event.created_at * 1000).toISOString() : new Date().toISOString(),
display_name: content.name,
emojis: [],
fields: [],
follow_requests_count: 0,
followers_count: 0,
following_count: 0,
statuses_count: 0,
header: content.banner,
header_static: content.banner,
locked: false,
note: content.about,
fqn: `${pubkey}@${host}`,
url: `${origin}/users/${pubkey}`,
username: pubkey,
});
} }
return c.json({ error: 'Invalid token' }, 400); return c.json({ error: 'Invalid token' }, 400);

View File

@ -1,7 +1,8 @@
import { validator, z } from '@/deps.ts'; import { validator, z } from '@/deps.ts';
import { fetchUser } from '../client.ts';
import publish from '../publisher.ts'; import publish from '../publisher.ts';
import { toStatus } from '../transmute.ts'; import { toAccount, toStatus } from '../transmute.ts';
import { getKeys } from '../utils.ts'; import { getKeys } from '../utils.ts';
import type { Event } from '../event.ts'; import type { Event } from '../event.ts';
@ -10,7 +11,7 @@ const createStatusSchema = z.object({
status: z.string(), status: z.string(),
}); });
const createStatusController = validator('json', (value, c) => { const createStatusController = validator('json', async (value, c) => {
const keys = getKeys(c); const keys = getKeys(c);
const result = createStatusSchema.safeParse(value); const result = createStatusSchema.safeParse(value);
@ -27,7 +28,11 @@ const createStatusController = validator('json', (value, c) => {
}; };
publish(event, privatekey); publish(event, privatekey);
return c.json(toStatus(event));
return c.json({
...toStatus(event),
account: toAccount((await fetchUser(pubkey))!),
});
} else { } else {
return c.json({ error: 'Bad request' }, 400); return c.json({ error: 'Bad request' }, 400);
} }

View File

@ -1,7 +1,38 @@
import { LOCAL_DOMAIN } from './config.ts'; import { LOCAL_DOMAIN } from './config.ts';
import { MetaContent, metaContentSchema } from './schema.ts';
import type { Event } from './event.ts'; import type { Event } from './event.ts';
function toAccount(event: Event<0>) {
const { pubkey } = event;
const parsed = metaContentSchema.safeParse(JSON.parse(event?.content || ''));
const content: MetaContent = parsed.success ? parsed.data : {};
const { host, origin } = new URL(LOCAL_DOMAIN);
return {
id: pubkey,
acct: pubkey,
avatar: content.picture,
avatar_static: content.picture,
bot: false,
created_at: event ? new Date(event.created_at * 1000).toISOString() : new Date().toISOString(),
display_name: content.name,
emojis: [],
fields: [],
follow_requests_count: 0,
followers_count: 0,
following_count: 0,
statuses_count: 0,
header: content.banner,
header_static: content.banner,
locked: false,
note: content.about,
fqn: `${pubkey}@${host}`,
url: `${origin}/users/${pubkey}`,
username: pubkey,
};
}
function toStatus(event: Event<1>) { function toStatus(event: Event<1>) {
return { return {
id: event.id, id: event.id,
@ -36,4 +67,4 @@ function toStatus(event: Event<1>) {
}; };
} }
export { toStatus }; export { toAccount, toStatus };