I want to check documents as they are created in firestore to ensure there are no swear words contained in a publicly visible field. I would like to be able to delete the post after it has been detected to contain swear words. To do this I am trying to use firebase cloud functions:
// Package used to filter profanity
const badWordsFilter = require('bad words-list');
// The Cloud Functions for Firebase SDK to create Cloud Functions and setup triggers.
const functions = require('firebase-functions');
// The Firebase Admin SDK to access Cloud Firestore.
const admin = require('firebase-admin');
admin.initializeApp();
export const filterBadWords =
functions.firestore.document("posts/{userId}/userPosts/{postId}").onCreate((snapshot, context) => {
const message = snapshot.data.val();
// Check if post contains bad word
if (containsSwearwords(message)) {
//This is where I want to remove the post.
//What do I put here to delete the document?
}
})
// Returns true if the string contains swearwords.
function containsSwearwords(message: any) {
return message !== badWordsFilter.clean(message);
}
Database structure:
-posts(Collection)
|
+---{userID} (Document)
|
+---userPosts (collection)
|
+---{Documents to be checked} (Document)
|
+-Url1(field)
+-Url2(field)
+-string1(field)<- check this field for swear
+-string2(field)<- check this field for swear
The cloud functions are written using javascript
eventto the first argument of an onCreate function. They deliver a document snapshot as shown in the documentation. If you're using an old version, it's really old, and you should upgrade. firebase.google.com/docs/functions/…