2023-08-26 22:31:52 +00:00
|
|
|
import { Conf } from '@/config.ts';
|
2023-08-25 18:38:21 +00:00
|
|
|
import { type Event, type Filter, matchFilters } from '@/deps.ts';
|
2023-08-24 20:28:13 +00:00
|
|
|
|
2023-08-25 18:38:21 +00:00
|
|
|
import type { EventData } from '@/types.ts';
|
|
|
|
|
|
|
|
/** Custom filter interface that extends Nostr filters with extra options for Ditto. */
|
|
|
|
interface DittoFilter<K extends number = number> extends Filter<K> {
|
|
|
|
local?: boolean;
|
|
|
|
}
|
|
|
|
|
2023-12-05 23:27:52 +00:00
|
|
|
/** Additional properties that may be added to events. */
|
|
|
|
type With = 'authors';
|
|
|
|
|
2023-08-25 18:38:21 +00:00
|
|
|
/** Additional options to apply to the whole subscription. */
|
|
|
|
interface GetFiltersOpts {
|
|
|
|
/** How long to wait (in milliseconds) until aborting the request. */
|
|
|
|
timeout?: number;
|
|
|
|
/** Event limit for the whole subscription. */
|
|
|
|
limit?: number;
|
2023-12-05 23:15:08 +00:00
|
|
|
/** Whether to include a corresponding kind 0 event in the `authors` key of each event. */
|
2023-12-05 23:27:52 +00:00
|
|
|
with?: With[];
|
2023-08-25 18:38:21 +00:00
|
|
|
}
|
2023-08-24 20:28:13 +00:00
|
|
|
|
|
|
|
function matchDittoFilter(filter: DittoFilter, event: Event, data: EventData): boolean {
|
2023-08-26 22:31:52 +00:00
|
|
|
if (filter.local && !(data.user || event.pubkey === Conf.pubkey)) {
|
2023-08-24 20:28:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return matchFilters([filter], event);
|
|
|
|
}
|
|
|
|
|
2023-08-24 22:00:08 +00:00
|
|
|
/**
|
|
|
|
* Similar to nostr-tools `matchFilters`, but supports Ditto's custom keys.
|
|
|
|
* Database calls are needed to look up the extra data, so it's passed in as an argument.
|
|
|
|
*/
|
2023-08-24 20:28:13 +00:00
|
|
|
function matchDittoFilters(filters: DittoFilter[], event: Event, data: EventData): boolean {
|
|
|
|
for (const filter of filters) {
|
|
|
|
if (matchDittoFilter(filter, event, data)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-08-25 18:38:21 +00:00
|
|
|
export { type DittoFilter, type GetFiltersOpts, matchDittoFilters };
|