Issues (208)

server/profile.js (2 issues)

1
var express = require('express');
2
var extend  = require('extend');
3
4
// ------------------------------------------
5
//  CONSTRUCTOR
6
// ------------------------------------------
7
8
var init = function(){
9
  info('Starting ProfileManager ...');
10
  
11
  // Expose properties to global
12
  var Profile = {}
13
  global.Profile = Profile;
14
  
15
  return ProfileManager;
16
}
17
18
// ------------------------------------------
19
//  ROUTER
20
// ------------------------------------------
21
22
23
var updateProfile = function(req, res, next) { 
24
  
25
  var name = req.params.name;
26
  var query   = {}; 
27
  var profile = {};
28
29
  var keys = Object.keys(req.query);
30
  for(var i=0 ; i < keys.length ; i++){
31
    var key = keys[i];
32
    var value = req.query[key];
33
    
34
    if (!key.startsWith('profile_')){
35
      query[key] = value;
36
      continue; 
37
    }
38
    
39
    profile[key.substring(8)] = parse(value);
40
  }
41
42
  if (profile.face){
43
    
44
    // Merge previous with new profile
45
    var previous = Profile[profile.face];
46
    if (previous){
47
      extend(true, previous, profile);
48
      profile = previous;
49
    }
50
    
51
    // Save profile for given face
52
    Profile[profile.face] = profile;
53
  }
54
   
55
  // Local profile
56
  req.query = query;
57
  req.query.profile = profile;
58
  Profile.last = profile;
59
  
60
  next();
61
}
62
63
var parse = function(str){
64
  if (str === 'true')  return true;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
65
  if (str === 'false') return false;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
66
  var num = parseInt(str);
67
  return isNaN(num) ? str : num;
68
}
69
70
var Router = express.Router();
71
72
Router.all('/sarah/:name', updateProfile);
73
Router.all('/standby',     updateProfile);
74
Router.all('/motion',      updateProfile);
75
Router.all('/askme',       updateProfile);
76
77
78
// ------------------------------------------
79
//  PUBLIC
80
// ------------------------------------------
81
82
var ProfileManager = {
83
  'init'   : init,
84
  'Router' : Router
85
}
86
87
// Exports Manager
88
exports.init = ProfileManager.init;