diff --git a/pubnub/lib/src/subscribe/envelope.dart b/pubnub/lib/src/subscribe/envelope.dart index e3d88cb..0b617a4 100644 --- a/pubnub/lib/src/subscribe/envelope.dart +++ b/pubnub/lib/src/subscribe/envelope.dart @@ -120,9 +120,26 @@ class PresenceEvent { .map((uuid) => UUID(uuid)) .toList(); + static bool isValidPresenceEvent(Envelope envelope) { + final payload = envelope.payload; + if (payload is! Map) return false; + + final action = payload['action']; + final occupancy = payload['occupancy']; + + if (action is! String || + PresenceActionExtension.fromString(action) == PresenceAction.unknown) { + return false; + } + if (occupancy is! int) { + return false; + } + return true; + } + PresenceEvent.fromEnvelope(this.envelope) : action = PresenceActionExtension.fromString( - envelope.payload['action'] as String?), + envelope.payload['action'] as String), uuid = envelope.payload['uuid'] != null ? UUID(envelope.payload['uuid']) : null, diff --git a/pubnub/lib/src/subscribe/subscription.dart b/pubnub/lib/src/subscribe/subscription.dart index 0760446..8024572 100644 --- a/pubnub/lib/src/subscribe/subscription.dart +++ b/pubnub/lib/src/subscribe/subscription.dart @@ -62,9 +62,10 @@ class Subscription { /// Will only emit when [withPresence] is true. Stream get presence => _envelopesController.stream .where((envelope) => - presenceChannels.contains(envelope.channel) || - presenceChannels.contains(envelope.subscriptionPattern) || - presenceChannelGroups.contains(envelope.subscriptionPattern)) + PresenceEvent.isValidPresenceEvent(envelope) && + (presenceChannels.contains(envelope.channel) || + presenceChannels.contains(envelope.subscriptionPattern) || + presenceChannelGroups.contains(envelope.subscriptionPattern))) .map((envelope) => PresenceEvent.fromEnvelope(envelope)); final Completer _cancelCompleter = Completer();