def format_push_rules_for_user(user, rawrules, enabled_map):

    ruleslist = []
    for rawrule in rawrules:
        rule = dict(rawrule)
        rule["conditions"] = json.loads(rawrule["conditions"])
        rule["actions"] = json.loads(rawrule["actions"])
        ruleslist.append(rule)

    # We're going to be mutating this a lot, so do a deep copy
    ruleslist = copy.deepcopy(list_with_base_rules(ruleslist))

    rules = {'global': {}, 'device': {}}

    rules['global'] = _add_empty_priority_class_arrays(rules['global'])

    for r in ruleslist:
        rulearray = None

        template_name = _priority_class_to_template_name(r['priority_class'])

        # Remove internal stuff.
        for c in r["conditions"]:
            c.pop("_id", None)

            pattern_type = c.pop("pattern_type", None)
            if pattern_type == "user_id":
                c["pattern"] = user.to_string()
            elif pattern_type == "user_localpart":
                c["pattern"] = user.localpart

        rulearray = rules['global'][template_name]

        template_rule = _rule_to_template(r)
        if template_rule:
            if r['rule_id'] in enabled_map:
                template_rule['enabled'] = enabled_map[r['rule_id']]
            elif 'enabled' in r:
                template_rule['enabled'] = r['enabled']
            else:
                template_rule['enabled'] = True
            rulearray.append(template_rule)

    return rules
Converts a list of rawrules and a enabled map into nested dictionaries
dataset/ETHPy150Open matrix-org/synapse/synapse/push/clientformat.py/format_push_rules_for_user