Commit 947410e1 authored by Vladimir Baranov's avatar Vladimir Baranov

rref #5392. Improve new messages indication and message-window <-> header communication

parent 06918d29
angular.module("ulakbus.messaging") angular.module("ulakbus.messaging")
.directive('messaging', function (Generator, MessagingService, $log, $rootScope, MessagingPopup, Utils) { .directive('messaging', function (Generator, MessagingService, $log, $rootScope, MessagingPopup, Utils, $q) {
// get channel key // get channel key
function getKey (channel) { function getKey (channel) {
...@@ -30,8 +30,6 @@ angular.module("ulakbus.messaging") ...@@ -30,8 +30,6 @@ angular.module("ulakbus.messaging")
restrict: 'E', restrict: 'E',
scope: {}, scope: {},
link: function(iScope, iElem, iAttrs){ link: function(iScope, iElem, iAttrs){
var channelsMap = {};
iScope.chatAppIsHidden = true; iScope.chatAppIsHidden = true;
// reset state when user log in/log out // reset state when user log in/log out
...@@ -47,7 +45,6 @@ angular.module("ulakbus.messaging") ...@@ -47,7 +45,6 @@ angular.module("ulakbus.messaging")
function reset(){ function reset(){
iScope.selectedChannel = null; iScope.selectedChannel = null;
MessagingService.reset_current_channel();
iScope.publicChannels = []; iScope.publicChannels = [];
iScope.notificationsChannel = []; iScope.notificationsChannel = [];
iScope.directChannels = []; iScope.directChannels = [];
...@@ -82,14 +79,7 @@ angular.module("ulakbus.messaging") ...@@ -82,14 +79,7 @@ angular.module("ulakbus.messaging")
if (channel.messages){ if (channel.messages){
channel.messages.push(message); channel.messages.push(message);
} }
} else { };
// update unread counter
var ch = channelsMap[message.channel_key];
if (ch){
ch.unread += 1;
}
}
updateLastMessage(message); updateLastMessage(message);
} }
...@@ -112,13 +102,7 @@ angular.module("ulakbus.messaging") ...@@ -112,13 +102,7 @@ angular.module("ulakbus.messaging")
// FIXME: change to proper moment processing // FIXME: change to proper moment processing
// var ts = iScope.lastMessage.moment.toISOString(); // var ts = iScope.lastMessage.moment.toISOString();
var ts = iScope.lastMessage.moment.format("YYYY-MM-DDTHH:mm:ss"); var ts = iScope.lastMessage.moment.format("YYYY-MM-DDTHH:mm:ss");
MessagingService.report_last_seen_message(getKey(iScope.selectedChannel), iScope.lastMessage.key, ts).then(function(){ MessagingService.report_last_seen_message(getKey(iScope.selectedChannel), iScope.lastMessage.key, ts);
// set unread to 0 in channels list
var ch = channelsMap[getKey(iScope.selectedChannel)];
if (ch){
ch.unread = 0;
}
})
}; };
iScope.deleteConfirmation = function(title){ iScope.deleteConfirmation = function(title){
...@@ -132,20 +116,12 @@ angular.module("ulakbus.messaging") ...@@ -132,20 +116,12 @@ angular.module("ulakbus.messaging")
}; };
iScope.updateChannelsList = function(){ iScope.updateChannelsList = function(){
return MessagingService.list_channels().then(function (groupedChannels) { return MessagingService.list_channels().then(function (channels) {
var groupedChannels = channels.grouped;
iScope.publicChannels = groupedChannels[MessagingService.CHANNEL_TYPE.PUBLIC]; iScope.publicChannels = groupedChannels[MessagingService.CHANNEL_TYPE.PUBLIC];
iScope.notificationsChannel = groupedChannels[MessagingService.CHANNEL_TYPE.NOTIFICATION][0]; iScope.notificationsChannel = groupedChannels[MessagingService.CHANNEL_TYPE.NOTIFICATION][0];
iScope.directChannels = groupedChannels[MessagingService.CHANNEL_TYPE.DIRECT]; iScope.directChannels = groupedChannels[MessagingService.CHANNEL_TYPE.DIRECT];
// add all channels to channels map
for(var key in groupedChannels){
if (groupedChannels.hasOwnProperty(key)){
var channels = groupedChannels[key];
channels.forEach(function(channel){
channelsMap[channel.key] = channel;
})
}
}
console.error("CHALLL : ", channelsMap);
}); });
} }
...@@ -262,11 +238,10 @@ angular.module("ulakbus.messaging") ...@@ -262,11 +238,10 @@ angular.module("ulakbus.messaging")
function selectChannel(channelKey, silent){ function selectChannel(channelKey, silent){
if (!silent) iScope.loadingChannel = true; if (!silent) iScope.loadingChannel = true;
return MessagingService.show_channel(channelKey).then(function(result){ return MessagingService.show_channel(channelKey)
return result; .finally(function(){
}).finally(function(){ iScope.loadingChannel = false;
iScope.loadingChannel = false; })
})
} }
iScope.selectChannel = function(channel, silent){ iScope.selectChannel = function(channel, silent){
...@@ -354,11 +329,13 @@ angular.module("ulakbus.messaging") ...@@ -354,11 +329,13 @@ angular.module("ulakbus.messaging")
}); });
$rootScope.$on(MessagingService.SHOW_MESSAGING_WINDOW_EVENT, function(e, channelKey){ $rootScope.$on(MessagingService.SHOW_MESSAGING_WINDOW_EVENT, function(e, channelKey){
var showApp = $q.when();
if (iScope.chatAppIsHidden){ if (iScope.chatAppIsHidden){
iScope.showApp().then(function(){ showApp = iScope.showApp();
if (channelKey){ }
iScope.selectChannel(channelKey); if (channelKey && channelKey != getKey(iScope.selectedChannel)){
} showApp.then(function(){
iScope.selectChannel(channelKey);
}) })
} }
}) })
......
...@@ -18,6 +18,9 @@ angular.module('ulakbus.messaging', ['ui.bootstrap']) ...@@ -18,6 +18,9 @@ angular.module('ulakbus.messaging', ['ui.bootstrap'])
.factory('MessagingService', function ($q, $timeout, $compile, $log, $rootScope, Moment, WSOps, Utils) { .factory('MessagingService', function ($q, $timeout, $compile, $log, $rootScope, Moment, WSOps, Utils) {
var msg = {}; var msg = {};
var notificationsChannelKey; var notificationsChannelKey;
var channelsMap = {};
// channels loader promise
var channelsLoader;
msg.CHANNEL_TYPE = { msg.CHANNEL_TYPE = {
"PUBLIC": 15, "PUBLIC": 15,
...@@ -67,7 +70,37 @@ angular.module('ulakbus.messaging', ['ui.bootstrap']) ...@@ -67,7 +70,37 @@ angular.module('ulakbus.messaging', ['ui.bootstrap'])
if (!messagingAppIsHidden && message.channel_key == currentChannelKey){ if (!messagingAppIsHidden && message.channel_key == currentChannelKey){
return; return;
} }
unread[messageType].count += 1; checkIfInitialized().then(function(){
var channel = channelsMap[message.channel_key];
if (channel){
channel.unread += 1;
}
unread[messageType].count += 1;
})
}
function decreaseUnread(channel){
// get channel from channelsMap. Channels in channelMap has unread property
// which is updated when messages arrive
channel = channelsMap[channel.key];
if (channel && channel.unread){
var counter;
if (channel.type == msg.CHANNEL_TYPE.NOTIFICATION){
counter = unread.notifications
} else {
counter = unread.messages;
}
counter.count -= channel.unread;
if (counter.count < 0) counter.count = 0;
channel.unread = 0;
}
}
function checkIfInitialized(){
if (!channelsLoader){
return msg.list_channels()
}
return channelsLoader;
} }
// prepare message to show in UI // prepare message to show in UI
...@@ -82,21 +115,28 @@ angular.module('ulakbus.messaging', ['ui.bootstrap']) ...@@ -82,21 +115,28 @@ angular.module('ulakbus.messaging', ['ui.bootstrap'])
}; };
msg.get_notifications_channel_key = function(){ msg.get_notifications_channel_key = function(){
return notificationsChannelKey; return checkIfInitialized().then(function(){
return notificationsChannelKey;
});
}; };
msg.get_unread_counters = function(){ msg.get_unread_counters = function(){
return unread; return unread;
}; };
msg.reset_current_channel = function(){ msg.reset_state = function(){
currentChannelKey = null; currentChannelKey = null;
notificationsChannelKey = null;
channelsMap = {};
unread.messages.count = 0;
unread.notifications.count = 0;
channelsLoader = false;
} }
msg.toggle_messaging_window_visibility = function(visibility, resetCurrentChannel){ msg.toggle_messaging_window_visibility = function(visibility, resetState){
messagingAppIsHidden = !visibility; messagingAppIsHidden = !visibility;
if (resetCurrentChannel){ if (resetState){
msg.reset_current_channel(); msg.reset_state();
} }
}; };
...@@ -137,13 +177,21 @@ angular.module('ulakbus.messaging', ['ui.bootstrap']) ...@@ -137,13 +177,21 @@ angular.module('ulakbus.messaging', ['ui.bootstrap'])
var outgoing = { var outgoing = {
view: '_zops_list_channels' view: '_zops_list_channels'
}; };
return wsRequest(outgoing).then(function (data) {
console.error("channels: ", data.channels); channelsLoader = wsRequest(outgoing).then(function (data) {
var grouped = Utils.groupBy(data.channels||[], "type"); var grouped = Utils.groupBy(data.channels||[], "type");
// 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 // save notifications channel key
notificationsChannelKey = grouped[msg.CHANNEL_TYPE.NOTIFICATION][0].key; notificationsChannelKey = grouped[msg.CHANNEL_TYPE.NOTIFICATION][0].key;
return grouped;
return {grouped: grouped, channelsMap: channelsMap};
}); });
return channelsLoader;
}; };
msg.search_user = function (query) { msg.search_user = function (query) {
...@@ -264,16 +312,7 @@ angular.module('ulakbus.messaging', ['ui.bootstrap']) ...@@ -264,16 +312,7 @@ angular.module('ulakbus.messaging', ['ui.bootstrap'])
return wsRequest(outgoing).then(function(result){ return wsRequest(outgoing).then(function(result){
$log.info("Show channel ", channelKey, ": ", result); $log.info("Show channel ", channelKey, ": ", result);
// decrease unread messages for current channel // decrease unread messages for current channel
if (result.unread){ decreaseUnread(result);
var counter;
if (result.type == msg.CHANNEL_TYPE.NOTIFICATION){
counter = unread.notifications
} else {
counter = unread.messages;
}
counter.count -= result.unread;
if (counter.count < 0) counter.count = 0;
}
// save current channel key // save current channel key
currentChannelKey = result.key; currentChannelKey = result.key;
prepareMessages(result.last_messages); prepareMessages(result.last_messages);
...@@ -444,6 +483,13 @@ angular.module('ulakbus.messaging', ['ui.bootstrap']) ...@@ -444,6 +483,13 @@ angular.module('ulakbus.messaging', ['ui.bootstrap'])
increaseUnread(message, 'notifications'); increaseUnread(message, 'notifications');
}); });
// reset state on logout
$rootScope.$watch("loggedInUser", function(value){
if (!value){
msg.reset_state();
};
});
return msg; return msg;
}) })
......
...@@ -48,8 +48,10 @@ angular.module('ulakbus') ...@@ -48,8 +48,10 @@ angular.module('ulakbus')
$scope.showMessagesWindow = function(type){ $scope.showMessagesWindow = function(type){
if (type == 'notifications'){ if (type == 'notifications'){
var channelKey = MessagingService.get_notifications_channel_key(); return MessagingService.get_notifications_channel_key()
return MessagingService.show_messaging_window(channelKey); .then(function(channelKey){
return MessagingService.show_messaging_window(channelKey);
})
} }
MessagingService.show_messaging_window(); MessagingService.show_messaging_window();
} }
......
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