Skip to content

Commit

Permalink
Clean up unwrapping a tad
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Staab committed Nov 20, 2024
1 parent 851304d commit 7300dc6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 36 deletions.
9 changes: 1 addition & 8 deletions src/app/views/DataImport.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<script lang="ts">
import {sleep} from "hurdak"
import type {TrustedEvent} from "@welshman/util"
import {isTrustedEvent} from "@welshman/util"
import {repository} from "@welshman/app"
Expand All @@ -11,7 +10,6 @@
import FlexColumn from "src/partials/FlexColumn.svelte"
import Heading from "src/partials/Heading.svelte"
import {router} from "src/app/util/router"
import {projections} from "src/engine"
const setFile = e => {
file = e.target.files[0]
Expand All @@ -33,7 +31,7 @@
reader.onload = async loadEvent => {
try {
const data = new Uint8Array(loadEvent.target.result as ArrayBuffer)
const jsonl = new TextDecoder().decode(data).split('\n')
const jsonl = new TextDecoder().decode(data).split("\n")
const newEvents: TrustedEvent[] = []
for (let i = 0; i < jsonl.length; i++) {
Expand All @@ -53,14 +51,9 @@
for (const event of newEvents) {
if (isTrustedEvent(event)) {
repository.publish(event)
projections.push(event)
}
}
while (projections.buffer.length > 0) {
await sleep(100)
}
showInfo("Import complete!")
setTimeout(() => router.clearModals(), 2000)
Expand Down
55 changes: 27 additions & 28 deletions src/engine/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,29 +150,8 @@ export const sessionWithMeta = withGetter(derived(session, $s => $s as SessionWi

export const hasNip44 = derived(signer, $signer => Boolean($signer?.nip44))

// Base state

export const anonymous = withGetter(writable<AnonymousUserState>({follows: [], relays: []}))

export const projections = new Worker<TrustedEvent>({
getKey: prop("kind"),
})

projections.addGlobalHandler(ensurePlaintext)

const decryptKinds = [SEEN_GENERAL, SEEN_CONTEXT, SEEN_CONVERSATION, APP_DATA, FOLLOWS, MUTES]

// Synchronize repository with projections. All events should be published to the
// repository, and when accepted, be propagated to projections. This avoids processing
// the same event multiple times, since repository deduplicates
repository.on("update", ({added}: {added: TrustedEvent[]}) => {
for (const event of added) {
if (decryptKinds.includes(event.kind)) {
projections.push(event)
}
}
})

// Plaintext

export const ensureMessagePlaintext = async (e: TrustedEvent) => {
Expand Down Expand Up @@ -229,6 +208,32 @@ export const ensureUnwrapped = async (event: TrustedEvent) => {
return rumor
}

// Unwrap/decrypt stuff as it comes in

export const unwrapper = new Worker<TrustedEvent>({chunkSize: 10})

unwrapper.addGlobalHandler(async (event: TrustedEvent) => {
if (event.kind === WRAP) {
await ensureUnwrapped(event)
} else {
await ensurePlaintext(event)
}
})

const decryptKinds = [SEEN_GENERAL, SEEN_CONTEXT, SEEN_CONVERSATION, APP_DATA, FOLLOWS, MUTES]

repository.on("update", ({added}: {added: TrustedEvent[]}) => {
for (const event of added) {
if (decryptKinds.includes(event.kind) && event.content && !getPlaintext(event)) {
unwrapper.push(event)
}

if (event.kind === WRAP) {
unwrapper.push(event)
}
}
})

// Settings

export const defaultSettings = {
Expand Down Expand Up @@ -728,13 +733,7 @@ export const subscribe = ({forcePlatform, skipCache, ...request}: MySubscribeReq
request.relays = [...request.relays, LOCAL_RELAY_URL]
}

const sub = baseSubscribe(request)

sub.emitter.on(SubscriptionEvent.Event, async (url: string, event: TrustedEvent) => {
projections.push(await ensureUnwrapped(event))
})

return sub
return baseSubscribe(request)
}

export const subscribePersistent = (request: MySubscribeRequest) => {
Expand Down

0 comments on commit 7300dc6

Please sign in to comment.