Skip to content
Snippets Groups Projects
per-user-notes.ts 1.27 KiB
Newer Older
import autobind from 'autobind-decorator';
syuilo's avatar
syuilo committed
import Chart, { KVs } from '../core';
import { User } from '@/models/entities/user';
import { Notes } from '@/models/index';
import { Note } from '@/models/entities/note';
import { name, schema } from './entities/per-user-notes';
/**
 * ユーザーごとのノートに関するチャート
 */
// eslint-disable-next-line import/no-default-export
syuilo's avatar
syuilo committed
export default class PerUserNotesChart extends Chart<typeof schema> {
	constructor() {
		super(name, schema, true);
	}

	@autobind
syuilo's avatar
syuilo committed
	protected async queryCurrentState(group: string): Promise<Partial<KVs<typeof schema>>> {
		const [count] = await Promise.all([
			Notes.count({ userId: group }),
		]);

		return {
			total: count,
		};
	}

	@autobind
	public async update(user: { id: User['id'] }, note: Note, isAdditional: boolean): Promise<void> {
syuilo's avatar
syuilo committed
		await this.commit({
			'total': isAdditional ? 1 : -1,
			'inc': isAdditional ? 1 : 0,
			'dec': isAdditional ? 0 : 1,
			'diffs.normal': note.replyId == null && note.renoteId == null ? (isAdditional ? 1 : -1) : 0,
			'diffs.renote': note.renoteId != null ? (isAdditional ? 1 : -1) : 0,
			'diffs.reply': note.replyId != null ? (isAdditional ? 1 : -1) : 0,
			'diffs.withFile': note.fileIds.length > 0 ? (isAdditional ? 1 : -1) : 0,
syuilo's avatar
syuilo committed
		}, user.id);