Skip to content

Commit

Permalink
fix: added composite, fixed receipt
Browse files Browse the repository at this point in the history
  • Loading branch information
Aman035 committed Sep 26, 2023
1 parent a6782b1 commit f7a4f88
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 90 deletions.
17 changes: 8 additions & 9 deletions packages/restapi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 4 additions & 1 deletion packages/restapi/src/lib/chat/send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion packages/restapi/src/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
18 changes: 8 additions & 10 deletions packages/restapi/src/lib/types/messageTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ export const CHAT = {
FIRE: '\u{1F525}',
},

READ_RECEIPT: 'READ_RECEIPT',
RECEIPT: {
READ: 'READ_RECEIPT',
},

UA: {
LISTENER: {
Expand Down Expand Up @@ -96,9 +98,7 @@ export interface InfoMessage
}

interface ReferenceMessage
extends BaseMessage<
`${MessageType.REACTION}` | `${MessageType.READ_RECEIPT}`
> {
extends BaseMessage<`${MessageType.REACTION}` | `${MessageType.RECEIPT}`> {
reference: string;
}

Expand All @@ -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 =
Expand Down
10 changes: 6 additions & 4 deletions packages/restapi/src/lib/validations/messageObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
});

Expand Down Expand Up @@ -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: {
Expand Down
103 changes: 38 additions & 65 deletions packages/restapi/tests/lib/chat/send.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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,
Expand All @@ -2610,26 +2591,24 @@ describe('PushAPI.chat.send', () => {
messageType: MESSAGE_TYPE,
messageObj: {
content: 'Invalid Message',
reference:
'bafyreia22girudospfbs3q7t6eelb453rmwsi7shkejwxtwpp57xww6vae',
},
receiverAddress: account2,
signer: _signer1,
env: _env,
})
).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,
Expand All @@ -2642,8 +2621,6 @@ describe('PushAPI.chat.send', () => {
messageType: MESSAGE_TYPE,
messageObj: {
content: MESSAGE,
reference:
'bafyreia22girudospfbs3q7t6eelb453rmwsi7shkejwxtwpp57xww6vae',
},
receiverAddress: walletAddress2,
signer: _signer1,
Expand All @@ -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,
Expand All @@ -2678,8 +2655,6 @@ describe('PushAPI.chat.send', () => {
messageType: MESSAGE_TYPE,
messageObj: {
content: MESSAGE,
reference:
'bafyreia22girudospfbs3q7t6eelb453rmwsi7shkejwxtwpp57xww6vae',
},
receiverAddress: walletAddress2,
signer: _signer1,
Expand All @@ -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,
Expand All @@ -2707,8 +2682,6 @@ describe('PushAPI.chat.send', () => {
message: {
type: MESSAGE_TYPE,
content: MESSAGE,
reference:
'bafyreia22girudospfbs3q7t6eelb453rmwsi7shkejwxtwpp57xww6vae',
},
to: walletAddress2,
signer: _signer1,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit f7a4f88

Please sign in to comment.