-
Notifications
You must be signed in to change notification settings - Fork 13.8k
fix: Stop media player when message is not accessible #41600
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,12 @@ | ||
| import { useMergedRefs, useStableCallback } from '@rocket.chat/fuselage-hooks'; | ||
| import { useStream, useUserId } from '@rocket.chat/ui-contexts'; | ||
| import type { ReactNode } from 'react'; | ||
| import { useCallback, useMemo, useRef, useState } from 'react'; | ||
| import { useCallback, useEffect, useMemo, useRef, useState } from 'react'; | ||
|
|
||
| import type { MediaPlayerContextValue, PersistentAudioTrack } from './MediaPlayerContext'; | ||
| import { MediaPlayerContext } from './MediaPlayerContext'; | ||
| import { useReloadOnError } from '../../components/message/content/attachments/file/hooks/useReloadOnError'; | ||
| import { Messages } from '../../stores'; | ||
|
|
||
| const PLAYBACK_RATES = [1, 1.5, 2] as const; | ||
|
|
||
|
|
@@ -27,6 +29,7 @@ const MediaPlayerProvider = ({ children }: MediaPlayerProviderProps) => { | |
| const [currentTime, setCurrentTime] = useState(0); | ||
| const [duration, setDuration] = useState(0); | ||
| const [playbackRate, setPlaybackRate] = useState<number>(1); | ||
| const userId = useUserId(); | ||
|
|
||
| const trackRef = useRef<PersistentAudioTrack | null>(null); | ||
| trackRef.current = track; | ||
|
|
@@ -101,6 +104,78 @@ const MediaPlayerProvider = ({ children }: MediaPlayerProviderProps) => { | |
| }); | ||
|
|
||
| const isActive = useCallback((id: string) => trackRef.current?.id === id, []); | ||
| const subscribeToNotifyRoom = useStream('notify-room'); | ||
| const subscribeToNotifyUser = useStream('notify-user'); | ||
|
|
||
| //For hard message delete, when Message Message_ShowDeletedStatus is off | ||
| useEffect(() => { | ||
| if (!track) { | ||
| return; | ||
| } | ||
|
|
||
| const { rid, mid } = track; | ||
|
|
||
| if (!rid || !mid) { | ||
| return; | ||
| } | ||
|
|
||
| const unsubscribeFromDeleteMessage = subscribeToNotifyRoom(`${rid}/deleteMessage`, ({ _id }) => { | ||
| if (_id === mid) { | ||
| close(); | ||
| } | ||
| }); | ||
|
|
||
| const unsubscribeFromDeleteMessageBulk = subscribeToNotifyRoom(`${rid}/deleteMessageBulk`, ({ ids }) => { | ||
| if (ids?.includes(mid)) { | ||
| close(); | ||
| } | ||
| }); | ||
|
|
||
| return () => { | ||
| unsubscribeFromDeleteMessage(); | ||
| unsubscribeFromDeleteMessageBulk(); | ||
| }; | ||
| }, [track, subscribeToNotifyRoom, close]); | ||
|
|
||
| //For soft message delete, when Message_ShowDeletedStatus is on and server updates message.t to rm | ||
| useEffect(() => { | ||
| if (!track) { | ||
| return; | ||
| } | ||
|
|
||
| const unsub = Messages.use.subscribe((state, prevState) => { | ||
| const { mid } = track; | ||
| if (!mid) { | ||
| return; | ||
| } | ||
|
|
||
| const message = state.records.get(mid); | ||
|
|
||
| if (message && message !== prevState.records.get(mid) && message.t === 'rm') { | ||
| close(); | ||
| } | ||
| }); | ||
|
|
||
| return unsub; | ||
| }, [track, close]); | ||
|
|
||
| useEffect(() => { | ||
| if (!track || !userId) { | ||
| return; | ||
| } | ||
|
|
||
| const { rid } = track; | ||
|
|
||
| if (!rid) { | ||
| return; | ||
| } | ||
|
|
||
| return subscribeToNotifyUser(`${userId}/subscriptions-changed`, (event, subscription) => { | ||
| if (event === 'removed' && subscription.rid === rid) { | ||
| close(); | ||
| } | ||
| }); | ||
| }, [userId, subscribeToNotifyUser, track, close]); | ||
|
Comment on lines
+162
to
+178
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you leave a public room, you might still have access to the message, so listening for subscription removal would stop playing a sound even if you still have access to the message. Another scenario is, you're listening to an audio in a public room, without joining it, and loosing the permission |
||
|
|
||
| const value = useMemo<MediaPlayerContextValue>( | ||
| () => ({ track, playing, currentTime, duration, playbackRate, play, toggle, seek, cyclePlaybackRate, close, isActive }), | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like for this to at least be isolated in it's own hook.