-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.js
106 lines (103 loc) · 3.99 KB
/
init.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
var mongoose = require('mongoose');
var Metrics = mongoose.model('Metrics');
var Pack = mongoose.model('Pack');
var Author = mongoose.model('Author');
var Sticker = mongoose.model('Sticker');
var Tag = mongoose.model('Tag');
var async = require('async');
var https = require('https');
var fileType = require('file-type');
// Use only with arrays of a SINGLE, PRIMITIVE type
// e.g. [String] or [int]
var uniq = function(a) {
var seen = {};
return a.filter(function(item) {
return seen.hasOwnProperty(item) ? false : (seen[item] = true);
});
};
module.exports = {
run: function(callback) {
var calls = [];
/*
calls.push(function (callback) {
Sticker.find().populate({
path: 'author',
select: 'name'
}).exec(function (err, stickers) {
if (!err && stickers) {
stickers.forEach(function (sticker) {
if (sticker.author.name === 'Charlo Frade') {
sticker.remove(function (err) {
});
}
});
callback(null);
}
});
});
*/
// Animated stickers
calls.push(function(callback) {
Sticker.find().exec(function(err, stickers) {
var subcalls = [];
if (!err && stickers) {
stickers.forEach(function(sticker) {
subcalls.push(function(callback) {
https.get(sticker.image, res => {
res.once('data', chunk => {
res.destroy();
var type = fileType(chunk);
sticker.animated = (type.mime.indexOf('gif') !== -1 || type.mime.indexOf('apng') !== -1);
sticker.noUpdate = true;
sticker.save(function(err, sticker) {
callback(null);
});
});
});
});
});
}
async.parallel(subcalls, function(err, results) {
callback(err ? err : null);
});
});
});
// Fix timestamps
calls.push(function(callback) {
var models = [Sticker, Author, Pack, Tag];
var populate_subcalls = [];
var subcalls = [];
models.forEach(function(Model) {
populate_subcalls.push(function(callback) {
Model.find().exec(function(err, objs) {
objs.forEach(function(model_obj) {
subcalls.push(function(callback) {
model_obj.createdAtTimestamp = model_obj.createdAt.getTime();
model_obj.updatedAtTimestamp = model_obj.updatedAt.getTime();
model_obj.noUpdate = true;
model_obj.save(function(err, model_obj) {
callback(null);
});
});
});
callback(null);
});
});
});
async.series(populate_subcalls, function(err, results) {
async.parallel(subcalls, function(err, results) {
if (err) {
console.log("Error generating timestamps: " + err.message);
} else {
console.log("Updated timestamps.");
}
callback(null);
});
});
});
async.series(calls, function(err, results) {
console.log("Init done.");
callback();
});
}
};