'Override that adds specific access rights of mail.message, to remove
ids uid could not see according to our custom rules. Please refer to
check_access_rule for more details about those rules.
Non employees users see only message with subtype (aka do not see
internal logs).
After having received ids of a classic search, keep only:
- if author_id == pid, uid is the author, OR
- uid belongs to a notified channel, OR
- uid is in the specified recipients, OR
- uid have read access to the related document is model, res_id
- otherwise: remove the id'

@api.model
def _search(self, args, offset=0, limit=None, order=None, count=False, access_rights_uid=None):

    if (self._uid == SUPERUSER_ID):
        return super(Message, self)._search(args, offset=offset, limit=limit, order=order, count=count, access_rights_uid=access_rights_uid)
    if (not self.env['res.users'].has_group('base.group_user')):
        args = (['&', '&', ('subtype_id', '!=', False), ('subtype_id.internal', '=', False)] + list(args))
    ids = super(Message, self)._search(args, offset=offset, limit=limit, order=order, count=False, access_rights_uid=access_rights_uid)
    if ((not ids) and count):
        return 0
    elif (not ids):
        return ids
    pid = self.env.user.partner_id.id
    (author_ids, partner_ids, channel_ids, allowed_ids) = (set([]), set([]), set([]), set([]))
    model_ids = {}
    super(Message, self.sudo((access_rights_uid or self._uid))).check_access_rights('read')
    self._cr.execute(('SELECT    DISTINCT    m.id,    m.model,    m.res_id,    m.author_id,    partner_rel.res_partner_id,    channel_partner.channel_id    as    channel_id\n                                                FROM    "%s"    m\n                                                LEFT    JOIN    "mail_message_res_partner_rel"    partner_rel\n                                                ON    partner_rel.mail_message_id    =    m.id    AND    partner_rel.res_partner_id    =    (%%s)\n                                                LEFT    JOIN    "mail_message_mail_channel_rel"    channel_rel\n                                                ON    channel_rel.mail_message_id    =    m.id\n                                                LEFT    JOIN    "mail_channel"    channel\n                                                ON    channel.id    =    channel_rel.mail_channel_id\n                                                LEFT    JOIN    "mail_channel_partner"    channel_partner\n                                                ON    channel_partner.channel_id    =    channel.id    AND    channel_partner.partner_id    =    (%%s)\n                                                WHERE    m.id    =    ANY    (%%s)' % self._table), (pid, pid, ids))
    for (id, rmod, rid, author_id, partner_id, channel_id) in self._cr.fetchall():
        if (author_id == pid):
            author_ids.add(id)
        elif (partner_id == pid):
            partner_ids.add(id)
        elif channel_id:
            channel_ids.add(id)
        elif (rmod and rid):
            model_ids.setdefault(rmod, {}).setdefault(rid, set()).add(id)
    allowed_ids = self._find_allowed_doc_ids(model_ids)
    final_ids = (((author_ids | partner_ids) | channel_ids) | allowed_ids)
    if count:
        return len(final_ids)
    else:
        id_list = [id for id in ids if (id in final_ids)]
        return id_list
