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

feat: messageType and spaceId #314

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
33 changes: 33 additions & 0 deletions src/core/components/subscription_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,15 @@ export default class {
if (message.userMetadata) {
announce.userMetadata = message.userMetadata;
}
if (message.type) {
announce.messageType = message.type;
} else {
announce.messageType = 'pn_signal';
}

if (message.spaceId) {
announce.spaceId = message.spaceId;
}

announce.message = message.payload;

Expand Down Expand Up @@ -608,6 +617,8 @@ export default class {
data: message.payload.data,
};

announce.messageType = 'pn_object';

this._listenerManager.announceObjects(announce);

if (message.payload.type === 'uuid') {
Expand Down Expand Up @@ -662,6 +673,8 @@ export default class {

announce.event = message.payload.event;

announce.messageType = 'pn_messageAction';

this._listenerManager.announceMessageAction(announce);
} else if (message.messageType === 4) {
// this is a file message
Expand Down Expand Up @@ -697,6 +710,16 @@ export default class {
}),
};

if (message.type) {
announce.messageType = message.type;
} else {
announce.messageType = 'pn_file';
}

if (message.spaceId) {
announce.spaceId = message.spaceId;
}

this._listenerManager.announceFile(announce);
} else {
const announce = {};
Expand All @@ -723,6 +746,16 @@ export default class {
announce.message = message.payload;
}

if (message.type) {
announce.messageType = message.type;
} else {
announce.messageType = 'pn_message';
}

if (message.spaceId) {
announce.spaceId = message.spaceId;
}

this._listenerManager.announceMessage(announce);
}
});
Expand Down
42 changes: 30 additions & 12 deletions src/core/endpoints/fetch_messages.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
/* */

import {
FetchMessagesArguments,
FetchMessagesResponse,
MessageAnnouncement,
HistoryV3Response,
ModulesInject,
} from '../flow_interfaces';
import operationConstants from '../constants/operations';
import utils from '../utils';

Expand All @@ -21,6 +12,21 @@ function __processMessage(modules, message) {
}
}

function __getPNMessageTypeString(messageTypeInt) {
switch (messageTypeInt) {
case 0:
return 'pn_message';
case 3:
return 'pn_messageAction';
case 4:
return 'pn_file';
case null:
return 'pn_message';
default:
return `${messageTypeInt}`;
}
}

export function getOperation() {
return operationConstants.PNFetchMessagesOperation;
}
Expand Down Expand Up @@ -69,9 +75,11 @@ export function prepareParams(modules, incomingParams) {
includeUuid,
includeUUID = true,
includeMessageType = true,
includeSpaceId = false,
} = incomingParams;
const outgoingParams = {};

outgoingParams.include_message_type = 'true';
outgoingParams.include_type = 'true';
if (count) {
outgoingParams.max = count;
} else {
Expand All @@ -82,7 +90,11 @@ export function prepareParams(modules, incomingParams) {
if (stringifiedTimeToken) outgoingParams.string_message_token = 'true';
if (includeMeta) outgoingParams.include_meta = 'true';
if (includeUUID && includeUuid !== false) outgoingParams.include_uuid = 'true';
if (includeMessageType) outgoingParams.include_message_type = 'true';
if (!includeMessageType) {
outgoingParams.include_message_type = 'false';
outgoingParams.include_type = 'false';
}
if (includeSpaceId) outgoingParams.include_space_id = 'true';

return outgoingParams;
}
Expand All @@ -100,7 +112,6 @@ export function handleResponse(modules, serverResponse) {
announce.channel = channelName;
announce.timetoken = messageEnvelope.timetoken;
announce.message = __processMessage(modules, messageEnvelope.message);
announce.messageType = messageEnvelope.message_type;
announce.uuid = messageEnvelope.uuid;

if (messageEnvelope.actions) {
Expand All @@ -112,6 +123,13 @@ export function handleResponse(modules, serverResponse) {
if (messageEnvelope.meta) {
announce.meta = messageEnvelope.meta;
}
if (typeof messageEnvelope.message_type != 'undefined' || messageEnvelope.type) {
announce.messageType = messageEnvelope.type || __getPNMessageTypeString(messageEnvelope.message_type);
}

if (messageEnvelope.space_id) {
announce.spaceId = messageEnvelope.space_id;
}

response.channels[channelName].push(announce);
});
Expand Down
10 changes: 8 additions & 2 deletions src/core/endpoints/file_upload/publish_file.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/** */

import operationConstants from '../../constants/operations';

import utils from '../../utils';
Expand Down Expand Up @@ -69,6 +67,14 @@ const endpoint = {
outParams.meta = JSON.stringify(params.meta);
}

if (params.spaceId) {
outParams['space-id'] = params.spaceId;
}

if (params.messageType) {
outParams.type = params.messageType;
}

return outParams;
},

Expand Down
4 changes: 3 additions & 1 deletion src/core/endpoints/file_upload/send_file.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const sendFile = function ({
publishFile,
modules: { PubNubFile, config, cryptography, networking },
}) {
return async ({ channel, file: input, message, cipherKey, meta, ttl, storeInHistory }) => {
return async ({ channel, file: input, message, cipherKey, meta, ttl, storeInHistory, messageType, spaceId }) => {
if (!channel) {
throw new PubNubError(
'Validation failed, check status for details',
Expand Down Expand Up @@ -99,6 +99,8 @@ const sendFile = function ({
meta,
storeInHistory,
ttl,
messageType: messageType,
spaceId: spaceId,
});

wasSuccessful = true;
Expand Down
10 changes: 9 additions & 1 deletion src/core/endpoints/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export function postPayload(modules, incomingParams) {
}

export function prepareParams(modules, incomingParams) {
const { meta, replicate = true, storeInHistory, ttl } = incomingParams;
const { meta, replicate = true, storeInHistory, ttl, spaceId, messageType } = incomingParams;
const params = {};

if (storeInHistory != null) {
Expand All @@ -85,6 +85,14 @@ export function prepareParams(modules, incomingParams) {
params.meta = JSON.stringify(meta);
}

if (messageType) {
params.type = messageType;
}

if (spaceId) {
params['space-id'] = spaceId;
}

return params;
}

Expand Down
10 changes: 9 additions & 1 deletion src/core/endpoints/signal.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,17 @@ export function isAuthSupported() {
return true;
}

export function prepareParams() {
export function prepareParams(_, incomingParams) {
const { spaceId, messageType } = incomingParams;
const params = {};

if (messageType) {
params.type = messageType;
}

if (spaceId) {
params['space-id'] = spaceId;
}
return params;
}

Expand Down
12 changes: 2 additions & 10 deletions src/core/endpoints/subscribe.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
/* */

import {
SubscribeArguments,
PublishMetaData,
SubscribeMetadata,
SubscribeMessage,
SubscribeEnvelope,
ModulesInject,
} from '../flow_interfaces';
import operationConstants from '../constants/operations';
import utils from '../utils';

Expand Down Expand Up @@ -84,6 +74,8 @@ export function handleResponse(modules, serverResponse) {
subscribeKey: rawMessage.k,
originationTimetoken: rawMessage.o,
userMetadata: rawMessage.u,
type: rawMessage.mt,
spaceId: rawMessage.si,
publishMetaData,
};
messages.push(parsedMessage);
Expand Down
22 changes: 22 additions & 0 deletions test/contract/steps/files/files-vsf.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { When } from '@cucumber/cucumber';

When('I send a file with {string} space id and {string} message type', async function (testSpaceId, testMessageType) {
const pubnub = this.getPubnub({
publishKey: this.keyset.publishKey,
subscribeKey: this.keyset.subscribeKey,
});

const testContent = `dummytext`;
try {
const response = await pubnub.sendFile({
channel: 'filestest',
file: { data: Buffer.from(testContent), name: 'myFile.txt', mimeType: 'text/plain' },
message: { text: 'fileMessage' },
spaceId: testSpaceId,
messageType: testMessageType,
});
this.isSucceed = response.timetoken === '1';
} catch (e) {
this.isSucceed = false;
}
});
64 changes: 64 additions & 0 deletions test/contract/steps/history/fetchMessages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { Given, When, Then } from '@cucumber/cucumber';
import { expect } from 'chai';

Given('the demo keyset with enabled storage', function () {
this.keyset = this.fixtures.demoKeyset;
});

When('I fetch message history for {string} channel', async function (testChannel) {
const pubnub = this.getPubnub({
publishKey: this.keyset.publishKey,
subscribeKey: this.keyset.subscribeKey,
});

const response = await pubnub.fetchMessages({
channels: [testChannel],
});

this.isSucceed = true;
this.historyMessages = response.channels[testChannel];
});

When(
'I fetch message history with {string} set to {string} for {string} channel',
async function (param, paramValue, channel) {
const pubnub = this.getPubnub({
publishKey: this.keyset.publishKey,
subscribeKey: this.keyset.subscribeKey,
});

const request = {
channels: [channel],
};
request[param] = paramValue === 'true' ? true : false;

const response = await pubnub.fetchMessages(request);

this.isSucceed = true;
this.historyMessages = response.channels[channel];
},
);

Then(
'history response contains messages with {string} and {string} message types',
function (firstMessageType, secondMessageType) {
expect(this.historyMessages.filter((m) => m.messageType === firstMessageType)).to.have.lengthOf(1);
expect(this.historyMessages.filter((m) => m.messageType === secondMessageType)).to.have.lengthOf(1);
},
);

Then('history response contains messages without space ids', function () {
this.historyMessages.forEach((m) => expect(m).to.not.have.property('spaceId'));
});

Then('history response contains messages with space ids', function () {
this.historyMessages.forEach((m) => expect(m).to.have.property('spaceId'));
});

Then('history response contains messages with message types', function () {
this.historyMessages.forEach((m) => expect(m).to.have.property('messageType'));
});

Then('history response contains messages without message types', function () {
this.historyMessages.forEach((m) => expect(m).to.not.have.property('messageType'));
});
3 changes: 2 additions & 1 deletion test/contract/steps/objectsv2/uuid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ When('I get all UUID metadata with custom', async function () {
});

this.response = await pubnub.objects.getAllUUIDMetadata({ include: { customFields: true } });
this.isSucceed = this.response.status === 200;
});

Then('the UUID metadata for {string} persona', async function (persona) {
Expand All @@ -105,7 +106,7 @@ Then('the UUID metadata for {string} persona contains updated', async function (
});

Then('I receive a successful response', function () {
expect(this.response.status).to.equal(200);
expect(this.isSucceed).to.be.true;
});

Then('the response contains list with {string} and {string} UUID metadata', function (firstPersona, secondPersona) {
Expand Down
28 changes: 28 additions & 0 deletions test/contract/steps/publish/publishToSpace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { When, Then } from '@cucumber/cucumber';
import { expect } from 'chai';

When('I publish message with {string} space id and {string} message type', async function (spaceId, messageType) {
const pubnub = this.getPubnub({
publishKey: this.keyset.publishKey,
subscribeKey: this.keyset.subscribeKey,
});

try {
const response = await pubnub.publish({
channel: 'test',
message: { hello: 'world' },
spaceId: spaceId,
messageType: messageType,
});

this.isSucceed = typeof response.timetoken === 'string';
} catch (status) {
if ((status as { error: boolean }).error) {
this.isSucceed = false;
}
}
});

Then('I receive an error response', function () {
expect(this.isSucceed).to.be.false;
});
Loading