OverviewFilesCommitsIssuesDocsReleasesPeers

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

28 lines · javascript ref:
const t = 32
function e(e, n, i) {
  const o = e.reactions.findIndex((t) => t.text === i)
  ;(-1 === o ? e.reactions.push({ text: i, count: 1, latest: [n] }) : (e.reactions[o].count += 1),
    e.reactions.length > t && e.reactions.pop())
}
function n(t, e, n) {
  const i = t.reactions.findIndex((t) => t.text === n)
  if (-1 === i) return
  const o = t.reactions[i]
  ;((o.count -= 1), o.count > 0 || t.reactions.splice(i, 1))
}
module.exports = async function (t, i, o, c, s) {
  if ((o && c) || (!o && !c)) return
  const a = o ? o.id : c.id,
    r = (await t.get("@keet/reaction-digests", { id: a })) || {
      id: a,
      total: 0,
      reactions: [],
      editedAt: 0,
    }
  ;((r.editedAt = s),
    o
      ? (e(r, o.sender, o.reaction), (i.stats.chat.clock += 1), (r.total += 1))
      : (n(r, c.sender, c.reaction), (i.stats.chat.clock -= 1), (r.total -= 1)),
    await Promise.all([t.insert("@keet/threads", i), t.insert("@keet/reaction-digests", r)]))
}