forked from coracle/flotilla
Refine sync
This commit is contained in:
+39
-71
@@ -137,10 +137,9 @@ const syncUserSpaceMembership = (url: string) => {
|
|||||||
relays: [url],
|
relays: [url],
|
||||||
signal: controller.signal,
|
signal: controller.signal,
|
||||||
filters: [
|
filters: [
|
||||||
{
|
{kinds: [RELAY_ADD_MEMBER], "#p": [$pubkey], limit: 1},
|
||||||
kinds: [RELAY_ADD_MEMBER, RELAY_REMOVE_MEMBER, ROOM_CREATE_PERMISSION],
|
{kinds: [RELAY_REMOVE_MEMBER], "#p": [$pubkey], limit: 1},
|
||||||
"#p": [$pubkey],
|
{kinds: [ROOM_CREATE_PERMISSION], "#p": [$pubkey], limit: 1},
|
||||||
},
|
|
||||||
],
|
],
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -157,11 +156,8 @@ const syncUserRoomMembership = (url: string, h: string) => {
|
|||||||
relays: [url],
|
relays: [url],
|
||||||
signal: controller.signal,
|
signal: controller.signal,
|
||||||
filters: [
|
filters: [
|
||||||
{
|
{kinds: [ROOM_ADD_MEMBER], "#p": [$pubkey], "#h": [h], limit: 1},
|
||||||
kinds: [ROOM_ADD_MEMBER, ROOM_REMOVE_MEMBER],
|
{kinds: [ROOM_REMOVE_MEMBER], "#p": [$pubkey], "#h": [h], limit: 1},
|
||||||
"#p": [$pubkey],
|
|
||||||
"#h": [h],
|
|
||||||
},
|
|
||||||
],
|
],
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -172,20 +168,18 @@ const syncUserRoomMembership = (url: string, h: string) => {
|
|||||||
const syncUserData = () => {
|
const syncUserData = () => {
|
||||||
const unsubscribersByKey = new Map<string, Unsubscriber>()
|
const unsubscribersByKey = new Map<string, Unsubscriber>()
|
||||||
|
|
||||||
const unsubscribeGroupList = userGroupList.subscribe($l => {
|
const unsubscribeGroupList = userGroupList.subscribe($userGroupList => {
|
||||||
const $pubkey = pubkey.get()
|
if ($userGroupList) {
|
||||||
|
|
||||||
if ($pubkey) {
|
|
||||||
const keys = new Set<string>()
|
const keys = new Set<string>()
|
||||||
|
|
||||||
for (const url of getSpaceUrlsFromGroupList($l)) {
|
for (const url of getSpaceUrlsFromGroupList($userGroupList)) {
|
||||||
if (!unsubscribersByKey.has(url)) {
|
if (!unsubscribersByKey.has(url)) {
|
||||||
unsubscribersByKey.set(url, syncUserSpaceMembership(url))
|
unsubscribersByKey.set(url, syncUserSpaceMembership(url))
|
||||||
}
|
}
|
||||||
|
|
||||||
keys.add(url)
|
keys.add(url)
|
||||||
|
|
||||||
for (const h of getSpaceRoomsFromGroupList(url, $l)) {
|
for (const h of getSpaceRoomsFromGroupList(url, $userGroupList)) {
|
||||||
const key = `${url}'${h}`
|
const key = `${url}'${h}`
|
||||||
|
|
||||||
if (!unsubscribersByKey.has(key)) {
|
if (!unsubscribersByKey.has(key)) {
|
||||||
@@ -205,22 +199,20 @@ const syncUserData = () => {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
const unsubscribeList = userRelayList.subscribe($l => {
|
const unsubscribeRelayList = userRelayList.subscribe($userRelayList => {
|
||||||
const $pubkey = pubkey.get()
|
if ($userRelayList) {
|
||||||
|
loadAlerts($userRelayList.event.pubkey)
|
||||||
if ($pubkey) {
|
loadAlertStatuses($userRelayList.event.pubkey)
|
||||||
loadAlerts($pubkey)
|
loadBlossomServerList($userRelayList.event.pubkey)
|
||||||
loadAlertStatuses($pubkey)
|
loadFollowList($userRelayList.event.pubkey)
|
||||||
loadBlossomServerList($pubkey)
|
loadGroupList($userRelayList.event.pubkey)
|
||||||
loadFollowList($pubkey)
|
loadMuteList($userRelayList.event.pubkey)
|
||||||
loadGroupList($pubkey)
|
loadProfile($userRelayList.event.pubkey)
|
||||||
loadMuteList($pubkey)
|
loadSettings($userRelayList.event.pubkey)
|
||||||
loadProfile($pubkey)
|
|
||||||
loadSettings($pubkey)
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
const unsubscribeFollows = userFollowList.subscribe(async $l => {
|
const unsubscribeFollows = userFollowList.subscribe(async $userFollowList => {
|
||||||
for (const pubkeys of chunk(10, get(bootstrapPubkeys))) {
|
for (const pubkeys of chunk(10, get(bootstrapPubkeys))) {
|
||||||
// This isn't urgent, avoid clogging other stuff up
|
// This isn't urgent, avoid clogging other stuff up
|
||||||
await sleep(1000)
|
await sleep(1000)
|
||||||
@@ -237,18 +229,11 @@ const syncUserData = () => {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
const unsubscribePubkey = pubkey.subscribe($pubkey => {
|
|
||||||
if ($pubkey) {
|
|
||||||
loadRelayList($pubkey)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
unsubscribersByKey.forEach(call)
|
unsubscribersByKey.forEach(call)
|
||||||
unsubscribeGroupList()
|
unsubscribeGroupList()
|
||||||
unsubscribeList()
|
unsubscribeRelayList()
|
||||||
unsubscribeFollows()
|
unsubscribeFollows()
|
||||||
unsubscribePubkey()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -262,8 +247,8 @@ const syncSpace = (url: string) => {
|
|||||||
signal: controller.signal,
|
signal: controller.signal,
|
||||||
filters: [
|
filters: [
|
||||||
{kinds: [RELAY_MEMBERS]},
|
{kinds: [RELAY_MEMBERS]},
|
||||||
{kinds: [ROOM_META, ROOM_DELETE]},
|
|
||||||
{kinds: [ROOM_ADMINS, ROOM_MEMBERS]},
|
{kinds: [ROOM_ADMINS, ROOM_MEMBERS]},
|
||||||
|
{kinds: [ROOM_META, ROOM_DELETE], limit: 1000},
|
||||||
{kinds: [ROOM_ADD_MEMBER, ROOM_REMOVE_MEMBER]},
|
{kinds: [ROOM_ADD_MEMBER, ROOM_REMOVE_MEMBER]},
|
||||||
{kinds: [RELAY_ADD_MEMBER, RELAY_REMOVE_MEMBER]},
|
{kinds: [RELAY_ADD_MEMBER, RELAY_REMOVE_MEMBER]},
|
||||||
...MESSAGE_KINDS.map(kind => ({kinds: [kind]})),
|
...MESSAGE_KINDS.map(kind => ({kinds: [kind]})),
|
||||||
@@ -276,54 +261,37 @@ const syncSpace = (url: string) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const syncSpaces = () => {
|
const syncSpaces = () => {
|
||||||
const membershipUnsubscribersByUrl = new Map<string, Unsubscriber>()
|
const store = derived([userSpaceUrls, page], identity)
|
||||||
|
const unsubscribersByUrl = new Map<string, Unsubscriber>()
|
||||||
|
const unsubscribe = store.subscribe(([$userSpaceUrls, $page]) => {
|
||||||
|
const urls = Array.from($userSpaceUrls)
|
||||||
|
|
||||||
|
if ($page.params.relay) {
|
||||||
|
urls.push(decodeRelay($page.params.relay))
|
||||||
|
}
|
||||||
|
|
||||||
const unsubscribeSpaceUrls = userSpaceUrls.subscribe(urls => {
|
|
||||||
// stop syncing removed spaces
|
// stop syncing removed spaces
|
||||||
for (const [url, unsubscribe] of membershipUnsubscribersByUrl.entries()) {
|
for (const [url, unsubscribe] of unsubscribersByUrl.entries()) {
|
||||||
if (!urls.includes(url)) {
|
if (!urls.includes(url)) {
|
||||||
membershipUnsubscribersByUrl.delete(url)
|
unsubscribersByUrl.delete(url)
|
||||||
unsubscribe()
|
unsubscribe()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start syncing newly added spaces
|
// Start syncing newly added spaces
|
||||||
for (const url of urls) {
|
for (const url of urls) {
|
||||||
if (!membershipUnsubscribersByUrl.has(url)) {
|
if (!unsubscribersByUrl.has(url)) {
|
||||||
membershipUnsubscribersByUrl.set(url, syncSpace(url))
|
unsubscribersByUrl.set(url, syncSpace(url))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
const pageUnsubscribersByUrl = new Map<string, Unsubscriber>()
|
|
||||||
|
|
||||||
// Sync the space the user is currently visiting
|
|
||||||
const unsubscribePage = page.subscribe($page => {
|
|
||||||
if ($page.params.relay) {
|
|
||||||
const url = decodeRelay($page.params.relay)
|
|
||||||
|
|
||||||
// Don't subscribe twice if the user is a member
|
|
||||||
if (!pageUnsubscribersByUrl.has(url) && !get(userSpaceUrls).includes(url)) {
|
|
||||||
pageUnsubscribersByUrl.set(url, syncSpace(url))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Clean up old subscriptions
|
|
||||||
for (const [oldUrl, unsubscribe] of pageUnsubscribersByUrl.entries()) {
|
|
||||||
if (url !== oldUrl) {
|
|
||||||
pageUnsubscribersByUrl.delete(oldUrl)
|
|
||||||
unsubscribe()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
Array.from(pageUnsubscribersByUrl.values()).forEach(call)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
Array.from(membershipUnsubscribersByUrl.values()).forEach(call)
|
for (const unsubscriber of unsubscribersByUrl.values()) {
|
||||||
Array.from(pageUnsubscribersByUrl.values()).forEach(call)
|
unsubscriber()
|
||||||
unsubscribeSpaceUrls()
|
}
|
||||||
unsubscribePage()
|
|
||||||
|
unsubscribe()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user