feat: repost functionality

This commit is contained in:
P. Reis 2024-04-05 19:44:12 -03:00
parent d688e64219
commit 178a3c4d0e
4 changed files with 51 additions and 6 deletions

View File

@ -62,6 +62,7 @@ import {
favouritedByController,
pinController,
rebloggedByController,
reblogStatusController,
statusController,
unbookmarkController,
unpinController,
@ -167,6 +168,7 @@ app.post('/api/v1/statuses/:id{[0-9a-f]{64}}/unbookmark', requirePubkey, unbookm
app.post('/api/v1/statuses/:id{[0-9a-f]{64}}/pin', requirePubkey, pinController);
app.post('/api/v1/statuses/:id{[0-9a-f]{64}}/unpin', requirePubkey, unpinController);
app.post('/api/v1/statuses/:id{[0-9a-f]{64}}/zap', requirePubkey, zapController);
app.post('/api/v1/statuses/:id{[0-9a-f]{64}}/reblog', requirePubkey, reblogStatusController);
app.post('/api/v1/statuses', requirePubkey, createStatusController);
app.delete('/api/v1/statuses/:id{[0-9a-f]{64}}', requirePubkey, deleteStatusController);

View File

@ -7,7 +7,7 @@ import { jsonMetaContentSchema } from '@/schemas/nostr.ts';
import { addTag, deleteTag } from '@/tags.ts';
import { createEvent, paginationSchema, parseBody, updateListEvent } from '@/utils/api.ts';
import { renderEventAccounts } from '@/views.ts';
import { renderStatus } from '@/views/mastodon/statuses.ts';
import { renderReblog, renderStatus } from '@/views/mastodon/statuses.ts';
import { getLnurl } from '@/utils/lnurl.ts';
const createStatusSchema = z.object({
@ -173,6 +173,31 @@ const favouritedByController: AppController = (c) => {
return renderEventAccounts(c, [{ kinds: [7], '#e': [id], ...params }]);
};
/** https://docs.joinmastodon.org/methods/statuses/#boost */
const reblogStatusController: AppController = async (c) => {
const eventId = c.req.param('id');
const event = await getEvent(eventId, {
kind: 1,
});
if (event == undefined) {
return c.json({ error: 'Event not found.' }, 404);
}
const tags: string[][] = [['e', event.id], ['p', event.pubkey]];
const reblogEvent = await createEvent({
kind: 6,
content: JSON.stringify(event),
tags,
}, c);
const status = await renderReblog(reblogEvent, reblogEvent.pubkey);
return c.json(status);
};
const rebloggedByController: AppController = (c) => {
const id = c.req.param('id');
const params = paginationSchema.parse(c.req.query());
@ -339,6 +364,7 @@ export {
favouriteController,
favouritedByController,
pinController,
reblogStatusController,
rebloggedByController,
statusController,
unbookmarkController,

View File

@ -7,13 +7,13 @@ import { booleanParamSchema } from '@/schema.ts';
import { eventsDB } from '@/storages.ts';
import { hydrateEvents } from '@/storages/hydrate.ts';
import { paginated, paginationSchema } from '@/utils/api.ts';
import { renderStatus } from '@/views/mastodon/statuses.ts';
import { renderReblog, renderStatus } from '@/views/mastodon/statuses.ts';
const homeTimelineController: AppController = async (c) => {
const params = paginationSchema.parse(c.req.query());
const pubkey = c.get('pubkey')!;
const authors = await getFeedPubkeys(pubkey);
return renderStatuses(c, [{ authors, kinds: [1], ...params }]);
return renderStatuses(c, [{ authors, kinds: [1, 6], ...params }]);
};
const publicQuerySchema = z.object({
@ -25,7 +25,7 @@ const publicTimelineController: AppController = (c) => {
const params = paginationSchema.parse(c.req.query());
const { local, instance } = publicQuerySchema.parse(c.req.query());
const filter: NostrFilter = { kinds: [1], ...params };
const filter: NostrFilter = { kinds: [1, 6], ...params };
if (local) {
filter.search = `domain:${Conf.url.host}`;
@ -56,7 +56,12 @@ async function renderStatuses(c: AppContext, filters: NostrFilter[]) {
return c.json([]);
}
const statuses = await Promise.all(events.map((event) => renderStatus(event, c.get('pubkey'))));
const statuses = await Promise.all(events.map((event) => {
if (event.kind == 6) {
return renderReblog(event, c.get('pubkey'));
}
return renderStatus(event, c.get('pubkey'));
}));
return paginated(c, events, statuses);
}

View File

@ -98,6 +98,18 @@ async function renderStatus(event: DittoEvent, viewerPubkey?: string) {
};
}
async function renderReblog(event: DittoEvent, viewerPubkey?: string) {
if (event.author == undefined) return;
const reblog = await renderStatus(JSON.parse(event.content), viewerPubkey);
return {
id: event.id,
account: await renderAccount(event.author),
reblogged: true,
reblog,
};
}
async function toMention(pubkey: string) {
const author = await getAuthor(pubkey);
const account = author ? await renderAccount(author) : undefined;
@ -134,4 +146,4 @@ function buildInlineRecipients(mentions: Mention[]): string {
return `<span class="recipients-inline">${elements.join(' ')} </span>`;
}
export { renderStatus };
export { renderStatus, renderReblog };