Source

classes/modQueueItem.ts

import { ModQueueState } from "@prisma/client";
import {
    DBModQueue, DBModQueueAggregated, DBModQueueFull, DBModQueueReporter, DBModQueueUser
} from "../../prisma";
import { ModQueueItemType } from "../types/modQueueItem";
import Animation, { BasicAnimation } from "./animation";
import Comment from "./comment";
import File from "./file";
import Message from "./message";
import { RequestContext, RequestContextWithClient } from "./requestContext";
import Snowflake from "./snowflake";
import User from "./user";

/**
 * Handles data about a single mod queue item.
 */
class ModQueueItem {
    id?: Snowflake;
    type: ModQueueItemType;
    reporter?: User;
    count?: number;

    reason: string;
    state?: ModQueueState;

    user: User;
    animation?: BasicAnimation;
    message?: Message;
    comment?: Comment;
    file?: File;

    /**
     * Creates mod queue item instance
     * @param {object} param0
     */
    constructor({
        id,
        type,
        reporter,
        count,
        reason,
        user,
        animation,
        message,
        comment,
        file,
        state
    }: {
        id?: Snowflake,
        type: ModQueueItemType,
        reporter?: User,
        count?: number;
        reason: string,
        user: User,
        animation?: BasicAnimation,
        message?: Message,
        comment?: Comment,
        file?: File,
        state?: ModQueueState
    }) {
        this.id = id;
        this.type = type;
        this.reporter = reporter;
        this.reason = reason;
        this.user = user;
        this.count = count;
        this.animation = animation;
        this.message = message;
        this.comment = comment;
        this.file = file;
        this.state = state;
    }

    /**
     * @param {DBModQueueFull | DBModQueueReporter | DBModQueueUser | DBModQueue} data
     * @param {RequestContext} ctx
     * @return {Promise<ModQueueItem>}
     */
    static async fromDatabase(data: DBModQueueFull | DBModQueueReporter |
    DBModQueueUser | DBModQueue, ctx: RequestContext): Promise<ModQueueItem> {
        return new ModQueueItem({
            id: new Snowflake(data.snowflake),
            reason: data.reason,
            type: data.type,
            user: "user" in data ?
                User.fromDatabase(data.user, ctx) :
                await User.getUser(data.userId, ctx),
            reporter: "reporter" in data ?
                User.fromDatabase(data.reporter, ctx) :
                await User.getUser(data.userId, ctx),
            state: data.state,

            ...await this.getItemByType(data.resourceId, data.type, ctx)
        });
    }

    /**
     * @param {DBModQueueAggregated} data
     * @param {RequestContext} ctx
     * @return {Promise<ModQueueItem>}
     */
    static async fromAggregated(data: DBModQueueAggregated, ctx: RequestContext):
    Promise<ModQueueItem> {
        return new ModQueueItem({
            reason: data.reason,
            type: data.type,
            user: await User.getUser(data.userId, ctx),
            count: data.count,

            ...await this.getItemByType(data.resourceId, data.type, ctx)
        });
    }

    /**
     *
     * @param {bigint} id
     * @param {ModQueueItemType} type
     * @param {RequestContext} ctx
     */
    private static async getItemByType(id: bigint, type: ModQueueItemType, ctx: RequestContext) {
        var resource: {
            animation?: BasicAnimation,
            comment?: Comment,
            message?: Message,
            file?: File
        } = {};
        switch(type) {
            case ModQueueItemType.ANIMATION:
                resource.animation =
                    await Animation.getAnimation(id, ctx);
                break;
            case ModQueueItemType.ANIMATION_COMMENT:
                resource.comment =
                    await Comment.getComment(id, ctx);
                resource.animation =
                    resource.comment.animation;
                break;
            case ModQueueItemType.USER_MESSAGE:
                resource.message =
                    await Message.getMessage(id, ctx as RequestContextWithClient);
                break;
            case ModQueueItemType.USER:
                // already handled
                break;
            case ModQueueItemType.FILE:
                resource.file =
                    await File.load(id, ctx);
                break;
        }

        return resource;
    }

    /**
     * Converts the class into a JSON compatible object
     * @return {object}
     */
    toJSON() {
        return {
            id: this.id?.toString(),
            time: this.id?.time,
            type: this.type,
            reporter: this.reporter?.toJSON(),
            reason: this.reason,
            count: this.count,

            user: this.user?.toJSON(),
            animation: this.animation?.toJSON(),
            message: this.message?.toJSON(),
            comment: this.comment?.toJSON(),
            file: this.file?.toJSON()
        };
    }
};

export default ModQueueItem;