-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPhotoDnaCsemScanningApp.ts
230 lines (215 loc) · 10.1 KB
/
PhotoDnaCsemScanningApp.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import {
IAppAccessors,
ILogger,
IConfigurationExtend,
IEnvironmentRead,
IRead,
IHttp,
IPersistence,
IMessageBuilder,
IConfigurationModify,
} from '@rocket.chat/apps-engine/definition/accessors';
import { App } from '@rocket.chat/apps-engine/definition/App';
import { IMessage, IPostMessageSent, IPreMessageSentPrevent, IPreMessageSentModify } from '@rocket.chat/apps-engine/definition/messages';
import { IAppInfo } from '@rocket.chat/apps-engine/definition/metadata';
import { SettingType, ISetting } from '@rocket.chat/apps-engine/definition/settings';
import { PhotoDNACloudService } from './helper/PhotoDNACloudService';
import { IMatchResult } from './helper/IMatchResult';
import { IRoom } from '@rocket.chat/apps-engine/definition/rooms/IRoom';
import { SETTING_PHOTODNA_API_KEY, SETTING_QUARANTINE_CHANNEL, SETTING_LIMIT_ANALYSIS_TO_CHANNELS, SETTING_WATCH_DMS, SETTING_NCMEC_USER, SETTING_NCMEC_PASSWORD, SETTING_ENABLE_AUTOMATED_REPORT, SETTING_NCMEC_ORGNAME, SETTING_NCMEC_REPORTER_NAME, SETTING_NCMEC_REPORTER_EMAIL, SETTING_NCMEC_ENABLE_TEST_MODE } from './Settings';
export class PhotoDnaCsemScanningApp extends App implements IPreMessageSentModify {
private photoDnaService: PhotoDNACloudService;
private quarantineChannel: string;
private enableAutomatedReport: boolean;
private watchedRoomsId: Set<string> | undefined;
private watchDMs: boolean;
constructor(info: IAppInfo, logger: ILogger, accessors: IAppAccessors) {
super(info, logger, accessors);
this.photoDnaService = new PhotoDNACloudService();
}
protected async extendConfiguration(configuration: IConfigurationExtend, environmentRead: IEnvironmentRead): Promise<void> {
await configuration.settings.provideSetting({
id: SETTING_PHOTODNA_API_KEY,
type: SettingType.STRING,
packageValue: '',
required: true,
public: false,
i18nLabel: 'CSEM_Api_Key_Label',
i18nDescription: 'CSEM_Api_Key_Description',
});
await configuration.settings.provideSetting({
id: SETTING_QUARANTINE_CHANNEL,
type: SettingType.STRING,
packageValue: 'csem-quarantine',
required: true,
public: false,
i18nLabel: 'CSEM_Quarantine_Target_Channel_Label',
i18nDescription: 'CSEM_Quarantine_Target_Channel_Description',
});
await configuration.settings.provideSetting({
id: SETTING_LIMIT_ANALYSIS_TO_CHANNELS,
type: SettingType.STRING,
packageValue: '',
required: true,
public: false,
i18nLabel: 'CSEM_Limit_Analysis_To_Channels_Csv_Label',
i18nDescription: 'CSEM_Limit_Analysis_To_Channels_Csv_Description',
});
await configuration.settings.provideSetting({
id: SETTING_WATCH_DMS,
type: SettingType.BOOLEAN,
packageValue: false,
required: true,
public: false,
i18nLabel: 'CSEM_Watch_DMs_Label',
i18nDescription: 'CSEM_Watch_DMs_Description',
});
await configuration.settings.provideSetting({
id: SETTING_ENABLE_AUTOMATED_REPORT,
type: SettingType.BOOLEAN,
packageValue: false,
required: true,
public: false,
i18nLabel: 'CSEM_Enable_Automated_Report_Label',
i18nDescription: 'CSEM_Enable_Automated_Report_Description',
});
await configuration.settings.provideSetting({
id: SETTING_NCMEC_ORGNAME,
type: SettingType.STRING,
packageValue: 'TestOrg',
required: true,
public: false,
i18nLabel: 'CSEM_NCMEC_OrgName_Label',
i18nDescription: 'CSEM_NCMEC_OrgName_Description',
});
await configuration.settings.provideSetting({
id: SETTING_NCMEC_REPORTER_NAME,
type: SettingType.STRING,
packageValue: 'ReporterName',
required: true,
public: false,
i18nLabel: 'CSEM_NCMEC_ReporterName_Label',
i18nDescription: 'CSEM_NCMEC_ReporterName_Description',
});
await configuration.settings.provideSetting({
id: SETTING_NCMEC_REPORTER_EMAIL,
type: SettingType.STRING,
packageValue: '[email protected]',
required: true,
public: false,
i18nLabel: 'CSEM_NCMEC_ReporterEmail_Label',
i18nDescription: 'CSEM_NCMEC_ReporterEmail_Description',
});
await configuration.settings.provideSetting({
id: SETTING_NCMEC_USER,
type: SettingType.STRING,
packageValue: '',
required: true,
public: false,
i18nLabel: 'CSEM_NCMEC_User_Label',
i18nDescription: 'CSEM_NCMEC_User_Description',
});
await configuration.settings.provideSetting({
id: SETTING_NCMEC_PASSWORD,
type: SettingType.STRING,
packageValue: '',
required: true,
public: false,
i18nLabel: 'CSEM_NCMEC_Password_Label',
i18nDescription: 'CSEM_NCMEC_Password_Description',
});
await configuration.settings.provideSetting({
id: SETTING_NCMEC_ENABLE_TEST_MODE,
type: SettingType.BOOLEAN,
packageValue: true,
required: true,
public: false,
i18nLabel: 'CSEM_NCMEC_TestMode_Label',
i18nDescription: 'CSEM_NCMEC_TestMode_Description',
});
}
public async onEnable(environment: IEnvironmentRead, configurationModify: IConfigurationModify): Promise<boolean> {
this.quarantineChannel = await environment.getSettings().getValueById(SETTING_QUARANTINE_CHANNEL);
this.enableAutomatedReport = await environment.getSettings().getValueById(SETTING_ENABLE_AUTOMATED_REPORT);
let limitRoomNamesCsv = await environment.getSettings().getValueById(SETTING_LIMIT_ANALYSIS_TO_CHANNELS);
this.initLimitRoomNamesSet(limitRoomNamesCsv);
this.watchDMs = await environment.getSettings().getValueById(SETTING_WATCH_DMS);
return true;
}
public async onSettingUpdated(setting: ISetting, configurationModify: IConfigurationModify, read: IRead, http: IHttp): Promise<void> {
if (SETTING_QUARANTINE_CHANNEL === setting.id) {
this.quarantineChannel = setting.value;
} else if (SETTING_LIMIT_ANALYSIS_TO_CHANNELS === setting.id) {
await this.initLimitRoomNamesSet(setting.value);
} else if (SETTING_WATCH_DMS === setting.id) {
this.watchDMs = setting.value;
} else if (SETTING_ENABLE_AUTOMATED_REPORT === setting.id) {
this.enableAutomatedReport = setting.value;
}
}
private async initLimitRoomNamesSet(limitRoomNamesCsv: string) {
this.watchedRoomsId = undefined;
if (limitRoomNamesCsv && limitRoomNamesCsv.length > 0) {
this.watchedRoomsId = new Set<string>();
let _csvRoomNames = limitRoomNamesCsv.trim();
let _csvRoomsArray = _csvRoomNames.split(',');
for (const roomName of _csvRoomsArray) {
const room = await this.getAccessors().reader.getRoomReader().getByName(roomName.toLowerCase());
if (room) {
this.getLogger().debug(`Watching room \'${roomName}\'`);
this.watchedRoomsId!.add(room.id);
} else {
this.getLogger().warn(`Room not found for name \'${roomName}\'. Not adding to watch list.`);
}
}
}
}
async checkPreMessageSentModify(message: IMessage, read: IRead, http: IHttp): Promise<boolean> {
// https://developer.rocket.chat/reference/api/schema-definition/room
const roomTypes = {
dm: 'd',
chatroom: 'c',
private: 'p',
livechat: 'l'
}
if (
this.watchedRoomsId === undefined
|| this.watchedRoomsId.size === 0
|| this.watchedRoomsId.has(message.room.id)
|| (this.watchDMs && message.room.type === roomTypes.dm)
) {
return this.photoDnaService.preMatchMessage(message, this.getLogger());
} else {
return false;
}
}
async executePreMessageSentModify(message: IMessage, builder: IMessageBuilder, read: IRead, http: IHttp, persistence: IPersistence): Promise<IMessage> {
let logger = this.getLogger();
let result = await this.photoDnaService.matchMessage(message, logger, read, http);
if (result && result.IsMatch) {
this.handleMatchingMessage(result, message, read, persistence, builder, http, logger);
}
return builder.getMessage();
}
private async handleMatchingMessage(matchResult: IMatchResult, message: IMessage, read: IRead, persistence: IPersistence, builder: IMessageBuilder, http: IHttp, logger: ILogger): Promise<void> {
logger.warn('CSEM-MATCH', `enable automated report: ${this.enableAutomatedReport}`, `message ID: ${message.id}`, message.sender, JSON.stringify(matchResult));
if (this.quarantineChannel) {
const targetRoom: IRoom | undefined = await read.getRoomReader().getByName(this.quarantineChannel);
if (targetRoom) {
// we have a target room - move it to this room
// the original user uploading currently does not get notified
builder.setRoom(targetRoom);
} else {
logger.warn('Defined target Room/Channel does not exist: ' + this.quarantineChannel);
// we have no target room - at least remove the image
builder.removeAttachment(0);
}
} else {
logger.warn('No target channel for quarantined messages provided');
}
if (this.enableAutomatedReport) {
let result = await this.photoDnaService.performReportOperation(matchResult, http, message, read);
logger.warn('Violation-Report-Result', result);
}
}
}