Skip to content

Commit

Permalink
Few big changes
Browse files Browse the repository at this point in the history
Moved all globals to an "App" instance - now each execution of index.handler is entirely contained
Removed API cals for getting server machine identifier and player IP since we now cache all of that in the DB
Changed all references to "library 1" to a db-stored library URI - for now it just grabs the first tv library and uses that
  • Loading branch information
OverloadUT committed Jan 10, 2016
1 parent 3714d4d commit a0815eb
Show file tree
Hide file tree
Showing 15 changed files with 426 additions and 175 deletions.
28 changes: 3 additions & 25 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
/**
* @module
*/

require('dotenv').load();
var app = require('./lib/app');
var db = require('./lib/db');
var stateMachine = require('./lib/statemachine');
var User = require('./lib/user');
var Alexa = require('alexa-app');
var Plex = require('./lib/plex');
var App = require('./lib/app').App;

/**
* The main AWS Lambda handler.
Expand All @@ -24,22 +18,6 @@ exports.handler = function(event, context) {
}
}

app.skill = new Alexa.app('plex');
app.plex = new Plex.Plex();

db.initializeUserRecord(event.session.user.userId).then(function(dbuser) {
app.user = new User(dbuser);
app.plex.pinAuth.token = app.user.authtoken;

if(!app.user.authtoken) {
return stateMachine.initApp('not-authed');
} else {
return stateMachine.initApp('authed');
}
}).then(function() {
app.skill.lambda()(event, context);
}).catch(function(err) {
console.error(err);
console.error(err.stack);
});
var app = new App();
app.execute(event, context);
};
57 changes: 48 additions & 9 deletions lib/app.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,67 @@
/**
* @module app
* @module App
*/

//var Plex = require('./plex').Plex;
var AlexaSkill = require('alexa-app').app;
var User = require('./user').User;

var db = require('./db');
var stateMachine = require('./statemachine');

/**
* @name module:app
* Creates a new App object, which holds all of the various stateful objects necessary on each request
* @constructor App
* @classdesc Holds all of the various stateful objects necessary on each request
*/
app = module.exports = {
var App = function() {
var Plex = require('./plex').Plex;

/** @type {module:Plex~Plex} */
plex: null,
this.plex = new Plex(this);
/** @type {AlexaSkill} */
this.skill = new AlexaSkill('plex');

// TODO figure out how to properly jsdoc this so that it is recognized as an alexa-app.app object
/** @type {Object} */
skill: null,
/** @type {module:User~User} */
this.user = null;

/**
* How confident we need to be when trying to figure out which show a user is talkijng about
* @const
*/
CONFIDICE_CONFIRM_THRESHOLD: 0.4,
this.CONFIDICE_CONFIRM_THRESHOLD = 0.4;

/**
* The invocation name used for this app. Used in many responses so put here in case it changes.
* @const
* @type {string}
*/
INVOCATION_NAME: "the home theater"
this.INVOCATION_NAME = "the home theater";
};

App.prototype.execute = function(event, callbacks) {
var context = this;
db.initializeUserRecord(event.session.user.userId).then(function(dbuser) {
context.user = new User(context, dbuser);
context.plex.pinAuth.token = context.user.authtoken;

if(!context.user.authtoken) {
return stateMachine.initSkillState(context, 'not-authed');
} else {
return stateMachine.initSkillState(context, 'authed');
}
}).then(function() {
// Pass off the rest of the execution to the alexa-plex module which handles running intents
// TODO: we're doing so much of our own app framework now that it might make sense to just take over the last few things that alexa-app is handling?
// HUGE HACK to make this App object available to the Intent handlers running inside the alexa-app module. Another reason why the above note makes sense
event._plex_app = context;
context.skill.lambda()(event, callbacks);
}).catch(function(err) {
console.error(err);
console.error(err.stack);
});
};

module.exports = {
App: App
};
35 changes: 35 additions & 0 deletions lib/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,43 @@ function updateUserPlayer(user, player) {
return deferred.promise;
}

function updateUserLibraries(user, libraries) {
var dynamodbDoc = new AWS.DynamoDB.DocumentClient();
var deferred = Q.defer();

var userid = user;
if(typeof user === 'object') {
userid = user.dbobject.userid;
}

var updateParams = {
TableName: TABLE_NAME,
Key: { "userid": userid },
UpdateExpression: "set libraries = :l",
ExpressionAttributeValues:{
":l":libraries
},
ReturnValues:"ALL_NEW"
};

dynamodbDoc.update(updateParams, function(err, data) {
if (err) {
return deferred.reject(err);
}

if(typeof user === 'object') {
user.dbobject = data.Attributes;
}

deferred.fulfill(data);
});

return deferred.promise;
}

module.exports.initializeUserRecord = initializeUserRecord;
module.exports.updatePin = updatePin;
module.exports.updateAuthToken = updateAuthToken;
module.exports.updateUserServer = updateUserServer;
module.exports.updateUserPlayer = updateUserPlayer;
module.exports.updateUserLibraries = updateUserLibraries;
12 changes: 10 additions & 2 deletions lib/plex.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"use strict";
var PlexAPI = require('plex-api');
var PlexPinAuth = require('plex-api-pinauth');
var app = require('./app');

require('dotenv').load('../');

Expand All @@ -27,9 +26,16 @@ var PLEX_APP_CONFIG = {
* @constructor Plex
* @classdesc A container for the multiple stateful plex-api objects needed for the app
*/
var Plex = function() {
var Plex = function(app) {
var context = this;

/**
* The App object for this specific request
* @private
* @member {module:App~App}
*/
this._app = app;

/**
* The stateful PlexPinAuth object that provides handy PIN auth functions
* @member {PlexPinAuth}
Expand Down Expand Up @@ -103,6 +109,8 @@ Plex.prototype.initializeWebApi = function() {
* @returns {Boolean} true if creation of the API object was successful
*/
Plex.prototype.initializeServerApi = function() {
var app = this._app;

if(!app.user.server) {
return false;
}
Expand Down
Loading

0 comments on commit a0815eb

Please sign in to comment.