Use new welshman membership helpers
This commit is contained in:
parent
3dfb5e60de
commit
847d89845e
3 changed files with 9 additions and 61 deletions
|
|
@ -10,7 +10,7 @@
|
||||||
import type {Maybe} from "@welshman/lib"
|
import type {Maybe} from "@welshman/lib"
|
||||||
import type {TrustedEvent, EventContent} from "@welshman/util"
|
import type {TrustedEvent, EventContent} from "@welshman/util"
|
||||||
import {makeEvent, MESSAGE, RELAY_ADD_MEMBER, ROOM_ADD_MEMBER} from "@welshman/util"
|
import {makeEvent, MESSAGE, RELAY_ADD_MEMBER, ROOM_ADD_MEMBER} from "@welshman/util"
|
||||||
import {publish} from "@welshman/app"
|
import {MembershipStatus, publish} from "@welshman/app"
|
||||||
import AltArrowDown from "@assets/icons/alt-arrow-down.svg?dataurl"
|
import AltArrowDown from "@assets/icons/alt-arrow-down.svg?dataurl"
|
||||||
import ClockCircle from "@assets/icons/clock-circle.svg?dataurl"
|
import ClockCircle from "@assets/icons/clock-circle.svg?dataurl"
|
||||||
import Login2 from "@assets/icons/login-3.svg?dataurl"
|
import Login2 from "@assets/icons/login-3.svg?dataurl"
|
||||||
|
|
@ -37,7 +37,6 @@
|
||||||
PROTECTED,
|
PROTECTED,
|
||||||
RoomType,
|
RoomType,
|
||||||
deriveUserRoomMembershipStatus,
|
deriveUserRoomMembershipStatus,
|
||||||
MembershipStatus,
|
|
||||||
getRoomType,
|
getRoomType,
|
||||||
prependParent,
|
prependParent,
|
||||||
} from "@app/rooms"
|
} from "@app/rooms"
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@
|
||||||
import {onMount} from "svelte"
|
import {onMount} from "svelte"
|
||||||
import {goto} from "$app/navigation"
|
import {goto} from "$app/navigation"
|
||||||
import type {Command} from "@welshman/app"
|
import type {Command} from "@welshman/app"
|
||||||
import {publish} from "@welshman/app"
|
import {MembershipStatus, publish} from "@welshman/app"
|
||||||
import Pen from "@assets/icons/pen.svg?dataurl"
|
import Pen from "@assets/icons/pen.svg?dataurl"
|
||||||
import TrashBin2 from "@assets/icons/trash-bin-2.svg?dataurl"
|
import TrashBin2 from "@assets/icons/trash-bin-2.svg?dataurl"
|
||||||
import Login3 from "@assets/icons/login-3.svg?dataurl"
|
import Login3 from "@assets/icons/login-3.svg?dataurl"
|
||||||
|
|
@ -13,7 +13,7 @@
|
||||||
import Confirm from "@lib/components/Confirm.svelte"
|
import Confirm from "@lib/components/Confirm.svelte"
|
||||||
import RoomEdit from "@app/components/RoomEdit.svelte"
|
import RoomEdit from "@app/components/RoomEdit.svelte"
|
||||||
import {app, roomLists, rooms} from "@app/core"
|
import {app, roomLists, rooms} from "@app/core"
|
||||||
import {deriveUserIsRoomAdmin, deriveUserRoomMembershipStatus, MembershipStatus} from "@app/rooms"
|
import {deriveUserIsRoomAdmin, deriveUserRoomMembershipStatus} from "@app/rooms"
|
||||||
import {makeSpacePath} from "@app/routes"
|
import {makeSpacePath} from "@app/routes"
|
||||||
import {pushModal} from "@app/modal"
|
import {pushModal} from "@app/modal"
|
||||||
import {pushToast} from "@app/toast"
|
import {pushToast} from "@app/toast"
|
||||||
|
|
|
||||||
|
|
@ -2,20 +2,9 @@ import * as nip19 from "nostr-tools/nip19"
|
||||||
import {derived} from "svelte/store"
|
import {derived} from "svelte/store"
|
||||||
import {sortBy, uniq} from "@welshman/lib"
|
import {sortBy, uniq} from "@welshman/lib"
|
||||||
import type {Maybe} from "@welshman/lib"
|
import type {Maybe} from "@welshman/lib"
|
||||||
import {
|
import {MESSAGE, makeEvent, outbox, seen, toNostrURI} from "@welshman/util"
|
||||||
MESSAGE,
|
|
||||||
ROOM_ADD_MEMBER,
|
|
||||||
ROOM_JOIN,
|
|
||||||
ROOM_LEAVE,
|
|
||||||
ROOM_REMOVE_MEMBER,
|
|
||||||
makeEvent,
|
|
||||||
outbox,
|
|
||||||
seen,
|
|
||||||
sortEventsAsc,
|
|
||||||
toNostrURI,
|
|
||||||
} from "@welshman/util"
|
|
||||||
import type {EventContent, TrustedEvent} from "@welshman/util"
|
import type {EventContent, TrustedEvent} from "@welshman/util"
|
||||||
import {RoomLists, makeRoomKey, createSearch} from "@welshman/app"
|
import {MembershipStatus, RoomLists, makeRoomKey, createSearch} from "@welshman/app"
|
||||||
import type {Room, RoomMeta} from "@welshman/app"
|
import type {Room, RoomMeta} from "@welshman/app"
|
||||||
import {
|
import {
|
||||||
deriveUserItem,
|
deriveUserItem,
|
||||||
|
|
@ -29,15 +18,8 @@ import {
|
||||||
user,
|
user,
|
||||||
} from "@app/core"
|
} from "@app/core"
|
||||||
import {deriveUserIsSpaceAdmin} from "@app/management"
|
import {deriveUserIsSpaceAdmin} from "@app/management"
|
||||||
import {deriveEventsForUrl} from "@app/repository"
|
|
||||||
import {makeRoomPath} from "@app/routes"
|
import {makeRoomPath} from "@app/routes"
|
||||||
|
|
||||||
export enum MembershipStatus {
|
|
||||||
Initial,
|
|
||||||
Pending,
|
|
||||||
Granted,
|
|
||||||
}
|
|
||||||
|
|
||||||
export const ROOM = "h"
|
export const ROOM = "h"
|
||||||
|
|
||||||
export const PROTECTED = ["-"]
|
export const PROTECTED = ["-"]
|
||||||
|
|
@ -58,7 +40,7 @@ export const displayRoom = (url: string, h: string) =>
|
||||||
export const roomComparator = (url: string) => (h: string) => displayRoom(url, h).toLowerCase()
|
export const roomComparator = (url: string) => (h: string) => displayRoom(url, h).toLowerCase()
|
||||||
|
|
||||||
export const deriveRoomMembers = (url: string, h: string) =>
|
export const deriveRoomMembers = (url: string, h: string) =>
|
||||||
derived(rooms.get().forRoom(url, h), $room => $room?.members?.members() ?? [])
|
derived(rooms.get().members(url, h).$, $members => Array.from($members))
|
||||||
|
|
||||||
// A room member also has to be allowed at the relay level, or they won't be able to read the
|
// A room member also has to be allowed at the relay level, or they won't be able to read the
|
||||||
// room at all.
|
// room at all.
|
||||||
|
|
@ -181,44 +163,11 @@ export const deriveUserIsRoomAdmin = (url: string, h: string) =>
|
||||||
$isSpaceAdmin || Boolean($room?.admins?.pubkeys().includes($user.pubkey)),
|
$isSpaceAdmin || Boolean($room?.admins?.pubkeys().includes($user.pubkey)),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Room membership is the relay's business, but a space admin outranks it.
|
||||||
export const deriveUserRoomMembershipStatus = (url: string, h: string) =>
|
export const deriveUserRoomMembershipStatus = (url: string, h: string) =>
|
||||||
derived(
|
derived(
|
||||||
[
|
[rooms.get().membershipStatus(url, h).$, deriveUserIsRoomAdmin(url, h)],
|
||||||
user,
|
([$status, $isAdmin]) => ($isAdmin ? MembershipStatus.Granted : $status),
|
||||||
rooms.get().forRoom(url, h),
|
|
||||||
deriveEventsForUrl(url, [{kinds: [ROOM_ADD_MEMBER, ROOM_REMOVE_MEMBER], "#h": [h]}]),
|
|
||||||
deriveEventsForUrl(url, [{kinds: [ROOM_JOIN, ROOM_LEAVE], "#h": [h]}]),
|
|
||||||
deriveUserIsRoomAdmin(url, h),
|
|
||||||
],
|
|
||||||
([$user, $room, $addRemoveEvents, $joinLeaveEvents, $isAdmin]) => {
|
|
||||||
if ($isAdmin) {
|
|
||||||
return MembershipStatus.Granted
|
|
||||||
}
|
|
||||||
|
|
||||||
let isMember = false
|
|
||||||
|
|
||||||
if ($room?.members) {
|
|
||||||
isMember = $room.members.isMember($user.pubkey)
|
|
||||||
} else {
|
|
||||||
for (const event of sortEventsAsc($addRemoveEvents)) {
|
|
||||||
if (event.pubkey === $user.pubkey) {
|
|
||||||
isMember = event.kind === ROOM_ADD_MEMBER
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const event of $joinLeaveEvents) {
|
|
||||||
if (event.pubkey === $user.pubkey) {
|
|
||||||
if (event.kind === ROOM_JOIN) {
|
|
||||||
return isMember ? MembershipStatus.Granted : MembershipStatus.Pending
|
|
||||||
}
|
|
||||||
|
|
||||||
return MembershipStatus.Initial
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return isMember ? MembershipStatus.Granted : MembershipStatus.Initial
|
|
||||||
},
|
|
||||||
)
|
)
|
||||||
|
|
||||||
export const deriveUserRoomSearch = () =>
|
export const deriveUserRoomSearch = () =>
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue