Fix joining/leaving rooms

This commit is contained in:
Jonathan Staab 2023-02-09 12:05:26 -06:00
parent 3d546e9004
commit 40f5cdff0e
6 changed files with 25 additions and 19 deletions

View File

@ -71,9 +71,6 @@ If you like Coracle and want to support its development, you can donate sats via
# Current
- [ ] Switch to localforage
- Check that firefox private mode works (it won't work in dev)
- [ ] Add modal for follows/followers
- [ ] Implement gossip model https://bountsr.org/code/2023/02/03/gossip-model.html
- [ ] Make feeds page customizable. This could potentially use the "lists" NIP
- [ ] Show notification at top of feeds: "Showing notes from 3 relays". Click to customize.
@ -86,6 +83,10 @@ If you like Coracle and want to support its development, you can donate sats via
- [x] Converted threshold to percentage
- [x] Fixed slow leaving/joining chat rooms
- [x] Switch to localforage from dexie, fixing firefox/safari private windows
- [x] Tweaks to link parsing
- [x] Use write relays to publish events more intelligently
- [x] Add followers/follows lists
## 0.2.10

View File

@ -125,7 +125,7 @@ const processRoomEvents = async events => {
// if they don't have a name
if (content.name) {
updates[roomId] = {
joined: 0,
joined: false,
...room,
...updates[roomId],
...content,

View File

@ -1,7 +1,7 @@
import {debounce} from 'throttle-debounce'
import {is, prop, without} from 'ramda'
import {writable} from 'svelte/store'
import {switcherFn, ensurePlural, first} from 'hurdak/lib/hurdak'
import {switcherFn, createMap, ensurePlural, first} from 'hurdak/lib/hurdak'
import {defer, asyncIterableToArray} from 'src/util/misc'
// Types
@ -14,6 +14,8 @@ type Message = {
type Table = {
name: string
subscribe: (subscription: (value: any) => void) => (() => void)
put: (data: object) => void
patch: (data: object) => void
bulkPut: (data: object) => void
bulkPatch: (data: object) => void
all: (where?: object) => Promise<any>
@ -135,7 +137,7 @@ const iterate = (storeName, where = {}) => ({
const registry = {}
const defineTable = (name: string): Table => {
const defineTable = (name: string, pk: string): Table => {
let p = Promise.resolve()
let listeners = []
let data = {}
@ -191,6 +193,9 @@ const defineTable = (name: string): Table => {
bulkPut(newData)
}
const put = item => bulkPut(createMap(pk, [item]))
const patch = item => bulkPatch(createMap(pk, [item]))
const all = (where = {}) => asyncIterableToArray(iterate(name, where), prop('v'))
const one = (where = {}) => first(all(where))
const get = k => data[k]
@ -205,16 +210,16 @@ const defineTable = (name: string): Table => {
setAndNotify(initialData)
})()
registry[name] = {name, subscribe, bulkPut, bulkPatch, all, one, get}
registry[name] = {name, subscribe, bulkPut, bulkPatch, put, patch, all, one, get}
return registry[name]
}
const people = defineTable('people')
const rooms = defineTable('rooms')
const messages = defineTable('messages')
const alerts = defineTable('alerts')
const relays = defineTable('relays')
const people = defineTable('people', 'pubkey')
const rooms = defineTable('rooms', 'id')
const messages = defineTable('messages', 'id')
const alerts = defineTable('alerts', 'id')
const relays = defineTable('relays', 'url')
// Helper to allow us to listen to changes of any given table

View File

@ -1,7 +1,7 @@
import type {Relay} from 'nostr-tools'
import {relayInit} from 'nostr-tools'
import {uniqBy, reject, prop, find, whereEq, is} from 'ramda'
import {ensurePlural, createMap} from 'hurdak/lib/hurdak'
import {ensurePlural} from 'hurdak/lib/hurdak'
import {isRelay} from 'src/util/nostr'
import {sleep} from 'src/util/misc'
import {database} from 'src/agent'
@ -83,7 +83,7 @@ const findConnection = url => find(whereEq({url}), connections)
const connect = async url => {
const conn = findConnection(url) || new Connection(url)
await database.relays.bulkPatch(createMap('url', [{url}]))
await database.relays.patch({url})
await Promise.race([conn.connect(), sleep(5000)])
if (conn.status === 'ready') {

View File

@ -19,7 +19,7 @@
const {mostRecentByPubkey} = messages
const rooms = database.watch(['rooms', 'messages'], async () => {
const rooms = await database.rooms.all({joined: 1})
const rooms = await database.rooms.all({joined: true})
const messages = await database.messages.all()
const pubkeys = without([$user.pubkey], uniq(messages.flatMap(m => [m.pubkey, m.recipient])))
@ -31,7 +31,7 @@
})
const search = database.watch('rooms', async () => {
const rooms = await database.rooms.all({joined: 0})
const rooms = await database.rooms.all({'joined:!eq': true})
roomsCount = rooms.length
@ -49,11 +49,11 @@
}
const joinRoom = id => {
database.rooms.bulkPatch({id, joined: 1})
database.rooms.patch({id, joined: true})
}
const leaveRoom = id => {
database.rooms.bulkPatch({id, joined: 0})
database.rooms.patch({id, joined: false})
}
onMount(() => {

View File

@ -38,7 +38,7 @@
? await cmd.updateRoom(getWriteRelays(), room)
: await cmd.createRoom(getWriteRelays(), room)
await database.rooms.bulkPatch({id: room.id || event.id, joined: 1})
await database.rooms.patch({id: room.id || event.id, joined: true})
toast.show("info", `Your room has been ${room.id ? 'updated' : 'created'}!`)