diff --git a/addons/im/im.py b/addons/im/im.py index f34a66fc56f..919a196db2e 100644 --- a/addons/im/im.py +++ b/addons/im/im.py @@ -143,7 +143,7 @@ class im_message(osv.osv): 'message': fields.text(string="Message", required=True), 'from_id': fields.many2one("im.user", "From", required= True, ondelete='cascade'), 'session_id': fields.many2one("im.session", "Session", required=True, select=True, ondelete='cascade'), - 'to_id': fields.many2many("im.user", "To"), + 'to_id': fields.many2many("im.user", "im_message_users", 'message_id', 'user_id', 'To'), 'date': fields.datetime("Date", required=True, select=True), 'technical': fields.boolean("Technical Message"), } @@ -156,7 +156,6 @@ class im_message(osv.osv): def get_messages(self, cr, uid, last=None, users_watch=None, uuid=None, context=None): assert_uuid(uuid) users_watch = users_watch or [] - session_ids = session_ids or [] # complex stuff to determine the last message to show users = self.pool.get("im.user") @@ -188,8 +187,8 @@ class im_message(osv.osv): 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 != my_id] - self.create(cr, openerp.SUPERUSER_ID, {"message": message, 'from_id': my_id, 'to_id': to_ids}, context=context) - notify_channel(cr, "im_channel", {'type': 'message', 'receivers': to_user_id}) + self.create(cr, openerp.SUPERUSER_ID, {"message": message, 'from_id': my_id, 'to_id': to_ids, 'session_id': to_session_id}, context=context) + notify_channel(cr, "im_channel", {'type': 'message', 'receivers': to_ids}) return False class im_session(osv.osv): diff --git a/addons/im/security/im_security.xml b/addons/im/security/im_security.xml index a964855ccf9..c99b7ec96e1 100644 --- a/addons/im/security/im_security.xml +++ b/addons/im/security/im_security.xml @@ -5,11 +5,11 @@ Can only read messages that you sent or messages sent to you - ["|", ('to_id.user_id', '=', user.id), ('from_id.user_id', '=', user.id)] - - + ["|", ('to_id.user_id', 'in', [user.id]), ('from_id.user_id', '=', user.id)] + + @@ -17,10 +17,21 @@ [('user_id', '=', user.id)] - - + + + + + + Can modify sessions if you are in it. + + + [('user_ids', 'in', [user.id])] + + + + diff --git a/addons/im/security/ir.model.access.csv b/addons/im/security/ir.model.access.csv index db3fe77f690..87593f28ea2 100644 --- a/addons/im/security/ir.model.access.csv +++ b/addons/im/security/ir.model.access.csv @@ -1,3 +1,4 @@ id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink access_im_message,im.message,model_im_message,base.group_user,1,0,1,0 -access_im_user,im.user,model_im_user,,1,1,1,0 \ No newline at end of file +access_im_user,im.user,model_im_user,,1,1,1,0 +access_im_session,im.session,model_im_session,base.group_user,1,1,1,0 \ No newline at end of file diff --git a/addons/im/static/src/js/im.js b/addons/im/static/src/js/im.js index 63ab694a723..4f49464693c 100644 --- a/addons/im/static/src/js/im.js +++ b/addons/im/static/src/js/im.js @@ -125,7 +125,10 @@ this.shown = ! this.shown; }, activate_user: function(user) { - this.c_manager.activate_user(user, true); + var self = this; + im_common.connection.model("im.session").call("session_get", [user.get("id"), self.c_manager.me.get("uuid")]).then(function(session) { + self.c_manager.activate_session(session.id, true); + }); }, }); diff --git a/addons/im/static/src/js/im_common.js b/addons/im/static/src/js/im_common.js index 9e5e13a9099..6d88764b1d8 100644 --- a/addons/im/static/src/js/im_common.js +++ b/addons/im/static/src/js/im_common.js @@ -67,7 +67,6 @@ function declare($, _, openerp) { this.set("right_offset", 0); this.set("bottom_offset", 0); this.conversations = []; - this.users = {}; this.on("change:right_offset", this, this.calc_positions); this.on("change:bottom_offset", this, this.calc_positions); this.set("window_focus", true); @@ -178,12 +177,7 @@ function declare($, _, openerp) { self.get_user(el.id).set(el); }); self.last = result.last; - var user_ids = _.pluck(_.pluck(result.res, "from_id"), 0); - self.ensure_users(user_ids).then(function() { - _.each(result.res, function(mes) { - var user = self.get_user(mes.from_id[0]); - self.received_message(mes, user); - }); + self.received_messages(result.res).then(function() { self.poll(); }); }, function(unused, e) { @@ -217,32 +211,41 @@ function declare($, _, openerp) { openerp.webclient.set_title_part("im_messages", this.get("waiting_messages") === 0 ? undefined : _.str.sprintf(_t("%d Messages"), this.get("waiting_messages"))); }, - activate_user: function(user, focus) { - var conv = this.users[user.get('id')]; + activate_session: function(session_id, focus) { + var conv = _.find(this.conversations, function(conv) {return conv.session_id == session_id;}); + var def = $.when(); if (! conv) { - conv = new im_common.Conversation(this, user, this.me, this.options); - conv.appendTo($("body")); - conv.on("destroyed", this, function() { - this.conversations = _.without(this.conversations, conv); - delete this.users[conv.user.get('id')]; + conv = new im_common.Conversation(this, this, session_id, this.options); + def = conv.appendTo($("body")).then(_.bind(function() { + conv.on("destroyed", this, function() { + this.conversations = _.without(this.conversations, conv); + this.calc_positions(); + }); + this.conversations.push(conv); this.calc_positions(); - }); - this.conversations.push(conv); - this.users[user.get('id')] = conv; - this.calc_positions(); + }, this)); } - if (focus) - conv.focus(); - return conv; + if (focus) { + def = def.then(function() { + conv.focus(); + }); + } + return def.then(function() {return conv}); }, - received_message: function(message, user) { - if (! this.get("window_focus")) { - this.set("waiting_messages", this.get("waiting_messages") + 1); + received_messages: function(messages) { + var self = this; + if (! this.get("window_focus") && messages.length >= 1) { + this.set("waiting_messages", this.get("waiting_messages") + messages.length); this.ting.play(); this.create_ting(); } - var conv = this.activate_user(user); - conv.received_message(message); + var defs = []; + _.each(messages, function(message) { + defs.push(self.activate_session(message.session_id[0]).then(function(conv) { + return conv.received_message(message); + })); + }); + return $.when.apply($, defs); }, calc_positions: function() { var current = this.get("right_offset"); @@ -267,39 +270,52 @@ function declare($, _, openerp) { "click .oe_im_chatview_close": "destroy", "click .oe_im_chatview_header": "show_hide" }, - init: function(parent, user, me, options) { + init: function(parent, c_manager, session_id, options) { this._super(parent); - this.options = options; - this.me = me; - this.user = user; - this.user.add_watcher(); + this.c_manager = c_manager; + this.options = options || {}; + this.session_id = session_id; this.set("right_position", 0); this.set("bottom_position", 0); this.shown = true; this.set("pending", 0); this.inputPlaceholder = this.options.defaultInputPlaceholder; + this.users = []; + this.others = []; }, start: function() { - this.$().append(openerp.qweb.render("im_common.conversation", {widget: this, to_url: _.bind(im_common.connection.url, im_common.connection)})); - var change_status = function() { - this.$().toggleClass("oe_im_chatview_disconnected_status", this.user.get("im_status") === false); - this.$(".oe_im_chatview_online").toggle(this.user.get("im_status") === true); - this._go_bottom(); - }; - this.user.on("change:im_status", this, change_status); - change_status.call(this); + 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() { + self.users = _.map(user_ids, function(id) {return self.c_manager.get_user(id);}); + _.each(self.users, function(user) { + user.add_watcher(); + }); + // TODO: correctly display status + self.$().append(openerp.qweb.render("im_common.conversation", {widget: self, to_url: _.bind(im_common.connection.url, im_common.connection)})); + var change_status = function() { + self.$().toggleClass("oe_im_chatview_disconnected_status", self.users[0].get("im_status") === false); + self.$(".oe_im_chatview_online").toggle(self.users[0].get("im_status") === true); + self._go_bottom(); + }; + self.users[0].on("change:im_status", self, change_status); + change_status.call(self); - this.on("change:right_position", this, this.calc_pos); - this.on("change:bottom_position", this, this.calc_pos); - this.full_height = this.$().height(); - this.calc_pos(); - this.on("change:pending", this, _.bind(function() { - if (this.get("pending") === 0) { - this.$(".oe_im_chatview_nbr_messages").text(""); - } else { - this.$(".oe_im_chatview_nbr_messages").text("(" + this.get("pending") + ")"); - } - }, this)); + 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() { if (this.shown) { @@ -326,7 +342,14 @@ function declare($, _, openerp) { } else { this.set("pending", this.get("pending") + 1); } - this._add_bubble(this.user, message.message, openerp.str_to_datetime(message.date)); + this.c_manager.ensure_users([message.from_id[0]]).then(_.bind(function() { + var user = this.c_manager.get_user(message.from_id[0]); + if (! _.contains(this.users, user) && ! _.contains(this.others, user)) { + this.others.push(user); + user.add_watcher(); + } + this._add_bubble(user, message.message, openerp.str_to_datetime(message.date)); + }, this)); }, send_message: function(e) { if(e && e.which !== 13) { @@ -339,17 +362,15 @@ function declare($, _, openerp) { this.$("input").val(""); var send_it = _.bind(function() { var model = im_common.connection.model("im.message"); - return model.call("post", [mes, this.user.get('id')], {uuid: this.me.get("uuid"), context: {}}); + return model.call("post", [mes, this.session_id], {uuid: this.c_manager.me.get("uuid"), context: {}}); }, this); var tries = 0; - send_it().then(_.bind(function() { - this._add_bubble(this.me, mes, new Date()); - }, this), function(error, e) { + send_it().then(_.bind(function() {}, function(error, e) { e.preventDefault(); tries += 1; if (tries < 3) return send_it(); - }); + })); }, _add_bubble: function(user, item, date) { var items = [item]; @@ -378,7 +399,12 @@ function declare($, _, openerp) { this.show_hide(); }, destroy: function() { - this.user.remove_watcher(); + _.each(this.users, function(user) { + user.remove_watcher(); + }) + _.each(this.others, function(user) { + user.remove_watcher(); + }) this.trigger("destroyed"); return this._super(); } diff --git a/addons/im/static/src/xml/im_common.xml b/addons/im/static/src/xml/im_common.xml index 4286d0c9710..0cf788aab49 100644 --- a/addons/im/static/src/xml/im_common.xml +++ b/addons/im/static/src/xml/im_common.xml @@ -4,12 +4,12 @@
- +
- +
diff --git a/addons/im_livechat/im_livechat.py b/addons/im_livechat/im_livechat.py index a47f87a9fd2..cec546c8b8c 100644 --- a/addons/im_livechat/im_livechat.py +++ b/addons/im_livechat/im_livechat.py @@ -198,7 +198,7 @@ class im_livechat_channel(osv.osv): self.write(cr, uid, ids, {'user_ids': [(3, uid)]}) return True - +""" class im_message(osv.osv): _inherit = 'im.message' @@ -244,3 +244,4 @@ class im_message(osv.osv): 'direction': fields.function(_direction, type="selection", selection=[("s2c", "Support Member to Customer"), ("c2s", "Customer to Support Member")], string='Direction', store=False), } +""" diff --git a/addons/im_livechat/im_livechat_view.xml b/addons/im_livechat/im_livechat_view.xml index 65401f3b88f..a5e3de898ae 100644 --- a/addons/im_livechat/im_livechat_view.xml +++ b/addons/im_livechat/im_livechat_view.xml @@ -120,6 +120,7 @@ + diff --git a/addons/im_livechat/security/im_livechat_security.xml b/addons/im_livechat/security/im_livechat_security.xml index 9088d0ed3db..b7185584394 100644 --- a/addons/im_livechat/security/im_livechat_security.xml +++ b/addons/im_livechat/security/im_livechat_security.xml @@ -21,6 +21,7 @@ +