-
Notifications
You must be signed in to change notification settings - Fork 0
/
UserDao.ts
125 lines (110 loc) · 3.34 KB
/
UserDao.ts
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
/// <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"/>
import crypt = require("./common/Crypter");
import u = require("./User");
import _ = require("underscore");
import mongodb = require("mongodb");
import winston = require("winston");
export interface UserDao {
getUsersListeningToStation(station:string, callback:(err, users:u.User[]) => void): void;
incrementUserScrobble(listener: string, station: string, callback:(err, string) => void): void;
}
export class DummyUserDao implements UserDao {
getUsersListeningToStation(station:string, callback:(err, users:u.User[]) => void): void {
if (station == "Station1") {
callback(null, [
{ UserName: "User1", Session: "" },
{ UserName: "User2", Session: "" },
{ UserName: "User3", Session: "" }
]);
}
else {
callback(null, [
{ UserName: "User2", Session: "" }
]);
}
}
incrementUserScrobble(listener: string, station: string, callback:(err, string) => void): void {
callback(null, "ok");
}
}
export class MongoUserDao implements UserDao {
constructor (private dbClient: mongodb.Db, private crypter: crypt.Crypter) { }
getUsersListeningToStation(station:string, callback:(err, users:u.User[]) => void): void {
if (!this.dbClient || !this.crypter) {
callback("Invalid DAO setup", null);
return;
}
this.dbClient.collection('user', (error, collection) => {
if (error) {
callback(error, null);
return;
}
collection.find({ listening: station }).toArray((err, results) => {
if (err) {
callback(error, null);
return;
}
var users = [];
_.each(results, (record:any) => {
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);
});
});
}
incrementUserScrobble(listener: string, station: string, callback:(err, string) => void): void {
this.dbClient.collection('user', (error, collection) => {
if (error) {
callback(error, null);
return;
}
var incData = {};
incData['scrobbles.' + station] = 1;
collection.update({ _id: listener }, { $inc: incData }, (err) => {
if (err) {
callback(err, null);
}
else {
callback(null, "ok");
}
});
});
}
clearUserListening(username: string, callback:(err, string) => void): void {
this.dbClient.collection('user', (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);
}
});
});
}
}