This repository has been archived by the owner on Dec 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 67
/
remove-bogus-external.js
51 lines (49 loc) · 2 KB
/
remove-bogus-external.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
'use strict';
/**
* Script to clean up Actions incorrectly marked as 'external'.
* A bug in how we record actions
* (https://github.com/jsha/blocktogether/issues/178) led to a large number of
* duplicate actions being recorded in the DB as 'external'. The expected case
* from the bug was that we would execute a subscription block or auto-block,
* then sometime later observe that block and treat it as a new one. However,
* this script also catches a number of duplicate blocks caused by users
* deactivating and reactivating (see
* https://github.com/jsha/blocktogether/issues/180).
*/
var Q = require('q'),
util = require('./util'),
setup = require('./setup');
var BtUser = setup.BtUser;
BtUser
.findAll({
}).then(function(users) {
util.slowForEach(users, 2000, function(user) {
// Go from oldest actions to newest actions.
user.getActions({
order: [['updatedAt', 'ASC']]
}).then(function(actions) {
var hash = {};
actions.forEach(function(action) {
// We don't care about anything except blocks and unblocks.
if (action.type !== Action.BLOCK && action.type !== Action.UNBLOCK) {
return;
}
// If we find an external action where there was a previous action for
// the same sink_uid, and that action has the same type, delete this
// external action. This will catch incorrect 'external' actions
// (https://github.com/jsha/blocktogether/issues/178) as well as
// duplicate block actions triggered by users deactivating and
// reactivating.
if (action.cause === Action.EXTERNAL &&
hash[action.sink_uid] &&
hash[action.sink_uid].type === action.type) {
process.stdout.write('destroying ' + action.id + ' bc ' + hash[action.sink_uid].id + '\n');
action.destroy();
}
hash[action.sink_uid] = action;
});
});
});
}).catch(function(err) {
process.stderr.write(err);
});