Handle kind 14 for all pubkeys

This commit is contained in:
Jonathan Staab 2023-09-14 10:58:29 -07:00
parent ecbe312bdd
commit f519b8361c

View File

@ -1,8 +1,8 @@
import {prop, without, uniqBy, uniq} from "ramda"
import {prop, uniqBy, uniq} from "ramda"
import {Tags, appDataKeys} from "src/util/nostr"
import {tryJson} from "src/util/misc"
import type {Channel} from "src/engine2/model"
import {session, channels} from "src/engine2/state"
import {sessions, channels} from "src/engine2/state"
import {nip04, getNip24ChannelId, canSign} from "src/engine2/queries"
import {projections} from "src/engine2/projections/core"
@ -28,25 +28,30 @@ projections.addHandler(30078, async e => {
projections.addHandler(14, e => {
const tags = Tags.from(e)
const {pubkey} = session.get()
const pubkeys = without([pubkey], tags.type("p").values().all().concat(e.pubkey))
const pubkeys = tags.type("p").values().all().concat(e.pubkey)
const channelId = getNip24ChannelId(pubkeys)
channels.key(channelId).update($channel => {
const updates = {
...$channel,
id: channelId,
type: "nip24",
relays: uniq(tags.relays().concat($channel?.relays || [])),
messages: uniqBy(prop("id"), [e].concat($channel?.messages || [])),
for (const pubkey of Object.keys(sessions.get())) {
if (!pubkeys.includes(pubkey)) {
continue
}
if (e.pubkey === pubkey) {
updates.last_sent = Math.max(updates.last_sent || 0, e.created_at)
} else {
updates.last_received = Math.max(updates.last_received || 0, e.created_at)
}
channels.key(channelId).update($channel => {
const updates = {
...$channel,
id: channelId,
type: "nip24",
relays: uniq(tags.relays().concat($channel?.relays || [])),
messages: uniqBy(prop("id"), [e].concat($channel?.messages || [])),
}
return updates as Channel
})
if (e.pubkey === pubkey) {
updates.last_sent = Math.max(updates.last_sent || 0, e.created_at)
} else {
updates.last_received = Math.max(updates.last_received || 0, e.created_at)
}
return updates as Channel
})
}
})