Skip to content

Commit

Permalink
refactor(#141): [wip] centralize room actions
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyckahn committed Nov 23, 2024
1 parent 8df85b5 commit 2ad538d
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 74 deletions.
12 changes: 3 additions & 9 deletions src/components/Room/usePeerVerification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand All @@ -23,16 +23,10 @@ export const usePeerVerification = ({
const { getDisplayUsername } = usePeerNameDisplay()

const [sendVerificationTokenEncrypted, receiveVerificationTokenEncrypted] =
peerRoom.makeAction<ArrayBuffer>(
PeerAction.VERIFICATION_TOKEN_ENCRYPTED,
groupActionNamespace
)
peerRoom.makeAction<ArrayBuffer>(PeerAction.VERIFICATION_TOKEN_ENCRYPTED)

const [sendVerificationTokenRaw, receiveVerificationTokenRaw] =
peerRoom.makeAction<string>(
PeerAction.VERIFICATION_TOKEN_RAW,
groupActionNamespace
)
peerRoom.makeAction<string>(PeerAction.VERIFICATION_TOKEN_RAW)

const initPeerVerification = useCallback(
async (peer: Peer) => {
Expand Down
56 changes: 13 additions & 43 deletions src/components/Room/useRoom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -51,12 +43,6 @@ interface UseRoomConfig {
targetPeerId?: string | null
}

interface UserMetadata extends Record<string, any> {
userId: string
customUsername: string
publicKeyString: string
}

export function useRoom(
{ password, ...roomConfig }: BaseRoomConfig & RelayConfig,
{
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -188,16 +187,6 @@ export function useRoom(
]
)

const peerActionNamespace = isDirectMessageRoom
? directMessageActionNamespace
: groupActionNamespace

const [sendTypingStatusChange, receiveTypingStatusChange] =
peerRoom.makeAction<TypingStatus>(
PeerAction.TYPING_STATUS_CHANGE,
peerActionNamespace
)

const [isTyping, setIsTypingDebounced, setIsTyping] = useDebounce(
false,
2000,
Expand Down Expand Up @@ -250,25 +239,6 @@ export function useRoom(
if (isShowingMessages) setUnreadMessages(0)
}, [isShowingMessages, setUnreadMessages])

const [sendPeerMetadata, receivePeerMetadata] =
peerRoom.makeAction<UserMetadata>(
PeerAction.PEER_METADATA,
peerActionNamespace
)

const [sendMessageTranscript, receiveMessageTranscript] = peerRoom.makeAction<
Array<ReceivedMessage | ReceivedInlineMedia>
>(PeerAction.MESSAGE_TRANSCRIPT, peerActionNamespace)

const [sendPeerMessage, receivePeerMessage] =
peerRoom.makeAction<UnsentMessage>(PeerAction.MESSAGE, peerActionNamespace)

const [sendPeerInlineMedia, receivePeerInlineMedia] =
peerRoom.makeAction<UnsentInlineMedia>(
PeerAction.MEDIA_MESSAGE,
peerActionNamespace
)

const { privateKey } = settingsContext.getUserSettings()

const { verifyPeer } = usePeerVerification({
Expand Down
4 changes: 2 additions & 2 deletions src/components/Room/useRoomAudio.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -40,7 +40,7 @@ export function useRoomAudio({ peerRoom }: UseRoomAudioConfig) {

const [sendAudioChange, receiveAudioChange] = peerRoom.makeAction<
Partial<PeerAudioChannelState>
>(PeerAction.AUDIO_CHANGE, groupActionNamespace)
>(PeerAction.AUDIO_CHANGE)

receiveAudioChange((peerAudioChannelState, peerId) => {
setPeerList(peerList => {
Expand Down
7 changes: 2 additions & 5 deletions src/components/Room/useRoomFileShare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -33,10 +33,7 @@ export function useRoomFileShare({
const { peerOfferedFileMetadata, setPeerOfferedFileMetadata } = roomContext

const [sendFileOfferMetadata, receiveFileOfferMetadata] =
peerRoom.makeAction<FileOfferMetadata | null>(
PeerAction.FILE_OFFER,
groupActionNamespace
)
peerRoom.makeAction<FileOfferMetadata | null>(PeerAction.FILE_OFFER)

receiveFileOfferMetadata((fileOfferMetadata, peerId) => {
if (fileOfferMetadata) {
Expand Down
7 changes: 2 additions & 5 deletions src/components/Room/useRoomScreenShare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -38,10 +38,7 @@ export function useRoomScreenShare({ peerRoom }: UseRoomScreenShareConfig) {
} = roomContext

const [sendScreenShare, receiveScreenShare] =
peerRoom.makeAction<ScreenShareState>(
PeerAction.SCREEN_SHARE,
groupActionNamespace
)
peerRoom.makeAction<ScreenShareState>(PeerAction.SCREEN_SHARE)

receiveScreenShare((screenState, peerId) => {
const newPeerList = peerList.map(peer => {
Expand Down
5 changes: 2 additions & 3 deletions src/components/Room/useRoomVideo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -69,8 +69,7 @@ export function useRoomVideo({ peerRoom }: UseRoomVideoConfig) {
}, [peerRoom, selfVideoStream, setSelfVideoStream])

const [sendVideoChange, receiveVideoChange] = peerRoom.makeAction<VideoState>(
PeerAction.VIDEO_CHANGE,
groupActionNamespace
PeerAction.VIDEO_CHANGE
)

receiveVideoChange((videoState, peerId) => {
Expand Down
58 changes: 51 additions & 7 deletions src/lib/PeerRoom/PeerRoom.ts
Original file line number Diff line number Diff line change
@@ -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<string, any> {
userId: string
customUsername: string
publicKeyString: string
}

export enum PeerHookType {
NEW_PEER = 'NEW_PEER',
AUDIO = 'AUDIO',
Expand Down Expand Up @@ -62,6 +82,21 @@ export class PeerRoom {
this.isProcessingPendingStreams = false
}

sendPeerMetadata: ActionSender<UserMetadata>
receivePeerMetadata: ActionReceiver<UserMetadata>
sendMessageTranscript: ActionSender<
Array<ReceivedMessage | ReceivedInlineMedia>
>
receiveMessageTranscript: ActionReceiver<
Array<ReceivedMessage | ReceivedInlineMedia>
>
sendPeerMessage: ActionSender<UnsentMessage>
receivePeerMessage: ActionReceiver<UnsentMessage>
sendPeerInlineMedia: ActionSender<UnsentInlineMedia>
receivePeerInlineMedia: ActionReceiver<UnsentInlineMedia>
sendTypingStatusChange: ActionSender<TypingStatus>
receiveTypingStatusChange: ActionReceiver<TypingStatus>

constructor(config: RelayConfig & BaseRoomConfig, roomId: string) {
this.roomConfig = config
this.room = joinRoom(this.roomConfig, roomId)
Expand All @@ -83,6 +118,18 @@ export class PeerRoom {
peerStreamHandler(...args)
}
})
;[this.sendPeerMetadata, this.receivePeerMetadata] =
this.makeAction<UserMetadata>(PeerAction.PEER_METADATA)
;[this.sendMessageTranscript, this.receiveMessageTranscript] =
this.makeAction<Array<ReceivedMessage | ReceivedInlineMedia>>(
PeerAction.MESSAGE_TRANSCRIPT
)
;[this.sendPeerMessage, this.receivePeerMessage] =
this.makeAction<UnsentMessage>(PeerAction.MESSAGE)
;[this.sendPeerInlineMedia, this.receivePeerInlineMedia] =
this.makeAction<UnsentInlineMedia>(PeerAction.MEDIA_MESSAGE)
;[this.sendTypingStatusChange, this.receiveTypingStatusChange] =
this.makeAction<TypingStatus>(PeerAction.TYPING_STATUS_CHANGE)
}

flush = () => {
Expand Down Expand Up @@ -169,11 +216,8 @@ export class PeerRoom {
return peerConnections
}

makeAction = <T extends DataPayload>(
peerAction: PeerAction,
namespace: string
) => {
return this.room.makeAction<T>(`${namespace}.${peerAction}`)
makeAction = <T extends DataPayload>(peerAction: PeerAction) => {
return this.room.makeAction<T>(`${peerAction}`)
}

addStream = (
Expand Down

0 comments on commit 2ad538d

Please sign in to comment.