OverviewFilesCommitsIssuesDocsReleasesPeers

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

102 lines · javascript ref:
import { calcAvatarColorById as e } from "../lib/color"
import {
  CONTROL_WORD_5TH_INDEX as t,
  CONTROL_WORD_12TH_INDEX as r,
  SECRET_PHRASE_WORDS_COUNT as a,
  CHECK_WORDS_COUNT as o,
  IDENTITY_DEVICE_TYPE as s,
  IDENTITY_STATUS as n,
  IDENTITY_ALPHA_NUMERIC_REGEXP as i,
  AVATAR_BASE_PREFIX as d,
  AVATAR_BASE_EMPTY as c,
} from "./identity.constants"
export const phraseToWords = (e) =>
  "string" == typeof e
    ? e
        .split(/\s+/)
        .filter((e) => e)
        .map((e, t) => ({ index: t, value: e }))
    : []
export const wordsToPhrase = (e) => e.map(({ value: e }) => e).join(" ")
export const isControlWordIndex = (e) => e === t || e === r
export const hasCorrectWordsCount = (e) => phraseToWords(e).length === a
export const has5Or12WordDuplicates = (e) => {
  const a = e.split(" ")
  return a.reduce((e, o, s) => {
    if (e) return e
    const n = [t, r]
    return n.includes(s) ? e : n.map((e) => a[e]).includes(o)
  }, !1)
}
const l = (e) => e.sort(() => Math.random() - 0.5)
export const createCheckWords = (e) => {
  const t = e.filter(({ index: e }) => isControlWordIndex(e)),
    r = e.filter(({ index: e }) => !isControlWordIndex(e)),
    a = l(r),
    s = [...t, ...a.slice(0, o - t.length)]
  return l(s)
}
export const isAnonymousIdentityData = (e) => e?.anonymous
export const getAvatarUrl = (e) => {
  const t = e?.small?.pointer?.inlined
  return t && t !== c ? `${d}${t}` : ""
}
export const getAvatarUrlMedium = (e) => {
  const t = e?.medium?.pointer?.inlined
  return t ? `${d}${t}` : getAvatarUrl(e)
}
export const normalizeIdentity = (t) => {
  const {
      local: r,
      devices: a,
      confirmed: o,
      isLoggedOut: i,
      hasDiscoveryKey: d,
      settings: c,
      version: l,
    } = t || {},
    m = a
      .map(({ deviceId: e, name: t, confirmed: a, isMobile: o }) => ({
        deviceId: e,
        name: t,
        type: o ? s.MOBILE : s.DESKTOP,
        current: e === r?.deviceId,
        confirmed: a,
      }))
      .sort((e) => (e.current ? -1 : 1)),
    p = isAnonymousIdentityData(t) ? n.ANONYMOUS : n.COMPLETE,
    u = t.memberId ?? ""
  return {
    status: p,
    avatar: t.avatar ?? null,
    avatarUrl: getAvatarUrl(t.avatar),
    avatarUrlMedium: getAvatarUrlMedium(t.avatar),
    displayName: t.displayName ?? "",
    username: t.username || "",
    color: e(u),
    local: r,
    memberId: u,
    hasDiscoveryKey: d,
    devices: m,
    loggedOut: i,
    confirmed: o,
    profileId: t.profileId || "",
    settings: c,
    privateProfileVersion: l,
  }
}
export const normalizeRecoveryPhrase = (e) => e.toLowerCase().trim().split(/\s+/).join(" ")
export const parseUserSearchItem = ({ item: e }) => ({
  displayName: e?.displayName ?? "",
  username: e?.username ?? "",
  avatarSmall: e?.avatar ? getAvatarUrl(e.avatar) : "",
  avatarMedium: e?.avatar ? getAvatarUrlMedium(e.avatar) : "",
  memberId: e?.memberId ?? "",
  encodedContactInfo: e?.contactInfo ?? "",
})
export const parseUserSearchResponse = ({ response: e = [] }) =>
  e.map((e) => parseUserSearchItem({ item: e }))
export const isValidIdentityUsername = (e) => i.test(e)
export const getAvatarBase64 = (e) =>
  "string" != typeof e ? "" : e.startsWith(d) ? e.split(d)[1] : e