Skip to content

Commit

Permalink
Remove PNMessageType"
Browse files Browse the repository at this point in the history
  • Loading branch information
seba-aln committed Mar 29, 2023
1 parent 0ea2561 commit d443f63
Show file tree
Hide file tree
Showing 16 changed files with 64 additions and 113 deletions.
9 changes: 8 additions & 1 deletion pubnub/endpoints/fetch_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def __init__(self, pubnub):
self._include_meta = None
self._include_message_actions = None
self._include_message_type = True
self._include_type = True
self._include_space_id = None
self._include_uuid = None

Expand Down Expand Up @@ -72,6 +73,11 @@ 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
Expand All @@ -85,7 +91,7 @@ def include_space_id(self, include_space_id):
def custom_params(self):
params = {
'max': int(self._count),
'include_type': 'true' if self._include_message_type else 'false',
'include_type': 'true' if self._include_type else 'false',
'include_message_type': 'true' if self._include_message_type else 'false',
}

Expand Down Expand Up @@ -165,6 +171,7 @@ def create_response(self, envelope): # pylint: disable=W0221
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
10 changes: 4 additions & 6 deletions pubnub/endpoints/file_operations/publish_file_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
from pubnub import utils
from pubnub.models.consumer.file import PNPublishFileMessageResult
from pubnub.endpoints.mixins import TimeTokenOverrideMixin
from pubnub.models.consumer.message_type import PNMessageType
from typing import Union


class PublishFileMessage(FileOperationEndpoint, TimeTokenOverrideMixin):
Expand Down Expand Up @@ -53,11 +51,11 @@ def file_name(self, file_name):
self._file_name = file_name
return self

def message_type(self, message_type: Union[PNMessageType, str]):
def message_type(self, message_type: str):
self._message_type = message_type
return self

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

Expand Down Expand Up @@ -100,10 +98,10 @@ def custom_params(self):
"store": 1 if self._should_store else 0
})
if self._message_type:
params['type'] = str(self._message_type)
params['type'] = self._message_type

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

return params

Expand Down
6 changes: 2 additions & 4 deletions pubnub/endpoints/file_operations/send_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
from pubnub.endpoints.file_operations.fetch_upload_details import FetchFileUploadS3Data
from pubnub.request_handlers.requests_handler import RequestsRequestHandler
from pubnub.endpoints.mixins import TimeTokenOverrideMixin
from pubnub.models.consumer.message_type import PNMessageType
from typing import Union


class SendFileNative(FileOperationEndpoint, TimeTokenOverrideMixin):
Expand Down Expand Up @@ -120,11 +118,11 @@ def cipher_key(self, cipher_key):
self._cipher_key = cipher_key
return self

def message_type(self, message_type: Union[PNMessageType, str]):
def message_type(self, message_type: str):
self._message_type = message_type
return self

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

Expand Down
10 changes: 4 additions & 6 deletions pubnub/endpoints/pubsub/publish.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
from pubnub.models.consumer.pubsub import PNPublishResult
from pubnub.enums import HttpMethod, PNOperationType
from pubnub.endpoints.mixins import TimeTokenOverrideMixin
from pubnub.models.consumer.message_type import PNMessageType
from typing import Union


class Publish(Endpoint, TimeTokenOverrideMixin):
Expand All @@ -31,15 +29,15 @@ def channel(self, channel):
self._channel = str(channel)
return self

def space_id(self, space_id):
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: Union[PNMessageType, str]):
def message_type(self, message_type: str):
self._message_type = message_type
return self

Expand Down Expand Up @@ -104,10 +102,10 @@ def custom_params(self):
params["auth"] = utils.url_encode(self.pubnub.config.auth_key)

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

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

return params

Expand Down
16 changes: 7 additions & 9 deletions pubnub/endpoints/signal.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
from pubnub.endpoints.endpoint import Endpoint
from pubnub.enums import HttpMethod, PNOperationType
from pubnub.models.consumer.signal import PNSignalResult
from pubnub.models.consumer.message_type import PNMessageType
from typing import Union


class Signal(Endpoint):
Expand All @@ -13,7 +11,7 @@ def __init__(self, pubnub):
Endpoint.__init__(self, pubnub)
self._channel = None
self._message = None
self._space = None
self._space_id = None
self._message_type = None

def channel(self, channel):
Expand All @@ -24,11 +22,11 @@ def message(self, message):
self._message = message
return self

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

def message_type(self, message_type: Union[PNMessageType, str]):
def message_type(self, message_type: str):
self._message_type = message_type
return self

Expand All @@ -44,10 +42,10 @@ def custom_params(self):
params = {}

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

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

return params

Expand Down
22 changes: 12 additions & 10 deletions pubnub/models/consumer/history.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
from pubnub.models.consumer.message_type import PNMessageType


class PNHistoryResult(object):
def __init__(self, messages, start_timetoken, end_timetoken):
self.messages = messages
Expand Down Expand Up @@ -66,14 +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, include_message_type=False, include_space_id=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, include_message_actions, include_message_type, include_space_id)
message = PNFetchMessageItem(item, include_message_actions, include_message_type, include_type,
include_space_id)
channels[key].append(message)

return PNFetchMessagesResult(
Expand All @@ -89,7 +87,7 @@ class PNFetchMessageItem(object):
timetoken = None
actions = None

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

Expand All @@ -106,9 +104,13 @@ def __init__(self, item, include_message_actions, include_message_type, include_
self.actions = {}

if include_message_type:
self.message_type = PNMessageType.from_response(
message_type=item['type'] if 'type' in item.keys() else None,
pn_message_type=item['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']
Expand Down
27 changes: 0 additions & 27 deletions pubnub/models/consumer/message_type.py

This file was deleted.

5 changes: 3 additions & 2 deletions pubnub/models/consumer/pubsub.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

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

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


Expand Down
13 changes: 6 additions & 7 deletions pubnub/models/server/subscribe.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
from pubnub.models.consumer.message_type import PNMessageType


class SubscribeEnvelope:
def __init__(self, messages=None, metadata=None):
assert isinstance(messages, (list, None))
Expand Down Expand Up @@ -58,11 +55,13 @@ def from_json(cls, json_input):
if 'si' in json_input:
message.space_id = json_input['si']

# customers message type set during publish
if 'mt' in json_input:
if 'e' in json_input:
message.message_type = PNMessageType.from_response(json_input['mt'], json_input['e'])
else:
message.message_type = PNMessageType(json_input['mt'])
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
1 change: 1 addition & 0 deletions pubnub/workers.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ def _process_incoming_payload(self, message):
timetoken=publish_meta_data.publish_timetoken,
publisher=publisher,
message_type=message.message_type,
type=message.type,
space_id=message.space_id
)
self._listener_manager.announce_message(pn_message_result)
10 changes: 10 additions & 0 deletions tests/acceptance/history/steps/then_steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,13 @@ def step_impl(context):
@then('history response contains messages with space ids')
def step_impl(context):
assert all('space_id' in item.__dict__.keys() for item in context.messages)


@then("history response contains messages with '{type1}' and '{type2}' types")
def step_impl(context, type1, type2):
assert all(str(item.type) in [type1, type2] for item in context.messages)


@then('history response contains messages without types')
def step_impl(context):
assert all('type' not in item.__dict__.keys() for item in context.messages)
4 changes: 3 additions & 1 deletion tests/acceptance/history/steps/when_steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

@when("I fetch message history for '{channel}' channel")
def step_impl(context, channel):
envelope = context.peer.fetch_messages().channels(channel).include_message_type(True).sync()
envelope = context.peer.fetch_messages().channels(channel).include_message_type(True).include_type(True).sync()
context.status = envelope.status
context.result = envelope.result
context.messages = envelope.result.channels[channel]
Expand All @@ -15,6 +15,8 @@ def step_impl(context, flag, value, channel):
value = True if value == 'true' else False
if flag == 'includeMessageType':
request.include_message_type(value)
if flag == 'includeType':
request.include_type(value)
if flag == 'includeSpaceId':
request.include_space_id(value)

Expand Down
19 changes: 1 addition & 18 deletions tests/functional/test_publish.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

from pubnub.endpoints.pubsub.publish import Publish
from pubnub.managers import TelemetryManager
from pubnub.models.consumer.message_type import PNMessageType
from pubnub.pubnub import PubNub
from tests.helper import pnconf, sdk_name, url_encode
from unittest.mock import MagicMock
Expand Down Expand Up @@ -109,23 +108,7 @@ def test_pub_with_space_id(self):
'uuid': self.pubnub.uuid,
})

def test_pub_with_pn_message_type(self):
message = "hi"
message_type = 'test_type'
encoded_message = url_encode(message)

self.pub.channel("ch1").message(message).message_type(PNMessageType(message_type))

self.assertEqual(self.pub.build_path(), "/publish/%s/%s/0/ch1/0/%s"
% (pnconf.publish_key, pnconf.subscribe_key, encoded_message))

self.assertEqual(self.pub.build_params_callback()({}), {
'type': message_type,
'pnsdk': sdk_name,
'uuid': self.pubnub.uuid,
})

def test_pub_with_str_message_type(self):
def test_pub_with_message_type(self):
message = "hi"
message_type = 'test_type'
encoded_message = url_encode(message)
Expand Down
Loading

0 comments on commit d443f63

Please sign in to comment.