niv-openerp
2013-09-02 11:31:04 +02:00
parent 58e064d078
commit 4dc6ac8da4
9 changed files with 116 additions and 71 deletions
+3 -4
View File
@@ -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):
+16 -5
View File
@@ -5,11 +5,11 @@
<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="groups" eval="[(6,0,[ref('base.group_user')])]"/>
<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_write" eval="0"/>
<field name="domain_force">["|", ('to_id.user_id', 'in', [user.id]), ('from_id.user_id', '=', user.id)]</field>
<field name="perm_read" eval="1"/>
<field name="perm_write" eval="0"/>
<field name="perm_create" eval="0"/>
<field name="perm_unlink" eval="0"/>
</record>
<record id="users_rule_1" model="ir.rule">
@@ -17,10 +17,21 @@
<field name="model_id" ref="model_im_user"/>
<field name="groups" eval="[(6,0,[ref('base.group_user')])]"/>
<field name="domain_force">[('user_id', '=', user.id)]</field>
<field name="perm_unlink" eval="1"/>
<field name="perm_write" eval="1"/>
<field name="perm_read" eval="0"/>
<field name="perm_write" eval="1"/>
<field name="perm_create" eval="1"/>
<field name="perm_unlink" eval="1"/>
</record>
<record id="sessions_rule_1" model="ir.rule">
<field name="name">Can modify sessions if you are in it.</field>
<field name="model_id" ref="model_im_session"/>
<field name="groups" eval="[(6,0,[ref('base.group_user')])]"/>
<field name="domain_force">[('user_ids', 'in', [user.id])]</field>
<field name="perm_read" eval="0"/>
<field name="perm_write" eval="1"/>
<field name="perm_create" eval="0"/>
<field name="perm_unlink" eval="0"/>
</record>
</data>
</openerp>
+2 -1
View File
@@ -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
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
1 id name model_id:id group_id:id perm_read perm_write perm_create perm_unlink
2 access_im_message im.message model_im_message base.group_user 1 0 1 0
3 access_im_user im.user model_im_user 1 1 1 0
4 access_im_session im.session model_im_session base.group_user 1 1 1 0
+4 -1
View File
@@ -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);
});
},
});
+83 -57
View File
@@ -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();
}
+2 -2
View File
@@ -4,12 +4,12 @@
<t t-name="im_common.conversation">
<div class="oe_im_chatview_header">
<img t-att-src="to_url('/im/static/src/img/green.png')" class="oe_im_chatview_online"/>
<t t-esc="widget.user.get('name')"/>
<t t-esc="widget.users[0].get('name')"/>
<scan class="oe_im_chatview_nbr_messages" />
<button class="oe_im_chatview_close">×</button>
</div>
<div class="oe_im_chatview_disconnected">
<t t-esc='widget.user.get("name") + _t(" is offline. He/She will receive your messages on his/her next connection.")'/>
<t t-esc='widget.users[0].get("name") + _t(" is offline. He/She will receive your messages on his/her next connection.")'/>
</div>
<div class="oe_im_chatview_content">
<div></div>
+2 -1
View File
@@ -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),
}
"""
+2
View File
@@ -120,6 +120,7 @@
</field>
</record>
<!--
<record model="ir.actions.act_window" id="action_history">
<field name="name">History</field>
<field name="res_model">im.message</field>
@@ -141,6 +142,7 @@
</tree>
</field>
</record>
-->
</data>
</openerp>
@@ -21,6 +21,7 @@
<field name="users" eval="[(4, ref('base.user_root'))]"/>
</record>
<!--
<record id="message_rule_1" model="ir.rule">
<field name="name">Live Support Managers can read messages from live support</field>
<field name="model_id" ref="im.model_im_message"/>
@@ -31,6 +32,7 @@
<field name="perm_read" eval="1"/>
<field name="perm_create" eval="0"/>
</record>
-->
</data>
</openerp>