Merge branch 'bookmark' into 'main'
Support Bookmarks See merge request soapbox-pub/ditto!92
This commit is contained in:
commit
2d249ff20e
|
@ -37,6 +37,7 @@ import {
|
||||||
} from './controllers/api/accounts.ts';
|
} from './controllers/api/accounts.ts';
|
||||||
import { appCredentialsController, createAppController } from './controllers/api/apps.ts';
|
import { appCredentialsController, createAppController } from './controllers/api/apps.ts';
|
||||||
import { blocksController } from './controllers/api/blocks.ts';
|
import { blocksController } from './controllers/api/blocks.ts';
|
||||||
|
import { bookmarksController } from './controllers/api/bookmarks.ts';
|
||||||
import { emptyArrayController, emptyObjectController, notImplementedController } from './controllers/api/fallback.ts';
|
import { emptyArrayController, emptyObjectController, notImplementedController } from './controllers/api/fallback.ts';
|
||||||
import { instanceController } from './controllers/api/instance.ts';
|
import { instanceController } from './controllers/api/instance.ts';
|
||||||
import { mediaController } from './controllers/api/media.ts';
|
import { mediaController } from './controllers/api/media.ts';
|
||||||
|
@ -47,12 +48,14 @@ import { preferencesController } from './controllers/api/preferences.ts';
|
||||||
import { relayController } from './controllers/nostr/relay.ts';
|
import { relayController } from './controllers/nostr/relay.ts';
|
||||||
import { searchController } from './controllers/api/search.ts';
|
import { searchController } from './controllers/api/search.ts';
|
||||||
import {
|
import {
|
||||||
|
bookmarkController,
|
||||||
contextController,
|
contextController,
|
||||||
createStatusController,
|
createStatusController,
|
||||||
favouriteController,
|
favouriteController,
|
||||||
favouritedByController,
|
favouritedByController,
|
||||||
rebloggedByController,
|
rebloggedByController,
|
||||||
statusController,
|
statusController,
|
||||||
|
unbookmarkController,
|
||||||
} from './controllers/api/statuses.ts';
|
} from './controllers/api/statuses.ts';
|
||||||
import { streamingController } from './controllers/api/streaming.ts';
|
import { streamingController } from './controllers/api/streaming.ts';
|
||||||
import {
|
import {
|
||||||
|
@ -152,7 +155,9 @@ app.get('/api/v1/statuses/:id{[0-9a-f]{64}}/favourited_by', favouritedByControll
|
||||||
app.get('/api/v1/statuses/:id{[0-9a-f]{64}}/reblogged_by', rebloggedByController);
|
app.get('/api/v1/statuses/:id{[0-9a-f]{64}}/reblogged_by', rebloggedByController);
|
||||||
app.get('/api/v1/statuses/:id{[0-9a-f]{64}}/context', contextController);
|
app.get('/api/v1/statuses/:id{[0-9a-f]{64}}/context', contextController);
|
||||||
app.get('/api/v1/statuses/:id{[0-9a-f]{64}}', statusController);
|
app.get('/api/v1/statuses/:id{[0-9a-f]{64}}', statusController);
|
||||||
app.post('/api/v1/statuses/:id{[0-9a-f]{64}}/favourite', favouriteController);
|
app.post('/api/v1/statuses/:id{[0-9a-f]{64}}/favourite', requirePubkey, favouriteController);
|
||||||
|
app.post('/api/v1/statuses/:id{[0-9a-f]{64}}/bookmark', requirePubkey, bookmarkController);
|
||||||
|
app.post('/api/v1/statuses/:id{[0-9a-f]{64}}/unbookmark', requirePubkey, unbookmarkController);
|
||||||
app.post('/api/v1/statuses', requirePubkey, createStatusController);
|
app.post('/api/v1/statuses', requirePubkey, createStatusController);
|
||||||
|
|
||||||
app.post('/api/v1/media', requireRole('user', { validatePayload: false }), mediaController);
|
app.post('/api/v1/media', requireRole('user', { validatePayload: false }), mediaController);
|
||||||
|
@ -173,12 +178,12 @@ app.get('/api/v1/trends', cache({ cacheName: 'web', expires: Time.minutes(15) })
|
||||||
|
|
||||||
app.get('/api/v1/notifications', requirePubkey, notificationsController);
|
app.get('/api/v1/notifications', requirePubkey, notificationsController);
|
||||||
app.get('/api/v1/favourites', requirePubkey, favouritesController);
|
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/blocks', requirePubkey, blocksController);
|
||||||
|
|
||||||
app.post('/api/v1/pleroma/admin/config', requireRole('admin'), updateConfigController);
|
app.post('/api/v1/pleroma/admin/config', requireRole('admin'), updateConfigController);
|
||||||
|
|
||||||
// Not (yet) implemented.
|
// Not (yet) implemented.
|
||||||
app.get('/api/v1/bookmarks', emptyArrayController);
|
|
||||||
app.get('/api/v1/custom_emojis', emptyArrayController);
|
app.get('/api/v1/custom_emojis', emptyArrayController);
|
||||||
app.get('/api/v1/filters', emptyArrayController);
|
app.get('/api/v1/filters', emptyArrayController);
|
||||||
app.get('/api/v1/mutes', emptyArrayController);
|
app.get('/api/v1/mutes', emptyArrayController);
|
||||||
|
|
|
@ -0,0 +1,22 @@
|
||||||
|
import { type AppController } from '@/app.ts';
|
||||||
|
import { eventsDB } from '@/db/events.ts';
|
||||||
|
import { getTagSet } from '@/tags.ts';
|
||||||
|
import { renderStatuses } from '@/views.ts';
|
||||||
|
|
||||||
|
/** https://docs.joinmastodon.org/methods/bookmarks/#get */
|
||||||
|
const bookmarksController: AppController = async (c) => {
|
||||||
|
const pubkey = c.get('pubkey')!;
|
||||||
|
|
||||||
|
const [event10003] = await eventsDB.getEvents([
|
||||||
|
{ kinds: [10003], authors: [pubkey], limit: 1 },
|
||||||
|
]);
|
||||||
|
|
||||||
|
if (event10003) {
|
||||||
|
const eventIds = getTagSet(event10003.tags, 'e');
|
||||||
|
return renderStatuses(c, [...eventIds].reverse());
|
||||||
|
} else {
|
||||||
|
return c.json([]);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export { bookmarksController };
|
|
@ -2,7 +2,8 @@ import { type AppController } from '@/app.ts';
|
||||||
import { getUnattachedMediaByIds } from '@/db/unattached-media.ts';
|
import { getUnattachedMediaByIds } from '@/db/unattached-media.ts';
|
||||||
import { type Event, ISO6391, z } from '@/deps.ts';
|
import { type Event, ISO6391, z } from '@/deps.ts';
|
||||||
import { getAncestors, getAuthor, getDescendants, getEvent } from '@/queries.ts';
|
import { getAncestors, getAuthor, getDescendants, getEvent } from '@/queries.ts';
|
||||||
import { createEvent, paginationSchema, parseBody } from '@/utils/web.ts';
|
import { addTag, deleteTag } from '@/tags.ts';
|
||||||
|
import { createEvent, paginationSchema, parseBody, updateListEvent } from '@/utils/web.ts';
|
||||||
import { renderEventAccounts } from '@/views.ts';
|
import { renderEventAccounts } from '@/views.ts';
|
||||||
import { renderStatus } from '@/views/mastodon/statuses.ts';
|
import { renderStatus } from '@/views/mastodon/statuses.ts';
|
||||||
|
|
||||||
|
@ -152,11 +153,67 @@ const rebloggedByController: AppController = (c) => {
|
||||||
return renderEventAccounts(c, [{ kinds: [6], '#e': [id], ...params }]);
|
return renderEventAccounts(c, [{ kinds: [6], '#e': [id], ...params }]);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/** https://docs.joinmastodon.org/methods/statuses/#bookmark */
|
||||||
|
const bookmarkController: AppController = async (c) => {
|
||||||
|
const pubkey = c.get('pubkey')!;
|
||||||
|
const eventId = c.req.param('id');
|
||||||
|
|
||||||
|
const event = await getEvent(eventId, {
|
||||||
|
kind: 1,
|
||||||
|
relations: ['author', 'event_stats', 'author_stats'],
|
||||||
|
});
|
||||||
|
|
||||||
|
if (event) {
|
||||||
|
await updateListEvent(
|
||||||
|
{ kinds: [10003], authors: [pubkey] },
|
||||||
|
(tags) => addTag(tags, ['e', eventId]),
|
||||||
|
c,
|
||||||
|
);
|
||||||
|
|
||||||
|
const status = await renderStatus(event, pubkey);
|
||||||
|
if (status) {
|
||||||
|
status.bookmarked = true;
|
||||||
|
}
|
||||||
|
return c.json(status);
|
||||||
|
} else {
|
||||||
|
return c.json({ error: 'Event not found.' }, 404);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/** https://docs.joinmastodon.org/methods/statuses/#unbookmark */
|
||||||
|
const unbookmarkController: AppController = async (c) => {
|
||||||
|
const pubkey = c.get('pubkey')!;
|
||||||
|
const eventId = c.req.param('id');
|
||||||
|
|
||||||
|
const event = await getEvent(eventId, {
|
||||||
|
kind: 1,
|
||||||
|
relations: ['author', 'event_stats', 'author_stats'],
|
||||||
|
});
|
||||||
|
|
||||||
|
if (event) {
|
||||||
|
await updateListEvent(
|
||||||
|
{ kinds: [10003], authors: [pubkey] },
|
||||||
|
(tags) => deleteTag(tags, ['e', eventId]),
|
||||||
|
c,
|
||||||
|
);
|
||||||
|
|
||||||
|
const status = await renderStatus(event, pubkey);
|
||||||
|
if (status) {
|
||||||
|
status.bookmarked = false;
|
||||||
|
}
|
||||||
|
return c.json(status);
|
||||||
|
} else {
|
||||||
|
return c.json({ error: 'Event not found.' }, 404);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
export {
|
export {
|
||||||
|
bookmarkController,
|
||||||
contextController,
|
contextController,
|
||||||
createStatusController,
|
createStatusController,
|
||||||
favouriteController,
|
favouriteController,
|
||||||
favouritedByController,
|
favouritedByController,
|
||||||
rebloggedByController,
|
rebloggedByController,
|
||||||
statusController,
|
statusController,
|
||||||
|
unbookmarkController,
|
||||||
};
|
};
|
||||||
|
|
|
@ -20,7 +20,7 @@ type TagCondition = ({ event, count, value }: {
|
||||||
/** Conditions for when to index certain tags. */
|
/** Conditions for when to index certain tags. */
|
||||||
const tagConditions: Record<string, TagCondition> = {
|
const tagConditions: Record<string, TagCondition> = {
|
||||||
'd': ({ event, count }) => count === 0 && isParameterizedReplaceableKind(event.kind),
|
'd': ({ event, count }) => count === 0 && isParameterizedReplaceableKind(event.kind),
|
||||||
'e': ({ count, value }) => count < 15 && isNostrId(value),
|
'e': ({ event, count, value, opts }) => ((opts.data?.user && event.kind === 10003) || count < 15) && isNostrId(value),
|
||||||
'media': ({ count, value, opts }) => (opts.data?.user || count < 4) && isURL(value),
|
'media': ({ count, value, opts }) => (opts.data?.user || count < 4) && isURL(value),
|
||||||
'p': ({ event, count, value }) => (count < 15 || event.kind === 3) && isNostrId(value),
|
'p': ({ event, count, value }) => (count < 15 || event.kind === 3) && isNostrId(value),
|
||||||
'proxy': ({ count, value }) => count === 0 && isURL(value),
|
'proxy': ({ count, value }) => count === 0 && isURL(value),
|
||||||
|
|
|
@ -119,9 +119,10 @@ function buildLinkHeader(url: string, events: Event[]): string | undefined {
|
||||||
const firstEvent = events[0];
|
const firstEvent = events[0];
|
||||||
const lastEvent = events[events.length - 1];
|
const lastEvent = events[events.length - 1];
|
||||||
|
|
||||||
|
const { localDomain } = Conf;
|
||||||
const { pathname, search } = new URL(url);
|
const { pathname, search } = new URL(url);
|
||||||
const next = new URL(pathname + search, Conf.localDomain);
|
const next = new URL(pathname + search, localDomain);
|
||||||
const prev = new URL(pathname + search, Conf.localDomain);
|
const prev = new URL(pathname + search, localDomain);
|
||||||
|
|
||||||
next.searchParams.set('until', String(lastEvent.created_at));
|
next.searchParams.set('until', String(lastEvent.created_at));
|
||||||
prev.searchParams.set('since', String(firstEvent.created_at));
|
prev.searchParams.set('since', String(firstEvent.created_at));
|
||||||
|
@ -132,7 +133,7 @@ function buildLinkHeader(url: string, events: Event[]): string | undefined {
|
||||||
type Entity = { id: string };
|
type Entity = { id: string };
|
||||||
type HeaderRecord = Record<string, string | string[]>;
|
type HeaderRecord = Record<string, string | string[]>;
|
||||||
|
|
||||||
/** Return results with pagination headers. */
|
/** Return results with pagination headers. Assumes chronological sorting of events. */
|
||||||
function paginated(c: AppContext, events: Event[], entities: (Entity | undefined)[], headers: HeaderRecord = {}) {
|
function paginated(c: AppContext, events: Event[], entities: (Entity | undefined)[], headers: HeaderRecord = {}) {
|
||||||
const link = buildLinkHeader(c.req.url, events);
|
const link = buildLinkHeader(c.req.url, events);
|
||||||
|
|
||||||
|
|
71
src/views.ts
71
src/views.ts
|
@ -1,38 +1,71 @@
|
||||||
import { AppContext } from '@/app.ts';
|
import { AppContext } from '@/app.ts';
|
||||||
import { eventsDB } from '@/db/events.ts';
|
import { eventsDB } from '@/db/events.ts';
|
||||||
import { type Filter } from '@/deps.ts';
|
import { type Filter } from '@/deps.ts';
|
||||||
import { getAuthor } from '@/queries.ts';
|
|
||||||
import { renderAccount } from '@/views/mastodon/accounts.ts';
|
import { renderAccount } from '@/views/mastodon/accounts.ts';
|
||||||
import { paginated } from '@/utils/web.ts';
|
import { renderStatus } from '@/views/mastodon/statuses.ts';
|
||||||
|
import { paginated, paginationSchema } from '@/utils/web.ts';
|
||||||
|
|
||||||
/** Render account objects for the author of each event. */
|
/** Render account objects for the author of each event. */
|
||||||
async function renderEventAccounts(c: AppContext, filters: Filter[]) {
|
async function renderEventAccounts(c: AppContext, filters: Filter[], signal = AbortSignal.timeout(1000)) {
|
||||||
const events = await eventsDB.getEvents(filters);
|
if (!filters.length) {
|
||||||
|
return c.json([]);
|
||||||
|
}
|
||||||
|
|
||||||
|
const events = await eventsDB.getEvents(filters, { signal });
|
||||||
const pubkeys = new Set(events.map(({ pubkey }) => pubkey));
|
const pubkeys = new Set(events.map(({ pubkey }) => pubkey));
|
||||||
|
|
||||||
if (!pubkeys.size) {
|
if (!pubkeys.size) {
|
||||||
return c.json([]);
|
return c.json([]);
|
||||||
}
|
}
|
||||||
|
|
||||||
const accounts = await Promise.all([...pubkeys].map(async (pubkey) => {
|
const authors = await eventsDB.getEvents(
|
||||||
const author = await getAuthor(pubkey, { relations: ['author_stats'] });
|
[{ kinds: [0], authors: [...pubkeys], relations: ['author_stats'] }],
|
||||||
if (author) {
|
{ signal },
|
||||||
return renderAccount(author);
|
);
|
||||||
}
|
|
||||||
}));
|
const accounts = await Promise.all(
|
||||||
|
authors.map((event) => renderAccount(event)),
|
||||||
|
);
|
||||||
|
|
||||||
return paginated(c, events, accounts);
|
return paginated(c, events, accounts);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function renderAccounts(c: AppContext, pubkeys: string[]) {
|
async function renderAccounts(c: AppContext, authors: string[], signal = AbortSignal.timeout(1000)) {
|
||||||
// TODO: pagination by offset.
|
const { since, until, limit } = paginationSchema.parse(c.req.query());
|
||||||
// FIXME: this is very inefficient!
|
|
||||||
const accounts = await Promise.all(pubkeys.map(async (pubkey) => {
|
|
||||||
const event = await getAuthor(pubkey);
|
|
||||||
return event ? await renderAccount(event) : undefined;
|
|
||||||
}));
|
|
||||||
|
|
||||||
return c.json(accounts.filter(Boolean));
|
const events = await eventsDB.getEvents(
|
||||||
|
[{ kinds: [0], authors, relations: ['author_stats'], since, until, limit }],
|
||||||
|
{ signal },
|
||||||
|
);
|
||||||
|
|
||||||
|
const accounts = await Promise.all(
|
||||||
|
events.map((event) => renderAccount(event)),
|
||||||
|
);
|
||||||
|
|
||||||
|
return paginated(c, events, accounts);
|
||||||
}
|
}
|
||||||
|
|
||||||
export { renderAccounts, renderEventAccounts };
|
/** Render statuses by event IDs. */
|
||||||
|
async function renderStatuses(c: AppContext, ids: string[], signal = AbortSignal.timeout(1000)) {
|
||||||
|
const { limit } = paginationSchema.parse(c.req.query());
|
||||||
|
|
||||||
|
const events = await eventsDB.getEvents(
|
||||||
|
[{ kinds: [1], ids, relations: ['author', 'event_stats', 'author_stats'], limit }],
|
||||||
|
{ signal },
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!events.length) {
|
||||||
|
return c.json([]);
|
||||||
|
}
|
||||||
|
|
||||||
|
const sortedEvents = [...events].sort((a, b) => ids.indexOf(a.id) - ids.indexOf(b.id));
|
||||||
|
|
||||||
|
const statuses = await Promise.all(
|
||||||
|
sortedEvents.map((event) => renderStatus(event, c.get('pubkey'))),
|
||||||
|
);
|
||||||
|
|
||||||
|
// TODO: pagination with min_id and max_id based on the order of `ids`.
|
||||||
|
return c.json(statuses);
|
||||||
|
}
|
||||||
|
|
||||||
|
export { renderAccounts, renderEventAccounts, renderStatuses };
|
||||||
|
|
|
@ -2,13 +2,18 @@ import { eventsDB } from '@/db/events.ts';
|
||||||
import { hasTag } from '@/tags.ts';
|
import { hasTag } from '@/tags.ts';
|
||||||
|
|
||||||
async function renderRelationship(sourcePubkey: string, targetPubkey: string) {
|
async function renderRelationship(sourcePubkey: string, targetPubkey: string) {
|
||||||
const [event3, target3, event10000, target10000] = await eventsDB.getEvents([
|
const events = await eventsDB.getEvents([
|
||||||
{ kinds: [3], authors: [sourcePubkey], limit: 1 },
|
{ kinds: [3], authors: [sourcePubkey], limit: 1 },
|
||||||
{ kinds: [3], authors: [targetPubkey], limit: 1 },
|
{ kinds: [3], authors: [targetPubkey], limit: 1 },
|
||||||
{ kinds: [10000], authors: [sourcePubkey], limit: 1 },
|
{ kinds: [10000], authors: [sourcePubkey], limit: 1 },
|
||||||
{ kinds: [10000], authors: [targetPubkey], limit: 1 },
|
{ kinds: [10000], authors: [targetPubkey], limit: 1 },
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
const event3 = events.find((event) => event.kind === 3 && event.pubkey === sourcePubkey);
|
||||||
|
const target3 = events.find((event) => event.kind === 3 && event.pubkey === targetPubkey);
|
||||||
|
const event10000 = events.find((event) => event.kind === 10000 && event.pubkey === sourcePubkey);
|
||||||
|
const target10000 = events.find((event) => event.kind === 10000 && event.pubkey === targetPubkey);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
id: targetPubkey,
|
id: targetPubkey,
|
||||||
following: event3 ? hasTag(event3.tags, ['p', targetPubkey]) : false,
|
following: event3 ? hasTag(event3.tags, ['p', targetPubkey]) : false,
|
||||||
|
|
|
@ -30,14 +30,23 @@ async function renderStatus(event: DittoEvent<1>, viewerPubkey?: string) {
|
||||||
|
|
||||||
const { html, links, firstUrl } = parseNoteContent(event.content);
|
const { html, links, firstUrl } = parseNoteContent(event.content);
|
||||||
|
|
||||||
const [mentions, card, [repostEvent], [reactionEvent]] = await Promise
|
const [mentions, card, relatedEvents] = await Promise
|
||||||
.all([
|
.all([
|
||||||
Promise.all(mentionedPubkeys.map(toMention)),
|
Promise.all(mentionedPubkeys.map(toMention)),
|
||||||
firstUrl ? unfurlCardCached(firstUrl) : null,
|
firstUrl ? unfurlCardCached(firstUrl) : null,
|
||||||
viewerPubkey ? eventsDB.getEvents([{ kinds: [6], '#e': [event.id], authors: [viewerPubkey] }], { limit: 1 }) : [],
|
viewerPubkey
|
||||||
viewerPubkey ? eventsDB.getEvents([{ kinds: [7], '#e': [event.id], authors: [viewerPubkey] }], { limit: 1 }) : [],
|
? await eventsDB.getEvents([
|
||||||
|
{ kinds: [6], '#e': [event.id], authors: [viewerPubkey], limit: 1 },
|
||||||
|
{ kinds: [7], '#e': [event.id], authors: [viewerPubkey], limit: 1 },
|
||||||
|
{ kinds: [10003], '#e': [event.id], authors: [viewerPubkey], limit: 1 },
|
||||||
|
])
|
||||||
|
: [],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
const reactionEvent = relatedEvents.find((event) => event.kind === 6);
|
||||||
|
const repostEvent = relatedEvents.find((event) => event.kind === 7);
|
||||||
|
const bookmarkEvent = relatedEvents.find((event) => event.kind === 10003);
|
||||||
|
|
||||||
const content = buildInlineRecipients(mentions) + html;
|
const content = buildInlineRecipients(mentions) + html;
|
||||||
|
|
||||||
const cw = event.tags.find(isCWTag);
|
const cw = event.tags.find(isCWTag);
|
||||||
|
@ -69,7 +78,7 @@ async function renderStatus(event: DittoEvent<1>, viewerPubkey?: string) {
|
||||||
favourited: reactionEvent?.content === '+',
|
favourited: reactionEvent?.content === '+',
|
||||||
reblogged: Boolean(repostEvent),
|
reblogged: Boolean(repostEvent),
|
||||||
muted: false,
|
muted: false,
|
||||||
bookmarked: false,
|
bookmarked: Boolean(bookmarkEvent),
|
||||||
reblog: null,
|
reblog: null,
|
||||||
application: null,
|
application: null,
|
||||||
media_attachments: media.map(renderAttachment),
|
media_attachments: media.map(renderAttachment),
|
||||||
|
|
Loading…
Reference in New Issue