Merge branch 'repost-feat' into 'main'

feat: repost functionality

See merge request soapbox-pub/ditto!133
This commit is contained in:
Alex Gleason 2024-04-08 15:54:43 +00:00
commit 70a29a74fb
6 changed files with 74 additions and 13 deletions

View File

@ -62,6 +62,7 @@ import {
favouritedByController, favouritedByController,
pinController, pinController,
rebloggedByController, rebloggedByController,
reblogStatusController,
statusController, statusController,
unbookmarkController, unbookmarkController,
unpinController, 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}}/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}}/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}}/zap', requirePubkey, zapController);
app.post('/api/v1/statuses/:id{[0-9a-f]{64}}/reblog', requirePubkey, reblogStatusController);
app.post('/api/v1/statuses', requirePubkey, createStatusController); app.post('/api/v1/statuses', requirePubkey, createStatusController);
app.delete('/api/v1/statuses/:id{[0-9a-f]{64}}', requirePubkey, deleteStatusController); 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 { addTag, deleteTag } from '@/tags.ts';
import { createEvent, paginationSchema, parseBody, updateListEvent } from '@/utils/api.ts'; import { createEvent, paginationSchema, parseBody, updateListEvent } from '@/utils/api.ts';
import { renderEventAccounts } from '@/views.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'; import { getLnurl } from '@/utils/lnurl.ts';
import { nip05Cache } from '@/utils/nip05.ts'; import { nip05Cache } from '@/utils/nip05.ts';
import { asyncReplaceAll } from '@/utils/text.ts'; import { asyncReplaceAll } from '@/utils/text.ts';
@ -203,6 +203,28 @@ const favouritedByController: AppController = (c) => {
return renderEventAccounts(c, [{ kinds: [7], '#e': [id], ...params }]); 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) {
return c.json({ error: 'Event not found.' }, 404);
}
const reblogEvent = await createEvent({
kind: 6,
tags: [['e', event.id], ['p', event.pubkey]],
}, c);
const status = await renderReblog(reblogEvent);
return c.json(status);
};
const rebloggedByController: AppController = (c) => { const rebloggedByController: AppController = (c) => {
const id = c.req.param('id'); const id = c.req.param('id');
const params = paginationSchema.parse(c.req.query()); const params = paginationSchema.parse(c.req.query());
@ -370,6 +392,7 @@ export {
favouritedByController, favouritedByController,
pinController, pinController,
rebloggedByController, rebloggedByController,
reblogStatusController,
statusController, statusController,
unbookmarkController, unbookmarkController,
unpinController, unpinController,

View File

@ -5,7 +5,7 @@ import { Debug, z } from '@/deps.ts';
import { getFeedPubkeys } from '@/queries.ts'; import { getFeedPubkeys } from '@/queries.ts';
import { Sub } from '@/subs.ts'; import { Sub } from '@/subs.ts';
import { bech32ToPubkey } from '@/utils.ts'; import { bech32ToPubkey } from '@/utils.ts';
import { renderStatus } from '@/views/mastodon/statuses.ts'; import { renderReblog, renderStatus } from '@/views/mastodon/statuses.ts';
const debug = Debug('ditto:streaming'); const debug = Debug('ditto:streaming');
@ -63,6 +63,13 @@ const streamingController: AppController = (c) => {
if (filter) { if (filter) {
for await (const event of Sub.sub(socket, '1', [filter])) { for await (const event of Sub.sub(socket, '1', [filter])) {
if (event.kind === 6) {
const status = await renderReblog(event);
if (status) {
send('update', status);
}
return;
}
const status = await renderStatus(event, pubkey); const status = await renderStatus(event, pubkey);
if (status) { if (status) {
send('update', status); send('update', status);
@ -87,20 +94,20 @@ async function topicToFilter(
switch (topic) { switch (topic) {
case 'public': case 'public':
return { kinds: [1] }; return { kinds: [1, 6] };
case 'public:local': case 'public:local':
return { kinds: [1], search: `domain:${host}` }; return { kinds: [1, 6], search: `domain:${host}` };
case 'hashtag': case 'hashtag':
if (query.tag) return { kinds: [1], '#t': [query.tag] }; if (query.tag) return { kinds: [1, 6], '#t': [query.tag] };
break; break;
case 'hashtag:local': case 'hashtag:local':
if (query.tag) return { kinds: [1], '#t': [query.tag], search: `domain:${host}` }; if (query.tag) return { kinds: [1, 6], '#t': [query.tag], search: `domain:${host}` };
break; break;
case 'user': case 'user':
// HACK: this puts the user's entire contacts list into RAM, // HACK: this puts the user's entire contacts list into RAM,
// and then calls `matchFilters` over it. Refreshing the page // and then calls `matchFilters` over it. Refreshing the page
// is required after following a new user. // is required after following a new user.
return pubkey ? { kinds: [1], authors: await getFeedPubkeys(pubkey) } : undefined; return pubkey ? { kinds: [1, 6], authors: await getFeedPubkeys(pubkey) } : undefined;
} }
} }

View File

@ -7,13 +7,13 @@ import { booleanParamSchema } from '@/schema.ts';
import { eventsDB } from '@/storages.ts'; import { eventsDB } from '@/storages.ts';
import { hydrateEvents } from '@/storages/hydrate.ts'; import { hydrateEvents } from '@/storages/hydrate.ts';
import { paginated, paginationSchema } from '@/utils/api.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 homeTimelineController: AppController = async (c) => {
const params = paginationSchema.parse(c.req.query()); const params = paginationSchema.parse(c.req.query());
const pubkey = c.get('pubkey')!; const pubkey = c.get('pubkey')!;
const authors = await getFeedPubkeys(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({ const publicQuerySchema = z.object({
@ -25,7 +25,7 @@ const publicTimelineController: AppController = (c) => {
const params = paginationSchema.parse(c.req.query()); const params = paginationSchema.parse(c.req.query());
const { local, instance } = publicQuerySchema.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) { if (local) {
filter.search = `domain:${Conf.url.host}`; filter.search = `domain:${Conf.url.host}`;
@ -56,7 +56,17 @@ async function renderStatuses(c: AppContext, filters: NostrFilter[]) {
return c.json([]); 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);
}
return renderStatus(event, c.get('pubkey'));
}))).filter((boolean) => boolean);
if (!statuses.length) {
return c.json([]);
}
return paginated(c, events, statuses); return paginated(c, events, statuses);
} }

View File

@ -22,4 +22,5 @@ export interface DittoEvent extends NostrEvent {
event_stats?: EventStats; event_stats?: EventStats;
d_author?: DittoEvent; d_author?: DittoEvent;
user?: DittoEvent; user?: DittoEvent;
repost?: NostrEvent;
} }

View File

@ -4,7 +4,7 @@ import { Conf } from '@/config.ts';
import { nip19 } from '@/deps.ts'; import { nip19 } from '@/deps.ts';
import { type DittoEvent } from '@/interfaces/DittoEvent.ts'; import { type DittoEvent } from '@/interfaces/DittoEvent.ts';
import { getMediaLinks, parseNoteContent } from '@/note.ts'; import { getMediaLinks, parseNoteContent } from '@/note.ts';
import { getAuthor } from '@/queries.ts'; import { getAuthor, getEvent } from '@/queries.ts';
import { jsonMediaDataSchema } from '@/schemas/nostr.ts'; import { jsonMediaDataSchema } from '@/schemas/nostr.ts';
import { eventsDB } from '@/storages.ts'; import { eventsDB } from '@/storages.ts';
import { findReplyTag } from '@/tags.ts'; import { findReplyTag } from '@/tags.ts';
@ -100,6 +100,24 @@ async function renderStatus(event: DittoEvent, viewerPubkey?: string) {
}; };
} }
async function renderReblog(event: DittoEvent) {
if (!event.author) return;
const repostId = event.tags.find(([name]) => name === 'p')?.[1];
event.repost = await getEvent(repostId, { kind: 1 });
if (!event.repost) return;
const reblog = await renderStatus(event.repost);
reblog.reblogged = true;
return {
id: event.id,
account: await renderAccount(event.author),
reblogged: true,
reblog,
};
}
async function toMention(pubkey: string) { async function toMention(pubkey: string) {
const author = await getAuthor(pubkey); const author = await getAuthor(pubkey);
const account = author ? await renderAccount(author) : undefined; const account = author ? await renderAccount(author) : undefined;
@ -136,4 +154,4 @@ function buildInlineRecipients(mentions: Mention[]): string {
return `<span class="recipients-inline">${elements.join(' ')} </span>`; return `<span class="recipients-inline">${elements.join(' ')} </span>`;
} }
export { renderStatus }; export { renderReblog, renderStatus };