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

Message Type Implementations #730

Merged
merged 4 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
89 changes: 43 additions & 46 deletions packages/restapi/src/lib/chat/send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,8 @@ import {
import { conversationHash } from './conversationHash';
import { ISendMessagePayload, sendMessagePayload } from './helpers';
import { getGroup } from './getGroup';
import {
MessageObj,
REACTION_SYMBOL,
ReactionMessage,
} from '../types/messageTypes';
import {
messageObjSchema,
metaMessageObjSchema,
reationMessageObjSchema,
} from '../validations/messageObject';
import { MessageObj } from '../types/messageTypes';
import { validateMessageObj } from '../validations/messageObject';

/**
* SENDS A PUSH CHAT MESSAGE
Expand Down Expand Up @@ -54,14 +46,18 @@ export const send = async (
})
: null;

// OVERRIDE CONTENT FOR REACTION MESSAGE
if (messageType === MessageType.REACTION) {
messageObj.content =
REACTION_SYMBOL[(messageObj as Omit<ReactionMessage, 'type'>).action];
// 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 ||
messageType === MessageType.COMPOSITE
) {
messageContent =
'MessageType Not Supported by this sdk version. Plz upgrade !!!';
} else {
messageContent = messageObj.content as string;
}

const messageContent = messageObj.content; // provide backward compatibility & override deprecated field

const conversationResponse = await conversationHash({
conversationId: receiver,
account: sender.did,
Expand Down Expand Up @@ -138,36 +134,7 @@ const validateOptions = async (options: ComputedOptionsType) => {
}
}

if (
messageType === MessageType.TEXT ||
messageType === MessageType.IMAGE ||
messageType === MessageType.FILE ||
messageType === MessageType.MEDIA_EMBED ||
messageType === MessageType.GIF
) {
const { error } = messageObjSchema.validate(messageObj);
if (error) {
throw new Error(
`Unable to parse this messageType. Please ensure 'messageObj' is properly defined.`
);
}
}

if (messageType === MessageType.META) {
const { error } = metaMessageObjSchema.validate(messageObj);
if (error) {
throw new Error(
`Unable to parse this messageType. Please ensure 'messageObj' is properly defined.`
);
}
} else if (messageType === MessageType.REACTION) {
const { error } = reationMessageObjSchema.validate(messageObj);
if (error) {
throw new Error(
`Unable to parse this messageType. Please ensure 'messageObj' is properly defined.`
);
}
}
validateMessageObj(messageObj, messageType);
};

const computeOptions = (options: ChatSendOptionsType): ComputedOptionsType => {
Expand Down Expand Up @@ -203,6 +170,36 @@ const computeOptions = (options: ChatSendOptionsType): ComputedOptionsType => {
messageObj = rest;
}

// Parse Reply Message
if (messageType === MessageType.REPLY) {
if (typeof messageObj.content === 'object') {
const { type, ...rest } = messageObj.content;
messageObj.content = {
messageType: type,
messageObj: rest,
};
} else {
throw new Error('Options.message is not properly defined for Reply');
}
}

// Parse Composite Message
if (messageType === MessageType.COMPOSITE) {
if (messageObj.content instanceof Array) {
messageObj.content = messageObj.content.map(
(obj: { type: string; content: string }) => {
const { type, ...rest } = obj;
return {
messageType: type,
messageObj: rest,
};
}
);
} else {
throw new Error('Options.message is not properly defined for Composite');
}
}

const account = options.account !== undefined ? options.account : null;

const to = options.to !== undefined ? options.to : options.receiverAddress;
Expand Down
21 changes: 10 additions & 11 deletions packages/restapi/src/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,20 @@ export enum ENCRYPTION_TYPE {
export enum MessageType {
TEXT = 'Text',
IMAGE = 'Image',
VIDEO = 'Video',
AUDIO = 'Audio',
FILE = 'File',
/** @deprecated - Use `MediaEmbed` Instead */
GIF = 'GIF',
MEDIA_EMBED = 'MediaEmbed',
META = 'Meta',
REACTION = 'Reaction',
/**
* @deprecated - Use MediaEmbed Instead
*/
GIF = 'GIF',

// TODO
// AUDIO = 'Audio',
// VIDEO = 'Video',
// PAYMENT = 'Payment',
// REPLY = 'Reply',
// COMPOSITE = 'Composite',
RECEIPT = 'Receipt',
USER_ACTIVITY = 'UserActivity',
INTENT = 'Intent',
REPLY = 'Reply',
COMPOSITE = 'Composite',
PAYMENT = 'Payment',
}

const Constants = {
Expand Down
4 changes: 2 additions & 2 deletions packages/restapi/src/lib/space/Space.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
} from '../types';
import { VIDEO_CALL_TYPE } from '../payloads/constants';
import sendLiveSpaceData from './helpers/sendLiveSpaceData';
import { META_ACTION } from '../types/messageTypes';
import { CHAT } from '../types/messageTypes';
import { broadcastRaisedHand } from './broadcastRaisedHand';
import { onReceiveMetaMessage } from './onReceiveMetaMessage';
import { onJoinListener } from './onJoinListener';
Expand Down Expand Up @@ -157,7 +157,7 @@ export class Space extends Video {
env: this.env,
spaceId: this.spaceSpecificData.spaceId,
signer: this.signer,
action: META_ACTION.PROMOTE_TO_ADMIN, // TODO: Add a meta action for SPEAKER_JOINED
action: CHAT.META.GROUP.ADMIN.PRVILEGE, // TODO: Add a meta action for SPEAKER_JOINED
});
}
},
Expand Down
4 changes: 2 additions & 2 deletions packages/restapi/src/lib/space/broadcastRaisedHand.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { produce } from 'immer';
import sendLiveSpaceData from './helpers/sendLiveSpaceData';
import { META_ACTION } from '../types/messageTypes';
import { CHAT } from '../types/messageTypes';
import { pCAIP10ToWallet } from '../helpers';

import type Space from './Space';
Expand Down Expand Up @@ -41,6 +41,6 @@ export async function broadcastRaisedHand(
env: this.env,
spaceId: this.spaceSpecificData.spaceId,
signer: this.signer,
action: META_ACTION.USER_INTERACTION,
action: CHAT.META.GROUP.USER.INTERACTION,
});
}
4 changes: 2 additions & 2 deletions packages/restapi/src/lib/space/helpers/getLiveSpaceData.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { conversationHash, history } from '../../chat';
import { MessageType } from '../../constants';
import { EnvOptionsType, LiveSpaceData } from '../../types';
import { MetaMessage } from '../../types/messageTypes';
import { InfoMessage } from '../../types/messageTypes';
import { initLiveSpaceData } from '../Space';

interface GetLatestMessageType extends EnvOptionsType {
Expand Down Expand Up @@ -54,7 +54,7 @@ const getLiveSpaceData = async ({
latestMetaMessage.messageObj !== null
) {
// found the latest meta message
liveSpaceData = (latestMetaMessage.messageObj as Omit<MetaMessage, 'type'>)
liveSpaceData = (latestMetaMessage.messageObj as Omit<InfoMessage, 'type'>)
?.info?.arbitrary as LiveSpaceData;
}

Expand Down
6 changes: 2 additions & 4 deletions packages/restapi/src/lib/space/helpers/sendLiveSpaceData.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { send } from '../../chat';
import { MessageType } from '../../constants';
import { EnvOptionsType, LiveSpaceData, SignerType } from '../../types';
import { META_ACTION } from '../../types/messageTypes';

interface SendLiveSpaceData extends EnvOptionsType {
liveSpaceData?: LiveSpaceData;
action: META_ACTION;
action: string;
spaceId: string;
pgpPrivateKey: string;
signer: SignerType;
Expand All @@ -26,8 +25,7 @@ const sendLiveSpaceData = async ({
signer,
messageType: MessageType.META,
messageObj: {
content: 'PUSH SPACE META MESSAGE',
action,
content: action,
info: {
affected: [],
arbitrary: liveSpaceData,
Expand Down
15 changes: 8 additions & 7 deletions packages/restapi/src/lib/space/onJoinListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { get } from './get';
import { pCAIP10ToWallet } from '../helpers';
import { produce } from 'immer';

import { META_ACTION } from '../types/messageTypes';
import { CHAT } from '../types/messageTypes';
import type Space from './Space';

export interface OnJoinListenerType {
Expand Down Expand Up @@ -43,11 +43,12 @@ export async function onJoinListener(this: Space, options: OnJoinListenerType) {
const modifiedLiveSpaceData = produce(
this.spaceSpecificData.liveSpaceData,
(draft) => {
const isListenerAlreadyAdded = this.spaceSpecificData.liveSpaceData.listeners.find(
(currentListener) =>
pCAIP10ToWallet(currentListener.address) ===
pCAIP10ToWallet(receivedAddress)
);
const isListenerAlreadyAdded =
this.spaceSpecificData.liveSpaceData.listeners.find(
(currentListener) =>
pCAIP10ToWallet(currentListener.address) ===
pCAIP10ToWallet(receivedAddress)
);

if (isListenerAlreadyAdded) {
// listener is already added in the meta message
Expand Down Expand Up @@ -75,6 +76,6 @@ export async function onJoinListener(this: Space, options: OnJoinListenerType) {
env: this.env,
signer: this.signer,
liveSpaceData: modifiedLiveSpaceData,
action: META_ACTION.ADD_LISTENER,
action: CHAT.META.SPACE.LISTENER.ADD,
});
}
6 changes: 3 additions & 3 deletions packages/restapi/src/lib/space/onReceiveMetaMessage.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { MessageType } from '../constants';
import { IMessageIPFS, LiveSpaceData } from '../types';
import { MetaMessage } from '../types/messageTypes';
import { InfoMessage } from '../types/messageTypes';
import type Space from './Space';

export interface OnReceiveMetaMessageType {
Expand All @@ -18,14 +18,14 @@ export function onReceiveMetaMessage(
if (
receivedMetaMessage.messageType !== MessageType.META ||
typeof receivedMetaMessage.messageObj !== 'object' ||
!(receivedMetaMessage?.messageObj as Omit<MetaMessage, 'type'>)?.info
!(receivedMetaMessage?.messageObj as Omit<InfoMessage, 'type'>)?.info
?.arbitrary
) {
return;
}

const receivedLiveSpaceData = (
receivedMetaMessage.messageObj as Omit<MetaMessage, 'type'>
receivedMetaMessage.messageObj as Omit<InfoMessage, 'type'>
).info.arbitrary as LiveSpaceData;

console.log('RECEIVED LIVE SPACE DATA', receivedLiveSpaceData);
Expand Down
4 changes: 2 additions & 2 deletions packages/restapi/src/lib/space/rejectPromotionRequest.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { produce } from 'immer';
import sendLiveSpaceData from './helpers/sendLiveSpaceData';
import { META_ACTION } from '../types/messageTypes';
import { CHAT } from '../types/messageTypes';
import { pCAIP10ToWallet } from '../helpers';

import type Space from './Space';
Expand Down Expand Up @@ -41,6 +41,6 @@ export async function rejectPromotionRequest(
env: this.env,
spaceId: this.spaceSpecificData.spaceId,
signer: this.signer,
action: META_ACTION.USER_INTERACTION, // TODO: Add a reject request type
action: CHAT.META.GROUP.USER.INTERACTION, // TODO: Add a reject request type
});
}
4 changes: 2 additions & 2 deletions packages/restapi/src/lib/space/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export interface StartSpaceType extends EnvOptionsType {
import type Space from './Space';
import { produce } from 'immer';
import { pCAIP10ToWallet } from '../helpers';
import { META_ACTION } from '../types/messageTypes';
import { CHAT } from '../types/messageTypes';
import sendLiveSpaceData from './helpers/sendLiveSpaceData';

type StartType = {
Expand Down Expand Up @@ -93,7 +93,7 @@ export async function start(this: Space, options: StartType): Promise<void> {

await sendLiveSpaceData({
liveSpaceData,
action: META_ACTION.CREATE_SPACE,
action: CHAT.META.SPACE.CREATE,
spaceId: this.spaceSpecificData.spaceId,
signer: this.signer,
pgpPrivateKey: this.pgpPrivateKey,
Expand Down
Loading
Loading