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. 36
      delete-unused-accounts-cron/functions/index.js
  2. 8
      moderate-images/functions/index.js

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

@ -40,32 +40,32 @@ exports.accountcleanup = functions.https().onRequest((req, res) => {
if (key !== functions.env.cron.key) { 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, 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`'); '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.'); 'cron.key environment variable.');
return;
} }
// We'll fetch all user details. // Fetch all user details.
getUsers().then(users => { 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(() => { const promisePool = new PromisePool(() => {
let user; if (inactiveUsers.length > 0) {
// We search for users that have not signed in in the last 30 days. const userToDelete = inactiveUsers.pop();
while (!user || parseInt(user.lastLoginAt) > Date.now() - 30 * 24 * 60 * 60 * 1000) {
if (users.length === 0) {
return null;
}
user = users.pop();
}
// If an inactive user is found we delete it. // Delete the inactive user.
return firebaseAdmin.auth().deleteUser(user.uid).then(() => { return firebaseAdmin.auth().deleteUser(userToDelete.uid).then(() => {
console.log('Deleted user account', user.uid, 'because of inactivity'); console.log('Deleted user account', userToDelete.uid, 'because of inactivity');
}).catch(error => { }).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); }, MAX_CONCURRENT);
return promisePool.start().then(() => { promisePool.start().then(() => {
console.log('User cleanup finished'); console.log('User cleanup finished');
res.send('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 * 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. * API and if it is we blur it using ImageMagick.
*/ */
exports.blurOffensiveImages = functions.storage().onChange(event => { exports.blurOffensiveImages = functions.storage().onChange(object => {
const file = gcs.bucket(event.data.bucket).file(event.data.name); const file = gcs.bucket(object.bucket).file(object.name);
// Exit if this is a move or deletion event. // 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.'); 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); console.log('SafeSearch results on image', safeSearch);
if (safeSearch.adult || safeSearch.violence) { 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