Skip to content

Commit 7534df6

Browse files
author
Nicolas Garnier
committed
Simplified delete-unused-accounts-cron and moderate-images
Change-Id: I3c3a8ac72633b69f961a77fc4ec76288b556a3e9
1 parent 7e21531 commit 7534df6

File tree

2 files changed

+22
-22
lines changed

2 files changed

+22
-22
lines changed

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

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,32 +40,32 @@ exports.accountcleanup = functions.https().onRequest((req, res) => {
4040
if (key !== functions.env.cron.key) {
4141
console.log('The key provided in the request does not match the key set in the environment. Check that', key,
4242
'matches the cron.key attribute in `firebase env:get`');
43-
return res.status(403).send('Security key does not match. Make sure your "key" URL query parameter matches the ' +
43+
res.status(403).send('Security key does not match. Make sure your "key" URL query parameter matches the ' +
4444
'cron.key environment variable.');
45+
return;
4546
}
4647

47-
// We'll fetch all user details.
48+
// Fetch all user details.
4849
getUsers().then(users => {
49-
// We'll use a pool so that we delete maximum `MAX_CONCURRENT` users in parallel.
50+
// Find users that have not signed in in the last 30 days.
51+
const inactiveUsers = users.filter(
52+
user => parseInt(user.lastLoginAt, 10) > Date.now() - 30 * 24 * 60 * 60 * 1000);
53+
54+
// Use a pool so that we delete maximum `MAX_CONCURRENT` users in parallel.
5055
const promisePool = new PromisePool(() => {
51-
let user;
52-
// We search for users that have not signed in in the last 30 days.
53-
while (!user || parseInt(user.lastLoginAt) > Date.now() - 30 * 24 * 60 * 60 * 1000) {
54-
if (users.length === 0) {
55-
return null;
56-
}
57-
user = users.pop();
58-
}
56+
if (inactiveUsers.length > 0) {
57+
const userToDelete = inactiveUsers.pop();
5958

60-
// If an inactive user is found we delete it.
61-
return firebaseAdmin.auth().deleteUser(user.uid).then(() => {
62-
console.log('Deleted user account', user.uid, 'because of inactivity');
63-
}).catch(error => {
64-
console.error('Deletion of inactive user account', user.uid, 'failed:', error);
65-
});
59+
// Delete the inactive user.
60+
return firebaseAdmin.auth().deleteUser(userToDelete.uid).then(() => {
61+
console.log('Deleted user account', userToDelete.uid, 'because of inactivity');
62+
}).catch(error => {
63+
console.error('Deletion of inactive user account', userToDelete.uid, 'failed:', error);
64+
});
65+
}
6666
}, MAX_CONCURRENT);
6767

68-
return promisePool.start().then(() => {
68+
promisePool.start().then(() => {
6969
console.log('User cleanup finished');
7070
res.send('User cleanup finished');
7171
});

moderate-images/functions/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ const LOCAL_TMP_FOLDER = '/tmp/';
2626
* When an image is uploaded we check if it is flagged as Adult or Violence by the Cloud Vision
2727
* API and if it is we blur it using ImageMagick.
2828
*/
29-
exports.blurOffensiveImages = functions.storage().onChange(event => {
30-
const file = gcs.bucket(event.data.bucket).file(event.data.name);
29+
exports.blurOffensiveImages = functions.storage().onChange(object => {
30+
const file = gcs.bucket(object.bucket).file(object.name);
3131

3232
// Exit if this is a move or deletion event.
33-
if (event.data.resourceState === 'not_exists') {
33+
if (object.resourceState === 'not_exists') {
3434
return console.log('This is a deletion event.');
3535
}
3636

@@ -40,7 +40,7 @@ exports.blurOffensiveImages = functions.storage().onChange(event => {
4040
console.log('SafeSearch results on image', safeSearch);
4141

4242
if (safeSearch.adult || safeSearch.violence) {
43-
return blurImage(event.data.name, event.data.bucket, event.data.metadata);
43+
return blurImage(object.name, object.bucket, object.metadata);
4444
}
4545
});
4646
});

0 commit comments

Comments
 (0)