1
0
mirror of https://github.com/tooot-app/app synced 2025-06-05 22:19:13 +02:00
No physical phone to test
This commit is contained in:
xmflsct
2022-12-10 00:31:11 +01:00
parent 1b58bcad3e
commit e7fb9ed452
7 changed files with 115 additions and 175 deletions

View File

@ -10,10 +10,10 @@ import { useMutation, useQuery, UseQueryOptions } from 'react-query'
type AccountWithSource = Mastodon.Account & Required<Pick<Mastodon.Account, 'source'>>
type QueryKeyProfile = ['Profile']
export type QueryKeyProfile = ['Profile']
const queryKey: QueryKeyProfile = ['Profile']
const queryFunction = async () => {
const queryFunctionProfile = async () => {
const res = await apiInstance<AccountWithSource>({
method: 'get',
url: `accounts/verify_credentials`
@ -26,7 +26,7 @@ const useProfileQuery = (
options: UseQueryOptions<AccountWithSource, AxiosError>
} | void
) => {
return useQuery(queryKey, queryFunction, params?.options)
return useQuery(queryKey, queryFunctionProfile, params?.options)
}
type MutationVarsProfileBase =
@ -155,4 +155,4 @@ const useProfileMutation = () => {
)
}
export { useProfileQuery, useProfileMutation }
export { queryFunctionProfile, useProfileQuery, useProfileMutation }

View File

@ -1,11 +0,0 @@
import * as Notifications from 'expo-notifications'
const androidDefaults = {
importance: Notifications.AndroidImportance.DEFAULT,
bypassDnd: false,
showBadge: true,
enableLights: true,
enableVibrate: true
}
export default androidDefaults

View File

@ -1,17 +1,15 @@
import apiInstance from '@api/instance'
import apiTooot, { TOOOT_API_DOMAIN } from '@api/tooot'
import { displayMessage } from '@components/Message'
import i18n from '@root/i18n/i18n'
import { RootState } from '@root/store'
import * as Sentry from '@sentry/react-native'
import { InstanceLatest } from '@utils/migrations/instances/migration'
import { getInstance } from '@utils/slices/instancesSlice'
import * as Notifications from 'expo-notifications'
import * as Random from 'expo-random'
import i18next from 'i18next'
import { Platform } from 'react-native'
import base64 from 'react-native-base64'
import androidDefaults from './androidDefaults'
import { setChannels } from './utils'
const subscribe = async ({
expoToken,
@ -100,46 +98,7 @@ const pushRegister = async (
})
if (Platform.OS === 'android') {
Notifications.setNotificationChannelGroupAsync(accountFull, {
name: accountFull,
...androidDefaults
}).then(group => {
if (group) {
if (instancePush.decode === false) {
Notifications.setNotificationChannelAsync(`${group.id}_default`, {
groupId: group.id,
name: i18n.t('meSettingsPush:content.default.heading'),
...androidDefaults
})
} else {
Notifications.setNotificationChannelAsync(`${group.id}_follow`, {
groupId: group.id,
name: i18n.t('meSettingsPush:content.follow.heading'),
...androidDefaults
})
Notifications.setNotificationChannelAsync(`${group.id}_favourite`, {
groupId: group.id,
name: i18n.t('meSettingsPush:content.favourite.heading'),
...androidDefaults
})
Notifications.setNotificationChannelAsync(`${group.id}_reblog`, {
groupId: group.id,
name: i18n.t('meSettingsPush:content.reblog.heading'),
...androidDefaults
})
Notifications.setNotificationChannelAsync(`${group.id}_mention`, {
groupId: group.id,
name: i18n.t('meSettingsPush:content.mention.heading'),
...androidDefaults
})
Notifications.setNotificationChannelAsync(`${group.id}_poll`, {
groupId: group.id,
name: i18n.t('meSettingsPush:content.poll.heading'),
...androidDefaults
})
}
}
})
setChannels(instance)
}
return Promise.resolve(auth)

View File

@ -0,0 +1,76 @@
import { PERMISSION_MANAGE_REPORTS, PERMISSION_MANAGE_USERS } from '@helpers/permissions'
import queryClient from '@helpers/queryClient'
import i18n from '@root/i18n/i18n'
import { InstanceLatest } from '@utils/migrations/instances/migration'
import { queryFunctionProfile, QueryKeyProfile } from '@utils/queryHooks/profile'
import * as Notifications from 'expo-notifications'
export const PUSH_DEFAULT: [
'follow',
'follow_request',
'favourite',
'reblog',
'mention',
'poll',
'status'
] = ['follow', 'follow_request', 'favourite', 'reblog', 'mention', 'poll', 'status']
export const PUSH_ADMIN: { type: 'admin.sign_up' | 'admin.report'; permission: number }[] = [
{ type: 'admin.sign_up', permission: PERMISSION_MANAGE_USERS },
{ type: 'admin.report', permission: PERMISSION_MANAGE_REPORTS }
]
export const checkPushAdminPermission = (
permission: number,
permissions?: string | number
): boolean =>
permissions
? !!(
(typeof permissions === 'string' ? parseInt(permissions || '0') : permissions) & permission
)
: false
export const setChannels = async (instance: InstanceLatest) => {
const account = `@${instance.account.acct}@${instance.uri}`
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`),
importance: Notifications.AndroidImportance.DEFAULT,
bypassDnd: false,
showBadge: true,
enableLights: true,
enableVibrate: true
})
const queryKey: QueryKeyProfile = ['Profile']
const profileQuery = await queryClient.fetchQuery(queryKey, queryFunctionProfile)
const channelGroup = await Notifications.getNotificationChannelGroupAsync(account)
if (!channelGroup) {
await Notifications.setNotificationChannelGroupAsync(account, { name: account })
}
if (!instance.push.decode) {
await setChannel('default')
for (const push of PUSH_DEFAULT) {
await deleteChannel(push)
}
for (const { type } of PUSH_ADMIN) {
await deleteChannel(type)
}
} else {
await deleteChannel('default')
for (const push of PUSH_DEFAULT) {
await setChannel(push)
}
for (const { type, permission } of PUSH_ADMIN) {
if (checkPushAdminPermission(permission, profileQuery.role?.permissions)) {
await setChannel(type)
}
}
}
}

View File

@ -1,12 +1,10 @@
import apiTooot from '@api/tooot'
import { createAsyncThunk } from '@reduxjs/toolkit'
import i18n from '@root/i18n/i18n'
import { RootState } from '@root/store'
import { InstanceLatest } from '@utils/migrations/instances/migration'
import * as Notifications from 'expo-notifications'
import { Platform } from 'react-native'
import { getInstance } from '../instancesSlice'
import androidDefaults from './push/androidDefaults'
import { setChannels } from './push/utils'
export const updateInstancePushDecode = createAsyncThunk(
'instances/updatePushDecode',
@ -34,49 +32,7 @@ export const updateInstancePushDecode = createAsyncThunk(
})
if (Platform.OS === 'android') {
const accountFull = `@${instance.account.acct}@${instance.uri}`
switch (disable) {
case true:
Notifications.deleteNotificationChannelAsync(`${accountFull}_default`)
Notifications.setNotificationChannelAsync(`${accountFull}_follow`, {
groupId: accountFull,
name: i18n.t('meSettingsPush:content.follow.heading'),
...androidDefaults
})
Notifications.setNotificationChannelAsync(`${accountFull}_favourite`, {
groupId: accountFull,
name: i18n.t('meSettingsPush:content.favourite.heading'),
...androidDefaults
})
Notifications.setNotificationChannelAsync(`${accountFull}_reblog`, {
groupId: accountFull,
name: i18n.t('meSettingsPush:content.reblog.heading'),
...androidDefaults
})
Notifications.setNotificationChannelAsync(`${accountFull}_mention`, {
groupId: accountFull,
name: i18n.t('meSettingsPush:content.mention.heading'),
...androidDefaults
})
Notifications.setNotificationChannelAsync(`${accountFull}_poll`, {
groupId: accountFull,
name: i18n.t('meSettingsPush:content.poll.heading'),
...androidDefaults
})
break
case false:
Notifications.setNotificationChannelAsync(`${accountFull}_default`, {
groupId: accountFull,
name: i18n.t('meSettingsPush:content.default.heading'),
...androidDefaults
})
Notifications.deleteNotificationChannelAsync(`${accountFull}_follow`)
Notifications.deleteNotificationChannelAsync(`${accountFull}_favourite`)
Notifications.deleteNotificationChannelAsync(`${accountFull}_reblog`)
Notifications.deleteNotificationChannelAsync(`${accountFull}_mention`)
Notifications.deleteNotificationChannelAsync(`${accountFull}_poll`)
break
}
setChannels(instance)
}
return Promise.resolve({ disable })