OverviewFilesCommitsIssuesDocsReleasesPeers

keet-deobfuscated / @holepunchto / keet-store / store / chat / last-message / last-message.reducer.js

82 lines · javascript ref:
import { EMPTY_OBJ as e } from "../../lib/object"
import { updateMessage as r, updateChatLastMessage as t } from "../message"
import {
  updateChatLastMessageBlocked as o,
  updateChatLastMessageMentions as s,
} from "./last-message.action"
import { LAST_MESSAGE_MAX_LENGTH as a } from "./last-message.constant"
const n = (e) => (e < 10 ? 0 : e > 10 ? 3 : 0)
export const lastMessageReducer = (m = e, d) => {
  switch (d.type) {
    case t.type: {
      const {
        roomId: e,
        ignoreBlocked: t,
        message: o,
        unread: s,
        prevSeen: c,
        peerNotSeen: i,
      } = d.payload
      if (!o) return m
      const p = m[e]?.message
      let l = o
      return (
        l.id === p?.id &&
          (l = {
            ...o,
            reported: l.reported ?? p.reported,
            reportedByMe: l.reportedByMe ?? p.reportedByMe,
          }),
        {
          ...m,
          [e]: {
            ignoreBlocked: t,
            message: r(l, { range: { start: 0, end: a } }),
            unread: s,
            mentionUnread: n(s),
            prevSeen: c,
            peerNotSeen: i,
          },
        }
      )
    }
    case o.type: {
      const { blockedByMemberId: e } = d.payload
      let r = null
      return (
        Object.keys(m).forEach((t) => {
          const o = m[t]
          if (!o) return
          const { ignoreBlocked: s } = o
          let { message: a } = o
          if (!a) return
          const n = !!e[a.memberId] && !s
          ;(!!a.blocked !== n && (a = { ...a, blocked: n }),
            a !== o.message && ((r = r || {}), (r[t] = { ...o, message: a })))
        }),
        r ? { ...m, ...r } : m
      )
    }
    case s.type: {
      const { roomId: e, profile: t } = d.payload,
        { memberId: o, displayName: s, capabilities: a } = t || {},
        { message: n } = m[e]
      if (!n) return m
      const c = n?.chat?.mentions
      return c?.some((e) => e.memberId === o)
        ? {
            ...m,
            [e]: {
              ...m[e],
              message: r(n, {
                memberById: { [o]: { memberId: o, displayName: s, capabilities: a } },
              }),
            },
          }
        : m
    }
    default:
      return m
  }
}