Skip to content

Commit

Permalink
Step 15.4: Implement getContactsFromAddressbook in the phone service
Browse files Browse the repository at this point in the history
  • Loading branch information
darkbasic committed Oct 16, 2017
1 parent a953d70 commit eae8c9a
Showing 1 changed file with 59 additions and 1 deletion.
60 changes: 59 additions & 1 deletion src/services/phone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Accounts } from 'meteor/accounts-base';
import { Meteor } from 'meteor/meteor';
import { Platform } from 'ionic-angular';
import { Sim } from '@ionic-native/sim';
import { Contact, ContactFieldType, Contacts, IContactField, IContactFindOptions } from "@ionic-native/contacts";
import { SmsReceiver } from "../ionic/sms-receiver";
import * as Bluebird from "bluebird";
import { TWILIO_SMS_NUMBERS } from "api/models";
Expand All @@ -12,7 +13,8 @@ import { Observable } from "rxjs";
export class PhoneService {
constructor(private platform: Platform,
private sim: Sim,
private smsReceiver: SmsReceiver) {
private smsReceiver: SmsReceiver,
private contacts: Contacts) {
Bluebird.promisifyAll(this.smsReceiver);
}

Expand Down Expand Up @@ -64,6 +66,62 @@ export class PhoneService {
}
}

getContactsFromAddressbook(): Promise<string[]> {
const getContacts = (): Promise<Contact[]> => {
if (!this.platform.is('cordova')) {
return Promise.reject(new Error('Cannot get contacts: not cordova.'));
}

const fields: ContactFieldType[] = ["phoneNumbers"];
const options: IContactFindOptions = {
filter: "",
multiple: true,
desiredFields: ["phoneNumbers"],
hasPhoneNumber: true
};
return this.contacts.find(fields, options);
};

const cleanPhoneNumber = (phoneNumber: string): string => {
const phoneNumberNoSpaces: string = phoneNumber.replace(/ /g, '');

if (phoneNumberNoSpaces.charAt(0) === '+') {
return phoneNumberNoSpaces;
} else if (phoneNumberNoSpaces.substring(0, 2) === "00") {
return '+' + phoneNumberNoSpaces.slice(2);
} else {
// Use user's international prefix when absent
// FIXME: update meteor-accounts-phone typings
const prefix: string = (<any>Meteor.user()).phone.number.substring(0, 3);

return prefix + phoneNumberNoSpaces;
}
};

return new Promise((resolve, reject) => {
getContacts()
.then((contacts: Contact[]) => {
const arrayOfArrays: string[][] = contacts
.map((contact: Contact) => {
return contact.phoneNumbers
.filter((phoneNumber: IContactField) => {
return phoneNumber.type === "mobile";
}).map((phoneNumber: IContactField) => {
return cleanPhoneNumber(phoneNumber.value);
}).filter((phoneNumber: string) => {
return phoneNumber.slice(1).match(/^[0-9]+$/) && phoneNumber.length >= 8;
});
});
const flattenedArray: string[] = [].concat(...arrayOfArrays);
const uniqueArray: string[] = [...new Set(flattenedArray)];
resolve(uniqueArray);
})
.catch((e: Error) => {
reject(e);
});
});
}

verify(phoneNumber: string): Promise<void> {
return new Promise<void>((resolve, reject) => {
Accounts.requestPhoneVerification(phoneNumber, (e: Error) => {
Expand Down

0 comments on commit eae8c9a

Please sign in to comment.