Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
SupertigerDev committed Oct 19, 2024
2 parents db56b63 + 2e71a9f commit 3e775e7
Show file tree
Hide file tree
Showing 20 changed files with 875 additions and 526 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
29 changes: 18 additions & 11 deletions src/chat-api/events/voiceEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,22 @@ 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 {user} = useAccount();
export function onVoiceUserLeft(payload: {
userId: string;
channelId: string;
}) {
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 @@ -27,12 +30,16 @@ interface VoiceSignalReceivedPayload {

export function onVoiceSignalReceived(payload: VoiceSignalReceivedPayload) {
const voiceUsers = useVoiceUsers();
const voiceUser = voiceUsers.getVoiceUser(payload.channelId, payload.fromUserId);

const voiceUser = voiceUsers.getVoiceUser(
payload.channelId,
payload.fromUserId
);
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 3e775e7

Please sign in to comment.