@octane-xplat/contacts 0.19.0 → 0.20.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -35
- package/package.json +5 -5
- package/src/contacts.macos.ts +326 -287
- package/src/contacts.ts +234 -203
- package/src/contacts.web.ts +40 -35
- package/src/contacts.windows.ts +10 -9
- package/src/types.ts +107 -107
- package/types/generated/.tsrx-typegen-manifest.json +0 -6
- package/types/generated/contacts.linux.d.ts +0 -1
- package/types/generated/contacts.macos.d.ts +0 -3
- package/types/generated/contacts.web.d.ts +0 -3
- package/types/generated/contacts.windows.d.ts +0 -3
- package/types/generated/index.macos.d.ts +0 -2
- package/types/generated/index.web.d.ts +0 -2
package/src/contacts.macos.ts
CHANGED
|
@@ -4,341 +4,380 @@
|
|
|
4
4
|
// it is excluded from fetch keys and `note` stays absent unless the store
|
|
5
5
|
// reports the key available.
|
|
6
6
|
import {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
7
|
+
CNContact,
|
|
8
|
+
CNContactFetchRequest,
|
|
9
|
+
CNContactIdentifierKey,
|
|
10
|
+
CNContactEmailAddressesKey,
|
|
11
|
+
CNContactFamilyNameKey,
|
|
12
|
+
CNContactGivenNameKey,
|
|
13
|
+
CNContactImageDataKey,
|
|
14
|
+
CNContactJobTitleKey,
|
|
15
|
+
CNContactMiddleNameKey,
|
|
16
|
+
CNContactNicknameKey,
|
|
17
|
+
CNContactNoteKey,
|
|
18
|
+
CNContactOrganizationNameKey,
|
|
19
|
+
CNContactPhoneNumbersKey,
|
|
20
|
+
CNContactPostalAddressesKey,
|
|
21
|
+
CNContactStore,
|
|
22
|
+
CNContactUrlAddressesKey,
|
|
23
|
+
CNEntityType,
|
|
24
|
+
CNLabeledValue,
|
|
25
|
+
CNMutableContact,
|
|
26
|
+
CNPhoneNumber,
|
|
27
|
+
CNSaveRequest,
|
|
28
28
|
} from '@octane-xplat/platform/macos'
|
|
29
|
+
|
|
29
30
|
import type {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
31
|
+
Contact,
|
|
32
|
+
ContactAddress,
|
|
33
|
+
ContactField,
|
|
34
|
+
Contacts,
|
|
35
|
+
GetContactsOptions,
|
|
36
|
+
NewContact,
|
|
37
|
+
PermissionResult,
|
|
37
38
|
} from './types'
|
|
38
39
|
|
|
39
40
|
let store: any = null
|
|
40
41
|
const contactStore = () => {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
42
|
+
if (store) {
|
|
43
|
+
return store
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
try {
|
|
47
|
+
if (typeof CNContactStore?.authorizationStatusForEntityType !== 'function') {
|
|
48
|
+
return null
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return (store = CNContactStore.new())
|
|
52
|
+
} catch {
|
|
53
|
+
return null
|
|
54
|
+
}
|
|
53
55
|
}
|
|
54
56
|
|
|
55
57
|
// CNAuthorizationStatus: 0 notDetermined 1 restricted 2 denied 3 authorized
|
|
56
58
|
const rawStatus = () =>
|
|
57
|
-
|
|
59
|
+
Number(CNContactStore.authorizationStatusForEntityType(CNEntityType.CNEntityTypeContacts ?? 0))
|
|
58
60
|
|
|
59
61
|
function ensureAccess(): Promise<PermissionResult> {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
62
|
+
const s = contactStore()
|
|
63
|
+
|
|
64
|
+
if (!s) {
|
|
65
|
+
return Promise.resolve('unsupported')
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const status = rawStatus()
|
|
69
|
+
|
|
70
|
+
if (status === 3) {
|
|
71
|
+
return Promise.resolve('granted')
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (status === 1 || status === 2) {
|
|
75
|
+
return Promise.resolve('denied')
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return new Promise((resolve) => {
|
|
79
|
+
s.requestAccessForEntityTypeCompletionHandler(
|
|
80
|
+
CNEntityType.CNEntityTypeContacts ?? 0,
|
|
81
|
+
(granted: boolean) => resolve(granted ? 'granted' : 'denied'),
|
|
82
|
+
)
|
|
83
|
+
})
|
|
79
84
|
}
|
|
80
85
|
|
|
81
86
|
const BASE_KEYS = [
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
+
CNContactIdentifierKey,
|
|
88
|
+
CNContactGivenNameKey,
|
|
89
|
+
CNContactMiddleNameKey,
|
|
90
|
+
CNContactFamilyNameKey,
|
|
91
|
+
CNContactNicknameKey,
|
|
87
92
|
]
|
|
88
93
|
|
|
89
94
|
const FIELD_KEYS: Record<Exclude<ContactField, 'name'>, string[]> = {
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
95
|
+
emails: [CNContactEmailAddressesKey],
|
|
96
|
+
phoneNumbers: [CNContactPhoneNumbersKey],
|
|
97
|
+
addresses: [CNContactPostalAddressesKey],
|
|
98
|
+
urls: [CNContactUrlAddressesKey],
|
|
99
|
+
company: [CNContactOrganizationNameKey, CNContactJobTitleKey],
|
|
100
|
+
note: [], // entitlement-gated on macOS too — read only when the key is available
|
|
101
|
+
image: [CNContactImageDataKey],
|
|
102
|
+
nickname: [],
|
|
98
103
|
}
|
|
99
104
|
|
|
100
105
|
function fetchKeys(fields?: ContactField[]): string[] {
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
106
|
+
const keys = [...BASE_KEYS]
|
|
107
|
+
|
|
108
|
+
for (const field of fields ?? (Object.keys(FIELD_KEYS) as ContactField[])) {
|
|
109
|
+
for (const key of FIELD_KEYS[field as keyof typeof FIELD_KEYS] ?? []) {
|
|
110
|
+
if (!keys.includes(key)) {
|
|
111
|
+
keys.push(key)
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
return keys
|
|
110
117
|
}
|
|
111
118
|
|
|
112
119
|
const nsarray = (list: any): any[] => {
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
120
|
+
if (!list) {
|
|
121
|
+
return []
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
if (Array.isArray(list)) {
|
|
125
|
+
return list
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const out: any[] = []
|
|
129
|
+
|
|
130
|
+
for (let i = 0, count = Number(list.count ?? 0); i < count; i++) {
|
|
131
|
+
out.push(list.objectAtIndex(i))
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
return out
|
|
126
135
|
}
|
|
127
136
|
|
|
128
137
|
const labeled = (list: any, value: (entry: any) => unknown) =>
|
|
129
|
-
|
|
138
|
+
nsarray(list).map((entry) => ({
|
|
139
|
+
label: entry?.label ? String(entry.label) : undefined,
|
|
140
|
+
value: value(entry),
|
|
141
|
+
}))
|
|
130
142
|
|
|
131
143
|
function asContact(native: any): Contact {
|
|
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
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
return contact
|
|
144
|
+
const firstName = native?.givenName ? String(native.givenName) : undefined
|
|
145
|
+
const lastName = native?.familyName ? String(native.familyName) : undefined
|
|
146
|
+
const name =
|
|
147
|
+
native?.name && typeof native.name === 'string'
|
|
148
|
+
? native.name
|
|
149
|
+
: [firstName, lastName].filter(Boolean).join(' ') ||
|
|
150
|
+
(native?.organizationName ? String(native.organizationName) : '')
|
|
151
|
+
|
|
152
|
+
const contact: Contact = {
|
|
153
|
+
id: String(native?.identifier ?? ''),
|
|
154
|
+
name,
|
|
155
|
+
firstName,
|
|
156
|
+
middleName: native?.middleName ? String(native.middleName) : undefined,
|
|
157
|
+
lastName,
|
|
158
|
+
nickname: native?.nickname ? String(native.nickname) : undefined,
|
|
159
|
+
emails: labeled(native?.emailAddresses, (e) => String(e?.value ?? '')).map(
|
|
160
|
+
({ label, value }) => ({ label, email: value as string }),
|
|
161
|
+
),
|
|
162
|
+
phoneNumbers: labeled(native?.phoneNumbers, (e) => String(e?.value?.stringValue ?? '')).map(
|
|
163
|
+
({ label, value }) => ({ label, number: value as string }),
|
|
164
|
+
),
|
|
165
|
+
addresses: labeled(native?.postalAddresses, (e): ContactAddress => ({
|
|
166
|
+
street: e?.value?.street ? String(e.value.street) : undefined,
|
|
167
|
+
city: e?.value?.city ? String(e.value.city) : undefined,
|
|
168
|
+
region: e?.value?.state ? String(e.value.state) : undefined,
|
|
169
|
+
postalCode: e?.value?.postalCode ? String(e.value.postalCode) : undefined,
|
|
170
|
+
country: e?.value?.country ? String(e.value.country) : undefined,
|
|
171
|
+
})).map(({ label, value }) => ({ label, ...(value as ContactAddress) })),
|
|
172
|
+
urls: labeled(native?.urlAddresses, (e) => String(e?.value ?? '')).map(({ label, value }) => ({
|
|
173
|
+
label,
|
|
174
|
+
url: value as string,
|
|
175
|
+
})),
|
|
176
|
+
company: native?.organizationName ? String(native.organizationName) : undefined,
|
|
177
|
+
jobTitle: native?.jobTitle ? String(native.jobTitle) : undefined,
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
// The notes entitlement is a distribution choice; read it only when the
|
|
181
|
+
// fetched keys already carry it.
|
|
182
|
+
try {
|
|
183
|
+
if (native?.isKeyAvailable?.(CNContactNoteKey) && native?.note) {
|
|
184
|
+
contact.note = String(native.note)
|
|
185
|
+
}
|
|
186
|
+
} catch {
|
|
187
|
+
// A store without the notes entitlement leaves the field absent.
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
try {
|
|
191
|
+
if (native?.imageDataAvailable) {
|
|
192
|
+
const data = native?.imageData
|
|
193
|
+
const base64 = data?.base64EncodedStringWithOptions?.(0)
|
|
194
|
+
|
|
195
|
+
if (base64) {
|
|
196
|
+
contact.image = { data: String(base64) }
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
} catch {
|
|
200
|
+
// Unfetched or unreadable image data is omitted, never fatal.
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
return contact
|
|
193
204
|
}
|
|
194
205
|
|
|
195
206
|
function applySort(list: Contact[], sort?: GetContactsOptions['sort']): Contact[] {
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
207
|
+
if (!sort || sort === 'none') {
|
|
208
|
+
return list
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
const key = sort === 'firstName' ? 'firstName' : 'lastName'
|
|
199
212
|
|
|
200
|
-
|
|
201
|
-
return [...list].sort((a, b) => (a[key] ?? a.name).localeCompare(b[key] ?? b.name))
|
|
213
|
+
return [...list].sort((a, b) => (a[key] ?? a.name).localeCompare(b[key] ?? b.name))
|
|
202
214
|
}
|
|
203
215
|
|
|
204
216
|
function applyPage(list: Contact[], options?: GetContactsOptions): Contact[] {
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
217
|
+
const start = options?.pageOffset ?? 0
|
|
218
|
+
const size = options?.pageSize
|
|
219
|
+
|
|
220
|
+
return size === undefined ? list.slice(start) : list.slice(start, start + size)
|
|
208
221
|
}
|
|
209
222
|
|
|
210
223
|
function requireStore(): any {
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
224
|
+
const s = contactStore()
|
|
225
|
+
|
|
226
|
+
if (!s) {
|
|
227
|
+
throw new Error('contacts: the Contacts store is unavailable on this host')
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
return s
|
|
216
231
|
}
|
|
217
232
|
|
|
218
233
|
function applyNewContact(target: any, source: NewContact): void {
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
+
target.givenName = source.firstName ?? ''
|
|
235
|
+
target.middleName = source.middleName ?? ''
|
|
236
|
+
target.familyName = source.lastName ?? ''
|
|
237
|
+
target.nickname = source.nickname ?? ''
|
|
238
|
+
target.organizationName = source.company ?? ''
|
|
239
|
+
target.jobTitle = source.jobTitle ?? ''
|
|
240
|
+
target.emailAddresses = (source.emails ?? []).map((e) =>
|
|
241
|
+
CNLabeledValue.labeledValueWithLabelValue(e.label ?? null, e.email),
|
|
242
|
+
)
|
|
243
|
+
|
|
244
|
+
target.phoneNumbers = (source.phoneNumbers ?? []).map((p) =>
|
|
245
|
+
CNLabeledValue.labeledValueWithLabelValue(
|
|
246
|
+
p.label ?? null,
|
|
247
|
+
CNPhoneNumber.phoneNumberWithStringValue(p.number),
|
|
248
|
+
),
|
|
249
|
+
)
|
|
234
250
|
}
|
|
235
251
|
|
|
236
252
|
/** Contacts service — system address book. */
|
|
237
253
|
export const contacts: Contacts = {
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
254
|
+
get supported() {
|
|
255
|
+
return contactStore() !== null
|
|
256
|
+
},
|
|
257
|
+
|
|
258
|
+
ensure(): Promise<PermissionResult> {
|
|
259
|
+
return ensureAccess()
|
|
260
|
+
},
|
|
261
|
+
|
|
262
|
+
async getAll(options?: GetContactsOptions): Promise<Contact[]> {
|
|
263
|
+
const s = requireStore()
|
|
264
|
+
const request = CNContactFetchRequest.alloc().initWithKeysToFetch(fetchKeys(options?.fields))
|
|
265
|
+
const out: Contact[] = []
|
|
266
|
+
|
|
267
|
+
if (
|
|
268
|
+
!s.enumerateContactsWithFetchRequestErrorUsingBlock(request, null, (contact: any) => {
|
|
269
|
+
out.push(asContact(contact))
|
|
270
|
+
})
|
|
271
|
+
) {
|
|
272
|
+
throw new Error('contacts.getAll: the store refused the fetch request')
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
return applyPage(applySort(out, options?.sort), options)
|
|
276
|
+
},
|
|
277
|
+
|
|
278
|
+
async getByName(predicate: string, fields?: ContactField[]): Promise<Contact[]> {
|
|
279
|
+
const s = requireStore()
|
|
280
|
+
const list = s.unifiedContactsMatchingPredicateKeysToFetchError(
|
|
281
|
+
CNContact.predicateForContactsMatchingName(predicate),
|
|
282
|
+
fetchKeys(fields),
|
|
283
|
+
null,
|
|
284
|
+
)
|
|
285
|
+
|
|
286
|
+
return nsarray(list).map(asContact)
|
|
287
|
+
},
|
|
288
|
+
|
|
289
|
+
async getById(id: string, fields?: ContactField[]): Promise<Contact | null> {
|
|
290
|
+
const s = requireStore()
|
|
291
|
+
const native = s.unifiedContactWithIdentifierKeysToFetchError(id, fetchKeys(fields), null)
|
|
292
|
+
|
|
293
|
+
return native ? asContact(native) : null
|
|
294
|
+
},
|
|
295
|
+
|
|
296
|
+
async add(contact: NewContact): Promise<string> {
|
|
297
|
+
const s = requireStore()
|
|
298
|
+
const native = CNMutableContact.new()
|
|
299
|
+
|
|
300
|
+
applyNewContact(native, contact)
|
|
301
|
+
|
|
302
|
+
const request = CNSaveRequest.new()
|
|
303
|
+
|
|
304
|
+
request.addContactToContainerWithIdentifier(native, null)
|
|
305
|
+
if (!s.executeSaveRequestError(request, null)) {
|
|
306
|
+
throw new Error('contacts.add: the store refused the save request')
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
return String(native.identifier ?? '')
|
|
310
|
+
},
|
|
311
|
+
|
|
312
|
+
async update(id: string, contact: NewContact): Promise<string> {
|
|
313
|
+
const s = requireStore()
|
|
314
|
+
const existing = s.unifiedContactWithIdentifierKeysToFetchError(id, fetchKeys(), null)
|
|
315
|
+
|
|
316
|
+
if (!existing) {
|
|
317
|
+
throw new Error(`contacts.update: no contact with id ${id}`)
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
const native = existing.mutableCopy()
|
|
321
|
+
|
|
322
|
+
applyNewContact(native, contact)
|
|
323
|
+
|
|
324
|
+
const request = CNSaveRequest.new()
|
|
325
|
+
|
|
326
|
+
request.updateContact(native)
|
|
327
|
+
if (!s.executeSaveRequestError(request, null)) {
|
|
328
|
+
throw new Error('contacts.update: the store refused the save request')
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
return id
|
|
332
|
+
},
|
|
333
|
+
|
|
334
|
+
async remove(id: string): Promise<void> {
|
|
335
|
+
const s = requireStore()
|
|
336
|
+
const existing = s.unifiedContactWithIdentifierKeysToFetchError(
|
|
337
|
+
id,
|
|
338
|
+
[CNContactIdentifierKey],
|
|
339
|
+
null,
|
|
340
|
+
)
|
|
341
|
+
|
|
342
|
+
if (!existing) {
|
|
343
|
+
return
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
const request = CNSaveRequest.new()
|
|
347
|
+
|
|
348
|
+
request.deleteContact(existing.mutableCopy())
|
|
349
|
+
if (!s.executeSaveRequestError(request, null)) {
|
|
350
|
+
throw new Error('contacts.remove: the store refused the save request')
|
|
351
|
+
}
|
|
352
|
+
},
|
|
323
353
|
}
|
|
324
354
|
|
|
325
355
|
const permissionOwners = ((globalThis as any).__xplatPermissionOwners ??= {})
|
|
356
|
+
|
|
326
357
|
permissionOwners.contacts = {
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
358
|
+
getPermissions: async () => {
|
|
359
|
+
const s = contactStore()
|
|
360
|
+
|
|
361
|
+
if (!s) {
|
|
362
|
+
return { status: 'unsupported' as const, granted: false, canAskAgain: false }
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
const status = rawStatus()
|
|
366
|
+
|
|
367
|
+
return {
|
|
368
|
+
status:
|
|
369
|
+
status === 3
|
|
370
|
+
? ('granted' as const)
|
|
371
|
+
: status === 0
|
|
372
|
+
? ('undetermined' as const)
|
|
373
|
+
: ('denied' as const),
|
|
374
|
+
granted: status === 3,
|
|
375
|
+
canAskAgain: status === 0 || status === 3,
|
|
376
|
+
}
|
|
377
|
+
},
|
|
378
|
+
requestPermissions: async () => {
|
|
379
|
+
const result = await contacts.ensure()
|
|
380
|
+
|
|
381
|
+
return { status: result, granted: result === 'granted', canAskAgain: result === 'granted' }
|
|
382
|
+
},
|
|
344
383
|
}
|