Add setProfile and sync helper

This commit is contained in:
Jon Staab
2025-07-15 21:26:53 -07:00
parent fd84b80734
commit 2ed88c3345
3 changed files with 59 additions and 27 deletions
+19 -7
View File
@@ -53,11 +53,23 @@ const publish = async (content: string) => {
## Built in commands ## Built in commands
Several thunk factories are provided for more complicated scenarios like updating lists: Several thunk factories are provided for common or more complicated scenarios like updating lists:
- `follow(pubkey)` - `removeRelay(url: string, mode: RelayMode)`
- `unfollow(pubkey)` - `addRelay(url: string, mode: RelayMode)`
- `mute(pubkey)` - `removeInboxRelay(url: string)`
- `unmute(pubkey)` - `addInboxRelay(url: string)`
- `pin(tag)` - `setProfile(profile: Profile)`
- `unpin(tag)` - `unfollow(value: string)`
- `follow(tag: string[])`
- `unmute(value: string)`
- `mute(tag: string[])`
- `unpin(value: string)`
- `pin(tag: string[])`
- `sendWrapped({template, pubkeys, ...options}: SendWrappedOptions)`
- `manageRelay(url: string, request: ManagementRequest)`
- `createRoom(url: string, room: RoomMeta)`
- `deleteRoom(url: string, room: RoomMeta)`
- `editRoom(url: string, room: RoomMeta)`
- `joinRoom(url: string, room: RoomMeta)`
- `leaveRoom(url: string, room: RoomMeta)`
+14 -1
View File
@@ -15,6 +15,9 @@ import {
makeRoomEditEvent, makeRoomEditEvent,
makeRoomJoinEvent, makeRoomJoinEvent,
makeRoomLeaveEvent, makeRoomLeaveEvent,
isPublishedProfile,
createProfile,
editProfile,
RelayMode, RelayMode,
INBOX_RELAYS, INBOX_RELAYS,
FOLLOWS, FOLLOWS,
@@ -22,7 +25,7 @@ import {
MUTES, MUTES,
PINS, PINS,
} from "@welshman/util" } from "@welshman/util"
import type {RoomMeta} from "@welshman/util" import type {RoomMeta, Profile} from "@welshman/util"
import {Nip59, stamp} from "@welshman/signer" import {Nip59, stamp} from "@welshman/signer"
import {Router, addMaximalFallbacks} from "@welshman/router" import {Router, addMaximalFallbacks} from "@welshman/router"
import { import {
@@ -86,6 +89,16 @@ export const addInboxRelay = async (url: string) => {
return publishThunk({event, relays}) return publishThunk({event, relays})
} }
// NIP 01
export const setProfile = (profile: Profile) => {
const router = Router.get()
const relays = router.merge([router.Index(), router.FromUser()]).getUrls()
const event = isPublishedProfile(profile) ? editProfile(profile) : createProfile(profile)
return publishThunk({event, relays})
}
// NIP 02 // NIP 02
export const unfollow = async (value: string) => { export const unfollow = async (value: string) => {
+26 -19
View File
@@ -1,4 +1,4 @@
import {writable} from "svelte/store" import {writable, Writable} from "svelte/store"
import {getJson, setJson} from "@welshman/lib" import {getJson, setJson} from "@welshman/lib"
export interface StorageProvider { export interface StorageProvider {
@@ -6,32 +6,39 @@ export interface StorageProvider {
set: (key: string, value: any) => Promise<void> set: (key: string, value: any) => Promise<void>
} }
export const localStorageProvider: StorageProvider = {
get: async (key: string) => getJson(key),
set: async (key: string, value: any) => setJson(key, value),
}
export interface SyncConfig<T> {
key: string
store: Writable<T>
storage: StorageProvider
}
export const sync = <T>({key, store, storage}: SyncConfig<T>) => {
storage.get(key).then((value: T | undefined) => {
if (value !== undefined) {
store.set(value)
}
})
store.subscribe(async (value: T) => {
await storage.set(key, value)
})
}
export interface SyncedConfig<T> { export interface SyncedConfig<T> {
key: string key: string
storage: StorageProvider storage: StorageProvider
defaultValue: T defaultValue: T
} }
export const localStorageProvider: StorageProvider = { export const synced = <T>({key, storage, defaultValue}: SyncedConfig<T>) => {
get: async (key: string) => getJson(key),
set: async (key: string, value: any) => setJson(key, value),
}
export const synced = <T>(config: SyncedConfig<T>) => {
const {key, storage, defaultValue} = config
const store = writable<T>(defaultValue) const store = writable<T>(defaultValue)
// Async initialization sync({key, store, storage})
storage.get(key).then((value: any) => {
if (value !== undefined) {
store.set(value)
}
})
// Subscribe to changes
store.subscribe(async (value: T) => {
await storage.set(key, value)
})
return store return store
} }