diff --git a/addons/mail/mail_followers.py b/addons/mail/mail_followers.py index a9d33f4585d..25772d7bb33 100644 --- a/addons/mail/mail_followers.py +++ b/addons/mail/mail_followers.py @@ -75,16 +75,21 @@ class mail_notification(osv.Model): if not cr.fetchone(): cr.execute('CREATE INDEX mail_notification_partner_id_read_starred_message_id ON mail_notification (partner_id, read, starred, message_id)') - def get_partners_to_notify(self, cr, uid, message, context=None): + def get_partners_to_notify(self, cr, uid, message, partners_to_notify=None, context=None): """ Return the list of partners to notify, based on their preferences. :param browse_record message: mail.message to notify + :param list partners_to_notify: optional list of partner ids restricting + the notifications to process """ notify_pids = [] for notification in message.notification_ids: if notification.read: continue partner = notification.partner_id + # If partners_to_notify specified: restrict to them + if partners_to_notify and partner.id not in partners_to_notify: + continue # Do not send to partners without email address defined if not partner.email: continue @@ -100,16 +105,35 @@ class mail_notification(osv.Model): notify_pids.append(partner.id) return notify_pids - def _notify(self, cr, uid, msg_id, context=None): - """ Send by email the notification depending on the user preferences """ + def _notify(self, cr, uid, msg_id, partners_to_notify=None, context=None): + """ Send by email the notification depending on the user preferences + + :param list partners_to_notify: optional list of partner ids restricting + the notifications to process + """ if context is None: context = {} + mail_message_obj = self.pool.get('mail.message') + + # optional list of partners to notify: subscribe them if not already done or update the notification + if partners_to_notify: + notifications_to_update = [] + notified_partners = [] + notif_ids = self.search(cr, SUPERUSER_ID, [('message_id', '=', msg_id), ('partner_id', 'in', partners_to_notify)], context=context) + for notification in self.browse(cr, SUPERUSER_ID, notif_ids, context=context): + notified_partners.append(notification.partner_id.id) + notifications_to_update.append(notification.id) + partners_to_notify = filter(lambda item: item not in notified_partners, partners_to_notify) + if notifications_to_update: + self.write(cr, SUPERUSER_ID, notifications_to_update, {'read': False}, context=context) + mail_message_obj.write(cr, uid, msg_id, {'notified_partner_ids': [(4, id) for id in partners_to_notify]}, context=context) + # mail_notify_noemail (do not send email) or no partner_ids: do not send, return if context.get('mail_notify_noemail'): return True # browse as SUPERUSER_ID because of access to res_partner not necessarily allowed msg = self.pool.get('mail.message').browse(cr, SUPERUSER_ID, msg_id, context=context) - notify_partner_ids = self.get_partners_to_notify(cr, uid, msg, context=context) + notify_partner_ids = self.get_partners_to_notify(cr, uid, msg, partners_to_notify=partners_to_notify, context=context) if not notify_partner_ids: return True @@ -136,16 +160,12 @@ class mail_notification(osv.Model): mail_values = { 'mail_message_id': msg.id, - 'email_to': [], 'auto_delete': True, 'body_html': body_html, 'email_from': email_from, - 'state': 'outgoing', } - mail_values['email_to'] = ', '.join(mail_values['email_to']) email_notif_id = mail_mail.create(cr, uid, mail_values, context=context) try: return mail_mail.send(cr, uid, [email_notif_id], recipient_ids=notify_partner_ids, context=context) except Exception: return False - diff --git a/addons/mail/mail_message.py b/addons/mail/mail_message.py index 91d46098b4b..2f56e620543 100644 --- a/addons/mail/mail_message.py +++ b/addons/mail/mail_message.py @@ -861,7 +861,7 @@ class mail_message(osv.Model): # message has no subtype_id: pure log message -> no partners, no one notified if not message.subtype_id: return True - + # all followers of the mail.message document have to be added as partners and notified if message.model and message.res_id: fol_obj = self.pool.get("mail.followers") @@ -884,8 +884,7 @@ class mail_message(osv.Model): # notify if partners_to_notify: - self.write(cr, SUPERUSER_ID, [newid], {'notified_partner_ids': [(4, p.id) for p in partners_to_notify]}, context=context) - notification_obj._notify(cr, uid, newid, context=context) + notification_obj._notify(cr, uid, newid, partners_to_notify=[p.id for p in partners_to_notify], context=context) message.refresh() # An error appear when a user receive a notification without notifying diff --git a/addons/mail/mail_thread.py b/addons/mail/mail_thread.py index e4b282060cd..1c7ec4bf4fb 100644 --- a/addons/mail/mail_thread.py +++ b/addons/mail/mail_thread.py @@ -1135,7 +1135,6 @@ class mail_thread(osv.AbstractModel): # find first email message, set it as unread for auto_subscribe fields for them to have a notification if user_id_partner_ids: - notification_obj = self.pool.get('mail.notification') msg_ids = self.pool.get('mail.message').search(cr, uid, [ ('model', '=', self._name), ('res_id', '=', record.id), @@ -1143,12 +1142,7 @@ class mail_thread(osv.AbstractModel): if not msg_ids and record.message_ids: msg_ids = [record.message_ids[-1].id] if msg_ids: - for partner_id in user_id_partner_ids: - notif_ids = notification_obj.search(cr, uid, [('partner_id', '=', partner_id), ('message_id', '=', msg_ids[0])], context=context) - if notif_ids: - notification_obj.write(cr, uid, notif_ids, {'read': False}, context=context) - else: - notification_obj.create(cr, uid, {'partner_id': partner_id, 'message_id': msg_ids[0], 'read': False}, context=context) + self.pool.get('mail.notification')._notify(cr, uid, msg_ids[0], partners_to_notify=user_id_partner_ids, context=context) return True