From 857314013426113312e6dc2328608f12bef82656 Mon Sep 17 00:00:00 2001 From: Jonathan Staab Date: Wed, 12 Apr 2023 16:41:37 -0500 Subject: [PATCH] Add topics database table --- src/agent/db.ts | 1 + src/agent/sync.ts | 17 +++++++++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/agent/db.ts b/src/agent/db.ts index 87bc413d..b4dc19e4 100644 --- a/src/agent/db.ts +++ b/src/agent/db.ts @@ -209,6 +209,7 @@ export const contacts = new Table("contacts", "pubkey") export const rooms = new Table("rooms", "id") export const relays = new Table("relays", "url") export const routes = new Table("routes", "id", {max: 3000, sort: sortByLastSeen}) +export const topics = new Table("topics", "name") export const getPersonWithFallback = pubkey => people.get(pubkey) || {pubkey} export const getRelayWithFallback = url => relays.get(url) || {url} diff --git a/src/agent/sync.ts b/src/agent/sync.ts index 006b29ee..113256b1 100644 --- a/src/agent/sync.ts +++ b/src/agent/sync.ts @@ -1,4 +1,4 @@ -import {uniq, pick, identity} from "ramda" +import {uniq, nth, objOf, pick, identity} from "ramda" import {nip05} from "nostr-tools" import {noop, ensurePlural, chunk} from "hurdak/lib/hurdak" import { @@ -13,7 +13,7 @@ import { hash, } from "src/util/misc" import {Tags, roomAttrs, isRelay, isShareableRelay, normalizeRelayUrl} from "src/util/nostr" -import {people, userEvents, relays, rooms, routes} from "src/agent/db" +import {topics, people, userEvents, relays, rooms, routes} from "src/agent/db" import {uniqByUrl} from "src/agent/relays" import user from "src/agent/user" @@ -341,4 +341,17 @@ addHandler(10002, e => { }) }) +// Topics + +const processTopics = e => { + const matches = Array.from(e.content.matchAll(/#(\w{2,100})/g)) + + if (matches.length > 0) { + topics.patch(matches.map(nth(1)).map(objOf("name"))) + } +} + +addHandler(1, processTopics) +addHandler(42, processTopics) + export default {processEvents}