1
0
mirror of https://github.com/spacebarchat/server.git synced 2024-11-25 11:43:07 +01:00

Implement CRUD for automod

This commit is contained in:
Emma [it/its]@Rory& 2024-10-15 11:10:58 +02:00
parent a56ded91d5
commit a4a70cf89b
17 changed files with 38561 additions and 3 deletions

5
.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/

View File

@ -0,0 +1,62 @@
<component name="ProjectCodeStyleConfiguration">
<code_scheme name="Project" version="173">
<option name="OTHER_INDENT_OPTIONS">
<value>
<option name="INDENT_SIZE" value="2" />
<option name="TAB_SIZE" value="2" />
</value>
</option>
<option name="RIGHT_MARGIN" value="180" />
<HTMLCodeStyleSettings>
<option name="HTML_SPACE_INSIDE_EMPTY_TAG" value="true" />
<option name="HTML_DO_NOT_INDENT_CHILDREN_OF" value="" />
</HTMLCodeStyleSettings>
<JSCodeStyleSettings version="0">
<option name="FORCE_SEMICOLON_STYLE" value="true" />
<option name="SPACE_BEFORE_FUNCTION_LEFT_PARENTH" value="false" />
<option name="FORCE_QUOTE_STYlE" value="true" />
<option name="ENFORCE_TRAILING_COMMA" value="WhenMultiline" />
<option name="SPACES_WITHIN_OBJECT_LITERAL_BRACES" value="true" />
<option name="SPACES_WITHIN_IMPORTS" value="true" />
</JSCodeStyleSettings>
<TypeScriptCodeStyleSettings version="0">
<option name="FORCE_SEMICOLON_STYLE" value="true" />
<option name="SPACE_BEFORE_FUNCTION_LEFT_PARENTH" value="false" />
<option name="FORCE_QUOTE_STYlE" value="true" />
<option name="ENFORCE_TRAILING_COMMA" value="WhenMultiline" />
<option name="SPACES_WITHIN_OBJECT_LITERAL_BRACES" value="true" />
<option name="SPACES_WITHIN_IMPORTS" value="true" />
</TypeScriptCodeStyleSettings>
<VueCodeStyleSettings>
<option name="INTERPOLATION_NEW_LINE_AFTER_START_DELIMITER" value="false" />
<option name="INTERPOLATION_NEW_LINE_BEFORE_END_DELIMITER" value="false" />
</VueCodeStyleSettings>
<codeStyleSettings language="HTML">
<option name="SOFT_MARGINS" value="80" />
<indentOptions>
<option name="CONTINUATION_INDENT_SIZE" value="4" />
<option name="USE_TAB_CHARACTER" value="true" />
</indentOptions>
</codeStyleSettings>
<codeStyleSettings language="JavaScript">
<option name="SOFT_MARGINS" value="80" />
<indentOptions>
<option name="USE_TAB_CHARACTER" value="true" />
</indentOptions>
</codeStyleSettings>
<codeStyleSettings language="TypeScript">
<option name="SOFT_MARGINS" value="80" />
<indentOptions>
<option name="USE_TAB_CHARACTER" value="true" />
</indentOptions>
</codeStyleSettings>
<codeStyleSettings language="Vue">
<option name="SOFT_MARGINS" value="80" />
<indentOptions>
<option name="INDENT_SIZE" value="4" />
<option name="TAB_SIZE" value="4" />
<option name="USE_TAB_CHARACTER" value="true" />
</indentOptions>
</codeStyleSettings>
</code_scheme>
</component>

View File

@ -0,0 +1,5 @@
<component name="ProjectCodeStyleConfiguration">
<state>
<option name="USE_PER_PROJECT_SETTINGS" value="true" />
</state>
</component>

4
.idea/encodings.xml Normal file
View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding" addBOMForNewFiles="with BOM under Windows, with no BOM otherwise" />
</project>

View File

@ -0,0 +1,6 @@
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="Eslint" enabled="true" level="WARNING" enabled_by_default="true" />
</profile>
</component>

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="JavaScriptLibraryMappings">
<includedPredefinedLibrary name="Node.js Core" />
</component>
</project>

8
.idea/modules.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/server.iml" filepath="$PROJECT_DIR$/.idea/server.iml" />
</modules>
</component>
</project>

6
.idea/prettier.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="PrettierConfiguration">
<option name="myConfigurationMode" value="AUTOMATIC" />
</component>
</project>

12
.idea/server.iml Normal file
View File

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/.tmp" />
<excludeFolder url="file://$MODULE_DIR$/temp" />
<excludeFolder url="file://$MODULE_DIR$/tmp" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,147 @@
/*
Spacebar: A FOSS re-implementation and extension of the Discord.com backend.
Copyright (C) 2024 Spacebar and Spacebar Contributors
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { route } from "@spacebar/api";
import { User, AutomodRuleSchema, AutomodRule } from "@spacebar/util";
import { Request, Response, Router } from "express";
import { HTTPError } from "lambert-server";
const router: Router = Router();
router.get(
"/",
route({
permission: ["MANAGE_GUILD"],
responses: {
200: {
body: "AutomodRuleSchemaWithId[]",
},
403: {
body: "APIErrorResponse",
},
},
}),
async (req: Request, res: Response) => {
const { guild_id } = req.params;
const rules = await AutomodRule.find({ where: { guild_id } });
return res.json(rules);
},
);
router.post(
"/",
route({
// requestBody: "AutomodRuleSchema",
permission: ["MANAGE_GUILD"],
responses: {
200: {
body: "AutomodRuleSchemaWithId",
},
400: {
body: "APIErrorResponse",
},
403: {
body: "APIErrorResponse",
},
},
}),
async (req: Request, res: Response) => {
const { guild_id } = req.params;
if (req.user_id !== req.body.creator_id)
throw new HTTPError(
"You can't create a rule for someone else",
403,
);
if (guild_id !== req.body.guild_id)
throw new HTTPError(
"You can't create a rule for another guild",
403,
);
if (req.body.id) {
throw new HTTPError("You can't specify an ID for a new rule", 400);
}
const data = req.body as AutomodRuleSchema;
const created = AutomodRule.create({
creator: await User.findOneOrFail({
where: { id: data.creator_id },
}),
...data,
});
const savedRule = await AutomodRule.save(created);
return res.json(savedRule);
},
);
router.patch(
"/:rule_id",
route({
// requestBody: "AutomodRuleSchema
permission: ["MANAGE_GUILD"],
responses: {
200: {
body: "AutomodRuleSchemaWithId",
},
400: {
body: "APIErrorResponse",
},
403: {
body: "APIErrorResponse",
},
},
}),
async (req: Request, res: Response) => {
const { rule_id } = req.params;
const rule = await AutomodRule.findOneOrFail({
where: { id: rule_id },
});
const data = req.body as AutomodRuleSchema;
AutomodRule.merge(rule, data);
const savedRule = await AutomodRule.save(rule);
return res.json(savedRule);
},
);
router.delete(
"/:rule_id",
route({
permission: ["MANAGE_GUILD"],
responses: {
204: {},
403: {
body: "APIErrorResponse",
},
404: {
body: "APIErrorResponse",
},
},
}),
async (req: Request, res: Response) => {
const { rule_id } = req.params;
await AutomodRule.delete({ id: rule_id });
return res.status(204).send();
},
);
export default router;

View File

@ -0,0 +1,88 @@
/*
Spacebar: A FOSS re-implementation and extension of the Discord.com backend.
Copyright (C) 2024 Spacebar and Spacebar Contributors
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { dbEngine, User } from "@spacebar/util";
import { BaseClass } from "./BaseClass";
import { Entity, JoinColumn, ManyToOne, Column } from "typeorm";
export class AutomodMentionSpamRule {
mention_total_limit: number;
mention_raid_protection_enabled: boolean;
}
export class AutomodSuspectedSpamRule {}
export class AutomodCommonlyFlaggedWordsRule {
allow_list: [string];
presets: [number];
}
export class AutomodCustomWordsRule {
allow_list: [string];
keyword_filter: [string];
regex_patterns: [string];
}
@Entity({
name: "automod_rules",
engine: dbEngine,
})
export class AutomodRule extends BaseClass {
@JoinColumn({ name: "creator_id" })
@ManyToOne(() => User, { onDelete: "CASCADE" })
creator: User;
@Column()
enabled: boolean;
@Column()
event_type: number; // No idea...
@Column({ type: "simple-array" })
exempt_channels: [string];
@Column({ type: "simple-array" })
exempt_roles: [string];
@Column()
guild_id: string;
@Column()
name: string;
@Column()
position: number;
@Column()
trigger_type: number;
@Column({
type: "simple-json",
nullable: true,
})
trigger_metadata?: // this is null for "Block suspected spam content"
| AutomodMentionSpamRule
| AutomodSuspectedSpamRule
| AutomodCommonlyFlaggedWordsRule
| AutomodCustomWordsRule;
@Column({
type: "simple-json",
})
actions: { type: number; metadata: unknown }[];
}

View File

@ -1,6 +1,6 @@
/* /*
Spacebar: A FOSS re-implementation and extension of the Discord.com backend. Spacebar: A FOSS re-implementation and extension of the Discord.com backend.
Copyright (C) 2023 Spacebar and Spacebar Contributors Copyright (C) 2024 Spacebar and Spacebar Contributors
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published it under the terms of the GNU Affero General Public License as published
@ -19,6 +19,7 @@
export * from "./Application"; export * from "./Application";
export * from "./Attachment"; export * from "./Attachment";
export * from "./AuditLog"; export * from "./AuditLog";
export * from "./AutomodRule";
export * from "./BackupCodes"; export * from "./BackupCodes";
export * from "./Badge"; export * from "./Badge";
export * from "./Ban"; export * from "./Ban";

View File

@ -0,0 +1,56 @@
/*
Spacebar: A FOSS re-implementation and extension of the Discord.com backend.
Copyright (C) 2024 Spacebar and Spacebar Contributors
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
export interface AutomodMentionSpamRuleSchema {
mention_total_limit: number;
mention_raid_protection_enabled: boolean;
}
export interface AutomodSuspectedSpamRuleSchema {}
export interface AutomodCommonlyFlaggedWordsRuleSchema {
allow_list: [string];
presets: [number];
}
export interface AutomodCustomWordsRuleSchema {
allow_list: [string];
keyword_filter: [string];
regex_patterns: [string];
}
export interface AutomodRuleSchema {
creator_id: string;
enabled: boolean;
event_type: number; // No idea...
exempt_channels: [string];
exempt_roles: [string];
guild_id: string;
name: string;
position: number;
trigger_type: number; //AutomodTriggerTypes
trigger_metadata:
| AutomodMentionSpamRuleSchema
| AutomodSuspectedSpamRuleSchema
| AutomodCommonlyFlaggedWordsRuleSchema
| AutomodCustomWordsRuleSchema;
}
export interface AutomodRuleSchemaWithId extends AutomodRuleSchema {
id: string;
}

View File

@ -1,6 +1,6 @@
/* /*
Spacebar: A FOSS re-implementation and extension of the Discord.com backend. Spacebar: A FOSS re-implementation and extension of the Discord.com backend.
Copyright (C) 2023 Spacebar and Spacebar Contributors Copyright (C) 2024 Spacebar and Spacebar Contributors
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published it under the terms of the GNU Affero General Public License as published
@ -21,6 +21,7 @@ export * from "./ActivitySchema";
export * from "./ApplicationAuthorizeSchema"; export * from "./ApplicationAuthorizeSchema";
export * from "./ApplicationCreateSchema"; export * from "./ApplicationCreateSchema";
export * from "./ApplicationModifySchema"; export * from "./ApplicationModifySchema";
export * from "./AutomodRuleSchema";
export * from "./BackupCodesChallengeSchema"; export * from "./BackupCodesChallengeSchema";
export * from "./BanCreateSchema"; export * from "./BanCreateSchema";
export * from "./BanModeratorSchema"; export * from "./BanModeratorSchema";

View File

@ -1,6 +1,6 @@
/* /*
Spacebar: A FOSS re-implementation and extension of the Discord.com backend. Spacebar: A FOSS re-implementation and extension of the Discord.com backend.
Copyright (C) 2023 Spacebar and Spacebar Contributors Copyright (C) 2024 Spacebar and Spacebar Contributors
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published it under the terms of the GNU Affero General Public License as published
@ -353,6 +353,20 @@ export const ClientApplicationAssetTypes = {
BIG: 2, BIG: 2,
}; };
export const AutomodActionTypes = {
BLOCK_MESSAGE: 1,
SEND_ALERT: 2,
TIMEOUT_MEMBER: 3,
};
export const AutomodTriggerTypes = {
CUSTOM_WORDS: 1,
UNKNOWN_2: 2,
SUSPECTED_SPAM_CONTENT: 3,
COMMONLY_FLAGGED_WORDS: 4,
MENTION_SPAM: 5,
};
export const Colors = { export const Colors = {
DEFAULT: 0x000000, DEFAULT: 0x000000,
WHITE: 0xffffff, WHITE: 0xffffff,