-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathuser.js
231 lines (197 loc) · 7.01 KB
/
user.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
229
230
231
/**
* @module User
*/
var plexutils = require('./plexutils');
var Q = require('q');
var url = require('url');
var db = require('./db');
/**
* @constructor User
* @param {Object} dbobject - an object representing exactly what is in the database for this user
*/
var User = function(app, dbobject) {
this.dbobject = dbobject;
/**
* The App object for this specific request
* @private
* @member {module:App~App}
*/
this._app = app;
var context = this;
Object.defineProperty(this, 'authtoken', {
get: function() {
return context.dbobject.authtoken;
},
set: function(token) {
context.dbobject.authtoken = token;
}
});
Object.defineProperty(this, 'libraries', {
get: function() {
return context.dbobject.libraries;
}
});
Object.defineProperty(this, 'TVLibrary', {
get: function() {
if (!context.dbobject.libraries) {
throw new Error("Trying to get TVLirary with no libraries on the user record");
}
var libraries = context.dbobject.libraries.filter(function(library) {
return library.type == "show";
});
// For now we're going to sort by key, which more or less gives us a sort by creation date.
libraries.sort(function(a, b) {
return Number(a.key) - Number(b.key);
});
return libraries[0];
}
});
Object.defineProperty(this, 'server', {
get: function() {
return context.dbobject.server;
}
});
Object.defineProperty(this, 'serverName', {
get: function() {
if (!context.dbobject.server) {
throw new Error("Trying to get serverName with no pms on the user record");
}
return context.dbobject.server.attributes.name;
}
});
Object.defineProperty(this, 'hostname', {
get: function() {
if (!context.dbobject.server) {
throw new Error("Trying to get hostname with no pms on the user record");
}
var connection = context.dbobject.server.Connection.filter(function(connection) {
return connection.attributes.local == "0";
})[0];
var uri = url.parse(connection.attributes.uri);
return uri.hostname;
}
});
Object.defineProperty(this, 'port', {
get: function() {
if (!context.dbobject.server) {
throw new Error("Trying to get port with no pms on the user record");
}
var connection = context.dbobject.server.Connection.filter(function(connection) {
return connection.attributes.local == "0";
})[0];
var uri = url.parse(connection.attributes.uri);
return uri.port;
}
});
Object.defineProperty(this, 'https', {
get: function() {
if (!context.dbobject.server) {
throw new Error("Trying to get https with no pms on the user record");
}
var connection = context.dbobject.server.Connection.filter(function(connection) {
return connection.attributes.local == "0";
})[0];
var uri = url.parse(connection.attributes.uri);
return (uri.protocol == 'https:');
}
});
Object.defineProperty(this, 'serverIdentifier', {
get: function() {
return context.dbobject.server.attributes.clientIdentifier;
}
});
Object.defineProperty(this, 'player', {
get: function() {
return context.dbobject.player;
}
});
Object.defineProperty(this, 'playerName', {
get: function() {
return context.dbobject.player.name;
}
});
Object.defineProperty(this, 'playerURI', {
get: function() {
return context.dbobject.player.uri;
}
});
Object.defineProperty(this, 'pin', {
get: function() {
return context.dbobject.pin;
}
});
};
/**
* Sets the user's server to the first one in their account if they didn't have a server already set
* @param {boolean} forceReset - forces the server to be reset even if they already had one
* @returns {Promise.boolean} Resolves with bool representing whether or not an update was made
*/
User.prototype.setDefaultServer = function(forceReset) {
var context = this;
if (!this.server || forceReset) {
return plexutils.getServers(context._app)
.then(function(servers) {
return db.updateUserServer(context, servers[0])
.then(function() {
return true;
})
})
}
return Q.resolve(false);
};
/**
* Retrieves the list of "Directories" on the server and caches them in the database
* so we can save ourselves an API call on every future request.
* @param {boolean} forceReset - forces the update even if we already had this in the database
* @returns {Promise.boolean} Resolves with bool representing whether or not an update was made
*/
User.prototype.cacheServerLibraries = function(forceReset) {
var context = this;
if (!this.libraries || forceReset) {
return plexutils.getLibrarySections(context._app)
.then(function(directories) {
return db.updateUserLibraries(context, directories._children)
.then(function() {
return true;
})
})
}
return Q.resolve(false);
};
/**
* Sets the user's player to the first one in their account if they didn't have a player already set
* @param {boolean} forceReset - forces the player to be reset even if they already had one
* @returns {Promise.boolean} Resolves with bool representing whether or not an update was made
*/
User.prototype.setDefaultPlayer = function(forceReset) {
var context = this;
if (!this.player || forceReset) {
return plexutils.getPlayers(context._app)
.then(function(players) {
return db.updateUserPlayer(context, players[0])
.then(function() {
return true;
})
})
}
return Q.resolve(false);
};
/**
* Sets the user's server and player if none are already set
* @param {boolean} [forceReset=false] - forces a refresh from the account
* @returns {Promise.boolean} Resolves when all API and DB calls are done with whether or not any updates were made
*/
User.prototype.setupDefaults = function(forceReset) {
var context = this;
return this.setDefaultServer(forceReset)
.then(function(serverUpdated) {
// We want to force update the player if the server changed
return context.setDefaultPlayer(forceReset || serverUpdated)
.then(function() {
return context.cacheServerLibraries(forceReset || serverUpdated);
});
});
};
module.exports = {
User: User
};