Skip to content

Commit

Permalink
fix: Merge branch 'group-scalability' into pagination_apis
Browse files Browse the repository at this point in the history
  • Loading branch information
mohammeds1992 committed Nov 5, 2023
2 parents 8774ca0 + 436475e commit bc4cd16
Show file tree
Hide file tree
Showing 5 changed files with 220 additions and 149 deletions.
4 changes: 2 additions & 2 deletions packages/restapi/src/lib/chat/createGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@ export const createGroupCore = async (
groupType,
scheduleAt,
scheduleEnd,
rules
rules,
} = options || {};

try {
if (account == null && signer == null) {
throw new Error(`At least one from account or signer is necessary!`);
}

validateScheduleDates(scheduleAt, scheduleEnd)
validateScheduleDates(scheduleAt, scheduleEnd);

const wallet = getWallet({ account, signer });

Expand Down
210 changes: 109 additions & 101 deletions packages/restapi/src/lib/chat/helpers/validator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import {
isValidETHAddress,
isValidNFTCAIP10Address,
} from '../../helpers';
import { isValidETHAddress, isValidNFTCAIP10Address } from '../../helpers';

export const createGroupRequestValidator = (
groupName: string,
Expand Down Expand Up @@ -71,132 +68,143 @@ export const createGroupRequestValidator = (
};

export const createSpaceRequestValidator = (
spaceName: string, spaceDescription: string | null,members: Array < string > , admins: Array < string > , contractAddressNFT ? : string,
numberOfNFTs ? : number,
contractAddressERC20 ? : string,
numberOfERC20 ? : number
spaceName: string,
spaceDescription: string | null,
members: Array<string>,
admins: Array<string>,
contractAddressNFT?: string,
numberOfNFTs?: number,
contractAddressERC20?: string,
numberOfERC20?: number
): void => {
if (spaceName == null || spaceName.length == 0) {
throw new Error(`spaceName cannot be null or empty`);
}

if (spaceName == null || spaceName.length == 0) {
throw new Error(`spaceName cannot be null or empty`);
}

if (spaceName.length > 50) {
throw new Error(`groupName cannot be more than 50 characters`);
}
if (spaceName.length > 50) {
throw new Error(`groupName cannot be more than 50 characters`);
}

if (spaceDescription && spaceDescription.length > 150) {
throw new Error(`spaceDescription cannot be more than 150 characters`);
}
if (spaceDescription && spaceDescription.length > 150) {
throw new Error(`spaceDescription cannot be more than 150 characters`);
}

if (members == null) {
throw new Error(`members cannot be null`);
}
if (members == null) {
throw new Error(`members cannot be null`);
}

for (let i = 0; i < members.length; i++) {
if (members[i] && !isValidETHAddress(members[i])) {
throw new Error(`Invalid member address!`);
}
for (let i = 0; i < members.length; i++) {
if (members[i] && !isValidETHAddress(members[i])) {
throw new Error(`Invalid member address!`);
}
}

if (admins == null) {
throw new Error(`admins cannot be null`);
}
if (admins == null) {
throw new Error(`admins cannot be null`);
}

for (let i = 0; i < admins.length; i++) {
if (!isValidETHAddress(admins[i])) {
throw new Error(`Invalid admin address!`);
}
for (let i = 0; i < admins.length; i++) {
if (!isValidETHAddress(admins[i])) {
throw new Error(`Invalid admin address!`);
}
}

if (contractAddressNFT != null && contractAddressNFT?.length > 0 && !isValidNFTCAIP10Address(contractAddressNFT)) {
throw new Error(`Invalid contractAddressNFT address!`);
}
if (
contractAddressNFT != null &&
contractAddressNFT?.length > 0 &&
!isValidNFTCAIP10Address(contractAddressNFT)
) {
throw new Error(`Invalid contractAddressNFT address!`);
}

if (numberOfNFTs != null && numberOfNFTs < 0) {
throw new Error(`numberOfNFTs cannot be negative number`);
}
if (numberOfNFTs != null && numberOfNFTs < 0) {
throw new Error(`numberOfNFTs cannot be negative number`);
}

if (contractAddressERC20 != null && contractAddressERC20?.length > 0 && !isValidNFTCAIP10Address(contractAddressERC20)) {
throw new Error(`Invalid contractAddressERC20 address!`);
}
if (
contractAddressERC20 != null &&
contractAddressERC20?.length > 0 &&
!isValidNFTCAIP10Address(contractAddressERC20)
) {
throw new Error(`Invalid contractAddressERC20 address!`);
}

if (numberOfERC20 != null && numberOfERC20 < 0) {
throw new Error(`numberOfERC20 cannot be negative number`);
}
if (numberOfERC20 != null && numberOfERC20 < 0) {
throw new Error(`numberOfERC20 cannot be negative number`);
}
};

export const validateScheduleDates = (scheduleAt?: Date | null,
scheduleEnd ? : Date | null): void => {

if (scheduleAt) {
const start = new Date(scheduleAt)
const now = new Date()
export const validateScheduleDates = (
scheduleAt?: Date | null,
scheduleEnd?: Date | null
): void => {
if (scheduleAt) {
const start = new Date(scheduleAt);
const now = new Date();

if (start < now) {
throw new Error('Schedule start time must be in the future.')
}
if (start < now) {
throw new Error('Schedule start time must be in the future.');
}

if (scheduleEnd) {
const end = new Date(scheduleEnd)
if (scheduleEnd) {
const end = new Date(scheduleEnd);

if (end < now) {
throw new Error('Schedule end time must be in the future.')
}
if (end < now) {
throw new Error('Schedule end time must be in the future.');
}

if (start >= end) {
throw new Error('Schedule start time must be earlier than end time.')
}
}
if (start >= end) {
throw new Error('Schedule start time must be earlier than end time.');
}
}
}
};

export const updateGroupRequestValidator = (
chatId: string,
groupName: string,
members: Array < string > ,
admins: Array < string > ,
address: string,
groupDescription?: string | null
chatId: string,
groupName: string,
members: Array<string>,
admins: Array<string>,
address: string,
groupDescription?: string | null
): void => {
if (chatId == null || chatId.length == 0) {
throw new Error(`chatId cannot be null or empty`);
}

if (groupName == null || groupName.length == 0) {
throw new Error(`groupName cannot be null or empty`);
}

if (chatId == null || chatId.length == 0) {
throw new Error(`chatId cannot be null or empty`);
}

if (groupName == null || groupName.length == 0) {
throw new Error(`groupName cannot be null or empty`);
}

if (groupName != null && groupName.length > 50) {
throw new Error(`groupName cannot be more than 50 characters`);
}
if (groupName != null && groupName.length > 50) {
throw new Error(`groupName cannot be more than 50 characters`);
}

if (
groupDescription && groupDescription != null &&
groupDescription.length > 150
) {
throw new Error(`groupDescription cannot be more than 150 characters`);
}
if (
groupDescription &&
groupDescription != null &&
groupDescription.length > 150
) {
throw new Error(`groupDescription cannot be more than 150 characters`);
}

if (members != null && members.length > 0) {
for (let i = 0; i < members.length; i++) {
if (!isValidETHAddress(members[i])) {
throw new Error(`Invalid member address in members list!`);
}
}
if (members != null && members.length > 0) {
for (let i = 0; i < members.length; i++) {
if (!isValidETHAddress(members[i])) {
throw new Error(`Invalid member address in members list!`);
}
}
}

if (admins != null && admins.length > 0) {
for (let i = 0; i < admins.length; i++) {
if (!isValidETHAddress(admins[i])) {
throw new Error(`Invalid member address in admins list!`);
}
}
if (admins != null && admins.length > 0) {
for (let i = 0; i < admins.length; i++) {
if (!isValidETHAddress(admins[i])) {
throw new Error(`Invalid member address in admins list!`);
}
}
}

if (address != null && !isValidETHAddress(address)) {
throw new Error(`Invalid address field!`);
}
};
if (address != null && !isValidETHAddress(address)) {
throw new Error(`Invalid address field!`);
}
};
79 changes: 36 additions & 43 deletions packages/restapi/src/lib/chat/updateGroupProfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,14 @@ import {
SignerType,
} from '../types';
import {
IUpdateGroupRequestPayload,
updateGroupPayload,
sign,
getWallet,
getAccountAddress,
getConnectedUserV2,
updateGroupRequestValidator,
getAdminsList,
getMembersList,
} from './helpers';

import { getGroup } from './getGroup';
import * as CryptoJS from 'crypto-js';
import { getGroup } from './getGroup';

export interface ChatUpdateGroupProfileType extends EnvOptionsType {
account?: string | null;
Expand All @@ -34,9 +29,6 @@ export interface ChatUpdateGroupProfileType extends EnvOptionsType {
scheduleAt?: Date | null;
scheduleEnd?: Date | null;
status?: ChatStatus | null;
// If meta is not passed, old meta is not affected
// If passed as null will update to null
// If passed as string will update to that value
meta?: string | null;
rules?: Rules | null;
}
Expand All @@ -63,6 +55,9 @@ export const updateGroupProfile = async (
rules,
} = options || {};
try {
/**
* VALIDATIONS
*/
if (account == null && signer == null) {
throw new Error(`At least one from account or signer is necessary!`);
}
Expand All @@ -78,54 +73,52 @@ export const updateGroupProfile = async (
groupDescription
);

const connectedUser = await getConnectedUserV2(wallet, pgpPrivateKey, env);

const group = await getGroup({
chatId: chatId,
env: env,
chatId,
env,
});

// TODO: look at user did in updateGroup
const convertedMembers = getMembersList(
group.members,
group.pendingMembers
);

const convertedAdmins = getAdminsList(group.members, group.pendingMembers);

/**
* CREATE PROFILE VERIFICATION PROOF
*/
const bodyToBeHashed = {
chatId: chatId,
groupName: groupName,
groupDescription: groupDescription,
groupImage: groupImage,
members: convertedMembers,
admins: convertedAdmins,
chatId: chatId,
meta: meta,
scheduleAt: scheduleAt,
scheduleEnd: scheduleEnd,
rules: rules,
status: status,
isPublic: group.isPublic,
groupType: group.groupType,
};

const hash = CryptoJS.SHA256(JSON.stringify(bodyToBeHashed)).toString();
const connectedUser = await getConnectedUserV2(wallet, pgpPrivateKey, env);
const signature: string = await sign({
message: hash,
signingKey: connectedUser.privateKey!,
});
const sigType = 'pgp';
const verificationProof: string = sigType + ':' + signature + ':' + account;
const sigType = 'pgpv2';
// Account is need to verify the signature at any future point
const verificationProof: string =
sigType + ':' + signature + ':' + connectedUser.did;

/**
* API CALL TO PUSH NODES
*/
const API_BASE_URL = getAPIBaseUrls(env);
const apiEndpoint = `${API_BASE_URL}/v1/chat/groups/${chatId}`;
const body: IUpdateGroupRequestPayload = updateGroupPayload(
groupName,
convertedMembers,
convertedAdmins,
connectedUser.did,
verificationProof,
null,
null,
groupDescription,
groupImage,
scheduleAt,
scheduleEnd,
status,
meta,
rules
);
const apiEndpoint = `${API_BASE_URL}/v1/chat/groups/${chatId}/profile`;

const {
chatId: chat_id,
isPublic: is_public,
groupType: group_type,
...body
} = bodyToBeHashed;
(body as any).verificationProof = verificationProof;

return axios
.put(apiEndpoint, body)
Expand Down
Loading

0 comments on commit bc4cd16

Please sign in to comment.