Skip to content

Commit

Permalink
feat(message-type): add custom message type support
Browse files Browse the repository at this point in the history
Add custom message type support for the following APIs: publish, signal, share file, subscribe
and history.
  • Loading branch information
parfeon committed Nov 14, 2024
1 parent 66520c2 commit 1e5bcc1
Show file tree
Hide file tree
Showing 21 changed files with 352 additions and 75 deletions.
35 changes: 23 additions & 12 deletions dist/web/pubnub.js
Original file line number Diff line number Diff line change
Expand Up @@ -6230,6 +6230,8 @@
};
if (envelope.u)
event.userMetadata = envelope.u;
if (envelope.cmt)
event.customMessageType = envelope.cmt;
if (decryptionError)
event.error = decryptionError;
return event;
Expand All @@ -6245,6 +6247,8 @@
};
if (envelope.u)
event.userMetadata = envelope.u;
if (envelope.cmt)
event.customMessageType = envelope.cmt;
return event;
}
messageActionFromEnvelope(envelope) {
Expand Down Expand Up @@ -6296,6 +6300,8 @@
};
}
}
if (envelope.cmt)
event.customMessageType = envelope.cmt;
if (errorMessage)
event.error = errorMessage;
return event;
Expand Down Expand Up @@ -8303,8 +8309,10 @@
return `/publish/${keySet.publishKey}/${keySet.subscribeKey}/0/${encodeString(channel)}/0${!this.parameters.sendByPost ? `/${encodeString(stringifiedPayload)}` : ''}`;
}
get queryParameters() {
const { meta, replicate, storeInHistory, ttl } = this.parameters;
const { customMessageType, meta, replicate, storeInHistory, ttl } = this.parameters;
const query = {};
if (customMessageType)
query.custom_message_type = customMessageType;
if (storeInHistory !== undefined)
query.store = storeInHistory ? '1' : '0';
if (ttl !== undefined)
Expand Down Expand Up @@ -8380,6 +8388,13 @@
const stringifiedPayload = JSON.stringify(message);
return `/signal/${publishKey}/${subscribeKey}/0/${encodeString(channel)}/0/${encodeString(stringifiedPayload)}`;
}
get queryParameters() {
const { customMessageType } = this.parameters;
const query = {};
if (customMessageType)
query.custom_message_type = customMessageType;
return query;
}
}

/**
Expand Down Expand Up @@ -9151,13 +9166,7 @@
if (payload.message_type === null)
payload.message_type = PubNubMessageType.Message;
const processedPayload = this.processPayload(channel, payload);
const item = {
channel,
timetoken: payload.timetoken,
message: processedPayload.payload,
messageType: payload.message_type,
uuid: payload.uuid,
};
const item = Object.assign(Object.assign({ channel, timetoken: payload.timetoken, message: processedPayload.payload, messageType: payload.message_type }, (payload.custom_message_type ? { customMessageType: payload.custom_message_type } : {})), { uuid: payload.uuid });
if (payload.actions) {
const itemWithActions = item;
itemWithActions.actions = payload.actions;
Expand All @@ -9183,8 +9192,10 @@
return `/v3/${endpoint}/sub-key/${subscribeKey}/channel/${encodeNames(channels)}`;
}
get queryParameters() {
const { start, end, count, includeMessageType, includeMeta, includeUUID, stringifiedTimeToken } = this.parameters;
return Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({ max: count }, (start ? { start } : {})), (end ? { end } : {})), (stringifiedTimeToken ? { string_message_token: 'true' } : {})), (includeMeta !== undefined && includeMeta ? { include_meta: 'true' } : {})), (includeUUID ? { include_uuid: 'true' } : {})), (includeMessageType ? { include_message_type: 'true' } : {}));
const { start, end, count, includeCustomMessageType, includeMessageType, includeMeta, includeUUID, stringifiedTimeToken, } = this.parameters;
return Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({ max: count }, (start ? { start } : {})), (end ? { end } : {})), (stringifiedTimeToken ? { string_message_token: 'true' } : {})), (includeMeta !== undefined && includeMeta ? { include_meta: 'true' } : {})), (includeUUID ? { include_uuid: 'true' } : {})), (includeCustomMessageType !== undefined && includeCustomMessageType !== null
? { include_custom_message_type: includeCustomMessageType ? 'true' : 'false' }
: {})), (includeMessageType ? { include_message_type: 'true' } : {}));
}
/**
* Parse single channel data entry.
Expand Down Expand Up @@ -9461,8 +9472,8 @@
return `/v1/files/publish-file/${publishKey}/${subscribeKey}/0/${encodeString(channel)}/0/${encodeString(this.prepareMessagePayload(fileMessage))}`;
}
get queryParameters() {
const { storeInHistory, ttl, meta } = this.parameters;
return Object.assign(Object.assign({ store: storeInHistory ? '1' : '0' }, (ttl ? { ttl } : {})), (meta && typeof meta === 'object' ? { meta: JSON.stringify(meta) } : {}));
const { customMessageType, storeInHistory, ttl, meta } = this.parameters;
return Object.assign(Object.assign(Object.assign({ store: storeInHistory ? '1' : '0' }, (customMessageType ? { custom_message_type: customMessageType } : {})), (ttl ? { ttl } : {})), (meta && typeof meta === 'object' ? { meta: JSON.stringify(meta) } : {}));
}
/**
* Pre-process provided data.
Expand Down
4 changes: 2 additions & 2 deletions dist/web/pubnub.min.js

Large diffs are not rendered by default.

14 changes: 5 additions & 9 deletions lib/core/endpoints/fetch_messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,7 @@ class FetchMessagesRequest extends request_1.AbstractRequest {
if (payload.message_type === null)
payload.message_type = History.PubNubMessageType.Message;
const processedPayload = this.processPayload(channel, payload);
const item = {
channel,
timetoken: payload.timetoken,
message: processedPayload.payload,
messageType: payload.message_type,
uuid: payload.uuid,
};
const item = Object.assign(Object.assign({ channel, timetoken: payload.timetoken, message: processedPayload.payload, messageType: payload.message_type }, (payload.custom_message_type ? { customMessageType: payload.custom_message_type } : {})), { uuid: payload.uuid });
if (payload.actions) {
const itemWithActions = item;
itemWithActions.actions = payload.actions;
Expand All @@ -167,8 +161,10 @@ class FetchMessagesRequest extends request_1.AbstractRequest {
return `/v3/${endpoint}/sub-key/${subscribeKey}/channel/${(0, utils_1.encodeNames)(channels)}`;
}
get queryParameters() {
const { start, end, count, includeMessageType, includeMeta, includeUUID, stringifiedTimeToken } = this.parameters;
return Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({ max: count }, (start ? { start } : {})), (end ? { end } : {})), (stringifiedTimeToken ? { string_message_token: 'true' } : {})), (includeMeta !== undefined && includeMeta ? { include_meta: 'true' } : {})), (includeUUID ? { include_uuid: 'true' } : {})), (includeMessageType ? { include_message_type: 'true' } : {}));
const { start, end, count, includeCustomMessageType, includeMessageType, includeMeta, includeUUID, stringifiedTimeToken, } = this.parameters;
return Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({ max: count }, (start ? { start } : {})), (end ? { end } : {})), (stringifiedTimeToken ? { string_message_token: 'true' } : {})), (includeMeta !== undefined && includeMeta ? { include_meta: 'true' } : {})), (includeUUID ? { include_uuid: 'true' } : {})), (includeCustomMessageType !== undefined && includeCustomMessageType !== null
? { include_custom_message_type: includeCustomMessageType ? 'true' : 'false' }
: {})), (includeMessageType ? { include_message_type: 'true' } : {}));
}
/**
* Parse single channel data entry.
Expand Down
4 changes: 2 additions & 2 deletions lib/core/endpoints/file_upload/publish_file.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ class PublishFileMessageRequest extends request_1.AbstractRequest {
return `/v1/files/publish-file/${publishKey}/${subscribeKey}/0/${(0, utils_1.encodeString)(channel)}/0/${(0, utils_1.encodeString)(this.prepareMessagePayload(fileMessage))}`;
}
get queryParameters() {
const { storeInHistory, ttl, meta } = this.parameters;
return Object.assign(Object.assign({ store: storeInHistory ? '1' : '0' }, (ttl ? { ttl } : {})), (meta && typeof meta === 'object' ? { meta: JSON.stringify(meta) } : {}));
const { customMessageType, storeInHistory, ttl, meta } = this.parameters;
return Object.assign(Object.assign(Object.assign({ store: storeInHistory ? '1' : '0' }, (customMessageType ? { custom_message_type: customMessageType } : {})), (ttl ? { ttl } : {})), (meta && typeof meta === 'object' ? { meta: JSON.stringify(meta) } : {}));
}
/**
* Pre-process provided data.
Expand Down
4 changes: 3 additions & 1 deletion lib/core/endpoints/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,10 @@ class PublishRequest extends request_1.AbstractRequest {
return `/publish/${keySet.publishKey}/${keySet.subscribeKey}/0/${(0, utils_1.encodeString)(channel)}/0${!this.parameters.sendByPost ? `/${(0, utils_1.encodeString)(stringifiedPayload)}` : ''}`;
}
get queryParameters() {
const { meta, replicate, storeInHistory, ttl } = this.parameters;
const { customMessageType, meta, replicate, storeInHistory, ttl } = this.parameters;
const query = {};
if (customMessageType)
query.custom_message_type = customMessageType;
if (storeInHistory !== undefined)
query.store = storeInHistory ? '1' : '0';
if (ttl !== undefined)
Expand Down
7 changes: 7 additions & 0 deletions lib/core/endpoints/signal.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,12 @@ class SignalRequest extends request_1.AbstractRequest {
const stringifiedPayload = JSON.stringify(message);
return `/signal/${publishKey}/${subscribeKey}/0/${(0, utils_1.encodeString)(channel)}/0/${(0, utils_1.encodeString)(stringifiedPayload)}`;
}
get queryParameters() {
const { customMessageType } = this.parameters;
const query = {};
if (customMessageType)
query.custom_message_type = customMessageType;
return query;
}
}
exports.SignalRequest = SignalRequest;
6 changes: 6 additions & 0 deletions lib/core/endpoints/subscribe.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ class BaseSubscribeRequest extends request_1.AbstractRequest {
};
if (envelope.u)
event.userMetadata = envelope.u;
if (envelope.cmt)
event.customMessageType = envelope.cmt;
if (decryptionError)
event.error = decryptionError;
return event;
Expand All @@ -231,6 +233,8 @@ class BaseSubscribeRequest extends request_1.AbstractRequest {
};
if (envelope.u)
event.userMetadata = envelope.u;
if (envelope.cmt)
event.customMessageType = envelope.cmt;
return event;
}
messageActionFromEnvelope(envelope) {
Expand Down Expand Up @@ -282,6 +286,8 @@ class BaseSubscribeRequest extends request_1.AbstractRequest {
};
}
}
if (envelope.cmt)
event.customMessageType = envelope.cmt;
if (errorMessage)
event.error = errorMessage;
return event;
Expand Down
39 changes: 39 additions & 0 deletions lib/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4764,6 +4764,10 @@ declare namespace PubNub {
userMetadata?: {
[p: string]: Payload;
};
/**
* User-provided message type.
*/
customMessageType?: string;
/**
* Sent data.
*/
Expand Down Expand Up @@ -6086,6 +6090,13 @@ declare namespace PubNub {
* The message may be any valid JSON type including objects, arrays, strings, and numbers.
*/
message: Payload;
/**
* User-specified message type.
*
* **Important:** string limited by **3**-**50** case-sensitive alphanumeric characters with only
* `-` and `_` special characters allowed.
*/
customMessageType?: string;
/**
* Whether published data should be available with `Storage API` later or not.
*
Expand Down Expand Up @@ -6150,6 +6161,13 @@ declare namespace PubNub {
* The message may be any valid JSON type including objects, arrays, strings, and numbers.
*/
message: Payload;
/**
* User-specified message type.
*
* **Important:** string limited by **3**-**50** case-sensitive alphanumeric characters with only
* `-` and `_` special characters allowed.
*/
customMessageType?: string;
};

/**
Expand Down Expand Up @@ -6566,6 +6584,10 @@ declare namespace PubNub {
* PubNub-defined message type.
*/
messageType?: PubNubMessageType.Message;
/**
* User-provided message type.
*/
customMessageType?: string;
};

/**
Expand Down Expand Up @@ -6610,6 +6632,10 @@ declare namespace PubNub {
* PubNub-defined message type.
*/
messageType?: PubNubMessageType.Files;
/**
* User-provided message type.
*/
customMessageType?: string;
};

/**
Expand Down Expand Up @@ -6652,6 +6678,12 @@ declare namespace PubNub {
* @default `100` or `25`
*/
count?: number;
/**
* Include messages' custom type flag.
*
* Message / signal and file messages may contain user-provided type.
*/
includeCustomMessageType?: boolean;
/**
* Whether message type should be returned with each history message or not.
*
Expand Down Expand Up @@ -7194,6 +7226,13 @@ declare namespace PubNub {
* File annotation message.
*/
message?: Payload;
/**
* User-specified message type.
*
* **Important:** string limited by **3**-**50** case-sensitive alphanumeric characters with only
* `-` and `_` special characters allowed.
*/
customMessageType?: string;
/**
* Custom file and message encryption key.
*
Expand Down
20 changes: 19 additions & 1 deletion src/core/endpoints/fetch_messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ type ServiceResponse = {
*/
uuid?: string;

/**
* User-provided message type.
*/
custom_message_type?: string;

/**
* PubNub-defined message type.
*/
Expand Down Expand Up @@ -232,6 +237,7 @@ export class FetchMessagesRequest extends AbstractRequest<History.FetchMessagesR
timetoken: payload.timetoken,
message: processedPayload.payload,
messageType: payload.message_type,
...(payload.custom_message_type ? { customMessageType: payload.custom_message_type } : {}),
uuid: payload.uuid,
};

Expand Down Expand Up @@ -269,7 +275,16 @@ export class FetchMessagesRequest extends AbstractRequest<History.FetchMessagesR
}

protected get queryParameters(): Query {
const { start, end, count, includeMessageType, includeMeta, includeUUID, stringifiedTimeToken } = this.parameters;
const {
start,
end,
count,
includeCustomMessageType,
includeMessageType,
includeMeta,
includeUUID,
stringifiedTimeToken,
} = this.parameters;

return {
max: count!,
Expand All @@ -278,6 +293,9 @@ export class FetchMessagesRequest extends AbstractRequest<History.FetchMessagesR
...(stringifiedTimeToken! ? { string_message_token: 'true' } : {}),
...(includeMeta !== undefined && includeMeta ? { include_meta: 'true' } : {}),
...(includeUUID! ? { include_uuid: 'true' } : {}),
...(includeCustomMessageType !== undefined && includeCustomMessageType !== null
? { include_custom_message_type: includeCustomMessageType ? 'true' : 'false' }
: {}),
...(includeMessageType! ? { include_message_type: 'true' } : {}),
};
}
Expand Down
3 changes: 2 additions & 1 deletion src/core/endpoints/file_upload/publish_file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,10 @@ export class PublishFileMessageRequest extends AbstractRequest<FileSharing.Publi
}

protected get queryParameters(): Query {
const { storeInHistory, ttl, meta } = this.parameters;
const { customMessageType, storeInHistory, ttl, meta } = this.parameters;
return {
store: storeInHistory! ? '1' : '0',
...(customMessageType ? { custom_message_type: customMessageType } : {}),
...(ttl ? { ttl } : {}),
...(meta && typeof meta === 'object' ? { meta: JSON.stringify(meta) } : {}),
};
Expand Down
11 changes: 10 additions & 1 deletion src/core/endpoints/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ export type PublishParameters = {
*/
message: Payload;

/**
* User-specified message type.
*
* **Important:** string limited by **3**-**50** case-sensitive alphanumeric characters with only
* `-` and `_` special characters allowed.
*/
customMessageType?: string;

/**
* Whether published data should be available with `Storage API` later or not.
*
Expand Down Expand Up @@ -176,9 +184,10 @@ export class PublishRequest extends AbstractRequest<PublishResponse> {
}

protected get queryParameters(): Query {
const { meta, replicate, storeInHistory, ttl } = this.parameters;
const { customMessageType, meta, replicate, storeInHistory, ttl } = this.parameters;
const query: Query = {};

if (customMessageType) query.custom_message_type = customMessageType;
if (storeInHistory !== undefined) query.store = storeInHistory ? '1' : '0';
if (ttl !== undefined) query.ttl = ttl;
if (replicate !== undefined && !replicate) query.norep = 'true';
Expand Down
17 changes: 17 additions & 0 deletions src/core/endpoints/signal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ export type SignalParameters = {
* The message may be any valid JSON type including objects, arrays, strings, and numbers.
*/
message: Payload;

/**
* User-specified message type.
*
* **Important:** string limited by **3**-**50** case-sensitive alphanumeric characters with only
* `-` and `_` special characters allowed.
*/
customMessageType?: string;
};

/**
Expand Down Expand Up @@ -105,4 +113,13 @@ export class SignalRequest extends AbstractRequest<SignalResponse> {

return `/signal/${publishKey}/${subscribeKey}/0/${encodeString(channel)}/0/${encodeString(stringifiedPayload)}`;
}

protected get queryParameters(): Query {
const { customMessageType } = this.parameters;
const query: Query = {};

if (customMessageType) query.custom_message_type = customMessageType;

return query;
}
}
Loading

0 comments on commit 1e5bcc1

Please sign in to comment.