diff --git a/addons/mail/models/mail_message_subtype.py b/addons/mail/models/mail_message_subtype.py index ee754acdbed..a634c9d61c0 100644 --- a/addons/mail/models/mail_message_subtype.py +++ b/addons/mail/models/mail_message_subtype.py @@ -57,6 +57,9 @@ class MailMessageSubtype(models.Model): @tools.ormcache('model_name') def _get_auto_subscription_subtypes(self, model_name): """ Return data related to auto subscription based on subtype matching. + Here model_name indicates child model (like a task) on which we want to + make subtype matching based on its parents (like a project). + Example with tasks and project : * generic: discussion, res_model = False @@ -65,13 +68,15 @@ class MailMessageSubtype(models.Model): Returned data - * all_ids: all subtypes that are generic or related to task and project - * def_ids: for task, default subtypes ids - * int_ids: for task, internal-only default subtypes ids + * child_ids: all subtypes that are generic or related to task (res_model = False or model_name) + * def_ids: default subtypes ids (either generic or task specific) + * all_int_ids: all internal-only subtypes ids (generic or task or project) * parent: dict(parent subtype id, child subtype id), i.e. {task_new.id: new.id} * relation: dict(parent_model, relation_fields), i.e. {'project.project': ['project_id']} """ - all_ids, def_ids, int_ids, parent, relation = list(), list(), list(), dict(), dict() + child_ids, def_ids = list(), list() + all_int_ids = list() + parent, relation = dict(), dict() subtypes = self.sudo().search([ '|', '|', ('res_model', '=', False), ('res_model', '=', model_name), @@ -79,15 +84,16 @@ class MailMessageSubtype(models.Model): ]) for subtype in subtypes: if not subtype.res_model or subtype.res_model == model_name: - all_ids += subtype.ids + child_ids += subtype.ids if subtype.default: def_ids += subtype.ids elif subtype.relation_field: parent[subtype.id] = subtype.parent_id.id relation.setdefault(subtype.res_model, set()).add(subtype.relation_field) + # required for backward compatibility if subtype.internal: - int_ids += subtype.ids - return all_ids, def_ids, int_ids, parent, relation + all_int_ids += subtype.ids + return child_ids, def_ids, all_int_ids, parent, relation @api.model def default_subtypes(self, model_name): diff --git a/addons/mail/models/mail_thread.py b/addons/mail/models/mail_thread.py index 2ea9f9ed872..57f47d94a27 100644 --- a/addons/mail/models/mail_thread.py +++ b/addons/mail/models/mail_thread.py @@ -2784,9 +2784,10 @@ class MailThread(models.AbstractModel): new_partners, new_channels = dict(), dict() - # fetch auto subscription subtypes data + # return data related to auto subscription based on subtype matching (aka: + # default task subtypes or subtypes from project triggering task subtypes) updated_relation = dict() - all_ids, def_ids, int_ids, parent, relation = self.env['mail.message.subtype']._get_auto_subscription_subtypes(self._name) + child_ids, def_ids, all_int_ids, parent, relation = self.env['mail.message.subtype']._get_auto_subscription_subtypes(self._name) # check effectively modified relation field for res_model, fnames in relation.items(): @@ -2795,15 +2796,21 @@ class MailThread(models.AbstractModel): udpated_fields = [fname for fnames in updated_relation.values() for fname in fnames if updated_values.get(fname)] if udpated_fields: + # fetch "parent" subscription data (aka: subtypes on project to propagate on task) doc_data = [(model, [updated_values[fname] for fname in fnames]) for model, fnames in updated_relation.items()] res = self.env['mail.followers']._get_subscription_data(doc_data, None, None, include_pshare=True, include_active=True) for fid, rid, pid, cid, subtype_ids, pshare, active in res: + # use project.task_new -> task.new link sids = [parent[sid] for sid in subtype_ids if parent.get(sid)] - sids += [sid for sid in subtype_ids if sid not in parent and sid in def_ids or sid in int_ids] + # add checked subtypes matching model_name + sids += [sid for sid in subtype_ids if sid not in parent and sid in child_ids] if pid and active: # auto subscribe only active partners - new_partners[pid] = (set(sids) & set(all_ids)) - set(int_ids) if pshare else set(sids) & set(all_ids) - if cid: - new_channels[cid] = (set(sids) & set(all_ids)) - set(int_ids) + if pshare: # remove internal subtypes for customers + new_partners[pid] = set(sids) - set(all_int_ids) + else: + new_partners[pid] = set(sids) + if cid: # never subscribe channels to internal subtypes + new_channels[cid] = set(sids) - set(all_int_ids) notify_data = dict() res = self._message_auto_subscribe_followers(updated_values, def_ids)