fixes #108: avoid processing our own messages

This commit is contained in:
Davide Alberani 2016-04-24 16:03:49 +02:00
parent 86f5a109a7
commit 756a776eba
4 changed files with 34 additions and 12 deletions

View file

@ -28,8 +28,10 @@ var eventManApp = angular.module('eventManApp', [
/* Add some utilities to the global scope. */
eventManApp.run(['$rootScope', '$state', '$stateParams',
function($rootScope, $state, $stateParams) {
eventManApp.run(['$rootScope', '$state', '$stateParams', '$log',
function($rootScope, $state, $stateParams, $log) {
$rootScope.app_uuid = guid();
$log.debug('App UUID: ' + $rootScope.app_uuid);
$rootScope.$state = $state;
$rootScope.$stateParams = $stateParams;
}]

View file

@ -96,8 +96,8 @@ eventManControllers.controller('EventsListCtrl', ['$scope', 'Event', '$modal', '
);
eventManControllers.controller('EventDetailsCtrl', ['$scope', '$state', 'Event', 'Person', 'EventUpdates', '$stateParams', 'Setting', '$log', '$translate',
function ($scope, $state, Event, Person, EventUpdates, $stateParams, Setting, $log, $translate) {
eventManControllers.controller('EventDetailsCtrl', ['$scope', '$state', 'Event', 'Person', 'EventUpdates', '$stateParams', 'Setting', '$log', '$translate', '$rootScope',
function ($scope, $state, Event, Person, EventUpdates, $stateParams, Setting, $log, $translate, $rootScope) {
$scope.personsOrder = ["name", "surname"];
$scope.countAttendees = 0;
$scope.message = {};
@ -127,7 +127,11 @@ eventManControllers.controller('EventDetailsCtrl', ['$scope', '$state', 'Event',
return;
}
var data = $scope.EventUpdates.data.update;
$log.debug('received ' + data.action + ' action from websocket');
$log.debug('received ' + data.action + ' action from websocket source ' + data.uuid);
if ($rootScope.app_uuid == data.uuid) {
$log.debug('do not process our own message');
return false;
}
if (!$scope.event.persons) {
$scope.event.persons = [];
}

View file

@ -4,8 +4,8 @@
var eventManServices = angular.module('eventManServices', ['ngResource']);
eventManServices.factory('Event', ['$resource',
function($resource) {
eventManServices.factory('Event', ['$resource', '$rootScope',
function($resource, $rootScope) {
return $resource('events/:id', {id: '@_id', person_id: '@person_id'}, {
all: {
@ -43,6 +43,7 @@ eventManServices.factory('Event', ['$resource',
method: 'PUT',
isArray: false,
url: 'events/:id/persons/:person_id',
params: {uuid: $rootScope.app_uuid},
transformResponse: function(data, headers) {
return angular.fromJson(data);
}
@ -52,6 +53,7 @@ eventManServices.factory('Event', ['$resource',
method: 'POST',
isArray: false,
url: 'events/:id/persons/:person_id',
params: {uuid: $rootScope.app_uuid},
transformResponse: function(data, headers) {
return angular.fromJson(data);
}
@ -61,6 +63,7 @@ eventManServices.factory('Event', ['$resource',
method: 'DELETE',
isArray: false,
url: 'events/:_id/persons/:person_id',
params: {uuid: $rootScope.app_uuid},
transformResponse: function(data, headers) {
return angular.fromJson(data);
}

View file

@ -66,6 +66,16 @@ class BaseHandler(tornado.web.RequestHandler):
arguments = property(lambda self: dict([(k, v[0])
for k, v in self.request.arguments.iteritems()]))
# A property to access both the UUID and the clean arguments.
@property
def uuid_arguments(self):
uuid = None
arguments = self.arguments
if 'uuid' in arguments:
uuid = arguments['uuid']
del arguments['uuid']
return uuid, arguments
_bool_convert = {
'0': False,
'n': False,
@ -400,6 +410,7 @@ class EventsHandler(CollectionHandler):
def handle_post_persons(self, id_, person_id, data):
# Add a person to the list of persons registered at this event.
uuid, arguments = self.uuid_arguments
self._clean_dict(data)
data['seq'] = self.get_next_seq('event_%s_persons' % id_)
data['seq_hex'] = '%06X' % data['seq']
@ -407,7 +418,7 @@ class EventsHandler(CollectionHandler):
{'_id': id_, 'persons.person_id': person_id})
if '_id' in data:
del data['_id']
ret = {'action': 'add', 'person_id': person_id, 'person': data}
ret = {'action': 'add', 'person_id': person_id, 'person': data, 'uuid': uuid}
if not doc:
merged, doc = self.db.update('events',
{'_id': id_},
@ -420,7 +431,8 @@ class EventsHandler(CollectionHandler):
def handle_put_persons(self, id_, person_id, data):
# Update an existing entry for a person registered at this event.
self._clean_dict(data)
query = dict([('persons.%s' % k, v) for k, v in self.arguments.iteritems()])
uuid, arguments = self.uuid_arguments
query = dict([('persons.%s' % k, v) for k, v in arguments.iteritems()])
query['_id'] = id_
if person_id is not None:
query['persons.person_id'] = person_id
@ -452,16 +464,17 @@ class EventsHandler(CollectionHandler):
if new_person_data.get('attended'):
self.run_triggers('attends', stdin_data=stdin_data, env=env)
ret = {'action': 'update', 'person_id': person_id, 'person': new_person_data}
ret = {'action': 'update', 'person_id': person_id, 'person': new_person_data, 'uuid': uuid}
if old_person_data != new_person_data:
self.send_ws_message('event/%s/updates' % id_, json.dumps(ret))
return ret
def handle_delete_persons(self, id_, person_id):
# Remove a specific person from the list of persons registered at this event.
uuid, arguments = self.uuid_arguments
doc = self.db.query('events',
{'_id': id_, 'persons.person_id': person_id})
ret = {'action': 'delete', 'person_id': person_id}
ret = {'action': 'delete', 'person_id': person_id, 'uuid': uuid}
if doc:
merged, doc = self.db.update('events',
{'_id': id_},
@ -686,7 +699,7 @@ def run():
# database backend connector
db_connector = backend.EventManDB(url=options.mongo_url, dbName=options.db_name)
init_params = dict(db=db_connector, data_dir=options.data_dir, listen_port=options.port,
authentication=options.authentication)
authentication=options.authentication, logger=logger)
# If not present, we store a user 'admin' with password 'eventman' into the database.
if not db_connector.query('users', {'username': 'admin'}):