Create system/settings

This commit is contained in:
Jonathan Staab 2023-06-28 17:33:22 -07:00
parent 82aab16605
commit 01c2945f06
12 changed files with 78 additions and 50 deletions

View File

@ -152,7 +152,9 @@ export const uniqByUrl = pipe(uniqBy(prop("url")), filter(prop("url")))
export const sortByScore = sortBy(r => -r.score)
export const sampleRelays = (relays, scale = 1) => {
let limit = user.getSetting("relayLimit")
// TODO
// let limit = settings.getSetting("relayLimit")
let limit = 10
// Allow the caller to scale down how many relays we're bothering depending on
// the use case, but only if we have enough relays to handle it

View File

@ -242,15 +242,6 @@ addHandler(
})
)
addHandler(
30078,
profileHandler("settings", async (e, p) => {
if (Tags.from(e).getMeta("d") === "coracle/settings/v1") {
return {...p.settings, ...(await keys.decryptJson(e.content))}
}
})
)
addHandler(
30078,
profileHandler("last_checked", async (e, p) => {

View File

@ -13,14 +13,6 @@ const profile = synced("agent/user/profile", {
kind0: null,
lnurl: null,
zapper: null,
settings: {
relayLimit: 20,
defaultZap: 21,
showMedia: true,
reportAnalytics: true,
dufflepudUrl: import.meta.env.VITE_DUFFLEPUD_URL,
multiplextrUrl: import.meta.env.VITE_MULTIPLEXTR_URL,
},
rooms_joined: [],
last_checked: {},
relays: pool.defaultRelays,
@ -28,7 +20,6 @@ const profile = synced("agent/user/profile", {
lists: [],
})
const settings = derived(profile, prop("settings"))
const roomsJoined = derived(profile, prop("rooms_joined")) as Readable<string>
const lastChecked = derived(profile, prop("last_checked")) as Readable<Record<string, number>>
const relays = derived(profile, p =>
@ -43,7 +34,6 @@ let profileCopy = null
profile.subscribe($profile => {
profileCopy = $profile
pool.Config.multiplextrUrl = $profile.settings.multiplextrUrl
})
// Watch pubkey and add to profile
@ -61,18 +51,6 @@ export default {
getProfile: () => profileCopy,
getPubkey: () => profileCopy?.pubkey,
// Settings
settings,
getSettings: () => profileCopy.settings,
getSetting: k => profileCopy.settings[k],
dufflepud: path => `${profileCopy.settings.dufflepudUrl}${path}`,
async setSettings(settings) {
profile.update($p => ({...$p, settings}))
return this.setAppData("settings/v1", settings)
},
// App data
lastChecked,

View File

@ -125,7 +125,7 @@
}
const interval = setInterval(async () => {
const {dufflepudUrl} = user.getSettings()
const {dufflepudUrl} = system.settings.getSettings()
if (!dufflepudUrl) {
return

View File

@ -5,14 +5,14 @@
import NoteContentKind9802 from "src/app/shared/NoteContentKind9802.svelte"
import NoteContentKind1063 from "src/app/shared/NoteContentKind1063.svelte"
import NoteContentKind30023 from "src/app/shared/NoteContentKind30023.svelte"
import user from "src/agent/user"
import {settings} from "src/system"
export let note
export let anchorId = null
export let maxLength = 700
export let showEntire = false
export let expandable = true
export let showMedia = user.getSetting("showMedia")
export let showMedia = settings.getSetting("showMedia")
</script>
{#if note.kind === 40}

View File

@ -14,7 +14,7 @@ import {Tags, isNotification, userKinds, noteKinds} from "src/util/nostr"
import {findReplyId} from "src/util/nostr"
import {modal, toast} from "src/partials/state"
import {notifications, watch, userEvents, contacts, rooms} from "src/agent/db"
import {DEFAULT_FOLLOWS, ENABLE_ZAPS, keys, social} from "src/system"
import {DEFAULT_FOLLOWS, ENABLE_ZAPS, keys, social, settings} from "src/system"
import network from "src/agent/network"
import pool from "src/agent/pool"
import {getUserReadRelays, getUserRelays} from "src/agent/relays"
@ -45,7 +45,7 @@ setTimeout(() => {
return false
}
if (!user.getSetting("reportAnalytics")) {
if (!settings.getSetting("reportAnalytics")) {
return false
}
@ -69,7 +69,7 @@ export const logUsage = async name => {
// anything about them
const pubkey = user.getPubkey()
const ident = pubkey ? hash(pubkey) : "unknown"
const {dufflepudUrl, reportAnalytics} = user.getSettings()
const {dufflepudUrl, reportAnalytics} = settings.getSettings()
if (reportAnalytics) {
try {

View File

@ -13,15 +13,14 @@
import {getEventPublishRelays} from "src/agent/relays"
import {getPersonWithFallback} from "src/agent/db"
import network from "src/agent/network"
import user from "src/agent/user"
import {keys} from "src/system"
import {keys, settings} from "src/system"
import cmd from "src/agent/cmd"
export let note
let sub
let zap = {
amount: user.getSetting("defaultZap"),
amount: settings.getSetting("defaultZap"),
message: "",
invoice: null,
loading: false,

View File

@ -6,13 +6,13 @@
import Anchor from "src/partials/Anchor.svelte"
import Content from "src/partials/Content.svelte"
import Heading from "src/partials/Heading.svelte"
import user from "src/agent/user"
import {settings} from "src/system"
import pool from "src/agent/pool"
let values = {...user.getSettings()}
let values = {...settings.getSettings()}
const submit = () => {
user.setSettings(values)
settings.setSettings(values)
toast.show("info", "Your settings have been saved!")
}

View File

@ -6,7 +6,7 @@
import Anchor from "src/partials/Anchor.svelte"
import {listenForFile, stripExifData, blobToFile} from "src/util/html"
import {uploadFile, postJson} from "src/util/misc"
import user from "src/agent/user"
import {settings} from "src/system"
export let value
export let icon
@ -25,7 +25,7 @@
const opts = filter(identity, {maxWidth, maxHeight})
file = blobToFile(await stripExifData(inputFile, opts))
quote = await postJson(user.dufflepud("/upload/quote"), {
quote = await postJson(settings.dufflepud("/upload/quote"), {
uploads: [{size: file.size}],
})
} else {
@ -41,7 +41,7 @@
try {
const {id} = quote.uploads[0]
const {url} = await uploadFile(user.dufflepud(`/upload/${id}`), file)
const {url} = await uploadFile(settings.dufflepud(`/upload/${id}`), file)
value = url
} finally {

View File

@ -3,14 +3,14 @@
import {ellipsize} from "hurdak/lib/hurdak"
import Anchor from "src/partials/Anchor.svelte"
import Spinner from "src/partials/Spinner.svelte"
import user from "src/agent/user"
import {settings} from "src/system"
export let link
export let onClick = null
export let onClose = null
const loadPreview = async () => {
const res = await fetch(user.dufflepud("/link/preview"), {
const res = await fetch(settings.dufflepud("/link/preview"), {
method: "POST",
body: JSON.stringify({url: link.url}),
headers: {

View File

@ -2,8 +2,10 @@ export * from "src/system/env"
import keys from "src/system/keys"
import initSync from "src/system/sync"
import initSocial from "src/system/social"
import initSettings from "src/system/settings"
import {getUserWriteRelays} from "src/agent/relays"
import {default as agentSync} from "src/agent/sync"
import pool from "src/agent/pool"
import cmd from "src/agent/cmd"
// ===========================================================
@ -11,12 +13,17 @@ import cmd from "src/agent/cmd"
const sync = initSync({keys})
const social = initSocial({keys, sync, cmd, getUserWriteRelays})
const settings = initSettings({keys, sync, cmd, getUserWriteRelays})
// Glue stuff together
agentSync.addHandler("ALL_KINDS", sync.processEvents)
settings.store.subscribe($settings => {
pool.Config.multiplextrUrl = $settings.multiplextrUrl
})
// ===========================================================
// Exports
export {keys, sync, social}
export {keys, sync, social, settings}

51
src/system/settings.ts Normal file
View File

@ -0,0 +1,51 @@
import {get} from "svelte/store"
import {synced, getter} from "src/util/misc"
import {Tags} from "src/util/nostr"
import {DUFFLEPUD_URL, MULTIPLEXTR_URL} from "src/system/env"
export default ({keys, sync, cmd, getUserWriteRelays}) => {
const store = synced("settings/store", {
lastUpdated: 0,
relayLimit: 20,
defaultZap: 21,
showMedia: true,
reportAnalytics: true,
dufflepudUrl: DUFFLEPUD_URL,
multiplextrUrl: MULTIPLEXTR_URL,
})
sync.addHandler(30078, async e => {
if (
Tags.from(e).getMeta("d") === "coracle/settings/v1" &&
e.created_at > getSetting("lastUpdated")
) {
store.set({
...(await keys.decryptJson(e.content)),
lastUpdated: e.created_at,
})
}
})
const getSettings = getter(store)
const getSetting = k => getSettings()[k]
const dufflepud = path => `${getSetting("dufflepudUrl")}${path}`
const setSettings = async settings => {
store.update($settings => ({...$settings, ...settings}))
if (get(keys.canSign)) {
const d = "coracle/settings/v1"
const v = await keys.encryptJson(settings)
return cmd.setAppData(d, v).publish(getUserWriteRelays())
}
}
return {
store,
getSettings,
getSetting,
dufflepud,
setSettings,
}
}