models/communities/SearchResult.js

import PagingResult from '../PagingResult';
import Topic from './Topic';
import Group from './Group';
import User from './User';
import Activity from './Activity';
import Label from './Label';
import Tag from './Tag';

export default class SearchResult {
    /**
    * Creates a new SearchResult
    *
    * @constructor SearchResult
    * @param {object} resultMap - Result parameters
    * @param {PagingResult<Topic[]>} resultMap.topics - Topics
    * @param {PagingResult<Group[]>} resultMap.groups - Groups
    * @param {PagingResult<User[]>} resultMap.users - Users
    * @param {PagingResult<Activity[]>} resultMap.activities - Activities
    * @param {PagingResult<Label[]>} resultMap.labels - Labels
    * @param {PagingResult<Tag[]>} resultMap.tags - Tags
    */
    constructor(resultMap) {
        this.topics = resultMap.topics;
        this.groups = resultMap.groups;
        this.users = resultMap.users;
        this.activities = resultMap.activities;
        this.labels = resultMap.labels;
        this.tags = resultMap.tags;

        Object.freeze(this);
    }

    /**
     * Create SearchResult from GlobalSearchResponse
     *
     * @memberof SearchResult
     * @param {Object} res - GlobalSearchResponse
     * @returns {SearchResult} SearchResult instance
     * @ignore
     */
    static create(res) {
        return new SearchResult({
            topics: res.topics
                ? new PagingResult(
                    res.topics.topics.map((topic) => Topic.create(topic)),
                    res.topics.nextCursor
                )
                : null,
            groups: res.groups
                ? new PagingResult(
                    res.groups.groups.map((group) => Group.create(group)),
                    res.groups.nextCursor
                )
                : null,
            users: res.users
                ? new PagingResult(
                    res.users.users.map((user) => User.create(user)),
                    res.users.nextCursor
                )
                : null,
            activities: res.activities
                ? new PagingResult(
                    res.activities.data.map((item) => {
                        const authors = res.activities.authors;
                        return Activity.create({
                            ...item,
                            author: item.author.isApp
                                ? User.appUserData()
                                : (item.author.publicUser &&
                                    authors[item.author.publicUser.id]) ||
                                    item.author.publicUser
                        })
                    }),
                    res.activities.nextCursor
                )
                : null,
            labels: res.labels
                ? new PagingResult(
                    res.labels.labels.map((label) => Label.create(label)),
                    res.labels.nextCursor
                )
                : null,
            tags: res.hashtags
                ? new PagingResult(
                    res.hashtags.hashtags.map((tag) => Tag.create(tag)),
                    res.hashtags.nextCursor
                )
                : null,
        });
    }
}