From c827c7f3a02d3961decef0433d312f27c3f1cce9 Mon Sep 17 00:00:00 2001 From: Viren070 Date: Mon, 24 Nov 2025 14:55:52 +0000 Subject: [PATCH] refactor: move are you still there functionality into AIOStreams --- packages/core/src/main.ts | 57 ++++++++++++++++++++ packages/server/src/routes/stremio/stream.ts | 47 ++-------------- 2 files changed, 61 insertions(+), 43 deletions(-) diff --git a/packages/core/src/main.ts b/packages/core/src/main.ts index a173df5b..37683926 100644 --- a/packages/core/src/main.ts +++ b/packages/core/src/main.ts @@ -1094,6 +1094,63 @@ export class AIOStreams { return this.addons.find((a) => a.instanceId === instanceId); } + public async shouldStopAutoPlay(type: string, id: string): Promise { + if ( + !this.userData.areYouStillThere?.enabled || + !this.userData.uuid || + type !== 'series' + ) { + return false; + } + logger.info(`Determining if autoplay should be stopped`, { + type, + id, + uuid: this.userData.uuid, + }); + // Decide whether to disable autoplay (suppress bingeGroup) per user+show + let disableAutoplay = false; + + const cfg = this.userData.areYouStillThere; + const threshold = cfg.episodesBeforeCheck ?? 3; + const cooldownMs = (cfg.cooldownMinutes ?? 60) * 60 * 1000; + const cache = Cache.getInstance( + 'ays', + 10000, + Env.REDIS_URI ? undefined : 'sql' + ); + const parsed = IdParser.parse(id, type); + const baseSeriesKey = parsed + ? `${parsed.type}:${parsed.value}` + : id.split(':')[0] || id; + const key = `${this.userData.uuid}:${baseSeriesKey}`; + logger.debug(`Formed AYS cache key: ${key}`); + const now = Date.now(); + const prev = (await cache.get(key)) || { count: 0, lastAt: 0 }; + const withinWindow = now - prev.lastAt <= cooldownMs; + const nextCount = withinWindow ? prev.count + 1 : 1; + if (nextCount >= threshold) { + // Trigger: disable autoplay for this response and reset counter + disableAutoplay = true; + await cache.set( + key, + { count: 0, lastAt: now }, + Math.ceil(cooldownMs / 1000) + ); + } else { + await cache.set( + key, + { count: nextCount, lastAt: now }, + Math.ceil(cooldownMs / 1000) + ); + } + logger.info(`Autoplay disable check result`, { + disableAutoplay, + count: nextCount, + withinWindow, + }); + return disableAutoplay; + } + private async getProxyIp() { let userIp = this.userData.ip; const PRIVATE_IP_REGEX = diff --git a/packages/server/src/routes/stremio/stream.ts b/packages/server/src/routes/stremio/stream.ts index 330f377a..8c2f3e39 100644 --- a/packages/server/src/routes/stremio/stream.ts +++ b/packages/server/src/routes/stremio/stream.ts @@ -45,54 +45,15 @@ router.get( try { const { type, id } = req.params; - // Decide whether to disable autoplay (suppress bingeGroup) per user+show - let disableAutoplay = false; - if ( - req.userData?.areYouStillThere?.enabled && - type === 'series' && - req.uuid - ) { - const cfg = req.userData.areYouStillThere; - const threshold = cfg.episodesBeforeCheck ?? 3; - const cooldownMs = (cfg.cooldownMinutes ?? 60) * 60 * 1000; - const cache = Cache.getInstance< - string, - { count: number; lastAt: number } - >('areYouStillThere', 10000); - // Use parsed series identifier (per show) via IdParser to avoid mis-parsing - const parsed = IdParser.parse(id, type); - const baseSeriesKey = parsed - ? `${parsed.type}:${parsed.value}` - : id.split(':')[0] || id; - const key = `ays:${req.uuid}:${baseSeriesKey}`; - const now = Date.now(); - const prev = (await cache.get(key)) || { count: 0, lastAt: 0 }; - const withinWindow = now - prev.lastAt <= cooldownMs; - const nextCount = withinWindow ? prev.count + 1 : 1; - if (nextCount >= threshold) { - // Trigger: disable autoplay for this response and reset counter - disableAutoplay = true; - await cache.set( - key, - { count: 0, lastAt: now }, - Math.ceil(cooldownMs / 1000) - ); - } else { - await cache.set( - key, - { count: nextCount, lastAt: now }, - Math.ceil(cooldownMs / 1000) - ); - } - } + const aiostreams = await new AIOStreams(req.userData).initialise(); + + const disableAutoplay = await aiostreams.shouldStopAutoPlay(type, id); res .status(200) .json( await transformer.transformStreams( - await ( - await new AIOStreams(req.userData).initialise() - ).getStreams(id, type), + await aiostreams.getStreams(id, type), { provideStreamData, disableAutoplay } ) );