Files
Showy/backend/controllers/watchBuddyController.js
X4Lo e82b905886 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)
2026-06-25 03:11:14 +01:00

206 lines
5.3 KiB
JavaScript

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();