Skip to content

Commit

Permalink
revert [API] Treatments with invalid IDs can be uploaded successfully n…
Browse files Browse the repository at this point in the history
  • Loading branch information
PieterGit committed Jul 4, 2018
1 parent 59d5279 commit 11d6237
Showing 1 changed file with 34 additions and 40 deletions.
74 changes: 34 additions & 40 deletions lib/server/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var levels = require('../levels');
var times = require('../times');
var calcData = require('../data/calcdelta');
var ObjectID = require('mongodb').ObjectID;

function init (env, ctx, server) {

function websocket ( ) {
Expand All @@ -17,15 +17,15 @@ function init (env, ctx, server) {
var log_reset = '\x1B[0m';
var LOG_WS = log_green + 'WS: ' + log_reset;
var LOG_DEDUP = log_magenta + 'DEDUPE: ' + log_reset;

var io;
var watchers = 0;
var lastData = {};
var lastProfileSwitch = null;

// TODO: this would be better to have somehow integrated/improved
var supportedCollections = {
'treatments' : env.treatments_collection,
var supportedCollections = {
'treatments' : env.treatments_collection,
'entries': env.entries_collection,
'devicestatus': env.devicestatus_collection,
'profile': env.profile_collection,
Expand All @@ -41,10 +41,10 @@ function init (env, ctx, server) {
versionNum = 10000 * parseInt(verParse[1]) + 100 * parseInt(verParse[2]) + 1 * parseInt(verParse[3]) ;
}
var apiEnabled = env.api_secret ? true : false;

var activeProfile = ctx.ddata.lastProfileFromSwitch;

var info = {
var info = {
status: 'ok'
, name: env.name
, version: env.version
Expand All @@ -56,7 +56,7 @@ function init (env, ctx, server) {
, settings: env.settings
, extendedSettings: ctx.plugins && ctx.plugins.extendedClientSettings ? ctx.plugins.extendedClientSettings(env.extendedSettings) : {}
};

if (activeProfile) {
info.activeProfile = activeProfile;
}
Expand Down Expand Up @@ -125,7 +125,7 @@ function init (env, ctx, server) {
console.log(LOG_WS + 'Disconnected client ID: ',socket.client.id);
});


function checkConditions (action, data) {
var collection = supportedCollections[data.collection];
if (!collection) {
Expand All @@ -149,7 +149,7 @@ function init (env, ctx, server) {
return { result: 'Not permitted' };
}
}

if (action === 'dbUpdate' && !data._id) {
console.log('WS dbUpdate/dbAddnot sure abou documentati call: ', 'Missing _id', data);
return { result: 'Missing _id' };
Expand Down Expand Up @@ -179,33 +179,27 @@ function init (env, ctx, server) {
socket.on('dbUpdate', function dbUpdate (data, callback) {
console.log(LOG_WS + 'dbUpdate client ID: ', socket.client.id, ' data: ', data);
var collection = supportedCollections[data.collection];

var check = checkConditions('dbUpdate', data);
if (check) {
if (callback) {
callback( check );
}
return;
}
var id ;
try {
id = new ObjectID(data._id);
} catch (err){
console.error(err);
id = new ObjectID();
}

ctx.store.collection(collection).update(
{ '_id': id },
{ '_id': new ObjectID(data._id) },
{ $set: data.data }
);

if (callback) {
callback( { result: 'success' } );
}
ctx.bus.emit('data-received');
});

// dbUpdateUnset message
// dbUpdateUnset message
// {
// collection: treatments
// _id: 'some mongo record id'
Expand All @@ -217,27 +211,27 @@ function init (env, ctx, server) {
socket.on('dbUpdateUnset', function dbUpdateUnset (data, callback) {
console.log(LOG_WS + 'dbUpdateUnset client ID: ', socket.client.id, ' data: ', data);
var collection = supportedCollections[data.collection];

var check = checkConditions('dbUpdate', data);
if (check) {
if (callback) {
callback( check );
}
return;
}

ctx.store.collection(collection).update(
{ '_id': new ObjectID(data._id) },
{ $unset: data.data }
);

if (callback) {
callback( { result: 'success' } );
}
ctx.bus.emit('data-received');
});

// dbAdd message
// dbAdd message
// {
// collection: treatments
// data: {
Expand All @@ -249,22 +243,22 @@ function init (env, ctx, server) {
console.log(LOG_WS + 'dbAdd client ID: ', socket.client.id, ' data: ', data);
var collection = supportedCollections[data.collection];
var maxtimediff = times.mins(1).msecs;

var check = checkConditions('dbAdd', data);
if (check) {
if (callback) {
callback( check );
}
return;
}

if (data.collection === 'treatments' && !('eventType' in data.data)) {
data.data.eventType = '<none>';
}
if (!('created_at' in data.data)) {
data.data.created_at = new Date().toISOString();
}

// treatments deduping
if (data.collection === 'treatments') {
var query;
Expand All @@ -286,7 +280,7 @@ function init (env, ctx, server) {
}
return;
}

var selected = false;
var query_similiar = {
created_at: {$gte: new Date(new Date(data.data.created_at).getTime() - maxtimediff).toISOString(), $lte: new Date(new Date(data.data.created_at).getTime() + maxtimediff).toISOString()}
Expand Down Expand Up @@ -381,36 +375,36 @@ function init (env, ctx, server) {
});
}
});
// dbRemove message
// dbRemove message
// {
// collection: treatments
// _id: 'some mongo record id'
// }
socket.on('dbRemove', function dbRemove (data, callback) {
console.log(LOG_WS + 'dbRemove client ID: ', socket.client.id, ' data: ', data);
var collection = supportedCollections[data.collection];

var check = checkConditions('dbUpdate', data);
if (check) {
if (callback) {
callback( check );
}
return;
}

ctx.store.collection(collection).remove(
{ '_id': new ObjectID(data._id) }
);

if (callback) {
callback( { result: 'success' } );
}
ctx.bus.emit('data-received');
});

// Authorization message
// {
// client: 'web' | 'phone' | 'pump'
// client: 'web' | 'phone' | 'pump'
// , secret: 'secret_hash'
// [, history : history_in_hours ]
// [, status : true ]
Expand Down Expand Up @@ -453,10 +447,10 @@ function init (env, ctx, server) {
}
});
});

// Pind message
// {
// mills: <local_time_in_milliseconds>
// mills: <local_time_in_milliseconds>
// }
socket.on('nsping', function ping (message, callback) {
var clientTime = message.mills;
Expand Down Expand Up @@ -497,7 +491,7 @@ function init (env, ctx, server) {
console.info(LOG_WS + 'emitted announcement to all clients');
}
};

start( );
listeners( );

Expand Down

0 comments on commit 11d6237

Please sign in to comment.