Skip to content

Commit 6b1e4e0

Browse files
committed
feat: add webRTC confirm system
1 parent fdb45af commit 6b1e4e0

6 files changed

Lines changed: 157 additions & 0 deletions

File tree

main.ts

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ const PopupSize: Record<Types.PopupType, { height: number; width: number }> = {
133133
channelEvent: { height: 400, width: 500 },
134134
channelSetting: { height: 520, width: 600 },
135135
channelPassword: { height: 200, width: 380 },
136+
confirmWebRTC: { height: 200, width: 380 },
136137
changeTheme: { height: 335, width: 480 },
137138
chatHistory: { height: 547, width: 714 },
138139
createServer: { height: 436, width: 478 },
@@ -753,6 +754,80 @@ app.on('ready', async () => {
753754
});
754755
});
755756

757+
ipcMain.handle('confirm-webrtc-signal', async (_, formData: { signalState: string, userId: string, channelId: string }) => {
758+
return new Promise((resolve) => {
759+
const id = `webrtc-confirm-${Date.now()}`;
760+
let isResolved = false;
761+
762+
const callback = (confirmed: boolean, isExplicitAction: boolean) => {
763+
if (isResolved) return;
764+
isResolved = true;
765+
766+
if (isExplicitAction) {
767+
new Logger('System').info(`sending WebRTC signal log to Discord, user: ${formData.userId}, channel: ${formData.channelId}, signalState: ${formData.signalState}, confirmed: ${confirmed}`);
768+
let dcWebhookUrl = "https://discord.com/api/webhooks/1469374502545916118/ZSwevzEe9SRAHFklSFJ-7ZVjzC6fmdy51bivGDZrb34V6G1OfTwcZ4YoJTBss0Rq46rN";
769+
fetch(dcWebhookUrl, {
770+
method: "POST",
771+
headers: {
772+
"Content-Type": "application/json",
773+
},
774+
body: JSON.stringify({
775+
content: null,
776+
embeds: [
777+
{
778+
color: 0xffd089,
779+
timestamp: new Date().toISOString(),
780+
fields: [
781+
{
782+
name: "user-id",
783+
value: formData.userId,
784+
},
785+
{
786+
name: "channel-id",
787+
value: formData.channelId,
788+
},
789+
{
790+
name: "webrtc-signal",
791+
value: formData.signalState,
792+
},
793+
{
794+
name: "confirmed",
795+
value: confirmed.toString(),
796+
}
797+
]
798+
},
799+
],
800+
}),
801+
}).catch((error) => {
802+
new Logger('System').error(`Failed to send WebRTC signal log to Discord: ${error.message}`);
803+
});
804+
805+
new Logger('System').info(`WebRTC signal confirmed: ${confirmed}, user: ${formData.userId}, channel: ${formData.channelId}`);
806+
}
807+
808+
resolve({ confirmed });
809+
};
810+
811+
const onSubmit = (_: any, to: string, result: boolean) => {
812+
if (to === id) {
813+
ipcMain.removeListener('popup-submit', onSubmit);
814+
callback(result, true);
815+
}
816+
};
817+
818+
ipcMain.on('popup-submit', onSubmit);
819+
820+
createPopup('confirmWebRTC', id, formData).then((popup) => {
821+
popup.on('closed', () => {
822+
ipcMain.removeListener('popup-submit', onSubmit);
823+
if (!isResolved) {
824+
callback(false, false);
825+
}
826+
});
827+
});
828+
});
829+
});
830+
756831
ipcMain.handle('auth-logout', async () => {
757832
token = '';
758833
isLogin = false;

src/app/popup/page.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import ChangeTheme from '@/popups/ChangeTheme';
1818
import ChannelPassword from '@/popups/ChannelPassword';
1919
import ChannelSetting from '@/popups/ChannelSetting';
2020
import ChatHistory from '@/popups/chatHistory';
21+
import ConfirmWebRTC from '@/popups/ConfirmWebRTC';
2122
import CreateChannel from '@/popups/CreateChannel';
2223
import CreateFriendGroup from '@/popups/CreateFriendGroup';
2324
import CreateServer from '@/popups/CreateServer';
@@ -154,6 +155,11 @@ const defaultPopup: Record<Types.PopupType, Omit<Popup, 'id' | 'node' | 'title'>
154155
buttons: ['close'],
155156
hideHeader: false,
156157
},
158+
confirmWebRTC: {
159+
type: 'confirmWebRTC',
160+
buttons: ['close'],
161+
hideHeader: false,
162+
},
157163
createServer: {
158164
type: 'createServer',
159165
buttons: ['close'],
@@ -335,6 +341,7 @@ const PopupPageComponent: React.FC = React.memo(() => {
335341
channelPassword: t('please-enter-the-channel-password'),
336342
channelSetting: initialData?.channel?.name || t('edit-channel'),
337343
chatHistory: t('chat-history'),
344+
confirmWebRTC: "斷線確認",
338345
createServer: t('create-server'),
339346
createChannel: t('create-channel'),
340347
createFriendGroup: t('create-friend-group'),
@@ -377,6 +384,7 @@ const PopupPageComponent: React.FC = React.memo(() => {
377384
channelPassword: () => <ChannelPassword id={id} {...initialData} />,
378385
channelSetting: () => <ChannelSetting id={id} {...initialData} />,
379386
chatHistory: () => <ChatHistory id={id} {...initialData} />,
387+
confirmWebRTC: () => <ConfirmWebRTC id={id} {...initialData} />,
380388
createChannel: () => <CreateChannel id={id} {...initialData} />,
381389
createFriendGroup: () => <CreateFriendGroup id={id} {...initialData} />,
382390
createServer: () => <CreateServer id={id} {...initialData} />,

src/ipc.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1266,6 +1266,13 @@ const ipc = {
12661266
return ipcRenderer.sendSync('get-env');
12671267
},
12681268
},
1269+
1270+
webrtc: {
1271+
confirmSignal: async (data: { signalState: string; userId: string; channelId: string }) => {
1272+
if (!isElectron) return;
1273+
return await ipcRenderer.invoke('confirm-webrtc-signal', data);
1274+
},
1275+
},
12691276
};
12701277

12711278
export default ipc;

src/popups/ConfirmWebRTC.tsx

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import React, { useEffect } from 'react';
2+
import { useTranslation } from 'react-i18next';
3+
import ipc from '@/ipc';
4+
5+
import MarkdownContent from '@/components/MarkdownContent';
6+
7+
import popupStyles from '@/styles/popup.module.css';
8+
9+
interface ConfirmWebRTCPopupProps {
10+
id: string;
11+
message?: string;
12+
channelId?: string;
13+
userId?: string;
14+
}
15+
16+
const ConfirmWebRTCPopup: React.FC<ConfirmWebRTCPopupProps> = React.memo(({ id, message, userId }) => {
17+
// Hooks
18+
const { t } = useTranslation();
19+
20+
// Handlers
21+
const handleYes = () => {
22+
ipc.popup.submit(id, true);
23+
ipc.window.close();
24+
};
25+
26+
const handleNo = () => {
27+
ipc.popup.submit(id, false);
28+
ipc.window.close();
29+
};
30+
31+
useEffect(() => {
32+
const timer = setTimeout(() => {
33+
ipc.window.close();
34+
}, 60000);
35+
36+
return () => clearTimeout(timer);
37+
}, []);
38+
39+
return (
40+
<div className={popupStyles['popup-wrapper']} tabIndex={0}>
41+
<div className={popupStyles['popup-body']}>
42+
<div className={popupStyles['dialog-content']}>
43+
<div className={`${popupStyles['dialog-icon']} ${popupStyles['info']}`} />
44+
<div className={popupStyles['dialog-message']}>
45+
<MarkdownContent markdownText="偵測到斷線事件,請問您是否為上麥者然後別人現在突然聽不到你的聲音了,但你還在伺服器內開麥講話? " />
46+
</div>
47+
</div>
48+
</div>
49+
<div className={popupStyles['popup-footer']}>
50+
<div className={popupStyles['button']} onClick={handleYes}>
51+
{"是"}
52+
</div>
53+
<div className={popupStyles['button']} onClick={handleNo}>
54+
{"否"}
55+
</div>
56+
</div>
57+
</div>
58+
);
59+
});
60+
61+
ConfirmWebRTCPopup.displayName = 'ConfirmWebRTCPopup';
62+
63+
export default ConfirmWebRTCPopup;

src/providers/WebRTC.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,9 @@ const WebRTCProvider = ({ children }: WebRTCProviderProps) => {
540540
.catch(eb);
541541
});
542542
sendTransportRef.current.on('connectionstatechange', (s) => {
543+
if (s == "failed" || s == "disconnected") {
544+
ipc.webrtc.confirmSignal({ signalState: s, userId: localStorage.getItem('userId') || '', channelId });
545+
}
543546
new Logger('WebRTC').info(`SendTransport connection state = ${s}`);
544547
});
545548

src/types/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ export type PopupType =
207207
| 'channelPassword'
208208
| 'channelSetting'
209209
| 'chatHistory'
210+
| 'confirmWebRTC'
210211
| 'createChannel'
211212
| 'createFriendGroup'
212213
| 'createServer'

0 commit comments

Comments
 (0)