2024-08-11 18:53:30 +02:00
|
|
|
const { Request, Response } = require('express')
|
2021-11-22 03:00:40 +01:00
|
|
|
const Logger = require('../Logger')
|
2022-11-24 22:53:58 +01:00
|
|
|
const SocketAuthority = require('../SocketAuthority')
|
2023-07-05 01:14:44 +02:00
|
|
|
const Database = require('../Database')
|
2022-08-14 17:24:41 +02:00
|
|
|
const { sort } = require('../libs/fastSort')
|
2024-08-11 19:16:45 +02:00
|
|
|
const { toNumber, isNullOrNaN } = require('../utils/index')
|
2023-12-20 00:19:33 +01:00
|
|
|
const userStats = require('../utils/queries/userStats')
|
2021-11-22 03:00:40 +01:00
|
|
|
|
2024-08-11 18:53:30 +02:00
|
|
|
/**
|
|
|
|
* @typedef RequestUserObjects
|
|
|
|
* @property {import('../models/User')} userNew
|
|
|
|
* @property {import('../objects/user/User')} user
|
|
|
|
*
|
|
|
|
* @typedef {Request & RequestUserObjects} RequestWithUser
|
|
|
|
*/
|
|
|
|
|
2021-11-22 03:00:40 +01:00
|
|
|
class MeController {
|
2024-05-23 23:32:34 +02:00
|
|
|
constructor() {}
|
2021-11-22 03:00:40 +01:00
|
|
|
|
2024-08-11 18:53:30 +02:00
|
|
|
/**
|
|
|
|
* GET: /api/me
|
|
|
|
*
|
|
|
|
* @param {RequestWithUser} req
|
|
|
|
* @param {Response} res
|
|
|
|
*/
|
2023-02-05 23:52:17 +01:00
|
|
|
getCurrentUser(req, res) {
|
2024-08-11 18:53:30 +02:00
|
|
|
res.json(req.userNew.toOldJSONForBrowser())
|
2023-02-05 23:52:17 +01:00
|
|
|
}
|
|
|
|
|
2024-08-11 18:53:30 +02:00
|
|
|
/**
|
|
|
|
* GET: /api/me/listening-sessions
|
|
|
|
*
|
2024-08-11 20:09:53 +02:00
|
|
|
* @this import('../routers/ApiRouter')
|
|
|
|
*
|
2024-08-11 18:53:30 +02:00
|
|
|
* @param {RequestWithUser} req
|
|
|
|
* @param {Response} res
|
|
|
|
*/
|
2021-11-22 03:00:40 +01:00
|
|
|
async getListeningSessions(req, res) {
|
2024-08-11 18:53:30 +02:00
|
|
|
const listeningSessions = await this.getUserListeningSessionsHelper(req.userNew.id)
|
2022-06-04 17:52:37 +02:00
|
|
|
|
|
|
|
const itemsPerPage = toNumber(req.query.itemsPerPage, 10) || 10
|
|
|
|
const page = toNumber(req.query.page, 0)
|
|
|
|
|
|
|
|
const start = page * itemsPerPage
|
|
|
|
const sessions = listeningSessions.slice(start, start + itemsPerPage)
|
|
|
|
|
|
|
|
const payload = {
|
|
|
|
total: listeningSessions.length,
|
|
|
|
numPages: Math.ceil(listeningSessions.length / itemsPerPage),
|
|
|
|
page,
|
|
|
|
itemsPerPage,
|
|
|
|
sessions
|
|
|
|
}
|
|
|
|
|
|
|
|
res.json(payload)
|
2021-11-22 03:00:40 +01:00
|
|
|
}
|
|
|
|
|
2024-05-23 23:32:34 +02:00
|
|
|
/**
|
|
|
|
* GET: /api/me/item/listening-sessions/:libraryItemId/:episodeId
|
|
|
|
*
|
|
|
|
* @this import('../routers/ApiRouter')
|
|
|
|
*
|
2024-08-11 18:53:30 +02:00
|
|
|
* @param {RequestWithUser} req
|
|
|
|
* @param {Response} res
|
2024-05-23 23:32:34 +02:00
|
|
|
*/
|
2024-05-14 10:51:50 +02:00
|
|
|
async getItemListeningSessions(req, res) {
|
|
|
|
const libraryItem = await Database.libraryItemModel.findByPk(req.params.libraryItemId)
|
|
|
|
const episode = await Database.podcastEpisodeModel.findByPk(req.params.episodeId)
|
2024-05-05 13:14:30 +02:00
|
|
|
|
2024-05-23 23:32:34 +02:00
|
|
|
if (!libraryItem || (libraryItem.mediaType === 'podcast' && !episode)) {
|
2024-05-23 23:35:36 +02:00
|
|
|
Logger.error(`[MeController] Media item not found for library item id "${req.params.libraryItemId}"`)
|
|
|
|
return res.sendStatus(404)
|
2024-05-05 13:14:30 +02:00
|
|
|
}
|
|
|
|
|
2024-05-23 23:35:36 +02:00
|
|
|
const mediaItemId = episode?.id || libraryItem.mediaId
|
2024-08-11 18:53:30 +02:00
|
|
|
let listeningSessions = await this.getUserItemListeningSessionsHelper(req.userNew.id, mediaItemId)
|
2024-05-05 13:14:30 +02:00
|
|
|
|
|
|
|
const itemsPerPage = toNumber(req.query.itemsPerPage, 10) || 10
|
|
|
|
const page = toNumber(req.query.page, 0)
|
|
|
|
|
|
|
|
const start = page * itemsPerPage
|
|
|
|
const sessions = listeningSessions.slice(start, start + itemsPerPage)
|
|
|
|
|
|
|
|
const payload = {
|
|
|
|
total: listeningSessions.length,
|
|
|
|
numPages: Math.ceil(listeningSessions.length / itemsPerPage),
|
|
|
|
page,
|
|
|
|
itemsPerPage,
|
|
|
|
sessions
|
|
|
|
}
|
|
|
|
|
|
|
|
res.json(payload)
|
|
|
|
}
|
|
|
|
|
2024-08-11 18:53:30 +02:00
|
|
|
/**
|
|
|
|
* GET: /api/me/listening-stats
|
|
|
|
*
|
2024-08-11 20:09:53 +02:00
|
|
|
* @this import('../routers/ApiRouter')
|
|
|
|
*
|
2024-08-11 18:53:30 +02:00
|
|
|
* @param {RequestWithUser} req
|
|
|
|
* @param {Response} res
|
|
|
|
*/
|
2021-11-22 03:00:40 +01:00
|
|
|
async getListeningStats(req, res) {
|
2024-08-11 18:53:30 +02:00
|
|
|
const listeningStats = await this.getUserListeningStatsHelpers(req.userNew.id)
|
2021-11-22 03:00:40 +01:00
|
|
|
res.json(listeningStats)
|
|
|
|
}
|
|
|
|
|
2024-08-11 18:53:30 +02:00
|
|
|
/**
|
|
|
|
* GET: /api/me/progress/:id/:episodeId?
|
|
|
|
*
|
|
|
|
* @param {RequestWithUser} req
|
|
|
|
* @param {Response} res
|
|
|
|
*/
|
2022-06-04 01:59:42 +02:00
|
|
|
async getMediaProgress(req, res) {
|
2024-08-11 18:53:30 +02:00
|
|
|
const mediaProgress = req.userNew.getOldMediaProgress(req.params.id, req.params.episodeId || null)
|
2022-06-04 01:59:42 +02:00
|
|
|
if (!mediaProgress) {
|
|
|
|
return res.sendStatus(404)
|
|
|
|
}
|
|
|
|
res.json(mediaProgress)
|
|
|
|
}
|
|
|
|
|
2024-08-11 18:53:30 +02:00
|
|
|
/**
|
|
|
|
* DELETE: /api/me/progress/:id
|
|
|
|
*
|
|
|
|
* @param {RequestWithUser} req
|
|
|
|
* @param {Response} res
|
|
|
|
*/
|
2022-03-26 17:59:34 +01:00
|
|
|
async removeMediaProgress(req, res) {
|
2024-08-11 18:53:30 +02:00
|
|
|
await Database.mediaProgressModel.removeById(req.params.id)
|
|
|
|
req.userNew.mediaProgresses = req.userNew.mediaProgresses.filter((mp) => mp.id !== req.params.id)
|
2022-06-25 18:01:01 +02:00
|
|
|
|
2024-08-11 18:53:30 +02:00
|
|
|
SocketAuthority.clientEmitter(req.userNew.id, 'user_updated', req.userNew.toOldJSONForBrowser())
|
2021-11-22 03:00:40 +01:00
|
|
|
res.sendStatus(200)
|
|
|
|
}
|
|
|
|
|
2024-08-11 18:53:30 +02:00
|
|
|
/**
|
|
|
|
* PATCH: /api/me/progress/:libraryItemId/:episodeId?
|
|
|
|
* TODO: Update to use mediaItemId and mediaItemType
|
|
|
|
*
|
|
|
|
* @param {RequestWithUser} req
|
|
|
|
* @param {Response} res
|
|
|
|
*/
|
|
|
|
async createUpdateMediaProgress(req, res) {
|
|
|
|
const progressUpdatePayload = {
|
|
|
|
...req.body,
|
|
|
|
libraryItemId: req.params.libraryItemId,
|
|
|
|
episodeId: req.params.episodeId
|
2022-03-26 23:41:26 +01:00
|
|
|
}
|
2024-08-11 18:53:30 +02:00
|
|
|
const mediaProgressResponse = await req.userNew.createUpdateMediaProgressFromPayload(progressUpdatePayload)
|
|
|
|
if (mediaProgressResponse.error) {
|
|
|
|
return res.status(mediaProgressResponse.statusCode || 400).send(mediaProgressResponse.error)
|
2022-03-26 23:41:26 +01:00
|
|
|
}
|
|
|
|
|
2024-08-11 18:53:30 +02:00
|
|
|
SocketAuthority.clientEmitter(req.userNew.id, 'user_updated', req.userNew.toOldJSONForBrowser())
|
2022-03-26 23:41:26 +01:00
|
|
|
res.sendStatus(200)
|
|
|
|
}
|
|
|
|
|
2024-08-11 18:53:30 +02:00
|
|
|
/**
|
|
|
|
* PATCH: /api/me/progress/batch/update
|
|
|
|
* TODO: Update to use mediaItemId and mediaItemType
|
|
|
|
*
|
|
|
|
* @param {RequestWithUser} req
|
|
|
|
* @param {Response} res
|
|
|
|
*/
|
2022-03-26 17:59:34 +01:00
|
|
|
async batchUpdateMediaProgress(req, res) {
|
2023-07-05 01:14:44 +02:00
|
|
|
const itemProgressPayloads = req.body
|
|
|
|
if (!itemProgressPayloads?.length) {
|
2022-11-23 02:15:36 +01:00
|
|
|
return res.status(400).send('Missing request payload')
|
2021-11-22 03:00:40 +01:00
|
|
|
}
|
|
|
|
|
2024-08-11 18:53:30 +02:00
|
|
|
let hasUpdated = false
|
2023-07-05 01:14:44 +02:00
|
|
|
for (const itemProgress of itemProgressPayloads) {
|
2024-08-11 18:53:30 +02:00
|
|
|
const mediaProgressResponse = await req.userNew.createUpdateMediaProgressFromPayload(itemProgress)
|
|
|
|
if (mediaProgressResponse.error) {
|
|
|
|
Logger.error(`[MeController] batchUpdateMediaProgress: ${mediaProgressResponse.error}`)
|
|
|
|
continue
|
2022-03-17 19:33:22 +01:00
|
|
|
} else {
|
2024-08-11 18:53:30 +02:00
|
|
|
hasUpdated = true
|
2021-11-22 03:00:40 +01:00
|
|
|
}
|
2023-07-05 01:14:44 +02:00
|
|
|
}
|
2021-11-22 03:00:40 +01:00
|
|
|
|
2024-08-11 18:53:30 +02:00
|
|
|
if (hasUpdated) {
|
|
|
|
SocketAuthority.clientEmitter(req.userNew.id, 'user_updated', req.userNew.toOldJSONForBrowser())
|
2021-11-22 03:00:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
res.sendStatus(200)
|
|
|
|
}
|
|
|
|
|
2024-08-11 18:53:30 +02:00
|
|
|
/**
|
|
|
|
* POST: /api/me/item/:id/bookmark
|
|
|
|
*
|
|
|
|
* @param {RequestWithUser} req
|
|
|
|
* @param {Response} res
|
|
|
|
*/
|
2022-03-18 02:28:04 +01:00
|
|
|
async createBookmark(req, res) {
|
2024-05-23 23:32:34 +02:00
|
|
|
if (!(await Database.libraryItemModel.checkExistsById(req.params.id))) return res.sendStatus(404)
|
2023-08-12 22:52:09 +02:00
|
|
|
|
2022-03-18 02:28:04 +01:00
|
|
|
const { time, title } = req.body
|
2024-08-11 19:16:45 +02:00
|
|
|
if (isNullOrNaN(time)) {
|
|
|
|
Logger.error(`[MeController] createBookmark invalid time`, time)
|
|
|
|
return res.status(400).send('Invalid time')
|
|
|
|
}
|
|
|
|
if (!title || typeof title !== 'string') {
|
|
|
|
Logger.error(`[MeController] createBookmark invalid title`, title)
|
|
|
|
return res.status(400).send('Invalid title')
|
|
|
|
}
|
|
|
|
|
|
|
|
const bookmark = await req.userNew.createBookmark(req.params.id, time, title)
|
|
|
|
SocketAuthority.clientEmitter(req.userNew.id, 'user_updated', req.userNew.toOldJSONForBrowser())
|
2022-03-18 02:28:04 +01:00
|
|
|
res.json(bookmark)
|
|
|
|
}
|
|
|
|
|
2024-08-11 19:16:45 +02:00
|
|
|
/**
|
|
|
|
* PATCH: /api/me/item/:id/bookmark
|
|
|
|
*
|
|
|
|
* @param {RequestWithUser} req
|
|
|
|
* @param {Response} res
|
|
|
|
*/
|
2022-03-18 02:28:04 +01:00
|
|
|
async updateBookmark(req, res) {
|
2024-05-23 23:32:34 +02:00
|
|
|
if (!(await Database.libraryItemModel.checkExistsById(req.params.id))) return res.sendStatus(404)
|
2023-08-12 22:52:09 +02:00
|
|
|
|
2022-03-18 02:28:04 +01:00
|
|
|
const { time, title } = req.body
|
2024-08-11 19:16:45 +02:00
|
|
|
if (isNullOrNaN(time)) {
|
|
|
|
Logger.error(`[MeController] updateBookmark invalid time`, time)
|
|
|
|
return res.status(400).send('Invalid time')
|
|
|
|
}
|
|
|
|
if (!title || typeof title !== 'string') {
|
|
|
|
Logger.error(`[MeController] updateBookmark invalid title`, title)
|
|
|
|
return res.status(400).send('Invalid title')
|
2022-03-18 02:28:04 +01:00
|
|
|
}
|
2023-08-12 22:52:09 +02:00
|
|
|
|
2024-08-11 19:16:45 +02:00
|
|
|
const bookmark = await req.userNew.updateBookmark(req.params.id, time, title)
|
|
|
|
if (!bookmark) {
|
|
|
|
Logger.error(`[MeController] updateBookmark not found for library item id "${req.params.id}" and time "${time}"`)
|
|
|
|
return res.sendStatus(404)
|
|
|
|
}
|
2023-08-12 22:52:09 +02:00
|
|
|
|
2024-08-11 19:16:45 +02:00
|
|
|
SocketAuthority.clientEmitter(req.userNew.id, 'user_updated', req.userNew.toOldJSONForBrowser())
|
2022-03-18 02:28:04 +01:00
|
|
|
res.json(bookmark)
|
|
|
|
}
|
|
|
|
|
2024-08-11 19:16:45 +02:00
|
|
|
/**
|
|
|
|
* DELETE: /api/me/item/:id/bookmark/:time
|
|
|
|
*
|
|
|
|
* @param {RequestWithUser} req
|
|
|
|
* @param {Response} res
|
|
|
|
*/
|
2022-03-18 02:28:04 +01:00
|
|
|
async removeBookmark(req, res) {
|
2024-05-23 23:32:34 +02:00
|
|
|
if (!(await Database.libraryItemModel.checkExistsById(req.params.id))) return res.sendStatus(404)
|
2023-08-12 22:52:09 +02:00
|
|
|
|
|
|
|
const time = Number(req.params.time)
|
2024-08-11 19:16:45 +02:00
|
|
|
if (isNaN(time)) {
|
|
|
|
return res.status(400).send('Invalid time')
|
|
|
|
}
|
2022-03-18 02:28:04 +01:00
|
|
|
|
2024-08-11 19:16:45 +02:00
|
|
|
if (!req.userNew.findBookmark(req.params.id, time)) {
|
2022-03-18 02:28:04 +01:00
|
|
|
Logger.error(`[MeController] removeBookmark not found`)
|
|
|
|
return res.sendStatus(404)
|
|
|
|
}
|
2023-08-12 22:52:09 +02:00
|
|
|
|
2024-08-11 19:16:45 +02:00
|
|
|
await req.userNew.removeBookmark(req.params.id, time)
|
|
|
|
|
|
|
|
SocketAuthority.clientEmitter(req.userNew.id, 'user_updated', req.userNew.toOldJSONForBrowser())
|
2022-03-18 02:28:04 +01:00
|
|
|
res.sendStatus(200)
|
|
|
|
}
|
|
|
|
|
2024-08-11 20:09:53 +02:00
|
|
|
/**
|
|
|
|
* PATCH: /api/me/password
|
|
|
|
* User change password. Requires current password.
|
|
|
|
* Guest users cannot change password.
|
|
|
|
*
|
|
|
|
* @this import('../routers/ApiRouter')
|
|
|
|
*
|
|
|
|
* @param {RequestWithUser} req
|
|
|
|
* @param {Response} res
|
|
|
|
*/
|
2021-11-22 03:00:40 +01:00
|
|
|
updatePassword(req, res) {
|
2024-08-11 20:09:53 +02:00
|
|
|
if (req.userNew.isGuest) {
|
|
|
|
Logger.error(`[MeController] Guest user "${req.userNew.username}" attempted to change password`)
|
2022-04-30 01:38:13 +02:00
|
|
|
return res.sendStatus(500)
|
|
|
|
}
|
2021-11-22 03:00:40 +01:00
|
|
|
this.auth.userChangePassword(req, res)
|
|
|
|
}
|
|
|
|
|
2024-08-11 20:09:53 +02:00
|
|
|
/**
|
|
|
|
* GET: /api/me/items-in-progress
|
|
|
|
* Pull items in progress for all libraries
|
|
|
|
* Used in Android Auto in progress list since there is no easy library selection
|
|
|
|
* TODO: Update to use mediaItemId and mediaItemType. Use sort & limit in query
|
|
|
|
*
|
|
|
|
* @param {RequestWithUser} req
|
|
|
|
* @param {Response} res
|
|
|
|
*/
|
2023-09-04 23:33:55 +02:00
|
|
|
async getAllLibraryItemsInProgress(req, res) {
|
2022-08-14 17:24:41 +02:00
|
|
|
const limit = !isNaN(req.query.limit) ? Number(req.query.limit) || 25 : 25
|
|
|
|
|
2024-08-11 20:09:53 +02:00
|
|
|
const mediaProgressesInProgress = req.userNew.mediaProgresses.filter((mp) => !mp.isFinished && (mp.currentTime > 0 || mp.ebookProgress > 0))
|
|
|
|
|
|
|
|
const libraryItemsIds = [...new Set(mediaProgressesInProgress.map((mp) => mp.extraData?.libraryItemId).filter((id) => id))]
|
|
|
|
const libraryItems = await Database.libraryItemModel.getAllOldLibraryItems({ id: libraryItemsIds })
|
|
|
|
|
2023-03-25 20:07:35 +01:00
|
|
|
let itemsInProgress = []
|
2024-08-11 20:09:53 +02:00
|
|
|
|
|
|
|
for (const mediaProgress of mediaProgressesInProgress) {
|
|
|
|
const oldMediaProgress = mediaProgress.getOldMediaProgress()
|
|
|
|
const libraryItem = libraryItems.find((li) => li.id === oldMediaProgress.libraryItemId)
|
|
|
|
if (libraryItem) {
|
|
|
|
if (oldMediaProgress.episodeId && libraryItem.mediaType === 'podcast') {
|
|
|
|
const episode = libraryItem.media.episodes.find((ep) => ep.id === oldMediaProgress.episodeId)
|
|
|
|
if (episode) {
|
|
|
|
const libraryItemWithEpisode = {
|
2022-08-14 17:24:41 +02:00
|
|
|
...libraryItem.toJSONMinified(),
|
2024-08-11 20:09:53 +02:00
|
|
|
recentEpisode: episode.toJSON(),
|
|
|
|
progressLastUpdate: oldMediaProgress.lastUpdate
|
|
|
|
}
|
|
|
|
itemsInProgress.push(libraryItemWithEpisode)
|
2022-08-14 17:24:41 +02:00
|
|
|
}
|
2024-08-11 20:09:53 +02:00
|
|
|
} else if (!oldMediaProgress.episodeId) {
|
|
|
|
itemsInProgress.push({
|
|
|
|
...libraryItem.toJSONMinified(),
|
|
|
|
progressLastUpdate: oldMediaProgress.lastUpdate
|
|
|
|
})
|
2022-08-14 17:24:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-23 23:32:34 +02:00
|
|
|
itemsInProgress = sort(itemsInProgress)
|
|
|
|
.desc((li) => li.progressLastUpdate)
|
|
|
|
.slice(0, limit)
|
2022-08-14 17:24:41 +02:00
|
|
|
res.json({
|
|
|
|
libraryItems: itemsInProgress
|
|
|
|
})
|
|
|
|
}
|
2022-09-29 00:12:27 +02:00
|
|
|
|
2024-08-11 20:09:53 +02:00
|
|
|
/**
|
|
|
|
* GET: /api/me/series/:id/remove-from-continue-listening
|
|
|
|
*
|
|
|
|
* @param {RequestWithUser} req
|
|
|
|
* @param {Response} res
|
|
|
|
*/
|
2022-09-29 00:45:39 +02:00
|
|
|
async removeSeriesFromContinueListening(req, res) {
|
2024-08-11 20:09:53 +02:00
|
|
|
if (!(await Database.seriesModel.checkExistsById(req.params.id))) {
|
2022-09-29 00:45:39 +02:00
|
|
|
Logger.error(`[MeController] removeSeriesFromContinueListening: Series ${req.params.id} not found`)
|
2022-09-29 00:12:27 +02:00
|
|
|
return res.sendStatus(404)
|
|
|
|
}
|
|
|
|
|
2024-08-11 20:09:53 +02:00
|
|
|
const hasUpdated = await req.userNew.addSeriesToHideFromContinueListening(req.params.id)
|
2022-09-29 00:12:27 +02:00
|
|
|
if (hasUpdated) {
|
2024-08-11 20:09:53 +02:00
|
|
|
SocketAuthority.clientEmitter(req.userNew.id, 'user_updated', req.userNew.toOldJSONForBrowser())
|
2022-09-29 00:12:27 +02:00
|
|
|
}
|
2024-08-11 20:09:53 +02:00
|
|
|
res.json(req.userNew.toOldJSONForBrowser())
|
2022-09-29 00:12:27 +02:00
|
|
|
}
|
2022-09-29 00:45:39 +02:00
|
|
|
|
2024-08-11 20:09:53 +02:00
|
|
|
/**
|
|
|
|
* GET: api/me/series/:id/readd-to-continue-listening
|
|
|
|
*
|
|
|
|
* @param {RequestWithUser} req
|
|
|
|
* @param {Response} res
|
|
|
|
*/
|
2022-11-16 00:20:57 +01:00
|
|
|
async readdSeriesFromContinueListening(req, res) {
|
2024-08-11 20:09:53 +02:00
|
|
|
if (!(await Database.seriesModel.checkExistsById(req.params.id))) {
|
2022-11-16 00:20:57 +01:00
|
|
|
Logger.error(`[MeController] readdSeriesFromContinueListening: Series ${req.params.id} not found`)
|
|
|
|
return res.sendStatus(404)
|
|
|
|
}
|
|
|
|
|
2024-08-11 20:09:53 +02:00
|
|
|
const hasUpdated = await req.userNew.removeSeriesFromHideFromContinueListening(req.params.id)
|
2022-11-16 00:20:57 +01:00
|
|
|
if (hasUpdated) {
|
2024-08-11 20:09:53 +02:00
|
|
|
SocketAuthority.clientEmitter(req.userNew.id, 'user_updated', req.userNew.toOldJSONForBrowser())
|
2022-11-16 00:20:57 +01:00
|
|
|
}
|
2024-08-11 20:09:53 +02:00
|
|
|
res.json(req.userNew.toOldJSONForBrowser())
|
2022-11-16 00:20:57 +01:00
|
|
|
}
|
|
|
|
|
2024-08-11 20:09:53 +02:00
|
|
|
/**
|
|
|
|
* GET: api/me/progress/:id/remove-from-continue-listening
|
|
|
|
*
|
|
|
|
* @param {RequestWithUser} req
|
|
|
|
* @param {Response} res
|
|
|
|
*/
|
2022-09-29 00:45:39 +02:00
|
|
|
async removeItemFromContinueListening(req, res) {
|
2024-08-11 20:09:53 +02:00
|
|
|
const mediaProgress = req.userNew.mediaProgresses.find((mp) => mp.id === req.params.id)
|
2023-09-08 00:49:35 +02:00
|
|
|
if (!mediaProgress) {
|
|
|
|
return res.sendStatus(404)
|
|
|
|
}
|
2024-08-11 20:09:53 +02:00
|
|
|
|
|
|
|
// Already hidden
|
|
|
|
if (mediaProgress.hideFromContinueListening) {
|
|
|
|
return res.json(req.userNew.toOldJSONForBrowser())
|
2022-09-29 00:45:39 +02:00
|
|
|
}
|
2024-08-11 20:09:53 +02:00
|
|
|
|
|
|
|
mediaProgress.hideFromContinueListening = true
|
|
|
|
await mediaProgress.save()
|
|
|
|
|
|
|
|
SocketAuthority.clientEmitter(req.userNew.id, 'user_updated', req.userNew.toOldJSONForBrowser())
|
|
|
|
|
|
|
|
res.json(req.userNew.toOldJSONForBrowser())
|
2022-09-29 00:45:39 +02:00
|
|
|
}
|
2023-12-20 00:19:33 +01:00
|
|
|
|
|
|
|
/**
|
2024-02-16 23:05:02 +01:00
|
|
|
* GET: /api/me/stats/year/:year
|
2024-05-05 13:14:30 +02:00
|
|
|
*
|
|
|
|
* @param {import('express').Request} req
|
|
|
|
* @param {import('express').Response} res
|
2023-12-20 00:19:33 +01:00
|
|
|
*/
|
|
|
|
async getStatsForYear(req, res) {
|
|
|
|
const year = Number(req.params.year)
|
|
|
|
if (isNaN(year) || year < 2000 || year > 9999) {
|
|
|
|
Logger.error(`[MeController] Invalid year "${year}"`)
|
|
|
|
return res.status(400).send('Invalid year')
|
|
|
|
}
|
2024-08-11 20:09:53 +02:00
|
|
|
const data = await userStats.getStatsForYear(req.userNew.id, year)
|
2023-12-20 00:19:33 +01:00
|
|
|
res.json(data)
|
|
|
|
}
|
2021-11-22 03:00:40 +01:00
|
|
|
}
|
2024-05-05 13:14:30 +02:00
|
|
|
module.exports = new MeController()
|