Browse Source

Simplified delete-unused-accounts-cron and moderate-images

Change-Id: I3c3a8ac72633b69f961a77fc4ec76288b556a3e9
ryanpbrewster-patch-1
Nicolas Garnier 8 years ago
parent
commit
7534df61ff
  1. 32
      delete-unused-accounts-cron/functions/index.js
  2. 8
      moderate-images/functions/index.js

32
delete-unused-accounts-cron/functions/index.js

@ -40,32 +40,32 @@ exports.accountcleanup = functions.https().onRequest((req, res) => {
if (key !== functions.env.cron.key) {
console.log('The key provided in the request does not match the key set in the environment. Check that', key,
'matches the cron.key attribute in `firebase env:get`');
return res.status(403).send('Security key does not match. Make sure your "key" URL query parameter matches the ' +
res.status(403).send('Security key does not match. Make sure your "key" URL query parameter matches the ' +
'cron.key environment variable.');
return;
}
// We'll fetch all user details.
// Fetch all user details.
getUsers().then(users => {
// We'll use a pool so that we delete maximum `MAX_CONCURRENT` users in parallel.
// Find users that have not signed in in the last 30 days.
const inactiveUsers = users.filter(
user => parseInt(user.lastLoginAt, 10) > Date.now() - 30 * 24 * 60 * 60 * 1000);
// Use a pool so that we delete maximum `MAX_CONCURRENT` users in parallel.
const promisePool = new PromisePool(() => {
let user;
// We search for users that have not signed in in the last 30 days.
while (!user || parseInt(user.lastLoginAt) > Date.now() - 30 * 24 * 60 * 60 * 1000) {
if (users.length === 0) {
return null;
}
user = users.pop();
}
if (inactiveUsers.length > 0) {
const userToDelete = inactiveUsers.pop();
// If an inactive user is found we delete it.
return firebaseAdmin.auth().deleteUser(user.uid).then(() => {
console.log('Deleted user account', user.uid, 'because of inactivity');
// Delete the inactive user.
return firebaseAdmin.auth().deleteUser(userToDelete.uid).then(() => {
console.log('Deleted user account', userToDelete.uid, 'because of inactivity');
}).catch(error => {
console.error('Deletion of inactive user account', user.uid, 'failed:', error);
console.error('Deletion of inactive user account', userToDelete.uid, 'failed:', error);
});
}
}, MAX_CONCURRENT);
return promisePool.start().then(() => {
promisePool.start().then(() => {
console.log('User cleanup finished');
res.send('User cleanup finished');
});

8
moderate-images/functions/index.js

@ -26,11 +26,11 @@ const LOCAL_TMP_FOLDER = '/tmp/';
* When an image is uploaded we check if it is flagged as Adult or Violence by the Cloud Vision
* API and if it is we blur it using ImageMagick.
*/
exports.blurOffensiveImages = functions.storage().onChange(event => {
const file = gcs.bucket(event.data.bucket).file(event.data.name);
exports.blurOffensiveImages = functions.storage().onChange(object => {
const file = gcs.bucket(object.bucket).file(object.name);
// Exit if this is a move or deletion event.
if (event.data.resourceState === 'not_exists') {
if (object.resourceState === 'not_exists') {
return console.log('This is a deletion event.');
}
@ -40,7 +40,7 @@ exports.blurOffensiveImages = functions.storage().onChange(event => {
console.log('SafeSearch results on image', safeSearch);
if (safeSearch.adult || safeSearch.violence) {
return blurImage(event.data.name, event.data.bucket, event.data.metadata);
return blurImage(object.name, object.bucket, object.metadata);
}
});
});

Loading…
Cancel
Save