-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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." | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} | ||
|