aboutsummaryrefslogtreecommitdiff
path: root/src/services/notifications_fetcher/notifications_fetcher.service.js
blob: 32854fed5d38a94f94d83319e9ca162d98da470b (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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import apiService from '../api/api.service.js'
import { promiseInterval } from '../promise_interval/promise_interval.js'

// For using include_types when fetching notifications.
// Note: chat_mention excluded as pleroma-fe polls them separately
const mastoApiNotificationTypes = [
  'mention',
  'status',
  'favourite',
  'reblog',
  'follow',
  'follow_request',
  'move',
  'poll',
  'pleroma:emoji_reaction',
  'pleroma:chat_mention',
  'pleroma:report'
]

const update = ({ store, notifications, older }) => {
  store.dispatch('addNewNotifications', { notifications, older })
}

const fetchAndUpdate = ({ store, credentials, older = false, since }) => {
  const args = { credentials }
  const { getters } = store
  const rootState = store.rootState || store.state
  const timelineData = rootState.notifications
  const hideMutedPosts = getters.mergedConfig.hideMutedPosts

  args.includeTypes = mastoApiNotificationTypes
  args.withMuted = !hideMutedPosts

  args.timeline = 'notifications'
  if (older) {
    if (timelineData.minId !== Number.POSITIVE_INFINITY) {
      args.until = timelineData.minId
    }
    return fetchNotifications({ store, args, older })
  } else {
    // fetch new notifications
    if (since === undefined && timelineData.maxId !== Number.POSITIVE_INFINITY) {
      args.since = timelineData.maxId
    } else if (since !== null) {
      args.since = since
    }
    const result = fetchNotifications({ store, args, older })

    // If there's any unread notifications, try fetch notifications since
    // the newest read notification to check if any of the unread notifs
    // have changed their 'seen' state (marked as read in another session), so
    // we can update the state in this session to mark them as read as well.
    // The normal maxId-check does not tell if older notifications have changed
    const notifications = timelineData.data
    const readNotifsIds = notifications.filter(n => n.seen).map(n => n.id)
    const unreadNotifsIds = notifications.filter(n => !n.seen).map(n => n.id)
    if (readNotifsIds.length > 0 && readNotifsIds.length > 0) {
      const minId = Math.min(...unreadNotifsIds) // Oldest known unread notification
      if (minId !== Infinity) {
        args.since = false // Don't use since_id since it sorta conflicts with min_id
        args.minId = minId - 1 // go beyond
        fetchNotifications({ store, args, older })
      }
    }

    return result
  }
}

const fetchNotifications = ({ store, args, older }) => {
  return apiService.fetchTimeline(args)
    .then((response) => {
      if (response.errors) {
        throw new Error(`${response.status} ${response.statusText}`)
      }
      const notifications = response.data
      update({ store, notifications, older })
      return notifications
    })
    .catch((error) => {
      store.dispatch('pushGlobalNotice', {
        level: 'error',
        messageKey: 'notifications.error',
        messageArgs: [error.message],
        timeout: 5000
      })
      console.error(error)
    })
}

const startFetching = ({ credentials, store }) => {
  // Initially there's set flag to silence all desktop notifications so
  // that there won't spam of them when user just opened up the FE we
  // reset that flag after a while to show new notifications once again.
  setTimeout(() => store.dispatch('setNotificationsSilence', false), 10000)
  const boundFetchAndUpdate = () => fetchAndUpdate({ credentials, store })
  boundFetchAndUpdate()
  return promiseInterval(boundFetchAndUpdate, 10000)
}

const notificationsFetcher = {
  fetchAndUpdate,
  startFetching
}

export default notificationsFetcher