Persist settings to nostr

This commit is contained in:
Jonathan Staab 2023-04-18 16:16:11 -05:00
parent 6c0eab5214
commit be5deb0532
7 changed files with 47 additions and 10 deletions

View File

@ -1,6 +1,6 @@
# Current
- [ ] Details on note in modal is broken
- [ ] Fix iOS https://css-tricks.com/the-trick-to-viewport-units-on-mobile/
- [ ] Remember message/chat status
- [ ] Image classification
- https://github.com/bhky/opennsfw2

View File

@ -31,6 +31,9 @@ const setRelays = newRelays =>
}),
})
const setSettings = content =>
new PublishableEvent(30078, {content, tags: [["d", "coracle/settings/v1"]]})
const setPetnames = petnames => new PublishableEvent(3, {tags: petnames})
const setMutes = mutes => new PublishableEvent(10000, {tags: mutes})
@ -199,6 +202,7 @@ export default {
authenticate,
updateUser,
setRelays,
setSettings,
setPetnames,
setMutes,
createList,

View File

@ -88,6 +88,18 @@ const getCrypt = () => {
throw new Error("No encryption method available.")
}
const encryptJson = data => getCrypt().encrypt(get(pubkey), JSON.stringify(data))
const decryptJson = async data => {
try {
return JSON.parse(await getCrypt().decrypt(get(pubkey), data))
} catch (e) {
console.warn(e)
return null
}
}
export default {
method,
pubkey,
@ -98,4 +110,6 @@ export default {
clear,
sign,
getCrypt,
encryptJson,
decryptJson,
}

View File

@ -15,6 +15,7 @@ import {
import {Tags, roomAttrs, isRelay, isShareableRelay, normalizeRelayUrl} from "src/util/nostr"
import {topics, people, userEvents, relays, rooms, routes} from "src/agent/db"
import {uniqByUrl} from "src/agent/relays"
import keys from "src/agent/keys"
import user from "src/agent/user"
const handlers = {}
@ -35,7 +36,7 @@ const processEvents = async events => {
}
for (const handler of handlers[event.kind] || []) {
handler(event)
await handler(event)
}
}
@ -148,7 +149,7 @@ addHandler(3, e => {
// User profile, except for events also handled for other users
const profileHandler = (key, getValue) => e => {
const profileHandler = (key, getValue) => async e => {
const profile = user.getProfile()
if (e.pubkey !== profile.pubkey) {
@ -161,12 +162,12 @@ const profileHandler = (key, getValue) => e => {
return
}
user.profile.update($p => {
const value = getValue(e, $p)
const value = await getValue(e, profile)
// If we didn't get a value, don't update the key
return value ? {...$p, [key]: value, [updated_at_key]: e.created_at} : $p
})
// If we didn't get a value, don't update the key
if (value) {
user.profile.set({...profile, [key]: value, [updated_at_key]: e.created_at})
}
}
addHandler(
@ -235,6 +236,15 @@ addHandler(
})
)
addHandler(
30078,
profileHandler("settings", async (e, p) => {
if (Tags.from(e).getMeta("d") === "coracle/settings/v1") {
return keys.decryptJson(e.content)
}
})
)
// Rooms
addHandler(40, e => {

View File

@ -82,6 +82,15 @@ export default {
getSettings: () => profileCopy.settings,
getSetting: k => profileCopy.settings[k],
dufflepud: path => `${profileCopy.settings.dufflepudUrl}${path}`,
async setSettings(settings) {
profile.update($p => ({...$p, settings}))
if (keys.canSign()) {
const content = await keys.encryptJson(settings)
return cmd.setSettings(content).publish(profileCopy.relays)
}
},
// Petnames

View File

@ -21,7 +21,7 @@
})
const submit = () => {
user.profile.update($p => ({...$p, settings: values}))
user.setSettings(values)
toast.show("info", "Your settings have been saved!")
}

View File

@ -6,7 +6,7 @@ import {tryJson} from "src/util/misc"
import {invoiceAmount} from "src/util/lightning"
export const personKinds = [0, 2, 3, 10001, 10002]
export const userKinds = personKinds.concat([10000, 30001])
export const userKinds = personKinds.concat([10000, 30001, 30078])
export class Tags {
tags: Array<any>