From 3ee12d3c0ffe2b10f53470a8e98f852680c5a983 Mon Sep 17 00:00:00 2001 From: Ivan Socolsky Date: Tue, 9 Aug 2016 16:44:45 -0300 Subject: [PATCH] add session model --- lib/model/index.js | 1 + lib/model/session.js | 52 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 lib/model/session.js diff --git a/lib/model/index.js b/lib/model/index.js index f463e88..98b30bb 100644 --- a/lib/model/index.js +++ b/lib/model/index.js @@ -8,5 +8,6 @@ Model.Notification = require('./notification'); Model.Preferences = require('./preferences'); Model.Email = require('./email'); Model.TxNote = require('./txnote'); +Model.Session = require('./session'); module.exports = Model; diff --git a/lib/model/session.js b/lib/model/session.js new file mode 100644 index 0000000..32183ee --- /dev/null +++ b/lib/model/session.js @@ -0,0 +1,52 @@ +var _ = require('lodash'); +var Uuid = require('uuid'); + +var Defaults = require('../common/defaults'); + +function Session() {}; + +Session.create = function(opts) { + opts = opts || {}; + + var now = Math.floor(Date.now() / 1000); + + var x = new Session(); + + x.id = Uuid.v4(); + x.version = 1; + x.createdOn = now; + x.updatedOn = now; + x.copayerId = opts.copayerId; + x.walletId = opts.walletId; + + return x; +}; + +Session.fromObj = function(obj) { + var x = new Session(); + + x.id = obj.id; + x.version = obj.version; + x.createdOn = obj.createdOn; + x.updatedOn = obj.updatedOn; + x.copayerId = obj.copayerId; + x.walletId = obj.walletId; + + return x; +}; + +Session.prototype.toObject = function() { + return this; +}; + +Session.prototype.isValid = function() { + var now = Math.floor(Date.now() / 1000); + return (now - this.updatedOn) <= Defaults.SESSION_EXPIRATION; +}; + +Session.prototype.touch = function() { + var now = Math.floor(Date.now() / 1000); + this.updatedOn = now; +}; + +module.exports = Session;