From babd47b963aa421c5b92624a485eb509ea7e970e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibault=20Delavall=C3=A9e?= Date: Wed, 22 Feb 2012 10:09:23 +0100 Subject: [PATCH] [IMP] Notification push moved from mail.message to mail.thread to use message_get_subscribers method. Also added a temporary debug log on message.create method, to help tracking messages not using mail.thread. bzr revid: tde@openerp.com-20120222090923-1h6wx3bkwr5ngt4d --- addons/mail/mail_message.py | 31 +++++++++---------------------- addons/mail/mail_thread.py | 35 ++++++++++++++++++++++++++++++++--- 2 files changed, 41 insertions(+), 25 deletions(-) diff --git a/addons/mail/mail_message.py b/addons/mail/mail_message.py index 03eb57bfd0f..87e73dbb471 100644 --- a/addons/mail/mail_message.py +++ b/addons/mail/mail_message.py @@ -197,31 +197,18 @@ class mail_message(osv.osv): #------------------------------------------------------ def create(self, cr, uid, vals, context=None): - # OpenSocial: notifications do not come from any user but from system - if vals.get('type') == 'notification': vals['user_id'] = False - need_action_pushed = False + # temporary log directly created messages (to debug OpenSocial) + if not 'mail.thread' in context: + _logger.warning('Creating message without using mail.thread API') + _logger.warning('Message details: %s', str(vals)) msg_id = super(mail_message, self).create(cr, uid, vals, context) - # push the message to suscribed users - subscription_obj = self.pool.get('mail.subscription') - notification_obj = self.pool.get('mail.notification') - # not pure-email: check for subscriptions - if not 'need_action_user_id' in vals: vals['need_action_user_id'] = False - if not 'model' in vals: vals['model'] = False - if not 'res_id' in vals: vals['res_id'] = 0 - sub_ids = subscription_obj.search(cr, uid, ['&', - ('res_model', '=', vals['model']), - ('res_id', '=', vals['res_id'])], context=context) - subs = subscription_obj.browse(cr, uid, sub_ids, context=context) - for sub in subs: - notification_obj.create(cr, uid, {'user_id': sub.user_id.id, 'message_id': msg_id}, context=context) - if vals['need_action_user_id'] == sub.user_id: need_action_pushed = True - # push to need_action_user_id if user does not follow the object - if vals['need_action_user_id'] and not need_action_pushed: - notification_obj.create(cr, uid, {'user_id': vals['need_action_user_id'], 'message_id': msg_id}, context=context) return msg_id def get_pushed_messages(self, cr, uid, ids, filter_search=False, context=None): - """Wall: get messages to display""" + """OpenSocial: wall: get messages to display (=pushed notifications) + :param filter_search: TODO + :return: dict { res_model: { res_id: [pushed message list] }} + """ notification_obj = self.pool.get('mail.notification') notification_ids = notification_obj.search(cr, uid, [('user_id', '=', uid)], context=context) notifications = notification_obj.browse(cr, uid, notification_ids, context=context) @@ -230,7 +217,7 @@ class mail_message(osv.osv): msgs = self.read(cr, uid, msg_ids, context=context) print msgs - # TODO / REMARK: classify based on res_model / res_id to have a 1_level hierarchy ? + # sort return msgs diff --git a/addons/mail/mail_thread.py b/addons/mail/mail_thread.py index b3592e0954e..89273389dd2 100644 --- a/addons/mail/mail_thread.py +++ b/addons/mail/mail_thread.py @@ -72,6 +72,33 @@ class mail_thread(osv.osv): #------------------------------------------------------ # Generic message api #------------------------------------------------------ + + def message_create(self, cr, uid, ids, vals, context=None): + """OpenSocial: wrapper of mail.message create method + - creates the mail.message + - push the message to subscribed users""" + subscription_obj = self.pool.get('mail.subscription') + notification_obj = self.pool.get('mail.notification') + + # notifications do not come from any user, but from system + if vals.get('type') == 'notification': vals['user_id'] = False + if not 'need_action_user_id' in vals: vals['need_action_user_id'] = False + if not 'model' in vals: vals['model'] = False + if not 'res_id' in vals: vals['res_id'] = 0 + need_action_pushed = False + + msg_id = self.pool.get('mail.message').create(cr, uid, vals, context=context) + + # push the message to suscribed users + users = self.message_get_subscribers(cr, uid, ids, context=context) + for user in users: + notification_obj.create(cr, uid, {'user_id': user['id'], 'message_id': msg_id}, context=context) + if vals['need_action_user_id'] == user['id']: need_action_pushed = True + # push to need_action_user_id if user does not follow the object + if vals['need_action_user_id'] and not need_action_pushed: + notification_obj.create(cr, uid, {'user_id': vals['need_action_user_id'], 'message_id': msg_id}, context=context) + return msg_id + def message_capable_models(self, cr, uid, context=None): ret_dict = {} for model_name in self.pool.obj_list(): @@ -197,7 +224,9 @@ class mail_thread(osv.osv): 'reply_to': reply_to, 'original': original, } - mail_message.create(cr, uid, data, context=context) + #mail_message.create(cr, uid, data, context=context) + context['mail.thread'] = True + self.message_create(cr, uid, [thread.id], data, context=context) return True def message_append_dict(self, cr, uid, ids, msg_dict, context=None): @@ -533,14 +562,14 @@ class mail_thread(osv.osv): # Subscription mechanism #------------------------------------------------------ - def message_get_subscribers_ids(self, cr, uid, ids, context=None): + def _message_get_subscribers_ids(self, cr, uid, ids, context=None): subscription_obj = self.pool.get('mail.subscription') 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 [sub['user_id'][0] for sub in subs] def message_get_subscribers(self, cr, uid, ids, context=None): - user_ids = self.message_get_subscribers_ids(cr, uid, ids, context=context) + user_ids = self._message_get_subscribers_ids(cr, uid, ids, context=context) users = self.pool.get('res.users').read(cr, uid, user_ids, context=context) return users