mirror of
https://github.com/coracle-social/coracle.git
synced 2024-09-18 19:23:40 +00:00
Persist settings to nostr
This commit is contained in:
parent
6c0eab5214
commit
be5deb0532
@ -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
|
||||
|
@ -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,
|
||||
|
@ -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,
|
||||
}
|
||||
|
@ -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 => {
|
||||
|
@ -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
|
||||
|
||||
|
@ -21,7 +21,7 @@
|
||||
})
|
||||
|
||||
const submit = () => {
|
||||
user.profile.update($p => ({...$p, settings: values}))
|
||||
user.setSettings(values)
|
||||
|
||||
toast.show("info", "Your settings have been saved!")
|
||||
}
|
||||
|
@ -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>
|
||||
|
Loading…
Reference in New Issue
Block a user