From bb03449bc8114c9da94ad99e5aad96cc9c90e23a Mon Sep 17 00:00:00 2001 From: Sagar Ahirrao Date: Wed, 7 Dec 2016 18:18:44 +0530 Subject: [PATCH] [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. --- addons/im_livechat/controllers/main.py | 7 +++ addons/im_livechat/models/mail_channel.py | 27 +++++++++- .../im_livechat/static/src/js/im_livechat.js | 49 ++++++++++++++++--- 3 files changed, 74 insertions(+), 9 deletions(-) diff --git a/addons/im_livechat/controllers/main.py b/addons/im_livechat/controllers/main.py index fb968d728c1..f033fc72cc7 100644 --- a/addons/im_livechat/controllers/main.py +++ b/addons/im_livechat/controllers/main.py @@ -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 diff --git a/addons/im_livechat/models/mail_channel.py b/addons/im_livechat/models/mail_channel.py index 6153e7cb7d8..3f6c47ba5ce 100644 --- a/addons/im_livechat/models/mail_channel.py +++ b/addons/im_livechat/models/mail_channel.py @@ -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 '%s

' % (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 = ['
  • %s
  • ' % (page, page) for page in page_history] + message_body = '
      %s
    ' % (''.join(html_links)) + self.env['bus.bus'].sendone((self._cr.dbname, 'res.partner', pid), { + 'body': message_body, + 'channel_ids': self.ids, + 'info': 'transient_message', + }) diff --git a/addons/im_livechat/static/src/js/im_livechat.js b/addons/im_livechat/static/src/js/im_livechat.js index 446717b1b7f..4d0f1da89a4 100644 --- a/addons/im_livechat/static/src/js/im_livechat.js +++ b/addons/im_livechat/static/src/js/im_livechat.js @@ -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'),