about summary refs log tree commit diff
path: root/src/lib/notifee.ts
blob: 485d79aedb810423e862655a67b113cd00b6a077 (plain) (blame)
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
import notifee, {EventType} from '@notifee/react-native'
import {AppBskyEmbedImages, AtUri} from '@atproto/api'
import {RootStoreModel} from 'state/models/root-store'
import {NotificationsFeedItemModel} from 'state/models/feeds/notifications'
import {enforceLen} from 'lib/strings/helpers'
import {sanitizeDisplayName} from './strings/display-names'
import {resetToTab} from '../Navigation'

export function init(store: RootStoreModel) {
  store.onUnreadNotifications(count => notifee.setBadgeCount(count))
  store.onPushNotification(displayNotificationFromModel)
  store.onSessionLoaded(() => {
    // request notifications permission once the user has logged in
    notifee.requestPermission()
  })
  notifee.onForegroundEvent(async ({type}: {type: EventType}) => {
    store.log.debug('Notifee foreground event', {type})
    if (type === EventType.PRESS) {
      store.log.debug('User pressed a notifee, opening notifications')
      resetToTab('NotificationsTab')
    }
  })
  notifee.onBackgroundEvent(async _e => {}) // notifee requires this but we handle it with onForegroundEvent
}

export function displayNotification(
  title: string,
  body?: string,
  image?: string,
) {
  const opts: {title: string; body?: string; ios?: any} = {title}
  if (body) {
    opts.body = enforceLen(body, 70, true)
  }
  if (image) {
    opts.ios = {
      attachments: [{url: image}],
    }
  }
  return notifee.displayNotification(opts)
}

export function displayNotificationFromModel(
  notification: NotificationsFeedItemModel,
) {
  let author = sanitizeDisplayName(
    notification.author.displayName || notification.author.handle,
  )
  let title: string
  let body: string = ''
  if (notification.isLike) {
    title = `${author} liked your post`
    body = notification.additionalPost?.thread?.postRecord?.text || ''
  } else if (notification.isRepost) {
    title = `${author} reposted your post`
    body = notification.additionalPost?.thread?.postRecord?.text || ''
  } else if (notification.isMention) {
    title = `${author} mentioned you`
    body = notification.additionalPost?.thread?.postRecord?.text || ''
  } else if (notification.isReply) {
    title = `${author} replied to your post`
    body = notification.additionalPost?.thread?.postRecord?.text || ''
  } else if (notification.isFollow) {
    title = 'New follower!'
    body = `${author} has followed you`
  } else if (notification.isCustomFeedLike) {
    title = `${author} liked your custom feed`
    body = `${new AtUri(notification.subjectUri).rkey}`
  } else {
    return
  }
  let image
  if (
    AppBskyEmbedImages.isView(
      notification.additionalPost?.thread?.post.embed,
    ) &&
    notification.additionalPost?.thread?.post.embed.images[0]?.thumb
  ) {
    image = notification.additionalPost.thread.post.embed.images[0].thumb
  }
  return displayNotification(title, body, image)
}