-
Notifications
You must be signed in to change notification settings - Fork 0
/
UserDao.js
111 lines (111 loc) · 4.16 KB
/
UserDao.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
"use strict";
/// <reference path="./definitions/DefinitelyTyped/mongodb/mongodb.d.ts"/>
/// <reference path="./definitions/DefinitelyTyped/underscore/underscore.d.ts"/>
/// <reference path="./definitions/typescript-node-definitions/winston.d.ts"/>
exports.__esModule = true;
var _ = require("underscore");
var winston = require("winston");
var DummyUserDao = (function () {
function DummyUserDao() {
}
DummyUserDao.prototype.getUsersListeningToStation = function (station, callback) {
if (station == "Station1") {
callback(null, [
{ UserName: "User1", Session: "" },
{ UserName: "User2", Session: "" },
{ UserName: "User3", Session: "" }
]);
}
else {
callback(null, [
{ UserName: "User2", Session: "" }
]);
}
};
DummyUserDao.prototype.incrementUserScrobble = function (listener, station, callback) {
callback(null, "ok");
};
return DummyUserDao;
}());
exports.DummyUserDao = DummyUserDao;
var MongoUserDao = (function () {
function MongoUserDao(dbClient, crypter) {
this.dbClient = dbClient;
this.crypter = crypter;
}
MongoUserDao.prototype.getUsersListeningToStation = function (station, callback) {
var _this = this;
if (!this.dbClient || !this.crypter) {
callback("Invalid DAO setup", null);
return;
}
this.dbClient.collection('user', function (error, collection) {
if (error) {
callback(error, null);
return;
}
collection.find({ listening: station }).toArray(function (err, results) {
if (err) {
callback(error, null);
return;
}
var users = [];
_.each(results, function (record) {
if (!record._id || !record.session) {
winston.error("Invalid user record found in DB:", record);
}
else {
var user = {
UserName: record._id,
Session: record.session ? _this.crypter.decrypt(record.session) : null,
ScrobbleTimeoutEnabled: record.scrobbleTimeoutEnabled || false,
ScrobbleTimeoutTime: record.scrobbleTimeoutTime || null
};
winston.info("Found user listening to " + station + ":", user.UserName);
users.push(user);
}
});
callback(null, users);
});
});
};
MongoUserDao.prototype.incrementUserScrobble = function (listener, station, callback) {
this.dbClient.collection('user', function (error, collection) {
if (error) {
callback(error, null);
return;
}
var incData = {};
incData['scrobbles.' + station] = 1;
collection.update({ _id: listener }, { $inc: incData }, function (err) {
if (err) {
callback(err, null);
}
else {
callback(null, "ok");
}
});
});
};
MongoUserDao.prototype.clearUserListening = function (username, callback) {
this.dbClient.collection('user', function (error, collection) {
if (error) {
callback(error, null);
return;
}
collection.findAndModify({ _id: username }, [['_id', 'asc']], { $unset: { listening: 1 } }, { upsert: false }, function (error, record) {
if (error) {
var message = "Could not clear listening for user " + username + ": " + error.message;
winston.error(message);
callback(error, null);
}
else {
callback(null, record);
}
});
});
};
return MongoUserDao;
}());
exports.MongoUserDao = MongoUserDao;
//# sourceMappingURL=UserDao.js.map