setState method

void setState(
  1. Event state
)

Adds the state to this room and overwrites a state with the same typeKey/stateKey key pair if there is one.

Implementation

void setState(Event state) {
  // Decrypt if necessary
  if (state.type == EventTypes.Encrypted && client.encryptionEnabled) {
    try {
      state = client.encryption?.decryptRoomEventSync(id, state) ?? state;
    } catch (e, s) {
      Logs().e('[LibOlm] Could not decrypt room state', e, s);
    }
  }

  // We ignore room verification events for lastEvents
  if (state.type == EventTypes.Message &&
      state.messageType.startsWith('m.room.verification.')) {
    return;
  }

  final isMessageEvent = client.roomPreviewLastEvents.contains(state.type);

  // We ignore events editing events older than the current-latest here so
  // i.e. newly sent edits for older events don't show up in room preview
  final lastEvent = this.lastEvent;
  if (isMessageEvent &&
      state.relationshipEventId != null &&
      state.relationshipType == RelationshipTypes.edit &&
      lastEvent != null &&
      !state.matchesEventOrTransactionId(lastEvent.eventId) &&
      lastEvent.eventId != state.relationshipEventId &&
      !(lastEvent.relationshipType == RelationshipTypes.edit &&
          lastEvent.relationshipEventId == state.relationshipEventId)) {
    return;
  }

  // Ignore other non-state events
  final stateKey = isMessageEvent ? '' : state.stateKey;
  final roomId = state.roomId;
  if (stateKey == null || roomId == null) {
    return;
  }

  // Do not set old events as state events
  final prevEvent = getState(state.type, stateKey);
  if (prevEvent != null &&
      prevEvent.eventId != state.eventId &&
      prevEvent.originServerTs.millisecondsSinceEpoch >
          state.originServerTs.millisecondsSinceEpoch) {
    return;
  }

  (states[state.type] ??= {})[stateKey] = state;

  client.onRoomState.add(state);
}