Home Reference Source

src/contacts/OwnPresence.js

import {Presence} from './Presence';
import {scaleImg} from '../utils';
import Logs from '../Logs';
import {PresenceActivity} from './PresenceActivity';
import {PresenceMood} from './PresenceMood';

const log = Logs.instance.getLogger('SippoJS/OwnPresence');
/**
 * This class must not be instantiated. OwnPresence object can be accessed using
 * {@link ContactManager#OwnPresence}
 */
export class OwnPresence extends Presence {

	constructor(stack, presenceManager, ownAddress) {
		super(presenceManager, ownAddress);
		this._stack = stack;
	}

	/**
	 * @type {boolean}
	 */
	get online() {
		return super.online;
	}

	/**
	 * @type {boolean}
	 */
	set online(online) {
		this._online = online;
		this._sendUpdate();
	}

	/**
	 * @type {PresenceActivity}
	 */
	get activity() {
		return super.activity;
	}

	/**
	 * @type {PresenceActivity}
	 */
	set activity(activity) {
		if (!PresenceActivity[activity]) {
			throw Error(`${activity} is not a valid activity`);
		}
		this._activity = activity;
		this._sendUpdate();
	}

	/**
	 * @type {PresenceMood}
	 */
	get mood() {
		return super.mood;
	}

	/**
	 * @type {PresenceMood}
	 */
	set mood(mood) {
		if (!PresenceMood[mood]) {
			throw Error(`${mood} is not a valid mood`);
		}
		this._mood = mood;
		this._sendUpdate();
	}

	/**
	 * @type {string}
	 */
	get note() {
		return super.note;
	}

	/**
	 * @type {string}
	 */
	set note(note) {
		this._note = note;
		this._sendUpdate();
	}

	/**
	 * @type {string}
	 */
	get avatar() {
		return super.avatar;
	}

	/**
	 * @type {string}
	 */
	set avatar(avatar) {
		this._avatar = avatar;
		this._sendUpdate();
	}

	/**
	 * @type {string}
	 */
	get displayName() {
		return super.displayName;
	}

	/**
	 * @type {string}
	 */
	set displayName(displayName) {
		this._displayName = displayName;
		this._sendUpdate();
	}

	setDisplayName(displayName) {
		this._displayName = displayName;
		return this._sendUpdate();
	}

	/**
	 * Sends to the server the current values
	 * @private
	 * @return {Promise<undefined>}
	 */
	_sendUpdate() {
		return this._presenceManager.updatePresence(this.address, this.toJSON()).catch((err) => {
			log.error(`Error updating presence: ${err}`);
		});
	}

	scaleImg(url, width, height) {
		return scaleImg(url, width, height);
	}
}