Skip to content

Commit

Permalink
Redo notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
remdex committed Jan 27, 2017
1 parent dee8a9d commit ab8a5b6
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 113 deletions.
50 changes: 42 additions & 8 deletions lhc_web/design/defaulttheme/js/angular.lhc.js
Expand Up @@ -537,38 +537,71 @@ lhcAppControllers.controller('LiveHelperChatCtrl',['$scope','$http','$location',
ee.emitEvent('eventLoadChatList', [data, $scope, _that]);

if (typeof data.items_processed == 'undefined') {

var notifiedChatsIds = [];

angular.forEach(data.result, function(item, key){
$scope[key] = item;
if ( item.last_id_identifier ) {
if (lhinst.trackLastIDS[item.last_id_identifier] == undefined ) {
lhinst.trackLastIDS[item.last_id_identifier] = parseInt(item.last_id);

if (typeof item.uid != 'undefined') {
lhinst.trackLastIDSUser[item.last_id_identifier] = parseInt(item.uid);
}

} else if (lhinst.trackLastIDS[item.last_id_identifier] < parseInt(item.last_id)) {
lhinst.trackLastIDS[item.last_id_identifier] = parseInt(item.last_id);
if (lastNotifiedId != lhinst.trackLastIDS[item.last_id_identifier]) {

if (lastNotifiedId != lhinst.trackLastIDS[item.last_id_identifier] && (parseInt(item.uid) == 0 || (parseInt(item.uid) > 0 && lhinst.trackLastIDSUser[item.last_id_identifier] != item.uid))) {
lastNotifiedId = lhinst.trackLastIDS[item.last_id_identifier];
lhinst.playSoundNewAction(item.last_id_identifier,parseInt(item.last_id),(item.nick ? item.nick : 'Live Help'),(item.msg ? item.msg : confLH.transLation.new_chat));
_that.lastidEvent = parseInt(item.last_id);

if (notifiedChatsIds.indexOf(item.last_id) === -1) {
lhinst.playSoundNewAction(item.last_id_identifier,parseInt(item.last_id),(item.nick ? item.nick : 'Live Help'),(item.msg ? item.msg : confLH.transLation.new_chat), item.nt, item.uid);
notifiedChatsIds.push(item.last_id);
}

lhinst.trackLastIDSUser[item.last_id_identifier] = parseInt(item.uid);
}

} else if (lhinst.trackLastIDS[item.last_id_identifier] > parseInt(item.last_id)) {
lhinst.trackLastIDS[item.last_id_identifier] = parseInt(item.last_id);
};
lhinst.trackLastIDSUser[item.last_id_identifier] = parseInt(item.uid);
} else if (lhinst.trackLastIDS[item.last_id_identifier] == parseInt(item.last_id) && typeof item.uid != 'undefined' && lhinst.trackLastIDSUser[item.last_id_identifier] != parseInt(item.uid)) {

if (typeof lhinst.notificationsArray[item.last_id] != 'undefined') {
lhinst.notificationsArray[item.last_id].close();
delete lhinst.notificationsArray[item.last_id];
}

if (notifiedChatsIds.indexOf(item.last_id) === -1) {
_that.lastidEvent = parseInt(item.last_id);
lastNotifiedId = lhinst.trackLastIDS[item.last_id_identifier];
lhinst.playSoundNewAction(item.last_id_identifier,parseInt(item.last_id),(item.nick ? item.nick : 'Live Help'),(item.msg ? item.msg : confLH.transLation.new_chat), item.nt, item.uid);
notifiedChatsIds.push(item.last_id);
}

lhinst.trackLastIDSUser[item.last_id_identifier] = parseInt(item.uid);
}

if (item.last_id == 0) {
lhinst.trackLastIDS[item.last_id_identifier] = 0;
lhinst.trackLastIDSUser[item.last_id_identifier] = lhinst.trackLastIDS[item.last_id_identifier] = 0;
};

if (parseInt(item.last_id) > 0 && _that.lastidEvent == item.last_id) {
hasPendingItems = true;
};

};
});
});


} else {
hasPendingItems = data.items_processed_has_pending_items;
lastNotifiedId = data.items_processed_last_notified_id;
}

if (hasPendingItems == false) {
if (hasPendingItems == false) {
lhinst.hideNotifications();
};

Expand All @@ -582,7 +615,7 @@ lhcAppControllers.controller('LiveHelperChatCtrl',['$scope','$http','$location',

if (typeof data.fs !== 'undefined' && data.fs.length > 0) {
angular.forEach(data.fs, function(item, key) {
lhinst.playSoundNewAction('pending_transfered',parseInt(item.id),(item.nick ? item.nick : 'Live Help'), confLH.transLation.transfered);
lhinst.playSoundNewAction('pending_transfered',parseInt(item.id),(item.nick ? item.nick : 'Live Help'), confLH.transLation.transfered, item.nt, item.uid);
});
}

Expand All @@ -601,6 +634,7 @@ lhcAppControllers.controller('LiveHelperChatCtrl',['$scope','$http','$location',
};

},function(error){
console.log(error);
$scope.timeoutControl = setTimeout(function(){
$scope.loadChatList();
},confLH.back_office_sinterval);
Expand Down
2 changes: 1 addition & 1 deletion lhc_web/design/defaulttheme/js/angular.lhc.min.js

Large diffs are not rendered by default.

159 changes: 65 additions & 94 deletions lhc_web/design/defaulttheme/js/lh.js
Expand Up @@ -79,13 +79,15 @@ function lh(){
};

this.trackLastIDS = {};
this.trackLastIDSUser = {};

// Chats currently under synchronization
this.chatsSynchronising = [];
this.chatsSynchronisingMsg = [];

// Notifications array
this.notificationsArray = [];
this.notificationsArrayMap = [];

this.speechHandler = false;

Expand Down Expand Up @@ -1478,103 +1480,76 @@ function lh(){

this.showNewMessageNotification = function(chat_id,message,nick) {
try {
if (window.webkitNotifications || window.Notification) {
if (focused == false) {
if (typeof this.notificationsArrayMessages[chat_id] !== 'undefined') {
if (window.webkitNotifications) {
this.notificationsArrayMessages[chat_id].cancel();
} else {
this.notificationsArrayMessages[chat_id].close();
}
};
if (window.webkitNotifications) {
var havePermission = window.webkitNotifications.checkPermission();
if (havePermission == 0) {
// 0 is PERMISSION_ALLOWED
var notification = window.webkitNotifications.createNotification(
WWW_DIR_JAVASCRIPT_FILES_NOTIFICATION + '/notification.png',
nick,
message
);
notification.onclick = function () {
window.focus();
notification.cancel();
};
notification.show();
this.notificationsArrayMessages[chat_id] = notification;
}
} else if(window.Notification) {
if (window.Notification.permission == 'granted') {
var notification = new Notification(nick, { icon: WWW_DIR_JAVASCRIPT_FILES_NOTIFICATION + '/notification.png', body: message });
notification.onclick = function () {
window.focus();
notification.close();
};
this.notificationsArrayMessages[chat_id] = notification;
}
}
}

if (window.Notification && focused == false && window.Notification.permission == 'granted') {
if (typeof this.notificationsArrayMessages[chat_id] !== 'undefined') {
this.notificationsArrayMessages[chat_id].close();
};

var notification = new Notification(nick, { icon: WWW_DIR_JAVASCRIPT_FILES_NOTIFICATION + '/notification.png', body: message });
notification.onclick = function () {
window.focus();
notification.close();
};
this.notificationsArrayMessages[chat_id] = notification;
this.scheduleNewMessageClose(notification,chat_id);
}
} catch(err) {
console.log(err);
};
};

this.playSoundNewAction = function(identifier,chat_id,nick,message) {
if (confLH.new_chat_sound_enabled == 1 && (identifier == 'pending_chat' || identifier == 'transfer_chat' || identifier == 'unread_chat' || identifier == 'pending_transfered')) {
this.scheduleNewMessageClose = function(notification, chat_id) {
var _that = this;
setTimeout(function() {
if (window.webkitNotifications) {
notification.cancel();
} else {
notification.close();
}
},10*1000);
};

this.playSoundNewAction = function(identifier,chat_id,nick,message,nt,uid) {

if (confLH.new_chat_sound_enabled == 1 && (uid == 0 || confLH.user_id == uid) && $('#online-offline-user').text() == 'flash_on' && (identifier == 'pending_chat' || identifier == 'transfer_chat' || identifier == 'unread_chat' || identifier == 'pending_transfered')) {
this.soundPlayedTimes = 0;
this.playNewChatAudio();
this.playNewChatAudio();
};

if(!$("textarea[name=ChatMessage]").is(":focus")) {
if(!$("textarea[name=ChatMessage]").is(":focus") && (uid == 0 || confLH.user_id == uid) && $('#online-offline-user').text() == 'flash_on' && (identifier == 'pending_chat' || identifier == 'transfer_chat' || identifier == 'unread_chat' || identifier == 'pending_transfered')) {
this.startBlinking();
};

var inst = this;
if ( (identifier == 'pending_chat' || identifier == 'transfer_chat' || identifier == 'unread_chat' || identifier == 'pending_transfered') && (window.webkitNotifications || window.Notification)) {

if (window.webkitNotifications) {
var havePermission = window.webkitNotifications.checkPermission();
if (havePermission == 0) {
// 0 is PERMISSION_ALLOWED
var notification = window.webkitNotifications.createNotification(
WWW_DIR_JAVASCRIPT_FILES_NOTIFICATION + '/notification.png',
nick,
message
);
notification.onclick = function () {
if (identifier == 'pending_chat' || identifier == 'unread_chat' || identifier == 'pending_transfered'){
inst.startChatNewWindow(chat_id,'ChatRequest');
} else {
inst.startChatNewWindowTransferByTransfer(chat_id);
};
notification.cancel();
};
notification.show();

if (identifier != 'pending_transfered') {
this.notificationsArray.push(notification);
}
}
} else if(window.Notification) {
if (window.Notification.permission == 'granted') {
var notification = new Notification(nick, { icon: WWW_DIR_JAVASCRIPT_FILES_NOTIFICATION + '/notification.png', body: message });

notification.onclick = function () {
if (identifier == 'pending_chat' || identifier == 'unread_chat' || identifier == 'pending_transfered') {
inst.startChatNewWindow(chat_id,'ChatRequest');
} else {
inst.startChatNewWindowTransferByTransfer(chat_id);
};
notification.close();
};

if (identifier != 'pending_transfered') {
this.notificationsArray.push(notification);
}
}
}


if ( (identifier == 'pending_chat' || identifier == 'transfer_chat' || identifier == 'unread_chat' || identifier == 'pending_transfered') && (uid == 0 || confLH.user_id == uid) && $('#online-offline-user').text() == 'flash_on' && window.Notification && window.Notification.permission == 'granted') {

var notification = new Notification(nick, { icon: WWW_DIR_JAVASCRIPT_FILES_NOTIFICATION + '/notification.png', body: message });

notification.onclick = function () {
if (identifier == 'pending_chat' || identifier == 'unread_chat' || identifier == 'pending_transfered') {
if ($('#tabs').size() > 0) {
window.focus();
inst.startChat(chat_id, $('#tabs'), nt);
} else {
inst.startChatNewWindow(chat_id,'ChatRequest');
}
} else {
inst.startChatNewWindowTransferByTransfer(chat_id);
};
notification.close();
};

if (identifier != 'pending_transfered') {
if (this.notificationsArray[chat_id] !== 'undefined') {
notification.close();
}

this.notificationsArray[chat_id] = notification;
this.notificationsArrayMap.push(this.notificationsArray[chat_id]);
};

};

if (confLH.show_alert == 1) {
Expand All @@ -1592,24 +1567,20 @@ function lh(){
};
};

this.hideNotifications = function(){

this.hideNotifications = function() {
clearTimeout(this.soundIsPlaying);

$.each(this.notificationsArray,function(i,item) {
try {
if (window.webkitNotifications) {
item.cancel();
} else {
item.close();
}

$.each(this.notificationsArrayMap,function(i,item) {
try {
item.close();
} catch(err) {
console.log(err);
};
});

// Reset array
this.notificationsArray = [];
this.notificationsArrayMap = [];
};

this.syncadmininterfacestatic = function()
Expand Down
4 changes: 2 additions & 2 deletions lhc_web/design/defaulttheme/js/lh.min.js

Large diffs are not rendered by default.

Expand Up @@ -16,6 +16,7 @@
confLH.repeat_sound = <?php echo (int)$soundData['repeat_sound']?>;
confLH.repeat_sound_delay = <?php echo (int)$soundData['repeat_sound_delay']?>;
confLH.show_alert = <?php echo (int)$soundData['show_alert']?>;
confLH.user_id = '<?php echo erLhcoreClassUser::instance()->getUserID()?>';
</script>
<script type="text/javascript" src="<?php echo erLhcoreClassDesign::designJS('vendor/jquery/jquery.min.js;vendor/bootstrap/js/bootstrap.min.js;js/modernizr.js;js/lh.min.js;js/jquery.hotkeys.min.js;js/fileupload/jquery.fileupload.min.js;js/jquery.zoom.min.js;js/datepicker.min.js;js/lh/dist/common.js;js/lh/dist/bundle.js;js/EventEmitter.min.js;js/events.js');?>"></script>
<?php echo isset($Result['additional_header_js']) ? $Result['additional_header_js'] : ''?>
2 changes: 1 addition & 1 deletion lhc_web/lib/core/lhchat/lhchat.php
Expand Up @@ -87,7 +87,7 @@ public static function getPendingChats($limit = 50, $offset = 0, $filterAddition
if (!empty($filterAdditional)) {
$filter = array_merge_recursive($filter,$filterAdditional);
}

return self::getList($filter);
}

Expand Down
1 change: 1 addition & 0 deletions lhc_web/lib/core/lhuser/lhuser.php
Expand Up @@ -278,6 +278,7 @@ function logout()

$this->session->destroy();

session_destroy();
session_regenerate_id(true);
}

Expand Down
21 changes: 14 additions & 7 deletions lhc_web/modules/lhchat/syncadmininterface.php
Expand Up @@ -184,17 +184,21 @@
* Get last pending chat
* */
$lastPendingChatID = 0;
$lastChatNick = erTranslationClassLhTranslation::getInstance()->getTranslation('chat/startchat','Visitor');
$lastChatNickTab = $lastChatNick = erTranslationClassLhTranslation::getInstance()->getTranslation('chat/startchat','Visitor');
$lastMessage = erTranslationClassLhTranslation::getInstance()->getTranslation('pagelayout/pagelayout','New chat request');
$lastChatUserId = 0;

if (!empty($pendingChats)) {
$lastPendingChatID = max(array_keys($pendingChats));
$chatRecent = reset($pendingChats);
$lastChatNick = $chatRecent->nick.' | '.$chatRecent->department;
$lastMessage = erLhcoreClassChat::getGetLastChatMessagePending($chatRecent->id);
$lastChatNickTab = $chatRecent->nick;
$lastChatUserId = $chatRecent->user_id;
}

erLhcoreClassChat::prefillGetAttributes($pendingChats,array('time_created_front','product_name','department_name','wait_time_pending','wait_time_seconds','plain_user_name'), array('product_id','product','department','time','status','user_id','user'));
$ReturnMessages['pending_chats'] = array('list' => array_values($pendingChats),'nick' => $lastChatNick,'msg' => $lastMessage, 'last_id_identifier' => 'pending_chat', 'last_id' => $lastPendingChatID);
$ReturnMessages['pending_chats'] = array('list' => array_values($pendingChats), 'uid' => $lastChatUserId,'nick' => $lastChatNick, 'nt' => $lastChatNickTab,'msg' => $lastMessage, 'last_id_identifier' => 'pending_chat', 'last_id' => $lastPendingChatID);

$chatsList[] = & $ReturnMessages['pending_chats']['list'];
}
Expand Down Expand Up @@ -225,8 +229,8 @@
}
}

$ReturnMessages['transfer_chats'] = array('list' => array_values($transferchatsUser),'last_id_identifier' => 'transfer_chat','last_id' => $lastPendingTransferID);
$ReturnMessages['transfer_dep_chats'] = array('list' => array_values($transferchatsDep),'last_id_identifier' => 'transfer_chat','last_id' => $lastPendingTransferID);
$ReturnMessages['transfer_chats'] = array('list' => array_values($transferchatsUser),'last_id_identifier' => 'transfer_chat', 'uid' => 0, 'last_id' => $lastPendingTransferID);
$ReturnMessages['transfer_dep_chats'] = array('list' => array_values($transferchatsDep),'last_id_identifier' => 'transfer_chat', 'uid' => 0, 'last_id' => $lastPendingTransferID);

if ($canListOnlineUsers == true || $canListOnlineUsersAll == true) {

Expand Down Expand Up @@ -264,17 +268,20 @@
$unreadChats = erLhcoreClassChat::getUnreadMessagesChats($limitList,0,$filter);

$lastPendingChatID = 0;
$lastChatNick = erTranslationClassLhTranslation::getInstance()->getTranslation('chat/startchat','Visitor');
$lastMessage = erTranslationClassLhTranslation::getInstance()->getTranslation('pagelayout/pagelayout','New unread message');
$lastChatNickTab = $lastChatNick = erTranslationClassLhTranslation::getInstance()->getTranslation('chat/startchat','Visitor');
$lastMessage = erTranslationClassLhTranslation::getInstance()->getTranslation('pagelayout/pagelayout','New unread message');
$lastChatUserId = 0;
if (!empty($unreadChats)) {
$lastPendingChatID = max(array_keys($unreadChats));
$chatRecent = reset($unreadChats);
$lastChatNick = erTranslationClassLhTranslation::getInstance()->getTranslation('chat/startchat','Unread message') . ' | ' .$chatRecent->nick . ' | ' . $chatRecent->department;
$lastMessage = erLhcoreClassChat::getGetLastChatMessagePending($chatRecent->id);
$lastChatNickTab = $chatRecent->nick;
$lastChatUserId = $chatRecent->user_id;
}

erLhcoreClassChat::prefillGetAttributes($unreadChats,array('time_created_front','product_name','department_name','unread_time','plain_user_name'),array('product_id','product','department','time','status','user_id','user'));
$ReturnMessages['unread_chats'] = array('msg' => $lastMessage, 'nick' => $lastChatNick, 'last_id' => $lastPendingChatID, 'last_id_identifier' => 'unread_chat', 'list' => array_values($unreadChats));
$ReturnMessages['unread_chats'] = array('msg' => $lastMessage, 'nick' => $lastChatNick, 'uid' => $lastChatUserId, 'nt' => $lastChatNickTab, 'last_id' => $lastPendingChatID, 'last_id_identifier' => 'unread_chat', 'list' => array_values($unreadChats));

$chatsList[] = & $ReturnMessages['unread_chats']['list'];
}
Expand Down

0 comments on commit ab8a5b6

Please sign in to comment.