Skip to content

Commit

Permalink
refactor: added start function implementation (#709)
Browse files Browse the repository at this point in the history
* refactor: added start function implementation

* refactor: added start method in class

* refactor: added comment for start function
  • Loading branch information
0xNilesh authored Sep 15, 2023
1 parent 76dab91 commit e477955
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/restapi/src/lib/spaceV2/SpaceV2.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { produce } from "immer";

import { join } from "./join";
import { start } from "./start";
import { connect } from "./connect";
import { acceptInvite } from "./acceptInvite";
import { ISpaceInviteInputOptions, inviteToJoin } from "./inviteToJoin";
Expand Down Expand Up @@ -190,4 +191,6 @@ export class SpaceV2 {
public join = join;

public acceptInvite = acceptInvite;

public start = start;
}
81 changes: 81 additions & 0 deletions packages/restapi/src/lib/spaceV2/start.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/**
* start.ts
*
* The 'start' function is responsible for starting the space and updating that in backend
*
*/
import { produce } from 'immer';

import { get } from './get';
import { SpaceV2 } from './SpaceV2';

import { pCAIP10ToWallet } from '../helpers';
import { ChatStatus } from '../types';

// chat imports
import { updateGroup } from '../chat/updateGroup';
import {
groupDtoToSpaceDto,
getSpacesMembersList,
getSpaceAdminsList,
} from './../chat/helpers';

// ToDo: Add a liveSpaceData in data as well, and figure out on how to broadcast it and populate it on a new node's side as well
export async function start(this: SpaceV2): Promise<void> {
try {
// host should have there audio stream
if (!this.data.local.stream) {
throw new Error('Local audio stream not found');
}

const space = await get({
spaceId: this.data.spaceInfo.spaceId,
env: this.env,
});

if (space.status !== ChatStatus.PENDING) {
throw new Error(
'Unable to start the space as it is not in the pending state'
);
}

// Only host is allowed to start a space
if (this.data.local.address !== pCAIP10ToWallet(space.spaceCreator)) {
throw new Error('Only host is allowed to start a space');
}

const convertedMembers = getSpacesMembersList(
space.members,
space.pendingMembers
);
const convertedAdmins = getSpaceAdminsList(
space.members,
space.pendingMembers
);

const group = await updateGroup({
chatId: this.data.spaceInfo.spaceId,
groupName: space.spaceName,
groupImage: space.spaceImage,
groupDescription: space.spaceDescription,
members: convertedMembers,
admins: convertedAdmins,
signer: this.signer,
env: this.env,
pgpPrivateKey: this.pgpPrivateKey,
scheduleAt: space.scheduleAt,
scheduleEnd: space.scheduleEnd,
status: ChatStatus.ACTIVE,
});

// update space data
this.setSpaceV2Data((oldSpaceData) => {
return produce(oldSpaceData, (draft) => {
draft.spaceInfo = groupDtoToSpaceDto(group);
});
});
} catch (err) {
console.error(`[Push SDK] - API - Error - API ${start.name} -: `, err);
throw Error(`[Push SDK] - API - Error - API ${start.name} -: ${err}`);
}
}

0 comments on commit e477955

Please sign in to comment.