[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.")
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):
_name = "im.user"

View File

@ -254,22 +254,25 @@ function declare($, _, openerp) {
},
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 defs = [];
var received = false;
_.each(messages, function(message) {
if (! message.technical) {
defs.push(self.activate_session(message.session_id[0]).then(function(conv) {
received = true;
return conv.received_message(message);
}));
} else {
var json = JSON.parse(message.message);
message.json = json;
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);
},
calc_positions: function() {
@ -292,7 +295,7 @@ function declare($, _, openerp) {
className: "openerp_style oe_im_chatview",
events: {
"keydown input": "keydown",
"click .oe_im_chatview_close": "destroy",
"click .oe_im_chatview_close": "close",
"click .oe_im_chatview_header": "show_hide"
},
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;
};
date = "" + zpad(date.getHours(), 2) + ":" + zpad(date.getMinutes(), 2);
this.last_bubble = $(openerp.qweb.render("im_common.conversation_bubble", {"items": items, "user": user, "time": date}));
var to_show = _.map(items, im_common.escape_keep_url);
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._go_bottom();
},
@ -456,7 +459,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() {
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));
},
focus: function() {
@ -464,6 +467,20 @@ function declare($, _, openerp) {
if (! this.shown)
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() {
_.each(this.get("users"), function(user) {
user.remove_watcher();
@ -479,11 +496,36 @@ 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();
var def = $.when();
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;
}

View File

@ -33,7 +33,7 @@
<div class="oe_im_chatview_from"><t t-esc="user.get('name')"/></div>
<div class="oe_im_chatview_bubble_list">
<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>
</div>
<div class="oe_im_chatview_time"><t t-esc="time"/></div>