Skip to content

Commit 959babf

Browse files
committed
feat: add socialProfiles optional property
1 parent b6bc0b6 commit 959babf

File tree

3 files changed

+49
-5
lines changed

3 files changed

+49
-5
lines changed

README.md

Lines changed: 6 additions & 4 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`, or `contactThumbnailImage`.
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`.
5252

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

@@ -68,6 +68,7 @@ The returned objects will take the following format:
6868
* `note` String (optional) - The note associated with the contact.
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.
71+
* `socialProfiles` Object[] (optional) - An array of labeled social profiles for a contact.
7172

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

@@ -77,7 +78,7 @@ Example:
7778
const allContacts = contacts.getAllContacts()
7879

7980
console.log(allContacts[0])
80-
/* prints
81+
/* Prints:
8182
[
8283
{
8384
firstName: 'Jonathan',
@@ -94,7 +95,7 @@ console.log(allContacts[0])
9495

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

97-
* `extraProperties` string[] (optional) - an array of extra contact properties to fetch that can be any of: `jobTitle`, `departmentName`, `organizationName`, `middleName`, `note`, `contactImage`, or `contactThumbnailImage`.
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`.
9899

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

@@ -116,6 +117,7 @@ The returned object will take the following format:
116117
* `note` String (optional) - The note associated with the contact.
117118
* `contactImage` Buffer (optional) - a Buffer representation of the contact's profile picture.
118119
* `contactThumbnailImage` Buffer (optional) - a Buffer representation of The thumbnail version of the contact’s profile picture.
120+
* `socialProfiles` Object[] (optional) - An array of labeled social profiles for a contact.
119121

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

@@ -125,7 +127,7 @@ Example:
125127
const contacts = contacts.getContactsByName('Appleseed')
126128

127129
console.log(contacts)
128-
/* prints
130+
/* Prints:
129131
[
130132
{
131133
firstName: 'Jonathan',

contacts.mm

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,35 @@
5252
return postal_addresses;
5353
}
5454

55+
// Parses and returns an array of social profiles as objects.
56+
Napi::Array GetSocialProfiles(Napi::Env env, CNContact *cncontact) {
57+
int num_social_profiles = [[cncontact socialProfiles] count];
58+
Napi::Array social_profiles = Napi::Array::New(env, num_social_profiles);
59+
60+
NSArray *profiles =
61+
(NSArray *)[[cncontact socialProfiles] valueForKey:@"value"];
62+
for (int i = 0; i < num_social_profiles; i++) {
63+
Napi::Object profile = Napi::Object::New(env);
64+
CNSocialProfile *social_profile = [profiles objectAtIndex:i];
65+
66+
profile.Set("service",
67+
std::string([social_profile service]
68+
? [[social_profile service] UTF8String]
69+
: ""));
70+
profile.Set("username",
71+
std::string([social_profile username]
72+
? [[social_profile username] UTF8String]
73+
: ""));
74+
profile.Set("url", std::string([social_profile urlString]
75+
? [[social_profile urlString] UTF8String]
76+
: ""));
77+
78+
social_profiles[i] = profile;
79+
}
80+
81+
return social_profiles;
82+
}
83+
5584
// Parses and returns birthdays as strings in YYYY-MM-DD format.
5685
std::string GetBirthday(CNContact *cncontact) {
5786
std::string result;
@@ -137,6 +166,10 @@
137166
contact.Set("middleName", std::string([[cncontact middleName] UTF8String]));
138167
}
139168

169+
if ([cncontact isKeyAvailable:CNContactSocialProfilesKey]) {
170+
contact.Set("socialProfiles", GetSocialProfiles(env, cncontact));
171+
}
172+
140173
return contact;
141174
}
142175

@@ -232,6 +265,12 @@ CNAuthorizationStatus AuthStatus() {
232265
[keys addObject:CNContactNoteKey];
233266
} else if (key == "middleName") {
234267
[keys addObject:CNContactMiddleNameKey];
268+
} else if (key == "socialProfiles") {
269+
[keys addObjectsFromArray:@[
270+
CNContactSocialProfilesKey, CNSocialProfileServiceKey,
271+
CNSocialProfileURLStringKey, CNSocialProfileUsernameKey,
272+
CNSocialProfileUserIdentifierKey
273+
]];
235274
}
236275
}
237276
}

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44
"description": "A native module that allows you to access and manipulate macOS contacts",
55
"main": "index.js",
66
"scripts": {
7-
"build": "node-gyp rebuild",
7+
"build": "node-gyp build",
8+
"build:dev": "node-gyp build --debug",
89
"clean": "node-gyp clean",
910
"lint": "prettier --check index.js",
1011
"format": "clang-format -i contacts.mm && prettier --write index.js",
12+
"rebuild": "node-gyp rebuild",
13+
"rebuild:dev": "node-gyp rebuild --debug",
1114
"test": "./node_modules/.bin/mocha --reporter spec"
1215
},
1316
"repository": {

0 commit comments

Comments
 (0)