[REF] mail.thread: fixed an error in is_subscriber, cleaned code; mail.js: threadview widget: cleaned code

bzr revid: tde@openerp.com-20120216160302-g5knaca3vie1ekot
This commit is contained in:
Thibault Delavallée 2012-02-16 17:03:02 +01:00
parent 8e14b6a706
commit 67bb139660
3 changed files with 59 additions and 58 deletions

View File

@ -241,23 +241,19 @@ class mail_thread(osv.osv):
# Message loading
def message_load_ids(self, cr, uid, ids, context=None):
""" OpenSocial feature added this method
get ids of thread messages
""" OpenSocial feature: return thread messages ids (for web compatibility)
loading messages: search in mail.messages where res_id = ids, (res_)model = current model
"""
msg_obj = self.pool.get('mail.message')
msg_ids = [msg_obj.search(cr, uid, ['&', ('res_id', '=', id), ('model', '=', self._name)], context=context) for id in ids]
return msg_ids[0]
msg_ids = msg_obj.search(cr, uid, ['&', ('res_id', 'in', ids), ('model', '=', self._name)], context=context)
return msg_ids
def message_load(self, cr, uid, ids, context=None):
""" OpenSocial feature added this method
loading message: search in mail.messages where res_id = ids, (res_)model = current model
""" OpenSocial feature: return thread messages
loading messages: search in mail.messages where res_id = ids, (res_)model = current model
"""
msg_ids = []
msg_obj = self.pool.get('mail.message')
for id in ids:
msg_ids += msg_obj.search(cr, uid, ['&', ('res_id', '=', id), ('model', '=', self._name)], context=context)
msgs = msg_obj.read(cr, uid, msg_ids, context=context)
return msgs
msg_ids = self.message_load_ids(cr, uid, ids, context=context)
return self.pool.get('mail.message').read(cr, uid, msg_ids, context=context)
#------------------------------------------------------
# Email specific
@ -528,9 +524,10 @@ class mail_thread(osv.osv):
Find by: res_id (thread id), model (self._name), need_action_user_id != false
"""
msg_obj = self.pool.get('mail.message')
for id in ids:
msg_ids = msg_obj.search(cr, uid, ['&', ('res_id', '=', id), ('model', '=', self._name)], context=context)
msg_obj.write(cr, uid, msg_ids, {'need_action_user_id': False}, context=context)
msg_ids = msg_obj.search(cr, uid,
['&', '&', ('res_id', 'in', ids), ('model', '=', self._name), ('need_action_user_id', '!=', False)], context=context)
msg_obj.write(cr, uid, msg_ids, {'need_action_user_id': False}, context=context)
return True
#------------------------------------------------------
# Subscription mechanism
@ -538,22 +535,17 @@ class mail_thread(osv.osv):
def message_get_subscribers(self, cr, uid, ids, context=None):
subscription_obj = self.pool.get('mail.subscription')
for id in ids:
sub_ids = subscription_obj.search(cr, uid, ['&', ('res_model', '=', self._name), ('res_id', '=', id)], context=context)
subs = subscription_obj.browse(cr, uid, sub_ids, context=context)
return subs
def message_get_subscribers_web(self, cr, uid, ids, context=None):
subscription_obj = self.pool.get('mail.subscription')
for id in ids:
sub_ids = subscription_obj.search(cr, uid, ['&', ('res_model', '=', self._name), ('res_id', '=', id)], context=context)
subs = subscription_obj.read(cr, uid, sub_ids, context=context)
sub_ids = subscription_obj.search(cr, uid, ['&', ('res_model', '=', self._name), ('res_id', 'in', ids)], context=context)
subs = subscription_obj.read(cr, uid, sub_ids, context=context)
return subs
def message_is_subscriber(self, cr, uid, ids, context=None):
for subscription in self.message_get_subscribers(cr, uid, ids, context=context):
if subscription.user_id == uid: return True
return False
subscription_obj = self.pool.get('mail.subscription')
sub_ids = subscription_obj.search(cr, uid,
['&', '&', ('res_model', '=', self._name), ('res_id', 'in', ids), ('user_id', '=', uid)], context=context)
if len(sub_ids) > 1:
print 'cacaprout error !'
return True if sub_ids else False
def message_subscribe(self, cr, uid, ids, context=None):
subscription_obj = self.pool.get('mail.subscription')
@ -565,9 +557,8 @@ class mail_thread(osv.osv):
def message_unsubscribe(self, cr, uid, ids, context=None):
subscription_obj = self.pool.get('mail.subscription')
subscriber_id = uid # TODO
sub_ids = []
for id in ids:
sub_ids += subscription_obj.search(cr, uid, ['&', '&', ('res_model', '=', self._name), ('res_id', '=', id), ('user_id', '=', subscriber_id)], context=context)
sub_ids = subscription_obj.search(cr, uid,
['&', '&', ('res_model', '=', self._name), ('res_id', 'in', ids), ('user_id', '=', subscriber_id)], context=context)
subscription_obj.unlink(cr, uid, sub_ids, context=context)
return True

View File

@ -14,47 +14,55 @@ openerp.mail = function(session) {
template: 'ThreadView',
init: function() {
// this.timeout;
this.follow_state = 0;
this._super.apply(this, arguments);
/* DataSets */
this.ds = new session.web.DataSet(this, this.view.model);
this.ds_sub = new session.web.DataSet(this, 'mail.subscription');
// this.ds_msg = new session.web.DataSet(this, 'mail.message');
},
start: function() {
var self = this;
this._super.apply(this, arguments);
/* bind follow and unfollow buttons */
self.$element.find('button.oe_mail_button_follow').bind('click', function () { self.do_follow(); });
self.$element.find('button.oe_mail_button_follow').hide();
self.$element.find('button.oe_mail_button_unfollow').bind('click', function () { self.do_unfollow(); });
self.$element.find('button.oe_mail_button_unfollow').hide();
/* bind buttons */
self.$element.find('button.oe_mail_button_comment').bind('click', function () { self.do_comment(); });
self.$element.find('button.oe_mail_button_followers').bind('click', function () { self.do_toggle_followers(); });
/* find wich (un)follow buttons to show */
var call_res = this.ds.call('message_is_subscriber', [[this.session.uid]]).then(function (records) {
if (records == true) { self.follow_state = 1; self.$element.find('button.oe_mail_button_unfollow').show(); }
else { self.follow_state = 0; self.$element.find('button.oe_mail_button_follow').show(); }
});
self.$element.find('button.oe_mail_button_follow').bind('click', function () { self.do_follow(); });
self.$element.find('button.oe_mail_button_unfollow').bind('click', function () { self.do_unfollow(); });
/* hide follow/unfollow buttons */
self.$element.find('button.oe_mail_button_follow').hide();
self.$element.find('button.oe_mail_button_unfollow').hide();
},
stop: function () {
// clearTimeout(this.timeout);
this._super();
console.log('stop');
this._super.apply(this, arguments);
},
set_value: function() {
var self = this;
this._super.apply(this, arguments);
/* hide follow/unfollow buttons */
self.$element.find('button.oe_mail_button_follow').hide();
self.$element.find('button.oe_mail_button_unfollow').hide();
if (! this.view.datarecord.id) { return; }
return this.fetch_data();
/* find wich (un)follow buttons to show */
var call_res = this.ds.call('message_is_subscriber', [[this.view.datarecord.id]]).then(function (records) {
if (records == true) { self.follow_state = 1; self.$element.find('button.oe_mail_button_unfollow').show(); }
else { self.follow_state = 0; self.$element.find('button.oe_mail_button_follow').show(); }
});
/* fetch comments and subscribers */
this.fetch_subscribers();
return this.fetch_comments();
},
fetch_data: function () {
fetch_comments: function () {
var load_res = this.ds.call('message_load', [[this.view.datarecord.id]]).then(
this.proxy('display_comments'));
var follow_res = this.ds.call('message_get_subscribers_web', [[this.view.datarecord.id]]).then(
return load_res;
},
fetch_subscribers: function () {
var follow_res = this.ds.call('message_get_subscribers', [[this.view.datarecord.id]]).then(
this.proxy('display_followers'));
return follow_res;
},
@ -69,35 +77,37 @@ openerp.mail = function(session) {
});
$('<div class="oe_mail_comment">').html(render_res).appendTo(self.$element.find('div.oe_mail_msg'));
});
// this.timeout = setTimeout(this.proxy('fetch_messages'), 5000);
},
display_followers: function (records) {
this.$element.find('div.oe_mail_followers').empty();
var self = this;
_(records).each(function (record) {
console.log(record);
// <div class="oe_mail_followers_vignette" title="Raoul Grobedon"><img src="people.png"/></div>
$('<div class="oe_mail_followers_vignette">').text(record.user_id[1]).appendTo(self.$element.find('div.oe_mail_followers'));
});
},
do_follow: function () {
this.$element.find('button.oe_mail_button_unfollow').show();
this.$element.find('button.oe_mail_button_follow').hide();
return this.ds_sub.create({'res_model': this.view.model, 'user_id': this.session.uid, 'res_id': this.view.datarecord.id}).then();
this.do_toggle_follow();
return this.ds.call('message_subscribe', [[this.view.datarecord.id]]).then();
},
do_unfollow: function () {
this.$element.find('button.oe_mail_button_follow').show();
this.$element.find('button.oe_mail_button_unfollow').hide();
this.do_toggle_follow();
return this.ds.call('message_unsubscribe', [[this.view.datarecord.id]]).then();
},
do_comment: function () {
var body_text = this.$element.find('textarea').val();
return this.ds.call('message_append_note', [[this.view.datarecord.id], 'Reply comment', body_text, type='comment']).then(
this.proxy('fetch_messages'));
this.proxy('fetch_comments'));
},
do_toggle_follow: function () {
this.follow_state = 1 - this.follow_state;
this.$element.find('button.oe_mail_button_unfollow').toggle();
this.$element.find('button.oe_mail_button_follow').toggle();
},
do_toggle_followers: function () {

View File

@ -56,4 +56,4 @@
</div>
</div>
</template>
</template>