-
Notifications
You must be signed in to change notification settings - Fork 0
/
cleanSnaps.js
137 lines (102 loc) · 2.24 KB
/
cleanSnaps.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
var fs = require('fs');
module.exports = cleanSnaps;
function cleanSnaps(data, callback){
// if they're aren't any snaps stop everything maaan
if(typeof data.snaps === 'undefined' || !data.snaps.length){
console.log("No snaps!");
return;
}
var snaps = clean(data.snaps).received;
var newSnaps = [];
for(var i = 0; i < snaps.length; i++){
var snap = snaps[i];
newSnaps.push(snap.username);
}
callback(getUnique(newSnaps));
}
function clean(snaps){
var newSnaps = { "sent": [], "received": [], "other": [] };
snaps.forEach(function(snap){
var newSnap = {};
// video
if(typeof snap.m !== "undefined"){
if(snap.m == 1){
snap.type = "video";
}else if(snap.m == 3){
snap.type = "added";
}else{
snap.type = "image";
}
delete snap.m;
}
// screenshot
if(typeof snap.c !== "undefined"){
snap.screenshot = snap.c;
delete snap.c
}
// username
if(typeof snap.sn !== "undefined"){
snap.username = snap.sn;
delete snap.sn;
}
// timestamp
if(typeof snap.ts !== "undefined"){
snap.timestamp = snap.ts;
delete snap.ts;
}
// sent timestmap
if(typeof snap.sts !== "undefined"){
snap.timestamp_sent = snap.sts;
delete snap.sts;
}
// recipient
if(typeof snap.rp !== "undefined"){
snap.recipient = snap.rp;
delete snap.rp;
}
// rounded timer
if(typeof snap.t !== "undefined"){
delete snap.t;
}
// state
if(typeof snap.st !== "undefined"){
if(snap.st == 1){
snap.state = "unopened";
}
else if(snap.st == 2){
snap.state = "opened";
}
else{
snap.state = snap.st;
};
delete snap.st;
}
if(snap.type == "image" || snap.type == "video"){
// if there is a recipient then it was sent
if(typeof snap.recipient !== "undefined"){
newSnaps.sent.push(snap);
}else{
newSnaps.received.push(snap);
}
}else{
delete snap.t;
delete snap.timer;
newSnaps.other.push(snap);
}
});
return newSnaps;
}
function getUnique(arr){
var newArr = [];
arr.forEach(function(element, index, array){
var found = false;
var newId = element;
newArr.forEach(function(element, index, array){
if(element === newId) found = true;
});
if(!found){
newArr.push(element);
}
});
return newArr;
}