Home Reference Source

js/models/Request/FollowUser.js

import Request, { HTTPMethod } from '~/models/Request/Request';

/**
 * Follows **or unfollows** a user. This updates the state
 * @extends {Request}
 */
export default class FollowUser extends Request {
    /** @override */
    format(object) {
        this.user.isFollowing = object.following;
    }

    /**
     * **Requires** authorization
     * @param {User} user - The user to follow.
     * @param {Object} opts
     * @param {boolean} [opts.shouldFollow=true] - what the resulting follow status should be
     */
    constructor(user, { shouldFollow = true }) {
        const action = shouldFollow ? 'follow' : 'unfollow';

        super({
            path: `/user/${action}/${user.id}`,
            method: HTTPMethod.POST
        });

        /**
         * The user target of the request
         * @type {User}
         */
        this.user = user;
    }
}