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.

159 lines
5.7 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 MIT (https://opensource.org/licenses/MIT).
  6. from odoo import api, fields, models
  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. {
  22. "checked": recipient.user_ids.id
  23. and not any(recipient.user_ids.mapped("share")),
  24. "partner_id": recipient.id,
  25. "full_name": recipient.name,
  26. "name": recipient.name,
  27. "email_address": recipient.email,
  28. "reason": "Recipient",
  29. }
  30. )
  31. for channel in channel_ids:
  32. result["channels"].append(
  33. {
  34. "checked": True,
  35. "channel_id": channel.id,
  36. "full_name": channel.name,
  37. "name": "# " + channel.name,
  38. "reason": "Channel",
  39. }
  40. )
  41. return result
  42. @api.multi
  43. def _notify(self, force_send=False, send_after_commit=True, user_signature=True):
  44. self_sudo = self.sudo()
  45. if not self_sudo.is_private:
  46. super(MailMessage, self)._notify(
  47. force_send, send_after_commit, user_signature
  48. )
  49. else:
  50. self._notify_mail_private(force_send, send_after_commit, user_signature)
  51. @api.multi
  52. def _notify_mail_private(
  53. self, force_send=False, send_after_commit=True, user_signature=True
  54. ):
  55. """ Compute recipients to notify based on specified recipients and document
  56. followers. Delegate notification to partners to send emails and bus notifications
  57. and to channels to broadcast messages on channels """
  58. # have a sudoed copy to manipulate partners (public can go here with website modules like forum / blog / ... )
  59. self_sudo = self.sudo()
  60. self.ensure_one()
  61. partners_sudo = self_sudo.partner_ids
  62. channels_sudo = self_sudo.channel_ids
  63. # remove author from notified partners
  64. if not self._context.get("mail_notify_author", False) and self_sudo.author_id:
  65. partners_sudo = partners_sudo - self_sudo.author_id
  66. # update message, with maybe custom valuesz
  67. message_values = {}
  68. if channels_sudo:
  69. message_values["channel_ids"] = [(6, 0, channels_sudo.ids)]
  70. if partners_sudo:
  71. message_values["needaction_partner_ids"] = [(6, 0, partners_sudo.ids)]
  72. if (
  73. self.model
  74. and self.res_id
  75. and hasattr(self.env[self.model], "message_get_message_notify_values")
  76. ):
  77. message_values.update(
  78. self.env[self.model]
  79. .browse(self.res_id)
  80. .message_get_message_notify_values(self, message_values)
  81. )
  82. if message_values:
  83. self.write(message_values)
  84. # notify partners and channels
  85. # those methods are called as SUPERUSER because portal users posting messages
  86. # have no access to partner model. Maybe propagating a real uid could be necessary.
  87. email_channels = channels_sudo.filtered(lambda channel: channel.email_send)
  88. notif_partners = partners_sudo.filtered(
  89. lambda partner: "inbox" in partner.mapped("user_ids.notification_type")
  90. )
  91. if email_channels or partners_sudo - notif_partners:
  92. partners_sudo.search(
  93. [
  94. "|",
  95. ("id", "in", (partners_sudo - notif_partners).ids),
  96. ("channel_ids", "in", email_channels.ids),
  97. ("email", "!=", self_sudo.author_id.email or self_sudo.email_from),
  98. ]
  99. )._notify(
  100. self,
  101. force_send=force_send,
  102. send_after_commit=send_after_commit,
  103. user_signature=user_signature,
  104. )
  105. channels_sudo._notify(self)
  106. # Discard cache, because child / parent allow reading and therefore
  107. # change access rights.
  108. if self.parent_id:
  109. self.parent_id.invalidate_cache()
  110. return True
  111. class MailThread(models.AbstractModel):
  112. _inherit = "mail.thread"
  113. @api.multi
  114. @api.returns("self", lambda value: value.id)
  115. def message_post(
  116. self,
  117. body="",
  118. subject=None,
  119. message_type="notification",
  120. subtype=None,
  121. parent_id=False,
  122. attachments=None,
  123. content_subtype="html",
  124. **kwargs
  125. ):
  126. if "channel_ids" in kwargs:
  127. kwargs["channel_ids"] = [(4, pid) for pid in kwargs["channel_ids"]]
  128. return super(MailThread, self).message_post(
  129. body,
  130. subject,
  131. message_type,
  132. subtype,
  133. parent_id,
  134. attachments,
  135. content_subtype,
  136. **kwargs
  137. )