[IMP] im_livechat: visitor history command
this commit introduces the '/history' command for an operator during a livechat session. When the visitor load a page, its url is added in a cookie (kept only 1 day) in order to track his visited web pages. The '/history' command will ask the cookie content and send it to the operator as a transient message in the livechat channel.
This commit is contained in:
committed by
Jérome Maes
parent
be697f7425
commit
bb03449bc8
@@ -105,3 +105,10 @@ class LivechatController(http.Controller):
|
||||
rating.write(values)
|
||||
return rating.id
|
||||
return False
|
||||
|
||||
@http.route('/im_livechat/history', type="json", auth="public")
|
||||
def history_pages(self, pid, channel_uuid, page_history=None):
|
||||
channel = request.env['mail.channel'].search([('uuid', '=', channel_uuid)])
|
||||
if channel:
|
||||
channel._send_history_message(pid, page_history)
|
||||
return True
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
|
||||
from odoo import api, fields, models
|
||||
from odoo import api, fields, models, _
|
||||
|
||||
|
||||
class ChannelPartner(models.Model):
|
||||
@@ -107,3 +107,28 @@ class MailChannel(models.Model):
|
||||
if help:
|
||||
return '<p">%s</p>' % (help)
|
||||
return super(MailChannel, self).get_empty_list_help(help)
|
||||
|
||||
def _define_command_history(self):
|
||||
return {
|
||||
'channel_types': ['livechat'],
|
||||
'help': _('See 15 last visited pages')
|
||||
}
|
||||
|
||||
def _execute_command_history(self, **kwargs):
|
||||
notification = []
|
||||
notification_values = {
|
||||
'_type': 'history_command',
|
||||
}
|
||||
notification.append([self.uuid, dict(notification_values)])
|
||||
return self.env['bus.bus'].sendmany(notification)
|
||||
|
||||
def _send_history_message(self, pid, page_history):
|
||||
message_body = _('No history found')
|
||||
if page_history:
|
||||
html_links = ['<li><a href="%s" target="_blank">%s</a></li>' % (page, page) for page in page_history]
|
||||
message_body = '<span class="o_mail_notification"><ul>%s</ul></span>' % (''.join(html_links))
|
||||
self.env['bus.bus'].sendone((self._cr.dbname, 'res.partner', pid), {
|
||||
'body': message_body,
|
||||
'channel_ids': self.ids,
|
||||
'info': 'transient_message',
|
||||
})
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
odoo.define('im_livechat.im_livechat', function (require) {
|
||||
"use strict";
|
||||
|
||||
var local_storage = require('web.local_storage');
|
||||
var bus = require('bus.bus').bus;
|
||||
var config = require('web.config');
|
||||
var core = require('web.core');
|
||||
@@ -14,6 +15,26 @@ var ChatWindow = require('mail.ChatWindow');
|
||||
var _t = core._t;
|
||||
var QWeb = core.qweb;
|
||||
|
||||
// Constants
|
||||
var LIVECHAT_COOKIE_HISTORY = 'im_livechat_history';
|
||||
var HISTORY_LIMIT = 15;
|
||||
|
||||
// History tracking
|
||||
var page = window.location.href.replace(/^.*\/\/[^\/]+/, '');
|
||||
var page_history = utils.get_cookie(LIVECHAT_COOKIE_HISTORY);
|
||||
var url_history = [];
|
||||
if(page_history){
|
||||
url_history = JSON.parse(page_history) || [];
|
||||
}
|
||||
if (!_.contains(url_history, page)) {
|
||||
url_history.push(page);
|
||||
while (url_history.length > HISTORY_LIMIT) {
|
||||
url_history.shift();
|
||||
}
|
||||
utils.set_cookie(LIVECHAT_COOKIE_HISTORY, JSON.stringify(url_history), 60*60*24); // 1 day cookie
|
||||
}
|
||||
|
||||
|
||||
var LivechatButton = Widget.extend({
|
||||
className:"openerp o_livechat_button hidden-print",
|
||||
|
||||
@@ -69,18 +90,30 @@ var LivechatButton = Widget.extend({
|
||||
bus.on('notification', this, function (notifications) {
|
||||
var self = this;
|
||||
_.each(notifications, function (notification) {
|
||||
if (self.channel && (notification[0] === self.channel.uuid)) {
|
||||
self.add_message(notification[1]);
|
||||
self.render_messages();
|
||||
if (self.chat_window.folded || !self.chat_window.thread.is_at_bottom()) {
|
||||
self.chat_window.update_unread(self.chat_window.unread_msgs+1);
|
||||
}
|
||||
}
|
||||
self._on_notification(notification);
|
||||
});
|
||||
});
|
||||
return this._super();
|
||||
},
|
||||
|
||||
_on_notification: function(notification){
|
||||
if (this.channel && (notification[0] === this.channel.uuid)) {
|
||||
if(notification[1]._type === "history_command") { // history request
|
||||
var cookie = utils.get_cookie(LIVECHAT_COOKIE_HISTORY);
|
||||
var history = cookie ? JSON.parse(cookie) : [];
|
||||
session.rpc("/im_livechat/history", {
|
||||
pid: this.channel.operator_pid[0],
|
||||
channel_uuid: this.channel.uuid,
|
||||
page_history: history,
|
||||
});
|
||||
}else{ // normal message
|
||||
this.add_message(notification[1]);
|
||||
this.render_messages();
|
||||
if (this.chat_window.folded || !this.chat_window.thread.is_at_bottom()) {
|
||||
this.chat_window.update_unread(this.chat_window.unread_msgs+1);
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
load_qweb_template: function(){
|
||||
return $.when(
|
||||
$.get('/mail/static/src/xml/chat_window.xml'),
|
||||
|
||||
Reference in New Issue
Block a user