Skip to content
This repository was archived by the owner on Oct 30, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
0509db3
add basic settings
yash-rajpal May 30, 2021
6ab7149
add i18n
yash-rajpal May 30, 2021
f497d9f
add settings update handler and fetch from url
yash-rajpal May 31, 2021
a61ee9e
refactoring getting blocked words
yash-rajpal May 31, 2021
b719c0e
removing allowed words from blocked list
yash-rajpal Jun 9, 2021
6c991dd
reading message and basic filtering
yash-rajpal Jun 9, 2021
7fa7806
refactoring PreMessageSentHandler
yash-rajpal Jun 17, 2021
2648f35
adding CheckPreMessageSent and channels check
yash-rajpal Jul 5, 2021
991fc10
add settings for Direct and LiveChat Messages
yash-rajpal Jul 5, 2021
a7b4faa
Check badwords for Edited Messages
yash-rajpal Jul 5, 2021
e4acd89
improving internationalisation
yash-rajpal Jul 5, 2021
6a544db
Attachment description filtering
yash-rajpal Jul 15, 2021
c7dd1ba
Refactoring Attachment desc filtering
yash-rajpal Jul 15, 2021
370e1bc
persist offending users
yash-rajpal Aug 9, 2021
8252b41
get stats of a room
yash-rajpal Aug 9, 2021
68ab102
notifying users of bad lang
yash-rajpal Aug 9, 2021
59dabdb
adding slashCommand
yash-rajpal Aug 9, 2021
d34367f
adding slash command to config
yash-rajpal Aug 9, 2021
d735fa4
refactoring send notify message
yash-rajpal Aug 10, 2021
2714670
adding functionality to slash command
yash-rajpal Aug 10, 2021
3caf89f
slashCommand minor fix
yash-rajpal Aug 10, 2021
41ebe73
add app icon
yash-rajpal Aug 24, 2021
4b90f90
add more settings for ban and notify message
yash-rajpal Aug 24, 2021
7161186
adding prevent message, banning users
yash-rajpal Aug 24, 2021
60b7c0b
adding ban user and clear records
yash-rajpal Aug 24, 2021
af8ab32
adding more functionalities to slash command
yash-rajpal Aug 24, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
172 changes: 168 additions & 4 deletions BadWordsApp.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,176 @@
import {
IAppAccessors,
IConfigurationExtend,
IConfigurationModify,
IEnvironmentRead,
IHttp,
ILogger,
} from '@rocket.chat/apps-engine/definition/accessors';
import { App } from '@rocket.chat/apps-engine/definition/App';
import { IAppInfo } from '@rocket.chat/apps-engine/definition/metadata';
IMessageBuilder,
IModify,
IPersistence,
IRead,
} from "@rocket.chat/apps-engine/definition/accessors";
import { App } from "@rocket.chat/apps-engine/definition/App";
import {
IMessage,
IPreMessageSentModify,
IPreMessageSentPrevent,
IPreMessageUpdatedModify,
} from "@rocket.chat/apps-engine/definition/messages";
import { IAppInfo } from "@rocket.chat/apps-engine/definition/metadata";
import { ISetting } from "@rocket.chat/apps-engine/definition/settings";
import { BadWordsCommand } from "./commands/BadWordsCommand";
import { Settings } from "./config/Settings";
import { CheckPreMessageSentHandler } from "./handlers/CheckPreMessageSentHandler";
import { OnSettingsUpdatedHandler } from "./handlers/OnSettingsUpdatedHandler";
import { PreMessageSentHandler } from "./handlers/PreMessageSentHandler";
import { getBlockedWords } from "./lib/Settings";
import { PreMessageSentPreventHandler } from "./handlers/PreMessageSentPreventHandler";
import {
IUIKitInteractionHandler,
IUIKitResponse,
UIKitBlockInteractionContext,
} from "@rocket.chat/apps-engine/definition/uikit";
import { BlockActionHandler } from "./handlers/BlockActionHandler";

export class BadWordsApp extends App {
export class BadWordsApp
extends App
implements
IPreMessageSentModify,
IPreMessageUpdatedModify,
IPreMessageSentPrevent,
IUIKitInteractionHandler
{
constructor(info: IAppInfo, logger: ILogger, accessors: IAppAccessors) {
super(info, logger, accessors);
}
public blockedWords: Array<string>;

async executePreMessageSentPrevent(
message: IMessage,
read: IRead
): Promise<boolean> {
const preMessageSentPreventHandler = new PreMessageSentPreventHandler(
message,
read
);
return preMessageSentPreventHandler.run();
}

async checkPreMessageSentModify(
message: IMessage,
read: IRead,
http: IHttp
): Promise<boolean> {
const checkPreMessageSentHandler = new CheckPreMessageSentHandler(
this,
message,
read,
http,
this.blockedWords
);
return checkPreMessageSentHandler.check();
}

async executePreMessageSentModify(
message: IMessage,
builder: IMessageBuilder,
read: IRead,
http: IHttp,
persist: IPersistence
): Promise<IMessage> {
const preMessageSentHandler = new PreMessageSentHandler(
this,
message,
builder,
read,
http,
persist,
this.blockedWords
);
return preMessageSentHandler.run();
}

async checkPreMessageUpdatedModify(
message: IMessage,
read: IRead,
http: IHttp
): Promise<boolean> {
const checkPreMessageUpdatedHandler = new CheckPreMessageSentHandler(
this,
message,
read,
http,
this.blockedWords
);
return checkPreMessageUpdatedHandler.check();
}

async executePreMessageUpdatedModify(
message: IMessage,
builder: IMessageBuilder,
read: IRead,
http: IHttp,
persist: IPersistence
): Promise<IMessage> {
const preMessageUpdatedHandler = new PreMessageSentHandler(
this,
message,
builder,
read,
http,
persist,
this.blockedWords
);
return preMessageUpdatedHandler.run();
}

async executeBlockActionHandler(
context: UIKitBlockInteractionContext,
read: IRead,
http: IHttp,
persist: IPersistence,
modify: IModify
): Promise<IUIKitResponse> {
const blockActionHandler = new BlockActionHandler(
context,
read,
persist
);
return blockActionHandler.run();
}

public async onSettingUpdated(
setting: ISetting,
configurationModify: IConfigurationModify,
read: IRead,
http: IHttp
): Promise<void> {
const settingsUpdated = new OnSettingsUpdatedHandler(
this,
setting,
configurationModify,
read,
http
);
this.blockedWords = await settingsUpdated.run();
}

protected async extendConfiguration(
configuration: IConfigurationExtend,
environmentRead: IEnvironmentRead
): Promise<void> {
await Promise.all(
Settings.map((setting) =>
configuration.settings.provideSetting(setting)
)
);
await configuration.slashCommands.provideSlashCommand(
new BadWordsCommand()
);
this.blockedWords = await getBlockedWords(
environmentRead,
this.getAccessors().http
);
}
}
10 changes: 8 additions & 2 deletions app.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "925a2e1e-d239-4ab8-a583-57203a8c3754",
"version": "0.0.1",
"requiredApiVersion": "^1.4.0",
"requiredApiVersion": "^1.19.0",
"iconFile": "icon.png",
"author": {
"name": "Rocket.Chat",
Expand All @@ -11,5 +11,11 @@
"name": "BadWords",
"nameSlug": "badwords",
"classFile": "BadWordsApp.ts",
"description": "A simple app to block badwords and apply moderation policies in channels"
"description": "A simple app to block badwords and apply moderation policies in channels",
"implements": [
"IPreMessageSentModify",
"IPreMessageUpdatedModify",
"IPreMessageSentPrevent",
"IUIKitInteractionHandler"
]
}
173 changes: 173 additions & 0 deletions commands/BadWordsCommand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
import {
IHttp,
IModify,
IPersistence,
IRead,
} from "@rocket.chat/apps-engine/definition/accessors";
import {
ISlashCommand,
SlashCommandContext,
} from "@rocket.chat/apps-engine/definition/slashcommands";
import { sendNotifyMessage } from "../lib/sendNotifyMessage";
import { showModal } from "../lib/showModal";
import { banUser, clearStats, unBanUser } from "../lib/storeStats";

export class BadWordsCommand implements ISlashCommand {
public command = "bad-words";
public i18nParamsExample = "example params";
public i18nDescription = "command description";
public providesPreview = false;

public async executor(
context: SlashCommandContext,
read: IRead,
modify: IModify,
http: IHttp,
persis: IPersistence
): Promise<void> {
const triggerId = context.getTriggerId();
const room = context.getRoom();
const sender = context.getSender();
const threadId = context.getThreadId();

if (sender.roles.includes("admin") || sender.roles.includes("owner")) {
const args = context.getArguments();
console.log("the args are = ", args);
switch (args[0]) {
case "stats": {
if (triggerId) {
const modal = await showModal(room, read, modify);
await modify
.getUiController()
.openModalView(modal, { triggerId }, sender);
} else {
sendNotifyMessage(
room,
sender,
threadId,
modify.getNotifier(),
"Something went wrong!!"
);
}
break;
}

case "clear": {
if (args[1]) {
const username = args[1];
const user = await read
.getUserReader()
.getByUsername(username);
if (user) {
clearStats(room.id, user.id, persis, read);
sendNotifyMessage(
room,
sender,
threadId,
modify.getNotifier(),
`*${user.username}'s* record has been cleared`
);
} else {
sendNotifyMessage(
room,
sender,
threadId,
modify.getNotifier(),
"User not found! Please enter a valid username."
);
}
} else {
sendNotifyMessage(
room,
sender,
threadId,
modify.getNotifier(),
"Please provide a username after clear keyword!"
);
}
break;
}

case "ban": {
if (args[1]) {
const username = args[1];
const user = await read
.getUserReader()
.getByUsername(username);
if (user) {
banUser(room.id, user.id, persis, read);
sendNotifyMessage(
room,
sender,
threadId,
modify.getNotifier(),
`*${user.username}* has been banned from this room!`
);
} else {
sendNotifyMessage(
room,
sender,
threadId,
modify.getNotifier(),
"User not found! Please enter a valid username."
);
}
} else {
sendNotifyMessage(
room,
sender,
threadId,
modify.getNotifier(),
"Please provide a username after ban keyword!"
);
}
break;
}

case "unban": {
if (args[1]) {
const username = args[1];
const user = await read
.getUserReader()
.getByUsername(username);
if (user) {
unBanUser(room.id, user.id, persis, read);
sendNotifyMessage(
room,
sender,
threadId,
modify.getNotifier(),
`*${user.username}* has been un-banned from this room!`
);
} else {
sendNotifyMessage(
room,
sender,
threadId,
modify.getNotifier(),
"User not found! Please enter a valid username."
);
}
} else {
sendNotifyMessage(
room,
sender,
threadId,
modify.getNotifier(),
"Please provide a username after unban keyword!"
);
}
break;
}
}
} else {
sendNotifyMessage(
room,
sender,
threadId,
modify.getNotifier(),
"This slash command can be only used by admins and owners"
);
}
}
}
Loading