OverviewFilesCommitsIssuesDocsReleasesPeers

keet-deobfuscated / @holepunchto / keet-store / store / member / member.model.js

158 lines · javascript ref:
import { calcAvatarColorById as e } from "../lib/color"
import { getAvatarUrl as t, getAvatarUrlMedium as a } from "../identity"
export const CAN_SHARE_FILES = 16
export const CAN_REACT = 8
export const CAN_MODERATE = 4
export const CAN_INDEX = 2
export const CAN_WRITE = 1
export const MEMBER_ROLE = { ADMIN: 1, MODERATOR: 2, PEER: 3, READ_ONLY: 4 }
export const CHANGE_ROLE_OPERATION = { ASSIGN: "assign", REVOKE: "revoke" }
export const MEMBER_ROLE_MANAGEMENT_RULES = {
  [MEMBER_ROLE.ADMIN]: {
    [CHANGE_ROLE_OPERATION.ASSIGN]: [MEMBER_ROLE.ADMIN, MEMBER_ROLE.MODERATOR],
    [CHANGE_ROLE_OPERATION.REVOKE]: [MEMBER_ROLE.ADMIN, MEMBER_ROLE.MODERATOR],
  },
  [MEMBER_ROLE.MODERATOR]: {
    [CHANGE_ROLE_OPERATION.ASSIGN]: [MEMBER_ROLE.MODERATOR],
    [CHANGE_ROLE_OPERATION.REVOKE]: [MEMBER_ROLE.MODERATOR],
  },
}
const E = 16
export const MemberCapabilities = {
  CAN_MODERATE: 4,
  CAN_INDEX: 2,
  CAN_SHARE_FILES: 16,
  CAN_WRITE: 1,
  CAN_REACT: 8,
  ALL_WRITE_CAPABILITIES: 25,
  ALL_CAPABILITIES: 31,
}
export function canModerate(e) {
  return !!(4 & e)
}
export function canWrite(e) {
  return !!(1 & e)
}
export function canIndex(e) {
  return !!(2 & e)
}
export function hasCapability(e, t) {
  return 0 !== (e & t)
}
export const canRemoveMember = ({ myRole: e, managedMemberRole: t }) =>
  t !== MEMBER_ROLE.ADMIN && (e === MEMBER_ROLE.ADMIN || e === MEMBER_ROLE.MODERATOR)
export const canMuteMember = ({ myRole: e, managedMemberRole: t }) =>
  (e === MEMBER_ROLE.ADMIN || e === MEMBER_ROLE.MODERATOR) &&
  t !== MEMBER_ROLE.ADMIN &&
  t !== MEMBER_ROLE.MODERATOR
export const createByRoomIdInitialData = () => ({
  bySwarmId: {},
  byMemberId: {},
  unknownSwarmIds: [],
  notFoundMemberIds: [],
  removedMemberIds: [],
  disabledCapabilities: !1,
})
export const getMemberRole = (e) =>
  e?.capabilities
    ? canIndex(e.capabilities)
      ? MEMBER_ROLE.ADMIN
      : canModerate(e.capabilities)
        ? MEMBER_ROLE.MODERATOR
        : canWrite(e.capabilities)
          ? MEMBER_ROLE.PEER
          : MEMBER_ROLE.READ_ONLY
    : null
const o = [MEMBER_ROLE.PEER, MEMBER_ROLE.MODERATOR, MEMBER_ROLE.ADMIN]
export const getCanChangeRole = ({
  myRole: e,
  managedMemberRole: t,
  managedMemberTargetRole: a,
  operation: E,
}) => {
  if (!MEMBER_ROLE_MANAGEMENT_RULES[e]) return !1
  const o = MEMBER_ROLE_MANAGEMENT_RULES[e][E]
  switch (E) {
    case CHANGE_ROLE_OPERATION.ASSIGN:
      return !(t <= a) && -1 !== o.indexOf(a)
    case CHANGE_ROLE_OPERATION.REVOKE:
      return -1 !== o.indexOf(t)
    default:
      return !1
  }
}
export const getRoleManageOperation = ({ currentRole: e, targetRole: t }) =>
  e < t ? CHANGE_ROLE_OPERATION.REVOKE : CHANGE_ROLE_OPERATION.ASSIGN
export const getRolesSelectOptions = ({ myRole: e, managedMemberRole: t }) => {
  const a = []
  for (const E of o) {
    const o = getRoleManageOperation({ currentRole: t, targetRole: E })
    ;(getCanChangeRole({
      myRole: e,
      managedMemberRole: t,
      managedMemberTargetRole: E,
      operation: o,
    }) ||
      E === t) &&
      a.push(E)
  }
  return a
}
export const parseMemberResponse = (E) => {
  const { status: o } = E,
    { isActive: n, isMuted: R, isPending: r } = o || {},
    M = E.memberId ?? ""
  return {
    memberId: M,
    name: E.name ?? "",
    displayName: E.displayName ?? "",
    capabilities: E.capabilities ?? null,
    local: !!E.local,
    avatar: E.avatar ?? "",
    avatarUrl: t(E.avatar),
    avatarUrlMedium: a(E.avatar),
    color: e(M),
    anonymous: !!E.anonymous,
    deviceIds: E.deviceIds ?? [],
    contactInfo: E.contactInfo ?? {},
    canDM: !E.local && !!E.canDM,
    isMuted: R || !1,
    isRemoved: !1 === n || !0 === r,
    reportedClock: E.labels?.flagsClock || 0,
    reportedCount: E.labels?.total || 0,
  }
}
export const disableMemberCapabilities = (e) => ({ ...e, capabilities: null })
export const isMemberDataChanged = (e, t) => {
  if (
    !e ||
    !t ||
    e.memberId !== t.memberId ||
    e.capabilities !== t.capabilities ||
    e.displayName !== t.displayName ||
    e.name !== t.name ||
    e.local !== t.local ||
    e.avatarUrl !== t.avatarUrl ||
    e.color !== t.color ||
    e.anonymous !== t.anonymous ||
    e.deviceIds.length !== t.deviceIds.length ||
    e.isMuted !== t.isMuted ||
    e.isRemoved !== t.isRemoved
  )
    return !0
  for (let a = 0; a < e.deviceIds.length; a += 1) if (e.deviceIds[a] !== t.deviceIds[a]) return !0
  if (e.contactInfo !== t.contactInfo) {
    if (!e.contactInfo || !t.contactInfo) return !0
    if (
      e.contactInfo.memberId !== t.contactInfo.memberId ||
      e.contactInfo.displayName !== t.contactInfo.displayName ||
      e.contactInfo.avatar !== t.contactInfo.avatar
    )
      return !0
  }
  return !1
}
export const isMemberChanged = (e, t) => !e || !t || isMemberDataChanged(e, t)
export const hasMemberCapabilityChanged = (e, t) =>
  "number" == typeof e?.capabilities && e?.capabilities !== t?.capabilities