summaryrefslogtreecommitdiff
path: root/packages/backend/src/server/api/endpoints/channels/mute/create.ts
blob: 26ce707c7a41f89ad3e30858575abb0a8c3d7425 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
 * SPDX-FileCopyrightText: syuilo and misskey-project
 * SPDX-License-Identifier: AGPL-3.0-only
 */

import { Inject, Injectable } from '@nestjs/common';
import { Endpoint } from '@/server/api/endpoint-base.js';
import type { ChannelsRepository } from '@/models/_.js';
import { DI } from '@/di-symbols.js';
import { ApiError } from '@/server/api/error.js';
import { ChannelMutingService } from '@/core/ChannelMutingService.js';

export const meta = {
	tags: ['channels', 'mute'],

	requireCredential: true,
	prohibitMoved: true,

	kind: 'write:channels',

	errors: {
		noSuchChannel: {
			message: 'No such Channel.',
			code: 'NO_SUCH_CHANNEL',
			id: '7174361e-d58f-31d6-2e7c-6fb830786a3f',
		},

		alreadyMuting: {
			message: 'You are already muting that user.',
			code: 'ALREADY_MUTING_CHANNEL',
			id: '5a251978-769a-da44-3e89-3931e43bb592',
		},

		expiresAtIsPast: {
			message: 'Cannot set past date to "expiresAt".',
			code: 'EXPIRES_AT_IS_PAST',
			id: '42b32236-df2c-a45f-fdbf-def67268f749',
		},
	},
} as const;

export const paramDef = {
	type: 'object',
	properties: {
		channelId: { type: 'string', format: 'misskey:id' },
		expiresAt: {
			type: 'integer',
			nullable: true,
			description: 'A Unix Epoch timestamp that must lie in the future. `null` means an indefinite mute.',
		},
	},
	required: ['channelId'],
} as const;

@Injectable()
export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-disable-line import/no-default-export
	constructor(
		@Inject(DI.channelsRepository)
		private channelsRepository: ChannelsRepository,
		private channelMutingService: ChannelMutingService,
	) {
		super(meta, paramDef, async (ps, me) => {
			// Check if exists the channel
			const targetChannel = await this.channelsRepository.findOneBy({ id: ps.channelId });
			if (!targetChannel) {
				throw new ApiError(meta.errors.noSuchChannel);
			}

			// Check if already muting
			const exist = await this.channelMutingService.isMuted({
				requestUserId: me.id,
				targetChannelId: targetChannel.id,
			});
			if (exist) {
				throw new ApiError(meta.errors.alreadyMuting);
			}

			// Check if expiresAt is past
			if (ps.expiresAt && ps.expiresAt <= Date.now()) {
				throw new ApiError(meta.errors.expiresAtIsPast);
			}

			await this.channelMutingService.mute({
				requestUserId: me.id,
				targetChannelId: targetChannel.id,
				expiresAt: ps.expiresAt ? new Date(ps.expiresAt) : null,
			});
		});
	}
}