Fix more type errors

This commit is contained in:
Jonathan Staab 2023-07-21 13:23:46 -07:00
parent 57f48d54b2
commit 4dd45cf45e
25 changed files with 208 additions and 207 deletions

View File

@ -3,6 +3,7 @@
import "@fortawesome/fontawesome-free/css/solid.css"
import type {ComponentType, SvelteComponentTyped} from "svelte"
import type {Relay} from "src/engine"
import {onMount} from "svelte"
import {Router, links} from "svelte-routing"
import {globalHistory} from "svelte-routing/src/history"
@ -63,11 +64,18 @@
$: style.textContent = `:root { ${getThemeVariables($theme)}; background: var(--gray-8); }`
tryFunc(() =>
(
navigator.registerProtocolHandler as (scheme: string, handler: string, name: string) => void
)?.("web+nostr", `${location.origin}/%s`, appName)
)
try {
const handler = navigator.registerProtocolHandler as (
scheme: string,
handler: string,
name: string
) => void
handler?.("web+nostr", `${location.origin}/%s`, appName)
handler?.("nostr", `${location.origin}/%s`, appName)
} catch (e) {
// pass
}
const seenChallenges = new Set()
@ -160,7 +168,7 @@
engine.Nip65.relays
.get()
.filter(r => (r.info?.last_checked || 0) < now() - seconds(7, "day"))
).slice(0, 10)
).slice(0, 10) as Relay[]
for (const relay of staleRelays) {
tryFetch(async () => {

View File

@ -18,11 +18,8 @@
const createNote = () => {
const pubkeyMatch = $location.pathname.match(/people\/(npub1[0-9a-z]+)/)
const pubkey = pubkeyMatch ? nip19.decode(pubkeyMatch[1]).data : null
const relayMatch = $location.pathname.match(/relays\/(.+)/)
const relay = tryFunc(() => atob(relayMatch[1])) || relayMatch?.[1]
const relays = relay ? [relay] : null
modal.push({type: "note/create", pubkey, relays})
modal.push({type: "note/create", pubkey})
}
</script>

View File

@ -38,41 +38,42 @@ const engine = new Engine({
ENABLE_ZAPS,
})
export default engine.components
export const Alerts = engine.components.Alerts
export const Builder = engine.components.Builder
export const Content = engine.components.Content
export const Crypt = engine.components.Crypt
export const Directory = engine.components.Directory
export const Events = engine.components.Events
export const Keys = engine.components.Keys
export const Meta = engine.components.Meta
export const Network = engine.components.Network
export const Nip02 = engine.components.Nip02
export const Nip04 = engine.components.Nip04
export const Nip05 = engine.components.Nip05
export const Nip28 = engine.components.Nip28
export const Nip57 = engine.components.Nip57
export const Nip65 = engine.components.Nip65
export const Outbox = engine.components.Outbox
export const PubkeyLoader = engine.components.PubkeyLoader
export const Storage = engine.components.Storage
export const User = engine.components.User
export const alerts = engine.components.Alerts
export const builder = engine.components.Builder
export const content = engine.components.Content
export const directory = engine.components.Directory
export const events = engine.components.Events
export const keys = engine.components.Keys
export const meta = engine.components.Meta
export const network = engine.components.Network
export const nip02 = engine.components.Nip02
export const nip04 = engine.components.Nip04
export const nip05 = engine.components.Nip05
export const nip28 = engine.components.Nip28
export const nip57 = engine.components.Nip57
export const nip65 = engine.components.Nip65
export const outbox = engine.components.Outbox
export const pubkeyLoader = engine.components.PubkeyLoader
export const storage = engine.components.Storage
export const user = engine.components.User
export default engine
export const E = engine
export const Alerts = engine.Alerts
export const Builder = engine.Builder
export const Content = engine.Content
export const Crypt = engine.Crypt
export const Directory = engine.Directory
export const Events = engine.Events
export const Keys = engine.Keys
export const Meta = engine.Meta
export const Network = engine.Network
export const Nip02 = engine.Nip02
export const Nip04 = engine.Nip04
export const Nip05 = engine.Nip05
export const Nip28 = engine.Nip28
export const Nip57 = engine.Nip57
export const Nip65 = engine.Nip65
export const Outbox = engine.Outbox
export const PubkeyLoader = engine.PubkeyLoader
export const Storage = engine.Storage
export const User = engine.User
export const alerts = engine.Alerts
export const builder = engine.Builder
export const content = engine.Content
export const directory = engine.Directory
export const events = engine.Events
export const keys = engine.Keys
export const meta = engine.Meta
export const network = engine.Network
export const nip02 = engine.Nip02
export const nip04 = engine.Nip04
export const nip05 = engine.Nip05
export const nip28 = engine.Nip28
export const nip57 = engine.Nip57
export const nip65 = engine.Nip65
export const outbox = engine.Outbox
export const pubkeyLoader = engine.PubkeyLoader
export const storage = engine.Storage
export const user = engine.User

View File

@ -7,14 +7,16 @@
import BorderLeft from "src/partials/BorderLeft.svelte"
import Content from "src/partials/Content.svelte"
import ListSummary from "src/app/shared/ListSummary.svelte"
import {user, content} from "src/app/engine"
import E from "src/app/engine"
export let item
const lists = content.lists.derived(() => user.getLists())
const lists = E.Content.lists.derived(() => E.User.getLists())
const label = item.type === "p" ? "person" : "topic"
const modifyList = updateIn("tags", tags => (tags || []).concat([[item.type, item.value]]))
const modifyList = updateIn("tags", (tags: string[][]) =>
(tags || []).concat([[item.type, item.value]])
)
const selectlist = list => modal.replace({type: "list/edit", list: modifyList(list)})
</script>

View File

@ -21,32 +21,30 @@ import {User} from "./components/User"
export class Engine {
Env: Env
components = {
Alerts: new Alerts(),
Builder: new Builder(),
Content: new Content(),
Crypt: new Crypt(),
Directory: new Directory(),
Events: new Events(),
Keys: new Keys(),
Meta: new Meta(),
Network: new Network(),
Nip02: new Nip02(),
Nip04: new Nip04(),
Nip05: new Nip05(),
Nip28: new Nip28(),
Nip57: new Nip57(),
Nip65: new Nip65(),
Outbox: new Outbox(),
PubkeyLoader: new PubkeyLoader(),
Storage: new Storage(),
User: new User(),
}
Alerts = new Alerts()
Builder = new Builder()
Content = new Content()
Crypt = new Crypt()
Directory = new Directory()
Events = new Events()
Keys = new Keys()
Meta = new Meta()
Network = new Network()
Nip02 = new Nip02()
Nip04 = new Nip04()
Nip05 = new Nip05()
Nip28 = new Nip28()
Nip57 = new Nip57()
Nip65 = new Nip65()
Outbox = new Outbox()
PubkeyLoader = new PubkeyLoader()
Storage = new Storage()
User = new User()
constructor(Env: Env) {
this.Env = Env
for (const component of Object.values(this.components)) {
for (const component of Object.values(this)) {
component.initialize?.(this)
}
}

View File

@ -12,7 +12,7 @@ export class Alerts {
hasNewNotfications = derived([this.lastChecked, this.latestNotification], ([c, n]) => n > c)
initialize(engine: Engine) {
const {Alerts, Events, Keys, User} = engine.components
const {Alerts, Events, Keys, User} = engine
const isMention = (e: Event) => Tags.from(e).pubkeys().includes(Keys.pubkey.get())

View File

@ -24,15 +24,15 @@ const buildEvent = (kind: number, {content = "", tags = [], tagClient = true}: E
export class Builder {
engine: Engine
getEventHint = (event: Event) => first(this.engine.components.Nip65.getEventHints(1, event)) || ""
getEventHint = (event: Event) => first(this.engine.Nip65.getEventHints(1, event)) || ""
getPubkeyHint = (pubkey: string): string =>
first(this.engine.components.Nip65.getPubkeyHints(1, pubkey)) || ""
first(this.engine.Nip65.getPubkeyHints(1, pubkey)) || ""
getPubkeyPetname = (pubkey: string) => {
const profile = this.engine.components.Directory.getProfile(pubkey)
const profile = this.engine.Directory.getProfile(pubkey)
return profile ? this.engine.components.Directory.displayProfile(profile) : ""
return profile ? this.engine.Directory.displayProfile(profile) : ""
}
mention = (pubkey: string): string[] => {
@ -171,7 +171,7 @@ export class Builder {
deleteNaddrs = (naddrs: string[]) => buildEvent(5, {tags: naddrs.map(naddr => ["a", naddr])})
createLabel = (payload: {content: string; tags: string[][]}) => buildEvent(1985, payload)
createLabel = (payload: EventOpts) => buildEvent(1985, payload)
initialize(engine: Engine) {
this.engine = engine

View File

@ -33,11 +33,11 @@ export class Content {
}
}
engine.components.Events.addHandler(1, processTopics)
engine.Events.addHandler(1, processTopics)
engine.components.Events.addHandler(42, processTopics)
engine.Events.addHandler(42, processTopics)
engine.components.Events.addHandler(30001, (e: Event) => {
engine.Events.addHandler(30001, (e: Event) => {
const {pubkey, kind, created_at} = e
const name = Tags.from(e).getMeta("d")
const naddr = nip19.naddrEncode({identifier: name, pubkey, kind})
@ -58,7 +58,7 @@ export class Content {
})
})
engine.components.Events.addHandler(5, (e: Event) => {
engine.Events.addHandler(5, (e: Event) => {
Tags.from(e)
.type("a")
.values()

View File

@ -8,14 +8,14 @@ export class Crypt {
engine: Engine
async encrypt(pubkey: string, message: string) {
const {method, privkey} = this.engine.components.Keys.current.get() as KeyState
const {method, privkey} = this.engine.Keys.current.get() as KeyState
return switcherFn(method, {
extension: () =>
this.engine.components.Keys.withExtension((ext: any) => ext.nip04.encrypt(pubkey, message)),
this.engine.Keys.withExtension((ext: any) => ext.nip04.encrypt(pubkey, message)),
privkey: () => nip04.encrypt(privkey as string, pubkey, message),
bunker: async () => {
const ndk = await this.engine.components.Keys.getNDK()
const ndk = await this.engine.Keys.getNDK()
const user = ndk.getUser({hexpubkey: pubkey})
return ndk.signer.encrypt(user, message)
@ -24,11 +24,11 @@ export class Crypt {
}
async decrypt(pubkey: string, message: string) {
const {method, privkey} = this.engine.components.Keys.current.get() as KeyState
const {method, privkey} = this.engine.Keys.current.get() as KeyState
return switcherFn(method, {
extension: () =>
this.engine.components.Keys.withExtension((ext: any) => {
this.engine.Keys.withExtension((ext: any) => {
return new Promise(async resolve => {
let result
@ -53,7 +53,7 @@ export class Crypt {
)
},
bunker: async () => {
const ndk = await this.engine.components.Keys.getNDK()
const ndk = await this.engine.Keys.getNDK()
const user = ndk.getUser({hexpubkey: pubkey})
return ndk.signer.decrypt(user, message)
@ -62,13 +62,13 @@ export class Crypt {
}
async encryptJson(data: any) {
const {pubkey} = this.engine.components.Keys.current.get() as KeyState
const {pubkey} = this.engine.Keys.current.get() as KeyState
return this.encrypt(pubkey, JSON.stringify(data))
}
async decryptJson(data: string) {
const {pubkey} = this.engine.components.Keys.current.get() as KeyState
const {pubkey} = this.engine.Keys.current.get() as KeyState
return tryJson(async () => JSON.parse(await this.decrypt(pubkey, data)))
}

View File

@ -40,7 +40,7 @@ export class Directory {
})
initialize(engine: Engine) {
engine.components.Events.addHandler(0, (e: Event) => {
engine.Events.addHandler(0, (e: Event) => {
tryJson(() => {
const kind0 = JSON.parse(e.content)
const profile = this.profiles.key(e.pubkey)

View File

@ -14,7 +14,7 @@ export class Events {
initialize(engine: Engine) {
this.queue.listen(async event => {
if (event.pubkey === engine.components.Keys.pubkey.get()) {
if (event.pubkey === engine.Keys.pubkey.get()) {
this.cache.key(event.id).set(event)
}

View File

@ -4,7 +4,7 @@ import {switcher} from "hurdak"
import {collection} from "src/engine/util/store"
import type {RelayStat} from "src/engine/types"
import type {Engine} from "src/engine/Engine"
import type {Network} from "src/engine/components/Network"
import type {Network} from "src/engine/Network"
export class Meta {
Network: Network
@ -55,7 +55,7 @@ export class Meta {
}
initialize(engine: Engine) {
this.Network = engine.components.Network
this.Network = engine.Network
this.Network.pool.on("open", ({url}: {url: string}) => {
this.relayStats.key(url).merge({last_opened: now(), last_activity: now()})

View File

@ -67,7 +67,7 @@ export class Network {
let target
const muxUrl = this.engine.components.User.getSetting("multiplextr_url")
const muxUrl = this.engine.User.getSetting("multiplextr_url")
// Try to use our multiplexer, but if it fails to connect fall back to relays. If
// we're only connecting to a single relay, just do it directly, unless we already
@ -106,7 +106,7 @@ export class Network {
// Eagerly connect and handle AUTH
executor.target.sockets.forEach((socket: any) => {
const {limitation} = this.engine.components.Nip65.getRelayInfo(socket.url)
const {limitation} = this.engine.Nip65.getRelayInfo(socket.url)
const waitForBoot = limitation?.payment_required || limitation?.auth_required
// This happens automatically, but kick it off anyway
@ -258,7 +258,7 @@ export class Network {
this.emitter.emit("event", {url, event})
if (shouldProcess) {
this.engine.components.Events.queue.push(event)
this.engine.Events.queue.push(event)
}
onEvent?.(event)

View File

@ -60,7 +60,7 @@ export class Nip02 {
isIgnoring = (a: string, b: string) => this.getMutesSet(a).has(b)
initialize(engine: Engine) {
engine.components.Events.addHandler(3, e => {
engine.Events.addHandler(3, e => {
const entry = this.graph.key(e.pubkey).get()
if (e.created_at < entry?.petnames_updated_at) {
@ -74,7 +74,7 @@ export class Nip02 {
})
})
engine.components.Events.addHandler(10000, e => {
engine.Events.addHandler(10000, e => {
const entry = this.graph.key(e.pubkey).get()
if (e.created_at < entry?.mutes_updated_at) {

View File

@ -23,7 +23,7 @@ export class Nip04 {
searchContacts = this.messages.derived($messages => {
const pubkeySet = new Set(pluck("pubkey", $messages))
const searchProfiles = this.engine.components.Directory.searchProfiles.get()
const searchProfiles = this.engine.Directory.searchProfiles.get()
return (q: string) =>
searchProfiles(q)
@ -34,10 +34,10 @@ export class Nip04 {
initialize(engine: Engine) {
this.engine = engine
engine.components.Events.addHandler(30078, async e => {
engine.Events.addHandler(30078, async e => {
if (Tags.from(e).getMeta("d") === appDataKeys.NIP04_LAST_CHECKED) {
await tryJson(async () => {
const payload = await engine.components.Crypt.decryptJson(e.content)
const payload = await engine.Crypt.decryptJson(e.content)
for (const key of Object.keys(payload)) {
// Backwards compat from when we used to prefix id/pubkey
@ -56,15 +56,15 @@ export class Nip04 {
}
})
engine.components.Events.addHandler(4, async e => {
if (!engine.components.Keys.canSign.get()) {
engine.Events.addHandler(4, async e => {
if (!engine.Keys.canSign.get()) {
return
}
const author = e.pubkey
const recipient = Tags.from(e).type("p").values().first()
if (![author, recipient].includes(engine.components.Keys.pubkey.get())) {
if (![author, recipient].includes(engine.Keys.pubkey.get())) {
return
}
@ -73,18 +73,18 @@ export class Nip04 {
}
await tryFunc(async () => {
const other = engine.components.Keys.pubkey.get() === author ? recipient : author
const other = engine.Keys.pubkey.get() === author ? recipient : author
this.messages.key(e.id).set({
id: e.id,
contact: other,
pubkey: e.pubkey,
created_at: e.created_at,
content: await engine.components.Crypt.decrypt(other, e.content),
content: await engine.Crypt.decrypt(other, e.content),
tags: e.tags,
})
if (engine.components.Keys.pubkey.get() === author) {
if (engine.Keys.pubkey.get() === author) {
const contact = this.contacts.key(recipient).get()
this.contacts.key(recipient).merge({

View File

@ -15,7 +15,7 @@ export class Nip05 {
handle.address.startsWith("_@") ? last(handle.address.split("@")) : handle.address
initialize(engine: Engine) {
engine.components.Events.addHandler(0, e => {
engine.Events.addHandler(0, e => {
tryJson(async () => {
const kind0 = JSON.parse(e.content)
const handle = this.handles.key(e.pubkey)

View File

@ -29,7 +29,7 @@ export class Nip28 {
searchChannels = this.getSearchChannels(this.channels)
initialize(engine: Engine) {
engine.components.Events.addHandler(40, (e: Event) => {
engine.Events.addHandler(40, (e: Event) => {
const channel = this.channels.key(e.id).get()
if (e.created_at < channel?.updated_at) {
@ -50,7 +50,7 @@ export class Nip28 {
})
})
engine.components.Events.addHandler(41, (e: Event) => {
engine.Events.addHandler(41, (e: Event) => {
const channelId = Tags.from(e).getMeta("e")
if (!channelId) {
@ -81,10 +81,10 @@ export class Nip28 {
})
})
engine.components.Events.addHandler(30078, async (e: Event) => {
engine.Events.addHandler(30078, async (e: Event) => {
if (Tags.from(e).getMeta("d") === appDataKeys.NIP28_LAST_CHECKED) {
await tryJson(async () => {
const payload = await engine.components.Crypt.decryptJson(e.content)
const payload = await engine.Crypt.decryptJson(e.content)
for (const key of Object.keys(payload)) {
// Backwards compat from when we used to prefix id/pubkey
@ -103,10 +103,10 @@ export class Nip28 {
}
})
engine.components.Events.addHandler(30078, async (e: Event) => {
engine.Events.addHandler(30078, async (e: Event) => {
if (Tags.from(e).getMeta("d") === appDataKeys.NIP28_ROOMS_JOINED) {
await tryJson(async () => {
const channelIds = await engine.components.Crypt.decryptJson(e.content)
const channelIds = await engine.Crypt.decryptJson(e.content)
// Just a bug from when I was building the feature, remove someday
if (!Array.isArray(channelIds)) {
@ -124,7 +124,7 @@ export class Nip28 {
}
})
engine.components.Events.addHandler(42, (e: Event) => {
engine.Events.addHandler(42, (e: Event) => {
if (this.messages.key(e.id).exists()) {
return
}
@ -147,7 +147,7 @@ export class Nip28 {
tags: e.tags,
})
if (e.pubkey === engine.components.Keys.pubkey.get()) {
if (e.pubkey === engine.Keys.pubkey.get()) {
this.channels.key(channelId).merge({last_sent: e.created_at, hints})
} else {
this.channels.key(channelId).merge({last_received: e.created_at, hints})

View File

@ -86,7 +86,7 @@ export class Nip57 {
}
initialize(engine: Engine) {
engine.components.Events.addHandler(0, (e: Event) => {
engine.Events.addHandler(0, (e: Event) => {
tryJson(async () => {
const kind0 = JSON.parse(e.content)
const zapper = this.zappers.key(e.pubkey)

View File

@ -26,7 +26,10 @@ export class Nip65 {
}
}
setPolicy = ({pubkey, created_at}: {pubkey: string, created_at: number}, relays: RelayPolicyEntry[]) => {
setPolicy = (
{pubkey, created_at}: {pubkey: string; created_at: number},
relays: RelayPolicyEntry[]
) => {
if (relays?.length > 0) {
if (created_at < this.policies.key(pubkey).get()?.created_at) {
return
@ -91,7 +94,7 @@ export class Nip65 {
for (const url of chain(
hints,
this.engine.components.User.getRelayUrls("write"),
this.engine.User.getRelayUrls("write"),
this.engine.Env.DEFAULT_RELAYS
)) {
if (seen.has(url)) {
@ -104,8 +107,8 @@ export class Nip65 {
if (!isShareableRelay(url)) {
bad.push(url)
} else if (
this.engine.components.Network.relayHasError(url) ||
this.engine.components.Meta.getRelayQuality(url)[0] < 0.5
this.engine.Network.relayHasError(url) ||
this.engine.Meta.getRelayQuality(url)[0] < 0.5
) {
bad.push(url)
} else {
@ -201,13 +204,13 @@ export class Nip65 {
initialize(engine: Engine) {
this.engine = engine
engine.components.Events.addHandler(2, e => {
engine.Events.addHandler(2, e => {
if (isShareableRelay(e.content)) {
this.addRelay(normalizeRelayUrl(e.content))
}
})
engine.components.Events.addHandler(3, e => {
engine.Events.addHandler(3, e => {
this.setPolicy(
e,
tryJson<RelayPolicyEntry[]>(() => {
@ -225,7 +228,7 @@ export class Nip65 {
)
})
engine.components.Events.addHandler(10002, e => {
engine.Events.addHandler(10002, e => {
this.setPolicy(
e,
Tags.from(e)

View File

@ -3,7 +3,7 @@ import type {UnsignedEvent} from "nostr-tools"
import {assoc} from "ramda"
import {doPipe} from "hurdak"
import {now} from "src/util/misc"
import type {Progress} from "src/engine/components/Network"
import type {Progress} from "src/engine/Network"
import type {Engine} from "src/engine/Engine"
import type {Event} from "src/engine/types"
@ -18,12 +18,12 @@ export class Outbox {
const event = {
...rawEvent,
created_at: now(),
pubkey: this.engine.components.Keys.pubkey.get(),
pubkey: this.engine.Keys.pubkey.get(),
}
event.id = getEventHash(event as UnsignedEvent)
return this.engine.components.Keys.sign(event as Event)
return this.engine.Keys.sign(event as Event)
}
publish = async (
@ -35,14 +35,14 @@ export class Outbox {
const event = rawEvent.sig ? (rawEvent as Event) : await this.prepEvent(rawEvent)
if (!relays) {
relays = this.engine.components.User.getRelayUrls("write")
relays = this.engine.User.getRelayUrls("write")
}
// return console.log(event)
this.engine.components.Events.queue.push(event)
this.engine.Events.queue.push(event)
return [event, this.engine.components.Network.publish({event, relays, onProgress, verb})]
return [event, this.engine.Network.publish({event, relays, onProgress, verb})]
}
initialize(engine: Engine) {

View File

@ -27,7 +27,7 @@ export class PubkeyLoader {
this.attemptedPubkeys.add(pubkey)
if (this.engine.components.Directory.profiles.key(pubkey).get()?.updated_at || 0 > since) {
if (this.engine.Directory.profiles.key(pubkey).get()?.updated_at || 0 > since) {
continue
}
@ -49,9 +49,9 @@ export class PubkeyLoader {
return relays
}
return this.engine.components.Nip65.mergeHints(
this.engine.components.User.getSetting("relay_limit"),
chunk.map(pubkey => this.engine.components.Nip65.getPubkeyHints(3, pubkey))
return this.engine.Nip65.mergeHints(
this.engine.User.getSetting("relay_limit"),
chunk.map(pubkey => this.engine.Nip65.getPubkeyHints(3, pubkey))
)
}
@ -73,7 +73,7 @@ export class PubkeyLoader {
pluck(
"complete",
chunk(256, pubkeys).map((chunk: string[]) =>
this.engine.components.Network.subscribe({
this.engine.Network.subscribe({
relays: getChunkRelays(chunk),
filter: getChunkFilter(chunk),
timeout: 10_000,

View File

@ -17,8 +17,7 @@ const sortContacts = sortBy((e: Contact) => -Math.max(e.last_checked || 0, e.las
const policy = (key: string, max: number, sort: (xs: any[]) => any[]) => ({key, max, sort})
const getStore = (key: string, engine: Engine) =>
getPath(key.split("."), engine.components) as Collection<any>
const getStore = (key: string, engine: Engine) => getPath(key.split("."), engine) as Collection<any>
export class Storage {
engine: Engine
@ -41,9 +40,9 @@ export class Storage {
}
getPubkeyWhitelist = () => {
const pubkeys = this.engine.components.Keys.keyState.get().map(prop("pubkey"))
const pubkeys = this.engine.Keys.keyState.get().map(prop("pubkey"))
return [new Set(pubkeys), this.engine.components.Nip02.getFollowsSet(pubkeys)]
return [new Set(pubkeys), this.engine.Nip02.getFollowsSet(pubkeys)]
}
sortByPubkeyWhitelist = (fallback: (x: any) => number) => (rows: Record<string, any>[]) => {

View File

@ -10,9 +10,9 @@ export class User {
engine: Engine
settings: Writable<Record<string, any>>
getPubkey = () => this.engine.components.Keys.pubkey.get()
getPubkey = () => this.engine.Keys.pubkey.get()
getStateKey = () => (this.engine.components.Keys.canSign.get() ? this.getPubkey() : "anonymous")
getStateKey = () => (this.engine.Keys.canSign.get() ? this.getPubkey() : "anonymous")
// Settings
@ -23,40 +23,36 @@ export class User {
setSettings = async (settings: Record<string, any>) => {
this.settings.update($settings => ({...$settings, ...settings}))
if (this.engine.components.Keys.canSign.get()) {
if (this.engine.Keys.canSign.get()) {
const d = appDataKeys.USER_SETTINGS
const v = await this.engine.components.Crypt.encryptJson(settings)
const v = await this.engine.Crypt.encryptJson(settings)
return this.engine.components.Outbox.publish(this.engine.components.Builder.setAppData(d, v))
return this.engine.Outbox.publish(this.engine.Builder.setAppData(d, v))
}
}
setAppData = async (d: string, content: any) => {
const v = await this.engine.components.Crypt.encryptJson(content)
const v = await this.engine.Crypt.encryptJson(content)
return this.engine.components.Outbox.publish(this.engine.components.Builder.setAppData(d, v))
return this.engine.Outbox.publish(this.engine.Builder.setAppData(d, v))
}
// Nip65
getRelays = (mode?: string) =>
this.engine.components.Nip65.getPubkeyRelays(this.getStateKey(), mode)
getRelays = (mode?: string) => this.engine.Nip65.getPubkeyRelays(this.getStateKey(), mode)
getRelayUrls = (mode?: string) =>
this.engine.components.Nip65.getPubkeyRelayUrls(this.getStateKey(), mode)
getRelayUrls = (mode?: string) => this.engine.Nip65.getPubkeyRelayUrls(this.getStateKey(), mode)
setRelays = (relays: RelayPolicyEntry[]) => {
if (this.engine.components.Keys.canSign.get()) {
return this.engine.components.Outbox.publish(this.engine.components.Builder.setRelays(relays))
if (this.engine.Keys.canSign.get()) {
return this.engine.Outbox.publish(this.engine.Builder.setRelays(relays))
} else {
this.engine.components.Nip65.setPolicy(
{pubkey: this.getStateKey(), created_at: now()},
relays
)
this.engine.Nip65.setPolicy({pubkey: this.getStateKey(), created_at: now()}, relays)
}
}
addRelay = (url: string) => this.setRelays(this.getRelays().concat({url, read: true, write: true}))
addRelay = (url: string) =>
this.setRelays(this.getRelays().concat({url, read: true, write: true}))
removeRelay = (url: string) =>
this.setRelays(reject(whereEq({url: normalizeRelayUrl(url)}), this.getRelays()))
@ -66,37 +62,35 @@ export class User {
// Nip02
getPetnames = () => this.engine.components.Nip02.getPetnames(this.getStateKey())
getPetnames = () => this.engine.Nip02.getPetnames(this.getStateKey())
getMutedTags = () => this.engine.components.Nip02.getMutedTags(this.getStateKey())
getMutedTags = () => this.engine.Nip02.getMutedTags(this.getStateKey())
getFollowsSet = () => this.engine.components.Nip02.getFollowsSet(this.getStateKey())
getFollowsSet = () => this.engine.Nip02.getFollowsSet(this.getStateKey())
getMutesSet = () => this.engine.components.Nip02.getMutesSet(this.getStateKey())
getMutesSet = () => this.engine.Nip02.getMutesSet(this.getStateKey())
getFollows = () => this.engine.components.Nip02.getFollows(this.getStateKey())
getFollows = () => this.engine.Nip02.getFollows(this.getStateKey())
getMutes = () => this.engine.components.Nip02.getMutes(this.getStateKey())
getMutes = () => this.engine.Nip02.getMutes(this.getStateKey())
getNetworkSet = () => this.engine.components.Nip02.getNetworkSet(this.getStateKey())
getNetworkSet = () => this.engine.Nip02.getNetworkSet(this.getStateKey())
getNetwork = () => this.engine.components.Nip02.getNetwork(this.getStateKey())
getNetwork = () => this.engine.Nip02.getNetwork(this.getStateKey())
isFollowing = (pubkey: string) => this.engine.components.Nip02.isFollowing(this.getStateKey(), pubkey)
isFollowing = (pubkey: string) => this.engine.Nip02.isFollowing(this.getStateKey(), pubkey)
isIgnoring = (pubkeyOrEventId: string) =>
this.engine.components.Nip02.isIgnoring(this.getStateKey(), pubkeyOrEventId)
this.engine.Nip02.isIgnoring(this.getStateKey(), pubkeyOrEventId)
setProfile = ($profile: Record<string, any>) =>
this.engine.components.Outbox.publish(this.engine.components.Builder.setProfile($profile))
this.engine.Outbox.publish(this.engine.Builder.setProfile($profile))
setPetnames = async ($petnames: string[][]) => {
if (this.engine.components.Keys.canSign.get()) {
await this.engine.components.Outbox.publish(
this.engine.components.Builder.setPetnames($petnames)
)
if (this.engine.Keys.canSign.get()) {
await this.engine.Outbox.publish(this.engine.Builder.setPetnames($petnames))
} else {
this.engine.components.Nip02.graph.key(this.getStateKey()).merge({
this.engine.Nip02.graph.key(this.getStateKey()).merge({
updated_at: now(),
petnames_updated_at: now(),
petnames: $petnames,
@ -108,7 +102,7 @@ export class User {
this.setPetnames(
this.getPetnames()
.filter(t => t[1] !== pubkey)
.concat([this.engine.components.Builder.mention(pubkey)])
.concat([this.engine.Builder.mention(pubkey)])
)
unfollow = (pubkey: string) =>
@ -123,12 +117,10 @@ export class User {
applyMutes = (events: Event[]) => reject(this.isMuted, events)
setMutes = async ($mutes: string[][]) => {
if (this.engine.components.Keys.canSign.get()) {
await this.engine.components.Outbox.publish(
this.engine.components.Builder.setMutes($mutes.map(t => t.slice(0, 2)))
)
if (this.engine.Keys.canSign.get()) {
await this.engine.Outbox.publish(this.engine.Builder.setMutes($mutes.map(t => t.slice(0, 2))))
} else {
this.engine.components.Nip02.graph.key(this.getStateKey()).merge({
this.engine.Nip02.graph.key(this.getStateKey()).merge({
updated_at: now(),
mutes_updated_at: now(),
mutes: $mutes,
@ -137,30 +129,31 @@ export class User {
}
mute = (type: string, value: string) =>
this.setMutes(reject((t: string[]) => t[1] === value, this.getMutedTags()).concat([[type, value]]))
this.setMutes(
reject((t: string[]) => t[1] === value, this.getMutedTags()).concat([[type, value]])
)
unmute = (target: string) => this.setMutes(reject((t: string[]) => t[1] === target, this.getMutedTags()))
unmute = (target: string) =>
this.setMutes(reject((t: string[]) => t[1] === target, this.getMutedTags()))
// Lists
getLists = (f?: (l: List) => boolean) =>
this.engine.components.Content.getLists(
l => l.pubkey === this.getStateKey() && (f ? f(l) : true)
)
this.engine.Content.getLists(l => l.pubkey === this.getStateKey() && (f ? f(l) : true))
putList = (name: string, params: string[][], relays: string[]) =>
this.engine.components.Outbox.publish(
this.engine.components.Builder.createList([["d", name]].concat(params).concat(relays))
this.engine.Outbox.publish(
this.engine.Builder.createList([["d", name]].concat(params).concat(relays))
)
removeList = (naddr: string) =>
this.engine.components.Outbox.publish(this.engine.components.Builder.deleteNaddrs([naddr]))
this.engine.Outbox.publish(this.engine.Builder.deleteNaddrs([naddr]))
// Messages
markAllMessagesRead = () => {
const lastChecked = fromPairs(
uniq(pluck("contact", this.engine.components.Nip04.messages.get())).map(k => [k, now()])
uniq(pluck("contact", this.engine.Nip04.messages.get())).map(k => [k, now()])
)
return this.setAppData(appDataKeys.NIP04_LAST_CHECKED, lastChecked)
@ -168,7 +161,7 @@ export class User {
setContactLastChecked = (pubkey: string) => {
const lastChecked = fromPairs(
this.engine.components.Nip04.contacts
this.engine.Nip04.contacts
.get()
.filter(prop("last_checked"))
.map(r => [r.id, r.last_checked])
@ -181,7 +174,7 @@ export class User {
setChannelLastChecked = (id: string) => {
const lastChecked = fromPairs(
this.engine.components.Nip28.channels
this.engine.Nip28.channels
.get()
.filter(prop("last_checked"))
.map(r => [r.id, r.last_checked])
@ -193,18 +186,18 @@ export class User {
saveChannels = () =>
this.setAppData(
appDataKeys.NIP28_ROOMS_JOINED,
pluck("id", this.engine.components.Nip28.channels.get().filter(whereEq({joined: true})))
pluck("id", this.engine.Nip28.channels.get().filter(whereEq({joined: true})))
)
joinChannel = (id: string) => {
this.engine.components.Nip28.channels.key(id).merge({joined: false})
this.engine.Nip28.channels.key(id).merge({joined: false})
return this.saveChannels()
}
leaveChannel = (id: string) => {
this.engine.components.Nip28.channels.key(id).merge({joined: false})
this.engine.components.Nip28.messages.reject(m => m.channel === id)
this.engine.Nip28.channels.key(id).merge({joined: false})
this.engine.Nip28.messages.reject(m => m.channel === id)
return this.saveChannels()
}
@ -222,12 +215,12 @@ export class User {
multiplextr_url: engine.Env.MULTIPLEXTR_URL,
})
engine.components.Events.addHandler(30078, async e => {
engine.Events.addHandler(30078, async e => {
if (
Tags.from(e).getMeta("d") === "coracle/settings/v1" &&
e.created_at > this.getSetting("last_updated")
) {
const updates = await engine.components.Crypt.decryptJson(e.content)
const updates = await engine.Crypt.decryptJson(e.content)
if (updates) {
this.settings.update($settings => ({

View File

@ -3,7 +3,7 @@ import {ensurePlural, first} from "hurdak"
import {now} from "src/util/misc"
import type {Filter, Event} from "src/engine/types"
import type {Subscription} from "src/engine/util/Subscription"
import type {Network} from "src/engine/components/Network"
import type {Network} from "src/engine/Network"
export type CursorOpts = {
relay: string

View File

@ -104,7 +104,7 @@ export class Feed {
}
preprocessEvents = (events: Event[]) => {
const {User} = this.opts.engine.components
const {User} = this.opts.engine
events = reject((e: Event) => this.seen.has(e.id) || User.isMuted(e), events)
@ -116,7 +116,7 @@ export class Feed {
}
mergeHints(groups: string[][]) {
const {Nip65, User} = this.opts.engine.components
const {Nip65, User} = this.opts.engine
return Nip65.mergeHints(User.getSetting("relay_limit"), groups)
}
@ -182,13 +182,13 @@ export class Feed {
// Context loaders
loadPubkeys = (events: Event[]) => {
this.opts.engine.components.PubkeyLoader.load(
this.opts.engine.PubkeyLoader.load(
events.filter(this.isTextNote).flatMap((e: Event) => Tags.from(e).pubkeys().concat(e.pubkey))
)
}
loadParents = (events: Event[]) => {
const {Network, Nip65} = this.opts.engine.components
const {Network, Nip65} = this.opts.engine
const parentsInfo = events
.map((e: Event) => ({id: findReplyId(e), hints: Nip65.getParentHints(10, e)}))
.filter(({id}: any) => id && !this.seen.has(id))
@ -206,7 +206,7 @@ export class Feed {
}
loadContext = batch(300, (eventGroups: any) => {
const {Network, Nip65} = this.opts.engine.components
const {Network, Nip65} = this.opts.engine
const groupsByDepth = groupBy(prop("depth"), eventGroups)
for (const [depthStr, groups] of Object.entries(groupsByDepth)) {
@ -231,7 +231,7 @@ export class Feed {
})
listenForContext = throttle(5000, () => {
const {Network, Nip65} = this.opts.engine.components
const {Network, Nip65} = this.opts.engine
if (this.stopped) {
return
@ -292,7 +292,7 @@ export class Feed {
// No point in subscribing if we have an end date
if (!any(prop("until"), ensurePlural(filter) as any[])) {
this.addSubs("main", [
engine.components.Network.subscribe({
engine.Network.subscribe({
relays,
filter: ensurePlural(filter).map(assoc("since", since)),
onEvent: batch(1000, (context: Event[]) =>
@ -308,7 +308,7 @@ export class Feed {
new Cursor({
relay,
filter,
Network: engine.components.Network,
Network: engine.Network,
onEvent: batch(100, (context: Event[]) =>
this.addContext(context, {shouldLoadParents: true, depth})
),