Commit 54e0861f authored by Vladimir Baranov's avatar Vladimir Baranov

rref #5392. Commit timestamp of last seen message, add api calls for unread...

rref #5392. Commit timestamp of last seen message, add api calls for unread messages, enable notifications
parent 49671b41
......@@ -2,12 +2,13 @@ angular.module("ulakbus.messaging")
.directive('messaging', function (Generator, MessagingService, $log, $rootScope, MessagingPopup, Utils) {
// get channel key
function getKey (channel) {
if (!channel) return;
if (!angular.isObject(channel)) return channel;
var channelKey = channel.channel_key;
if (channel.hasOwnProperty('key')){
channelKey = channel.key; // direct channel
if (!channelKey && channel.hasOwnProperty('key')){
channelKey = channel.key;
}
return channelKey;
}
......@@ -36,7 +37,7 @@ angular.module("ulakbus.messaging")
var popupRootElement = $(iElem).find('.popup-placeholder');
function editChannelPopup (channel){
function editChannelPopup(channel){
return MessagingPopup.show({
templateUrl: "components/messaging/templates/create_channel.html",
rootElement: popupRootElement,
......@@ -61,7 +62,7 @@ angular.module("ulakbus.messaging")
}
function appendMessage(channel, message){
if (channel && getKey(channel) == getKey(iScope.selectedChannel)){
if (channel && getKey(message) == getKey(channel)){
if (channel.messages){
channel.messages.push(message);
}
......@@ -82,6 +83,15 @@ angular.module("ulakbus.messaging")
}
}
function reportLastSeenMessage(){
if (!iScope.lastMessage || !iScope.selectedChannel) return;
// instantly received messages haven't timestamp. Use moment
// 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);
};
iScope.deleteConfirmation = function(title){
return MessagingPopup.show({
templateUrl: "components/messaging/templates/delete_confirmation.html",
......@@ -222,13 +232,15 @@ angular.module("ulakbus.messaging")
var channelKey = getKey(channel);
selectChannel(channelKey, silent).then(function(result){
iScope.selectedChannel = result;
iScope.selectedChannel.read_only = channel.read_only;
iScope.selectedChannel.messages = result.last_messages;
updateLastMessage(channel.messages);
reportLastSeenMessage();
});
};
iScope.isChannelSelected = function(channel){
return getKey(channel) == getKey(iScope.selectedChannel);
return iScope.selectedChannel && getKey(channel) == getKey(iScope.selectedChannel);
}
iScope.sendMessage = function(content){
......
......@@ -15,8 +15,9 @@ angular.module('ulakbus.messaging', ['ui.bootstrap'])
* @name MessagingService
* @description Service handles all stuff related to messaging
*/
.factory('MessagingService', function ($q, $timeout, $sce, $location, $route, $compile, $log, $rootScope, Moment, WSOps, Utils) {
.factory('MessagingService', function ($q, $timeout, $compile, $log, $rootScope, Moment, WSOps, Utils) {
var msg = {};
var notificationsChannelKey;
msg.CHANNEL_TYPE = {
"PUBLIC": 15,
......@@ -28,7 +29,7 @@ angular.module('ulakbus.messaging', ['ui.bootstrap'])
/**
* wait until websocket will be open
*/
var deferred = $q.defer();
var deferred = $q.defer();
var dismissWatcher = $rootScope.$watch('websocketIsOpen', function(isOpen){
if (isOpen){
dismissWatcher();
......@@ -55,15 +56,22 @@ angular.module('ulakbus.messaging', ['ui.bootstrap'])
msg.prepareMessage = function(message){
if (!message.timestamp){
message.moment = Moment();
console.error("NO TS: ", message);
} else {
var ts = message.timestamp.replace(/\.0+Z$/, "");
// FIXME: process timezone properly
var ts = message.timestamp.replace("Z", "");
message.moment = Moment(ts);
}
return message;
};
msg.get_notifications_channel_key = function(){
return notificationsChannelKey;
};
/**
* API
*
* */
msg.list_channels = function list_channels (){
/**
* request channels list as below;
......@@ -94,7 +102,10 @@ angular.module('ulakbus.messaging', ['ui.bootstrap'])
};
return wsRequest(outgoing).then(function (data) {
console.error("channels: ", data.channels);
return Utils.groupBy(data.channels||[], "type");
var grouped = Utils.groupBy(data.channels||[], "type");
// save notifications channel key
notificationsChannelKey = grouped[msg.CHANNEL_TYPE.NOTIFICATION][0].key;
return grouped;
});
};
......@@ -209,14 +220,12 @@ angular.module('ulakbus.messaging', ['ui.bootstrap'])
};
msg.show_channel = function(channelKey){
var outgoing = {
view: '_zops_show_channel',
channel_key: channelKey
key: channelKey
};
return wsRequest(outgoing).then(function(result){
$log.info("Show channel ", channelKey, ": ", result);
console.error("channel: ", result);
prepareMessages(result.last_messages);
return result;
})
......@@ -234,8 +243,19 @@ angular.module('ulakbus.messaging', ['ui.bootstrap'])
})
};
msg.get_unread_messages_count = function(){
var outgoing = {
'view': '_zops_unread_count'
};
return wsRequest(outgoing).then(function(result){
$log.info("Get unread messages count: ", result);
return result;
})
};
msg.report_last_seen_message = function (channelKey, msgKey, timestamp){
var outgoing = {
view: '_zops_report_last_seen_message',
channel_key: channelKey,
msg_key: msgKey,
timestamp: timestamp
......
......@@ -6,7 +6,7 @@
<div class="side-navigation">
<ul class="channels">
<li class="title">NOTIFICATIONS <span class="badge" ng-show="notificationChannel.unread">{{notificationChannel.unread}}</span></li>
<li class="title" ng-click="selectChannel(notificationsChannel)">NOTIFICATIONS <span class="badge" ng-show="notificationsChannel.unread">{{notificationsChannel.unread}}</span></li>
</ul>
<ul class="channels">
......
......@@ -127,7 +127,6 @@
<script src="components/dashboard/dashboard_widgets_directives.js"></script>
<script src="components/messaging/messaging_service.js"></script>
<script src="components/messaging/messaging.js"></script>
<script src="components/messaging/messaging_forms.js"></script>
<script src="components/crud/crud_controller.js"></script>
<script src="components/crud/crud_widgets.js"></script>
<script src="components/debug/debug_controller.js"></script>
......
......@@ -134,7 +134,6 @@
<script src="components/dashboard/dashboard_widgets_directives.js"></script>
<script src="components/messaging/messaging_service.js"></script>
<script src="components/messaging/messaging.js"></script>
<script src="components/messaging/messaging_forms.js"></script>
<script src="components/crud/crud_controller.js"></script>
<script src="components/crud/crud_widgets.js"></script>
<script src="components/debug/debug_controller.js"></script>
......
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