mirror of
https://github.com/movie-web/backend.git
synced 2025-09-13 14:53:25 +00:00
Compare commits
19 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
3211f74387 | ||
|
8cffd0e7e8 | ||
|
c531329931 | ||
|
690357ba5a | ||
|
10e9e06c27 | ||
|
783d89492d | ||
|
4663b2c1f7 | ||
|
ceea274e70 | ||
|
6d2dcd04e9 | ||
|
8a3c0d6edb | ||
|
72657e73c8 | ||
|
1bb344ec2f | ||
|
b30623c483 | ||
|
233cb11ac6 | ||
|
d3aa4847f8 | ||
|
4bf2e658f7 | ||
|
c18ff489c3 | ||
|
fcc3dc60d9 | ||
|
c7a706cdf0 |
@@ -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',
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "backend",
|
"name": "backend",
|
||||||
"version": "1.1.0",
|
"version": "1.1.4",
|
||||||
"private": true,
|
"private": true,
|
||||||
"homepage": "https://github.com/movie-web/backend",
|
"homepage": "https://github.com/movie-web/backend",
|
||||||
"engines": {
|
"engines": {
|
||||||
|
@@ -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",
|
||||||
|
14
src/db/migrations/Migration20231122231620.ts
Normal file
14
src/db/migrations/Migration20231122231620.ts
Normal 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);');
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -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;
|
||||||
|
@@ -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';
|
||||||
|
@@ -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,
|
|
||||||
};
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
});
|
|
@@ -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);
|
||||||
|
|
@@ -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,
|
|
||||||
};
|
};
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
@@ -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);
|
||||||
|
Reference in New Issue
Block a user