db/events: pass EventData, index all media from local users
This commit is contained in:
parent
d24318fd0d
commit
96ff31719e
|
@ -6,12 +6,12 @@ import { insertUser } from '@/db/users.ts';
|
||||||
|
|
||||||
Deno.test('count filters', async () => {
|
Deno.test('count filters', async () => {
|
||||||
assertEquals(await countFilters([{ kinds: [1] }]), 0);
|
assertEquals(await countFilters([{ kinds: [1] }]), 0);
|
||||||
await insertEvent(event55920b75);
|
await insertEvent(event55920b75, { user: undefined });
|
||||||
assertEquals(await countFilters([{ kinds: [1] }]), 1);
|
assertEquals(await countFilters([{ kinds: [1] }]), 1);
|
||||||
});
|
});
|
||||||
|
|
||||||
Deno.test('insert and filter events', async () => {
|
Deno.test('insert and filter events', async () => {
|
||||||
await insertEvent(event55920b75);
|
await insertEvent(event55920b75, { user: undefined });
|
||||||
|
|
||||||
assertEquals(await getFilters([{ kinds: [1] }]), [event55920b75]);
|
assertEquals(await getFilters([{ kinds: [1] }]), [event55920b75]);
|
||||||
assertEquals(await getFilters([{ kinds: [3] }]), []);
|
assertEquals(await getFilters([{ kinds: [3] }]), []);
|
||||||
|
@ -24,14 +24,14 @@ Deno.test('insert and filter events', async () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
Deno.test('delete events', async () => {
|
Deno.test('delete events', async () => {
|
||||||
await insertEvent(event55920b75);
|
await insertEvent(event55920b75, { user: undefined });
|
||||||
assertEquals(await getFilters([{ kinds: [1] }]), [event55920b75]);
|
assertEquals(await getFilters([{ kinds: [1] }]), [event55920b75]);
|
||||||
await deleteFilters([{ kinds: [1] }]);
|
await deleteFilters([{ kinds: [1] }]);
|
||||||
assertEquals(await getFilters([{ kinds: [1] }]), []);
|
assertEquals(await getFilters([{ kinds: [1] }]), []);
|
||||||
});
|
});
|
||||||
|
|
||||||
Deno.test('query events with local filter', async () => {
|
Deno.test('query events with local filter', async () => {
|
||||||
await insertEvent(event55920b75);
|
await insertEvent(event55920b75, { user: undefined });
|
||||||
|
|
||||||
assertEquals(await getFilters([{}]), [event55920b75]);
|
assertEquals(await getFilters([{}]), [event55920b75]);
|
||||||
assertEquals(await getFilters([{ local: true }]), []);
|
assertEquals(await getFilters([{ local: true }]), []);
|
||||||
|
|
|
@ -2,12 +2,14 @@ import { db, type TagRow } from '@/db.ts';
|
||||||
import { type Event, type Insertable, SqliteError } from '@/deps.ts';
|
import { type Event, type Insertable, SqliteError } from '@/deps.ts';
|
||||||
import { isParameterizedReplaceableKind } from '@/kinds.ts';
|
import { isParameterizedReplaceableKind } from '@/kinds.ts';
|
||||||
import { jsonMetaContentSchema } from '@/schemas/nostr.ts';
|
import { jsonMetaContentSchema } from '@/schemas/nostr.ts';
|
||||||
|
import { EventData } from '@/types.ts';
|
||||||
import { isNostrId, isURL } from '@/utils.ts';
|
import { isNostrId, isURL } from '@/utils.ts';
|
||||||
|
|
||||||
import type { DittoFilter, GetFiltersOpts } from '@/filter.ts';
|
import type { DittoFilter, GetFiltersOpts } from '@/filter.ts';
|
||||||
|
|
||||||
type TagCondition = ({ event, count, value }: {
|
type TagCondition = ({ event, count, value }: {
|
||||||
event: Event;
|
event: Event;
|
||||||
|
data: EventData;
|
||||||
count: number;
|
count: number;
|
||||||
value: string;
|
value: string;
|
||||||
}) => boolean;
|
}) => boolean;
|
||||||
|
@ -16,7 +18,7 @@ type TagCondition = ({ event, count, value }: {
|
||||||
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': ({ count, value }) => count < 15 && isNostrId(value),
|
||||||
'media': ({ count, value }) => count < 4 && isURL(value),
|
'media': ({ count, value, data }) => (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),
|
||||||
'q': ({ event, count, value }) => count === 0 && event.kind === 1 && isNostrId(value),
|
'q': ({ event, count, value }) => count === 0 && event.kind === 1 && isNostrId(value),
|
||||||
|
@ -24,7 +26,7 @@ const tagConditions: Record<string, TagCondition> = {
|
||||||
};
|
};
|
||||||
|
|
||||||
/** Insert an event (and its tags) into the database. */
|
/** Insert an event (and its tags) into the database. */
|
||||||
function insertEvent(event: Event): Promise<void> {
|
function insertEvent(event: Event, data: EventData): Promise<void> {
|
||||||
return db.transaction().execute(async (trx) => {
|
return db.transaction().execute(async (trx) => {
|
||||||
await trx.insertInto('events')
|
await trx.insertInto('events')
|
||||||
.values({
|
.values({
|
||||||
|
@ -44,7 +46,14 @@ function insertEvent(event: Event): Promise<void> {
|
||||||
const tags = event.tags.reduce<Insertable<TagRow>[]>((results, [name, value]) => {
|
const tags = event.tags.reduce<Insertable<TagRow>[]>((results, [name, value]) => {
|
||||||
tagCounts[name] = (tagCounts[name] || 0) + 1;
|
tagCounts[name] = (tagCounts[name] || 0) + 1;
|
||||||
|
|
||||||
if (value && value.length < 200 && tagConditions[name]?.({ event, count: tagCounts[name] - 1, value })) {
|
const shouldIndex = tagConditions[name]?.({
|
||||||
|
event,
|
||||||
|
data,
|
||||||
|
count: tagCounts[name] - 1,
|
||||||
|
value,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (value && value.length < 200 && shouldIndex) {
|
||||||
results.push({
|
results.push({
|
||||||
event_id: event.id,
|
event_id: event.id,
|
||||||
tag: name,
|
tag: name,
|
||||||
|
|
|
@ -64,7 +64,7 @@ async function storeEvent(event: Event, data: EventData): Promise<void> {
|
||||||
if (deletion) {
|
if (deletion) {
|
||||||
return Promise.reject(new RelayError('blocked', 'event was deleted'));
|
return Promise.reject(new RelayError('blocked', 'event was deleted'));
|
||||||
} else {
|
} else {
|
||||||
await eventsDB.insertEvent(event).catch(console.warn);
|
await eventsDB.insertEvent(event, data).catch(console.warn);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return Promise.reject(new RelayError('blocked', 'only registered users can post'));
|
return Promise.reject(new RelayError('blocked', 'only registered users can post'));
|
||||||
|
|
Loading…
Reference in New Issue