diff --git a/src/components/Room/usePeerVerification.ts b/src/components/Room/usePeerVerification.ts index c4cd43ccc..8818c960f 100644 --- a/src/components/Room/usePeerVerification.ts +++ b/src/components/Room/usePeerVerification.ts @@ -3,7 +3,7 @@ import { ShellContext } from 'contexts/ShellContext' import { Peer, PeerVerificationState } from 'models/chat' import { encryption } from 'services/Encryption' import { PeerRoom } from 'lib/PeerRoom' -import { groupActionNamespace, PeerAction } from 'models/network' +import { PeerAction } from 'models/network' import { verificationTimeout } from 'config/messaging' import { usePeerNameDisplay } from 'components/PeerNameDisplay' @@ -23,16 +23,10 @@ export const usePeerVerification = ({ const { getDisplayUsername } = usePeerNameDisplay() const [sendVerificationTokenEncrypted, receiveVerificationTokenEncrypted] = - peerRoom.makeAction( - PeerAction.VERIFICATION_TOKEN_ENCRYPTED, - groupActionNamespace - ) + peerRoom.makeAction(PeerAction.VERIFICATION_TOKEN_ENCRYPTED) const [sendVerificationTokenRaw, receiveVerificationTokenRaw] = - peerRoom.makeAction( - PeerAction.VERIFICATION_TOKEN_RAW, - groupActionNamespace - ) + peerRoom.makeAction(PeerAction.VERIFICATION_TOKEN_RAW) const initPeerVerification = useCallback( async (peer: Peer) => { diff --git a/src/components/Room/useRoom.ts b/src/components/Room/useRoom.ts index 563229e81..90d5f036d 100644 --- a/src/components/Room/useRoom.ts +++ b/src/components/Room/useRoom.ts @@ -6,25 +6,17 @@ import { useDebounce } from '@react-hook/debounce' import { ShellContext } from 'contexts/ShellContext' import { SettingsContext } from 'contexts/SettingsContext' -import { - directMessageActionNamespace, - groupActionNamespace, - PeerAction, -} from 'models/network' import { AudioState, Message, - ReceivedMessage, UnsentMessage, InlineMedia, - ReceivedInlineMedia, UnsentInlineMedia, VideoState, ScreenShareState, isMessageReceived, isInlineMedia, FileOfferMetadata, - TypingStatus, Peer, PeerVerificationState, AudioChannelName, @@ -51,12 +43,6 @@ interface UseRoomConfig { targetPeerId?: string | null } -interface UserMetadata extends Record { - userId: string - customUsername: string - publicKeyString: string -} - export function useRoom( { password, ...roomConfig }: BaseRoomConfig & RelayConfig, { @@ -100,6 +86,19 @@ export function useRoom( peerRoomRef.current = peerRoom + const { + sendPeerMetadata, + receivePeerMetadata, + sendMessageTranscript, + receiveMessageTranscript, + sendPeerMessage, + receivePeerMessage, + sendPeerInlineMedia, + receivePeerInlineMedia, + sendTypingStatusChange, + receiveTypingStatusChange, + } = peerRoom + const settingsContext = useContext(SettingsContext) const { showActiveTypingStatus } = settingsContext.getUserSettings() const [isMessageSending, setIsMessageSending] = useState(false) @@ -188,16 +187,6 @@ export function useRoom( ] ) - const peerActionNamespace = isDirectMessageRoom - ? directMessageActionNamespace - : groupActionNamespace - - const [sendTypingStatusChange, receiveTypingStatusChange] = - peerRoom.makeAction( - PeerAction.TYPING_STATUS_CHANGE, - peerActionNamespace - ) - const [isTyping, setIsTypingDebounced, setIsTyping] = useDebounce( false, 2000, @@ -250,25 +239,6 @@ export function useRoom( if (isShowingMessages) setUnreadMessages(0) }, [isShowingMessages, setUnreadMessages]) - const [sendPeerMetadata, receivePeerMetadata] = - peerRoom.makeAction( - PeerAction.PEER_METADATA, - peerActionNamespace - ) - - const [sendMessageTranscript, receiveMessageTranscript] = peerRoom.makeAction< - Array - >(PeerAction.MESSAGE_TRANSCRIPT, peerActionNamespace) - - const [sendPeerMessage, receivePeerMessage] = - peerRoom.makeAction(PeerAction.MESSAGE, peerActionNamespace) - - const [sendPeerInlineMedia, receivePeerInlineMedia] = - peerRoom.makeAction( - PeerAction.MEDIA_MESSAGE, - peerActionNamespace - ) - const { privateKey } = settingsContext.getUserSettings() const { verifyPeer } = usePeerVerification({ diff --git a/src/components/Room/useRoomAudio.ts b/src/components/Room/useRoomAudio.ts index 0c536012f..638ae9693 100644 --- a/src/components/Room/useRoomAudio.ts +++ b/src/components/Room/useRoomAudio.ts @@ -1,7 +1,7 @@ import { useContext, useEffect, useCallback, useState } from 'react' import { ShellContext } from 'contexts/ShellContext' -import { groupActionNamespace, PeerAction } from 'models/network' +import { PeerAction } from 'models/network' import { AudioState, Peer, @@ -40,7 +40,7 @@ export function useRoomAudio({ peerRoom }: UseRoomAudioConfig) { const [sendAudioChange, receiveAudioChange] = peerRoom.makeAction< Partial - >(PeerAction.AUDIO_CHANGE, groupActionNamespace) + >(PeerAction.AUDIO_CHANGE) receiveAudioChange((peerAudioChannelState, peerId) => { setPeerList(peerList => { diff --git a/src/components/Room/useRoomFileShare.ts b/src/components/Room/useRoomFileShare.ts index 91842a708..3b04399e9 100644 --- a/src/components/Room/useRoomFileShare.ts +++ b/src/components/Room/useRoomFileShare.ts @@ -3,7 +3,7 @@ import { useContext, useEffect, useState } from 'react' import { sleep } from 'lib/sleep' import { RoomContext } from 'contexts/RoomContext' import { ShellContext } from 'contexts/ShellContext' -import { groupActionNamespace, PeerAction } from 'models/network' +import { PeerAction } from 'models/network' import { FileOfferMetadata, Peer } from 'models/chat' import { PeerRoom, PeerHookType } from 'lib/PeerRoom' import { fileTransfer } from 'lib/FileTransfer' @@ -33,10 +33,7 @@ export function useRoomFileShare({ const { peerOfferedFileMetadata, setPeerOfferedFileMetadata } = roomContext const [sendFileOfferMetadata, receiveFileOfferMetadata] = - peerRoom.makeAction( - PeerAction.FILE_OFFER, - groupActionNamespace - ) + peerRoom.makeAction(PeerAction.FILE_OFFER) receiveFileOfferMetadata((fileOfferMetadata, peerId) => { if (fileOfferMetadata) { diff --git a/src/components/Room/useRoomScreenShare.ts b/src/components/Room/useRoomScreenShare.ts index 8966476f5..d6411ee42 100644 --- a/src/components/Room/useRoomScreenShare.ts +++ b/src/components/Room/useRoomScreenShare.ts @@ -3,7 +3,7 @@ import { useContext, useEffect, useCallback, useState } from 'react' import { isRecord } from 'lib/type-guards' import { RoomContext } from 'contexts/RoomContext' import { ShellContext } from 'contexts/ShellContext' -import { groupActionNamespace, PeerAction } from 'models/network' +import { PeerAction } from 'models/network' import { ScreenShareState, Peer, @@ -38,10 +38,7 @@ export function useRoomScreenShare({ peerRoom }: UseRoomScreenShareConfig) { } = roomContext const [sendScreenShare, receiveScreenShare] = - peerRoom.makeAction( - PeerAction.SCREEN_SHARE, - groupActionNamespace - ) + peerRoom.makeAction(PeerAction.SCREEN_SHARE) receiveScreenShare((screenState, peerId) => { const newPeerList = peerList.map(peer => { diff --git a/src/components/Room/useRoomVideo.ts b/src/components/Room/useRoomVideo.ts index a5f54b6dd..9736b3b24 100644 --- a/src/components/Room/useRoomVideo.ts +++ b/src/components/Room/useRoomVideo.ts @@ -2,7 +2,7 @@ import { useContext, useEffect, useCallback, useState } from 'react' import { RoomContext } from 'contexts/RoomContext' import { ShellContext } from 'contexts/ShellContext' -import { groupActionNamespace, PeerAction } from 'models/network' +import { PeerAction } from 'models/network' import { VideoState, Peer, StreamType } from 'models/chat' import { PeerRoom, PeerHookType, PeerStreamType } from 'lib/PeerRoom' import { isRecord } from 'lib/type-guards' @@ -69,8 +69,7 @@ export function useRoomVideo({ peerRoom }: UseRoomVideoConfig) { }, [peerRoom, selfVideoStream, setSelfVideoStream]) const [sendVideoChange, receiveVideoChange] = peerRoom.makeAction( - PeerAction.VIDEO_CHANGE, - groupActionNamespace + PeerAction.VIDEO_CHANGE ) receiveVideoChange((videoState, peerId) => { diff --git a/src/lib/PeerRoom/PeerRoom.ts b/src/lib/PeerRoom/PeerRoom.ts index 367af0b0a..40058ebec 100644 --- a/src/lib/PeerRoom/PeerRoom.ts +++ b/src/lib/PeerRoom/PeerRoom.ts @@ -1,10 +1,30 @@ -import { joinRoom, Room, BaseRoomConfig, DataPayload } from 'trystero/torrent' +import { + joinRoom, + Room, + BaseRoomConfig, + DataPayload, + ActionSender, + ActionReceiver, +} from 'trystero/torrent' import { RelayConfig } from 'trystero/torrent' import { sleep } from 'lib/sleep' -import { StreamType } from 'models/chat' +import { + ReceivedInlineMedia, + ReceivedMessage, + StreamType, + TypingStatus, + UnsentInlineMedia, + UnsentMessage, +} from 'models/chat' import { PeerAction } from 'models/network' +interface UserMetadata extends Record { + userId: string + customUsername: string + publicKeyString: string +} + export enum PeerHookType { NEW_PEER = 'NEW_PEER', AUDIO = 'AUDIO', @@ -62,6 +82,21 @@ export class PeerRoom { this.isProcessingPendingStreams = false } + sendPeerMetadata: ActionSender + receivePeerMetadata: ActionReceiver + sendMessageTranscript: ActionSender< + Array + > + receiveMessageTranscript: ActionReceiver< + Array + > + sendPeerMessage: ActionSender + receivePeerMessage: ActionReceiver + sendPeerInlineMedia: ActionSender + receivePeerInlineMedia: ActionReceiver + sendTypingStatusChange: ActionSender + receiveTypingStatusChange: ActionReceiver + constructor(config: RelayConfig & BaseRoomConfig, roomId: string) { this.roomConfig = config this.room = joinRoom(this.roomConfig, roomId) @@ -83,6 +118,18 @@ export class PeerRoom { peerStreamHandler(...args) } }) + ;[this.sendPeerMetadata, this.receivePeerMetadata] = + this.makeAction(PeerAction.PEER_METADATA) + ;[this.sendMessageTranscript, this.receiveMessageTranscript] = + this.makeAction>( + PeerAction.MESSAGE_TRANSCRIPT + ) + ;[this.sendPeerMessage, this.receivePeerMessage] = + this.makeAction(PeerAction.MESSAGE) + ;[this.sendPeerInlineMedia, this.receivePeerInlineMedia] = + this.makeAction(PeerAction.MEDIA_MESSAGE) + ;[this.sendTypingStatusChange, this.receiveTypingStatusChange] = + this.makeAction(PeerAction.TYPING_STATUS_CHANGE) } flush = () => { @@ -169,11 +216,8 @@ export class PeerRoom { return peerConnections } - makeAction = ( - peerAction: PeerAction, - namespace: string - ) => { - return this.room.makeAction(`${namespace}.${peerAction}`) + makeAction = (peerAction: PeerAction) => { + return this.room.makeAction(`${peerAction}`) } addStream = (