forked from firebase/functions-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
85 lines (73 loc) · 2.8 KB
/
index.js
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
/**
* Copyright 2016 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
const functions = require('firebase-functions/v1');
const capitalizeSentence = require('capitalize-sentence');
const Filter = require('bad-words');
const badWordsFilter = new Filter();
// Moderates messages by lowering all uppercase messages and removing swearwords.
exports.moderator = functions.database.ref('/messages/{messageId}').onWrite((change) => {
const message = change.after.val();
if (message && !message.sanitized) {
// Retrieved the message values.
functions.logger.log('Retrieved message content: ', message);
// Run moderation checks on on the message and moderate if needed.
const moderatedMessage = moderateMessage(message.text);
// Update the Firebase DB with checked message.
functions.logger.log(
'Message has been moderated. Saving to DB: ',
moderatedMessage
);
return change.after.ref.update({
text: moderatedMessage,
sanitized: true,
moderated: message.text !== moderatedMessage,
});
}
return null;
});
// Moderates the given message if appropriate.
function moderateMessage(message) {
// Re-capitalize if the user is Shouting.
if (isShouting(message)) {
functions.logger.log('User is shouting. Fixing sentence case...');
message = stopShouting(message);
}
// Moderate if the user uses SwearWords.
if (containsSwearwords(message)) {
functions.logger.log('User is swearing. moderating...');
message = moderateSwearwords(message);
}
return message;
}
// Returns true if the string contains swearwords.
function containsSwearwords(message) {
return message !== badWordsFilter.clean(message);
}
// Hide all swearwords. e.g: Crap => ****.
function moderateSwearwords(message) {
return badWordsFilter.clean(message);
}
// Detect if the current message is shouting. i.e. there are too many Uppercase
// characters or exclamation points.
function isShouting(message) {
return message.replace(/[^A-Z]/g, '').length > message.length / 2 || message.replace(/[^!]/g, '').length >= 3;
}
// Correctly capitalize the string as a sentence (e.g. uppercase after dots)
// and remove exclamation points.
function stopShouting(message) {
return capitalizeSentence(message.toLowerCase()).replace(/!+/g, '.');
}