Home Reference Source

src/contacts/ContactRetriever.js

import {W3cContacts} from './W3cContacts';
import {
	isDevice,
	getDeviceId,
} from '../utils';

export class ContactRetriever {

	/**
	 * Constructs a get contacts instance
	 * @param {WacProxy} stack
	 */
	constructor(stack) {
		/**
		 * @type {WacProxy}
		 */
		this._stack = stack;

		/**
		 * @type {W3cContacts}
		 */
		this._w3cContacts = W3cContacts(this._stack, window || {});

		/**
		 * @type {String}
		 */
		this._deviceId = getDeviceId();
	}

	/**
	 * If we are on device, get the contacts from the WAC without the device contacts.
	 * In other case, get all the contacts stored in the WAC
	 *
	 * Return type :  <wac-contact>[]
	 */
	async getContactsWac() {
		let filter = {};
		if (isDevice()) {
			filter['deviceId'] = -1;
		}
		return this._stack.getContacts(filter);
	}

	/**
	 * Get the device contacts
	 * Return type :  <wac-contact>[]
	 */
	getContactsDevice() {
		return this._w3cContacts.getContacts();
	}

	/**
	 * Get the avatar for all device contacts
	 * @return {Map} A map with contactDeviceId as key and avatar as value
	 */
	getAvatarDeviceContacts() {
		return this._w3cContacts.getAvatarContacts().then((avatars) => {
			let avatarsContacts = new Map();
			avatars = avatars.filter(avatar => !!avatar.avatar);
			avatars.forEach((avatarElem) => {
				avatarsContacts.set(avatarElem.contactDeviceId, avatarElem.avatar);
			});
			return avatarsContacts;
		});
	}
}