2022-12-28 23:41:36 +01:00
|
|
|
import i18n from '@i18n/index'
|
|
|
|
import { featureCheck } from '@utils/helpers/featureCheck'
|
|
|
|
import {
|
|
|
|
checkPermission,
|
|
|
|
PERMISSION_MANAGE_REPORTS,
|
|
|
|
PERMISSION_MANAGE_USERS
|
|
|
|
} from '@utils/helpers/permissions'
|
2023-01-03 23:57:23 +01:00
|
|
|
import { queryClient } from '@utils/queryHooks'
|
2022-12-28 23:41:36 +01:00
|
|
|
import { QueryKeyProfile } from '@utils/queryHooks/profile'
|
|
|
|
import { getAccountDetails, getGlobalStorage } from '@utils/storage/actions'
|
|
|
|
import * as Notifications from 'expo-notifications'
|
|
|
|
|
2022-12-29 23:13:22 +01:00
|
|
|
export const PUSH_DEFAULT = () =>
|
|
|
|
['follow', 'follow_request', 'favourite', 'reblog', 'mention', 'poll', 'update', 'status'].filter(
|
|
|
|
type => {
|
|
|
|
switch (type) {
|
|
|
|
case 'status':
|
|
|
|
return featureCheck('notification_type_status')
|
|
|
|
case 'update':
|
|
|
|
return featureCheck('notification_type_update')
|
|
|
|
default:
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
) as ['follow', 'follow_request', 'favourite', 'reblog', 'mention', 'poll', 'update', 'status']
|
2022-12-28 23:41:36 +01:00
|
|
|
|
2022-12-29 23:13:22 +01:00
|
|
|
export const PUSH_ADMIN = () =>
|
|
|
|
[
|
|
|
|
{ type: 'admin.sign_up', permission: PERMISSION_MANAGE_USERS },
|
|
|
|
{ type: 'admin.report', permission: PERMISSION_MANAGE_REPORTS }
|
|
|
|
].filter(({ type, permission }) => {
|
|
|
|
const queryKeyProfile: QueryKeyProfile = ['Profile']
|
|
|
|
const permissions =
|
|
|
|
queryClient.getQueryData<Mastodon.Account>(queryKeyProfile)?.role?.permissions
|
|
|
|
switch (type) {
|
|
|
|
case 'admin.sign_up':
|
|
|
|
return (
|
|
|
|
featureCheck('notification_type_admin_signup') && checkPermission(permission, permissions)
|
|
|
|
)
|
|
|
|
case 'admin.report':
|
|
|
|
return (
|
|
|
|
featureCheck('notification_type_admin_report') && checkPermission(permission, permissions)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}) as { type: 'admin.sign_up' | 'admin.report'; permission: number }[]
|
2022-12-28 23:41:36 +01:00
|
|
|
|
|
|
|
export const setChannels = async (reset: boolean | undefined = false, specificAccount?: string) => {
|
|
|
|
const account = specificAccount || getGlobalStorage.string('account.active')
|
|
|
|
const accountDetails = getAccountDetails(['version', 'push'])
|
|
|
|
if (!account || !accountDetails) return null
|
|
|
|
|
|
|
|
const deleteChannel = async (type: string) =>
|
|
|
|
Notifications.deleteNotificationChannelAsync(`${account}_${type}`)
|
|
|
|
const setChannel = async (type: string) =>
|
|
|
|
Notifications.setNotificationChannelAsync(`${account}_${type}`, {
|
|
|
|
groupId: account,
|
|
|
|
name: i18n.t(`screenTabs:me.push.${type}.heading` as any),
|
|
|
|
importance: Notifications.AndroidImportance.DEFAULT,
|
|
|
|
bypassDnd: false,
|
|
|
|
showBadge: true,
|
|
|
|
enableLights: true,
|
|
|
|
enableVibrate: true
|
|
|
|
})
|
|
|
|
|
|
|
|
const channelGroup = await Notifications.getNotificationChannelGroupAsync(account)
|
|
|
|
if (channelGroup && !reset) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (!channelGroup) {
|
|
|
|
await Notifications.setNotificationChannelGroupAsync(account, { name: account })
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!accountDetails.push.decode) {
|
|
|
|
await setChannel('default')
|
2022-12-29 23:13:22 +01:00
|
|
|
for (const push of PUSH_DEFAULT()) {
|
2022-12-28 23:41:36 +01:00
|
|
|
await deleteChannel(push)
|
|
|
|
}
|
2022-12-29 23:13:22 +01:00
|
|
|
for (const { type } of PUSH_ADMIN()) {
|
2022-12-28 23:41:36 +01:00
|
|
|
await deleteChannel(type)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
await deleteChannel('default')
|
2022-12-29 23:13:22 +01:00
|
|
|
for (const push of PUSH_DEFAULT()) {
|
2022-12-28 23:41:36 +01:00
|
|
|
await setChannel(push)
|
|
|
|
}
|
2022-12-29 23:13:22 +01:00
|
|
|
for (const { type } of PUSH_ADMIN()) {
|
2022-12-28 23:41:36 +01:00
|
|
|
await setChannel(type)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|