-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
141 lines (118 loc) · 3.13 KB
/
utils.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
138
139
140
141
var crypto = require('crypto')
, salt = 'x8FsqCnMURG/cBuuBjqJMN4Ll/akxifULmg7MljWGyTDHOdw1NHzG'
, SHA2 = new (require('jshashes').SHA512)()
, User = require('./providers/user').User
, Station = require('./providers/station').Station
, Tone = require('./providers/tone').Tone;
/*
* Restrict paths
*/
exports.restrict = function(req, res, next){
if(req.isAuthenticated()) next();
else res.redirect('/');
};
/*
* Sort Case Insensitive
*/
exports.encodePassword = function (pass) {
if( typeof pass === 'string' && pass.length < 6 )
return ''
return SHA2.b64_hmac(pass, salt )
};
/*
* Sort Case Insensitive
*/
exports.caseInsensitiveSort = function (a, b) {
var ret = 0;
a = a.toLowerCase();
b = b.toLowerCase();
if(a > b) ret = 1;
if(a < b) ret = -1;
return ret;
};
/*
* add tone
*/
exports.createTone = function (data, station_id, user_id, fn) {
Tone.find({'id':data.id}).exec(function(err, tones){
if(err) res.send({'error':'An error has occurred'});
if(tones.length>0){
console.log('tone found:'+tones[0].id);
exports.addToneInStation(tones[0]._id, station_id, user_id, fn);
}else{
var tone = new Tone({
id:data.id,
thumb:data.thumb,
title:data.title,
category:data.category,
duration:data.duration,
});
tone.save(function(err, tone){
if(err) res.send(config.message.valid);
console.log('tone save:'+tone.id);
exports.addToneInStation(tone._id, station_id, user_id, fn);
});
}
});
};
exports.addToneInStation = function (tone_id, station_id, user_id, fn) {
console.log('add tone '+tone_id+' in station '+station_id);
Station.findById(station_id).exec(function(err, station){
// we add the id of the tone in the station
var tone={
'id':tone_id,
'user_id':user_id
}
station.tones.push(tone)
// we remove a tone from the user that created it
for(var i=0;i<station.users.length;i++){
var temp_id = station.users[i].id;
if(user_id+'' === temp_id+''){
if(station.users[i].nb_tones>0){
var user = {
"id":user_id,
"nb_tones":station.users[i].nb_tones-1
}
station.users.splice(i, 1, user);
}
}
}
station.save();
fn();
});
};
/*
* When the user enter in a station
*/
exports.enterStation = function (user_id, station_id, fn) {
Station.findById(station_id).exec(function(err, station){
for(var i=0;i<station.users.length;i++){
var temp_id = station.users[i].id;
if(user_id+'' === temp_id+''){
return fn();
}
}
var user = {
"id":user_id,
"nb_tones":station.nb_tones
}
station.users.push(user)
station.save();
fn();
});
};
/*
* When the user leave in a station
*/
exports.leaveStation = function (user_id, station_id, fn) {
Station.findById(station_id).exec(function(err, station){
for(var i=0;i<station.users.length;i++){
var temp_id = station.users[i].id;
if(user_id+'' === temp_id+''){
station.users.splice(i, 1);
station.save();
return fn();
}
}
});
};