diff --git a/FilterApp.ts b/FilterApp.ts new file mode 100644 index 0000000..724c745 --- /dev/null +++ b/FilterApp.ts @@ -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 { + 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 { + if (setting.id !== 'filterList') { + return; + } + + this.parseConfig(setting.value); + } + + public async checkPreMessageSentModify(message: IMessage): Promise { + return typeof message.text === 'string'; + } + + public async executePreMessageSentModify(message: IMessage, builder: IMessageBuilder, read: IRead, http: IHttp, persistence: IPersistence): Promise { + 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 { + 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', + }); + } +} diff --git a/README.md b/README.md index efe6426..04e9021 100644 --- a/README.md +++ b/README.md @@ -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) \ No newline at end of file diff --git a/app.json b/app.json new file mode 100644 index 0000000..a798b27 --- /dev/null +++ b/app.json @@ -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" +} diff --git a/i18n/en.json b/i18n/en.json new file mode 100644 index 0000000..9d64a9e --- /dev/null +++ b/i18n/en.json @@ -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." +} diff --git a/icon.jpg b/icon.jpg new file mode 100644 index 0000000..e54989e Binary files /dev/null and b/icon.jpg differ diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..7873450 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,18 @@ +{ + "requires": true, + "lockfileVersion": 1, + "dependencies": { + "@rocket.chat/apps-ts-definition": { + "version": "0.9.13", + "resolved": "https://registry.npmjs.org/@rocket.chat/apps-ts-definition/-/apps-ts-definition-0.9.13.tgz", + "integrity": "sha512-2fqbJG8TPq19o2uZCE3UqRpNobAfAcgu0WkM8PAmhf1de459Z2gbmzbCBAsQJq6eOrGkqDry0GTzEneiCEvwZg==", + "dev": true + }, + "typescript": { + "version": "2.9.2", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-2.9.2.tgz", + "integrity": "sha512-Gr4p6nFNaoufRIY4NMdpQRNmgxVIGMs4Fcu/ujdYk3nAZqk7supzBE9idmvfZIlH/Cuj//dvi+019qEue9lV0w==", + "dev": true + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..cb26d55 --- /dev/null +++ b/package.json @@ -0,0 +1,6 @@ +{ + "devDependencies": { + "@rocket.chat/apps-ts-definition": "^0.9.13", + "typescript": "^2.9.1" + } +} diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..52be1be --- /dev/null +++ b/tsconfig.json @@ -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" + ] + } + \ No newline at end of file