Skip to content

Commit

Permalink
Add spaces high level audio methods (#993)
Browse files Browse the repository at this point in the history
* feat(spaces): add high level audio methods

* fix: socket url fix (#995)

* feat(spaces-highlevel-audio-methods): add media function in Spaces class

---------

Co-authored-by: Mohammed S <[email protected]>
  • Loading branch information
madhur-push and mohammeds1992 authored Jan 11, 2024
1 parent 2330355 commit c26e62e
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 3 deletions.
13 changes: 12 additions & 1 deletion packages/restapi/src/lib/pushapi/pushAPITypes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import Constants, { ENV } from '../constants';
import { ChatStatus, ProgressHookType, Rules, SpaceRules } from '../types';
import {
ChatStatus,
ProgressHookType,
Rules,
SpaceData,
SpaceRules,
} from '../types';

export enum ChatListType {
CHATS = 'CHATS',
Expand Down Expand Up @@ -118,3 +124,8 @@ export interface SpaceParticipantStatus {
role: 'SPEAKER' | 'LISTENER';
participant: boolean;
}

export interface SpaceInitializeOptions {
spaceId: string;
setSpaceData: (fn: (data: SpaceData) => SpaceData) => void;
}
51 changes: 50 additions & 1 deletion packages/restapi/src/lib/pushapi/space.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
ManageSpaceOptions,
RemoveFromSpaceOptions,
SpaceCreationOptions,
SpaceInitializeOptions,
SpaceListType,
SpaceParticipantStatus,
SpaceQueryOptions,
Expand All @@ -34,9 +35,16 @@ import {
updateGroupProfile,
} from '../chat/updateGroupProfile';
import { updateGroupConfig } from '../chat/updateGroupConfig';
import { groupInfoDtoToSpaceInfoDto, mapSpaceListTypeToChatListType } from '../chat';
import {
groupInfoDtoToSpaceInfoDto,
mapSpaceListTypeToChatListType,
} from '../chat';
import { isValidETHAddress } from '../helpers';
import { Chat } from './chat';
import { Signer as PushSigner } from '../helpers';

import { SpaceV2 } from '../space/SpaceV2';
import { Space as SpaceV1 } from '../space/Space';

export class Space {
private chatInstance: Chat;
Expand Down Expand Up @@ -512,4 +520,45 @@ export class Space {
},
};
}

async initialize(options: SpaceInitializeOptions): Promise<SpaceV2> {
const { setSpaceData, spaceId } = options;

if (!this.signer) {
throw new Error('Signer is required for push space');
}

if (!this.decryptedPgpPvtKey) {
throw new Error(
'PushSDK was initialized in readonly mode. Space functionality is not available.'
);
}

const chainId = await new PushSigner(this.signer).getChainId();

if (!chainId) {
throw new Error('Chain Id not retrievable from signer');
}

// Initialize the spacev1 instance with the provided options
const spaceV1Instance = new SpaceV1({
signer: this.signer!,
chainId,
pgpPrivateKey: this.decryptedPgpPvtKey!,
setSpaceData: setSpaceData,
address: this.account,
env: this.env,
});

// Call the space v1 initialize() method to populate the space data
await spaceV1Instance.initialize({ spaceId });

const spaceInfo = await this.info(spaceId);

// Return an instance of the space v2 class
return new SpaceV2({
spaceV1Instance,
spaceInfo,
});
}
}
6 changes: 5 additions & 1 deletion packages/restapi/src/lib/pushstream/socketClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ export function createSocketConnection({
reconnectionDelayMax,
} = socketOptions || {};

const pushWSUrl = API_BASE_URL[env];
let pushWSUrl = API_BASE_URL[env];

if (pushWSUrl.endsWith('/apis')) {
pushWSUrl = pushWSUrl.substring(0, pushWSUrl.length - 5);
}
const transports = ['websocket'];

let pushSocket = null;
Expand Down
103 changes: 103 additions & 0 deletions packages/restapi/src/lib/space/SpaceV2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import { SPACE_INVITE_ROLES } from '../payloads/constants';
import { SpaceInfoDTO } from '../types';
import Space from './Space';
import { ChatUpdateSpaceType } from './update';

export class SpaceV2 {
private spaceV1Instance: Space;
private spaceInfo: SpaceInfoDTO;

constructor({
spaceV1Instance,
spaceInfo,
}: {
spaceV1Instance: Space;
spaceInfo: SpaceInfoDTO;
}) {
this.spaceV1Instance = spaceV1Instance;
this.spaceInfo = spaceInfo;
}

async activateUserAudio() {
await this.spaceV1Instance.createAudioStream();
}

async start() {
await this.spaceV1Instance.start();
}

async join() {
await this.spaceV1Instance.join();
}

async update(updateSpaceOptions: ChatUpdateSpaceType) {
await this.spaceV1Instance.update(updateSpaceOptions);
}

async leave() {
await this.spaceV1Instance.leave();
}

async stop() {
await this.spaceV1Instance.stop();
}

async requestForMic() {
await this.spaceV1Instance.requestToBePromoted({
role: SPACE_INVITE_ROLES.SPEAKER,
promotorAddress: this.spaceInfo.spaceCreator,
});
}

async acceptMicRequest({
address,
signal,
}: {
address: string;
signal: any;
}) {
await this.spaceV1Instance.acceptPromotionRequest({
promoteeAddress: address,
spaceId: this.spaceInfo.spaceId,
role: SPACE_INVITE_ROLES.SPEAKER,
signalData: signal,
});
}

async rejectMicRequest({ address }: { address: string }) {
await this.spaceV1Instance.rejectPromotionRequest({
promoteeAddress: address,
});
}

async inviteToPromote({ address }: { address: string }) {
await this.spaceV1Instance.inviteToPromote({
inviteeAddress: address,
role: SPACE_INVITE_ROLES.SPEAKER,
});
}

async acceptPromotionInvite({ signal }: { signal: any }) {
await this.spaceV1Instance.acceptPromotionInvite({
invitorAddress: this.spaceInfo.spaceCreator,
spaceId: this.spaceInfo.spaceId,
signalData: signal,
});
}

async rejectPromotionInvite() {
await this.spaceV1Instance.rejectPromotionInvite({
invitorAddress: this.spaceInfo.spaceCreator,
});
}

media({ video, audio }: { video?: boolean; audio?: boolean }) {
if (typeof video === 'boolean') {
this.spaceV1Instance.enableVideo({ state: video });
}

if (typeof audio === 'boolean') {
this.spaceV1Instance.enableAudio({ state: audio });
}
}
}

0 comments on commit c26e62e

Please sign in to comment.