diff --git a/README.md b/README.md index 2fccba5..a532a7c 100644 --- a/README.md +++ b/README.md @@ -13,15 +13,11 @@ This repository contains the following templates: ### [Text Moderation](/text-moderation) -How to moderate user input text for bad words. +How to moderate user input text for bad words. For example this can be used to moderate usernames, chat or forum messages. -### [Username Moderation](/username-moderation) TODO import from go/hearth-codesamples +### [LastModified Firebase DB tracking](/lastmodified-tracking) -How to moderate new user names for bad words. - -### [LastModified Firebase DB tracking](/lastmodified-tracking) TODO import from go/hearth-codesamples - -Tracking when the Firebase DB was last modified. +Tracking when the Firebase DB (or a subset) was last modified. ### [Automatic message translation](/message-translation) TODO import from go/hearth-codesamples diff --git a/lastmodified-tracking/README.md b/lastmodified-tracking/README.md new file mode 100644 index 0000000..c425703 --- /dev/null +++ b/lastmodified-tracking/README.md @@ -0,0 +1,58 @@ +# Tracking last modified Date of a Firebase Database + +This template shows how to keep track of the date at which the Firebase Database or a subset of a firebase Database was last modified. + +## Cloud Function Code + +See file [index.js](index.js) for the code. + +This is done by simply updating a `lastmodified` attribute on the parent of the node which is tracked (for instance at the root of the Database). + +## Sample Database Structure + +As an example we'll be using a simple chat database structure: + +``` +/functions-project-12345 + /chat + /key-123456 + username: "Mat" + text: "Hey Bob!" + /key-123457 + username: "Bob" + text: "Hey Mat" +``` + +## Trigger rules + +Below is the trigger rule for the `touch` function making sure it's triggered when a new chat message is added/modified. + +``` + "functions": { + ".source": "functions", + "touch": { + "triggers": { + "database": { + "path": "/chat/$message", + } + } + } + } +``` + +## Security Rules + +The following security rules ensures only a Cloud Function with admin access can update the `lastmodified` attribute. + +``` +{ + "rules": { + "lastmodified": { + ".write": "false", // TODO: Check taht this works with GCF admin access! + ".read": "true" + } + } +} +``` + + diff --git a/lastmodified-tracking/index.js b/lastmodified-tracking/index.js new file mode 100644 index 0000000..9b37e1f --- /dev/null +++ b/lastmodified-tracking/index.js @@ -0,0 +1,36 @@ +/** + * Copyright 2015 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'; + +var Firebase = require('firebase'); +var env = require('./env'); +var ref = new Firebase(env.get('firebase.database.url')); + +function touch(context, data) { + ref.auth(env.get('firebase.database.token'), function(error, result) { + if (error) { + context.done(error); + } else { + console.log('Authenticated successfully with admin rights'); + ref.child('lastmodified').set(Firebase.ServerValue.TIMESTAMP); + context.done(); + } + }); +} + +module.exports = { + touch: touch +} \ No newline at end of file