Skip to content

Commit

Permalink
reimplemented vc logic from scratch, Should hopefully be more stable 🤞
Browse files Browse the repository at this point in the history
  • Loading branch information
SupertigerDev committed Oct 17, 2024
1 parent 1fe195c commit 9292c7f
Show file tree
Hide file tree
Showing 13 changed files with 453 additions and 465 deletions.
4 changes: 2 additions & 2 deletions src/chat-api/events/connectionEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,8 @@ export const onAuthenticated = (payload: AuthenticatedPayload) => {
}

for (let i = 0; i < payload.voiceChannelUsers.length; i++) {
const voiceChannelUser = payload.voiceChannelUsers[i];
voiceUsers.set(voiceChannelUser);
const voiceChannelUser = payload.voiceChannelUsers[i]!;
voiceUsers.createVoiceUser(voiceChannelUser);
}
});

Expand Down
8 changes: 4 additions & 4 deletions src/chat-api/events/serverEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ export const onServerJoined = (payload: ServerJoinedPayload) => {
users.setPresence(presence.userId, presence);
}
for (let i = 0; i < payload.voiceChannelUsers.length; i++) {
const rawVoice = payload.voiceChannelUsers[i];
voiceUsers.set(rawVoice);
const rawVoice = payload.voiceChannelUsers[i]!;
voiceUsers.createVoiceUser(rawVoice);
}
});
};
Expand All @@ -75,7 +75,7 @@ export const onServerLeft = (payload: { serverId: string }) =>
const roles = useServerRoles();
const voiceUsers = useVoiceUsers();

const currentVoiceChannelId = voiceUsers.currentVoiceChannelId();
const currentVoiceChannelId = voiceUsers.currentUser()?.channelId;

const serverChannels = channels.getChannelsByServerId(payload.serverId);

Expand All @@ -90,7 +90,7 @@ export const onServerLeft = (payload: { serverId: string }) =>
const channel = serverChannels[i]!;
account.removeNotificationSettings(channel.id);
if (currentVoiceChannelId === channel.id) {
voiceUsers.setCurrentVoiceChannelId(null);
voiceUsers.setCurrentChannelId(null);
}
}
});
Expand Down
14 changes: 7 additions & 7 deletions src/chat-api/events/voiceEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ import useAccount from "../store/useAccount";
import useVoiceUsers from "../store/useVoiceUsers";

export function onVoiceUserJoined(payload: RawVoice) {
const {set} = useVoiceUsers();
const {createVoiceUser} = useVoiceUsers();

set(payload);
createVoiceUser(payload);
}
export function onVoiceUserLeft(payload: {userId: string, channelId: string}) {
const {removeUserInVoice, setCurrentVoiceChannelId} = useVoiceUsers();
const {removeVoiceUser, setCurrentChannelId} = useVoiceUsers();
const {user} = useAccount();

if (user()?.id === payload.userId) {
setCurrentVoiceChannelId(null);
setCurrentChannelId(null);
}

removeUserInVoice(payload.channelId, payload.userId);
removeVoiceUser(payload.channelId, payload.userId);
}

interface VoiceSignalReceivedPayload {
Expand All @@ -31,8 +31,8 @@ export function onVoiceSignalReceived(payload: VoiceSignalReceivedPayload) {
if (!voiceUser) return;

if (!voiceUser.peer) {
return voiceUser.addPeer(payload.signal);
return voiceUsers.createPeer(voiceUser, payload.signal);
}

voiceUser.addSignal(payload.signal);
voiceUsers.signal(voiceUser, payload.signal);
}
3 changes: 3 additions & 0 deletions src/chat-api/services/VoiceService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ const lastCredentials = {
generatedAt: null as null | number,
result: null as null | any
};


export const getCachedCredentials = () => lastCredentials.result;
export const postGenerateCredential = async () => {
if (lastCredentials.generatedAt) {
const diff = Date.now() - lastCredentials.generatedAt;
Expand Down
6 changes: 5 additions & 1 deletion src/chat-api/store/useAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ const updateUserNotificationSettings = (opts: {serverId?: string, channelId?: st
};


const isMe = (userId: string) => account.user && account.user.id === userId;


export default function useAccount() {
return {
user,
Expand All @@ -129,6 +132,7 @@ export default function useAccount() {
updateUserNotificationSettings,
removeNotificationSettings,
hasModeratorPerm,
lastAuthenticatedAt
lastAuthenticatedAt,
isMe
};
}
13 changes: 7 additions & 6 deletions src/chat-api/store/useChannels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,18 @@ function recipient(this: Channel) {
return users.get(this.recipientId!);
}


async function joinCall(this: Channel) {
const { setCurrentVoiceChannelId } = useVoiceUsers();
const { setCurrentChannelId } = useVoiceUsers();
await postGenerateCredential();
postJoinVoice(this.id, socketClient.id()!).then(() => {
setCurrentVoiceChannelId(this.id);
setCurrentChannelId(this.id);
});
}
function leaveCall(this: Channel) {
const { setCurrentVoiceChannelId } = useVoiceUsers();
const { setCurrentChannelId } = useVoiceUsers();
postLeaveVoice(this.id).then(() => {
setCurrentVoiceChannelId(null);
setCurrentChannelId(null);
});
}
function update(this: Channel, update: Partial<RawChannel>) {
Expand Down Expand Up @@ -154,7 +155,7 @@ const deleteChannel = (channelId: string, serverId?: string) =>
runWithContext(() => {
const messages = useMessages();
const voice = useVoiceUsers();
const voiceChannelId = voice.currentVoiceChannelId();
const voiceChannelId = voice.currentUser();

if (serverId) {
const servers = useServers();
Expand All @@ -172,7 +173,7 @@ const deleteChannel = (channelId: string, serverId?: string) =>

batch(() => {
if (voiceChannelId && voiceChannelId === channelId) {
voice.setCurrentVoiceChannelId(null);
voice.setCurrentChannelId(null);
}

messages.deleteChannelMessages(channelId);
Expand Down
2 changes: 1 addition & 1 deletion src/chat-api/store/useServerMembers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ const remove = (serverId: string, userId: string) => {
if (voiceChannelId) {
const channel = channels.get(voiceChannelId);
if (serverId === channel?.serverId) {
voiceUsers.removeUserInVoice(voiceChannelId, userId);
voiceUsers.removeVoiceUser(voiceChannelId, userId);
}
}

Expand Down
Loading

0 comments on commit 9292c7f

Please sign in to comment.