setPushRuleState method

Future<void> setPushRuleState(
  1. PushRuleState newState
)

Sends a request to the homeserver to set the PushRuleState for this room. Returns ErrorResponse if something goes wrong.

Implementation

Future<void> setPushRuleState(PushRuleState newState) async {
  if (newState == pushRuleState) return;
  dynamic resp;
  switch (newState) {
    // All push notifications should be sent to the user
    case PushRuleState.notify:
      if (pushRuleState == PushRuleState.dontNotify) {
        await client.deletePushRule('global', PushRuleKind.override, id);
      } else if (pushRuleState == PushRuleState.mentionsOnly) {
        await client.deletePushRule('global', PushRuleKind.room, id);
      }
      break;
    // Only when someone mentions the user, a push notification should be sent
    case PushRuleState.mentionsOnly:
      if (pushRuleState == PushRuleState.dontNotify) {
        await client.deletePushRule('global', PushRuleKind.override, id);
        await client.setPushRule(
          'global',
          PushRuleKind.room,
          id,
          [PushRuleAction.dontNotify],
        );
      } else if (pushRuleState == PushRuleState.notify) {
        await client.setPushRule(
          'global',
          PushRuleKind.room,
          id,
          [PushRuleAction.dontNotify],
        );
      }
      break;
    // No push notification should be ever sent for this room.
    case PushRuleState.dontNotify:
      if (pushRuleState == PushRuleState.mentionsOnly) {
        await client.deletePushRule('global', PushRuleKind.room, id);
      }
      await client.setPushRule(
        'global',
        PushRuleKind.override,
        id,
        [PushRuleAction.dontNotify],
        conditions: [
          PushCondition(kind: 'event_match', key: 'room_id', pattern: id)
        ],
      );
  }
  return resp;
}