Commit 607813a6 authored by Vladimir Baranov's avatar Vladimir Baranov

rref #5392. Process new channel and user status change messages

parent 98bc7944
...@@ -19,6 +19,7 @@ angular.module('ulakbus.messaging', ['ui.bootstrap']) ...@@ -19,6 +19,7 @@ angular.module('ulakbus.messaging', ['ui.bootstrap'])
var msg = {}; var msg = {};
var notificationsChannelKey; var notificationsChannelKey;
var channelsMap = {}; var channelsMap = {};
var groupedChannels = {};
// channels loader promise // channels loader promise
var channelsLoader; var channelsLoader;
...@@ -128,6 +129,7 @@ angular.module('ulakbus.messaging', ['ui.bootstrap']) ...@@ -128,6 +129,7 @@ angular.module('ulakbus.messaging', ['ui.bootstrap'])
currentChannelKey = null; currentChannelKey = null;
notificationsChannelKey = null; notificationsChannelKey = null;
channelsMap = {}; channelsMap = {};
groupedChannels = {};
unread.messages.count = 0; unread.messages.count = 0;
unread.notifications.count = 0; unread.notifications.count = 0;
channelsLoader = false; channelsLoader = false;
...@@ -179,16 +181,22 @@ angular.module('ulakbus.messaging', ['ui.bootstrap']) ...@@ -179,16 +181,22 @@ angular.module('ulakbus.messaging', ['ui.bootstrap'])
}; };
channelsLoader = wsRequest(outgoing).then(function (data) { channelsLoader = wsRequest(outgoing).then(function (data) {
var grouped = Utils.groupBy(data.channels||[], "type"); var initialGroups = {};
initialGroups[msg.CHANNEL_TYPE.PUBLIC] = [];
initialGroups[msg.CHANNEL_TYPE.DIRECT] = [];
initialGroups[msg.CHANNEL_TYPE.NOTIFICATION] = [];
groupedChannels = Utils.groupBy(data.channels||[], "type", initialGroups);
// add all channels to channels map // add all channels to channels map
for (var i = 0; i < data.channels.length; i++){ for (var i = 0; i < data.channels.length; i++){
var channel = data.channels[i]; var channel = data.channels[i];
channelsMap[channel.key] = channel; channelsMap[channel.key] = channel;
} }
// save notifications channel key // save notifications channel key
notificationsChannelKey = grouped[msg.CHANNEL_TYPE.NOTIFICATION][0].key; notificationsChannelKey = groupedChannels[msg.CHANNEL_TYPE.NOTIFICATION][0].key;
return {grouped: grouped, channelsMap: channelsMap}; return {grouped: groupedChannels, channelsMap: channelsMap};
}); });
return channelsLoader; return channelsLoader;
...@@ -486,6 +494,25 @@ angular.module('ulakbus.messaging', ['ui.bootstrap']) ...@@ -486,6 +494,25 @@ angular.module('ulakbus.messaging', ['ui.bootstrap'])
increaseUnread(message, 'notifications'); increaseUnread(message, 'notifications');
}); });
$rootScope.$on("channel_change", function(e, action, channel){
checkIfInitialized().then(function(){
if (action == 'add'){
var group = groupedChannels[channel.type];
if (!channelsMap[channel.key]){
channelsMap[channel.key] = channel;
}
return group.push(channel);
}
if (action == 'status'){
var localChannel = channelsMap[channel.channel_key];
if (localChannel){
localChannel.is_online = channel.is_online;
}
}
})
});
// reset state on logout // reset state on logout
$rootScope.$watch("loggedInUser", function(value){ $rootScope.$watch("loggedInUser", function(value){
if (!value){ if (!value){
......
...@@ -160,6 +160,16 @@ angular.module('ulakbus') ...@@ -160,6 +160,16 @@ angular.module('ulakbus')
task_list: function () { task_list: function () {
// broadcast task list to task_list directive in dashboard_widget_directives.js // broadcast task list to task_list directive in dashboard_widget_directives.js
$rootScope.$broadcast('task_list', msg_data["task_list"]); $rootScope.$broadcast('task_list', msg_data["task_list"]);
},
channel_subscription: function(){
$timeout(function(){
$rootScope.$broadcast('channel_change', 'add', msg_data);
})
},
user_status: function(){
$timeout(function(){
$rootScope.$broadcast('channel_change', 'status', msg_data);
})
} }
}; };
// do_action is the dispatcher function for incoming events // do_action is the dispatcher function for incoming events
...@@ -182,7 +192,7 @@ angular.module('ulakbus') ...@@ -182,7 +192,7 @@ angular.module('ulakbus')
} }
do_action(msg_data, msg_data.cmd); do_action(msg_data, msg_data.cmd);
$log.info("MESSAGE:", event, "Data:", JSON.parse(event.data)); $log.info("MESSAGE:", msg_data.cmd, event, "Data:", JSON.parse(event.data));
}; };
wsOps.onError = function (evt) { wsOps.onError = function (evt) {
$log.error("ERROR :: " + evt); $log.error("ERROR :: " + evt);
......
...@@ -26,14 +26,16 @@ angular.module("ulakbus") ...@@ -26,14 +26,16 @@ angular.module("ulakbus")
/** /**
* @param list {Array} Array of objects to group * @param list {Array} Array of objects to group
* @param propName {String} property name to group array by * @param propName {String} property name to group array by
* @param initialObject {Object} initial object for groups setup
* @returns {Object} * @returns {Object}
*/ */
this.groupBy = function (list, propName) { this.groupBy = function (list, propName, initialObject) {
if (!initialObject) initialObject = {};
return list.reduce(function(acc, item) { return list.reduce(function(acc, item) {
(acc[item[propName]] = acc[item[propName]] || []).push(item); (acc[item[propName]] = acc[item[propName]] || []).push(item);
return acc; return acc;
}, {}); }, initialObject);
}; };
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment