chore: major discord-rpc improvments

This commit is contained in:
RockinChaos 2024-09-26 20:19:24 -07:00
parent 1b1d07bb0b
commit 219ed4543f
3 changed files with 88 additions and 54 deletions

View file

@ -3,8 +3,6 @@
import AnimeResolver from '@/modules/animeresolver.js'
import { videoRx } from '@/modules/util.js'
import { tick } from 'svelte'
import { state } from '../WatchTogether/WatchTogether.svelte'
import IPC from '@/modules/ipc.js'
import { anilistClient } from "@/modules/anilist.js"
import Debug from 'debug'
@ -69,7 +67,6 @@
episodeTitle: streamingEpisode && episodeRx.exec(streamingEpisode.title)[2],
thumbnail: streamingEpisode?.thumbnail || media?.coverImage.extraLarge
}
setDiscordRPC(np)
setMediaSession(np)
nowPlaying.set(np)
}
@ -210,53 +207,6 @@
: new MediaMetadata({ title: name })
navigator.mediaSession.metadata = metadata
}
function setDiscordRPC (np = nowPlaying.value) {
const w2g = state.value?.code
const details = [np.title, np.episodeTitle].filter(i => i).join(' - ') || undefined
const activity = {
details,
state: details && 'Watching Episode ' + ((!np.media?.episodes && np.episode) || ''),
timestamps: {
start: Date.now()
},
party: {
size: (np.episode && np.media?.episodes && [np.episode, np.media.episodes]) || undefined
},
assets: {
large_text: np.title,
large_image: np.thumbnail,
small_image: 'logo',
small_text: 'https://github.com/ThaUnknown/miru'
},
instance: true,
type: 3
}
// cannot have buttons and secrets at once
if (w2g) {
activity.secrets = {
join: w2g,
match: w2g + 'm'
}
activity.party.id = w2g + 'p'
} else {
activity.buttons = [
{
label: 'Download app',
url: 'https://github.com/ThaUnknown/miru/releases/latest'
},
{
label: 'Watch on Miru',
url: `miru://anime/${np.media?.id}`
}
]
}
IPC.emit('discord', { activity })
}
state.subscribe(() => {
setDiscordRPC()
return noop
})
</script>
<script>

View file

@ -74,6 +74,13 @@
let playbackRate = 1
$: localStorage.setItem('volume', (volume || 0).toString())
$: safeduration = (isFinite(duration) ? duration : currentTime) || 0
$: {
if (hidden) {
setDiscordRPC(media, video?.currentTime)
} else {
setDiscordRPC(media, (paused && miniplayer))
}
}
function checkAudio () {
if ('audioTracks' in HTMLVideoElement.prototype) {
@ -282,7 +289,8 @@
}
const handleVisibility = visibility => {
if (!video?.ended && $settings.playerPause && !pip) {
if (visibility === 'hidden') {
hidden = (visibility === 'hidden')
if (hidden) {
visibilityPaused = paused
paused = true
} else {
@ -290,6 +298,7 @@
}
}
}
let hidden = false
let visibilityPaused = true
document.addEventListener('visibilitychange', () => handleVisibility(document.visibilityState))
IPC.on('visibilitychange', handleVisibility)
@ -359,7 +368,7 @@
for (const track of video.audioTracks) {
track.enabled = track.id === id
}
seek(-0.2) // stupid fix because video freezes up when chaging tracks
seek(-0.2) // stupid fix because video freezes up when changing tracks
}
}
function selectVideo (id) {
@ -1031,6 +1040,77 @@
document.querySelector('[data-name=\'toggleFullscreen\']')?.focus()
}
}
function setDiscordRPC (np = media, browsing) {
if ((!np || Object.keys(np).length === 0) && !browsing) return
if (hidden) {
IPC.emit('discord-hidden')
return
}
let activity
if (!browsing) {
const w2g = state.value?.code
const details = np.title || undefined
const timeLeft = safeduration - targetTime;
const timestamps = !paused ? {
start: Date.now() - (targetTime > 0 ? targetTime * 1000 : 0),
end: Date.now() + timeLeft * 1000
} : undefined
activity = {
details,
state: (details && (np.media?.format === 'MOVIE' ? 'The Movie' : (np.episode ? 'Episode: ' + np.episode + (np.media?.episodes ? ' of ' + np.media.episodes : '') : 'Streaming the Universe'))),
timestamps,
assets: {
large_text: np.title,
large_image: np.thumbnail,
small_image: !paused ? 'logo' : 'https://i.imgur.com/3RuaavC.png', // probably should upload the 'paused' image to the discord assets for the bot.
small_text: (!paused ? '(Playing)' : '(Paused)') + ' https://github.com/ThaUnknown/miru'
},
instance: true,
type: 3
}
// cannot have buttons and secrets at once
if (w2g) {
activity.secrets = {
join: w2g,
match: w2g + 'm'
}
activity.party.id = w2g + 'p'
} else {
activity.buttons = [
{
label: 'Download app',
url: 'https://github.com/ThaUnknown/miru/releases/latest'
},
{
label: 'Watch on Miru',
url: `miru://anime/${np.media?.id}`
}
]
}
} else {
activity = {
timestamps: { start: Date.now() },
details: 'Stream anime torrents',
state: 'Browsing for anime',
assets: {
large_image: 'logo',
large_text: 'https://github.com/ThaUnknown/miru',
small_image: 'https://i.imgur.com/GiDlvVA.png', // probably should upload the 'search' image to the discord assets for the bot.
small_text: 'Browsing for anime',
},
buttons: [
{
label: 'Download app',
url: 'https://github.com/ThaUnknown/miru/releases/latest'
}
],
instance: true,
type: 3
}
}
IPC.emit('discord', { activity })
}
</script>
<!-- <svelte:window bind:innerWidth bind:innerHeight /> -->

View file

@ -6,7 +6,7 @@ export default class Discord {
defaultStatus = {
activity: {
timestamps: { start: Date.now() },
details: 'Stream anime torrents, real-time.',
details: 'Stream anime torrents',
state: 'Watching anime',
assets: {
small_image: 'logo',
@ -55,6 +55,10 @@ export default class Discord {
}
})
ipcMain.on('discord-hidden', () => {
this.debouncedDiscordRPC(undefined, true)
})
this.discord.on('ready', async () => {
this.setDiscordRPC(this.cachedPresence || this.defaultStatus)
this.discord.subscribe('ACTIVITY_JOIN_REQUEST')
@ -66,7 +70,7 @@ export default class Discord {
window.webContents.send('w2glink', secret)
})
this.debouncedDiscordRPC = debounce(status => this.setDiscordRPC(status), 4500)
this.debouncedDiscordRPC = debounce((status, logout) => logout ? this.logoutRPC() : this.setDiscordRPC(status), 4500)
}
loginRPC () {