Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Presence engine - states and events #178

Merged
merged 16 commits into from
Feb 8, 2024
Merged
6 changes: 6 additions & 0 deletions pubnub/dtos.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ def __init__(self, channels=None, channel_groups=None, presence_enabled=None, ti
self.presence_enabled = presence_enabled
self.timetoken = timetoken

@property
def channels_with_pressence(self):
if not self.presence_enabled:
return self.channels
return [*self.channels] + [ch + '-pnpres' for ch in self.channels]


class UnsubscribeOperation(object):
def __init__(self, channels=None, channel_groups=None):
Expand Down
3 changes: 3 additions & 0 deletions pubnub/endpoints/presence/heartbeat.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ def custom_params(self):
if self._state is not None and len(self._state) > 0:
params['state'] = utils.url_write(self._state)

if hasattr(self.pubnub, '_subscription_manager'):
params.update(self.pubnub._subscription_manager.get_custom_params())

return params

def create_response(self, envelope):
Expand Down
3 changes: 3 additions & 0 deletions pubnub/endpoints/presence/leave.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ def custom_params(self):
if len(self._groups) > 0:
params['channel-group'] = utils.join_items(self._groups)

if hasattr(self.pubnub, '_subscription_manager'):
params.update(self.pubnub._subscription_manager.get_custom_params())

return params

def build_path(self):
Expand Down
3 changes: 3 additions & 0 deletions pubnub/endpoints/pubsub/subscribe.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ def custom_params(self):
if not self.pubnub.config.heartbeat_default_values:
params['heartbeat'] = self.pubnub.config.presence_timeout

if hasattr(self.pubnub, '_subscription_manager'):
params.update(self.pubnub._subscription_manager.get_custom_params())

return params

def create_response(self, envelope):
Expand Down
236 changes: 194 additions & 42 deletions pubnub/event_engine/manage_effects.py

Large diffs are not rendered by default.

48 changes: 48 additions & 0 deletions pubnub/event_engine/models/effects.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,51 @@ class EmitStatusEffect(PNEmittableEffect):
def __init__(self, status: Union[None, PNStatusCategory]) -> None:
super().__init__()
self.status = status


"""
Presence Effects
"""


class HeartbeatEffect(PNManageableEffect):
def __init__(self, channels: Union[None, List[str]] = None, groups: Union[None, List[str]] = None) -> None:
super().__init__()
self.channels = channels
self.groups = groups


class HeartbeatWaitEffect(PNManageableEffect):
def __init__(self, time) -> None:
self.wait_time = time
super().__init__()


class HeartbeatCancelWaitEffect(PNCancelEffect):
cancel_effect = HeartbeatWaitEffect.__name__


class HeartbeatLeaveEffect(PNManageableEffect):
def __init__(self, channels: Union[None, List[str]] = None, groups: Union[None, List[str]] = None,
suppress_leave: bool = False) -> None:
super().__init__()
self.channels = channels
self.groups = groups
self.suppress_leave = suppress_leave


class HeartbeatDelayedEffect(PNManageableEffect):
def __init__(self,
channels: Union[None, List[str]] = None,
groups: Union[None, List[str]] = None,
attempts: Union[None, int] = None,
reason: Union[None, PubNubException] = None):
super().__init__()
self.channels = channels
self.groups = groups
self.attempts = attempts
self.reason = reason


class HeartbeatCancelDelayedEffect(PNCancelEffect):
cancel_effect = HeartbeatDelayedEffect.__name__
49 changes: 49 additions & 0 deletions pubnub/event_engine/models/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,52 @@ class DisconnectEvent(PNEvent):

class ReconnectEvent(PNEvent):
pass


"""
Presence Events
"""


class HeartbeatJoinedEvent(PNChannelGroupsEvent):
pass


class HeartbeatReconnectEvent(PNEvent):
pass


class HeartbeatLeftAllEvent(PNEvent):
pass


class HeartbeatLeftEvent(PNChannelGroupsEvent):
def __init__(self, channels: List[str], groups: List[str], suppress_leave: bool = False) -> None:
PNChannelGroupsEvent.__init__(self, channels, groups)
self.suppress_leave = suppress_leave


class HeartbeatDisconnectEvent(PNChannelGroupsEvent):
pass


class HeartbeatSuccessEvent(PNChannelGroupsEvent):
pass


class HeartbeatFailureEvent(PNChannelGroupsEvent, PNFailureEvent):
def __init__(self, channels: List[str], groups: List[str], reason: PubNubException, attempt: int,
timetoken: int = 0) -> None:
PNChannelGroupsEvent.__init__(self, channels, groups)
PNFailureEvent.__init__(self, reason, attempt, timetoken)


class HeartbeatTimesUpEvent(PNEvent):
pass


class HeartbeatGiveUpEvent(PNChannelGroupsEvent, PNFailureEvent):
def __init__(self, channels: List[str], groups: List[str], reason: PubNubException, attempt: int,
timetoken: int = 0) -> None:
PNChannelGroupsEvent.__init__(self, channels, groups)
PNFailureEvent.__init__(self, reason, attempt, timetoken)
Loading
Loading