Skip to content

Commit 6c60668

Browse files
committed
feat: add instantMessageAddresses optional property
1 parent 959babf commit 6c60668

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-4
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ console.log(`Authorization access to contacts is: ${authStatus}`)
4848

4949
### contacts.getAllContacts([extraProperties])
5050

51-
* `extraProperties` string[] (optional) - an array of extra contact properties to fetch that can be any of: `jobTitle`, `departmentName`, `organizationName`, `middleName`, `note`, `contactImage`, `contactThumbnailImage`, or `socialProfiles`.
51+
* `extraProperties` string[] (optional) - an array of extra contact properties to fetch that can be any of: `jobTitle`, `departmentName`, `organizationName`, `middleName`, `note`, `contactImage`, `contactThumbnailImage`, `instantMessageAddresses`, or `socialProfiles`.
5252

5353
Returns `Array<Object>` - Returns an array of contact objects.
5454

@@ -69,6 +69,7 @@ The returned objects will take the following format:
6969
* `contactImage` Buffer (optional) - a Buffer representation of the contact's profile picture.
7070
* `contactThumbnailImage` Buffer (optional) - a Buffer representation of The thumbnail version of the contact’s profile picture.
7171
* `socialProfiles` Object[] (optional) - An array of labeled social profiles for a contact.
72+
* `instantMessageAddresses` Object[] (optional) - An array of labeled IM addresses for the contact.
7273

7374
This method will return an empty array (`[]`) if access to Contacts has not been granted.
7475

@@ -95,7 +96,7 @@ console.log(allContacts[0])
9596

9697
### contacts.getContactsByName(name[, extraProperties])
9798

98-
* `extraProperties` string[] (optional) - an array of extra contact properties to fetch that can be any of: `jobTitle`, `departmentName`, `organizationName`, `middleName`, `note`, `contactImage`, `contactThumbnailImage`, or `socialProfiles`.
99+
* `extraProperties` string[] (optional) - an array of extra contact properties to fetch that can be any of: `jobTitle`, `departmentName`, `organizationName`, `middleName`, `note`, `contactImage`, `contactThumbnailImage`, `instantMessageAddresses`, or `socialProfiles`.
99100

100101
Returns `Array<Object>` - Returns an array of contact objects where either the first or last name of the contact matches `name`.
101102

@@ -118,6 +119,7 @@ The returned object will take the following format:
118119
* `contactImage` Buffer (optional) - a Buffer representation of the contact's profile picture.
119120
* `contactThumbnailImage` Buffer (optional) - a Buffer representation of The thumbnail version of the contact’s profile picture.
120121
* `socialProfiles` Object[] (optional) - An array of labeled social profiles for a contact.
122+
* `instantMessageAddresses` Object[] (optional) - An array of labeled IM addresses for the contact.
121123

122124
This method will return an empty array (`[]`) if access to Contacts has not been granted.
123125

contacts.mm

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,30 @@
8181
return social_profiles;
8282
}
8383

84+
// Parses and returns an array of instant message addresses as objects.
85+
Napi::Array GetInstantMessageAddresses(Napi::Env env, CNContact *cncontact) {
86+
int num_im_addresses = [[cncontact instantMessageAddresses] count];
87+
Napi::Array im_addresses = Napi::Array::New(env, num_im_addresses);
88+
89+
NSArray *addresses =
90+
(NSArray *)[[cncontact instantMessageAddresses] valueForKey:@"value"];
91+
for (int i = 0; i < num_im_addresses; i++) {
92+
Napi::Object address = Napi::Object::New(env);
93+
CNSocialProfile *im_address = [addresses objectAtIndex:i];
94+
95+
address.Set("service", std::string([im_address service]
96+
? [[im_address service] UTF8String]
97+
: ""));
98+
address.Set("username", std::string([im_address username]
99+
? [[im_address username] UTF8String]
100+
: ""));
101+
102+
im_addresses[i] = address;
103+
}
104+
105+
return im_addresses;
106+
}
107+
84108
// Parses and returns birthdays as strings in YYYY-MM-DD format.
85109
std::string GetBirthday(CNContact *cncontact) {
86110
std::string result;
@@ -166,6 +190,11 @@
166190
contact.Set("middleName", std::string([[cncontact middleName] UTF8String]));
167191
}
168192

193+
if ([cncontact isKeyAvailable:CNContactInstantMessageAddressesKey]) {
194+
contact.Set("instantMessageAddresses",
195+
GetInstantMessageAddresses(env, cncontact));
196+
}
197+
169198
if ([cncontact isKeyAvailable:CNContactSocialProfilesKey]) {
170199
contact.Set("socialProfiles", GetSocialProfiles(env, cncontact));
171200
}
@@ -265,6 +294,11 @@ CNAuthorizationStatus AuthStatus() {
265294
[keys addObject:CNContactNoteKey];
266295
} else if (key == "middleName") {
267296
[keys addObject:CNContactMiddleNameKey];
297+
} else if (key == "instantMessageAddresses") {
298+
[keys addObjectsFromArray:@[
299+
CNContactInstantMessageAddressesKey,
300+
CNInstantMessageAddressServiceKey, CNInstantMessageAddressUsernameKey
301+
]];
268302
} else if (key == "socialProfiles") {
269303
[keys addObjectsFromArray:@[
270304
CNContactSocialProfilesKey, CNSocialProfileServiceKey,

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const contacts = require('bindings')('contacts.node')
22

33
function getAllContacts(extraProperties = []) {
4-
if (Array.isArray(extraProperties)) {
4+
if (!Array.isArray(extraProperties)) {
55
throw new TypeError('extraProperties must be an array')
66
}
77

@@ -13,7 +13,7 @@ function getContactsByName(name, extraProperties = []) {
1313
throw new TypeError('name must be a string')
1414
}
1515

16-
if (Array.isArray(extraProperties)) {
16+
if (!Array.isArray(extraProperties)) {
1717
throw new TypeError('extraProperties must be an array')
1818
}
1919

0 commit comments

Comments
 (0)