Add history page with list, timeline and year in review
- Backend: /history/years and /history/:year return every movie and episode watch in a year, with entry posters, genres, networks and buddies - History tab: watches by month and day, back-to-back episodes grouped - Timeline tab: latest to oldest, month dividers, stacked binge sessions - Year in Review tab: hours, top shows and genres, streaks, busiest day, biggest binge, monthly and weekday charts, buddies and new shows - Year picker, movies/TV filter, state kept in the URL; unknown-date watches badged and excluded from the review by default - History link in the sidebar and command palette
This commit is contained in:
1 parent
c4aa0488d4
commit
760afd8e84
13 files changed
+1724
No files matched your search
@@ -0,0 +1,208 @@
|
||||
const { Op } = require("sequelize");
|
||||
const sequelize = require("../config/database");
|
||||
|
||||
const Entry = require("../models/Entry");
|
||||
const EntryRating = require("../models/EntryRating");
|
||||
const EntrySeen = require("../models/EntrySeen");
|
||||
const Episode = require("../models/Episode");
|
||||
const EpisodeSeen = require("../models/EpisodeSeen");
|
||||
const Genre = require("../models/Genre");
|
||||
const Media = require("../models/Media");
|
||||
const Network = require("../models/Network");
|
||||
const Season = require("../models/Season");
|
||||
const WatchBuddy = require("../models/WatchBuddy");
|
||||
|
||||
const BadRequestError = require("../errors/BadRequestError");
|
||||
|
||||
const ENTRY_ATTRIBUTES = [
|
||||
"id",
|
||||
"title",
|
||||
"is_tvseries",
|
||||
"poster_path",
|
||||
"banner_path",
|
||||
"runtime",
|
||||
"release_date",
|
||||
"original_language",
|
||||
];
|
||||
|
||||
const BUDDY_INCLUDE = {
|
||||
model: WatchBuddy,
|
||||
as: "buddies",
|
||||
attributes: ["id", "name"],
|
||||
through: { attributes: [] },
|
||||
};
|
||||
|
||||
class HistoryService {
|
||||
constructor() {
|
||||
if (!HistoryService.instance) {
|
||||
HistoryService.instance = this;
|
||||
}
|
||||
return HistoryService.instance;
|
||||
}
|
||||
|
||||
/** Years that have watches, newest first, with movie/episode counts. */
|
||||
async getYears() {
|
||||
const [rows] = await sequelize.query(`
|
||||
SELECT y AS year, SUM(movies) AS movies, SUM(episodes) AS episodes FROM (
|
||||
SELECT YEAR(date_seen) AS y, COUNT(*) AS movies, 0 AS episodes
|
||||
FROM entrySeens WHERE date_seen IS NOT NULL GROUP BY y
|
||||
UNION ALL
|
||||
SELECT YEAR(date_seen) AS y, 0 AS movies, COUNT(*) AS episodes
|
||||
FROM episodeSeens WHERE date_seen IS NOT NULL GROUP BY y
|
||||
) t GROUP BY y ORDER BY y DESC
|
||||
`);
|
||||
return rows.map((r) => ({
|
||||
year: Number(r.year),
|
||||
movies: Number(r.movies),
|
||||
episodes: Number(r.episodes),
|
||||
}));
|
||||
}
|
||||
|
||||
/**
|
||||
* Every watch in a year, newest first. Entries are returned once in
|
||||
* `entries` (keyed by id) and referenced by `entry_id` from each event.
|
||||
* @param {number} year
|
||||
*/
|
||||
async getYear(year) {
|
||||
year = Number(year);
|
||||
if (!Number.isInteger(year) || year < 1900 || year > 3000) {
|
||||
throw new BadRequestError("Invalid year");
|
||||
}
|
||||
const range = {
|
||||
[Op.gte]: new Date(year, 0, 1),
|
||||
[Op.lt]: new Date(year + 1, 0, 1),
|
||||
};
|
||||
|
||||
const [movieSeens, episodeSeens] = await Promise.all([
|
||||
EntrySeen.findAll({
|
||||
where: { date_seen: range },
|
||||
attributes: ["id", "entry_id", "date_seen", "accuracy"],
|
||||
include: [BUDDY_INCLUDE],
|
||||
}),
|
||||
EpisodeSeen.findAll({
|
||||
where: { date_seen: range },
|
||||
attributes: ["id", "entry_id", "episode_id", "date_seen", "accuracy"],
|
||||
include: [
|
||||
BUDDY_INCLUDE,
|
||||
{
|
||||
model: Episode,
|
||||
attributes: [
|
||||
"id",
|
||||
"name",
|
||||
"episode_number",
|
||||
"runtime",
|
||||
"still_path",
|
||||
"season_id",
|
||||
],
|
||||
include: [
|
||||
{ model: Season, as: "season", attributes: ["season_number"] },
|
||||
],
|
||||
},
|
||||
],
|
||||
}),
|
||||
]);
|
||||
|
||||
const entryIds = [
|
||||
...new Set([
|
||||
...movieSeens.map((s) => s.entry_id),
|
||||
...episodeSeens.map((s) => s.entry_id),
|
||||
]),
|
||||
].filter(Boolean);
|
||||
|
||||
const entries = entryIds.length
|
||||
? await Entry.findAll({
|
||||
where: { id: entryIds },
|
||||
attributes: ENTRY_ATTRIBUTES,
|
||||
include: [
|
||||
{
|
||||
model: Media,
|
||||
as: "medias",
|
||||
attributes: ["id", "type", "path", "isSelected"],
|
||||
where: { type: "poster", isSelected: true },
|
||||
required: false,
|
||||
},
|
||||
{
|
||||
model: Genre,
|
||||
as: "genres",
|
||||
attributes: ["id", "name"],
|
||||
through: { attributes: [] },
|
||||
},
|
||||
{
|
||||
model: Network,
|
||||
as: "networks",
|
||||
attributes: ["id", "name", "logoPath"],
|
||||
through: { attributes: [] },
|
||||
},
|
||||
{
|
||||
model: EntryRating,
|
||||
as: "entryRating",
|
||||
attributes: ["user_rating", "tmdb_rating"],
|
||||
},
|
||||
],
|
||||
})
|
||||
: [];
|
||||
|
||||
const buddies = (seen) =>
|
||||
(seen.buddies ?? []).map((b) => ({ id: b.id, name: b.name }));
|
||||
|
||||
const events = [
|
||||
...movieSeens.map((s) => ({
|
||||
id: `m-${s.id}`,
|
||||
seen_id: s.id,
|
||||
kind: "movie",
|
||||
entry_id: s.entry_id,
|
||||
date_seen: s.date_seen,
|
||||
accuracy: s.accuracy,
|
||||
buddies: buddies(s),
|
||||
})),
|
||||
...episodeSeens.map((s) => {
|
||||
const ep = s.episode;
|
||||
return {
|
||||
id: `e-${s.id}`,
|
||||
seen_id: s.id,
|
||||
kind: "episode",
|
||||
entry_id: s.entry_id,
|
||||
date_seen: s.date_seen,
|
||||
accuracy: s.accuracy,
|
||||
buddies: buddies(s),
|
||||
episode: ep
|
||||
? {
|
||||
id: ep.id,
|
||||
name: ep.name,
|
||||
season_id: ep.season_id,
|
||||
season_number: ep.season?.season_number ?? null,
|
||||
episode_number: ep.episode_number,
|
||||
runtime: ep.runtime,
|
||||
still_path: ep.still_path,
|
||||
}
|
||||
: null,
|
||||
};
|
||||
}),
|
||||
].sort((a, b) => new Date(b.date_seen) - new Date(a.date_seen));
|
||||
|
||||
return {
|
||||
year,
|
||||
entries: Object.fromEntries(
|
||||
entries.map((e) => {
|
||||
const plain = e.get({ plain: true });
|
||||
return [
|
||||
plain.id,
|
||||
{
|
||||
...plain,
|
||||
genres: plain.genres?.map((g) => g.name) ?? [],
|
||||
networks:
|
||||
plain.networks?.map((n) => ({
|
||||
id: n.id,
|
||||
name: n.name,
|
||||
logo_path: n.logoPath,
|
||||
})) ?? [],
|
||||
},
|
||||
];
|
||||
}),
|
||||
),
|
||||
events,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = new HistoryService();
|
||||
Reference in new issue
Block a user