niv-openerp 2013-09-02 19:29:23 +02:00
parent 1a82946928
commit d8bbbdf7c5
4 changed files with 39 additions and 24 deletions

View File

@ -207,19 +207,21 @@ class im_session(osv.osv):
}
# Todo: reuse existing sessions if possible
def session_get(self, cr, uid, user_to, uuid=None, context=None):
def session_get(self, cr, uid, users_to, uuid=None, context=None):
my_id = self.pool.get("im.user").get_my_id(cr, uid, uuid, context=context)
users = [my_id] + users_to
domain = []
for user_to in users:
domain.append(('user_ids', 'in', [user_to]))
sids = self.search(cr, openerp.SUPERUSER_ID, domain, context=context, limit=1)
session_id = None
if user_to:
# FP Note: does the ORM allows something better than this? == on many2many
sids = self.search(cr, openerp.SUPERUSER_ID, [('user_ids', 'in', [user_to]), ('user_ids', 'in', [my_id])], context=context, limit=1)
for session in self.browse(cr, uid, sids, context=context):
if len(session.user_ids) == 2:
session_id = session.id
break
for session in self.browse(cr, uid, sids, context=context):
if len(session.user_ids) == len(users):
session_id = session.id
break
if not session_id:
session_id = self.create(cr, openerp.SUPERUSER_ID, {
'user_ids': [(6, 0, [user_to, my_id])]
'user_ids': [(6, 0, users)]
}, context=context)
return self.read(cr, uid, session_id, context=context)

View File

@ -55,6 +55,7 @@
this.set("current_search", "");
this.users = [];
this.c_manager = new im_common.ConversationManager(this);
window.im_conversation_manager = this.c_manager;
this.on("change:right_offset", this.c_manager, _.bind(function() {
this.c_manager.set("right_offset", this.get("right_offset"));
}, this));
@ -106,7 +107,7 @@
_.each(users, function(user) {
var widget = new instance.im.UserWidget(self, self.c_manager.get_user(user.id));
widget.appendTo(self.$(".oe_im_users"));
widget.on("activate_user", self, self.activate_user);
widget.on("activate_user", self, function(user) {self.c_manager.chat_with_users([user]);});
self.users.push(widget);
});
_.each(old_users, function(user) {
@ -136,12 +137,6 @@
}
this.shown = ! this.shown;
},
activate_user: function(user) {
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);
});
},
add_user: function(conversation, user) {
conversation.add_user(user);
},

View File

@ -141,12 +141,16 @@ function declare($, _, openerp) {
no_cache[el] = el;
}, this);
var self = this;
var def;
if (_.size(no_cache) === 0)
return $.when();
def = $.when();
else
return im_common.connection.model("im.user").call("read", [_.values(no_cache), []]).then(function(users) {
def = im_common.connection.model("im.user").call("read", [_.values(no_cache), []]).then(function(users) {
self.add_to_user_cache(users);
});
return def.then(function() {
return _.map(user_ids, function(id) { return self.get_user(id); });
});
},
add_to_user_cache: function(user_recs) {
_.each(user_recs, function(user_rec) {
@ -211,6 +215,21 @@ 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")));
},
chat_with_users: function(users) {
var self = this;
return im_common.connection.model("im.session").call("session_get", [_.map(users, function(user) {return user.get("id");}),
self.me.get("uuid")]).then(function(session) {
return self.activate_session(session.id, true);
});
},
chat_with_all_users: function() {
var self = this;
return im_common.connection.model("im.user").call("search", [[["uuid", "=", false]]]).then(function(user_ids) {
return self.ensure_users(_.without(user_ids, self.me.get("id")));
}).then(function(users) {
return self.chat_with_users(users);
});
},
activate_session: function(session_id, focus) {
var conv = _.find(this.conversations, function(conv) {return conv.session_id == session_id;});
var def = $.when();
@ -347,9 +366,8 @@ function declare($, _, openerp) {
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);});
return self.c_manager.ensure_users(user_ids);
}).then(function(users) {
self.set("users", users);
});
},
@ -378,8 +396,8 @@ function declare($, _, openerp) {
} else {
this.set("pending", this.get("pending") + 1);
}
this.c_manager.ensure_users([message.from_id[0]]).then(_.bind(function() {
var user = this.c_manager.get_user(message.from_id[0]);
this.c_manager.ensure_users([message.from_id[0]]).then(_.bind(function(users) {
var user = users[0];
if (! _.contains(this.get("users"), user) && ! _.contains(this.others, user)) {
this.others.push(user);
user.add_watcher();

View File

@ -175,7 +175,7 @@ class im_livechat_channel(osv.osv):
if len(users) == 0:
return False
user_id = random.choice(users).id
session = self.pool.get("im.session").session_get(cr, uid, user_id, uuid, context=context)
session = self.pool.get("im.session").session_get(cr, uid, [user_id], uuid, context=context)
self.pool.get("im.session").write(cr, openerp.SUPERUSER_ID, session.get("id"), {'channel_id': channel_id}, context=context)
return session.get("id")