[IMP] im: some more improvements and bug fixes to make the group chat more usable

bzr revid: nicolas.vanhoren@openerp.com-20130904132904-1tabxcy8hhthwuoh
This commit is contained in:
niv-openerp 2013-09-04 15:29:04 +02:00
commit 5e9f1c742c
3 changed files with 58 additions and 12 deletions

View File

@ -232,6 +232,10 @@ class im_session(osv.osv):
raise Exception("Not allowed to modify a session when you are not in it.") raise Exception("Not allowed to modify a session when you are not in it.")
self.write(cr, uid, session_id, {"user_ids": [(4, user_id)]}, context=context) self.write(cr, uid, session_id, {"user_ids": [(4, user_id)]}, context=context)
def remove_me_from_session(self, cr, uid, session_id, uuid=None, context=None):
my_id = self.pool.get("im.user").get_my_id(cr, uid, uuid, context=context)
self.write(cr, openerp.SUPERUSER_ID, session_id, {"user_ids": [(3, my_id)]}, context=context)
class im_user(osv.osv): class im_user(osv.osv):
_name = "im.user" _name = "im.user"

View File

@ -254,22 +254,25 @@ function declare($, _, openerp) {
}, },
received_messages: function(messages) { received_messages: function(messages) {
var self = this; 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 defs = []; var defs = [];
var received = false;
_.each(messages, function(message) { _.each(messages, function(message) {
if (! message.technical) { if (! message.technical) {
defs.push(self.activate_session(message.session_id[0]).then(function(conv) { defs.push(self.activate_session(message.session_id[0]).then(function(conv) {
received = true;
return conv.received_message(message); return conv.received_message(message);
})); }));
} else { } else {
var json = JSON.parse(message.message); var json = JSON.parse(message.message);
message.json = json;
defs.push($.when(im_common.technical_messages_handlers[json.type](self, message))); defs.push($.when(im_common.technical_messages_handlers[json.type](self, message)));
} }
}); });
if (! this.get("window_focus") && received) {
this.set("waiting_messages", this.get("waiting_messages") + messages.length);
this.ting.play();
this.create_ting();
}
return $.when.apply($, defs); return $.when.apply($, defs);
}, },
calc_positions: function() { calc_positions: function() {
@ -292,7 +295,7 @@ function declare($, _, openerp) {
className: "openerp_style oe_im_chatview", className: "openerp_style oe_im_chatview",
events: { events: {
"keydown input": "keydown", "keydown input": "keydown",
"click .oe_im_chatview_close": "destroy", "click .oe_im_chatview_close": "close",
"click .oe_im_chatview_header": "show_hide" "click .oe_im_chatview_header": "show_hide"
}, },
init: function(parent, c_manager, session_id, options) { init: function(parent, c_manager, session_id, options) {
@ -443,8 +446,8 @@ function declare($, _, openerp) {
return new Array(size - str.length + 1).join('0') + str; return new Array(size - str.length + 1).join('0') + str;
}; };
date = "" + zpad(date.getHours(), 2) + ":" + zpad(date.getMinutes(), 2); date = "" + zpad(date.getHours(), 2) + ":" + zpad(date.getMinutes(), 2);
var to_show = _.map(items, im_common.escape_keep_url);
this.last_bubble = $(openerp.qweb.render("im_common.conversation_bubble", {"items": items, "user": user, "time": date})); this.last_bubble = $(openerp.qweb.render("im_common.conversation_bubble", {"items": to_show, "user": user, "time": date}));
$(this.$(".oe_im_chatview_content").children()[0]).append(this.last_bubble); $(this.$(".oe_im_chatview_content").children()[0]).append(this.last_bubble);
this._go_bottom(); this._go_bottom();
}, },
@ -456,7 +459,7 @@ function declare($, _, openerp) {
return; return;
im_common.connection.model("im.session").call("add_to_session", 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() { [this.session_id, user.get("id"), this.c_manager.me.get("uuid")]).then(_.bind(function() {
this.send_message(JSON.stringify({"type": "session_modified"}), true); this.send_message(JSON.stringify({"type": "session_modified", "action": "added", "user_id": user.get("id")}), true);
}, this)); }, this));
}, },
focus: function() { focus: function() {
@ -464,6 +467,20 @@ function declare($, _, openerp) {
if (! this.shown) if (! this.shown)
this.show_hide(); this.show_hide();
}, },
close: function() {
var def = $.when();
if (this.get("users").length > 1) {
def = im_common.connection.model("im.session").call("remove_me_from_session",
[this.session_id, this.c_manager.me.get("uuid")]).then(_.bind(function() {
return this.send_message(JSON.stringify({"type": "session_modified", "action": "removed",
"user_id": this.c_manager.me.get("id")}), true)
}, this))
}
return def.then(_.bind(function() {
this.destroy();
}, this));
},
destroy: function() { destroy: function() {
_.each(this.get("users"), function(user) { _.each(this.get("users"), function(user) {
user.remove_watcher(); user.remove_watcher();
@ -479,11 +496,36 @@ function declare($, _, openerp) {
im_common.technical_messages_handlers = {}; im_common.technical_messages_handlers = {};
im_common.technical_messages_handlers.session_modified = function(c_manager, message) { im_common.technical_messages_handlers.session_modified = function(c_manager, message) {
c_manager.activate_session(message.session_id[0], true).then(function(conv) { var def = $.when();
conv.refresh_users(); if (message.json.action === "added" && message.json.user_id === c_manager.me.get("id")) {
def = c_manager.activate_session(message.session_id[0], true);
}
return def.then(function() {
var conv = _.find(c_manager.conversations, function(conv) {return conv.session_id == message.session_id[0];});
if (conv)
return conv.refresh_users();
return undefined;
}); });
}; };
var url_regex = /(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/gi;
im_common.escape_keep_url = function(str) {
var last = 0;
var txt = "";
while (true) {
var result = url_regex.exec(str);
if (! result)
break;
txt += _.escape(str.slice(last, result.index));
last = url_regex.lastIndex;
var url = _.escape(result[0]);
txt += '<a href="' + url + '">' + url + '</a>';
}
txt += str.slice(last, str.length);
return txt;
};
return im_common; return im_common;
} }

View File

@ -33,7 +33,7 @@
<div class="oe_im_chatview_from"><t t-esc="user.get('name')"/></div> <div class="oe_im_chatview_from"><t t-esc="user.get('name')"/></div>
<div class="oe_im_chatview_bubble_list"> <div class="oe_im_chatview_bubble_list">
<t t-foreach="items" t-as="item"> <t t-foreach="items" t-as="item">
<div class="oe_im_chatview_bubble_item"><t t-esc="item"/></div> <div class="oe_im_chatview_bubble_item"><t t-raw="item"/></div>
</t> </t>
</div> </div>
<div class="oe_im_chatview_time"><t t-esc="time"/></div> <div class="oe_im_chatview_time"><t t-esc="time"/></div>