setPushRule method

Future<void> setPushRule(
  1. String scope,
  2. PushRuleKind kind,
  3. String ruleId,
  4. List<Object?> actions, {
  5. String? before,
  6. String? after,
  7. List<PushCondition>? conditions,
  8. String? pattern,
})

This endpoint allows the creation and modification of user defined push rules.

If a rule with the same rule_id already exists among rules of the same kind, it is updated with the new parameters, otherwise a new rule is created.

If both after and before are provided, the new or updated rule must be the next most important rule with respect to the rule identified by before.

If neither after nor before are provided and the rule is created, it should be added as the most important user defined rule among rules of the same kind.

When creating push rules, they MUST be enabled by default.

scope global to specify global rules.

kind The kind of rule

ruleId The identifier for the rule. If the string starts with a dot ("."), the request MUST be rejected as this is reserved for server-default rules. Slashes ("/") and backslashes ("\") are also not allowed.

before Use 'before' with a rule_id as its value to make the new rule the next-most important rule with respect to the given user defined rule. It is not possible to add a rule relative to a predefined server rule.

after This makes the new rule the next-less important rule relative to the given user defined rule. It is not possible to add a rule relative to a predefined server rule.

actions The action(s) to perform when the conditions for this rule are met.

conditions The conditions that must hold true for an event in order for a rule to be applied to an event. A rule with no conditions always matches. Only applicable to underride and override rules.

pattern Only applicable to content rules. The glob-style pattern to match against.

Implementation

Future<void> setPushRule(
    String scope, PushRuleKind kind, String ruleId, List<Object?> actions,
    {String? before,
    String? after,
    List<PushCondition>? conditions,
    String? pattern}) async {
  final requestUri = Uri(
      path:
          '_matrix/client/v3/pushrules/${Uri.encodeComponent(scope)}/${Uri.encodeComponent(kind.name)}/${Uri.encodeComponent(ruleId)}',
      queryParameters: {
        if (before != null) 'before': before,
        if (after != null) 'after': after,
      });
  final request = Request('PUT', baseUri!.resolveUri(requestUri));
  request.headers['authorization'] = 'Bearer ${bearerToken!}';
  request.headers['content-type'] = 'application/json';
  request.bodyBytes = utf8.encode(jsonEncode({
    'actions': actions.map((v) => v).toList(),
    if (conditions != null)
      'conditions': conditions.map((v) => v.toJson()).toList(),
    if (pattern != null) 'pattern': pattern,
  }));
  final response = await httpClient.send(request);
  final responseBody = await response.stream.toBytes();
  if (response.statusCode != 200) unexpectedResponse(response, responseBody);
  final responseString = utf8.decode(responseBody);
  final json = jsonDecode(responseString);
  return ignore(json);
}