Skip to content

Commit

Permalink
Publish
Browse files Browse the repository at this point in the history
  • Loading branch information
vynmera committed Jul 6, 2018
1 parent 621f861 commit cabb7b3
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 0 deletions.
70 changes: 70 additions & 0 deletions FilterApp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { IConfigurationExtend, IConfigurationModify, IEnvironmentRead, IHttp, ILogger, IMessageBuilder, IMessageExtender, IPersistence, IRead } from '@rocket.chat/apps-ts-definition/accessors';
import { App } from '@rocket.chat/apps-ts-definition/App';
import { IMessage, IMessageAttachment, IPreMessageSentModify } from '@rocket.chat/apps-ts-definition/messages';
import { IAppInfo } from '@rocket.chat/apps-ts-definition/metadata';
import { SettingType, ISetting } from '@rocket.chat/apps-ts-definition/settings';

export class FilterApp extends App implements IPreMessageSentModify {
private filterList;

constructor(info: IAppInfo, logger: ILogger) {
super(info, logger);
this.filterList = [];
}

private parseConfig(text) {
let newFilterList = [];

try {
newFilterList = JSON.parse(text);
this.filterList = newFilterList;

return true;
} catch (e) {
return false;
}
}

public async onEnable(environment: IEnvironmentRead, configurationModify: IConfigurationModify): Promise<boolean> {
const metaFilterList = await environment.getSettings().getValueById('filterList') as string;

return this.parseConfig(metaFilterList);
}

public async onSettingUpdated(setting: ISetting, configurationModify: IConfigurationModify, read: IRead, http: IHttp): Promise<void> {
if (setting.id !== 'filterList') {
return;
}

this.parseConfig(setting.value);
}

public async checkPreMessageSentModify(message: IMessage): Promise<boolean> {
return typeof message.text === 'string';
}

public async executePreMessageSentModify(message: IMessage, builder: IMessageBuilder, read: IRead, http: IHttp, persistence: IPersistence): Promise<IMessage> {
let text = message.text || '';

Object.keys(this.filterList).forEach((key) => {
const filter = this.filterList[key] || {};

text = text.replace(new RegExp(filter.regex || '', filter.flags || 'gi'), filter.replacement || '');
});

return builder.setText(text).getMessage();
}

protected async extendConfiguration(configuration: IConfigurationExtend, environmentRead: IEnvironmentRead): Promise<void> {
await configuration.settings.provideSetting({
id: 'filterList',
type: SettingType.STRING,
packageValue: '[\n {\n "regex": "rocketchat",\n "flags": "gi",\n "replacement": "Rocket.Chat"\n }\n]',
required: false,
public: false,
multiline: true,
i18nLabel: 'FilterApp_FilterList',
i18nDescription: 'FilterApp_FilterList_Description',
});
}
}
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
# Rocket.Chat.WordFilter
Replaces all occurences of words in messages with other words.

Simply pop in the App, and tweak the filter list to your liking! It uses regular expressions to allow for further customization.

![Logo](/icon.jpg)
15 changes: 15 additions & 0 deletions app.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"id": "28186184-a2eb-4359-9c6f-b20086141085",
"name": "Word Filter",
"nameSlug": "word-filter",
"description": "Replaces all occurences of words in messages with other words.",
"version": "0.0.1",
"requiredApiVersion": "^0.9.13",
"author": {
"name": "vynmera",
"homepage": "https://github.com/vynmera/Rocket.Chat.WordFilter",
"support": "https://github.com/vynmera/Rocket.Chat.WordFilter/issues"
},
"classFile": "FilterApp.ts",
"iconFile": "icon.jpg"
}
4 changes: 4 additions & 0 deletions i18n/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"FilterApp_FilterList": "Filter List",
"FilterApp_FilterList_Description": "This should be an array of objects with keys `regex` containing a regex (without slashes), `flags` containing regex flags, and `replacement` containing the new content."
}
Binary file added icon.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"devDependencies": {
"@rocket.chat/apps-ts-definition": "^0.9.13",
"typescript": "^2.9.1"
}
}
18 changes: 18 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"compilerOptions": {
"target": "es2017",
"module": "commonjs",
"moduleResolution": "node",
"declaration": false,
"noImplicitAny": false,
"removeComments": true,
"strictNullChecks": true,
"noImplicitReturns": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
},
"include": [
"**/*.ts"
]
}

0 comments on commit cabb7b3

Please sign in to comment.