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

Feature/additional space and message type fields #153

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,13 @@ jobs:
cp sdk-specifications/features/access/authorization-failure-reporting.feature tests/acceptance/pam
cp sdk-specifications/features/access/grant-token.feature tests/acceptance/pam
cp sdk-specifications/features/access/revoke-token.feature tests/acceptance/pam
cp sdk-specifications/features/files/file-upload-to-space.feature tests/acceptance/files
cp sdk-specifications/features/history/history-vsp.feature tests/acceptance/history

sudo pip3 install -r requirements-dev.txt
behave --junit tests/acceptance/pam
behave --junit tests/acceptance/files
behave --junit tests/acceptance/history
seba-aln marked this conversation as resolved.
Show resolved Hide resolved
- name: Expose acceptance tests reports
uses: actions/upload-artifact@v3
if: always()
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ _trial_temp
# jupyter dev notebook
PubNubTwisted.ipynb

# Feature files for acceptance testing.
tests/acceptance/*/*.feature

# GitHub Actions #
##################
.github/.release
27 changes: 23 additions & 4 deletions pubnub/endpoints/fetch_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ def __init__(self, pubnub):
self._count = None
self._include_meta = None
self._include_message_actions = None
self._include_message_type = None
self._include_message_type = True
self._include_type = True
self._include_space_id = None
self._include_uuid = None

def channels(self, channels):
Expand Down Expand Up @@ -71,13 +73,27 @@ def include_message_type(self, include_message_type):
self._include_message_type = include_message_type
return self

def include_type(self, include_type):
assert isinstance(include_type, bool)
self._include_type = include_type
return self

def include_uuid(self, include_uuid):
assert isinstance(include_uuid, bool)
self._include_uuid = include_uuid
return self

def include_space_id(self, include_space_id):
assert isinstance(include_space_id, bool)
self._include_space_id = include_space_id
seba-aln marked this conversation as resolved.
Show resolved Hide resolved
return self

def custom_params(self):
params = {'max': int(self._count)}
params = {
'max': int(self._count),
'include_type': 'true' if self._include_type else 'false',
'include_message_type': 'true' if self._include_message_type else 'false',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'include_type': str(self._include_message_type).lower()

}

if self._start is not None:
params['start'] = str(self._start)
Expand All @@ -88,8 +104,8 @@ def custom_params(self):
if self._include_meta is not None:
params['include_meta'] = "true" if self._include_meta else "false"

if self._include_message_type is not None:
params['include_message_type'] = "true" if self._include_message_type else "false"
if self._include_space_id is not None:
params['include_space_id'] = "true" if self._include_space_id else "false"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

params['include_space_id'] = str(self. _include_space_id).lower()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

space_id can be null and in this case the parameter should not be added, that's why there's if statement


if self.include_message_actions and self._include_uuid is not None:
params['include_uuid'] = "true" if self._include_uuid else "false"
Expand Down Expand Up @@ -154,6 +170,9 @@ def create_response(self, envelope): # pylint: disable=W0221
return PNFetchMessagesResult.from_json(
json_input=envelope,
include_message_actions=self._include_message_actions,
include_message_type=self._include_message_type,
include_type=self._include_type,
include_space_id=self._include_space_id,
start_timetoken=self._start,
end_timetoken=self._end)

Expand Down
16 changes: 16 additions & 0 deletions pubnub/endpoints/file_operations/publish_file_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ def __init__(self, pubnub):
self._cipher_key = None
self._replicate = None
self._ptto = None
self._type = None
self._space_id = None

def meta(self, meta):
self._meta = meta
Expand Down Expand Up @@ -49,6 +51,14 @@ def file_name(self, file_name):
self._file_name = file_name
return self

def type(self, type: str):
self._type = type
return self

def space_id(self, space_id: str):
self._space_id = space_id
return self

def _encrypt_message(self, message):
if self._cipher_key or self._pubnub.config.cipher_key:
return self._pubnub.config.crypto.encrypt(
Expand Down Expand Up @@ -87,6 +97,12 @@ def custom_params(self):
"ttl": self._ttl,
"store": 1 if self._should_store else 0
})
if self._type:
params['type'] = self._type

if self._space_id:
params['space-id'] = self._space_id

return params

def is_auth_required(self):
Expand Down
24 changes: 21 additions & 3 deletions pubnub/endpoints/file_operations/send_file.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from pubnub.endpoints.file_operations.file_based_endpoint import FileOperationEndpoint

from pubnub.crypto import PubNubFileCrypto
from pubnub.enums import HttpMethod, PNOperationType
from pubnub.models.consumer.file import PNSendFileResult
Expand All @@ -23,6 +22,8 @@ def __init__(self, pubnub):
self._file_object = None
self._replicate = None
self._ptto = None
self._type = None
self._space_id = None

def file_object(self, fd):
self._file_object = fd
Expand Down Expand Up @@ -69,7 +70,14 @@ def is_compressable(self):
return True

def custom_params(self):
return {}
params = {}
if self._type:
params['type'] = str(self._type)

if self._space_id:
params['space-id'] = self._space_id

return params

def validate_params(self):
self.validate_subscribe_key()
Expand Down Expand Up @@ -110,6 +118,14 @@ def cipher_key(self, cipher_key):
self._cipher_key = cipher_key
return self

def type(self, type: str):
self._type = type
return self

def space_id(self, space_id: str):
self._space_id = space_id
return self

def create_response(self, envelope, data=None):
return PNSendFileResult(envelope, self._file_upload_envelope)

Expand Down Expand Up @@ -139,7 +155,9 @@ def sync(self):
ttl(self._ttl).\
replicate(self._replicate).\
ptto(self._ptto).\
cipher_key(self._cipher_key).sync()
cipher_key(self._cipher_key).\
type(self._type).\
space_id(self._space_id).sync()

response_envelope.result.timestamp = publish_file_response.result.timestamp
return response_envelope
Expand Down
16 changes: 16 additions & 0 deletions pubnub/endpoints/pubsub/publish.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ class Publish(Endpoint, TimeTokenOverrideMixin):
def __init__(self, pubnub):
super(Publish, self).__init__(pubnub)
self._channel = None
self._space_id = None
self._message = None
self._message_type = None
self._should_store = None
self._use_post = None
self._meta = None
Expand All @@ -27,10 +29,18 @@ def channel(self, channel):
self._channel = str(channel)
return self

def space_id(self, space_id: str):
self._space_id = str(space_id)
return self

def message(self, message):
self._message = message
return self

def message_type(self, message_type: str):
self._message_type = message_type
return self

def use_post(self, use_post):
self._use_post = bool(use_post)
return self
Expand Down Expand Up @@ -91,6 +101,12 @@ def custom_params(self):
if self.pubnub.config.auth_key is not None:
params["auth"] = utils.url_encode(self.pubnub.config.auth_key)

if self._message_type is not None:
params['type'] = self._message_type

if self._space_id is not None:
params['space-id'] = self._space_id

return params

def build_path(self):
Expand Down
20 changes: 19 additions & 1 deletion pubnub/endpoints/signal.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ def __init__(self, pubnub):
Endpoint.__init__(self, pubnub)
self._channel = None
self._message = None
self._space_id = None
self._message_type = None

def channel(self, channel):
self._channel = str(channel)
Expand All @@ -20,6 +22,14 @@ def message(self, message):
self._message = message
return self

def space_id(self, space_id: str):
self._space_id = str(space_id)
return self

def message_type(self, message_type: str):
self._message_type = message_type
return self

def build_path(self):
stringified_message = utils.write_value_as_string(self._message)
msg = utils.url_encode(stringified_message)
Expand All @@ -29,7 +39,15 @@ def build_path(self):
)

def custom_params(self):
return {}
params = {}

if self._message_type is not None:
params['type'] = self._message_type

if self._space_id is not None:
params['space-id'] = self._space_id

return params

def http_method(self):
return HttpMethod.GET
Expand Down
57 changes: 36 additions & 21 deletions pubnub/models/consumer/history.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,27 +63,15 @@ def __str__(self):
return "Fetch messages result for range %d..%d" % (self.start_timetoken, self.end_timetoken)

@classmethod
def from_json(cls, json_input, include_message_actions=False, start_timetoken=None, end_timetoken=None):
def from_json(cls, json_input, include_message_actions=False, include_message_type=False, include_type=False,
include_space_id=False, start_timetoken=None, end_timetoken=None):
channels = {}

for key, entry in json_input['channels'].items():
channels[key] = []
for item in entry:
message = PNFetchMessageItem(item['message'], item['timetoken'])
if 'uuid' in item:
message.uuid = item['uuid']
if 'message_type' in item:
message.message_type = item['message_type']

if 'meta' in item:
message.meta = item['meta']

if include_message_actions:
if 'actions' in item:
message.actions = item['actions']
else:
message.actions = {}

message = PNFetchMessageItem(item, include_message_actions, include_message_type, include_type,
include_space_id)
channels[key].append(message)

return PNFetchMessagesResult(
Expand All @@ -94,11 +82,38 @@ def from_json(cls, json_input, include_message_actions=False, start_timetoken=No


class PNFetchMessageItem(object):
def __init__(self, message, timetoken, meta=None, actions=None):
self.message = message
self.meta = meta
self.timetoken = timetoken
self.actions = actions
message = None
meta = None
timetoken = None
actions = None

def __init__(self, item, include_message_actions, include_message_type, include_type, include_space_id):
self.message = item['message']
self.timetoken = item['timetoken']

if 'uuid' in item:
self.uuid = item['uuid']

if 'meta' in item:
self.meta = item['meta']

if include_message_actions:
if 'actions' in item:
self.actions = item['actions']
else:
self.actions = {}

if include_message_type:
if 'message_type' in item:
self.message_type = str(item['message_type']) if item['message_type'] is not None else '0'
else:
self.message_type = None

if include_type:
self.type = item['type'] if 'type' in item.keys() else None

if include_space_id:
self.space_id = item['space_id']

def __str__(self):
return "Fetch message item with tt: %s and content: %s" % (self.timetoken, self.message)
7 changes: 5 additions & 2 deletions pubnub/models/consumer/pubsub.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@


class PNMessageResult(object):
def __init__(self, message, subscription, channel, timetoken, user_metadata=None, publisher=None):

def __init__(self, message, subscription, channel, timetoken, user_metadata=None, publisher=None, message_type=None,
type=None, space_id=None):
if subscription is not None:
assert isinstance(subscription, str)

Expand All @@ -29,6 +29,9 @@ def __init__(self, message, subscription, channel, timetoken, user_metadata=None
self.timetoken = timetoken
self.user_metadata = user_metadata
self.publisher = publisher
self.message_type = message_type
self.type = type
self.space_id = space_id


class PNSignalMessageResult(PNMessageResult):
Expand Down
14 changes: 14 additions & 0 deletions pubnub/models/server/subscribe.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ def __init__(self):
self.publish_metadata = None
self.only_channel_subscription = False
self.type = 0
self.message_type = None
self.space_id = None

@classmethod
def from_json(cls, json_input):
Expand All @@ -49,6 +51,18 @@ def from_json(cls, json_input):
message.publish_metadata = PublishMetadata.from_json(json_input['p'])
if 'e' in json_input:
message.type = json_input['e']

if 'si' in json_input:
message.space_id = json_input['si']

# customers message type set during publish
if 'mt' in json_input:
message.type = json_input['mt']

# internal message type (None|0 = message, 1 = file, etc)
if 'e' in json_input:
message.message_type = json_input['mt']

return message


Expand Down
5 changes: 4 additions & 1 deletion pubnub/workers.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,9 @@ def _process_incoming_payload(self, message):
channel=channel,
subscription=subscription_match,
timetoken=publish_meta_data.publish_timetoken,
publisher=publisher
publisher=publisher,
message_type=message.message_type,
type=message.type,
space_id=message.space_id
)
self._listener_manager.announce_message(pn_message_result)
Loading