mirror of
https://github.com/movie-web/backend.git
synced 2025-09-13 15:03:27 +00:00
Compare commits
13 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
e173003f55 | ||
|
9f90ba7da2 | ||
|
05bf651939 | ||
|
cf0125755c | ||
|
4129b80828 | ||
|
e5c3cde51b | ||
|
4bf0285d06 | ||
|
53d5ca1461 | ||
|
3211f74387 | ||
|
8cffd0e7e8 | ||
|
c531329931 | ||
|
690357ba5a | ||
|
10e9e06c27 |
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "backend",
|
||||
"version": "1.1.3",
|
||||
"version": "1.1.5",
|
||||
"private": true,
|
||||
"homepage": "https://github.com/movie-web/backend",
|
||||
"engines": {
|
||||
|
@@ -2,7 +2,7 @@ import { loginAuthRouter } from '@/routes/auth/login';
|
||||
import { manageAuthRouter } from '@/routes/auth/manage';
|
||||
import { metaRouter } from '@/routes/meta';
|
||||
import { metricsRouter } from '@/routes/metrics';
|
||||
import { sessionsRouter } from '@/routes/sessions';
|
||||
import { sessionsRouter } from '@/routes/sessions/sessions';
|
||||
import { userBookmarkRouter } from '@/routes/users/bookmark';
|
||||
import { userDeleteRouter } from '@/routes/users/delete';
|
||||
import { userEditRouter } from '@/routes/users/edit';
|
||||
|
@@ -1,63 +0,0 @@
|
||||
import { Session, formatSession } from '@/db/models/Session';
|
||||
import { StatusError } from '@/services/error';
|
||||
import { handle } from '@/services/handler';
|
||||
import { makeRouter } from '@/services/router';
|
||||
import { z } from 'zod';
|
||||
|
||||
export const sessionRouter = makeRouter((app) => {
|
||||
app.delete(
|
||||
'/sessions/:sid',
|
||||
{
|
||||
schema: {
|
||||
params: z.object({
|
||||
sid: z.string(),
|
||||
}),
|
||||
},
|
||||
},
|
||||
handle(async ({ auth, params, em }) => {
|
||||
await auth.assert();
|
||||
|
||||
const targetedSession = await em.findOne(Session, { id: params.sid });
|
||||
if (!targetedSession)
|
||||
return {
|
||||
id: params.sid,
|
||||
};
|
||||
|
||||
if (targetedSession.user !== auth.user.id)
|
||||
throw new StatusError('Cannot delete sessions you do not own', 401);
|
||||
|
||||
await em.removeAndFlush(targetedSession);
|
||||
return {
|
||||
id: params.sid,
|
||||
};
|
||||
}),
|
||||
);
|
||||
|
||||
app.patch(
|
||||
'/sessions/:sid',
|
||||
{
|
||||
schema: {
|
||||
params: z.object({
|
||||
sid: z.string(),
|
||||
}),
|
||||
body: z.object({
|
||||
deviceName: z.string().min(1).optional(),
|
||||
}),
|
||||
},
|
||||
},
|
||||
handle(async ({ auth, params, body, em }) => {
|
||||
await auth.assert();
|
||||
|
||||
const targetedSession = await em.findOne(Session, { id: params.sid });
|
||||
if (!targetedSession) throw new StatusError('Not found', 404);
|
||||
if (targetedSession.id !== params.sid)
|
||||
throw new StatusError('Cannot edit sessions other than your own', 401);
|
||||
|
||||
if (body.deviceName) targetedSession.device = body.deviceName;
|
||||
|
||||
await em.persistAndFlush(targetedSession);
|
||||
|
||||
return formatSession(targetedSession);
|
||||
}),
|
||||
);
|
||||
});
|
@@ -13,7 +13,7 @@ export const sessionsRouter = makeRouter((app) => {
|
||||
sid: z.string(),
|
||||
}),
|
||||
body: z.object({
|
||||
name: z.string().max(500).min(1).optional(),
|
||||
deviceName: z.string().max(500).min(1).optional(),
|
||||
}),
|
||||
},
|
||||
},
|
||||
@@ -25,10 +25,10 @@ export const sessionsRouter = makeRouter((app) => {
|
||||
if (!targetedSession)
|
||||
throw new StatusError('Session cannot be found', 404);
|
||||
|
||||
if (targetedSession.user !== auth.user.id)
|
||||
throw new StatusError('Cannot modify sessions you do not own', 401);
|
||||
if (targetedSession.id !== params.sid)
|
||||
throw new StatusError('Cannot edit sessions other than your own', 401);
|
||||
|
||||
if (body.name) targetedSession.device = body.name;
|
||||
if (body.deviceName) targetedSession.device = body.deviceName;
|
||||
|
||||
await em.persistAndFlush(targetedSession);
|
||||
|
@@ -18,6 +18,7 @@ const progressItemSchema = z.object({
|
||||
episodeId: z.string().optional(),
|
||||
seasonNumber: z.number().optional(),
|
||||
episodeNumber: z.number().optional(),
|
||||
updatedAt: z.string().datetime({ offset: true }).optional(),
|
||||
});
|
||||
|
||||
export const userProgressRouter = makeRouter((app) => {
|
||||
@@ -100,7 +101,9 @@ export const userProgressRouter = makeRouter((app) => {
|
||||
if (newItemIndex > -1) {
|
||||
const newItem = newItems[newItemIndex];
|
||||
if (existingItem.watched < newItem.watched) {
|
||||
existingItem.updatedAt = new Date();
|
||||
existingItem.updatedAt = defaultAndCoerceDateTime(
|
||||
newItem.updatedAt,
|
||||
);
|
||||
existingItem.watched = newItem.watched;
|
||||
}
|
||||
itemsUpserted.push(existingItem);
|
||||
@@ -123,7 +126,7 @@ export const userProgressRouter = makeRouter((app) => {
|
||||
tmdbId: newItem.tmdbId,
|
||||
userId: params.uid,
|
||||
watched: newItem.watched,
|
||||
updatedAt: new Date(),
|
||||
updatedAt: defaultAndCoerceDateTime(newItem.updatedAt),
|
||||
});
|
||||
}
|
||||
|
||||
@@ -207,3 +210,14 @@ export const userProgressRouter = makeRouter((app) => {
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
// 13th July 2021 - movie-web epoch
|
||||
const minEpoch = 1626134400000;
|
||||
|
||||
function defaultAndCoerceDateTime(dateTime: string | undefined) {
|
||||
const epoch = dateTime ? new Date(dateTime).getTime() : Date.now();
|
||||
|
||||
const clampedEpoch = Math.max(minEpoch, Math.min(epoch, Date.now()));
|
||||
|
||||
return new Date(clampedEpoch);
|
||||
}
|
||||
|
Reference in New Issue
Block a user