diff --git a/packages/restapi/README.md b/packages/restapi/README.md index d38862a06..a757114fa 100644 --- a/packages/restapi/README.md +++ b/packages/restapi/README.md @@ -4567,15 +4567,14 @@ const aliceMessagesBob = await userAlice.chat.send(bobAddress, { }); ``` -| Param | Type | Default | Remarks | -| ---------------------- | ---------------------------------------------------------------------------- | ------- | ---------------------------------------------------------------------------------- | -| `recipient` | `string` | - | Recipient ( For Group Chats target is chatId, for 1 To 1 chat target is Push DID ) | -| `options` | `object` | - | Configuration for message to be sent | -| `options.type` \* | `Text` or `Image` or `File` or `MediaEmbed` or `GIF` or `Meta` or `Reaction` | - | Type of message Content | -| `options.content` | `string` | - | Message Content | -| `options.action` \* | `string` | - | Message action ( Only available for Meta & Reaction Messages ) | -| `options.reference` \* | `string` or `null` | - | Message reference hash ( Only available for Reaction Messages ) | -| `options.info` \* | `{ affected : string[]: arbitrary?: { [key: string]: any } }` | - | Message reference hash ( Only available for Meta Messages ) | +| Param | Type | Default | Remarks | +| ---------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------- | ---------------------------------------------------------------------------------- | +| `recipient` | `string` | - | Recipient ( For Group Chats target is chatId, for 1 To 1 chat target is Push DID ) | +| `options` | `object` | - | Configuration for message to be sent | +| `options.type` \* | `Text` or `Image` or `Audio` or `Video` or `File` or `MediaEmbed` or `GIF` or `Meta` or `Reaction` or `Receipt` or `Intent` or `Reply` or `Composite` | - | Type of message Content | +| `options.content` | `string` or `{type: `Text`or`Image`or`Audio`or`Video`or`File`or`MediaEmbed`or`GIF` ; content: string}` [For Reply] or `{type: `Text`or`Image`or`Audio`or`Video`or`File`or`MediaEmbed`or`GIF` ; content: string}[]` [For Composite] | - | Message Content | +| `options.reference` \* | `string` | - | Message reference hash ( Only available for Reaction & Reply Messages ) | +| `options.info` \* | `{ affected : string[]: arbitrary?: { [key: string]: any } }` | - | Message reference hash ( Only available for Meta & UserActivity Messages ) | \* - Optional diff --git a/packages/restapi/src/lib/chat/send.ts b/packages/restapi/src/lib/chat/send.ts index f3dfa1be7..095f0fa73 100644 --- a/packages/restapi/src/lib/chat/send.ts +++ b/packages/restapi/src/lib/chat/send.ts @@ -48,7 +48,10 @@ export const send = async ( // Not supported by legacy sdk versions, need to override messageContent to avoid parsing errors on legacy sdk versions let messageContent: string; - if (messageType === MessageType.REPLY) { + if ( + messageType === MessageType.REPLY || + messageType === MessageType.COMPOSITE + ) { messageContent = 'MessageType Not Supported by this sdk version. Plz upgrade !!!'; } else { diff --git a/packages/restapi/src/lib/constants.ts b/packages/restapi/src/lib/constants.ts index c2a8a77cc..15bc97815 100644 --- a/packages/restapi/src/lib/constants.ts +++ b/packages/restapi/src/lib/constants.ts @@ -35,7 +35,7 @@ export enum MessageType { MEDIA_EMBED = 'MediaEmbed', META = 'Meta', REACTION = 'Reaction', - READ_RECEIPT = 'ReadReceipt', + RECEIPT = 'Receipt', USER_ACTIVITY = 'UserActivity', INTENT = 'Intent', REPLY = 'Reply', diff --git a/packages/restapi/src/lib/types/messageTypes.ts b/packages/restapi/src/lib/types/messageTypes.ts index 7c7c4d12b..634121784 100644 --- a/packages/restapi/src/lib/types/messageTypes.ts +++ b/packages/restapi/src/lib/types/messageTypes.ts @@ -57,7 +57,9 @@ export const CHAT = { FIRE: '\u{1F525}', }, - READ_RECEIPT: 'READ_RECEIPT', + RECEIPT: { + READ: 'READ_RECEIPT', + }, UA: { LISTENER: { @@ -96,9 +98,7 @@ export interface InfoMessage } interface ReferenceMessage - extends BaseMessage< - `${MessageType.REACTION}` | `${MessageType.READ_RECEIPT}` - > { + extends BaseMessage<`${MessageType.REACTION}` | `${MessageType.RECEIPT}`> { reference: string; } @@ -115,12 +115,10 @@ interface ReplyMessage { interface CompositeMessage { type: `${MessageType.COMPOSITE}`; /** Only Few BaseMessageTypes are allowed, this can be changed in the future */ - content: [ - { - type: string; - content: string; - } - ]; + content: { + type: string; + content: string; + }[]; } type BaseMessageTypes = diff --git a/packages/restapi/src/lib/validations/messageObject.ts b/packages/restapi/src/lib/validations/messageObject.ts index 4b423abf3..d40382126 100644 --- a/packages/restapi/src/lib/validations/messageObject.ts +++ b/packages/restapi/src/lib/validations/messageObject.ts @@ -35,8 +35,10 @@ const reationMessageObjSchema = Joi.object({ reference: Joi.string().required(), }); -const readReceiptMessageObjSchema = Joi.object({ - content: Joi.string().valid(CHAT.READ_RECEIPT).required(), +const receiptMessageObjSchema = Joi.object({ + content: Joi.string() + .valid(...Object.values(extractValidValues(CHAT.RECEIPT))) + .required(), reference: Joi.string().required(), }); @@ -125,8 +127,8 @@ export const validateMessageObj = ( error = reationMessageObjSchema.validate(messageObj).error; break; } - case MessageType.READ_RECEIPT: { - error = readReceiptMessageObjSchema.validate(messageObj).error; + case MessageType.RECEIPT: { + error = receiptMessageObjSchema.validate(messageObj).error; break; } case MessageType.USER_ACTIVITY: { diff --git a/packages/restapi/tests/lib/chat/send.test.ts b/packages/restapi/tests/lib/chat/send.test.ts index d18193785..5aeb21512 100644 --- a/packages/restapi/tests/lib/chat/send.test.ts +++ b/packages/restapi/tests/lib/chat/send.test.ts @@ -19,7 +19,7 @@ import { CHAT } from '../../../src/lib/types/messageTypes'; chai.use(chaiAsPromised); const _env = Constants.ENV.DEV; -describe('PushAPI.chat.send', () => { +describe.only('PushAPI.chat.send', () => { const provider = ethers.getDefaultProvider(5); let _signer1: any; let walletAddress1: string; @@ -1778,9 +1778,9 @@ describe('PushAPI.chat.send', () => { ); }); }); - describe('Read Receipt Message', () => { - const MESSAGE_TYPE = MessageType.READ_RECEIPT; - const MESSAGE = CHAT.READ_RECEIPT; + describe('Receipt Message', () => { + const MESSAGE_TYPE = MessageType.RECEIPT; + const MESSAGE = CHAT.RECEIPT.READ; it('should throw error using messageContent on wrong MessageObject', async () => { await expect( send({ @@ -2421,8 +2421,8 @@ describe('PushAPI.chat.send', () => { messageType: MESSAGE_TYPE, messageObj: { content: { - type: MessageType.READ_RECEIPT, - content: CHAT.READ_RECEIPT, + type: MessageType.RECEIPT, + content: CHAT.RECEIPT.READ, }, reference: 'bafyreia22girudospfbs3q7t6eelb453rmwsi7shkejwxtwpp57xww6vae', @@ -2577,26 +2577,7 @@ describe('PushAPI.chat.send', () => { messageType: MESSAGE_TYPE, messageObj: { content: MESSAGE, - }, - receiverAddress: account2, - signer: _signer1, - env: _env, - }) - ).to.be.rejected; - - await expect( - send({ - messageType: MESSAGE_TYPE, - messageObj: { - // Adding content is legacy format is not allowed for reply - content: { - messageType: MessageType.TEXT, - messageObj: { - content: 'Hey', - }, - }, - reference: - 'bafyreia22girudospfbs3q7t6eelb453rmwsi7shkejwxtwpp57xww6vae', + info: { affected: [] }, // not supported for composite }, receiverAddress: account2, signer: _signer1, @@ -2610,8 +2591,6 @@ describe('PushAPI.chat.send', () => { messageType: MESSAGE_TYPE, messageObj: { content: 'Invalid Message', - reference: - 'bafyreia22girudospfbs3q7t6eelb453rmwsi7shkejwxtwpp57xww6vae', }, receiverAddress: account2, signer: _signer1, @@ -2619,17 +2598,17 @@ describe('PushAPI.chat.send', () => { }) ).to.be.rejected; }); - it('should throw error for unsupported messageType reply', async () => { + it('should throw error for unsupported messageType composite', async () => { await expect( send({ messageType: MESSAGE_TYPE, messageObj: { - content: { - type: MessageType.READ_RECEIPT, - content: CHAT.READ_RECEIPT, - }, - reference: - 'bafyreia22girudospfbs3q7t6eelb453rmwsi7shkejwxtwpp57xww6vae', + content: [ + { + type: MessageType.READ_RECEIPT, + content: CHAT.READ_RECEIPT, + }, + ], }, receiverAddress: walletAddress2, signer: _signer1, @@ -2642,8 +2621,6 @@ describe('PushAPI.chat.send', () => { messageType: MESSAGE_TYPE, messageObj: { content: MESSAGE, - reference: - 'bafyreia22girudospfbs3q7t6eelb453rmwsi7shkejwxtwpp57xww6vae', }, receiverAddress: walletAddress2, signer: _signer1, @@ -2654,12 +2631,12 @@ describe('PushAPI.chat.send', () => { msg, MESSAGE_TYPE, { - content: { - messageType: MESSAGE.type, - messageObj: { content: MESSAGE.content }, - }, - reference: - 'bafyreia22girudospfbs3q7t6eelb453rmwsi7shkejwxtwpp57xww6vae', + content: [ + { + messageType: MESSAGE[0].type, + messageObj: { content: MESSAGE[0].content }, + }, + ], }, account1, _signer1, @@ -2678,8 +2655,6 @@ describe('PushAPI.chat.send', () => { messageType: MESSAGE_TYPE, messageObj: { content: MESSAGE, - reference: - 'bafyreia22girudospfbs3q7t6eelb453rmwsi7shkejwxtwpp57xww6vae', }, receiverAddress: walletAddress2, signer: _signer1, @@ -2689,12 +2664,12 @@ describe('PushAPI.chat.send', () => { msg, MESSAGE_TYPE, { - content: { - messageType: MESSAGE.type, - messageObj: { content: MESSAGE.content }, - }, - reference: - 'bafyreia22girudospfbs3q7t6eelb453rmwsi7shkejwxtwpp57xww6vae', + content: [ + { + messageType: MESSAGE[0].type, + messageObj: { content: MESSAGE[0].content }, + }, + ], }, account1, _signer1, @@ -2707,8 +2682,6 @@ describe('PushAPI.chat.send', () => { message: { type: MESSAGE_TYPE, content: MESSAGE, - reference: - 'bafyreia22girudospfbs3q7t6eelb453rmwsi7shkejwxtwpp57xww6vae', }, to: walletAddress2, signer: _signer1, @@ -2718,12 +2691,12 @@ describe('PushAPI.chat.send', () => { msg, MESSAGE_TYPE, { - content: { - messageType: MESSAGE.type, - messageObj: { content: MESSAGE.content }, - }, - reference: - 'bafyreia22girudospfbs3q7t6eelb453rmwsi7shkejwxtwpp57xww6vae', + content: [ + { + messageType: MESSAGE[0].type, + messageObj: { content: MESSAGE[0].content }, + }, + ], }, account1, _signer1, @@ -2751,12 +2724,12 @@ describe('PushAPI.chat.send', () => { msg, MESSAGE_TYPE, { - content: { - messageType: MESSAGE.type, - messageObj: { content: MESSAGE.content }, - }, - reference: - 'bafyreia22girudospfbs3q7t6eelb453rmwsi7shkejwxtwpp57xww6vae', + content: [ + { + messageType: MESSAGE[0].type, + messageObj: { content: MESSAGE[0].content }, + }, + ], }, account1, _signer1,