const e = require("hypercore-id-encoding"),
t = require("b4a")
function n({ start: e, length: t, content: n, preview: r }) {
return (r?.file && (r.file = a(r.file)), { range: { start: e, length: t }, url: n, preview: r })
}
function r(t) {
if (!t) return null
const { id: n, key: r, blob: i } = t
return { id: n, key: n ? e.decode(n) : r, blob: i }
}
function i(t) {
if (!t) return null
const { id: n, key: r, path: i, version: l, size: a } = t
return { id: n, key: n ? e.decode(n) : r, path: i, version: l, size: a }
}
function l(e) {
return e
? {
...e,
inlined: e.inlined ? t.from(e.inlined, "base64") : null,
externalBlob: r(e.externalBlob),
externalDrive: i(e.externalDrive),
}
: {}
}
function a(e) {
return e && (e.pointer || e.preview)
? (e.metadata?.userdata && (e.metadata.userdata = t.from(e.metadata.userdata, "base64")),
{ ...e, pointer: l(e.pointer), preview: d(e.preview) })
: null
}
function d(e) {
return e ? { small: a(e.small), medium: a(e.medium), large: a(e.large) } : null
}
function o(t, r) {
const i =
r.mentions?.map((t) => ({
range: { start: t.start, length: t.length },
memberKey: e.decode(t.memberId),
})) || null,
l =
r.translations?.map((e) => ({
range: { start: e.start, length: e.length },
language: e.language,
text: e.text,
})) || null,
d = {
httpLink: r?.httpLinks?.map(n),
pearLink: r?.pearLinks?.map(n),
mention: i,
translation: l,
bold: r.bold || [],
italic: r.italic || [],
code: r.code || [],
emoji: r?.emoji?.map(({ start: e, length: t, content: n }) => ({
range: { start: e, length: t },
content: n,
})),
codeBlock: r.codeBlock || [],
strike: r.strike || [],
},
o = {
callStarted: !0 === r?.callStarted,
callHangup: !0 === r?.callHangup,
callMissed: !0 === r?.callMissed,
locationShared: r?.locationShared,
}
let s = null
r.replyTo && (s = g(r.replyTo))
const c = !0 === r.forwarded,
u = []
for (const e of r?.files || []) {
const t = a(e)
t && u.push(t)
}
return { text: t, replyTo: s, forwarded: c, display: d, files: u, event: o }
}
function s({ gt: t, gte: n, lt: r, lte: i, limit: l, reverse: a } = {}) {
return {
limit: l,
reverse: a,
gt: t ? { key: e.decode(t) } : null,
gte: n ? { key: e.decode(n) } : null,
lt: r ? { key: e.decode(r) } : null,
lte: i ? { key: e.decode(i) } : null,
}
}
function c(e, t) {
const n = { ...t },
r = n.prefix || 0
return (
Number.isInteger(n.gt) && (n.gt = { id: { thread: r, index: t.gt } }),
Number.isInteger(n.gte) && (n.gte = { id: { thread: r, index: t.gte } }),
Number.isInteger(n.lt) && (n.lt = { id: { thread: r, index: t.lt } }),
Number.isInteger(n.lte) && (n.lte = { id: { thread: r, index: t.lte } }),
n.gt || n.gte || (n.gte = { id: { thread: r } }),
n.lt || n.lte || (n.lte = { id: { thread: r } }),
n
)
}
function u(
{ reverse: e = !0, limit: t = 50, gt: n, gte: r, lt: i, lte: l, type: a } = {
reverse: !0,
limit: 50,
},
) {
const d = { reverse: e, limit: t }
return (
Number.isInteger(n) && (d.gt = a ? { id: n, type: a } : { id: n }),
Number.isInteger(r) && (d.gte = a ? { id: r, type: a } : { id: r }),
Number.isInteger(i) && (d.lt = a ? { id: i, type: a } : { id: i }),
Number.isInteger(l) && (d.lte = a ? { id: l, type: a } : { id: l }),
a &&
(e && !d.lte && (d.lte = { type: a }),
e || d.gte || (d.gte = { type: a }),
d.lte && !d.gte && (d.gte = { type: a }),
d.gte && !d.lte && (d.lte = { type: a })),
d
)
}
function g(t) {
return { key: e.decode(t.deviceId), seq: t.seq }
}
function p(e) {
return e ? { ...e, avatar: d(e.avatar), blindPeers: y(e.blindPeers) } : null
}
function m(e) {
return e
? {
disableReadReceipts: !0 === e.disableReadReceipts,
stripAttachmentMetadata: !0 === e.stripAttachmentMetadata,
}
: null
}
function y(t) {
return t && Array.isArray(t) ? t.map((t) => e.decode(t)) : []
}
function b({ memberId: t, blindWriters: n, displayName: r, avatar: i }) {
return { memberKey: e.decode(t), blindWriters: f(n), displayName: r, avatar: d(i) }
}
function f(t) {
return t && Array.isArray(t)
? t.map((t) => ({ publicKey: e.decode(t.publicKey), blindingKey: e.decode(t.blindingKey) }))
: []
}
module.exports = {
decodeContactInfo: b,
decodeChatRangeOptions: c,
decodeKeyRangeOptions: s,
decodeChatMessage: o,
decodeImageFile: d,
decodeFile: a,
decodeMessageId: g,
decodeConfig: p,
decodeUserSettings: m,
decodeFileRangeOptions: u,
}