Search code examples
pythonodooodoo-12

Add channel as follower according the two users


Im trying via Automated Action to auto add Channel according the 2 users where they are followers.

Code so far:

record.message_subscribe(partner_ids=[record.user_id.partner_id.id, record.x_studio_subcontractor.user_id.partner_id.id])

The above code make the 2 users followers, what code should apply so I can make their channel also follower to task?


Solution

  • Search for chat channel between this two user both partner should be member of this channel, and the type of the chat channel is 'chat', for private channel the type of channel is 'channel' user can have multiple channels.

    partner_ids = [record.user_id.partner_id.id, 
                  ecord.x_studio_subcontractor.user_id.partner_id.id]
    
    channel_ids = self.env['mail.channel'].search([('channel_partner_ids', '=', partner_ids[0]),
                                             ('channel_partner_ids', '=', partner_ids[1]),
                                             ('public', '=', 'private'),
                                             ('channel_type', '=', 'chat'),  # only chat channel it should be only one
                                             ]).ids or None
    
    record.message_subscribe(partner_ids=partner_ids, channel_ids=channel_ids)
    

    @Fotic

    With the below attributes on search is not adding the channel (probably because there was 2 channels (Announcements and the Private))

    ('public', '=', 'private'),
    ('channel_type', '=', 'chat'),  
    

    My final solution:

    partner_ids = [record.user_id.partner_id.id,record.x_studio_subcontractor.user_id.partner_id.id]
    
    channel_ids = record.env['mail.channel'].search([('channel_partner_ids', '=', partner_ids[0]),
                                             ('channel_partner_ids', '=', partner_ids[1]),
                                             ('group_ids', '=', False),
                                             ]).ids or None
    
    record.message_subscribe(partner_ids=partner_ids, channel_ids=channel_ids) 
    

    Anyway thank you very much for the help @Charif DZ