Merge branch 'nip51' into 'main'

Add support for blocking, refactor user lists (NIP-51)

See merge request soapbox-pub/ditto!90
This commit is contained in:
Alex Gleason 2024-01-01 05:42:34 +00:00
commit 0c311732d3
10 changed files with 131 additions and 38 deletions

View File

@ -23,6 +23,7 @@ import {
accountLookupController, accountLookupController,
accountSearchController, accountSearchController,
accountStatusesController, accountStatusesController,
blockController,
createAccountController, createAccountController,
favouritesController, favouritesController,
followController, followController,
@ -135,6 +136,7 @@ app.patch(
app.get('/api/v1/accounts/search', accountSearchController); app.get('/api/v1/accounts/search', accountSearchController);
app.get('/api/v1/accounts/lookup', accountLookupController); app.get('/api/v1/accounts/lookup', accountLookupController);
app.get('/api/v1/accounts/relationships', relationshipsController); app.get('/api/v1/accounts/relationships', relationshipsController);
app.post('/api/v1/accounts/:pubkey{[0-9a-f]{64}}/block', blockController);
app.post('/api/v1/accounts/:pubkey{[0-9a-f]{64}}/follow', followController); app.post('/api/v1/accounts/:pubkey{[0-9a-f]{64}}/follow', followController);
app.get('/api/v1/accounts/:pubkey{[0-9a-f]{64}}/followers', followersController); app.get('/api/v1/accounts/:pubkey{[0-9a-f]{64}}/followers', followersController);
app.get('/api/v1/accounts/:pubkey{[0-9a-f]{64}}/following', followingController); app.get('/api/v1/accounts/:pubkey{[0-9a-f]{64}}/following', followingController);

View File

@ -52,6 +52,7 @@ function getEvents<K extends number>(filters: Filter<K>[], opts: GetEventsOpts =
/** Publish an event to the given relays, or the entire pool. */ /** Publish an event to the given relays, or the entire pool. */
function storeEvent(event: Event, opts: StoreEventOpts = {}): Promise<void> { function storeEvent(event: Event, opts: StoreEventOpts = {}): Promise<void> {
const { relays = activeRelays } = opts; const { relays = activeRelays } = opts;
const debug = Debug('ditto:client:publish');
debug('EVENT', event); debug('EVENT', event);
pool.publish(event, relays); pool.publish(event, relays);
return Promise.resolve(); return Promise.resolve();

View File

@ -4,12 +4,13 @@ import { eventsDB } from '@/db/events.ts';
import { insertUser } from '@/db/users.ts'; import { insertUser } from '@/db/users.ts';
import { findReplyTag, nip19, z } from '@/deps.ts'; import { findReplyTag, nip19, z } from '@/deps.ts';
import { type DittoFilter } from '@/filter.ts'; import { type DittoFilter } from '@/filter.ts';
import { getAuthor, getFollowedPubkeys, getFollows } from '@/queries.ts'; import { getAuthor, getFollowedPubkeys } from '@/queries.ts';
import { booleanParamSchema, fileSchema } from '@/schema.ts'; import { booleanParamSchema, fileSchema } from '@/schema.ts';
import { jsonMetaContentSchema } from '@/schemas/nostr.ts'; import { jsonMetaContentSchema } from '@/schemas/nostr.ts';
import { addTag } from '@/tags.ts';
import { uploadFile } from '@/upload.ts'; import { uploadFile } from '@/upload.ts';
import { isFollowing, lookupAccount, nostrNow } from '@/utils.ts'; import { lookupAccount, nostrNow } from '@/utils.ts';
import { paginated, paginationSchema, parseBody } from '@/utils/web.ts'; import { paginated, paginationSchema, parseBody, updateListEvent } from '@/utils/web.ts';
import { createEvent } from '@/utils/web.ts'; import { createEvent } from '@/utils/web.ts';
import { renderEventAccounts } from '@/views.ts'; import { renderEventAccounts } from '@/views.ts';
import { accountFromPubkey, renderAccount } from '@/views/mastodon/accounts.ts'; import { accountFromPubkey, renderAccount } from '@/views/mastodon/accounts.ts';
@ -216,18 +217,11 @@ const followController: AppController = async (c) => {
const sourcePubkey = c.get('pubkey')!; const sourcePubkey = c.get('pubkey')!;
const targetPubkey = c.req.param('pubkey'); const targetPubkey = c.req.param('pubkey');
const source = await getFollows(sourcePubkey); await updateListEvent(
{ kinds: [3], authors: [sourcePubkey] },
if (!source || !isFollowing(source, targetPubkey)) { (tags) => addTag(tags, ['p', targetPubkey]),
await createEvent({ c,
kind: 3, );
content: '',
tags: [
...(source?.tags ?? []),
['p', targetPubkey],
],
}, c);
}
const relationship = await renderRelationship(sourcePubkey, targetPubkey); const relationship = await renderRelationship(sourcePubkey, targetPubkey);
return c.json(relationship); return c.json(relationship);
@ -252,6 +246,20 @@ const followingController: AppController = async (c) => {
return c.json(accounts.filter(Boolean)); return c.json(accounts.filter(Boolean));
}; };
const blockController: AppController = async (c) => {
const sourcePubkey = c.get('pubkey')!;
const targetPubkey = c.req.param('pubkey');
await updateListEvent(
{ kinds: [10000], authors: [sourcePubkey] },
(tags) => addTag(tags, ['p', targetPubkey]),
c,
);
const relationship = await renderRelationship(sourcePubkey, targetPubkey);
return c.json(relationship);
};
const favouritesController: AppController = async (c) => { const favouritesController: AppController = async (c) => {
const pubkey = c.get('pubkey')!; const pubkey = c.get('pubkey')!;
const params = paginationSchema.parse(c.req.query()); const params = paginationSchema.parse(c.req.query());
@ -281,6 +289,7 @@ export {
accountLookupController, accountLookupController,
accountSearchController, accountSearchController,
accountStatusesController, accountStatusesController,
blockController,
createAccountController, createAccountController,
favouritesController, favouritesController,
followController, followController,

View File

@ -257,6 +257,7 @@ async function getEvents<K extends number>(
filters: DittoFilter<K>[], filters: DittoFilter<K>[],
opts: GetEventsOpts = {}, opts: GetEventsOpts = {},
): Promise<DittoEvent<K>[]> { ): Promise<DittoEvent<K>[]> {
if (opts.signal?.aborted) return Promise.resolve([]);
if (!filters.length) return Promise.resolve([]); if (!filters.length) return Promise.resolve([]);
debug('REQ', JSON.stringify(filters)); debug('REQ', JSON.stringify(filters));
let query = getEventsQuery(filters); let query = getEventsQuery(filters);

View File

@ -78,7 +78,7 @@ const getAuthor = async (pubkey: string, opts: GetEventOpts<0> = {}): Promise<Ev
}; };
/** Get users the given pubkey follows. */ /** Get users the given pubkey follows. */
const getFollows = async (pubkey: string, signal = AbortSignal.timeout(1000)): Promise<Event<3> | undefined> => { const getFollows = async (pubkey: string, signal?: AbortSignal): Promise<Event<3> | undefined> => {
const [event] = await eventsDB.getEvents([{ authors: [pubkey], kinds: [3], limit: 1 }], { limit: 1, signal }); const [event] = await eventsDB.getEvents([{ authors: [pubkey], kinds: [3], limit: 1 }], { limit: 1, signal });
return event; return event;
}; };

25
src/tags.test.ts Normal file
View File

@ -0,0 +1,25 @@
import { assertEquals } from '@/deps-test.ts';
import { addTag, deleteTag, getTagSet } from './tags.ts';
Deno.test('getTagSet', () => {
assertEquals(getTagSet([], 'p'), new Set());
assertEquals(getTagSet([['p', '123']], 'p'), new Set(['123']));
assertEquals(getTagSet([['p', '123'], ['p', '456']], 'p'), new Set(['123', '456']));
assertEquals(getTagSet([['p', '123'], ['p', '456'], ['q', '789']], 'p'), new Set(['123', '456']));
});
Deno.test('addTag', () => {
assertEquals(addTag([], ['p', '123']), [['p', '123']]);
assertEquals(addTag([['p', '123']], ['p', '123']), [['p', '123']]);
assertEquals(addTag([['p', '123'], ['p', '456']], ['p', '123']), [['p', '123'], ['p', '456']]);
assertEquals(addTag([['p', '123'], ['p', '456']], ['p', '789']), [['p', '123'], ['p', '456'], ['p', '789']]);
});
Deno.test('deleteTag', () => {
assertEquals(deleteTag([], ['p', '123']), []);
assertEquals(deleteTag([['p', '123']], ['p', '123']), []);
assertEquals(deleteTag([['p', '123']], ['p', '456']), [['p', '123']]);
assertEquals(deleteTag([['p', '123'], ['p', '123']], ['p', '123']), []);
assertEquals(deleteTag([['p', '123'], ['p', '456']], ['p', '456']), [['p', '123']]);
});

View File

@ -11,4 +11,24 @@ function getTagSet(tags: string[][], tagName: string): Set<string> {
return set; return set;
} }
export { getTagSet }; /** Check if the tag exists by its name and value. */
function hasTag(tags: string[][], tag: string[]): boolean {
return tags.some(([name, value]) => name === tag[0] && value === tag[1]);
}
/** Delete all occurences of the tag by its name/value pair. */
function deleteTag(tags: readonly string[][], tag: string[]): string[][] {
return tags.filter(([name, value]) => !(name === tag[0] && value === tag[1]));
}
/** Add a tag to the list, replacing the name/value pair if it already exists. */
function addTag(tags: readonly string[][], tag: string[]): string[][] {
const tagIndex = tags.findIndex(([name, value]) => name === tag[0] && value === tag[1]);
if (tagIndex === -1) {
return [...tags, tag];
} else {
return [...tags.slice(0, tagIndex), tag, ...tags.slice(tagIndex + 1)];
}
}
export { addTag, deleteTag, getTagSet, hasTag };

View File

@ -95,13 +95,6 @@ const relaySchema = z.string().max(255).startsWith('wss://').url();
/** Check whether the value is a valid relay URL. */ /** Check whether the value is a valid relay URL. */
const isRelay = (relay: string): relay is `wss://${string}` => relaySchema.safeParse(relay).success; const isRelay = (relay: string): relay is `wss://${string}` => relaySchema.safeParse(relay).success;
/** Check whether source is following target. */
function isFollowing(source: Event<3>, targetPubkey: string): boolean {
return Boolean(
source.tags.find(([tagName, tagValue]) => tagName === 'p' && tagValue === targetPubkey),
);
}
/** Deduplicate events by ID. */ /** Deduplicate events by ID. */
function dedupeEvents<K extends number>(events: Event<K>[]): Event<K>[] { function dedupeEvents<K extends number>(events: Event<K>[]): Event<K>[] {
return [...new Map(events.map((event) => [event.id, event])).values()]; return [...new Map(events.map((event) => [event.id, event])).values()];
@ -156,7 +149,6 @@ export {
eventDateComparator, eventDateComparator,
eventMatchesTemplate, eventMatchesTemplate,
findTag, findTag,
isFollowing,
isNostrId, isNostrId,
isRelay, isRelay,
isURL, isURL,

View File

@ -1,13 +1,22 @@
import { type AppContext } from '@/app.ts';
import { Conf } from '@/config.ts'; import { Conf } from '@/config.ts';
import { type Context, type Event, EventTemplate, HTTPException, parseFormData, type TypeFest, z } from '@/deps.ts'; import {
type Context,
type Event,
EventTemplate,
Filter,
HTTPException,
parseFormData,
type TypeFest,
z,
} from '@/deps.ts';
import * as pipeline from '@/pipeline.ts'; import * as pipeline from '@/pipeline.ts';
import { signAdminEvent, signEvent } from '@/sign.ts'; import { signAdminEvent, signEvent } from '@/sign.ts';
import { nostrNow } from '@/utils.ts'; import { nostrNow } from '@/utils.ts';
import { eventsDB } from '@/db/events.ts';
import type { AppContext } from '@/app.ts';
/** EventTemplate with defaults. */ /** EventTemplate with defaults. */
type EventStub<K extends number = number> = TypeFest.SetOptional<EventTemplate<K>, 'created_at' | 'tags'>; type EventStub<K extends number = number> = TypeFest.SetOptional<EventTemplate<K>, 'content' | 'created_at' | 'tags'>;
/** Publish an event through the pipeline. */ /** Publish an event through the pipeline. */
async function createEvent<K extends number>(t: EventStub<K>, c: AppContext): Promise<Event<K>> { async function createEvent<K extends number>(t: EventStub<K>, c: AppContext): Promise<Event<K>> {
@ -18,6 +27,7 @@ async function createEvent<K extends number>(t: EventStub<K>, c: AppContext): Pr
} }
const event = await signEvent({ const event = await signEvent({
content: '',
created_at: nostrNow(), created_at: nostrNow(),
tags: [], tags: [],
...t, ...t,
@ -26,9 +36,38 @@ async function createEvent<K extends number>(t: EventStub<K>, c: AppContext): Pr
return publishEvent(event, c); return publishEvent(event, c);
} }
/** Filter for fetching an existing event to update. */
interface UpdateEventFilter<K extends number> extends Filter<K> {
kinds: [K];
limit?: 1;
}
/** Fetch existing event, update it, then publish the new event. */
async function updateEvent<K extends number, E extends EventStub<K>>(
filter: UpdateEventFilter<K>,
fn: (prev: Event<K> | undefined) => E,
c: AppContext,
): Promise<Event<K>> {
const [prev] = await eventsDB.getEvents([filter], { limit: 1 });
return createEvent(fn(prev), c);
}
/** Fetch existing event, update its tags, then publish the new event. */
function updateListEvent<K extends number>(
filter: UpdateEventFilter<K>,
fn: (tags: string[][]) => string[][],
c: AppContext,
): Promise<Event<K>> {
return updateEvent(filter, (prev) => ({
kind: filter.kinds[0],
content: prev?.content ?? '',
tags: fn(prev?.tags ?? []),
}), c);
}
/** Publish an admin event through the pipeline. */ /** Publish an admin event through the pipeline. */
async function createAdminEvent<K extends number>(t: EventStub<K>, c: AppContext): Promise<Event<K>> { async function createAdminEvent<K extends number>(t: EventStub<K>, c: AppContext): Promise<Event<K>> {
const event = await signAdminEvent({ const event = await signAdminEvent({
content: '',
created_at: nostrNow(), created_at: nostrNow(),
tags: [], tags: [],
...t, ...t,
@ -139,4 +178,6 @@ export {
type PaginationParams, type PaginationParams,
paginationSchema, paginationSchema,
parseBody, parseBody,
updateEvent,
updateListEvent,
}; };

View File

@ -1,20 +1,22 @@
import { getFollows } from '@/queries.ts'; import { eventsDB } from '@/db/events.ts';
import { isFollowing } from '@/utils.ts'; import { hasTag } from '@/tags.ts';
async function renderRelationship(sourcePubkey: string, targetPubkey: string) { async function renderRelationship(sourcePubkey: string, targetPubkey: string) {
const [source, target] = await Promise.all([ const [event3, target3, event10000, target10000] = await eventsDB.getEvents([
getFollows(sourcePubkey), { kinds: [3], authors: [sourcePubkey], limit: 1 },
getFollows(targetPubkey), { kinds: [3], authors: [targetPubkey], limit: 1 },
{ kinds: [10000], authors: [sourcePubkey], limit: 1 },
{ kinds: [10000], authors: [targetPubkey], limit: 1 },
]); ]);
return { return {
id: targetPubkey, id: targetPubkey,
following: source ? isFollowing(source, targetPubkey) : false, following: event3 ? hasTag(event3.tags, ['p', targetPubkey]) : false,
showing_reblogs: true, showing_reblogs: true,
notifying: false, notifying: false,
followed_by: target ? isFollowing(target, sourcePubkey) : false, followed_by: target3 ? hasTag(target3?.tags, ['p', sourcePubkey]) : false,
blocking: false, blocking: event10000 ? hasTag(event10000.tags, ['p', targetPubkey]) : false,
blocked_by: false, blocked_by: target10000 ? hasTag(target10000.tags, ['p', sourcePubkey]) : false,
muting: false, muting: false,
muting_notifications: false, muting_notifications: false,
requested: false, requested: false,