-
Notifications
You must be signed in to change notification settings - Fork 6
/
utils.js
162 lines (149 loc) · 5.46 KB
/
utils.js
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import Toast from 'react-native-toast-message'
import {i18n} from './i18n/i18n.js'
import * as FileSystem from 'expo-file-system'
import * as Localization from 'expo-localization'
import axios from 'axios'
import { adapty } from 'react-native-adapty' // in-app purchases
import {createPaywallView} from '@adapty/react-native-ui' // in-app purchases
export async function fetchAttachments(boardId, stackId, cardId, server, token) {
console.log('fetching attachments from server')
return await axios.get(server.value + `/index.php/apps/deck/api/v1.1/boards/${boardId}/stacks/${stackId}/cards/${cardId}/attachments`, {
timeout: 8000,
headers: {
'Content-Type': 'application/json',
'Authorization': token
}
}).then((resp) => {
if (resp.status !== 200) {
Toast.show({
type: 'error',
text1: i18n.t('error'),
text2: resp,
})
console.log('Error', resp)
} else {
console.log('attachments fetched from server')
const attachments = resp.data.map(attachment => {
return {
'id': attachment.id,
'author': attachment.createdBy,
'creationDate': new Date(attachment.createdAt * 1000).toLocaleDateString(Localization.locale, { year: 'numeric', month: 'long', day: 'numeric', hour: 'numeric', minute: 'numeric' }),
'name': attachment.data
}
})
return attachments
}
}).catch((error) => {
Toast.show({
type: 'error',
text1: i18n.t('error'),
text2: error.message,
})
console.log(error)
})
}
export async function getAttachmentURI(attachment, boardId, stackId, cardId, server, token) {
console.log(`Getting attachment URI for attachement ${attachment.name}`)
try {
// iOS does not like spaces in file names.
const filename = attachment.name.replaceAll(/\s/g,'_')
// Downloads file if not already done
const fileInfo = await FileSystem.getInfoAsync(FileSystem.cacheDirectory + filename)
let uri
if (!fileInfo.exists) {
console.log('Downloading attachment')
const resp = await FileSystem.downloadAsync(
server.value + `/index.php/apps/deck/api/v1.1/boards/${boardId}/stacks/${stackId}/cards/${cardId}/attachments/file/${attachment.id}`,
FileSystem.cacheDirectory + filename,
{
headers: {
'Authorization': token
},
},
)
uri = await FileSystem.getContentUriAsync(resp.uri)
} else {
console.log('File already in cache')
uri = await FileSystem.getContentUriAsync(fileInfo.uri)
}
console.log(`attachment URI is ${uri}`)
return uri
} catch(error) {
Toast.show({
type: 'error',
text1: i18n.t('error'),
text2: error.message,
})
console.log(error)
return null
}
}
// Gets user details from the server
export async function getUserDetails(userId, server, token) {
return axios.get(server.value + `/ocs/v1.php/cloud/users/${userId}`,
{
headers: {
'Authorization': token,
'OCS-APIRequest': true,
},
}
).then( resp => {
return resp.data.ocs.data
})
}
// Tells if a user has edit rights on a board
export function canUserEditBoard(user, board) {
let canUserEditBoard = true
if (user === board.owner.uid) {
// User is owner of the board
console.log('User is owner of the board')
canUserEditBoard = true
} else {
// If user is listed in the board's acl explicitly, then return his edit permissions
const userPermissions = board.acl.find(acl => acl.participant.uid==user)?.permissionEdit
if (userPermissions !== undefined) {
console.log('User is listed in board ACL')
canUserEditBoard = userPermissions
} else {
// If user is member of several groups listed in the board's acl, every groups must have edit rights
board.acl.every( acl => {
if (user.groups.includes(acl.participant.uid)) {
console.log('User is listed in board ACL')
canUserEditBoard = acl.permissionEdit
}
})
}
}
console.log(canUserEditBoard ? 'User can edit board' : 'User cannot edit board')
return canUserEditBoard
}
// Tells if a user is subscribed to the paying version of the app
export async function isUserSubscribed() {
console.log('Getting user subscription status')
try {
const profile = await adapty.getProfile()
if (profile.accessLevels["No Ads"]?.isActive) {
console.log('User is subscribed')
return true
} else {
console.log('User is not subscribed')
return false
}
} catch (error) {
console.error(error)
return true
}
}
// Shows adapty paywall
export async function showPaywall(hard = false) {
try {
const paywallId = hard ? 'NoAdsForcedPlacement' : 'NoAdsDefaultPlacement'
console.log('Showing adapty paywall', paywallId)
const paywall = await adapty.getPaywall(paywallId, 'en')
const view = await createPaywallView(paywall)
view.registerEventHandlers()
await view.present()
} catch (error) {
console.error(error)
}
}