You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
4.8 KiB

  1. from odoo import models, fields, api
  2. class MailComposeMessage(models.TransientModel):
  3. _inherit = 'mail.compose.message'
  4. is_private = fields.Boolean(string='Send Internal Message')
  5. class MailMessage(models.Model):
  6. _inherit = 'mail.message'
  7. is_private = fields.Boolean(string='Send Internal Message')
  8. def send_recepients_for_internal_message(self, model, domain):
  9. result = []
  10. default_resource = self.env[model].search(domain)
  11. follower_ids = default_resource.message_follower_ids
  12. recipient_ids = [r.partner_id for r in follower_ids if r.partner_id]
  13. # channel_ids = [c.channel_id for c in follower_ids if c.channel_id]
  14. for recipient in recipient_ids:
  15. result.append({
  16. 'checked': len(recipient.user_ids) > 0,
  17. 'partner_id': recipient.id,
  18. 'full_name': recipient.name,
  19. 'name': recipient.name,
  20. 'email_address': recipient.email,
  21. 'reason': 'Recipient'
  22. })
  23. # for channel in channel_ids:
  24. # result.append({
  25. # 'checked': True,
  26. # 'channel_id': channel.id,
  27. # 'full_name': channel,
  28. # 'name': '# '+channel.name,
  29. # 'reason': 'Channel',
  30. # })
  31. return result
  32. @api.multi
  33. def _notify(self, force_send=False, send_after_commit=True, user_signature=True):
  34. self_sudo = self.sudo()
  35. if not self_sudo.is_private:
  36. super(MailMessage, self)._notify(force_send, send_after_commit, user_signature)
  37. else:
  38. self._notify_mail_private(force_send, send_after_commit, user_signature)
  39. @api.multi
  40. def _notify_mail_private(self, force_send=False, send_after_commit=True, user_signature=True):
  41. """ Compute recipients to notify based on specified recipients and document
  42. followers. Delegate notification to partners to send emails and bus notifications
  43. and to channels to broadcast messages on channels """
  44. group_user = self.env.ref('base.group_user')
  45. # have a sudoed copy to manipulate partners (public can go here with website modules like forum / blog / ... )
  46. self_sudo = self.sudo()
  47. self.ensure_one()
  48. partners_sudo = self_sudo.partner_ids
  49. channels_sudo = self_sudo.channel_ids
  50. if self_sudo.subtype_id and self.model and self.res_id:
  51. followers = self_sudo.env['mail.followers'].search([
  52. ('res_model', '=', self.model),
  53. ('res_id', '=', self.res_id),
  54. ('subtype_ids', 'in', self_sudo.subtype_id.id),
  55. ])
  56. if self_sudo.subtype_id.internal:
  57. followers = followers.filtered(lambda fol: fol.channel_id or (fol.partner_id.user_ids and group_user in fol.partner_id.user_ids[0].mapped('groups_id')))
  58. channels_sudo |= followers.mapped('channel_id')
  59. # remove author from notified partners
  60. if not self._context.get('mail_notify_author', False) and self_sudo.author_id:
  61. partners_sudo = partners_sudo - self_sudo.author_id
  62. # update message, with maybe custom valuesz
  63. message_values = {}
  64. if channels_sudo:
  65. message_values['channel_ids'] = [(6, 0, channels_sudo.ids)]
  66. if partners_sudo:
  67. message_values['needaction_partner_ids'] = [(6, 0, partners_sudo.ids)]
  68. if self.model and self.res_id and hasattr(self.env[self.model], 'message_get_message_notify_values'):
  69. message_values.update(self.env[self.model].browse(self.res_id).message_get_message_notify_values(self, message_values))
  70. if message_values:
  71. self.write(message_values)
  72. # notify partners and channels
  73. # those methods are called as SUPERUSER because portal users posting messages
  74. # have no access to partner model. Maybe propagating a real uid could be necessary.
  75. email_channels = channels_sudo.filtered(lambda channel: channel.email_send)
  76. notif_partners = partners_sudo.filtered(lambda partner: 'inbox' in partner.mapped('user_ids.notification_type'))
  77. if email_channels or partners_sudo - notif_partners:
  78. partners_sudo.search([
  79. '|',
  80. ('id', 'in', (partners_sudo - notif_partners).ids),
  81. ('channel_ids', 'in', email_channels.ids),
  82. ('email', '!=', self_sudo.author_id.email or self_sudo.email_from),
  83. ])._notify(self, force_send=force_send, send_after_commit=send_after_commit, user_signature=user_signature)
  84. channels_sudo._notify(self)
  85. # Discard cache, because child / parent allow reading and therefore
  86. # change access rights.
  87. if self.parent_id:
  88. self.parent_id.invalidate_cache()
  89. return True