refactor: move are you still there functionality into AIOStreams

This commit is contained in:
Viren070
2025-11-24 14:55:52 +00:00
parent 86ae86a272
commit c827c7f3a0
2 changed files with 61 additions and 43 deletions
+57
View File
@@ -1094,6 +1094,63 @@ export class AIOStreams {
return this.addons.find((a) => a.instanceId === instanceId);
}
public async shouldStopAutoPlay(type: string, id: string): Promise<boolean> {
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<string, { count: number; lastAt: number }>(
'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 =
+4 -43
View File
@@ -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 }
)
);