From 11d62377bfe2837cb38f86361f637f93500f2796 Mon Sep 17 00:00:00 2001 From: PieterGit Date: Wed, 4 Jul 2018 23:04:47 +0200 Subject: [PATCH] revert [API] Treatments with invalid IDs can be uploaded successfully #3468 https://github.com/nightscout/cgm-remote-monitor/pull/3464 --- lib/server/websocket.js | 74 +++++++++++++++++++---------------------- 1 file changed, 34 insertions(+), 40 deletions(-) diff --git a/lib/server/websocket.js b/lib/server/websocket.js index aa2eaad87039..5e40928b13cf 100644 --- a/lib/server/websocket.js +++ b/lib/server/websocket.js @@ -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 ( ) { @@ -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, @@ -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 @@ -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; } @@ -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) { @@ -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' }; @@ -179,7 +179,7 @@ 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) { @@ -187,25 +187,19 @@ function init (env, ctx, server) { } 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' @@ -217,7 +211,7 @@ 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) { @@ -225,19 +219,19 @@ function init (env, ctx, server) { } 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: { @@ -249,7 +243,7 @@ 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) { @@ -257,14 +251,14 @@ function init (env, ctx, server) { } return; } - + if (data.collection === 'treatments' && !('eventType' in data.data)) { data.data.eventType = ''; } if (!('created_at' in data.data)) { data.data.created_at = new Date().toISOString(); } - + // treatments deduping if (data.collection === 'treatments') { var query; @@ -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()} @@ -381,7 +375,7 @@ function init (env, ctx, server) { }); } }); - // dbRemove message + // dbRemove message // { // collection: treatments // _id: 'some mongo record id' @@ -389,7 +383,7 @@ function init (env, ctx, server) { 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) { @@ -397,20 +391,20 @@ function init (env, ctx, server) { } 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 ] @@ -453,10 +447,10 @@ function init (env, ctx, server) { } }); }); - + // Pind message // { - // mills: + // mills: // } socket.on('nsping', function ping (message, callback) { var clientTime = message.mills; @@ -497,7 +491,7 @@ function init (env, ctx, server) { console.info(LOG_WS + 'emitted announcement to all clients'); } }; - + start( ); listeners( );