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