/
db.js
228 lines (213 loc) · 6.4 KB
/
db.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
mongoose.connect('mongodb://localhost/ttfmbot');
var Bans = new Schema({
name: String,
banDate: { type: Date, default: Date.now }
});
var Artist = new Schema({
display_name: String,
identifying_names: [{ type: String }]
});
var DJ = new Schema({
bot_admin: { type: Boolean, default: false },
created: { type: Number, default: 0 },
last_seen: { type: Date, default: Date.now },
name: String
});
var Play = new Schema({
dj: { type: Schema.ObjectId, ref: 'DJ' },
downs: { type: Number, default: 0 },
listeners: { type: Number, default: 0 },
song: { type: Schema.ObjectId, ref: 'Song' },
starttime: { type: Number, unique: true },
ups: { type: Number, default: 0 }
});
var Song = new Schema({
album: String,
artist: { type: Schema.ObjectId, ref: 'Artist' },
coverart: String,
title: String
});
Artist.statics.Add = function(name, cb) {
this.findOne({ identifying_names: name.toLowerCase() }, function(err, data) {
if (err) {
cb(err, null);
} else if (data) {
cb(err, data);
} else {
var instance = new ArtistModel({ display_name: name });
instance.identifying_names.push(name.toLowerCase());
instance.save(function(err) { cb(err, instance); });
}
});
};
DJ.statics.Add = function(id, name, created, admin, cb) {
this.findOne({ _id: id }, function(err, data) {
if (err) {
cb(err, null);
} else if (data) {
data.bot_admin = admin;
data.created = created;
data.last_seen = new Date();
data.name = name;
data.save(function(err) { cb(err, data); });
} else {
var instance = new DJModel({
_id: id,
bot_admin: admin,
created: created,
last_seen: new Date(),
name: name
});
instance.save(function(err) { cb(err, instance); });
}
});
};
Bans.statics.Add = function(id, name, cb){
this.findOne({ _id: id }, function(err, data) {
if (err) {
cb(err, null);
} else if (data) {
data.name = name;
data.save(function(err) { cb(err, data); });
} else {
var instance = new BansModel({
_id: id,
banDate: new Date(),
name: name
});
instance.save(function(err) { cb(err, instance); });
}
});
};
DJ.statics.IsAdmin = function(id, cb) {
this.findOne({ _id: id }, function(err, data) {
if (err) {
cb(err, false);
} else if (data) {
cb(err, data.bot_admin);
} else {
cb(err, false);
}
});
};
/*
Bans.statics.IsBanned = function(id, cb) {
this.findOne({ _id: id }, function(err, data) {
if (err) { cb(err, false); }
else if (data) { cb(err, true); }
else { cb(err, false); }
if (err) {
cb(err, false);
} else if (data) {
cb(err, data.name);
} else {
cb(err, false);
}
});
};
*/
Bans.statics.testBan = function(id, cb) {
this.findOne( {'_id': id}, function(err, banData){
if(err) { console.log('we have an error.');}
else if (banData) { console.log('we have banData. ' + id);
//Send ban signal with id to bot.js function to remove user from booth.
cb(err, id);
}
//else { console.log('we dont know whats going on.'); }
});
};
Bans.statics.IsBanned = function(id) {
this.findOne({ '_id' : id }, function(err, cp){
if(err) { return false; }
else if (data) { return true; }
});
};
Play.statics.Add = function(dj, downs, listeners, song, starttime, ups, cb) {
this.findOne({ starttime: starttime }, function(err, data) {
if (err) {
cb(err, null);
} else if (data) {
data.downs = downs;
data.listeners = listeners;
data.ups = ups;
data.save(function(err) { cb(err, data); });
} else {
var instance = new PlayModel({
dj: dj,
downs: downs,
listeners: listeners,
song: song,
starttime: starttime,
ups: ups
});
instance.save(function(err) { cb(err, instance); });
}
});
};
Play.statics.UpdateVotes = function(id, downs, listeners, ups, cb) {
this.findOne({ _id: id }, function(err, data) {
if (data) {
data.downs = downs;
data.listeners = listeners;
data.ups = ups;
data.save(function(err) { cb(err); });
} else {
cb(err);
}
});
};
Song.statics.Add = function(album, artist, coverart, title, cb) {
if (album) { album = album.trim(); }
if (title) { title = title.trim(); }
var albumLower = album;
var titleLower = title;
var albumExists = false;
var titleExists = false;
if (albumLower) {
albumExists = true;
albumLower = albumLower.toLowerCase();
}
if (titleLower) {
titleExists = true;
titleLower = titleLower.toLowerCase();
}
this.findOne(
{ $where: '((!this.album && !'
+ albumExists
+ ') || (this.album && this.album.toLowerCase() == "'
+ albumLower
+ '")) && this.artist._id == "'
+ artist._id
+ '" && ((!this.title && !'
+ titleExists
+ ') || (this.title && this.title.toLowerCase() == "'
+ titleLower
+ '"))' },
function(err, data) {
if (err) {
cb(err, null);
} else if (data) {
cb(err, data);
} else {
var instance = new SongModel({
album: album,
artist: artist,
coverart: coverart,
title: title
});
instance.save(function(err) { cb(err, instance) });
}
});
};
var ArtistModel = mongoose.model('Artist', Artist);
var DJModel = mongoose.model('DJ', DJ);
var PlayModel = mongoose.model('Play', Play);
var SongModel = mongoose.model('Song', Song);
var BansModel = mongoose.model('Bans', Bans);
exports.Artist = ArtistModel;
exports.DJ = DJModel;
exports.Play = PlayModel;
exports.Song = SongModel;
exports.Bans = BansModel;