Make favourites stick, refactor some async logic in threads

This commit is contained in:
Alex Gleason 2023-08-29 14:44:59 -05:00
parent d4612d5f21
commit 2ee29bf1e2
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
5 changed files with 34 additions and 25 deletions

View File

@ -109,7 +109,7 @@ const accountStatusesController: AppController = async (c) => {
events = events.filter((event) => !findReplyTag(event)); events = events.filter((event) => !findReplyTag(event));
} }
const statuses = await Promise.all(events.map(toStatus)); const statuses = await Promise.all(events.map((event) => toStatus(event, c.get('pubkey'))));
return paginated(c, events, statuses); return paginated(c, events, statuses);
}; };

View File

@ -1,5 +1,5 @@
import { type AppController } from '@/app.ts'; import { type AppController } from '@/app.ts';
import { ISO6391, Kind, z } from '@/deps.ts'; import { type Event, ISO6391, z } from '@/deps.ts';
import { getAncestors, getDescendants, getEvent } from '@/queries.ts'; import { getAncestors, getDescendants, getEvent } from '@/queries.ts';
import { toStatus } from '@/transformers/nostr-to-mastoapi.ts'; import { toStatus } from '@/transformers/nostr-to-mastoapi.ts';
import { createEvent, parseBody } from '@/utils/web.ts'; import { createEvent, parseBody } from '@/utils/web.ts';
@ -69,7 +69,7 @@ const createStatusController: AppController = async (c) => {
} }
const event = await createEvent({ const event = await createEvent({
kind: Kind.Text, kind: 1,
content: data.status ?? '', content: data.status ?? '',
tags, tags,
}, c); }, c);
@ -82,17 +82,20 @@ const createStatusController: AppController = async (c) => {
const contextController: AppController = async (c) => { const contextController: AppController = async (c) => {
const id = c.req.param('id'); const id = c.req.param('id');
const event = await getEvent(id, { kind: 1 }); const event = await getEvent(id, { kind: 1 });
if (event) { async function renderStatuses(events: Event<1>[]) {
const ancestorEvents = await getAncestors(event); const statuses = await Promise.all(events.map((event) => toStatus(event, c.get('pubkey'))));
const descendantEvents = await getDescendants(event.id); return statuses.filter(Boolean);
}
return c.json({ if (event) {
ancestors: (await Promise.all(ancestorEvents.map(toStatus))).filter(Boolean), const [ancestors, descendants] = await Promise.all([
descendants: (await Promise.all(descendantEvents.map(toStatus))).filter(Boolean), getAncestors(event).then(renderStatuses),
}); getDescendants(event.id).then(renderStatuses),
]);
return c.json({ ancestors, descendants });
} }
return c.json({ error: 'Event not found.' }, 404); return c.json({ error: 'Event not found.' }, 404);
@ -104,7 +107,7 @@ const favouriteController: AppController = async (c) => {
if (target) { if (target) {
await createEvent({ await createEvent({
kind: Kind.Reaction, kind: 7,
content: '+', content: '+',
tags: [ tags: [
['e', target.id], ['e', target.id],

View File

@ -40,7 +40,7 @@ async function renderStatuses(c: AppContext, filters: DittoFilter<1>[]) {
return c.json([]); return c.json([]);
} }
const statuses = await Promise.all(events.map(toStatus)); const statuses = await Promise.all(events.map((event) => toStatus(event, c.get('pubkey'))));
return paginated(c, events, statuses); return paginated(c, events, statuses);
} }

View File

@ -18,7 +18,6 @@ export {
getEventHash, getEventHash,
getPublicKey, getPublicKey,
getSignature, getSignature,
Kind,
matchFilters, matchFilters,
nip04, nip04,
nip05, nip05,

View File

@ -1,7 +1,7 @@
import { isCWTag } from 'https://gitlab.com/soapbox-pub/mostr/-/raw/c67064aee5ade5e01597c6d23e22e53c628ef0e2/src/nostr/tags.ts'; import { isCWTag } from 'https://gitlab.com/soapbox-pub/mostr/-/raw/c67064aee5ade5e01597c6d23e22e53c628ef0e2/src/nostr/tags.ts';
import { Conf } from '@/config.ts'; import { Conf } from '@/config.ts';
import { countFilters } from '@/db/events.ts'; import * as eventsDB from '@/db/events.ts';
import { type Event, findReplyTag, lodash, nip19, sanitizeHtml, TTLCache, unfurl, z } from '@/deps.ts'; import { type Event, findReplyTag, lodash, nip19, sanitizeHtml, TTLCache, unfurl, z } from '@/deps.ts';
import { verifyNip05Cached } from '@/nip05.ts'; import { verifyNip05Cached } from '@/nip05.ts';
import { getMediaLinks, type MediaLink, parseNoteContent } from '@/note.ts'; import { getMediaLinks, type MediaLink, parseNoteContent } from '@/note.ts';
@ -92,7 +92,7 @@ async function toMention(pubkey: string) {
} }
} }
async function toStatus(event: Event<1>) { async function toStatus(event: Event<1>, viewerPubkey?: string) {
const profile = await getAuthor(event.pubkey); const profile = await getAuthor(event.pubkey);
const account = profile ? await toAccount(profile) : undefined; const account = profile ? await toAccount(profile) : undefined;
if (!account) return; if (!account) return;
@ -110,12 +110,19 @@ async function toStatus(event: Event<1>) {
const { html, links, firstUrl } = parseNoteContent(event.content); const { html, links, firstUrl } = parseNoteContent(event.content);
const mediaLinks = getMediaLinks(links); const mediaLinks = getMediaLinks(links);
const [mentions, card, repliesCount, reblogsCount, favouritesCount] = await Promise.all([ const [mentions, card, repliesCount, reblogsCount, favouritesCount, [repostEvent], [reactionEvent]] = await Promise
.all([
Promise.all(mentionedPubkeys.map(toMention)), Promise.all(mentionedPubkeys.map(toMention)),
firstUrl ? unfurlCardCached(firstUrl) : null, firstUrl ? unfurlCardCached(firstUrl) : null,
countFilters([{ kinds: [1], '#e': [event.id] }]), eventsDB.countFilters([{ kinds: [1], '#e': [event.id] }]),
countFilters([{ kinds: [6], '#e': [event.id] }]), eventsDB.countFilters([{ kinds: [6], '#e': [event.id] }]),
countFilters([{ kinds: [7], '#e': [event.id] }]), eventsDB.countFilters([{ kinds: [7], '#e': [event.id] }]),
viewerPubkey
? eventsDB.getFilters([{ kinds: [6], '#e': [event.id], authors: [viewerPubkey] }], { limit: 1 })
: [],
viewerPubkey
? eventsDB.getFilters([{ kinds: [7], '#e': [event.id], authors: [viewerPubkey] }], { limit: 1 })
: [],
]); ]);
const content = buildInlineRecipients(mentions) + html; const content = buildInlineRecipients(mentions) + html;
@ -138,8 +145,8 @@ async function toStatus(event: Event<1>) {
replies_count: repliesCount, replies_count: repliesCount,
reblogs_count: reblogsCount, reblogs_count: reblogsCount,
favourites_count: favouritesCount, favourites_count: favouritesCount,
favourited: false, favourited: reactionEvent?.content === '+',
reblogged: false, reblogged: Boolean(repostEvent),
muted: false, muted: false,
bookmarked: false, bookmarked: false,
reblog: null, reblog: null,