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") ...@@ -2,12 +2,13 @@ angular.module("ulakbus.messaging")
.directive('messaging', function (Generator, MessagingService, $log, $rootScope, MessagingPopup, Utils) { .directive('messaging', function (Generator, MessagingService, $log, $rootScope, MessagingPopup, Utils) {
// get channel key
function getKey (channel) { function getKey (channel) {
if (!channel) return; if (!channel) return;
if (!angular.isObject(channel)) return channel; if (!angular.isObject(channel)) return channel;
var channelKey = channel.channel_key; var channelKey = channel.channel_key;
if (channel.hasOwnProperty('key')){ if (!channelKey && channel.hasOwnProperty('key')){
channelKey = channel.key; // direct channel channelKey = channel.key;
} }
return channelKey; return channelKey;
} }
...@@ -36,7 +37,7 @@ angular.module("ulakbus.messaging") ...@@ -36,7 +37,7 @@ angular.module("ulakbus.messaging")
var popupRootElement = $(iElem).find('.popup-placeholder'); var popupRootElement = $(iElem).find('.popup-placeholder');
function editChannelPopup (channel){ function editChannelPopup(channel){
return MessagingPopup.show({ return MessagingPopup.show({
templateUrl: "components/messaging/templates/create_channel.html", templateUrl: "components/messaging/templates/create_channel.html",
rootElement: popupRootElement, rootElement: popupRootElement,
...@@ -61,7 +62,7 @@ angular.module("ulakbus.messaging") ...@@ -61,7 +62,7 @@ angular.module("ulakbus.messaging")
} }
function appendMessage(channel, message){ function appendMessage(channel, message){
if (channel && getKey(channel) == getKey(iScope.selectedChannel)){ if (channel && getKey(message) == getKey(channel)){
if (channel.messages){ if (channel.messages){
channel.messages.push(message); channel.messages.push(message);
} }
...@@ -82,6 +83,15 @@ angular.module("ulakbus.messaging") ...@@ -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){ iScope.deleteConfirmation = function(title){
return MessagingPopup.show({ return MessagingPopup.show({
templateUrl: "components/messaging/templates/delete_confirmation.html", templateUrl: "components/messaging/templates/delete_confirmation.html",
...@@ -222,13 +232,15 @@ angular.module("ulakbus.messaging") ...@@ -222,13 +232,15 @@ angular.module("ulakbus.messaging")
var channelKey = getKey(channel); var channelKey = getKey(channel);
selectChannel(channelKey, silent).then(function(result){ selectChannel(channelKey, silent).then(function(result){
iScope.selectedChannel = result; iScope.selectedChannel = result;
iScope.selectedChannel.read_only = channel.read_only;
iScope.selectedChannel.messages = result.last_messages; iScope.selectedChannel.messages = result.last_messages;
updateLastMessage(channel.messages); updateLastMessage(channel.messages);
reportLastSeenMessage();
}); });
}; };
iScope.isChannelSelected = function(channel){ iScope.isChannelSelected = function(channel){
return getKey(channel) == getKey(iScope.selectedChannel); return iScope.selectedChannel && getKey(channel) == getKey(iScope.selectedChannel);
} }
iScope.sendMessage = function(content){ iScope.sendMessage = function(content){
......
...@@ -15,8 +15,9 @@ angular.module('ulakbus.messaging', ['ui.bootstrap']) ...@@ -15,8 +15,9 @@ angular.module('ulakbus.messaging', ['ui.bootstrap'])
* @name MessagingService * @name MessagingService
* @description Service handles all stuff related to messaging * @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 msg = {};
var notificationsChannelKey;
msg.CHANNEL_TYPE = { msg.CHANNEL_TYPE = {
"PUBLIC": 15, "PUBLIC": 15,
...@@ -28,7 +29,7 @@ angular.module('ulakbus.messaging', ['ui.bootstrap']) ...@@ -28,7 +29,7 @@ angular.module('ulakbus.messaging', ['ui.bootstrap'])
/** /**
* wait until websocket will be open * wait until websocket will be open
*/ */
var deferred = $q.defer(); var deferred = $q.defer();
var dismissWatcher = $rootScope.$watch('websocketIsOpen', function(isOpen){ var dismissWatcher = $rootScope.$watch('websocketIsOpen', function(isOpen){
if (isOpen){ if (isOpen){
dismissWatcher(); dismissWatcher();
...@@ -55,15 +56,22 @@ angular.module('ulakbus.messaging', ['ui.bootstrap']) ...@@ -55,15 +56,22 @@ angular.module('ulakbus.messaging', ['ui.bootstrap'])
msg.prepareMessage = function(message){ msg.prepareMessage = function(message){
if (!message.timestamp){ if (!message.timestamp){
message.moment = Moment(); message.moment = Moment();
console.error("NO TS: ", message);
} else { } else {
var ts = message.timestamp.replace(/\.0+Z$/, ""); var ts = message.timestamp.replace("Z", "");
// FIXME: process timezone properly
message.moment = Moment(ts); message.moment = Moment(ts);
} }
return message; return message;
}; };
msg.get_notifications_channel_key = function(){
return notificationsChannelKey;
};
/**
* API
*
* */
msg.list_channels = function list_channels (){ msg.list_channels = function list_channels (){
/** /**
* request channels list as below; * request channels list as below;
...@@ -94,7 +102,10 @@ angular.module('ulakbus.messaging', ['ui.bootstrap']) ...@@ -94,7 +102,10 @@ angular.module('ulakbus.messaging', ['ui.bootstrap'])
}; };
return wsRequest(outgoing).then(function (data) { return wsRequest(outgoing).then(function (data) {
console.error("channels: ", data.channels); 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']) ...@@ -209,14 +220,12 @@ angular.module('ulakbus.messaging', ['ui.bootstrap'])
}; };
msg.show_channel = function(channelKey){ msg.show_channel = function(channelKey){
var outgoing = { var outgoing = {
view: '_zops_show_channel', view: '_zops_show_channel',
channel_key: channelKey key: channelKey
}; };
return wsRequest(outgoing).then(function(result){ return wsRequest(outgoing).then(function(result){
$log.info("Show channel ", channelKey, ": ", result); $log.info("Show channel ", channelKey, ": ", result);
console.error("channel: ", result);
prepareMessages(result.last_messages); prepareMessages(result.last_messages);
return result; return result;
}) })
...@@ -234,8 +243,19 @@ angular.module('ulakbus.messaging', ['ui.bootstrap']) ...@@ -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){ msg.report_last_seen_message = function (channelKey, msgKey, timestamp){
var outgoing = { var outgoing = {
view: '_zops_report_last_seen_message',
channel_key: channelKey, channel_key: channelKey,
msg_key: msgKey, msg_key: msgKey,
timestamp: timestamp timestamp: timestamp
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
<div class="side-navigation"> <div class="side-navigation">
<ul class="channels"> <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>
<ul class="channels"> <ul class="channels">
......
...@@ -127,7 +127,6 @@ ...@@ -127,7 +127,6 @@
<script src="components/dashboard/dashboard_widgets_directives.js"></script> <script src="components/dashboard/dashboard_widgets_directives.js"></script>
<script src="components/messaging/messaging_service.js"></script> <script src="components/messaging/messaging_service.js"></script>
<script src="components/messaging/messaging.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_controller.js"></script>
<script src="components/crud/crud_widgets.js"></script> <script src="components/crud/crud_widgets.js"></script>
<script src="components/debug/debug_controller.js"></script> <script src="components/debug/debug_controller.js"></script>
......
...@@ -134,7 +134,6 @@ ...@@ -134,7 +134,6 @@
<script src="components/dashboard/dashboard_widgets_directives.js"></script> <script src="components/dashboard/dashboard_widgets_directives.js"></script>
<script src="components/messaging/messaging_service.js"></script> <script src="components/messaging/messaging_service.js"></script>
<script src="components/messaging/messaging.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_controller.js"></script>
<script src="components/crud/crud_widgets.js"></script> <script src="components/crud/crud_widgets.js"></script>
<script src="components/debug/debug_controller.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