2022-02-27 02:07:39 +00:00
|
|
|
import { genId } from '@/misc/gen-id.js';
|
|
|
|
import { Mutings, NoteWatchings } from '@/models/index.js';
|
|
|
|
import { Muting } from '@/models/entities/muting.js';
|
|
|
|
import { publishUserEvent } from '@/services/stream.js';
|
2023-02-19 20:47:46 +00:00
|
|
|
import define from '@/server/api/define.js';
|
|
|
|
import { ApiError } from '@/server/api/error.js';
|
|
|
|
import { getUser } from '@/server/api/common/getters.js';
|
2023-05-05 23:02:24 +00:00
|
|
|
import { createMute } from '@/services/mute/create.js';
|
2017-12-21 21:03:54 +00:00
|
|
|
|
2018-07-16 19:36:44 +00:00
|
|
|
export const meta = {
|
2020-04-03 13:42:29 +00:00
|
|
|
tags: ['account'],
|
2019-02-23 02:20:58 +00:00
|
|
|
|
2022-01-18 13:27:10 +00:00
|
|
|
requireCredential: true,
|
2018-07-16 19:36:44 +00:00
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
kind: 'write:mutes',
|
2018-11-01 18:32:24 +00:00
|
|
|
|
2022-10-20 21:04:24 +00:00
|
|
|
errors: ['NO_SUCH_USER', 'MUTEE_IS_YOURSELF', 'ALREADY_MUTING'],
|
2022-01-18 13:27:10 +00:00
|
|
|
} as const;
|
2018-07-16 19:36:44 +00:00
|
|
|
|
2022-02-20 04:15:40 +00:00
|
|
|
export const paramDef = {
|
2022-02-19 05:05:32 +00:00
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
userId: { type: 'string', format: 'misskey:id' },
|
2022-04-03 04:57:26 +00:00
|
|
|
expiresAt: {
|
|
|
|
type: 'integer',
|
|
|
|
nullable: true,
|
|
|
|
description: 'A Unix Epoch timestamp that must lie in the future. `null` means an indefinite mute.',
|
|
|
|
},
|
2022-02-19 05:05:32 +00:00
|
|
|
},
|
|
|
|
required: ['userId'],
|
|
|
|
} as const;
|
|
|
|
|
2022-01-02 17:12:50 +00:00
|
|
|
// eslint-disable-next-line import/no-default-export
|
2022-02-19 05:05:32 +00:00
|
|
|
export default define(meta, paramDef, async (ps, user) => {
|
2018-11-01 18:32:24 +00:00
|
|
|
const muter = user;
|
2017-12-21 21:03:54 +00:00
|
|
|
|
|
|
|
// 自分自身
|
2022-10-20 21:04:24 +00:00
|
|
|
if (user.id === ps.userId) throw new ApiError('MUTEE_IS_YOURSELF');
|
2017-12-21 21:03:54 +00:00
|
|
|
|
|
|
|
// Get mutee
|
2023-03-21 20:13:11 +00:00
|
|
|
const mutee = await getUser(ps.userId);
|
2017-12-21 21:03:54 +00:00
|
|
|
|
|
|
|
// Check if already muting
|
2023-01-03 01:42:42 +00:00
|
|
|
const exist = await Mutings.countBy({
|
2019-04-07 12:50:36 +00:00
|
|
|
muterId: muter.id,
|
2021-12-09 14:58:30 +00:00
|
|
|
muteeId: mutee.id,
|
2017-12-21 21:03:54 +00:00
|
|
|
});
|
|
|
|
|
2023-01-03 01:42:42 +00:00
|
|
|
if (exist) throw new ApiError('ALREADY_MUTING');
|
2017-12-21 21:03:54 +00:00
|
|
|
|
2022-03-04 11:23:53 +00:00
|
|
|
if (ps.expiresAt && ps.expiresAt <= Date.now()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-05-05 23:02:24 +00:00
|
|
|
const expiresAt = ps.expiresAt ? new Date(ps.expiresAt) : null;
|
2017-12-21 21:03:54 +00:00
|
|
|
|
2023-05-05 23:02:24 +00:00
|
|
|
await createMute(muter, mutee, expiresAt);
|
2019-02-22 02:46:58 +00:00
|
|
|
});
|