Skip to content

Commit

Permalink
fix: Merge branch 'main' into deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
mohammeds1992 committed Aug 11, 2023
2 parents ae25b69 + 2856ece commit cd9f1e0
Show file tree
Hide file tree
Showing 6 changed files with 617 additions and 43 deletions.
8 changes: 4 additions & 4 deletions packages/restapi/src/lib/chat/helpers/payloadHelper.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { isValidETHAddress, walletToPCAIP10 } from '../../helpers';
import { IConnectedUser, GroupDTO, SpaceDTO, ChatStatus } from '../../types';
import { getEncryptedRequest } from './crypto';
import { ENV } from '../../constants';
import { ENV, MessageType } from '../../constants';
import * as AES from './aes';
import { META_MESSAGE_META } from '../../types/metaTypes';
import { MessageTypeSpecificMeta } from '../../types/metaTypes';
import { sign } from './pgp';
import * as CryptoJS from 'crypto-js';
export interface ISendMessagePayload {
Expand All @@ -14,7 +14,7 @@ export interface ISendMessagePayload {
messageObj:
| {
content: string;
meta?: META_MESSAGE_META;
meta?: MessageTypeSpecificMeta[MessageType];
}
| string;
messageType: string;
Expand Down Expand Up @@ -74,7 +74,7 @@ export const sendMessagePayload = async (
senderCreatedUser: IConnectedUser,
messageObj: {
content: string;
meta?: META_MESSAGE_META;
meta?: MessageTypeSpecificMeta[MessageType];
},
messageContent: string,
messageType: string,
Expand Down
33 changes: 29 additions & 4 deletions packages/restapi/src/lib/chat/send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
import { conversationHash } from './conversationHash';
import { ISendMessagePayload, sendMessagePayload } from './helpers';
import { getGroup } from './getGroup';
import { REACTION_SYMBOL, REACTION_TYPE } from '../types/metaTypes';

/**
* SENDS A PUSH CHAT MESSAGE
Expand All @@ -25,7 +26,6 @@ export const send = async (
account = null,
signer = null,
env = Constants.ENV.PROD,

} = options || {};

try {
Expand All @@ -44,6 +44,13 @@ export const send = async (
: null;

let messageObj = options.messageObj;

// OVERRIDE CONTENT FOR REACTION MESSAGE
if (messageType === MessageType.REACTION && messageObj) {
messageObj.content =
REACTION_SYMBOL[messageObj?.meta?.action as REACTION_TYPE];
}

// possible for initial types 'Text', 'Image', 'File', 'GIF', 'MediaEmbed'
if (!messageObj) {
messageObj = {
Expand Down Expand Up @@ -126,6 +133,20 @@ const validateOptions = async (options: ChatSendOptionsType) => {
}
}

if (
(messageType === MessageType.TEXT ||
messageType === MessageType.IMAGE ||
messageType === MessageType.FILE ||
messageType === MessageType.MEDIA_EMBED ||
messageType === MessageType.GIF) &&
messageObj &&
messageObj.meta
) {
throw new Error(
`Unable to parse this messageType. Meta is not allowed for this messageType.`
);
}

if (messageType === MessageType.META) {
if (
!(messageObj instanceof Object) ||
Expand All @@ -138,10 +159,14 @@ const validateOptions = async (options: ChatSendOptionsType) => {
`Unable to parse this messageType. Please ensure 'messageObj' is properly defined.`
);
}
} else {
if (messageObj && messageObj.meta) {
} else if (messageType === MessageType.REACTION) {
if (
!(messageObj instanceof Object) ||
!(messageObj.meta instanceof Object) ||
!('action' in messageObj.meta)
) {
throw new Error(
`Unable to parse this messageType. Meta is not allowed for this messageType.`
`Unable to parse this messageType. Please ensure 'messageObj' is properly defined.`
);
}
}
Expand Down
1 change: 1 addition & 0 deletions packages/restapi/src/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export enum MessageType {
FILE = 'File',
MEDIA_EMBED = 'MediaEmbed',
META = 'Meta',
REACTION = 'Reaction',
/**
* @deprecated - Use MediaEmbed Instead
*/
Expand Down
4 changes: 2 additions & 2 deletions packages/restapi/src/lib/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from '../../lib/payloads/constants';
import { ENV, MessageType } from '../constants';
import { EthEncryptedData } from '@metamask/eth-sig-util';
import { META_MESSAGE_META } from './metaTypes';
import { META_MESSAGE_META, MessageTypeSpecificMeta } from './metaTypes';

export type Env = typeof ENV[keyof typeof ENV];

Expand Down Expand Up @@ -440,7 +440,7 @@ export interface ChatSendOptionsType {
messageType?: `${MessageType}`;
messageObj?: {
content: string;
meta?: META_MESSAGE_META;
meta?: MessageTypeSpecificMeta[MessageType];
};
/**
* @deprecated - Use messageObj.content instead
Expand Down
43 changes: 43 additions & 0 deletions packages/restapi/src/lib/types/metaTypes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
/**
* This file defines the type for meta property for a Push Chat message
*/
import { MessageType } from '../constants';

export interface MessageTypeSpecificMeta {
[MessageType.TEXT]: never; // No meta for TEXT type
[MessageType.IMAGE]: never; // No meta for IMAGE type
[MessageType.FILE]: never; // No meta for FILE type
[MessageType.MEDIA_EMBED]: never; // No meta for MEDIA_EMBED type
[MessageType.GIF]: never; // No meta for GIF type
[MessageType.META]: META_MESSAGE_META;
[MessageType.REACTION]: REACTION_MESSAGE_META;
}

export const enum META_ACTION {
/**
Expand Down Expand Up @@ -39,6 +50,38 @@ export type META_MESSAGE_META = {
};
};

export enum REACTION_TYPE {
THUMBS_UP,
THUMBS_DOWN,
HEART,
CLAP,
LAUGHING_FACE,
SAD_FACE,
ANGRY_FACE,
SURPRISED_FACE,
CLAPPING_HANDS,
FIRE,
}

// Create a mapping object that associates reaction types with their Unicode escape sequences
export const REACTION_SYMBOL: Record<REACTION_TYPE, string> = {
[REACTION_TYPE.THUMBS_UP]: '\u{1F44D}',
[REACTION_TYPE.THUMBS_DOWN]: '\u{1F44E}',
[REACTION_TYPE.HEART]: '\u{2764}\u{FE0F}',
[REACTION_TYPE.CLAP]: '\u{1F44F}',
[REACTION_TYPE.LAUGHING_FACE]: '\u{1F602}',
[REACTION_TYPE.SAD_FACE]: '\u{1F622}',
[REACTION_TYPE.ANGRY_FACE]: '\u{1F621}',
[REACTION_TYPE.SURPRISED_FACE]: '\u{1F632}',
[REACTION_TYPE.CLAPPING_HANDS]: '\u{1F44F}\u{1F44F}',
[REACTION_TYPE.FIRE]: '\u{1F525}',
};

export type REACTION_MESSAGE_META = {
action: REACTION_TYPE;
conversationHash?: string;
};

// TODO
// export type REPLY_MESSAGE_META = {};
// export type COMPOSITE_MESSAGE_META = {};
Expand Down
Loading

0 comments on commit cd9f1e0

Please sign in to comment.