@ -1,3 +1,9 @@
# Copyright 2016 x620 <https://github.com/x620>
# Copyright 2016 manawi <https://github.com/manawi>
# Copyright 2017 Artyom Losev <https://github.com/ArtyomLosev>
# Copyright 2019 Artem Rafailov <https://it-projects.info/team/Ommo73/>
# License LGPL-3.0 (https://www.gnu.org/licenses/lgpl.html).
from odoo import models , fields , api
@ -13,17 +19,16 @@ class MailMessage(models.Model):
is_private = fields . Boolean ( string = ' Send Internal Message ' )
def send_recepients_for_internal_message ( self , model , domain ) :
result = [ ]
result = { ' partners ' : [ ] , ' channels ' : [ ] }
default_resource = self . env [ model ] . search ( domain )
follower_ids = default_resource . message_follower_ids
internal_ids = self . get_internal_users_ids ( )
recipient_ids = [ r . partner_id for r in follower_ids if r . partner_id ]
# channel_ids = [c.channel_id for c in follower_ids if c.channel_id ]
channel_ids = [ c . channel_id for c in follower_ids if c . channel_id ]
for recipient in recipient_ids :
result . append ( {
' checked ' : recipient . user_ids . id in internal_ids ,
result [ ' partners ' ] . append ( {
' checked ' : recipient . user_ids . id and not any ( recipient . user_ids . mapped ( ' share ' ) ) ,
' partner_id ' : recipient . id ,
' full_name ' : recipient . name ,
' name ' : recipient . name ,
@ -31,14 +36,14 @@ class MailMessage(models.Model):
' reason ' : ' Recipient '
} )
# for channel in channel_ids :
# result.append( {
# 'checked': True,
# 'channel_id': c hannel.id,
# 'full_name': c hannel,
# 'name': '# '+channel.name,
# 'reason': 'Channel',
# } )
for channel in channel_ids :
result [ ' channels ' ] . append ( {
' checked ': True ,
' channel_id ': c hannel . id ,
' full_name ': c hannel . name ,
' name ': ' # '+ channel . name ,
' reason ': ' Channel ',
} )
return result
@api.multi
@ -54,7 +59,6 @@ class MailMessage(models.Model):
""" Compute recipients to notify based on specified recipients and document
followers . Delegate notification to partners to send emails and bus notifications
and to channels to broadcast messages on channels """
group_user = self . env . ref ( ' base.group_user ' )
# have a sudoed copy to manipulate partners (public can go here with website modules like forum / blog / ... )
self_sudo = self . sudo ( )
@ -62,16 +66,6 @@ class MailMessage(models.Model):
partners_sudo = self_sudo . partner_ids
channels_sudo = self_sudo . channel_ids
if self_sudo . subtype_id and self . model and self . res_id :
followers = self_sudo . env [ ' mail.followers ' ] . search ( [
( ' res_model ' , ' = ' , self . model ) ,
( ' res_id ' , ' = ' , self . res_id ) ,
( ' subtype_ids ' , ' in ' , self_sudo . subtype_id . id ) ,
] )
if self_sudo . subtype_id . internal :
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 ' ) ) )
channels_sudo | = followers . mapped ( ' channel_id ' )
# remove author from notified partners
if not self . _context . get ( ' mail_notify_author ' , False ) and self_sudo . author_id :
partners_sudo = partners_sudo - self_sudo . author_id
@ -108,6 +102,16 @@ class MailMessage(models.Model):
return True
def get_internal_users_ids ( self ) :
internal_users_ids = self . env [ ' res.users ' ] . search ( [ ( ' share ' , ' = ' , False ) ] ) . ids
return internal_users_ids
class MailThread ( models . AbstractModel ) :
_inherit = ' mail.thread '
@api.multi
@api.returns ( ' self ' , lambda value : value . id )
def message_post ( self , body = ' ' , subject = None , message_type = ' notification ' , subtype = None , parent_id = False ,
attachments = None , content_subtype = ' html ' , * * kwargs ) :
if ' channel_ids ' in kwargs :
kwargs [ ' channel_ids ' ] = [ ( 4 , pid ) for pid in kwargs [ ' channel_ids ' ] ]
return super ( MailThread , self ) . message_post ( body , subject , message_type ,
subtype , parent_id , attachments ,
content_subtype , * * kwargs )