33 Commits
1.1.0 ... 1.1.6

Author SHA1 Message Date
William Oldham
f4e9a96666 Merge pull request #24 from movie-web/dev
Update bugfix
2023-12-14 20:48:14 +00:00
William Oldham
60dda8ac57 Merge branch 'master' into dev 2023-12-14 20:48:04 +00:00
William Oldham
d84cdc4239 Merge pull request #23 from movie-web/fix-show-delete
Fix show deletion
2023-12-14 20:47:40 +00:00
mrjvs
c2cba27e68 remove unused import 2023-12-14 21:10:48 +01:00
mrjvs
c3259156ac bump version 2023-12-14 21:07:38 +01:00
mrjvs
9ef12d1c0f Delete all entries of a show 2023-12-14 21:07:24 +01:00
William Oldham
e173003f55 Merge pull request #22 from movie-web/dev
v1.1.5 - Progress importing now uses the user's date
2023-12-06 20:03:22 +00:00
William Oldham
9f90ba7da2 Merge pull request #21 from movie-web/progress-date
Add UpdatedAt from User date for Progress
2023-12-06 20:01:35 +00:00
William Oldham
05bf651939 Revert "Use date to compare progress items"
This reverts commit cf0125755c.
2023-12-06 19:59:50 +00:00
William Oldham
cf0125755c Use date to compare progress items 2023-12-06 19:55:09 +00:00
William Oldham
4129b80828 Use movie-web birthday 2023-12-06 19:49:30 +00:00
William Oldham
e5c3cde51b Only use user date on bulk import 2023-12-06 19:46:22 +00:00
William Oldham
4bf0285d06 Bump version 2023-12-06 19:43:57 +00:00
William Oldham
53d5ca1461 Add updatedAt saving into progress imports 2023-12-06 19:43:37 +00:00
mrjvs
3211f74387 Merge pull request #20 from movie-web/dev
V1.1.4
2023-11-25 17:21:20 +01:00
mrjvs
8cffd0e7e8 Merge branch 'master' into dev 2023-11-25 17:11:46 +01:00
mrjvs
c531329931 Merge pull request #19 from movie-web/device-name-fix
Remove duplicate session router
2023-11-25 17:09:20 +01:00
mrjvs
690357ba5a bump version 2023-11-25 16:09:55 +01:00
mrjvs
10e9e06c27 only have one session router 2023-11-25 16:09:29 +01:00
mrjvs
783d89492d Merge pull request #18 from movie-web/dev
Version 1.1.3: Add endpoint to edit device name
2023-11-24 20:07:57 +01:00
mrjvs
4663b2c1f7 Merge branch 'master' into dev 2023-11-24 20:06:42 +01:00
William Oldham
ceea274e70 Merge pull request #17 from movie-web/device-name-update
Device name update + fixing settings endpoint
2023-11-24 18:56:08 +00:00
William Oldham
6d2dcd04e9 Apply suggestions from code review 2023-11-24 18:55:20 +00:00
mrjvs
8a3c0d6edb add settings nullable + undefined difference 2023-11-24 18:40:54 +01:00
mrjvs
72657e73c8 add update session endpoint 2023-11-24 18:00:06 +01:00
William Oldham
1bb344ec2f Merge pull request #16 from movie-web/dev
v1.1.2: Fix Settings
2023-11-22 23:23:00 +00:00
William Oldham
b30623c483 Merge pull request #15 from movie-web/fix-user-settings
Fix user settings
2023-11-22 23:21:26 +00:00
William Oldham
233cb11ac6 Bump version 2023-11-22 23:17:29 +00:00
William Oldham
d3aa4847f8 Update UserSettings primary key to text, to match user 2023-11-22 23:17:29 +00:00
William Oldham
4bf2e658f7 Fix ESLint Ignore for migrations 2023-11-22 23:17:29 +00:00
William Oldham
c18ff489c3 Merge pull request #14 from movie-web/dev
Fix @me endpoint
2023-11-19 20:00:54 +00:00
William Oldham
fcc3dc60d9 Merge branch 'master' into dev 2023-11-19 20:00:06 +00:00
William Oldham
c7a706cdf0 Fix @me endpoint 2023-11-19 19:59:02 +00:00
12 changed files with 82 additions and 76 deletions

View File

@@ -13,7 +13,7 @@ module.exports = {
sourceType: 'module', sourceType: 'module',
}, },
plugins: ['@typescript-eslint'], plugins: ['@typescript-eslint'],
ignorePatterns: ['./src/db/migrations/**/*'], ignorePatterns: ['src/db/migrations/**/*'],
rules: { rules: {
'@typescript-eslint/interface-name-prefix': 'off', '@typescript-eslint/interface-name-prefix': 'off',
'@typescript-eslint/explicit-function-return-type': 'off', '@typescript-eslint/explicit-function-return-type': 'off',

View File

@@ -1,6 +1,6 @@
{ {
"name": "backend", "name": "backend",
"version": "1.1.0", "version": "1.1.6",
"private": true, "private": true,
"homepage": "https://github.com/movie-web/backend", "homepage": "https://github.com/movie-web/backend",
"engines": { "engines": {

View File

@@ -587,12 +587,12 @@
"columns": { "columns": {
"id": { "id": {
"name": "id", "name": "id",
"type": "uuid", "type": "text",
"unsigned": false, "unsigned": false,
"autoincrement": false, "autoincrement": false,
"primary": false, "primary": false,
"nullable": false, "nullable": false,
"mappedType": "uuid" "mappedType": "text"
}, },
"application_theme": { "application_theme": {
"name": "application_theme", "name": "application_theme",

View File

@@ -0,0 +1,14 @@
import { Migration } from '@mikro-orm/migrations';
export class Migration20231122231620 extends Migration {
async up(): Promise<void> {
this.addSql('alter table "user_settings" alter column "id" type text using ("id"::text);');
}
async down(): Promise<void> {
this.addSql('alter table "user_settings" alter column "id" drop default;');
this.addSql('alter table "user_settings" alter column "id" type uuid using ("id"::text::uuid);');
}
}

View File

@@ -1,10 +1,9 @@
import { Entity, PrimaryKey, Property } from '@mikro-orm/core'; import { Entity, PrimaryKey, Property } from '@mikro-orm/core';
import { randomUUID } from 'crypto';
@Entity({ tableName: 'user_settings' }) @Entity({ tableName: 'user_settings' })
export class UserSettings { export class UserSettings {
@PrimaryKey({ name: 'id', type: 'uuid' }) @PrimaryKey({ name: 'id', type: 'text' })
id: string = randomUUID(); id!: string;
@Property({ name: 'application_theme', nullable: true }) @Property({ name: 'application_theme', nullable: true })
applicationTheme?: string | null; applicationTheme?: string | null;

View File

@@ -2,7 +2,7 @@ import { loginAuthRouter } from '@/routes/auth/login';
import { manageAuthRouter } from '@/routes/auth/manage'; import { manageAuthRouter } from '@/routes/auth/manage';
import { metaRouter } from '@/routes/meta'; import { metaRouter } from '@/routes/meta';
import { metricsRouter } from '@/routes/metrics'; import { metricsRouter } from '@/routes/metrics';
import { sessionsRouter } from '@/routes/sessions'; import { sessionsRouter } from '@/routes/sessions/sessions';
import { userBookmarkRouter } from '@/routes/users/bookmark'; import { userBookmarkRouter } from '@/routes/users/bookmark';
import { userDeleteRouter } from '@/routes/users/delete'; import { userDeleteRouter } from '@/routes/users/delete';
import { userEditRouter } from '@/routes/users/edit'; import { userEditRouter } from '@/routes/users/edit';

View File

@@ -1,35 +0,0 @@
import { Session } 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,
};
}),
);
});

View File

@@ -13,7 +13,7 @@ export const sessionsRouter = makeRouter((app) => {
sid: z.string(), sid: z.string(),
}), }),
body: z.object({ 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) if (!targetedSession)
throw new StatusError('Session cannot be found', 404); throw new StatusError('Session cannot be found', 404);
if (targetedSession.user !== auth.user.id) if (targetedSession.id !== params.sid)
throw new StatusError('Cannot modify sessions you do not own', 401); 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); await em.persistAndFlush(targetedSession);

View File

@@ -6,7 +6,6 @@ import {
import { StatusError } from '@/services/error'; import { StatusError } from '@/services/error';
import { handle } from '@/services/handler'; import { handle } from '@/services/handler';
import { makeRouter } from '@/services/router'; import { makeRouter } from '@/services/router';
import { randomUUID } from 'crypto';
import { z } from 'zod'; import { z } from 'zod';
const bookmarkDataSchema = z.object({ const bookmarkDataSchema = z.object({

View File

@@ -1,4 +1,4 @@
import { Session, formatSession } from '@/db/models/Session'; import { formatSession } from '@/db/models/Session';
import { User, formatUser } from '@/db/models/User'; import { User, formatUser } from '@/db/models/User';
import { StatusError } from '@/services/error'; import { StatusError } from '@/services/error';
import { handle } from '@/services/handler'; import { handle } from '@/services/handler';
@@ -6,6 +6,24 @@ import { makeRouter } from '@/services/router';
import { z } from 'zod'; import { z } from 'zod';
export const userGetRouter = makeRouter((app) => { export const userGetRouter = makeRouter((app) => {
app.get(
'/users/@me',
handle(async ({ auth, em }) => {
await auth.assert();
const user = await em.findOne(User, { id: auth.user.id });
if (!user) throw new StatusError('User does not exist', 404);
const session = await auth.getSession();
if (!session) throw new StatusError('Session does not exist', 400);
return {
user: formatUser(user),
session: formatSession(session),
};
}),
);
app.get( app.get(
'/users/:uid', '/users/:uid',
{ {
@@ -17,25 +35,15 @@ export const userGetRouter = makeRouter((app) => {
}, },
handle(async ({ auth, params, em }) => { handle(async ({ auth, params, em }) => {
await auth.assert(); await auth.assert();
let uid = params.uid;
if (uid === '@me') uid = auth.user.id;
if (auth.user.id !== uid) if (auth.user.id !== params.uid)
throw new StatusError('Cannot access users other than yourself', 403); throw new StatusError('Cannot access users other than yourself', 403);
const user = await em.findOne(User, { id: uid }); const user = await em.findOne(User, { id: params.uid });
if (!user) throw new StatusError('User does not exist', 404); if (!user) throw new StatusError('User does not exist', 404);
let session: Session | undefined = undefined;
if (uid === '@me') {
session = (await auth.getSession()) ?? undefined;
if (!session) throw new StatusError('Session does not exist', 400);
}
return { return {
user: formatUser(user), user: formatUser(user),
session: session ? formatSession(session) : undefined,
}; };
}), }),
); );

View File

@@ -6,6 +6,7 @@ import {
import { StatusError } from '@/services/error'; import { StatusError } from '@/services/error';
import { handle } from '@/services/handler'; import { handle } from '@/services/handler';
import { makeRouter } from '@/services/router'; import { makeRouter } from '@/services/router';
import { FilterQuery } from '@mikro-orm/core';
import { randomUUID } from 'crypto'; import { randomUUID } from 'crypto';
import { z } from 'zod'; import { z } from 'zod';
@@ -18,6 +19,7 @@ const progressItemSchema = z.object({
episodeId: z.string().optional(), episodeId: z.string().optional(),
seasonNumber: z.number().optional(), seasonNumber: z.number().optional(),
episodeNumber: z.number().optional(), episodeNumber: z.number().optional(),
updatedAt: z.string().datetime({ offset: true }).optional(),
}); });
export const userProgressRouter = makeRouter((app) => { export const userProgressRouter = makeRouter((app) => {
@@ -100,7 +102,9 @@ export const userProgressRouter = makeRouter((app) => {
if (newItemIndex > -1) { if (newItemIndex > -1) {
const newItem = newItems[newItemIndex]; const newItem = newItems[newItemIndex];
if (existingItem.watched < newItem.watched) { if (existingItem.watched < newItem.watched) {
existingItem.updatedAt = new Date(); existingItem.updatedAt = defaultAndCoerceDateTime(
newItem.updatedAt,
);
existingItem.watched = newItem.watched; existingItem.watched = newItem.watched;
} }
itemsUpserted.push(existingItem); itemsUpserted.push(existingItem);
@@ -123,7 +127,7 @@ export const userProgressRouter = makeRouter((app) => {
tmdbId: newItem.tmdbId, tmdbId: newItem.tmdbId,
userId: params.uid, userId: params.uid,
watched: newItem.watched, watched: newItem.watched,
updatedAt: new Date(), updatedAt: defaultAndCoerceDateTime(newItem.updatedAt),
}); });
} }
@@ -161,22 +165,28 @@ export const userProgressRouter = makeRouter((app) => {
if (auth.user.id !== params.uid) if (auth.user.id !== params.uid)
throw new StatusError('Cannot modify user other than yourself', 403); throw new StatusError('Cannot modify user other than yourself', 403);
const progressItem = await em.findOne(ProgressItem, { const query: FilterQuery<ProgressItem> = {
userId: params.uid, userId: params.uid,
tmdbId: params.tmdbid, tmdbId: params.tmdbid,
episodeId: body.episodeId, };
seasonId: body.seasonId, if (body.seasonId) query.seasonId = body.seasonId;
}); if (body.episodeId) query.episodeId = body.episodeId;
if (!progressItem) { const progressItems = await em.find(ProgressItem, query);
if (progressItems.length === 0) {
return { return {
count: 0,
tmdbId: params.tmdbid, tmdbId: params.tmdbid,
episodeId: body.episodeId, episodeId: body.episodeId,
seasonId: body.seasonId, seasonId: body.seasonId,
}; };
} }
await em.removeAndFlush(progressItem); progressItems.forEach((v) => em.remove(v));
await em.flush();
return { return {
count: progressItems.length,
tmdbId: params.tmdbid, tmdbId: params.tmdbid,
episodeId: body.episodeId, episodeId: body.episodeId,
seasonId: body.seasonId, seasonId: body.seasonId,
@@ -207,3 +217,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);
}

View File

@@ -38,9 +38,9 @@ export const userSettingsRouter = makeRouter((app) => {
uid: z.string(), uid: z.string(),
}), }),
body: z.object({ body: z.object({
applicationLanguage: z.string().optional(), applicationLanguage: z.string().nullable().optional(),
applicationTheme: z.string().optional(), applicationTheme: z.string().nullable().optional(),
defaultSubtitleLanguage: z.string().optional(), defaultSubtitleLanguage: z.string().nullable().optional(),
}), }),
}, },
}, },
@@ -58,12 +58,12 @@ export const userSettingsRouter = makeRouter((app) => {
settings.id = params.uid; settings.id = params.uid;
} }
if (body.applicationLanguage) if (body.applicationLanguage !== undefined)
settings.applicationLanguage = body.applicationLanguage; settings.applicationLanguage = body.applicationLanguage;
if (body.applicationTheme) if (body.defaultSubtitleLanguage !== undefined)
settings.applicationTheme = body.applicationTheme;
if (body.defaultSubtitleLanguage)
settings.defaultSubtitleLanguage = body.defaultSubtitleLanguage; settings.defaultSubtitleLanguage = body.defaultSubtitleLanguage;
if (body.applicationTheme !== undefined)
settings.applicationTheme = body.applicationTheme;
await em.persistAndFlush(settings); await em.persistAndFlush(settings);
return formatUserSettings(settings); return formatUserSettings(settings);