Add Watch Buddies feature
- Created WatchBuddy model - M:N junctions to EntrySeen/EpisodeSeen and Media relation for profile pictures - Full CRUD + search + profile picture upload - WatchBuddySelect multi-select with search and inline create, replaced all seen_with text inputs - WatchBuddiesPage (grid) and WatchBuddyProfilePage (edit form, icon-based contacts, poster-grid watch history, monthly timeline)
This commit is contained in:
1 parent
4313a794ce
commit
e82b905886
28 files changed
+6813
-374
No files matched your search
@@ -1,5 +1,6 @@
|
||||
const Entry = require('../models/Entry');
|
||||
const EntrySeen = require('../models/EntrySeen');
|
||||
const Entry = require("../models/Entry");
|
||||
const EntrySeen = require("../models/EntrySeen");
|
||||
const WatchBuddy = require("../models/WatchBuddy");
|
||||
|
||||
class EntrySeenController {
|
||||
constructor() {
|
||||
@@ -18,34 +19,37 @@ class EntrySeenController {
|
||||
const entrySeen = await EntrySeen.findByPk(req.params.id);
|
||||
|
||||
if (!entrySeen) {
|
||||
res.status(404).json({ message: 'Entry seen record not found.' });
|
||||
res.status(404).json({ message: "Entry seen record not found." });
|
||||
}
|
||||
|
||||
res.status(200).json(entrySeen);
|
||||
}
|
||||
|
||||
async createEntrySeen(req, res) {
|
||||
const { entry_id, date_seen, seen_with } = req.body;
|
||||
const { entry_id, date_seen, seen_with, buddy_ids } = req.body;
|
||||
|
||||
// ? checking the an entry with the given id does exist
|
||||
const entry = await Entry.findByPk(entry_id);
|
||||
if (!entry) {
|
||||
return res.status(404).json({ error: 'Entry not found' });
|
||||
return res.status(404).json({ error: "Entry not found" });
|
||||
}
|
||||
|
||||
// Create a new EntrySeen record
|
||||
const newEntrySeen = await EntrySeen.create({
|
||||
entry_id,
|
||||
date_seen,
|
||||
seen_with,
|
||||
});
|
||||
|
||||
// Link watch buddies
|
||||
if (Array.isArray(buddy_ids) && buddy_ids.length > 0) {
|
||||
await newEntrySeen.setBuddies(buddy_ids);
|
||||
}
|
||||
|
||||
// Auto-upgrade TV series status when first watched
|
||||
if (
|
||||
entry.is_tvseries &&
|
||||
(!entry.watch_status || entry.watch_status === 'plan_to_watch')
|
||||
(!entry.watch_status || entry.watch_status === "plan_to_watch")
|
||||
) {
|
||||
entry.watch_status = 'watching';
|
||||
entry.watch_status = "watching";
|
||||
await entry.save();
|
||||
}
|
||||
|
||||
@@ -56,7 +60,8 @@ class EntrySeenController {
|
||||
const { entryId } = req.params;
|
||||
const entrySeens = await EntrySeen.findAll({
|
||||
where: { entry_id: entryId },
|
||||
order: [['date_seen', 'DESC']],
|
||||
order: [["date_seen", "DESC"]],
|
||||
include: [{ model: WatchBuddy, as: "buddies" }],
|
||||
});
|
||||
|
||||
if (entrySeens.length > 0) {
|
||||
@@ -64,30 +69,34 @@ class EntrySeenController {
|
||||
} else {
|
||||
res
|
||||
.status(404)
|
||||
.json({ message: 'No records found for the given entry ID.' });
|
||||
.json({ message: "No records found for the given entry ID." });
|
||||
}
|
||||
}
|
||||
|
||||
async updateEntrySeen(req, res) {
|
||||
const { date_seen, seen_with } = req.body;
|
||||
const updated = await EntrySeen.update(
|
||||
const { date_seen, seen_with, buddy_ids } = req.body;
|
||||
await EntrySeen.update(
|
||||
{ date_seen, seen_with },
|
||||
{ where: { id: req.params.id } },
|
||||
);
|
||||
|
||||
if (updated[0] > 0) {
|
||||
const updatedEntrySeen = await EntrySeen.findByPk(req.params.id);
|
||||
res.status(200).json(updatedEntrySeen);
|
||||
} else {
|
||||
res.status(404).json({ message: 'Entry seen record not found.' });
|
||||
const record = await EntrySeen.findByPk(req.params.id);
|
||||
if (!record) {
|
||||
return res.status(404).json({ message: "Entry seen record not found." });
|
||||
}
|
||||
|
||||
if (Array.isArray(buddy_ids)) {
|
||||
await record.setBuddies(buddy_ids);
|
||||
}
|
||||
|
||||
res.status(200).json(record);
|
||||
}
|
||||
|
||||
async deleteEntrySeen(req, res) {
|
||||
const entrySeen = await EntrySeen.findByPk(req.params.id);
|
||||
|
||||
if (!entrySeen)
|
||||
res.status(404).json({ message: 'Entry seen record not found.' });
|
||||
res.status(404).json({ message: "Entry seen record not found." });
|
||||
|
||||
const deleted = await entrySeen.destroy();
|
||||
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
const Entry = require('../models/Entry');
|
||||
const Episode = require('../models/Episode');
|
||||
const EpisodeSeen = require('../models/EpisodeSeen');
|
||||
const Season = require('../models/Season');
|
||||
const Entry = require("../models/Entry");
|
||||
const Episode = require("../models/Episode");
|
||||
const EpisodeSeen = require("../models/EpisodeSeen");
|
||||
const Season = require("../models/Season");
|
||||
const WatchBuddy = require("../models/WatchBuddy");
|
||||
|
||||
class EpisodeSeenController {
|
||||
constructor() {
|
||||
@@ -25,7 +26,7 @@ class EpisodeSeenController {
|
||||
const episodeSeen = await EpisodeSeen.findByPk(req.params.id);
|
||||
|
||||
if (!episodeSeen) {
|
||||
res.status(404).json({ message: 'Episode seen record not found.' });
|
||||
res.status(404).json({ message: "Episode seen record not found." });
|
||||
}
|
||||
|
||||
res.status(200).json(episodeSeen);
|
||||
@@ -36,15 +37,15 @@ class EpisodeSeenController {
|
||||
|
||||
async createEpisodeSeen(req, res) {
|
||||
try {
|
||||
const { entry_id, episode_id, date_seen, seen_with } = req.body;
|
||||
const { entry_id, episode_id, date_seen, seen_with, buddy_ids } =
|
||||
req.body;
|
||||
|
||||
const entry = await Entry.findByPk(entry_id);
|
||||
if (!entry) return res.status(404).json({ error: 'Entry not found' });
|
||||
if (!entry) return res.status(404).json({ error: "Entry not found" });
|
||||
|
||||
const episode = await Episode.findByPk(episode_id);
|
||||
if (!episode) return res.status(404).json({ error: 'Episode not found' });
|
||||
if (!episode) return res.status(404).json({ error: "Episode not found" });
|
||||
|
||||
// Create a new EpisodeSeen record
|
||||
const newEpisodeSeen = await EpisodeSeen.create({
|
||||
entry_id,
|
||||
episode_id,
|
||||
@@ -52,12 +53,17 @@ class EpisodeSeenController {
|
||||
seen_with,
|
||||
});
|
||||
|
||||
// Link watch buddies
|
||||
if (Array.isArray(buddy_ids) && buddy_ids.length > 0) {
|
||||
await newEpisodeSeen.setBuddies(buddy_ids);
|
||||
}
|
||||
|
||||
// Auto-upgrade: plan_to_watch or null → watching when first episode is marked
|
||||
if (
|
||||
entry.is_tvseries &&
|
||||
(!entry.watch_status || entry.watch_status === 'plan_to_watch')
|
||||
(!entry.watch_status || entry.watch_status === "plan_to_watch")
|
||||
) {
|
||||
entry.watch_status = 'watching';
|
||||
entry.watch_status = "watching";
|
||||
await entry.save();
|
||||
}
|
||||
|
||||
@@ -73,7 +79,8 @@ class EpisodeSeenController {
|
||||
const { episodeId } = req.params;
|
||||
const episodeSeens = await EpisodeSeen.findAll({
|
||||
where: { episode_id: episodeId },
|
||||
order: [['date_seen', 'DESC']],
|
||||
order: [["date_seen", "DESC"]],
|
||||
include: [{ model: WatchBuddy, as: "buddies" }],
|
||||
});
|
||||
|
||||
if (episodeSeens.length > 0) {
|
||||
@@ -81,7 +88,7 @@ class EpisodeSeenController {
|
||||
} else {
|
||||
res
|
||||
.status(404)
|
||||
.json({ message: 'No records found for the given episode ID.' });
|
||||
.json({ message: "No records found for the given episode ID." });
|
||||
}
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
@@ -99,17 +106,18 @@ class EpisodeSeenController {
|
||||
include: [
|
||||
{
|
||||
model: Season,
|
||||
attributes: ['season_number'],
|
||||
attributes: ["season_number"],
|
||||
},
|
||||
],
|
||||
attributes: ['episode_number'],
|
||||
attributes: ["episode_number"],
|
||||
},
|
||||
{ model: WatchBuddy, as: "buddies" },
|
||||
],
|
||||
order: [
|
||||
['date_seen', 'DESC'],
|
||||
[{ model: Episode }, { model: Season }, 'season_number', 'DESC'],
|
||||
["date_seen", "DESC"],
|
||||
[{ model: Episode }, { model: Season }, "season_number", "DESC"],
|
||||
// [Sequelize.literal('"Episode->Season"."season_number"'), 'DESC'], // ! or this
|
||||
[Episode, 'episode_number', 'DESC'],
|
||||
[Episode, "episode_number", "DESC"],
|
||||
],
|
||||
});
|
||||
|
||||
@@ -122,21 +130,21 @@ class EpisodeSeenController {
|
||||
|
||||
async updateEpisodeSeen(req, res) {
|
||||
try {
|
||||
const { date_seen, seen_with, episode_id } = req.body;
|
||||
const { date_seen, seen_with, episode_id, buddy_ids } = req.body;
|
||||
|
||||
const episodeSeen = await EpisodeSeen.findByPk(req.params.id);
|
||||
if (!episodeSeen)
|
||||
return res
|
||||
.status(404)
|
||||
.json({ message: 'Episode seen record not found.' });
|
||||
.json({ message: "Episode seen record not found." });
|
||||
|
||||
const updated = await episodeSeen.update({ date_seen, seen_with });
|
||||
await episodeSeen.update({ date_seen, seen_with });
|
||||
|
||||
if (updated) {
|
||||
res.status(200).json(updated);
|
||||
} else {
|
||||
res.status(404).json({ message: 'Episode seen record not found.' });
|
||||
if (Array.isArray(buddy_ids)) {
|
||||
await episodeSeen.setBuddies(buddy_ids);
|
||||
}
|
||||
|
||||
res.status(200).json(episodeSeen);
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
@@ -147,7 +155,7 @@ class EpisodeSeenController {
|
||||
// ? getting the episode seen object
|
||||
const oldEpisodeSeen = await EpisodeSeen.findByPk(req.params.id);
|
||||
if (!oldEpisodeSeen)
|
||||
res.status(404).json({ message: 'Episode seen record not found.' });
|
||||
res.status(404).json({ message: "Episode seen record not found." });
|
||||
|
||||
// ?deleting the episode seen
|
||||
const deleted = await oldEpisodeSeen.destroy();
|
||||
|
||||
@@ -52,6 +52,7 @@ class MediaController {
|
||||
"seasons",
|
||||
"episodes",
|
||||
"peoples",
|
||||
"watch-buddies",
|
||||
].includes(category)
|
||||
) {
|
||||
return res.status(404).send("Category Not Found");
|
||||
|
||||
@@ -0,0 +1,205 @@
|
||||
const WatchBuddy = require("../models/WatchBuddy");
|
||||
const EntrySeen = require("../models/EntrySeen");
|
||||
const EpisodeSeen = require("../models/EpisodeSeen");
|
||||
const Media = require("../models/Media");
|
||||
const { Op } = require("sequelize");
|
||||
const path = require("path");
|
||||
const fs = require("fs");
|
||||
const { v4: uuidv4 } = require("uuid");
|
||||
const configService = require("../services/configService");
|
||||
const mediaOptimizerService = require("../services/mediaOptimizerService");
|
||||
|
||||
class WatchBuddyController {
|
||||
async search(req, res) {
|
||||
const { q } = req.query;
|
||||
const include = [
|
||||
{
|
||||
model: Media,
|
||||
as: "medias",
|
||||
required: false,
|
||||
},
|
||||
];
|
||||
if (!q) {
|
||||
const all = await WatchBuddy.findAll({
|
||||
include,
|
||||
order: [["name", "ASC"]],
|
||||
});
|
||||
return res.json(all);
|
||||
}
|
||||
const results = await WatchBuddy.findAll({
|
||||
where: { name: { [Op.like]: `%${q}%` } },
|
||||
include,
|
||||
order: [["name", "ASC"]],
|
||||
limit: 20,
|
||||
});
|
||||
res.json(results);
|
||||
}
|
||||
|
||||
async getById(req, res) {
|
||||
const buddy = await WatchBuddy.findByPk(req.params.id, {
|
||||
include: [
|
||||
{
|
||||
model: Media,
|
||||
as: "medias",
|
||||
required: false,
|
||||
},
|
||||
],
|
||||
});
|
||||
if (!buddy) return res.status(404).json({ error: "Not found" });
|
||||
res.json(buddy);
|
||||
}
|
||||
|
||||
async getHistory(req, res) {
|
||||
const buddy = await WatchBuddy.findByPk(req.params.id, {
|
||||
include: [
|
||||
{
|
||||
model: EntrySeen,
|
||||
as: "entrySeens",
|
||||
include: [
|
||||
{
|
||||
model: require("../models/Entry"),
|
||||
as: "entry",
|
||||
attributes: ["id", "title", "is_tvseries", "poster_path"],
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
model: EpisodeSeen,
|
||||
as: "episodeSeens",
|
||||
include: [
|
||||
{
|
||||
model: require("../models/Episode"),
|
||||
attributes: ["name", "episode_number"],
|
||||
include: [
|
||||
{
|
||||
model: require("../models/Season"),
|
||||
attributes: ["id", "entry_id", "season_number"],
|
||||
include: [
|
||||
{
|
||||
model: require("../models/Entry"),
|
||||
as: "entry",
|
||||
attributes: ["id", "title", "poster_path"],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
});
|
||||
if (!buddy) return res.status(404).json({ error: "Not found" });
|
||||
res.json({
|
||||
entrySeens: buddy.entrySeens || [],
|
||||
episodeSeens: buddy.episodeSeens || [],
|
||||
});
|
||||
}
|
||||
|
||||
async create(req, res) {
|
||||
const { name, gender, instagram, discord, phone, email, note, buddy_type } =
|
||||
req.body;
|
||||
if (!name) return res.status(400).json({ error: "Name is required" });
|
||||
const buddy = await WatchBuddy.create({
|
||||
name,
|
||||
gender,
|
||||
instagram,
|
||||
discord,
|
||||
phone,
|
||||
email,
|
||||
note,
|
||||
buddy_type,
|
||||
});
|
||||
res.status(201).json(buddy);
|
||||
}
|
||||
|
||||
async update(req, res) {
|
||||
const buddy = await WatchBuddy.findByPk(req.params.id);
|
||||
if (!buddy) return res.status(404).json({ error: "Not found" });
|
||||
const { name, gender, instagram, discord, phone, email, note, buddy_type } =
|
||||
req.body;
|
||||
await buddy.update({
|
||||
name,
|
||||
gender,
|
||||
instagram,
|
||||
discord,
|
||||
phone,
|
||||
email,
|
||||
note,
|
||||
buddy_type,
|
||||
});
|
||||
res.json(buddy);
|
||||
}
|
||||
|
||||
async remove(req, res) {
|
||||
const buddy = await WatchBuddy.findByPk(req.params.id);
|
||||
if (!buddy) return res.status(404).json({ error: "Not found" });
|
||||
await buddy.destroy();
|
||||
res.status(204).send();
|
||||
}
|
||||
|
||||
async uploadProfilePicture(req, res) {
|
||||
const buddy = await WatchBuddy.findByPk(req.params.id);
|
||||
if (!buddy) return res.status(404).json({ error: "Not found" });
|
||||
|
||||
if (!req.file) {
|
||||
return res.status(400).json({ error: "No file uploaded" });
|
||||
}
|
||||
|
||||
const settings = configService.getSettings();
|
||||
const dataPath = settings.userDataPath;
|
||||
const dir = path.join(dataPath, "images", "watch-buddies", "profiles");
|
||||
fs.mkdirSync(dir, { recursive: true });
|
||||
|
||||
const ext = path.extname(req.file.originalname);
|
||||
const fileName = `${uuidv4()}${ext}`;
|
||||
const destPath = path.join(dir, fileName);
|
||||
|
||||
fs.copyFileSync(req.file.path, destPath);
|
||||
fs.unlinkSync(req.file.path); // remove temp file
|
||||
|
||||
// Generate preview
|
||||
try {
|
||||
await mediaOptimizerService.doGeneratePreview(
|
||||
"watch-buddies",
|
||||
"profiles",
|
||||
fileName,
|
||||
);
|
||||
} catch {
|
||||
// preview generation failure is non-fatal
|
||||
}
|
||||
|
||||
// Delete old profile picture if exists
|
||||
const oldMedia = await Media.findOne({
|
||||
where: {
|
||||
mediaable: "watch_buddy",
|
||||
mediaableId: buddy.id,
|
||||
type: "profiles",
|
||||
},
|
||||
});
|
||||
if (oldMedia) {
|
||||
const oldPath = path.join(dir, oldMedia.path);
|
||||
const previewName = oldMedia.path.split(".")[0] + "_preview.jpg";
|
||||
try {
|
||||
fs.unlinkSync(oldPath);
|
||||
} catch {}
|
||||
try {
|
||||
fs.unlinkSync(path.join(dir, previewName));
|
||||
} catch {}
|
||||
await oldMedia.destroy();
|
||||
}
|
||||
|
||||
const media = await Media.create({
|
||||
mediaable: "watch_buddy",
|
||||
mediaableId: buddy.id,
|
||||
type: "profiles",
|
||||
path: fileName,
|
||||
isLocal: true,
|
||||
isCustom: true,
|
||||
isSelected: true,
|
||||
});
|
||||
|
||||
res.json({ media });
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = new WatchBuddyController();
|
||||
Reference in new issue
Block a user