Skip to content

Commit

Permalink
path based seen check
Browse files Browse the repository at this point in the history
  • Loading branch information
ticruz38 committed Dec 17, 2024
1 parent f23715e commit 213aed1
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/app/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,5 +103,5 @@ export const loadUserData = async (hints: string[] = []) => {

export const boot = () => {
router.at("login/connect").open({noEscape: true, mini: true})
setChecked(["*", "channels/*"])
setChecked("*")
}
4 changes: 2 additions & 2 deletions src/app/views/ChannelsDetail.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
onMount(() => {
isAccepted = $messages.some(m => m.pubkey === $session.pubkey)
setChecked(["channels/" + channelId])
setChecked("channels/" + channelId)
for (const pubkey of pubkeys) {
loadInboxRelaySelections(pubkey)
Expand All @@ -42,7 +42,7 @@
})
onDestroy(() => {
setChecked(["channels/" + channelId])
setChecked("channels/" + channelId)
})
document.title = `Direct Messages`
Expand Down
2 changes: 1 addition & 1 deletion src/app/views/ChannelsList.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
}
})
const markAllChannelsRead = () => setChecked(["channels/*"], now())
const markAllChannelsRead = () => setChecked("channels/*", now())
document.title = "Direct Messages"
</script>
Expand Down
4 changes: 2 additions & 2 deletions src/app/views/NotificationSectionMain.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@
})
onMount(() => {
setChecked(["replies", "mentions"])
setChecked("notes/*")
return () => {
setChecked(["replies", "mentions"])
setChecked("notes/*")
}
})
</script>
Expand Down
4 changes: 2 additions & 2 deletions src/app/views/NotificationSectionReactions.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@
})
onMount(() => {
setChecked(["reactions", "zaps"])
setChecked("reactions/*")
return () => {
setChecked(["reactions", "zaps"])
setChecked("reactions/*")
}
})
</script>
Expand Down
2 changes: 1 addition & 1 deletion src/app/views/Onboarding.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
// Start our notifications listener
listenForNotifications()
setChecked(["*", "channels/*"])
setChecked("*")
}
onMount(async () => {
Expand Down
13 changes: 4 additions & 9 deletions src/engine/notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,8 @@ export const isSeen = derived(
$getSeenAt => (key: string, event: TrustedEvent) => $getSeenAt(key, event) > 0,
)

export const setChecked = (keys: string[], ts = now()) =>
checked.update(state => {
keys.forEach(key => {
state = {...state, [key]: ts}
})
return state
})
export const setChecked = (path: string, ts = now()) =>
checked.update(state => ({...state, [path]: ts}))

// Notifications

Expand All @@ -37,7 +32,7 @@ export const mainNotifications = derived(
)

export const unreadMainNotifications = derived([isSeen, mainNotifications], ([$isSeen, events]) =>
events.filter(e => !$isSeen("replies", e) && !$isSeen("mentions", e)),
events.filter(e => !$isSeen("notes/*", e)),
)

export const hasNewNotifications = derived(
Expand Down Expand Up @@ -78,5 +73,5 @@ export const reactionNotifications = derived(

export const unreadReactionNotifications = derived(
[isSeen, reactionNotifications],
([$isSeen, events]) => events.filter(e => !$isSeen("reactions", e) && !$isSeen("zaps", e)),
([$isSeen, events]) => events.filter(e => !$isSeen("reactions/*", e)),
)
6 changes: 2 additions & 4 deletions src/engine/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,10 +371,8 @@ export const checked = writable<Record<string, number>>({})

export const deriveChecked = (key: string) => derived(checked, prop(key))

export const getSeenAt = derived([checked], ([$checked]) => (key: string, event: TrustedEvent) => {
const match = $checked[key]
const fallback = $checked[key.includes("channels") ? "channels/*" : "*"]
const ts = max([match, fallback])
export const getSeenAt = derived([checked], ([$checked]) => (path: string, event: TrustedEvent) => {
const ts = max([$checked[path], $checked[path.split("/")[0] + "/*"], $checked["*"]])

if (ts >= event.created_at) return ts

Expand Down

0 comments on commit 213aed1

Please sign in to comment.