diff --git a/pubnub/endpoints/fetch_messages.py b/pubnub/endpoints/fetch_messages.py index a5a34ca7..3883475e 100644 --- a/pubnub/endpoints/fetch_messages.py +++ b/pubnub/endpoints/fetch_messages.py @@ -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 @@ -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 @@ -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', } @@ -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) diff --git a/pubnub/endpoints/file_operations/publish_file_message.py b/pubnub/endpoints/file_operations/publish_file_message.py index 2097dd98..c61904e1 100644 --- a/pubnub/endpoints/file_operations/publish_file_message.py +++ b/pubnub/endpoints/file_operations/publish_file_message.py @@ -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): @@ -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 @@ -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 diff --git a/pubnub/endpoints/file_operations/send_file.py b/pubnub/endpoints/file_operations/send_file.py index d0be79e8..644ec873 100644 --- a/pubnub/endpoints/file_operations/send_file.py +++ b/pubnub/endpoints/file_operations/send_file.py @@ -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): @@ -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 diff --git a/pubnub/endpoints/pubsub/publish.py b/pubnub/endpoints/pubsub/publish.py index 73946fcd..9f910d07 100644 --- a/pubnub/endpoints/pubsub/publish.py +++ b/pubnub/endpoints/pubsub/publish.py @@ -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): @@ -31,7 +29,7 @@ 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 @@ -39,7 +37,7 @@ 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 @@ -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 diff --git a/pubnub/endpoints/signal.py b/pubnub/endpoints/signal.py index d2c92b2a..60874e22 100644 --- a/pubnub/endpoints/signal.py +++ b/pubnub/endpoints/signal.py @@ -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): @@ -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): @@ -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 @@ -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 diff --git a/pubnub/models/consumer/history.py b/pubnub/models/consumer/history.py index f4338289..1b5cebcc 100644 --- a/pubnub/models/consumer/history.py +++ b/pubnub/models/consumer/history.py @@ -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 @@ -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( @@ -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'] @@ -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'] diff --git a/pubnub/models/consumer/message_type.py b/pubnub/models/consumer/message_type.py deleted file mode 100644 index 6ef30e24..00000000 --- a/pubnub/models/consumer/message_type.py +++ /dev/null @@ -1,27 +0,0 @@ -class PNMessageType: - pn_message_type: str = None - message_type: str = None - _type_mapping = { - 'None': 'pn_message', - '0': 'pn_message', - '1': 'pn_signal', - '2': 'pn_object', - '3': 'pn_message_action', - '4': 'pn_file', - } - - def __init__(self, message_type: str = None) -> None: - self.message_type = message_type - - def set_pn_message_type(self, pn_message_type: str): - self.pn_message_type = self._type_mapping[str(pn_message_type)] - return self - - def from_response(message_type: str = None, pn_message_type: str = None): - return PNMessageType(message_type).set_pn_message_type(pn_message_type) - - def __str__(self) -> str: - return self.message_type if self.message_type else str(self.pn_message_type) - - def toJSON(self) -> str: - return str(self) diff --git a/pubnub/models/consumer/pubsub.py b/pubnub/models/consumer/pubsub.py index 1e5d47d1..0e5046d6 100644 --- a/pubnub/models/consumer/pubsub.py +++ b/pubnub/models/consumer/pubsub.py @@ -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) @@ -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 diff --git a/pubnub/models/server/subscribe.py b/pubnub/models/server/subscribe.py index dc594596..ba2d74b6 100644 --- a/pubnub/models/server/subscribe.py +++ b/pubnub/models/server/subscribe.py @@ -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)) @@ -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 diff --git a/pubnub/workers.py b/pubnub/workers.py index d2bfdc1c..62596dbf 100644 --- a/pubnub/workers.py +++ b/pubnub/workers.py @@ -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) diff --git a/tests/acceptance/history/steps/then_steps.py b/tests/acceptance/history/steps/then_steps.py index cec9533d..67f61d79 100644 --- a/tests/acceptance/history/steps/then_steps.py +++ b/tests/acceptance/history/steps/then_steps.py @@ -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) diff --git a/tests/acceptance/history/steps/when_steps.py b/tests/acceptance/history/steps/when_steps.py index b4b5811c..c50f9430 100644 --- a/tests/acceptance/history/steps/when_steps.py +++ b/tests/acceptance/history/steps/when_steps.py @@ -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] @@ -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) diff --git a/tests/functional/test_publish.py b/tests/functional/test_publish.py index 3d9940aa..634eff71 100644 --- a/tests/functional/test_publish.py +++ b/tests/functional/test_publish.py @@ -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 @@ -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) diff --git a/tests/functional/test_signal.py b/tests/functional/test_signal.py index 27c3951a..246c5df6 100644 --- a/tests/functional/test_signal.py +++ b/tests/functional/test_signal.py @@ -4,7 +4,6 @@ from pubnub.pubnub import PubNub from pubnub.exceptions import PubNubException from pubnub.endpoints.signal import Signal -from pubnub.models.consumer.message_type import PNMessageType from pubnub.managers import TelemetryManager from tests.helper import url_encode, pnconf_copy, sdk_name from unittest.mock import MagicMock @@ -70,24 +69,7 @@ def test_signal_with_space_id(self): 'uuid': self.pubnub.uuid, }) - def test_signal_with_pn_message_type(self): - message = "hi" - message_type = 'test_type' - encoded_message = url_encode(message) - - self.signal.channel("ch1").message(message).message_type(PNMessageType(message_type)) - - self.assertEqual(self.signal.build_path(), "/signal/%s/%s/0/ch1/0/%s" - % (pnconf.publish_key, pnconf.subscribe_key, encoded_message)) - - self.assertEqual(self.signal.build_params_callback()({}), { - 'auth': 'auth', - 'pnsdk': sdk_name, - 'type': message_type, - 'uuid': self.pubnub.uuid, - }) - - def test_signal_with_str_message_type(self): + def test_signal_with_message_type(self): message = "hi" message_type = 'test_type' encoded_message = url_encode(message) diff --git a/tests/integrational/asyncio/test_subscribe.py b/tests/integrational/asyncio/test_subscribe.py index c9517cb1..9824a28e 100644 --- a/tests/integrational/asyncio/test_subscribe.py +++ b/tests/integrational/asyncio/test_subscribe.py @@ -5,7 +5,6 @@ from unittest.mock import patch from pubnub.models.consumer.pubsub import PNMessageResult -from pubnub.models.consumer.message_type import PNMessageType from pubnub.pubnub_asyncio import PubNubAsyncio, AsyncioEnvelope, SubscribeListener from tests.helper import pnconf_env_copy, pnconf_enc_env_copy from tests.integrational.vcr_asyncio_sleeper import get_sleeper, VCR599Listener, VCR599ReconnectionManager @@ -483,7 +482,7 @@ async def test_subscribe_publish_message_type(event_loop, sleeper=asyncio.sleep) assert subscribe_envelope.subscription is None assert subscribe_envelope.message == message - assert isinstance(subscribe_envelope.type, PNMessageType) + assert isinstance(subscribe_envelope.type, str) assert str(subscribe_envelope.type) == message_type assert subscribe_envelope.timetoken > 0 diff --git a/tests/integrational/native_sync/test_fetch_messages.py b/tests/integrational/native_sync/test_fetch_messages.py index abfbe788..a4fe8912 100644 --- a/tests/integrational/native_sync/test_fetch_messages.py +++ b/tests/integrational/native_sync/test_fetch_messages.py @@ -148,4 +148,4 @@ def test_fetch_messages_actions_include_message_type(self): assert isinstance(envelope.result, PNFetchMessagesResult) history = envelope.result.channels[ch] assert len(history) == 1 - assert str(history[0].message_type) == 'pn_message' + assert str(history[0].message_type) == '0'