Changeset View
Changeset View
Standalone View
Standalone View
lib/context.js
"use strict"; | |||||
const locreq = require("locreq")(__dirname); | const locreq = require("locreq")(__dirname); | ||||
const promisify = require("bluebird-events"); | const promisify = require("bluebird-events"); | ||||
const EventEmitter = require("events"); | const EventEmitter = require("events"); | ||||
function Context( | function Context( | ||||
timestamp, | _timestamp, | ||||
ip, | ip, | ||||
user_id, | _user_id, | ||||
session_id, | session_id, | ||||
anonymous_session_id, | anonymous_session_id, | ||||
anon_session_is_new, | anon_session_is_new, | ||||
anonymous_user_id | anonymous_user_id | ||||
) { | ) { | ||||
Object.defineProperty(this, "_cached_user_data", { | Object.defineProperty(this, "_cached_user_data", { | ||||
value: false, | value: false, | ||||
writable: true, | writable: true, | ||||
Show All 16 Lines | ) { | ||||
Object.defineProperty(this, "anonymous_session_id", { | Object.defineProperty(this, "anonymous_session_id", { | ||||
value: anonymous_session_id || null, | value: anonymous_session_id || null, | ||||
}); // to make it non-enumerable and non-writeable | }); // to make it non-enumerable and non-writeable | ||||
Object.defineProperty(this, "anon_session_is_new", { | Object.defineProperty(this, "anon_session_is_new", { | ||||
value: anon_session_is_new || false, | value: anon_session_is_new || false, | ||||
}); // to make it non-enumerable and non-writeable | }); // to make it non-enumerable and non-writeable | ||||
if (user_id === undefined || user_id === false) { | const user_id = | ||||
user_id = null; | _user_id === undefined || _user_id === false ? null : _user_id; | ||||
} | |||||
if (timestamp === undefined) { | const timestamp = | ||||
const d = new Date(); | _timestamp === undefined ? new Date().getTime() : _timestamp; | ||||
timestamp = d.getTime(); | |||||
} | |||||
this.timestamp = timestamp; | this.timestamp = timestamp; | ||||
this.ip = ip || null; | this.ip = ip || null; | ||||
this.user_id = user_id; | this.user_id = user_id; | ||||
this.anonymous_user_id = anonymous_user_id || null; | this.anonymous_user_id = anonymous_user_id || null; | ||||
} | } | ||||
Context.prototype.get_user_data = function(app) { | Context.prototype.get_user_data = function(app) { | ||||
const self = this; | const self = this; | ||||
if (this.user_id === null) { | if (this.user_id === null) { | ||||
return Promise.resolve(null); | return Promise.resolve(null); | ||||
} else if (self.loading_user_data) { | } else if (self.loading_user_data) { | ||||
return promisify(self.e, { | return promisify(self.e, { | ||||
resolve: "loaded_user_data", | resolve: "loaded_user_data", | ||||
reject: "error", | reject: "error", | ||||
}); | }); | ||||
} else if (self._cached_user_data === false) { | } else if (self._cached_user_data === false) { | ||||
const SuperContext = locreq("lib/super-context.js"); | const SuperContext = locreq("lib/super-context.js"); | ||||
self.loading_user_data = true; | self.loading_user_data = true; | ||||
const c = new SuperContext(self); | const c = new SuperContext(self); | ||||
return app | return app | ||||
.run_action(c, ["collections", "users", this.user_id], "show") | .run_action(c, ["collections", "users", this.user_id], "show") | ||||
.then(function(user_data) { | .then(user_data => { | ||||
self._cached_user_data = user_data; | self._cached_user_data = user_data; | ||||
self.loading_user_data = false; | self.loading_user_data = false; | ||||
self.e.emit("loaded_user_data", user_data); | self.e.emit("loaded_user_data", user_data); | ||||
return user_data; | return user_data; | ||||
}) | }) | ||||
.catch(function(error) { | .catch(error => { | ||||
self.e.emit("error"); | self.e.emit("error"); | ||||
throw error; | throw error; | ||||
}); | }); | ||||
} else { | |||||
return Promise.resolve(self._cached_user_data); | |||||
} | } | ||||
return Promise.resolve(self._cached_user_data); | |||||
}; | }; | ||||
module.exports = Context; | module.exports = Context; |