Commit b88010e4 authored by Vladimir Baranov's avatar Vladimir Baranov

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

parent 4aba22a9
......@@ -19,6 +19,7 @@ angular.module('ulakbus.messaging', ['ui.bootstrap'])
var msg = {};
var notificationsChannelKey;
var channelsMap = {};
var groupedChannels = {};
// channels loader promise
var channelsLoader;
......@@ -128,6 +129,7 @@ angular.module('ulakbus.messaging', ['ui.bootstrap'])
currentChannelKey = null;
notificationsChannelKey = null;
channelsMap = {};
groupedChannels = {};
unread.messages.count = 0;
unread.notifications.count = 0;
channelsLoader = false;
......@@ -179,16 +181,22 @@ angular.module('ulakbus.messaging', ['ui.bootstrap'])
};
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
for (var i = 0; i < data.channels.length; i++){
var channel = data.channels[i];
channelsMap[channel.key] = channel;
}
// 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;
......@@ -486,6 +494,25 @@ angular.module('ulakbus.messaging', ['ui.bootstrap'])
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
$rootScope.$watch("loggedInUser", function(value){
if (!value){
......
......@@ -162,6 +162,16 @@ angular.module('ulakbus')
task_list: function () {
// broadcast task list to task_list directive in dashboard_widget_directives.js
$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
......
......@@ -26,14 +26,16 @@ angular.module("ulakbus")
/**
* @param list {Array} Array of objects to group
* @param propName {String} property name to group array by
* @param initialObject {Object} initial object for groups setup
* @returns {Object}
*/
this.groupBy = function (list, propName) {
this.groupBy = function (list, propName, initialObject) {
if (!initialObject) initialObject = {};
return list.reduce(function(acc, item) {
(acc[item[propName]] = acc[item[propName]] || []).push(item);
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