aboutsummaryrefslogtreecommitdiff
path: root/src/services/chat_utils/chat_utils.js
blob: a8da1eed7dee7d76014580acb37d9ec0aa387822 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { showDesktopNotification } from '../desktop_notification_utils/desktop_notification_utils.js'

export const maybeShowChatNotification = (store, chat) => {
  if (!chat.lastMessage) return
  if (store.rootState.chats.currentChatId === chat.id && !document.hidden) return
  if (store.rootState.users.currentUser.id === chat.lastMessage.account_id) return

  const opts = {
    tag: chat.lastMessage.id,
    title: chat.account.name,
    icon: chat.account.profile_image_url,
    body: chat.lastMessage.content
  }

  if (chat.lastMessage.attachment && chat.lastMessage.attachment.type === 'image') {
    opts.image = chat.lastMessage.attachment.preview_url
  }

  showDesktopNotification(store.rootState, opts)
}

export const buildFakeMessage = ({ content, chatId, attachments, userId, idempotencyKey }) => {
  const fakeMessage = {
    content,
    chat_id: chatId,
    created_at: new Date(),
    id: `${new Date().getTime()}`,
    attachments,
    account_id: userId,
    idempotency_key: idempotencyKey,
    emojis: [],
    pending: true,
    isNormalized: true
  }

  if (attachments[0]) {
    fakeMessage.attachment = attachments[0]
  }

  return fakeMessage
}