<commit_before>goog.provide('xmpptk.RosterItem');
goog.provide('xmpptk.RosterItem.Subscription');

goog.require('xmpptk.Model');

/**
 * @constructor
 * @extends {xmpptk.Model}
 * @param {jid: string, subscription: xmpptk.Roster.Subscription, name=: string} item props to fill item with
 */
xmpptk.RosterItem = function(item) {
    xmpptk.Model.call(this);

    /** @type {string} */
    this.jid = item['jid'];

    /** @type {xmpptk.RosterItem.Subscription} */
    this.subscription = item.subscription;

    /** @type {string} */
    this.name = item.name || (new JSJaCJID(item['jid'])).getNode();

    this._client = item.client;
    this.set('presence', xmpptk.RosterItem.PRESENCEDEFAULT);

    /** @type {Object.<string, xmpptk.Presence>} */
    this.resources = {};

    this.getVCard();
};
goog.inherits(xmpptk.RosterItem, xmpptk.Model);

xmpptk.RosterItem.id = 'jid';

/** @enum {string} */
xmpptk.RosterItem.Subscription = {
    BOTH: 'both',
    FROM: 'from',
    TO:   'to',
    NONE: 'NONE'
};

/** @enum {string} */
xmpptk.RosterItem.PresenceShow = {
    AVAILABLE: 'available',
    CHAT: 'chat',
    AWAY: 'away',
    XA: 'xa',
    DND: 'dnd',
    UNAVAILABLE: 'unavailable'
};

/** @const */
xmpptk.RosterItem.PRESENCEDEFAULT =
    {show: xmpptk.RosterItem.PresenceShow.UNAVAILABLE,
     status: '',
     priority: 0};

xmpptk.RosterItem.prototype._logger =
    goog.debug.Logger.getLogger('xmpptk.RosterItem');

/**
 * return own id
 */
xmpptk.RosterItem.prototype.getId = function() {
    return this.jid;
};

xmpptk.RosterItem.prototype.getVCard = function() {
    // lookup localStorage - if cache miss retrieve by XMPP
    var vCard;
    if (typeof 'Storage' !== 'undefined')
        vCard = sessionStorage['vCard'+this.getId()];

    if (vCard) {
        this._logger.info('got vCard from localStorage');
        this.set('vCard', goog.json.parse(vCard));
    } else {
        if (!this._client || typeof this._client.getVCard !== 'function')
            return;
        this._logger.info("retrieving vCard");
        this._client.getVCard(this.getId(), function(vCard) {
            if (!vCard)
                return;
            this._logger.info("got avatar by vCard: "+
                              goog.json.serialize(vCard));
            if (typeof 'Storage' !== 'undefined')
                sessionStorage['vCard'+this.getId()] =
                goog.json.serialize(vCard);
            this.set('vCard', vCard);
        }, this);
    }
};

/**
 * Update presence for resource given
 *
 * @param {string} resource the resource associated with this presence
 * @param {{show: xmpptk.RosterItem.PresenceShow, status=: string, priority=: number}} presence properties of the actual presence information
 */
xmpptk.RosterItem.prototype.setPresence = function(resource, presence) {
    if (presence.show === xmpptk.RosterItem.PresenceShow.UNVAILABLE)
        delete this.resources[resource];
    else
        this.resources[resource] = presence;
    this.set('presence', this._getPresence() || xmpptk.RosterItem.PRESENCEDEFAULT);
};

/**
 * Get 'show' value of presence with highest priority
 * @return {xmpptk.RosterItem.PresenceShow} a 'show' value
 */
xmpptk.RosterItem.prototype.getPresenceShow = function() {
    return this.getPresence().show;
};

/**
 * get the most significant presence information of thie entits
 *
 * @return {{show: xmpptk.RosterItem.PresenceShow, status=: string, priority=: number}}
 */
xmpptk.RosterItem.prototype.getPresence = function() {
    return this.get('presence');
};

/**
 * internal method to really determine the most significant presence
 * information of thie entits
 *<commit_msg>fix typo at presence unavailable<commit_after>goog.provide('xmpptk.RosterItem');
goog.provide('xmpptk.RosterItem.Subscription');

goog.require('xmpptk.Model');

/**
 * @constructor
 * @extends {xmpptk.Model}
 * @param {jid: string, subscription: xmpptk.Roster.Subscription, name=: string} item props to fill item with
 */
xmpptk.RosterItem = function(item) {
    xmpptk.Model.call(this);

    /** @type {string} */
    this.jid = item['jid'];

    /** @type {xmpptk.RosterItem.Subscription} */
    this.subscription = item.subscription;

    /** @type {string} */
    this.name = item.name || (new JSJaCJID(item['jid'])).getNode();

    this._client = item.client;
    this.set('presence', xmpptk.RosterItem.PRESENCEDEFAULT);

    /** @type {Object.<string, xmpptk.Presence>} */
    this.resources = {};

    this.getVCard();
};
goog.inherits(xmpptk.RosterItem, xmpptk.Model);

xmpptk.RosterItem.id = 'jid';

/** @enum {string} */
xmpptk.RosterItem.Subscription = {
    BOTH: 'both',
    FROM: 'from',
    TO:   'to',
    NONE: 'NONE'
};

/** @enum {string} */
xmpptk.RosterItem.PresenceShow = {
    AVAILABLE: 'available',
    CHAT: 'chat',
    AWAY: 'away',
    XA: 'xa',
    DND: 'dnd',
    UNAVAILABLE: 'unavailable'
};

/** @const */
xmpptk.RosterItem.PRESENCEDEFAULT =
    {show: xmpptk.RosterItem.PresenceShow.UNAVAILABLE,
     status: '',
     priority: 0};

xmpptk.RosterItem.prototype._logger =
    goog.debug.Logger.getLogger('xmpptk.RosterItem');

/**
 * return own id
 */
xmpptk.RosterItem.prototype.getId = function() {
    return this.jid;
};

xmpptk.RosterItem.prototype.getVCard = function() {
    // lookup localStorage - if cache miss retrieve by XMPP
    var vCard;
    if (typeof 'Storage' !== 'undefined')
        vCard = sessionStorage['vCard'+this.getId()];

    if (vCard) {
        this._logger.info('got vCard from localStorage');
        this.set('vCard', goog.json.parse(vCard));
    } else {
        if (!this._client || typeof this._client.getVCard !== 'function')
            return;
        this._logger.info("retrieving vCard");
        this._client.getVCard(this.getId(), function(vCard) {
            if (!vCard)
                return;
            this._logger.info("got avatar by vCard: "+
                              goog.json.serialize(vCard));
            if (typeof 'Storage' !== 'undefined')
                sessionStorage['vCard'+this.getId()] =
                goog.json.serialize(vCard);
            this.set('vCard', vCard);
        }, this);
    }
};

/**
 * Update presence for resource given
 *
 * @param {string} resource the resource associated with this presence
 * @param {{show: xmpptk.RosterItem.PresenceShow, status=: string, priority=: number}} presence properties of the actual presence information
 */
xmpptk.RosterItem.prototype.setPresence = function(resource, presence) {
    if (presence.show === xmpptk.RosterItem.PresenceShow.UNAVAILABLE)
        delete this.resources[resource];
    else
        this.resources[resource] = presence;
    this.set('presence', this._getPresence() ||
             xmpptk.RosterItem.PRESENCEDEFAULT);
};

/**
 * Get 'show' value of presence with highest priority
 * @return {xmpptk.RosterItem.PresenceShow} a 'show' value
 */
xmpptk.RosterItem.prototype.getPresenceShow = function() {
    return this.getPresence().show;
};

/**
 * get the most significant presence information of thie entits
 *
 * @return {{show: xmpptk.RosterItem.PresenceShow, status=: string, priority=: number}}
 */
xmpptk.RosterItem.prototype.getPresence = function() {
    return this.get('presence');
};

/**
 * internal method to really determine the most significant presence
 * information of thie entits
 *
{'fraction_non_alphanumeric': 0.15273421519159963, 'fraction_numerical': 0.0002727396699849993, 'mean_word_length': 3.1575963718820863, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5415768', 'n_tokens_mistral': 2459, 'n_tokens_neox': 2446, 'n_words': 478}