From 1a82946928760ba9b37a12ab76c26851a9d60cab Mon Sep 17 00:00:00 2001 From: niv-openerp Date: Mon, 2 Sep 2013 19:02:52 +0200 Subject: [PATCH] Now correctly send technical message. bzr revid: nicolas.vanhoren@openerp.com-20130902170252-nmp5kw1023jx65ij --- addons/im/im.py | 6 +-- addons/im/static/src/js/im.js | 3 +- addons/im/static/src/js/im_common.js | 80 ++++++++++++++++++---------- 3 files changed, 58 insertions(+), 31 deletions(-) diff --git a/addons/im/im.py b/addons/im/im.py index 15078a50fea..2ca3a7d2444 100644 --- a/addons/im/im.py +++ b/addons/im/im.py @@ -169,7 +169,7 @@ class im_message(osv.osv): # how fun it is to always need to reorder results from read mess_ids = self.search(cr, openerp.SUPERUSER_ID, ["&", ['id', '>', last], "|", ['from_id', '=', my_id], ['to_id', 'in', [my_id]]], order="id", context=context) - mess = self.read(cr, openerp.SUPERUSER_ID, mess_ids, ["id", "message", "from_id", "session_id", "date"], context=context) + mess = self.read(cr, openerp.SUPERUSER_ID, mess_ids, ["id", "message", "from_id", "session_id", "date", "technical"], context=context) index = {} for i in xrange(len(mess)): index[mess[i]["id"]] = mess[i] @@ -182,13 +182,13 @@ class im_message(osv.osv): users_status = users.read(cr, openerp.SUPERUSER_ID, users_watch, ["im_status"], context=context) return {"res": mess, "last": last, "dbname": cr.dbname, "users_status": users_status} - def post(self, cr, uid, message, to_session_id, uuid=None, technical=False, context=None): + def post(self, cr, uid, message, to_session_id, technical=False, uuid=None, context=None): assert_uuid(uuid) my_id = self.pool.get('im.user').get_my_id(cr, uid, uuid) session = self.pool.get('im.session').browse(cr, uid, to_session_id, context) to_ids = [x.id for x in session.user_ids if x.id != my_id] self.create(cr, openerp.SUPERUSER_ID, {"message": message, 'from_id': my_id, - 'to_id': [(6, 0, to_ids)], 'session_id': to_session_id, technical=technical}, context=context) + 'to_id': [(6, 0, to_ids)], 'session_id': to_session_id, 'technical': technical}, context=context) notify_channel(cr, "im_channel", {'type': 'message', 'receivers': [my_id] + to_ids}) return False diff --git a/addons/im/static/src/js/im.js b/addons/im/static/src/js/im.js index f26b8f5c4a2..085903cbde5 100644 --- a/addons/im/static/src/js/im.js +++ b/addons/im/static/src/js/im.js @@ -18,7 +18,8 @@ im_common.notification = function(message) { instance.client.do_warn(message); }; - im_common.connection = openerp.session; + // TODO: allow to use a different host for the chat + im_common.connection = new openerp.Session(self, null, {session_id: openerp.session.session_id}); var im = new instance.im.InstantMessaging(self); im.appendTo(instance.client.$el); diff --git a/addons/im/static/src/js/im_common.js b/addons/im/static/src/js/im_common.js index 708c9cd25f0..850218a257b 100644 --- a/addons/im/static/src/js/im_common.js +++ b/addons/im/static/src/js/im_common.js @@ -242,9 +242,14 @@ function declare($, _, openerp) { } var defs = []; _.each(messages, function(message) { - defs.push(self.activate_session(message.session_id[0]).then(function(conv) { - return conv.received_message(message); - })); + if (! message.technical) { + defs.push(self.activate_session(message.session_id[0]).then(function(conv) { + return conv.received_message(message); + })); + } else { + var json = JSON.parse(message.message); + defs.push($.when(im_common.technical_messages_handlers[json.type](self, message))); + } }); return $.when.apply($, defs); }, @@ -267,7 +272,7 @@ function declare($, _, openerp) { im_common.Conversation = openerp.Widget.extend({ className: "openerp_style oe_im_chatview", events: { - "keydown input": "send_message", + "keydown input": "keydown", "click .oe_im_chatview_close": "destroy", "click .oe_im_chatview_header": "show_hide" }, @@ -289,6 +294,7 @@ function declare($, _, openerp) { var self = this; self.$().append(openerp.qweb.render("im_common.conversation", {widget: self})); + this.$().hide(); var change_status = function() { var disconnected = _.every(this.get("users"), function(u) { return u.get("im_status") === false; }); @@ -304,34 +310,47 @@ function declare($, _, openerp) { user.on("change:im_status", self, change_status); }); change_status.call(self); + _.each(ev.oldValue, function(user) { + if (! _.contains(ev.newValue, user)) { + user.remove_watcher(); + } + }); + _.each(ev.newValue, function(user) { + if (! _.contains(ev.oldValue, user)) { + user.add_watcher(); + } + }); }); this.on("change:disconnected", this, function() { self.$().toggleClass("oe_im_chatview_disconnected_status", this.get("disconnected")); self._go_bottom(); }); + self.on("change:right_position", self, self.calc_pos); + self.on("change:bottom_position", self, self.calc_pos); + self.full_height = self.$().height(); + self.calc_pos(); + self.on("change:pending", self, _.bind(function() { + if (self.get("pending") === 0) { + self.$(".oe_im_chatview_nbr_messages").text(""); + } else { + self.$(".oe_im_chatview_nbr_messages").text("(" + self.get("pending") + ")"); + } + }, self)); + + return this.refresh_users().then(function() { + self.$().show(); + }); + }, + refresh_users: function() { + var self = this; var user_ids; return im_common.connection.model("im.session").call("read", [self.session_id]).then(function(session) { user_ids = _.without(session.user_ids, self.c_manager.me.get("id")); return self.c_manager.ensure_users(session.user_ids); }).then(function() { var users = _.map(user_ids, function(id) {return self.c_manager.get_user(id);}); - _.each(users, function(user) { - user.add_watcher(); - }); self.set("users", users); - - self.on("change:right_position", self, self.calc_pos); - self.on("change:bottom_position", self, self.calc_pos); - self.full_height = self.$().height(); - self.calc_pos(); - self.on("change:pending", self, _.bind(function() { - if (self.get("pending") === 0) { - self.$(".oe_im_chatview_nbr_messages").text(""); - } else { - self.$(".oe_im_chatview_nbr_messages").text("(" + self.get("pending") + ")"); - } - }, self)); }); }, show_hide: function() { @@ -368,7 +387,7 @@ function declare($, _, openerp) { this._add_bubble(user, message.message, openerp.str_to_datetime(message.date)); }, this)); }, - send_message: function(e) { + keydown: function(e) { if(e && e.which !== 13) { return; } @@ -377,9 +396,13 @@ function declare($, _, openerp) { return; } this.$("input").val(""); + this.send_message(mes); + }, + send_message: function(message, technical) { + technical = technical || false; var send_it = _.bind(function() { var model = im_common.connection.model("im.message"); - return model.call("post", [mes, this.session_id], {uuid: this.c_manager.me.get("uuid"), context: {}}); + return model.call("post", [message, this.session_id, technical], {uuid: this.c_manager.me.get("uuid"), context: {}}); }, this); var tries = 0; send_it().then(_.bind(function() {}, function(error, e) { @@ -415,12 +438,7 @@ function declare($, _, openerp) { return; im_common.connection.model("im.session").call("add_to_session", [this.session_id, user.get("id"), this.c_manager.me.get("uuid")]).then(_.bind(function() { - if (_.contains(this.others, user)) { - this.others = _.without(this.others, user); - } else { - user.add_watcher(); - } - this.set("users", this.get("users").concat([user])); + this.send_message(JSON.stringify({"type": "session_modified"}), true); }, this)); }, focus: function() { @@ -440,6 +458,14 @@ function declare($, _, openerp) { } }); + im_common.technical_messages_handlers = {}; + + im_common.technical_messages_handlers.session_modified = function(c_manager, message) { + c_manager.activate_session(message.session_id[0], true).then(function(conv) { + conv.refresh_users(); + }); + }; + return im_common; }