mirror of
https://github.com/SableClient/Sable.git
synced 2026-11-26 20:06:26 -08:00
restore call intents
This commit is contained in:
1 parent
177485d053
commit
0e2028fb7f
6 files changed
+313
-67
No files matched your search
@@ -1,75 +1,181 @@
|
||||
import { IconButton, Icon, Icons, TooltipProvider, Tooltip, Text } from 'folds';
|
||||
import type { MouseEventHandler } from 'react';
|
||||
import { useState } from 'react';
|
||||
import FocusTrap from 'focus-trap-react';
|
||||
import type { RectCords } from 'folds';
|
||||
import {
|
||||
Box,
|
||||
IconButton,
|
||||
Icon,
|
||||
Icons,
|
||||
TooltipProvider,
|
||||
Tooltip,
|
||||
Text,
|
||||
PopOut,
|
||||
Menu,
|
||||
MenuItem,
|
||||
config,
|
||||
} from 'folds';
|
||||
import { useAtomValue } from 'jotai';
|
||||
import type { Room, TimelineEvents } from '$types/matrix-sdk';
|
||||
import type { Room } from '$types/matrix-sdk';
|
||||
import { useCallStart, useCallJoined } from '$hooks/useCallEmbed';
|
||||
import type { CallPreferences } from '$state/callPreferences';
|
||||
import { callEmbedAtom } from '$state/callEmbed';
|
||||
import { useMatrixClient } from '$hooks/useMatrixClient';
|
||||
import { useCallPreferences } from '$state/hooks/callPreferences';
|
||||
import { stopPropagation } from '$utils/keyboard';
|
||||
|
||||
interface RoomCallButtonProps {
|
||||
room: Room;
|
||||
direct: boolean;
|
||||
defaultPreferences: CallPreferences;
|
||||
allowVideoStart?: boolean;
|
||||
}
|
||||
|
||||
export function RoomCallButton({ room }: RoomCallButtonProps) {
|
||||
const startCall = useCallStart();
|
||||
type CallStartMenuProps = {
|
||||
onVoiceCall: () => void;
|
||||
onVideoCall: () => void;
|
||||
requestClose: () => void;
|
||||
allowVideoStart: boolean;
|
||||
};
|
||||
|
||||
function CallStartMenu({
|
||||
onVoiceCall,
|
||||
onVideoCall,
|
||||
requestClose,
|
||||
allowVideoStart,
|
||||
}: CallStartMenuProps) {
|
||||
return (
|
||||
<Menu style={{ minWidth: '150px', padding: config.space.S100 }}>
|
||||
<Box direction="Column" gap="100">
|
||||
<MenuItem onClick={onVoiceCall} size="300" radii="300">
|
||||
<Text style={{ flexGrow: 1 }} as="span" size="T300" truncate>
|
||||
Voice Call
|
||||
</Text>
|
||||
</MenuItem>
|
||||
{allowVideoStart && (
|
||||
<MenuItem onClick={onVideoCall} size="300" radii="300">
|
||||
<Text style={{ flexGrow: 1 }} as="span" size="T300" truncate>
|
||||
Video Call
|
||||
</Text>
|
||||
</MenuItem>
|
||||
)}
|
||||
<MenuItem onClick={requestClose} size="300" radii="300">
|
||||
<Text style={{ flexGrow: 1 }} as="span" size="T300" truncate>
|
||||
Cancel
|
||||
</Text>
|
||||
</MenuItem>
|
||||
</Box>
|
||||
</Menu>
|
||||
);
|
||||
}
|
||||
|
||||
export function RoomCallButton({
|
||||
room,
|
||||
direct,
|
||||
defaultPreferences,
|
||||
allowVideoStart = true,
|
||||
}: RoomCallButtonProps) {
|
||||
const startCall = useCallStart(direct);
|
||||
const callEmbed = useAtomValue(callEmbedAtom);
|
||||
const joined = useCallJoined(callEmbed);
|
||||
const mx = useMatrixClient();
|
||||
const { microphone, video, sound } = useCallPreferences();
|
||||
const [menuAnchor, setMenuAnchor] = useState<RectCords>();
|
||||
|
||||
const isJoinedInThisRoom = joined && callEmbed?.roomId === room.roomId;
|
||||
const callStartingInThisRoom = !!callEmbed && callEmbed.roomId === room.roomId && !joined;
|
||||
const inAnotherCall = !!callEmbed && callEmbed.roomId !== room.roomId;
|
||||
const startDisabled = inAnotherCall || callStartingInThisRoom;
|
||||
|
||||
if (isJoinedInThisRoom) return null;
|
||||
|
||||
const handleStartCall = async () => {
|
||||
startCall(room, { microphone, video, sound });
|
||||
try {
|
||||
const now = Date.now();
|
||||
// TODO not use as any one day someday i swear
|
||||
await mx.sendEvent(
|
||||
room.roomId,
|
||||
'org.matrix.msc4075.rtc.notification' as keyof TimelineEvents,
|
||||
{
|
||||
notification_type: 'ring',
|
||||
sender_ts: now,
|
||||
lifetime: 30000,
|
||||
'm.mentions': {
|
||||
room: true,
|
||||
},
|
||||
application: 'm.call',
|
||||
call_id: room.roomId,
|
||||
'm.text': [
|
||||
{
|
||||
body: `Call started by ${mx.getUser(mx.getSafeUserId())?.displayName || 'User'} 🎶`,
|
||||
},
|
||||
],
|
||||
} as unknown as TimelineEvents[keyof TimelineEvents]
|
||||
);
|
||||
} catch {
|
||||
/* skill issue block */
|
||||
}
|
||||
const startVoiceCall = () => {
|
||||
startCall(room, {
|
||||
microphone: defaultPreferences.microphone,
|
||||
video: false,
|
||||
sound: defaultPreferences.sound,
|
||||
});
|
||||
setMenuAnchor(undefined);
|
||||
};
|
||||
|
||||
const startVideoCall = () => {
|
||||
startCall(room, {
|
||||
microphone: defaultPreferences.microphone,
|
||||
video: true,
|
||||
sound: defaultPreferences.sound,
|
||||
});
|
||||
setMenuAnchor(undefined);
|
||||
};
|
||||
|
||||
const startDefaultCall = () => {
|
||||
const resolvedVideo = allowVideoStart ? defaultPreferences.video : false;
|
||||
startCall(room, {
|
||||
microphone: defaultPreferences.microphone,
|
||||
video: resolvedVideo,
|
||||
sound: defaultPreferences.sound,
|
||||
});
|
||||
};
|
||||
|
||||
const handleOpenMenu: MouseEventHandler<HTMLButtonElement> = (evt) => {
|
||||
setMenuAnchor(evt.currentTarget.getBoundingClientRect());
|
||||
};
|
||||
|
||||
return (
|
||||
<TooltipProvider
|
||||
position="Bottom"
|
||||
offset={4}
|
||||
tooltip={
|
||||
<Tooltip>
|
||||
<Text>Start Voice Call</Text>
|
||||
</Tooltip>
|
||||
}
|
||||
>
|
||||
{(triggerRef) => (
|
||||
<IconButton
|
||||
fill="None"
|
||||
ref={triggerRef}
|
||||
onClick={handleStartCall}
|
||||
aria-label="Start Voice Call"
|
||||
>
|
||||
<Icon size="400" src={Icons.Phone} />
|
||||
</IconButton>
|
||||
)}
|
||||
</TooltipProvider>
|
||||
<>
|
||||
<TooltipProvider
|
||||
position="Bottom"
|
||||
offset={4}
|
||||
tooltip={
|
||||
<Tooltip>
|
||||
{inAnotherCall ? (
|
||||
<Text>Already in another call</Text>
|
||||
) : callStartingInThisRoom ? (
|
||||
<Text>Call is starting</Text>
|
||||
) : (
|
||||
<Text>Start Call</Text>
|
||||
)}
|
||||
</Tooltip>
|
||||
}
|
||||
>
|
||||
{(triggerRef) => (
|
||||
<IconButton
|
||||
fill="None"
|
||||
ref={triggerRef}
|
||||
onClick={handleOpenMenu}
|
||||
onContextMenu={(evt) => {
|
||||
evt.preventDefault();
|
||||
if (startDisabled) return;
|
||||
startDefaultCall();
|
||||
}}
|
||||
disabled={startDisabled}
|
||||
aria-label="Start Call"
|
||||
aria-pressed={!!menuAnchor}
|
||||
>
|
||||
<Icon size="400" src={Icons.VideoCamera} filled={!!menuAnchor} />
|
||||
</IconButton>
|
||||
)}
|
||||
</TooltipProvider>
|
||||
<PopOut
|
||||
anchor={menuAnchor}
|
||||
position="Bottom"
|
||||
align="Center"
|
||||
content={
|
||||
<FocusTrap
|
||||
focusTrapOptions={{
|
||||
initialFocus: false,
|
||||
returnFocusOnDeactivate: false,
|
||||
onDeactivate: () => setMenuAnchor(undefined),
|
||||
clickOutsideDeactivates: true,
|
||||
isKeyForward: (evt: KeyboardEvent) => evt.key === 'ArrowDown',
|
||||
isKeyBackward: (evt: KeyboardEvent) => evt.key === 'ArrowUp',
|
||||
escapeDeactivates: stopPropagation,
|
||||
}}
|
||||
>
|
||||
<CallStartMenu
|
||||
onVoiceCall={startVoiceCall}
|
||||
onVideoCall={startVideoCall}
|
||||
requestClose={() => setMenuAnchor(undefined)}
|
||||
allowVideoStart={allowVideoStart}
|
||||
/>
|
||||
</FocusTrap>
|
||||
}
|
||||
/>
|
||||
</>
|
||||
);
|
||||
}
|
||||
@@ -91,6 +91,7 @@ import { callChatAtom } from '$state/callEmbed';
|
||||
import { RoomSettingsPage } from '$state/roomSettings';
|
||||
import { roomIdToThreadBrowserAtomFamily } from '$state/room/roomToThreadBrowser';
|
||||
import { roomIdToOpenThreadAtomFamily } from '$state/room/roomToOpenThread';
|
||||
import { useCallPreferences } from '$state/hooks/callPreferences';
|
||||
import { JumpToTime } from './jump-to-time';
|
||||
import { RoomPinMenu } from './room-pin-menu';
|
||||
import * as css from './RoomViewHeader.css';
|
||||
@@ -355,6 +356,7 @@ export function RoomViewHeader({ callView }: Readonly<{ callView?: boolean }>) {
|
||||
const [pinMenuAnchor, setPinMenuAnchor] = useState<RectCords>();
|
||||
const direct = useIsDirectRoom();
|
||||
const [customDMCards] = useSetting(settingsAtom, 'customDMCards');
|
||||
const { microphone, video, sound } = useCallPreferences();
|
||||
|
||||
const [chat, setChat] = useAtom(callChatAtom);
|
||||
const [threadBrowserOpen, setThreadBrowserOpen] = useAtom(
|
||||
@@ -725,7 +727,14 @@ export function RoomViewHeader({ callView }: Readonly<{ callView?: boolean }>) {
|
||||
</IconButton>
|
||||
)}
|
||||
</TooltipProvider>
|
||||
{canUseCalls && shouldShowCallButton && <RoomCallButton room={room} />}
|
||||
{canUseCalls && shouldShowCallButton && (
|
||||
<RoomCallButton
|
||||
room={room}
|
||||
direct={direct}
|
||||
defaultPreferences={{ microphone, video, sound }}
|
||||
allowVideoStart
|
||||
/>
|
||||
)}
|
||||
<PopOut
|
||||
anchor={pinMenuAnchor}
|
||||
position="Bottom"
|
||||
|
||||
@@ -1,24 +1,36 @@
|
||||
import { useEffect } from 'react';
|
||||
import { useAtom } from 'jotai';
|
||||
import { useAtom, useAtomValue } from 'jotai';
|
||||
import { useCallStart } from '$hooks/useCallEmbed';
|
||||
import { useMatrixClient } from '$hooks/useMatrixClient';
|
||||
import { useSelectedRoom } from '$hooks/router/useSelectedRoom';
|
||||
import { autoJoinCallIntentAtom } from '$state/callEmbed';
|
||||
import { mDirectAtom } from '$state/mDirectList';
|
||||
|
||||
export function useAutoJoinCall() {
|
||||
const mx = useMatrixClient();
|
||||
const selectedRoomId = useSelectedRoom();
|
||||
const [autoJoinIntent, setAutoJoinIntent] = useAtom(autoJoinCallIntentAtom);
|
||||
const startCall = useCallStart();
|
||||
const mDirects = useAtomValue(mDirectAtom);
|
||||
const startDirectCall = useCallStart(true);
|
||||
const startRoomCall = useCallStart(false);
|
||||
|
||||
useEffect(() => {
|
||||
if (selectedRoomId && autoJoinIntent && selectedRoomId === autoJoinIntent) {
|
||||
const room = mx.getRoom(selectedRoomId);
|
||||
|
||||
if (room) {
|
||||
const startCall = mDirects.has(room.roomId) ? startDirectCall : startRoomCall;
|
||||
startCall(room);
|
||||
setAutoJoinIntent(null);
|
||||
}
|
||||
}
|
||||
}, [selectedRoomId, autoJoinIntent, startCall, setAutoJoinIntent, mx]);
|
||||
}, [
|
||||
selectedRoomId,
|
||||
autoJoinIntent,
|
||||
setAutoJoinIntent,
|
||||
mx,
|
||||
mDirects,
|
||||
startDirectCall,
|
||||
startRoomCall,
|
||||
]);
|
||||
}
|
||||
@@ -26,9 +26,9 @@ const intentCases: IntentCase[] = [
|
||||
{ dm: true, ongoing: true, video: true, expected: ElementCallIntent.JoinExistingDM },
|
||||
{ dm: true, ongoing: true, video: false, expected: ElementCallIntent.JoinExistingDMVoice },
|
||||
{ dm: false, ongoing: false, video: true, expected: ElementCallIntent.StartCall },
|
||||
{ dm: false, ongoing: false, video: false, expected: 'start_call_voice' },
|
||||
{ dm: false, ongoing: false, video: false, expected: ElementCallIntent.StartCallVoice },
|
||||
{ dm: false, ongoing: true, video: true, expected: ElementCallIntent.JoinExisting },
|
||||
{ dm: false, ongoing: true, video: false, expected: 'join_existing_voice' },
|
||||
{ dm: false, ongoing: true, video: false, expected: ElementCallIntent.JoinExistingVoice },
|
||||
];
|
||||
|
||||
describe('CallEmbed.getIntent', () => {
|
||||
@@ -37,3 +37,94 @@ describe('CallEmbed.getIntent', () => {
|
||||
expect(intent).toBe(tc.expected);
|
||||
});
|
||||
});
|
||||
|
||||
describe('CallEmbed.dmCall', () => {
|
||||
it.each([
|
||||
ElementCallIntent.StartCallDM,
|
||||
ElementCallIntent.StartCallDMVoice,
|
||||
ElementCallIntent.JoinExistingDM,
|
||||
ElementCallIntent.JoinExistingDMVoice,
|
||||
])('returns true for DM intent %s', (intent) => {
|
||||
expect(CallEmbed.dmCall(intent)).toBe(true);
|
||||
});
|
||||
|
||||
it.each([
|
||||
ElementCallIntent.StartCall,
|
||||
ElementCallIntent.StartCallVoice,
|
||||
ElementCallIntent.JoinExisting,
|
||||
ElementCallIntent.JoinExistingVoice,
|
||||
])('returns false for room intent %s', (intent) => {
|
||||
expect(CallEmbed.dmCall(intent)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('CallEmbed.startingCall', () => {
|
||||
it.each([
|
||||
ElementCallIntent.StartCall,
|
||||
ElementCallIntent.StartCallVoice,
|
||||
ElementCallIntent.StartCallDM,
|
||||
ElementCallIntent.StartCallDMVoice,
|
||||
])('returns true for start intent %s', (intent) => {
|
||||
expect(CallEmbed.startingCall(intent)).toBe(true);
|
||||
});
|
||||
|
||||
it.each([
|
||||
ElementCallIntent.JoinExisting,
|
||||
ElementCallIntent.JoinExistingVoice,
|
||||
ElementCallIntent.JoinExistingDM,
|
||||
ElementCallIntent.JoinExistingDMVoice,
|
||||
])('returns false for join intent %s', (intent) => {
|
||||
expect(CallEmbed.startingCall(intent)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('CallEmbed.getWidget', () => {
|
||||
vi.stubGlobal('window', {
|
||||
location: { origin: 'https://app.example.com' },
|
||||
});
|
||||
|
||||
const mx = {
|
||||
baseUrl: 'https://matrix.example.com',
|
||||
getSafeUserId: () => '@alice:example.com',
|
||||
getDeviceId: () => 'ALICEDEVICE',
|
||||
} as never;
|
||||
|
||||
const createRoom = (isCallRoom: boolean) =>
|
||||
({
|
||||
roomId: '!room:example.com',
|
||||
hasEncryptionStateEvent: () => false,
|
||||
isCallRoom: () => isCallRoom,
|
||||
}) as never;
|
||||
|
||||
it('adds ring notification delegation for starting DM calls in non-call rooms', () => {
|
||||
const room = createRoom(false);
|
||||
const widget = CallEmbed.getWidget(mx, room, ElementCallIntent.StartCallDMVoice, 'dark');
|
||||
const url = new URL(widget.getCompleteUrl({ currentUserId: '@alice:example.com' }));
|
||||
|
||||
expect(url.searchParams.get('sendNotificationType')).toBe('ring');
|
||||
});
|
||||
|
||||
it('adds notification delegation for starting room calls in non-call rooms', () => {
|
||||
const room = createRoom(false);
|
||||
const widget = CallEmbed.getWidget(mx, room, ElementCallIntent.StartCallVoice, 'dark');
|
||||
const url = new URL(widget.getCompleteUrl({ currentUserId: '@alice:example.com' }));
|
||||
|
||||
expect(url.searchParams.get('sendNotificationType')).toBe('notification');
|
||||
});
|
||||
|
||||
it('does not add notification delegation for join intents', () => {
|
||||
const room = createRoom(false);
|
||||
const widget = CallEmbed.getWidget(mx, room, ElementCallIntent.JoinExisting, 'dark');
|
||||
const url = new URL(widget.getCompleteUrl({ currentUserId: '@alice:example.com' }));
|
||||
|
||||
expect(url.searchParams.get('sendNotificationType')).toBeNull();
|
||||
});
|
||||
|
||||
it('does not add notification delegation in call rooms', () => {
|
||||
const room = createRoom(true);
|
||||
const widget = CallEmbed.getWidget(mx, room, ElementCallIntent.StartCallDM, 'dark');
|
||||
const url = new URL(widget.getCompleteUrl({ currentUserId: '@alice:example.com' }));
|
||||
|
||||
expect(url.searchParams.get('sendNotificationType')).toBeNull();
|
||||
});
|
||||
});
|
||||
@@ -40,15 +40,36 @@ export class CallEmbed {
|
||||
private readonly disposables: Array<() => void> = [];
|
||||
|
||||
static getIntent(dm: boolean, ongoing: boolean, video: boolean | undefined): ElementCallIntent {
|
||||
if (!dm) {
|
||||
return video ? ElementCallIntent.JoinExisting : ElementCallIntent.JoinExistingDMVoice;
|
||||
}
|
||||
|
||||
if (ongoing) {
|
||||
return video ? ElementCallIntent.JoinExistingDM : ElementCallIntent.JoinExistingDMVoice;
|
||||
if (dm) {
|
||||
return video ? ElementCallIntent.JoinExistingDM : ElementCallIntent.JoinExistingDMVoice;
|
||||
}
|
||||
return video ? ElementCallIntent.JoinExisting : ElementCallIntent.JoinExistingVoice;
|
||||
}
|
||||
|
||||
return video ? ElementCallIntent.StartCallDM : ElementCallIntent.StartCallDMVoice;
|
||||
if (dm) {
|
||||
return video ? ElementCallIntent.StartCallDM : ElementCallIntent.StartCallDMVoice;
|
||||
}
|
||||
|
||||
return video ? ElementCallIntent.StartCall : ElementCallIntent.StartCallVoice;
|
||||
}
|
||||
|
||||
static dmCall(intent: ElementCallIntent): boolean {
|
||||
return (
|
||||
intent === ElementCallIntent.JoinExistingDM ||
|
||||
intent === ElementCallIntent.JoinExistingDMVoice ||
|
||||
intent === ElementCallIntent.StartCallDM ||
|
||||
intent === ElementCallIntent.StartCallDMVoice
|
||||
);
|
||||
}
|
||||
|
||||
static startingCall(intent: ElementCallIntent): boolean {
|
||||
return (
|
||||
intent === ElementCallIntent.StartCallDM ||
|
||||
intent === ElementCallIntent.StartCallDMVoice ||
|
||||
intent === ElementCallIntent.StartCall ||
|
||||
intent === ElementCallIntent.StartCallVoice
|
||||
);
|
||||
}
|
||||
|
||||
static getWidget(
|
||||
@@ -77,8 +98,13 @@ export class CallEmbed {
|
||||
perParticipantE2EE: room.hasEncryptionStateEvent().toString(),
|
||||
lang: 'en-EN',
|
||||
theme: themeKind,
|
||||
header: 'none',
|
||||
});
|
||||
|
||||
if (!room.isCallRoom() && CallEmbed.startingCall(intent)) {
|
||||
params.append('sendNotificationType', CallEmbed.dmCall(intent) ? 'ring' : 'notification');
|
||||
}
|
||||
|
||||
const widgetUrl = new URL(
|
||||
`${trimTrailingSlash(import.meta.env.BASE_URL)}/public/element-call/index.html`,
|
||||
window.location.origin
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
export enum ElementCallIntent {
|
||||
StartCall = 'start_call',
|
||||
JoinExisting = 'join_existing',
|
||||
StartCallVoice = 'start_call_voice',
|
||||
JoinExistingVoice = 'join_existing_voice',
|
||||
StartCallDM = 'start_call_dm',
|
||||
JoinExistingDM = 'join_existing_dm',
|
||||
StartCallDMVoice = 'start_call_dm_voice',
|
||||
|
||||
Reference in new issue
Block a user