OverviewFilesCommitsIssuesDocsReleasesPeers

keet-cli / keet-core-4.14.4-pretty / lib / core / contract / stats / chat.js

33 lines · javascript ref:
const t = require("b4a")
module.exports = async function (e, s, n, a) {
  const i = s,
    m = n.message?.display?.mention || [],
    o = a?.message?.display?.mention || [],
    c = o.length ? new Set(o.map(({ memberKey: e }) => t.toString(e, "hex"))) : null,
    r = c ? m.filter(({ memberKey: e }) => !c.has(t.toString(e, "hex"))) : m,
    h = []
  for (const { memberKey: t } of r) h.push(e.get("@keet/member-activity", { key: t }))
  const l = await Promise.all(h)
  if (n && !a) {
    const t = Math.max(i.stats.chat.timestamp, n.timestamp)
    ;((i.stats.chat.clock += 1), (i.stats.chat.timestamp = t), (i.count += 1))
  }
  const p = !!a && a.status.isPinned,
    y = !!n && n.status.isPinned
  ;(y && !p && (i.stats.chat.pinned += 1), !y && p && (i.stats.chat.pinned -= 1))
  const u = []
  if (r.length)
    for (let t = 0; t < r.length; t++) {
      const s = r[t],
        n = l[t]
      u.push(
        e.insert("@keet/member-activity", {
          ...n,
          key: s.memberKey,
          mentions: n ? n.mentions + 1 : 1,
        }),
      )
    }
  ;(u.push(e.insert("@keet/threads", i)), await Promise.all(u))
}