import CommunitiesSettings from './CommunitiesSettings';
import { getLocalizedProperty } from '../../utils';
export default class Topic {
/**
* Creates a Topic.
*
* @constructor Topic
* @param {object} topicMap - Topic parameters
* @param {string} topicMap.id - ID
* @param {string} topicMap.title - Title
* @param {string} [topicMap.description] - Description
* @param {string} [topicMap.avatarUrl] - Avatar URL
* @param {number} topicMap.createdAt - Creation date
* @param {number} topicMap.updatedAt - Updated date
* @param {CommunitiesSettings} topicMap.settings - Settings
* @param {number} topicMap.followersCount - Followers count
* @param {boolean} [topicMap.isFollowedByMe=false] - True if followed by current user
* @param {number} [topicMap.popularity=0] - Popularity
*/
constructor(topicMap) {
this.id = topicMap.id || null;
this.title = topicMap.title || null;
this.description = topicMap.description || null;
this.avatarUrl = topicMap.avatarUrl || null;
this.createdAt = topicMap.createdAt || null;
this.updatedAt = topicMap.updatedAt || null;
this.settings = new CommunitiesSettings(topicMap.settings);
this.followersCount = topicMap.followersCount || 0;
this.isFollowedByMe = topicMap.isFollowedByMe === true;
this.popularity = topicMap.popularity || 0;
Object.freeze(this);
}
/**
* Create Topic from SGTopic
*
* @memberof Topic
* @param {object} topic - SGTopic
* @returns {Topic} Topic instance
* @ignore
*/
static create(topic) {
const title = getLocalizedProperty(topic, 'title');
const description = getLocalizedProperty(topic, 'topicDescription');
return new Topic({
...topic,
title,
description,
isFollowedByMe: topic.isFollower === true,
popularity: topic.score
});
}
}