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

fix: changes messageObj for meta and reaction message type #645

Merged
merged 2 commits into from
Aug 23, 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
1 change: 1 addition & 0 deletions packages/restapi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"buffer": "^6.0.3",
"crypto-js": "^4.1.1",
"immer": "^10.0.2",
"joi": "^17.9.2",
"livepeer": "^2.5.8",
"openpgp": "^5.5.0",
"simple-peer": "^9.11.1",
Expand Down
14 changes: 3 additions & 11 deletions packages/restapi/src/lib/chat/helpers/payloadHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,15 @@ import { IConnectedUser, GroupDTO, SpaceDTO, ChatStatus, Rules, SpaceRules } fro
import { getEncryptedRequest } from './crypto';
import { ENV, MessageType } from '../../constants';
import * as AES from './aes';
import { MessageTypeSpecificMeta } from '../../types/metaTypes';
import { MessageTypeSpecificObject } from '../../types/messageObjectTypes';
import { sign } from './pgp';
import * as CryptoJS from 'crypto-js';
export interface ISendMessagePayload {
fromDID: string;
toDID: string;
fromCAIP10: string;
toCAIP10: string;
messageObj:
| {
content: string;
meta?: MessageTypeSpecificMeta[MessageType];
}
| string;
messageObj: MessageTypeSpecificObject[MessageType] | string;
messageType: string;
encType: string;
encryptedSecret: string | null | undefined;
Expand Down Expand Up @@ -73,10 +68,7 @@ export interface IUpdateGroupRequestPayload {
export const sendMessagePayload = async (
receiverAddress: string,
senderCreatedUser: IConnectedUser,
messageObj: {
content: string;
meta?: MessageTypeSpecificMeta[MessageType];
},
messageObj: MessageTypeSpecificObject[MessageType],
messageContent: string,
messageType: string,
group: GroupDTO | null,
Expand Down
46 changes: 25 additions & 21 deletions packages/restapi/src/lib/chat/send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,15 @@ import {
import { conversationHash } from './conversationHash';
import { ISendMessagePayload, sendMessagePayload } from './helpers';
import { getGroup } from './getGroup';
import { REACTION_SYMBOL, REACTION_TYPE } from '../types/metaTypes';
import {
MessageTypeSpecificObject,
REACTION_SYMBOL,
} from '../types/messageObjectTypes';
import {
messageObjSchema,
metaMessageObjSchema,
reationMessageObjSchema,
} from '../validations/messageObject';

/**
* SENDS A PUSH CHAT MESSAGE
Expand Down Expand Up @@ -48,7 +56,9 @@ export const send = async (
// OVERRIDE CONTENT FOR REACTION MESSAGE
if (messageType === MessageType.REACTION && messageObj) {
messageObj.content =
REACTION_SYMBOL[messageObj?.meta?.action as REACTION_TYPE];
REACTION_SYMBOL[
(messageObj as MessageTypeSpecificObject[MessageType.REACTION]).action
];
}

// possible for initial types 'Text', 'Image', 'File', 'GIF', 'MediaEmbed'
Expand Down Expand Up @@ -139,32 +149,26 @@ const validateOptions = async (options: ChatSendOptionsType) => {
messageType === MessageType.FILE ||
messageType === MessageType.MEDIA_EMBED ||
messageType === MessageType.GIF) &&
messageObj &&
messageObj.meta
messageObj
) {
throw new Error(
`Unable to parse this messageType. Meta is not allowed for this messageType.`
);
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) {
if (
!(messageObj instanceof Object) ||
!(messageObj.meta instanceof Object) ||
!('action' in messageObj.meta) ||
!('info' in messageObj.meta) ||
!(messageObj.meta.info.affected instanceof Array)
) {
if (messageType === MessageType.META && messageObj) {
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) {
if (
!(messageObj instanceof Object) ||
!(messageObj.meta instanceof Object) ||
!('action' in messageObj.meta)
) {
} else if (messageType === MessageType.REACTION && messageObj) {
const { error } = reationMessageObjSchema.validate(messageObj);
if (error) {
throw new Error(
`Unable to parse this messageType. Please ensure 'messageObj' is properly defined.`
);
Expand Down
2 changes: 1 addition & 1 deletion packages/restapi/src/lib/space/Space.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import {
import { VIDEO_CALL_TYPE } from '../payloads/constants';
import getLiveSpaceData from './helpers/getLiveSpaceData';
import sendLiveSpaceData from './helpers/sendLiveSpaceData';
import { META_ACTION } from '../types/metaTypes';
import { META_ACTION } from '../types/messageObjectTypes';
import { broadcastRaisedHand } from './broadcastRaisedHand';
import { onReceiveMetaMessage } from './onReceiveMetaMessage';
import { onJoinListener } from './onJoinListener';
Expand Down
12 changes: 7 additions & 5 deletions packages/restapi/src/lib/space/acceptPromotionRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
SPACE_ACCEPT_REQUEST_TYPE,
SPACE_INVITE_ROLES,
} from '../payloads/constants';
import { META_ACTION } from '../types/metaTypes';
import { META_ACTION } from '../types/messageObjectTypes';
import { AdminPeer } from '../types';

export interface IAcceptPromotionRequestType {
Expand Down Expand Up @@ -38,7 +38,7 @@ export async function acceptPromotionRequest(
signer: this.signer,
pgpPrivateKey: this.pgpPrivateKey,
speakers: [pCAIP10ToWallet(promoteeAddress)],
env: this.env
env: this.env,
});

// get old live space data
Expand All @@ -51,15 +51,17 @@ export async function acceptPromotionRequest(

// update the metamessage
const updatedLiveSpaceData = produce(oldLiveSpaceData, (draft) => {
const listnerIndex = draft.listeners.findIndex((listner) => listner.address === pCAIP10ToWallet(promoteeAddress));
if (listnerIndex > -1) draft.listeners[listnerIndex].handRaised = false;
const listnerIndex = draft.listeners.findIndex(
(listner) => listner.address === pCAIP10ToWallet(promoteeAddress)
);
if (listnerIndex > -1) draft.listeners[listnerIndex].handRaised = false;

// convert listener to speaker type (ListenerPeer -> AdminPeer)
const promotedListener: AdminPeer = {
address: draft.listeners[listnerIndex].address,
emojiReactions: draft.listeners[listnerIndex].emojiReactions,
audio: true,
}
};

// remove listener from speaker array
draft.listeners.splice(listnerIndex, 1);
Expand Down
2 changes: 1 addition & 1 deletion packages/restapi/src/lib/space/broadcastRaisedHand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { produce } from 'immer';
import type Space from './Space';
import getLiveSpaceData from './helpers/getLiveSpaceData';
import sendLiveSpaceData from './helpers/sendLiveSpaceData';
import { META_ACTION } from '../types/metaTypes';
import { META_ACTION } from '../types/messageObjectTypes';
import { pCAIP10ToWallet } from '../helpers';

export interface BroadcastRaisedHandType {
Expand Down
3 changes: 2 additions & 1 deletion packages/restapi/src/lib/space/helpers/getLiveSpaceData.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { conversationHash, history } from '../../chat';
import { MessageType } from '../../constants';
import { EnvOptionsType, LiveSpaceData } from '../../types';
import { META_MESSAGE_OBJECT } from '../../types/messageObjectTypes';
import { initLiveSpaceData } from '../Space';

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

Expand Down
12 changes: 5 additions & 7 deletions packages/restapi/src/lib/space/helpers/sendLiveSpaceData.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { send } from '../../chat';
import { MessageType } from '../../constants';
import { EnvOptionsType, LiveSpaceData, SignerType } from '../../types';
import { META_ACTION } from '../../types/metaTypes';
import { META_ACTION } from '../../types/messageObjectTypes';

interface SendLiveSpaceData extends EnvOptionsType {
liveSpaceData?: LiveSpaceData;
Expand All @@ -27,12 +27,10 @@ const sendLiveSpaceData = async ({
messageType: MessageType.META,
messageObj: {
content: 'PUSH SPACE META MESSAGE',
meta: {
action,
info: {
affected: [],
arbitrary: liveSpaceData,
},
action,
info: {
affected: [],
arbitrary: liveSpaceData,
},
},
});
Expand Down
6 changes: 3 additions & 3 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 sendLiveSpaceData from './helpers/sendLiveSpaceData';
import { get } from './get';
import { pCAIP10ToWallet } from '../helpers';

import { META_ACTION } from '../types/metaTypes';
import { META_ACTION } from '../types/messageObjectTypes';
import type Space from './Space';

export interface OnJoinListenerType {
Expand Down Expand Up @@ -62,8 +62,8 @@ export async function onJoinListener(this: Space, options: OnJoinListenerType) {
fetchedLiveSpaceData.listeners.push({
address: pCAIP10ToWallet(receivedAddress),
handRaised: false,
emojiReactions: null
})
emojiReactions: null,
});
// firing a meta message
await sendLiveSpaceData({
spaceId: this.spaceSpecificData.spaceId,
Expand Down
8 changes: 5 additions & 3 deletions packages/restapi/src/lib/space/onReceiveMetaMessage.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { MessageType } from '../constants';
import { IMessageIPFS, LiveSpaceData } from '../types';
import { META_MESSAGE_OBJECT } from '../types/messageObjectTypes';
import type Space from './Space';

export interface OnReceiveMetaMessageType {
Expand All @@ -17,13 +18,14 @@ export function onReceiveMetaMessage(
if (
receivedMetaMessage.messageType !== MessageType.META ||
typeof receivedMetaMessage.messageObj !== 'object' ||
!receivedMetaMessage?.messageObj?.meta?.info?.arbitrary
!(receivedMetaMessage?.messageObj as META_MESSAGE_OBJECT)?.info?.arbitrary
) {
return;
}

const receivedLiveSpaceData = receivedMetaMessage.messageObj.meta.info
.arbitrary as LiveSpaceData;
const receivedLiveSpaceData = (
receivedMetaMessage.messageObj as META_MESSAGE_OBJECT
).info.arbitrary as LiveSpaceData;

console.log('RECEIVED LIVE SPACE DATA', receivedLiveSpaceData);

Expand Down
2 changes: 1 addition & 1 deletion packages/restapi/src/lib/space/rejectPromotionRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { produce } from 'immer';
import type Space from './Space';
import getLiveSpaceData from './helpers/getLiveSpaceData';
import sendLiveSpaceData from './helpers/sendLiveSpaceData';
import { META_ACTION } from '../types/metaTypes';
import { META_ACTION } from '../types/messageObjectTypes';
import { pCAIP10ToWallet } from '../helpers';

export interface RejectPromotionRequestType {
Expand Down
2 changes: 1 addition & 1 deletion packages/restapi/src/lib/space/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export interface StartSpaceType extends EnvOptionsType {
import type Space from './Space';
import { produce } from 'immer';
import { pCAIP10ToWallet } from '../helpers';
import { META_ACTION } from '../types/metaTypes';
import { META_ACTION } from '../types/messageObjectTypes';
import sendLiveSpaceData from './helpers/sendLiveSpaceData';

type StartType = {
Expand Down
26 changes: 5 additions & 21 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, MessageTypeSpecificMeta } from './metaTypes';
import { MessageTypeSpecificObject } from './messageObjectTypes';

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

Expand Down Expand Up @@ -162,12 +162,7 @@ export interface IMessageIPFS {
fromDID: string;
toDID: string;
messageType: string;
messageObj?:
| {
content: string;
meta?: META_MESSAGE_META;
}
| string;
messageObj?: MessageTypeSpecificObject[MessageType] | string;
/**
* @deprecated - Use messageObj.content instead
*/
Expand Down Expand Up @@ -467,10 +462,7 @@ export interface AccountEnvOptionsType extends EnvOptionsType {

export interface ChatStartOptionsType {
messageType: `${MessageType}`;
messageObj: {
content: string;
meta?: META_MESSAGE_META;
};
messageObj: MessageTypeSpecificObject[MessageType];
/**
* @deprecated - To be used for now to provide backward compatibility
*/
Expand All @@ -485,10 +477,7 @@ export interface ChatStartOptionsType {
*/
export interface ChatSendOptionsType {
messageType?: `${MessageType}`;
messageObj?: {
content: string;
meta?: MessageTypeSpecificMeta[MessageType];
};
messageObj?: MessageTypeSpecificObject[MessageType];
/**
* @deprecated - Use messageObj.content instead
*/
Expand Down Expand Up @@ -596,12 +585,7 @@ export type MessageWithCID = {
fromDID: string;
toDID: string;
messageType: string;
messageObj?:
| {
content: string;
meta?: META_MESSAGE_META;
}
| string;
messageObj?: MessageTypeSpecificObject[MessageType] | string;
/**
* @deprecated - Use messageObj.content instead
*/
Expand Down
Loading
Loading