Update alerts to use new anchor

This commit is contained in:
Jon Staab
2025-04-28 09:48:09 -07:00
parent da2457da9f
commit 2244ecad9b
3 changed files with 30 additions and 42 deletions
+7 -10
View File
@@ -1,6 +1,8 @@
import * as nip19 from "nostr-tools/nip19" import * as nip19 from "nostr-tools/nip19"
import {get} from "svelte/store" import {get} from "svelte/store"
import {randomId, ifLet, poll, uniq, equals} from "@welshman/lib" import {randomId, ifLet, poll, uniq, equals} from "@welshman/lib"
import type {Feed} from "@welshman/feeds"
import type {TrustedEvent, EventContent, EventTemplate} from "@welshman/util"
import { import {
DELETE, DELETE,
REPORT, REPORT,
@@ -28,11 +30,9 @@ import {
getRelayTags, getRelayTags,
getRelayTagValues, getRelayTagValues,
toNostrURI, toNostrURI,
unionFilters,
getRelaysFromList, getRelaysFromList,
RelayMode, RelayMode,
} from "@welshman/util" } from "@welshman/util"
import type {TrustedEvent, Filter, EventContent, EventTemplate} from "@welshman/util"
import {Pool, PublishStatus, AuthStatus, SocketStatus} from "@welshman/net" import {Pool, PublishStatus, AuthStatus, SocketStatus} from "@welshman/net"
import {Nip59, stamp} from "@welshman/signer" import {Nip59, stamp} from "@welshman/signer"
import {Router} from "@welshman/router" import {Router} from "@welshman/router"
@@ -431,19 +431,20 @@ export const publishComment = ({relays, ...params}: CommentParams & {relays: str
publishThunk({event: makeComment(params), relays}) publishThunk({event: makeComment(params), relays})
export type AlertParams = { export type AlertParams = {
feed: Feed
cron: string cron: string
email: string email: string
relay: string
filters: Filter[]
bunker: string bunker: string
secret: string secret: string
description: string
} }
export const makeAlert = async ({cron, email, relay, filters, bunker, secret}: AlertParams) => { export const makeAlert = async ({cron, email, feed, bunker, secret, description}: AlertParams) => {
const tags = [ const tags = [
["feed", JSON.stringify(feed)],
["cron", cron], ["cron", cron],
["email", email], ["email", email],
["relay", relay], ["description", description],
["channel", "email"], ["channel", "email"],
[ [
"handler", "handler",
@@ -453,10 +454,6 @@ export const makeAlert = async ({cron, email, relay, filters, bunker, secret}: A
], ],
] ]
for (const filter of unionFilters(filters)) {
tags.push(["filter", JSON.stringify(filter)])
}
if (bunker) { if (bunker) {
tags.push(["nip46", secret, bunker]) tags.push(["nip46", secret, bunker])
} }
+10 -2
View File
@@ -1,9 +1,10 @@
<script lang="ts"> <script lang="ts">
import {preventDefault} from "@lib/html" import {preventDefault} from "@lib/html"
import {randomInt, TIMEZONE} from "@welshman/lib" import {randomInt, displayList, TIMEZONE} from "@welshman/lib"
import {displayRelayUrl, THREAD, MESSAGE, EVENT_TIME, COMMENT} from "@welshman/util" import {displayRelayUrl, THREAD, MESSAGE, EVENT_TIME, COMMENT} from "@welshman/util"
import type {Filter} from "@welshman/util" import type {Filter} from "@welshman/util"
import type {Nip46ResponseWithResult} from "@welshman/signer" import type {Nip46ResponseWithResult} from "@welshman/signer"
import {makeIntersectionFeed, makeRelayFeed, feedFromFilters} from "@welshman/feeds"
import {pubkey} from "@welshman/app" import {pubkey} from "@welshman/app"
import Icon from "@lib/components/Icon.svelte" import Icon from "@lib/components/Icon.svelte"
import Button from "@lib/components/Button.svelte" import Button from "@lib/components/Button.svelte"
@@ -82,18 +83,22 @@
} }
const filters: Filter[] = [] const filters: Filter[] = []
const display: string[] = []
if (notifyThreads) { if (notifyThreads) {
display.push("threads")
filters.push({kinds: [THREAD]}) filters.push({kinds: [THREAD]})
filters.push({kinds: [COMMENT], "#k": [String(THREAD)]}) filters.push({kinds: [COMMENT], "#k": [String(THREAD)]})
} }
if (notifyCalendar) { if (notifyCalendar) {
display.push("calendar events")
filters.push({kinds: [EVENT_TIME]}) filters.push({kinds: [EVENT_TIME]})
filters.push({kinds: [COMMENT], "#k": [String(EVENT_TIME)]}) filters.push({kinds: [COMMENT], "#k": [String(EVENT_TIME)]})
} }
if (notifyChat) { if (notifyChat) {
display.push("chat")
filters.push({ filters.push({
kinds: [MESSAGE], kinds: [MESSAGE],
"#h": [GENERAL, ...getMembershipRoomsByUrl(relay, $userMembership)], "#h": [GENERAL, ...getMembershipRoomsByUrl(relay, $userMembership)],
@@ -103,7 +108,10 @@
loading = true loading = true
try { try {
const thunk = await publishAlert({cron, email, relay, filters, bunker, secret}) const cadence = cron?.endsWith("1") ? "Weekly" : "Daily"
const description = `${cadence} alert for ${displayList(display)} on ${displayRelayUrl(relay)}, sent via email.`
const feed = makeIntersectionFeed(feedFromFilters(filters), makeRelayFeed(relay))
const thunk = await publishAlert({cron, email, feed, bunker, secret, description})
await thunk.result await thunk.result
await loadAlertStatuses($pubkey!) await loadAlertStatuses($pubkey!)
+13 -30
View File
@@ -1,21 +1,12 @@
<script lang="ts"> <script lang="ts">
import {parseJson, displayList, nthEq} from "@welshman/lib" import {parseJson, nthEq} from "@welshman/lib"
import { import {displayFeeds} from "@welshman/feeds"
getAddress, import {getAddress, getTagValue, getTagValues} from "@welshman/util"
getTagValue,
getTagValues,
displayRelayUrl,
EVENT_TIME,
MESSAGE,
THREAD,
} from "@welshman/util"
import Link from "@lib/components/Link.svelte"
import Icon from "@lib/components/Icon.svelte" import Icon from "@lib/components/Icon.svelte"
import Button from "@lib/components/Button.svelte" import Button from "@lib/components/Button.svelte"
import AlertDelete from "@app/components/AlertDelete.svelte" import AlertDelete from "@app/components/AlertDelete.svelte"
import type {Alert} from "@app/state" import type {Alert} from "@app/state"
import {alertStatuses} from "@app/state" import {alertStatuses} from "@app/state"
import {makeSpacePath} from "@app/routes"
import {pushModal} from "@app/modal" import {pushModal} from "@app/modal"
type Props = { type Props = {
@@ -28,17 +19,15 @@
const status = $derived($alertStatuses.find(s => s.event.tags.some(nthEq(1, address)))) const status = $derived($alertStatuses.find(s => s.event.tags.some(nthEq(1, address))))
const cron = $derived(getTagValue("cron", alert.tags)) const cron = $derived(getTagValue("cron", alert.tags))
const channel = $derived(getTagValue("channel", alert.tags)) const channel = $derived(getTagValue("channel", alert.tags))
const relay = $derived(getTagValue("relay", alert.tags)!) const feeds = $derived(getTagValues("feed", alert.tags))
const filters = $derived(getTagValues("filter", alert.tags).map(parseJson)) const description = $derived(
const types = $derived.by(() => { getTagValue("description", alert.tags) ||
const t: string[] = [] [
`${cron?.endsWith("1") ? "Weekly" : "Daily"} alert for events`,
if (filters.some(f => f.kinds?.includes(THREAD))) t.push("threads") displayFeeds(feeds.map(parseJson)),
if (filters.some(f => f.kinds?.includes(EVENT_TIME))) t.push("calendar events") `sent via ${channel}.`,
if (filters.some(f => f.kinds?.includes(MESSAGE))) t.push("chat") ].join(" "),
)
return t
})
const startDelete = () => pushModal(AlertDelete, {alert}) const startDelete = () => pushModal(AlertDelete, {alert})
</script> </script>
@@ -48,13 +37,7 @@
<Button class="py-1" onclick={startDelete}> <Button class="py-1" onclick={startDelete}>
<Icon icon="trash-bin-2" /> <Icon icon="trash-bin-2" />
</Button> </Button>
<div class="flex-inline gap-1"> <div class="flex-inline gap-1">{description}</div>
{cron?.endsWith("1") ? "Weekly" : "Daily"} alert for
{displayList(types)} on
<Link class="link" href={makeSpacePath(relay)}>
{displayRelayUrl(relay)}
</Link>, sent via {channel}.
</div>
</div> </div>
{#if status} {#if status}
{@const statusText = getTagValue("status", status.tags) || "error"} {@const statusText = getTagValue("status", status.tags) || "error"}