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