models/communities/ActivitiesQuery.js

import UserId from './../UserId.js';
import CommunitiesIds from './CommunitiesIds.js';
import PollStatus from './PollStatus.js';

export default class ActivitiesQuery {
    /**
     * Creates an ActivitiesQuery
     *
     * @constructor ActivitiesQuery
     * @param {CommunitiesIds} ids - Entity IDs.
     */
    constructor(ids) {
        this.ids = ids;
    }

    /**
     * Get activities in user feed.
     *
     * @memberof ActivitiesQuery
     * @param {UserId} id - User ID.
     * @return {ActivitiesQuery} A new query.
     */
    static feedOf(id) {
        return new ActivitiesQuery(CommunitiesIds.user(id));
    }

    /**
     * Get activities in a topic.
     *
     * @memberof ActivitiesQuery
     * @param {string} id - Topic ID.
     * @return {ActivitiesQuery} A new query.
     */
    static inTopic(id) {
        return new ActivitiesQuery(CommunitiesIds.topic(id));
    }

    /**
     * Get activities from all topics.
     *
     * @memberof ActivitiesQuery
     * @return {ActivitiesQuery} A new query.
     */
    static inAllTopics() {
        return new ActivitiesQuery(CommunitiesIds.topics([]));
    }

    /**
     * Get activities in a group.
     *
     * @memberof ActivitiesQuery
     * @param {string} id - Group ID.
     * @return {ActivitiesQuery} A new query.
     */
    static inGroup(id) {
        return new ActivitiesQuery(CommunitiesIds.group(id));
    }

    /**
     * Get activities in user's timeline.
     *
     * @memberof ActivitiesQuery
     * @return {ActivitiesQuery} A new query.
     */
    static timeline() {
        return new ActivitiesQuery(CommunitiesIds.timeline());
    }

    /**
     * Get all activities.
     *
     * @memberof ActivitiesQuery
     * @return {ActivitiesQuery} A new query.
     */
    static everywhere() {
        return new ActivitiesQuery(CommunitiesIds.everywhere());
    }

    /**
     * Get comments to a certain activity.
     *
     * @memberof ActivitiesQuery
     * @param {string} id - Activity ID.
     * @return {ActivitiesQuery} A new query.
     */
    static commentsToActivity(id) {
        return new ActivitiesQuery(CommunitiesIds.activity(id));
    }

    /**
     * Get bookmarked activities.
     *
     * @memberof ActivitiesQuery
     * @return {ActivitiesQuery} A new query.
     */
    static bookmarkedActivities() {
        let query = ActivitiesQuery.everywhere();
        query.reactionGroup = 'bookmarks';

        return query;
    }

    /**
     * Get activities that match given reaction(s).
     *
     * @memberof ActivitiesQuery
     * @param {string[]} reactions - Reactions
     * @return {ActivitiesQuery} new query.
     */
    static reactedActivities(reactions) {
        let query = ActivitiesQuery.everywhere();
        query.reactionGroup = 'reactions';
        query.reactions = reactions

        return query;
    }

    /**
     * Get polls voted with the given option(s).
     *
     * @memberof ActivitiesQuery
     * @param {string[]} options - Options
     * @return {ActivitiesQuery} new query.
     */
    static votedActivities(options) {
        let query = ActivitiesQuery.everywhere();
        query.reactionGroup = 'votes';
        query.reactions = options

        return query;
    }

    /**
     * Get activities of a specific user.
     *
     * @memberof ActivitiesQuery
     * @instance
     * @param {UserId} id - Activities' Author ID.
     * @return {ActivitiesQuery} Same query.
     */
    byUser(id) {
        this.author = id;
        return this;
    }

    /**
     * Get activities with a specific tag.
     *
     * @memberof ActivitiesQuery
     * @instance
     * @param {string} tag - tag in activity text.
     * @return {ActivitiesQuery} Same query.
     */
    withTag(tag) {
        this.tag = tag
            ? '#' + tag
            : null;
        return this;
    }

    /**
     * Get activities with the specified poll status.
     *
     * @memberof ActivitiesQuery
     * @instance
     * @param {number} status - Poll status.
     * @return {ActivitiesQuery} Same query.
     */
    withPollStatus(status) {
        this.pollStatus = !!Object.keys(PollStatus)[status]
            ? status
            : null;
        return this;
    }

    /**
     * Get only trending activities.
     *
     * @memberof ActivitiesQuery
     * @instance
     * @param {boolean} trending Only trending activities or all.
     * @return {ActivitiesQuery} Same query.
     */
    onlyTrending(trending) {
        this.trending = trending;
        return this;
    }

    /**
     * Get activities matching the specified text.
     *
     * @memberof ActivitiesQuery
     * @instance
     * @param {string} searchTerm - Text.
     * @return {ActivitiesQuery} same query.
     */
    withText(searchTerm) {
        this.searchTerm = searchTerm;
        return this;
    }

    /**
     * Get activities matching the specified properties.
     *
     * @memberof ActivitiesQuery
     * @instance
     * @param {Object<string, string>} properties - Properties.
     * @return {ActivitiesQuery} same query.
     */
    withProperties(properties) {
        this.properties = properties;
        return this;
    }

    /**
     * Get activities matching the specified labels.
     *
     * @memberof ActivitiesQuery
     * @instance
     * @param {string[]} labels - Labels list.
     * @return {ActivitiesQuery} same query.
     */
    withLabels(labels) {
        this.labels = labels;
        return this;
    }

    /**
     * Get activities matching the specified mentions.
     *
     * @memberof ActivitiesQuery
     * @instance
     * @param {UserId[]} mentions - User IDs.
     * @return {ActivitiesQuery} same query.
     */
    withMentions(mentions) {
        this.mentions = mentions;
        return this;
    }

    /**
     * Include a number of comments from each activity in the response.
     *
     * @memberof ActivitiesQuery
     * @instance
     * @param {number} upTo - Number of comments to include (max: 3)
     * @return {ActivitiesQuery} same query.
     */
    includeComments(upTo) {
        this.includedComments = upTo;
        return this;
    }
}