Add watch status for TV series with UI and auto-fix

Added watch_status column to Entry model (plan_to_watch/watching/completed/
on_hold/dropped). Status pill on entry details page opens selection dialog.
Set Status in quick actions menu and EntryCard dropdown (TV only).
Status filter in Library filter popover. Maintenance auto-fix endpoint
that sets statuses based on watched episodes (excl. season 0).
This commit is contained in:
X4Lo committed 2026-06-23 03:39:16 +01:00
1 parent a5659cf4c8
commit cdbaedd7d0
11 files changed
+663 -286

No files matched your search

+142 -56
View File
@@ -1,14 +1,14 @@
const path = require("path");
const fsPromises = require("fs").promises;
const fs = require("fs");
const path = require('path');
const fsPromises = require('fs').promises;
const fs = require('fs');
const Media = require("../models/Media");
const People = require("../models/People");
const Credit = require("../models/Credit");
const Entry = require("../models/Entry");
const Media = require('../models/Media');
const People = require('../models/People');
const Credit = require('../models/Credit');
const Entry = require('../models/Entry');
const { logToFile } = require("./logService");
const configService = require("./configService");
const { logToFile } = require('./logService');
const configService = require('./configService');
const _configService = configService;
@@ -32,30 +32,30 @@ class MaintenanceService {
const dataPath = await _configService.getMediaFolder();
const mediaTypeFolder = {
"entry;poster": path.join(dataPath, "images", "entries", "posters"),
"entry;logo": path.join(dataPath, "images", "entries", "logos"),
"entry;backdrop": path.join(dataPath, "images", "entries", "backdrops"),
'entry;poster': path.join(dataPath, 'images', 'entries', 'posters'),
'entry;logo': path.join(dataPath, 'images', 'entries', 'logos'),
'entry;backdrop': path.join(dataPath, 'images', 'entries', 'backdrops'),
"collection;poster": path.join(
'collection;poster': path.join(
dataPath,
"images",
"collections",
"posters",
'images',
'collections',
'posters',
),
"collection;backdrop": path.join(
'collection;backdrop': path.join(
dataPath,
"images",
"collections",
"backdrops",
'images',
'collections',
'backdrops',
),
"season;poster": path.join(dataPath, "images", "seasons", "posters"),
"episode;still": path.join(dataPath, "images", "episodes", "stills"),
'season;poster': path.join(dataPath, 'images', 'seasons', 'posters'),
'episode;still': path.join(dataPath, 'images', 'episodes', 'stills'),
};
let missingFiles = [];
for (let i = 0; i < allMedia.length; i++) {
const folderType = `${allMedia[i].mediaable};${allMedia[i].type.replace("_preview", "")}`;
const folderType = `${allMedia[i].mediaable};${allMedia[i].type.replace('_preview', '')}`;
if (!mediaTypeFolder[folderType]) continue;
@@ -73,7 +73,7 @@ class MaintenanceService {
};
for (let i = 0; i < missingFiles.length; i++) {
removed[missingFiles[i].type + "s"] += 1;
removed[missingFiles[i].type + 's'] += 1;
if (!dry) missingFiles[i].destroy();
}
@@ -100,31 +100,31 @@ class MaintenanceService {
// ? Getting the name (without extention) of all media in database
const allMedia = (await Media.findAll()).map((m) => {
return m.path.split(".")[0];
return m.path.split('.')[0];
});
// ? Loop trough the files of the images folder and check
// ? if a file doesnt exist in the allMedia array and mark it to delete
const mediaTypeFolder = {
"entry;poster": path.join(dataPath, "images", "entries", "posters"),
"entry;logo": path.join(dataPath, "images", "entries", "logos"),
"entry;backdrop": path.join(dataPath, "images", "entries", "backdrops"),
'entry;poster': path.join(dataPath, 'images', 'entries', 'posters'),
'entry;logo': path.join(dataPath, 'images', 'entries', 'logos'),
'entry;backdrop': path.join(dataPath, 'images', 'entries', 'backdrops'),
"collection;poster": path.join(
'collection;poster': path.join(
dataPath,
"images",
"collections",
"posters",
'images',
'collections',
'posters',
),
"collection;backdrop": path.join(
'collection;backdrop': path.join(
dataPath,
"images",
"collections",
"backdrops",
'images',
'collections',
'backdrops',
),
"season;poster": path.join(dataPath, "images", "seasons", "posters"),
"episode;still": path.join(dataPath, "images", "episodes", "stills"),
'season;poster': path.join(dataPath, 'images', 'seasons', 'posters'),
'episode;still': path.join(dataPath, 'images', 'episodes', 'stills'),
};
// * counter
@@ -146,12 +146,12 @@ class MaintenanceService {
// Check each file in the directory
for (let file of files) {
// preparing the file name
const fileName = file.split(".")[0].replace("_preview", "");
const fileName = file.split('.')[0].replace('_preview', '');
const isInDatabase = allMedia.includes(fileName);
// if the file isn't in database even on other extentions then delete it
if (!isInDatabase) {
removed[folderType.split(";")[1] + "s"]++; // Increment the count of removed files
removed[folderType.split(';')[1] + 's']++; // Increment the count of removed files
if (!dry) {
const filePath = path.join(directory, file);
await fsPromises.unlink(filePath);
@@ -182,14 +182,14 @@ class MaintenanceService {
* @param {boolean} [dry=false] - if true only return the count
*/
async deleteOrphanPeople(dry = false) {
const { Sequelize } = require("sequelize");
const { Sequelize } = require('sequelize');
// Find all people who have no credits and are not pinned
const allPeople = await People.findAll({
include: [
{
model: Credit,
as: "roles",
as: 'roles',
required: false,
},
],
@@ -222,9 +222,9 @@ class MaintenanceService {
include: [
{
model: Credit,
as: "credits",
where: { cast_type: "crew", job: "Director" },
include: [{ model: People, as: "person", attributes: ["name"] }],
as: 'credits',
where: { cast_type: 'crew', job: 'Director' },
include: [{ model: People, as: 'person', attributes: ['name'] }],
required: false,
},
],
@@ -257,20 +257,20 @@ class MaintenanceService {
* @param {boolean} [dry=false] - if true only count, if false also delete
*/
async cleanDuplicateCredits(dry = false) {
const { Sequelize } = require("sequelize");
const { Sequelize } = require('sequelize');
const duplicates = await Credit.findAll({
attributes: [
"entry_id",
"people_id",
"cast_type",
"character",
"job",
[Sequelize.fn("COUNT", Sequelize.col("id")), "cnt"],
[Sequelize.fn("GROUP_CONCAT", Sequelize.col("id")), "ids"],
'entry_id',
'people_id',
'cast_type',
'character',
'job',
[Sequelize.fn('COUNT', Sequelize.col('id')), 'cnt'],
[Sequelize.fn('GROUP_CONCAT', Sequelize.col('id')), 'ids'],
],
group: ["entry_id", "people_id", "cast_type", "character", "job"],
having: Sequelize.literal("COUNT(id) > 1"),
group: ['entry_id', 'people_id', 'cast_type', 'character', 'job'],
having: Sequelize.literal('COUNT(id) > 1'),
raw: true,
});
@@ -280,7 +280,7 @@ class MaintenanceService {
if (!dry) {
for (const row of duplicates) {
const idList = row.ids
.split(",")
.split(',')
.map(Number)
.sort((a, b) => a - b);
// Keep the newest (highest ID), delete the older ones
@@ -310,6 +310,92 @@ class MaintenanceService {
deleted: deletedCount,
};
}
async autoFixWatchStatuses() {
const Episode = require('../models/Episode');
const EpisodeSeen = require('../models/EpisodeSeen');
const Season = require('../models/Season');
const { Op } = require('sequelize');
const tvEntries = await Entry.findAll({
where: { is_tvseries: true },
include: [
{
model: Season,
as: 'seasons',
where: { season_number: { [Op.gt]: 0 } },
include: [
{
model: Episode,
as: 'episodes',
include: [{ model: EpisodeSeen, as: 'episodeSeens' }],
},
],
},
],
});
let toCompleted = 0;
let toWatching = 0;
let toPlanToWatch = 0;
for (const entry of tvEntries) {
let totalEpisodes = 0;
let watchedEpisodes = 0;
for (const season of entry.seasons || []) {
for (const ep of season.episodes || []) {
totalEpisodes++;
if (ep.episodeSeens && ep.episodeSeens.length > 0) {
watchedEpisodes++;
}
}
}
const allWatched = totalEpisodes > 0 && watchedEpisodes === totalEpisodes;
const hasSomeWatched = watchedEpisodes > 0;
// Completed but not all watched → downgrade to watching
if (entry.watch_status === 'completed' && !allWatched) {
entry.watch_status = 'watching';
await entry.save();
toWatching++;
continue;
}
// All watched → completed (unless already completed)
if (allWatched && entry.watch_status !== 'completed') {
entry.watch_status = 'completed';
await entry.save();
toCompleted++;
continue;
}
// Has some watched but no status → watching
if (hasSomeWatched && !entry.watch_status) {
entry.watch_status = 'watching';
await entry.save();
toWatching++;
continue;
}
// In watchlist with no status → plan to watch
if (entry.watchlist && !entry.watch_status) {
entry.watch_status = 'plan_to_watch';
await entry.save();
toPlanToWatch++;
}
}
logToFile(
`Auto-fixed watch statuses: ${toCompleted} completed, ${toWatching} watching, ${toPlanToWatch} plan_to_watch`,
);
return {
completed: toCompleted,
watching: toWatching,
planToWatch: toPlanToWatch,
};
}
}
module.exports = new MaintenanceService();