Refactored to avoid user duplication problem
bzr revid: nicolas.vanhoren@openerp.com-20130826123947-alcsov647beqge0c
This commit is contained in:
parent
7bce27c528
commit
8ee25f9856
|
@ -109,7 +109,7 @@ class LongPollingController(http.Controller):
|
||||||
registry = openerp.modules.registry.RegistryManager.get(db)
|
registry = openerp.modules.registry.RegistryManager.get(db)
|
||||||
with registry.cursor() as cr:
|
with registry.cursor() as cr:
|
||||||
registry.get('im.user').im_connect(cr, uid, uuid=uuid, context=request.context)
|
registry.get('im.user').im_connect(cr, uid, uuid=uuid, context=request.context)
|
||||||
my_id = registry.get('im.user').get_by_user_id(cr, uid, uuid or uid, request.context)["id"]
|
my_id = registry.get('im.user').get_my_id(cr, uid, uuid, request.context)
|
||||||
num = 0
|
num = 0
|
||||||
while True:
|
while True:
|
||||||
with registry.cursor() as cr:
|
with registry.cursor() as cr:
|
||||||
|
@ -156,7 +156,7 @@ class im_message(osv.osv):
|
||||||
|
|
||||||
# complex stuff to determine the last message to show
|
# complex stuff to determine the last message to show
|
||||||
users = self.pool.get("im.user")
|
users = self.pool.get("im.user")
|
||||||
my_id = users.get_by_user_id(cr, uid, uuid or uid, context=context)["id"]
|
my_id = users.get_my_id(cr, uid, uuid, context=context)
|
||||||
c_user = users.browse(cr, openerp.SUPERUSER_ID, my_id, context=context)
|
c_user = users.browse(cr, openerp.SUPERUSER_ID, my_id, context=context)
|
||||||
if last:
|
if last:
|
||||||
if c_user.im_last_received < last:
|
if c_user.im_last_received < last:
|
||||||
|
@ -181,7 +181,7 @@ class im_message(osv.osv):
|
||||||
|
|
||||||
def post(self, cr, uid, message, to_user_id, uuid=None, context=None):
|
def post(self, cr, uid, message, to_user_id, uuid=None, context=None):
|
||||||
assert_uuid(uuid)
|
assert_uuid(uuid)
|
||||||
my_id = self.pool.get('im.user').get_by_user_id(cr, uid, uuid or uid)["id"]
|
my_id = self.pool.get('im.user').get_my_id(cr, uid, uuid)
|
||||||
self.create(cr, openerp.SUPERUSER_ID, {"message": message, 'from_id': my_id, 'to_id': to_user_id}, context=context)
|
self.create(cr, openerp.SUPERUSER_ID, {"message": message, 'from_id': my_id, 'to_id': to_user_id}, context=context)
|
||||||
notify_channel(cr, "im_channel", {'type': 'message', 'receiver': to_user_id})
|
notify_channel(cr, "im_channel", {'type': 'message', 'receiver': to_user_id})
|
||||||
return False
|
return False
|
||||||
|
@ -199,10 +199,9 @@ class im_user(osv.osv):
|
||||||
res[obj["id"]] = obj["im_last_status"] and (last_update + delta) > current
|
res[obj["id"]] = obj["im_last_status"] and (last_update + delta) > current
|
||||||
return res
|
return res
|
||||||
|
|
||||||
def search_users(self, cr, uid, domain, fields, limit, context=None):
|
def search_users(self, cr, uid, text_search, fields, limit, context=None):
|
||||||
# do not user openerp.SUPERUSER_ID, reserved to normal users
|
my_id = self.get_my_id(cr, uid, None, context)
|
||||||
found = self.pool.get('res.users').search(cr, uid, domain, limit=limit, context=context)
|
found = self.search(cr, uid, [["name", "ilike", text_search], ["id", "<>", my_id]], limit=limit, context=context)
|
||||||
found = self.get_by_user_ids(cr, uid, found, context=context)
|
|
||||||
return self.read(cr, uid, found, fields, context=context)
|
return self.read(cr, uid, found, fields, context=context)
|
||||||
|
|
||||||
def im_connect(self, cr, uid, uuid=None, context=None):
|
def im_connect(self, cr, uid, uuid=None, context=None):
|
||||||
|
@ -215,7 +214,7 @@ class im_user(osv.osv):
|
||||||
|
|
||||||
def _im_change_status(self, cr, uid, new_one, uuid=None, context=None):
|
def _im_change_status(self, cr, uid, new_one, uuid=None, context=None):
|
||||||
assert_uuid(uuid)
|
assert_uuid(uuid)
|
||||||
id = self.get_by_user_id(cr, uid, uuid or uid, context=context)["id"]
|
id = self.get_my_id(cr, uid, uuid, context=context)
|
||||||
current_status = self.read(cr, openerp.SUPERUSER_ID, id, ["im_status"], context=None)["im_status"]
|
current_status = self.read(cr, openerp.SUPERUSER_ID, id, ["im_status"], context=None)["im_status"]
|
||||||
self.write(cr, openerp.SUPERUSER_ID, id, {"im_last_status": new_one,
|
self.write(cr, openerp.SUPERUSER_ID, id, {"im_last_status": new_one,
|
||||||
"im_last_status_update": datetime.datetime.now().strftime(DEFAULT_SERVER_DATETIME_FORMAT)}, context=context)
|
"im_last_status_update": datetime.datetime.now().strftime(DEFAULT_SERVER_DATETIME_FORMAT)}, context=context)
|
||||||
|
@ -223,37 +222,20 @@ class im_user(osv.osv):
|
||||||
notify_channel(cr, "im_channel", {'type': 'status', 'user': id})
|
notify_channel(cr, "im_channel", {'type': 'status', 'user': id})
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def get_by_user_id(self, cr, uid, id, context=None):
|
def get_my_id(self, cr, uid, uuid=None, context=None):
|
||||||
ids = self.get_by_user_ids(cr, uid, [id], context=context)
|
assert_uuid(uuid)
|
||||||
return ids[0]
|
if uuid:
|
||||||
|
users = self.search(cr, openerp.SUPERUSER_ID, [["uuid", "=", uuid]], context=None)
|
||||||
def get_by_user_ids(self, cr, uid, ids, context=None):
|
else:
|
||||||
user_ids = [x for x in ids if isinstance(x, int)]
|
users = self.search(cr, openerp.SUPERUSER_ID, [["user_id", "=", uid]], context=None)
|
||||||
uuids = [x for x in ids if isinstance(x, (str, unicode))]
|
my_id = users[0] if len(users) >= 1 else False
|
||||||
users = self.search(cr, openerp.SUPERUSER_ID, ["|", ["user_id", "in", user_ids], ["uuid", "in", uuids]], context=None)
|
if not my_id:
|
||||||
records = self.read(cr, openerp.SUPERUSER_ID, users, ["user_id", "uuid"], context=None)
|
my_id = self.create(cr, openerp.SUPERUSER_ID, {"user_id": uid if not uuid else False, "uuid": uuid if uuid else False}, context=context)
|
||||||
inside = {}
|
return my_id
|
||||||
for i in records:
|
|
||||||
if i["user_id"]:
|
|
||||||
inside[i["user_id"][0]] = True
|
|
||||||
elif ["uuid"]:
|
|
||||||
inside[i["uuid"]] = True
|
|
||||||
not_inside = {}
|
|
||||||
for i in ids:
|
|
||||||
if not (i in inside):
|
|
||||||
not_inside[i] = True
|
|
||||||
for to_create in not_inside.keys():
|
|
||||||
if isinstance(to_create, int):
|
|
||||||
created = self.create(cr, openerp.SUPERUSER_ID, {"user_id": to_create}, context=context)
|
|
||||||
records.append({"id": created, "user_id": [to_create, ""]})
|
|
||||||
else:
|
|
||||||
created = self.create(cr, openerp.SUPERUSER_ID, {"uuid": to_create}, context=context)
|
|
||||||
records.append({"id": created, "uuid": to_create})
|
|
||||||
return records
|
|
||||||
|
|
||||||
def assign_name(self, cr, uid, uuid, name, context=None):
|
def assign_name(self, cr, uid, uuid, name, context=None):
|
||||||
assert_uuid(uuid)
|
assert_uuid(uuid)
|
||||||
id = self.get_by_user_id(cr, uid, uuid or uid, context=context)["id"]
|
id = self.get_my_id(cr, uid, uuid, context=context)
|
||||||
self.write(cr, openerp.SUPERUSER_ID, id, {"assigned_name": name}, context=context)
|
self.write(cr, openerp.SUPERUSER_ID, id, {"assigned_name": name}, context=context)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
<field name="name">Can only read messages that you sent or messages sent to you</field>
|
<field name="name">Can only read messages that you sent or messages sent to you</field>
|
||||||
<field name="model_id" ref="model_im_message"/>
|
<field name="model_id" ref="model_im_message"/>
|
||||||
<field name="groups" eval="[(6,0,[ref('base.group_user')])]"/>
|
<field name="groups" eval="[(6,0,[ref('base.group_user')])]"/>
|
||||||
<field name="domain_force">["|", ('to_id.user', '=', user.id), ('from_id.user', '=', user.id)]</field>
|
<field name="domain_force">["|", ('to_id.user_id', '=', user.id), ('from_id.user_id', '=', user.id)]</field>
|
||||||
<field name="perm_unlink" eval="0"/>
|
<field name="perm_unlink" eval="0"/>
|
||||||
<field name="perm_write" eval="0"/>
|
<field name="perm_write" eval="0"/>
|
||||||
<field name="perm_read" eval="1"/>
|
<field name="perm_read" eval="1"/>
|
||||||
|
@ -16,11 +16,11 @@
|
||||||
<field name="name">Can only modify your user</field>
|
<field name="name">Can only modify your user</field>
|
||||||
<field name="model_id" ref="model_im_user"/>
|
<field name="model_id" ref="model_im_user"/>
|
||||||
<field name="groups" eval="[(6,0,[ref('base.group_user')])]"/>
|
<field name="groups" eval="[(6,0,[ref('base.group_user')])]"/>
|
||||||
<field name="domain_force">[('user', '=', user.id)]</field>
|
<field name="domain_force">[('user_id', '=', user.id)]</field>
|
||||||
<field name="perm_unlink" eval="0"/>
|
<field name="perm_unlink" eval="1"/>
|
||||||
<field name="perm_write" eval="1"/>
|
<field name="perm_write" eval="1"/>
|
||||||
<field name="perm_read" eval="0"/>
|
<field name="perm_read" eval="0"/>
|
||||||
<field name="perm_create" eval="0"/>
|
<field name="perm_create" eval="1"/>
|
||||||
</record>
|
</record>
|
||||||
</data>
|
</data>
|
||||||
</openerp>
|
</openerp>
|
||||||
|
|
|
@ -85,9 +85,8 @@
|
||||||
search_changed: function(e) {
|
search_changed: function(e) {
|
||||||
var users = new instance.web.Model("im.user");
|
var users = new instance.web.Model("im.user");
|
||||||
var self = this;
|
var self = this;
|
||||||
return this.user_search_dm.add(users.call("search_users",
|
return this.user_search_dm.add(users.call("search_users", [this.get("current_search"), ["name", "user_id", "uuid", "im_status"],
|
||||||
[[["name", "ilike", this.get("current_search")], ["id", "<>", instance.session.uid]],
|
USERS_LIMIT], {context:new instance.web.CompoundContext()})).then(function(result) {
|
||||||
["name", "user_id", "uuid", "im_status"], USERS_LIMIT], {context:new instance.web.CompoundContext()})).then(function(result) {
|
|
||||||
self.c_manager.add_to_user_cache(result);
|
self.c_manager.add_to_user_cache(result);
|
||||||
self.$(".oe_im_input").val("");
|
self.$(".oe_im_input").val("");
|
||||||
var old_users = self.users;
|
var old_users = self.users;
|
||||||
|
|
|
@ -92,34 +92,25 @@ function declare($, _, openerp) {
|
||||||
},
|
},
|
||||||
start_polling: function() {
|
start_polling: function() {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
var def = $.when();
|
||||||
var auth_def = null;
|
var uuid = false;
|
||||||
var user_id = null;
|
|
||||||
|
|
||||||
if (this.options.anonymous_mode) {
|
if (this.options.anonymous_mode) {
|
||||||
var uuid = localStorage["oe_livesupport_uuid"];
|
uuid = localStorage["oe_livesupport_uuid"] || false;
|
||||||
var def = $.when(uuid);
|
|
||||||
|
|
||||||
if (! uuid) {
|
if (! uuid) {
|
||||||
def = im_common.connection.rpc("/longpolling/im/gen_uuid", {});
|
def = im_common.connection.rpc("/longpolling/im/gen_uuid", {}).then(function(my_uuid) {
|
||||||
|
uuid = my_uuid;
|
||||||
|
localStorage["oe_livesupport_uuid"] = uuid;
|
||||||
|
return im_common.connection.model("im.user").call("assign_name", [uuid, self.options.userName]);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
var anonymous_user_id = null;
|
|
||||||
auth_def = def.then(function(uuid) {
|
|
||||||
localStorage["oe_livesupport_uuid"] = uuid;
|
|
||||||
return im_common.connection.model("im.user").call("get_by_user_id", [uuid]);
|
|
||||||
}).then(function(my_id) {
|
|
||||||
user_id = my_id["id"];
|
|
||||||
return im_common.connection.model("im.user").call("assign_name", [uuid, self.options.userName]);
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
auth_def = im_common.connection.model("im.user").call("get_by_user_id",
|
|
||||||
[im_common.connection.uid]).then(function(my_id) {
|
|
||||||
user_id = my_id["id"];
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return auth_def.then(function() {
|
return def.then(function() {
|
||||||
self.my_id = user_id;
|
return im_common.connection.model("im.user").call("get_my_id", [uuid]);
|
||||||
|
}).then(function(my_user_id) {
|
||||||
|
self.my_id = my_user_id;
|
||||||
return self.ensure_users([self.my_id]);
|
return self.ensure_users([self.my_id]);
|
||||||
}).then(function() {
|
}).then(function() {
|
||||||
var me = self.users_cache[self.my_id];
|
var me = self.users_cache[self.my_id];
|
||||||
|
|
|
@ -161,9 +161,9 @@ class im_livechat_channel(osv.osv):
|
||||||
|
|
||||||
def get_available_user(self, cr, uid, channel_id, context=None):
|
def get_available_user(self, cr, uid, channel_id, context=None):
|
||||||
channel = self.browse(cr, openerp.SUPERUSER_ID, channel_id, context=context)
|
channel = self.browse(cr, openerp.SUPERUSER_ID, channel_id, context=context)
|
||||||
|
im_user_ids = self.pool.get("im.user").search(cr, uid, [["user_id", "in", [user.id for user in channel.user_ids]]], context=context)
|
||||||
users = []
|
users = []
|
||||||
for user in channel.user_ids:
|
for iuid in im_user_ids:
|
||||||
iuid = self.pool.get("im.user").get_by_user_id(cr, uid, user.id, context=context)["id"]
|
|
||||||
imuser = self.pool.get("im.user").browse(cr, uid, iuid, context=context)
|
imuser = self.pool.get("im.user").browse(cr, uid, iuid, context=context)
|
||||||
if imuser.im_status:
|
if imuser.im_status:
|
||||||
users.append(imuser)
|
users.append(imuser)
|
||||||
|
|
|
@ -124,7 +124,7 @@
|
||||||
<field name="name">History</field>
|
<field name="name">History</field>
|
||||||
<field name="res_model">im.message</field>
|
<field name="res_model">im.message</field>
|
||||||
<field name="view_mode">list</field>
|
<field name="view_mode">list</field>
|
||||||
<field name="domain">["|", ('to_id.user', '=', None), ('from_id.user', '=', None)]</field>
|
<field name="domain">["|", ('to_id.user_id', '=', None), ('from_id.user_id', '=', None)]</field>
|
||||||
</record>
|
</record>
|
||||||
<menuitem name="History" parent="im_livechat" id="history" action="action_history" groups="group_im_livechat_manager"/>
|
<menuitem name="History" parent="im_livechat" id="history" action="action_history" groups="group_im_livechat_manager"/>
|
||||||
|
|
||||||
|
|
|
@ -25,7 +25,7 @@
|
||||||
<field name="name">Live Support Managers can read messages from live support</field>
|
<field name="name">Live Support Managers can read messages from live support</field>
|
||||||
<field name="model_id" ref="im.model_im_message"/>
|
<field name="model_id" ref="im.model_im_message"/>
|
||||||
<field name="groups" eval="[(6,0,[ref('im_livechat.group_im_livechat_manager')])]"/>
|
<field name="groups" eval="[(6,0,[ref('im_livechat.group_im_livechat_manager')])]"/>
|
||||||
<field name="domain_force">["|", ('to_id.user', '=', None), ('from_id.user', '=', None)]</field>
|
<field name="domain_force">["|", ('to_id.user_id', '=', None), ('from_id.user_id', '=', None)]</field>
|
||||||
<field name="perm_unlink" eval="0"/>
|
<field name="perm_unlink" eval="0"/>
|
||||||
<field name="perm_write" eval="0"/>
|
<field name="perm_write" eval="0"/>
|
||||||
<field name="perm_read" eval="1"/>
|
<field name="perm_read" eval="1"/>
|
||||||
|
|
Loading…
Reference in New Issue