models/communities/Group.js

import CommunitiesSettings from './CommunitiesSettings.js';
import Membership from './Membership.js';
import { getLocalizedProperty } from '../../utils';

export default class Group {
    /**
     * Creates a Group.
     *
     * @constructor Group
     * @param {object} groupMap - Group parameters
     * @param {string} groupMap.id - ID
     * @param {string} [groupMap.title] - Title
     * @param {string} [groupMap.description] - Description
     * @param {string} [groupMap.avatarUrl] - Avatar URL
     * @param {number} groupMap.createdAt - Creation date
     * @param {number} groupMap.updatedAt - Updated date
     * @param {CommunitiesSettings} groupMap.settings - Settings
     * @param {number} [groupMap.followersCount=0] - Followers count
     * @param {number} [groupMap.membersCount=0] - Members count
     * @param {Membership} [groupMap.membership] - User's membership
     * @param {boolean} [groupMap.isFollowedByMe=false] - True if followed by current user
     * @param {number} [groupMap.popularity=0] - Popularity
     */
    constructor(groupMap) {
        this.id = groupMap.id || null;
        this.title = groupMap.title || null;
        this.description = groupMap.description || null;
        this.avatarUrl = groupMap.avatarUrl || null;
        this.createdAt = groupMap.createdAt || null;
        this.updatedAt = groupMap.updatedAt || null;
        this.settings = new CommunitiesSettings(groupMap.settings);
        this.followersCount = groupMap.followersCount || 0;
        this.isFollowedByMe = groupMap.isFollowedByMe === true;
        this.membersCount = groupMap.membersCount || 0;
        this.popularity = groupMap.popularity || 0;

        const rawMembership = groupMap.membership;
        if (rawMembership !== undefined && rawMembership != null) {
            const membership = new Membership(rawMembership);
            this.membership = membership;
        }

        Object.freeze(this);
    }

    /**
     * Create Group from SGGroup
     *
     * @memberof Group
     * @param {object} group - SGGroup
     * @returns {Group} Group instance
     * @ignore
     */
    static create(group) {
        const title = getLocalizedProperty(group, 'title');
        const description = getLocalizedProperty(group, 'groupDescription');

        return new Group({
            ...group,
            title,
            description,
            isFollowedByMe: group.isFollower === true,
            popularity: group.score
        });
  }
}