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.

117 lines
5.2 KiB

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