2021-11-12 12:11:15 +00:00
|
|
|
import * as sanitizeHtml from 'sanitize-html';
|
2022-02-27 02:07:39 +00:00
|
|
|
import define from '../../define.js';
|
|
|
|
import { publishAdminStream } from '@/services/stream.js';
|
|
|
|
import { ApiError } from '../../error.js';
|
|
|
|
import { getUser } from '../../common/getters.js';
|
|
|
|
import { AbuseUserReports, Users } from '@/models/index.js';
|
|
|
|
import { genId } from '@/misc/gen-id.js';
|
|
|
|
import { sendEmail } from '@/services/send-email.js';
|
|
|
|
import { fetchMeta } from '@/misc/fetch-meta.js';
|
2019-01-19 10:16:48 +00:00
|
|
|
|
|
|
|
export const meta = {
|
2019-02-23 02:20:58 +00:00
|
|
|
tags: ['users'],
|
|
|
|
|
2022-01-18 13:27:10 +00:00
|
|
|
requireCredential: true,
|
2019-01-19 10:16:48 +00:00
|
|
|
|
2019-02-22 02:46:58 +00:00
|
|
|
errors: {
|
|
|
|
noSuchUser: {
|
|
|
|
message: 'No such user.',
|
|
|
|
code: 'NO_SUCH_USER',
|
2021-11-12 12:11:15 +00:00
|
|
|
id: '1acefcb5-0959-43fd-9685-b48305736cb5',
|
2019-02-22 02:46:58 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
cannotReportYourself: {
|
|
|
|
message: 'Cannot report yourself.',
|
|
|
|
code: 'CANNOT_REPORT_YOURSELF',
|
2021-11-12 12:11:15 +00:00
|
|
|
id: '1e13149e-b1e8-43cf-902e-c01dbfcb202f',
|
2019-02-22 02:46:58 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
cannotReportAdmin: {
|
|
|
|
message: 'Cannot report the admin.',
|
|
|
|
code: 'CANNOT_REPORT_THE_ADMIN',
|
2021-11-12 12:11:15 +00:00
|
|
|
id: '35e166f5-05fb-4f87-a2d5-adb42676d48f',
|
|
|
|
},
|
|
|
|
},
|
2022-01-18 13:27:10 +00:00
|
|
|
} as const;
|
2019-01-19 10:16:48 +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' },
|
|
|
|
comment: { type: 'string', minLength: 1, maxLength: 2048 },
|
|
|
|
},
|
|
|
|
required: ['userId', 'comment'],
|
|
|
|
} as const;
|
|
|
|
|
2021-11-12 12:11:15 +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, me) => {
|
2019-01-19 10:16:48 +00:00
|
|
|
// Lookup user
|
2019-02-22 05:02:56 +00:00
|
|
|
const user = await getUser(ps.userId).catch(e => {
|
|
|
|
if (e.id === '15348ddd-432d-49c2-8a5a-8069753becff') throw new ApiError(meta.errors.noSuchUser);
|
|
|
|
throw e;
|
2019-01-19 10:16:48 +00:00
|
|
|
});
|
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
if (user.id === me.id) {
|
2019-02-22 02:46:58 +00:00
|
|
|
throw new ApiError(meta.errors.cannotReportYourself);
|
2019-01-19 10:16:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (user.isAdmin) {
|
2019-02-22 02:46:58 +00:00
|
|
|
throw new ApiError(meta.errors.cannotReportAdmin);
|
2019-01-19 10:16:48 +00:00
|
|
|
}
|
|
|
|
|
2022-01-02 17:20:30 +00:00
|
|
|
const report = await AbuseUserReports.insert({
|
2019-04-07 12:50:36 +00:00
|
|
|
id: genId(),
|
2019-01-19 10:16:48 +00:00
|
|
|
createdAt: new Date(),
|
2020-10-19 10:29:04 +00:00
|
|
|
targetUserId: user.id,
|
|
|
|
targetUserHost: user.host,
|
2019-04-07 12:50:36 +00:00
|
|
|
reporterId: me.id,
|
2020-10-19 10:29:04 +00:00
|
|
|
reporterHost: null,
|
|
|
|
comment: ps.comment,
|
2022-01-02 17:20:30 +00:00
|
|
|
}).then(x => AbuseUserReports.findOneOrFail(x.identifiers[0]));
|
2019-01-19 10:16:48 +00:00
|
|
|
|
2019-01-27 05:55:02 +00:00
|
|
|
// Publish event to moderators
|
2022-03-25 04:11:52 +00:00
|
|
|
setImmediate(async () => {
|
2019-04-07 12:50:36 +00:00
|
|
|
const moderators = await Users.find({
|
|
|
|
where: [{
|
2021-11-12 12:11:15 +00:00
|
|
|
isAdmin: true,
|
2019-01-27 05:55:02 +00:00
|
|
|
}, {
|
2021-11-12 12:11:15 +00:00
|
|
|
isModerator: true,
|
|
|
|
}],
|
2019-01-27 05:55:02 +00:00
|
|
|
});
|
2019-02-22 02:46:58 +00:00
|
|
|
|
2019-01-27 05:55:02 +00:00
|
|
|
for (const moderator of moderators) {
|
2019-04-07 12:50:36 +00:00
|
|
|
publishAdminStream(moderator.id, 'newAbuseUserReport', {
|
|
|
|
id: report.id,
|
2020-10-19 10:29:04 +00:00
|
|
|
targetUserId: report.targetUserId,
|
2019-01-27 05:55:02 +00:00
|
|
|
reporterId: report.reporterId,
|
2021-11-12 12:11:15 +00:00
|
|
|
comment: report.comment,
|
2019-01-27 05:55:02 +00:00
|
|
|
});
|
|
|
|
}
|
2021-11-12 12:11:15 +00:00
|
|
|
|
|
|
|
const meta = await fetchMeta();
|
|
|
|
if (meta.email) {
|
|
|
|
sendEmail(meta.email, 'New abuse report',
|
|
|
|
sanitizeHtml(ps.comment),
|
|
|
|
sanitizeHtml(ps.comment));
|
|
|
|
}
|
2022-03-25 04:11:52 +00:00
|
|
|
});
|
2019-02-22 02:46:58 +00:00
|
|
|
});
|