mirror of
https://github.com/movie-web/backend.git
synced 2025-09-13 12:43:26 +00:00
Compare commits
40 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
1bb344ec2f | ||
|
b30623c483 | ||
|
233cb11ac6 | ||
|
d3aa4847f8 | ||
|
4bf2e658f7 | ||
|
c18ff489c3 | ||
|
fcc3dc60d9 | ||
|
c7a706cdf0 | ||
|
36c0548c63 | ||
|
60a285316d | ||
|
fa0e5d0309 | ||
|
e2b8f123b3 | ||
|
fd9b61061c | ||
|
449fd7b153 | ||
|
f54b4f6553 | ||
|
787c8a96d6 | ||
|
3643eea655 | ||
|
a73baf555b | ||
|
6d200e8568 | ||
|
8e10eb7d59 | ||
|
b8016023a4 | ||
|
6f48b62275 | ||
|
709d871a51 | ||
|
b2400775fb | ||
|
c6720d390a | ||
|
d55e0897a9 | ||
|
4937f148c6 | ||
|
f54a45256b | ||
|
f1f7660ea2 | ||
|
f94c1afdf5 | ||
|
f7074157c2 | ||
|
1cecc2cccd | ||
|
effe139683 | ||
|
16fb41c898 | ||
|
01c96babd4 | ||
|
53801f926a | ||
|
ae0ffa9894 | ||
|
b1a79242aa | ||
|
241b1b1f47 | ||
|
b4e185d297 |
@@ -13,7 +13,7 @@ module.exports = {
|
||||
sourceType: 'module',
|
||||
},
|
||||
plugins: ['@typescript-eslint'],
|
||||
ignorePatterns: ['./src/db/migrations/**/*'],
|
||||
ignorePatterns: ['src/db/migrations/**/*'],
|
||||
rules: {
|
||||
'@typescript-eslint/interface-name-prefix': 'off',
|
||||
'@typescript-eslint/explicit-function-return-type': 'off',
|
||||
|
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "backend",
|
||||
"version": "1.0.0",
|
||||
"version": "1.1.2",
|
||||
"private": true,
|
||||
"homepage": "https://github.com/movie-web/backend",
|
||||
"engines": {
|
||||
@@ -15,7 +15,9 @@
|
||||
"build:pre": "rimraf dist/",
|
||||
"build:compile": "tsc && tsc-alias",
|
||||
"preinstall": "npx -y only-allow pnpm",
|
||||
"migration:create": "npx -y mikro-orm migration:create"
|
||||
"migration:create": "npx -y mikro-orm migration:create",
|
||||
"migration:up": "npx -y mikro-orm migration:up",
|
||||
"migration:down": "npx -y mikro-orm migration:down"
|
||||
},
|
||||
"mikro-orm": {
|
||||
"useTsNode": true,
|
||||
@@ -44,7 +46,7 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"@fastify/cors": "^8.3.0",
|
||||
"@mikro-orm/core": "^5.9.0",
|
||||
"@mikro-orm/core": "^5.9.2",
|
||||
"@mikro-orm/postgresql": "^5.9.2",
|
||||
"@types/ms": "^0.7.33",
|
||||
"async-ratelimiter": "^1.3.12",
|
||||
|
127
pnpm-lock.yaml
generated
127
pnpm-lock.yaml
generated
@@ -9,11 +9,11 @@ dependencies:
|
||||
specifier: ^8.3.0
|
||||
version: 8.4.0
|
||||
'@mikro-orm/core':
|
||||
specifier: ^5.9.0
|
||||
version: 5.9.0(@mikro-orm/migrations@5.9.2)(@mikro-orm/postgresql@5.9.2)
|
||||
specifier: ^5.9.2
|
||||
version: 5.9.2(@mikro-orm/migrations@5.9.2)(@mikro-orm/postgresql@5.9.2)
|
||||
'@mikro-orm/postgresql':
|
||||
specifier: ^5.9.2
|
||||
version: 5.9.2(@mikro-orm/core@5.9.0)(@mikro-orm/migrations@5.9.2)
|
||||
version: 5.9.2(@mikro-orm/core@5.9.2)(@mikro-orm/migrations@5.9.2)
|
||||
'@types/ms':
|
||||
specifier: ^0.7.33
|
||||
version: 0.7.33
|
||||
@@ -72,7 +72,7 @@ devDependencies:
|
||||
version: 5.9.2(@mikro-orm/migrations@5.9.2)(@mikro-orm/postgresql@5.9.2)(pg@8.11.3)
|
||||
'@mikro-orm/migrations':
|
||||
specifier: ^5.9.2
|
||||
version: 5.9.2(@mikro-orm/core@5.9.0)(pg@8.11.3)
|
||||
version: 5.9.2(@mikro-orm/core@5.9.2)(pg@8.11.3)
|
||||
'@types/jsonwebtoken':
|
||||
specifier: ^9.0.4
|
||||
version: 9.0.4
|
||||
@@ -322,8 +322,8 @@ packages:
|
||||
'@jercle/yargonaut': 1.1.5
|
||||
'@mikro-orm/core': 5.9.2(@mikro-orm/migrations@5.9.2)(@mikro-orm/postgresql@5.9.2)
|
||||
'@mikro-orm/knex': 5.9.2(@mikro-orm/core@5.9.2)(@mikro-orm/migrations@5.9.2)(pg@8.11.3)
|
||||
'@mikro-orm/migrations': 5.9.2(@mikro-orm/core@5.9.0)(pg@8.11.3)
|
||||
'@mikro-orm/postgresql': 5.9.2(@mikro-orm/core@5.9.0)(@mikro-orm/migrations@5.9.2)
|
||||
'@mikro-orm/migrations': 5.9.2(@mikro-orm/core@5.9.2)(pg@8.11.3)
|
||||
'@mikro-orm/postgresql': 5.9.2(@mikro-orm/core@5.9.2)(@mikro-orm/migrations@5.9.2)
|
||||
fs-extra: 11.1.1
|
||||
tsconfig-paths: 4.2.0
|
||||
yargs: 17.7.2
|
||||
@@ -339,52 +339,6 @@ packages:
|
||||
- tedious
|
||||
dev: true
|
||||
|
||||
/@mikro-orm/core@5.9.0(@mikro-orm/migrations@5.9.2)(@mikro-orm/postgresql@5.9.2):
|
||||
resolution: {integrity: sha512-w+fc7HrjY30LCUkMOsKGK3b7gPICnrRX1GHjMXRkST8pQLx3U8sc0AyUyqNMta+LVZf0P+KIDvIwuy9Eo4iDvQ==}
|
||||
engines: {node: '>= 14.0.0'}
|
||||
peerDependencies:
|
||||
'@mikro-orm/better-sqlite': ^5.0.0
|
||||
'@mikro-orm/entity-generator': ^5.0.0
|
||||
'@mikro-orm/mariadb': ^5.0.0
|
||||
'@mikro-orm/migrations': ^5.0.0
|
||||
'@mikro-orm/migrations-mongodb': ^5.0.0
|
||||
'@mikro-orm/mongodb': ^5.0.0
|
||||
'@mikro-orm/mysql': ^5.0.0
|
||||
'@mikro-orm/postgresql': ^5.0.0
|
||||
'@mikro-orm/seeder': ^5.0.0
|
||||
'@mikro-orm/sqlite': ^5.0.0
|
||||
peerDependenciesMeta:
|
||||
'@mikro-orm/better-sqlite':
|
||||
optional: true
|
||||
'@mikro-orm/entity-generator':
|
||||
optional: true
|
||||
'@mikro-orm/mariadb':
|
||||
optional: true
|
||||
'@mikro-orm/migrations':
|
||||
optional: true
|
||||
'@mikro-orm/migrations-mongodb':
|
||||
optional: true
|
||||
'@mikro-orm/mongodb':
|
||||
optional: true
|
||||
'@mikro-orm/mysql':
|
||||
optional: true
|
||||
'@mikro-orm/postgresql':
|
||||
optional: true
|
||||
'@mikro-orm/seeder':
|
||||
optional: true
|
||||
'@mikro-orm/sqlite':
|
||||
optional: true
|
||||
dependencies:
|
||||
'@mikro-orm/migrations': 5.9.2(@mikro-orm/core@5.9.0)(pg@8.11.3)
|
||||
'@mikro-orm/postgresql': 5.9.2(@mikro-orm/core@5.9.0)(@mikro-orm/migrations@5.9.2)
|
||||
acorn-loose: 8.3.0
|
||||
acorn-walk: 8.2.0
|
||||
dotenv: 16.3.1
|
||||
fs-extra: 11.1.1
|
||||
globby: 11.1.0
|
||||
mikro-orm: 5.9.0
|
||||
reflect-metadata: 0.1.13
|
||||
|
||||
/@mikro-orm/core@5.9.2(@mikro-orm/migrations@5.9.2)(@mikro-orm/postgresql@5.9.2):
|
||||
resolution: {integrity: sha512-/KzF8MSrrw0ojVRdMEiL5/LdPUh23PRFXyIRgXB+AvyAdtlsztZKEZyBgJMLUE0nVSVrGhPjkLquV2pmVxgyAw==}
|
||||
engines: {node: '>= 14.0.0'}
|
||||
@@ -421,8 +375,8 @@ packages:
|
||||
'@mikro-orm/sqlite':
|
||||
optional: true
|
||||
dependencies:
|
||||
'@mikro-orm/migrations': 5.9.2(@mikro-orm/core@5.9.0)(pg@8.11.3)
|
||||
'@mikro-orm/postgresql': 5.9.2(@mikro-orm/core@5.9.0)(@mikro-orm/migrations@5.9.2)
|
||||
'@mikro-orm/migrations': 5.9.2(@mikro-orm/core@5.9.2)(pg@8.11.3)
|
||||
'@mikro-orm/postgresql': 5.9.2(@mikro-orm/core@5.9.2)(@mikro-orm/migrations@5.9.2)
|
||||
acorn-loose: 8.3.0
|
||||
acorn-walk: 8.2.0
|
||||
dotenv: 16.3.1
|
||||
@@ -430,49 +384,6 @@ packages:
|
||||
globby: 11.1.0
|
||||
mikro-orm: 5.9.2
|
||||
reflect-metadata: 0.1.13
|
||||
dev: true
|
||||
|
||||
/@mikro-orm/knex@5.9.2(@mikro-orm/core@5.9.0)(@mikro-orm/migrations@5.9.2)(pg@8.11.3):
|
||||
resolution: {integrity: sha512-bWBuEHQurYIuIZkIA9JaNt5tX1zSNZyiG8vrYYnn6nUZt8ZQVsMmleajljI56QJGTrsCQ/2GBWaF2S6Y86JoDw==}
|
||||
engines: {node: '>= 14.0.0'}
|
||||
peerDependencies:
|
||||
'@mikro-orm/core': ^5.0.0
|
||||
'@mikro-orm/entity-generator': ^5.0.0
|
||||
'@mikro-orm/migrations': ^5.0.0
|
||||
better-sqlite3: '*'
|
||||
mssql: '*'
|
||||
mysql: '*'
|
||||
mysql2: '*'
|
||||
pg: '*'
|
||||
sqlite3: '*'
|
||||
peerDependenciesMeta:
|
||||
'@mikro-orm/entity-generator':
|
||||
optional: true
|
||||
'@mikro-orm/migrations':
|
||||
optional: true
|
||||
better-sqlite3:
|
||||
optional: true
|
||||
mssql:
|
||||
optional: true
|
||||
mysql:
|
||||
optional: true
|
||||
mysql2:
|
||||
optional: true
|
||||
pg:
|
||||
optional: true
|
||||
sqlite3:
|
||||
optional: true
|
||||
dependencies:
|
||||
'@mikro-orm/core': 5.9.0(@mikro-orm/migrations@5.9.2)(@mikro-orm/postgresql@5.9.2)
|
||||
'@mikro-orm/migrations': 5.9.2(@mikro-orm/core@5.9.0)(pg@8.11.3)
|
||||
fs-extra: 11.1.1
|
||||
knex: 2.5.1(pg@8.11.3)
|
||||
pg: 8.11.3
|
||||
sqlstring: 2.3.3
|
||||
transitivePeerDependencies:
|
||||
- pg-native
|
||||
- supports-color
|
||||
- tedious
|
||||
|
||||
/@mikro-orm/knex@5.9.2(@mikro-orm/core@5.9.2)(@mikro-orm/migrations@5.9.2)(pg@8.11.3):
|
||||
resolution: {integrity: sha512-bWBuEHQurYIuIZkIA9JaNt5tX1zSNZyiG8vrYYnn6nUZt8ZQVsMmleajljI56QJGTrsCQ/2GBWaF2S6Y86JoDw==}
|
||||
@@ -506,7 +417,7 @@ packages:
|
||||
optional: true
|
||||
dependencies:
|
||||
'@mikro-orm/core': 5.9.2(@mikro-orm/migrations@5.9.2)(@mikro-orm/postgresql@5.9.2)
|
||||
'@mikro-orm/migrations': 5.9.2(@mikro-orm/core@5.9.0)(pg@8.11.3)
|
||||
'@mikro-orm/migrations': 5.9.2(@mikro-orm/core@5.9.2)(pg@8.11.3)
|
||||
fs-extra: 11.1.1
|
||||
knex: 2.5.1(pg@8.11.3)
|
||||
pg: 8.11.3
|
||||
@@ -515,16 +426,15 @@ packages:
|
||||
- pg-native
|
||||
- supports-color
|
||||
- tedious
|
||||
dev: true
|
||||
|
||||
/@mikro-orm/migrations@5.9.2(@mikro-orm/core@5.9.0)(pg@8.11.3):
|
||||
/@mikro-orm/migrations@5.9.2(@mikro-orm/core@5.9.2)(pg@8.11.3):
|
||||
resolution: {integrity: sha512-DmWP5vGvW25JtyiVBz1J6GwRY9y3sbrdnQj4PNGOXKGE6qFX8znEjM2Lx7OubEohGfnXVW8QPTG0Lb7hu9bWQQ==}
|
||||
engines: {node: '>= 14.0.0'}
|
||||
peerDependencies:
|
||||
'@mikro-orm/core': ^5.0.0
|
||||
dependencies:
|
||||
'@mikro-orm/core': 5.9.0(@mikro-orm/migrations@5.9.2)(@mikro-orm/postgresql@5.9.2)
|
||||
'@mikro-orm/knex': 5.9.2(@mikro-orm/core@5.9.0)(@mikro-orm/migrations@5.9.2)(pg@8.11.3)
|
||||
'@mikro-orm/core': 5.9.2(@mikro-orm/migrations@5.9.2)(@mikro-orm/postgresql@5.9.2)
|
||||
'@mikro-orm/knex': 5.9.2(@mikro-orm/core@5.9.2)(@mikro-orm/migrations@5.9.2)(pg@8.11.3)
|
||||
fs-extra: 11.1.1
|
||||
knex: 2.5.1(pg@8.11.3)
|
||||
umzug: 3.3.1
|
||||
@@ -540,7 +450,7 @@ packages:
|
||||
- supports-color
|
||||
- tedious
|
||||
|
||||
/@mikro-orm/postgresql@5.9.2(@mikro-orm/core@5.9.0)(@mikro-orm/migrations@5.9.2):
|
||||
/@mikro-orm/postgresql@5.9.2(@mikro-orm/core@5.9.2)(@mikro-orm/migrations@5.9.2):
|
||||
resolution: {integrity: sha512-aTrIJ0QYH89Lwt8UH2gwdAuWB3zwHGW+JuW9SJukj08L4V8bA9d7D4dv/alPHf9g1z47SLI84lAA2AR+At0FIw==}
|
||||
engines: {node: '>= 14.0.0'}
|
||||
peerDependencies:
|
||||
@@ -556,9 +466,9 @@ packages:
|
||||
'@mikro-orm/seeder':
|
||||
optional: true
|
||||
dependencies:
|
||||
'@mikro-orm/core': 5.9.0(@mikro-orm/migrations@5.9.2)(@mikro-orm/postgresql@5.9.2)
|
||||
'@mikro-orm/knex': 5.9.2(@mikro-orm/core@5.9.0)(@mikro-orm/migrations@5.9.2)(pg@8.11.3)
|
||||
'@mikro-orm/migrations': 5.9.2(@mikro-orm/core@5.9.0)(pg@8.11.3)
|
||||
'@mikro-orm/core': 5.9.2(@mikro-orm/migrations@5.9.2)(@mikro-orm/postgresql@5.9.2)
|
||||
'@mikro-orm/knex': 5.9.2(@mikro-orm/core@5.9.2)(@mikro-orm/migrations@5.9.2)(pg@8.11.3)
|
||||
'@mikro-orm/migrations': 5.9.2(@mikro-orm/core@5.9.2)(pg@8.11.3)
|
||||
pg: 8.11.3
|
||||
transitivePeerDependencies:
|
||||
- better-sqlite3
|
||||
@@ -2166,14 +2076,9 @@ packages:
|
||||
braces: 3.0.2
|
||||
picomatch: 2.3.1
|
||||
|
||||
/mikro-orm@5.9.0:
|
||||
resolution: {integrity: sha512-havRz1BdUY22zxcnkKulrkC7m+blFDvWD4pZ7cg1GLtMXcl0F68G2pJb9n6iRG18824DxM+qOKsoQfFwY1PbxA==}
|
||||
engines: {node: '>= 14.0.0'}
|
||||
|
||||
/mikro-orm@5.9.2:
|
||||
resolution: {integrity: sha512-EgWEVtmyNNa2rHzySGhr3SATBQ/rnbxZfuqhWTsM3wsWG1/piz/rROekCzM+2QEaXe/WgPaVYRQS/wBGnDGQTA==}
|
||||
engines: {node: '>= 14.0.0'}
|
||||
dev: true
|
||||
|
||||
/mimic-fn@2.1.0:
|
||||
resolution: {integrity: sha512-OqbOk5oEQeAZ8WXWydlu9HJjz9WVdEIvamMCcXmuqUYjTknH/sqsWvhQ3vgwKFRR1HpjvNBKQ37nbJgYzGqGcg==}
|
||||
|
@@ -2,7 +2,7 @@ import { FragmentSchema } from '@/config/fragments/types';
|
||||
|
||||
export const devFragment: FragmentSchema = {
|
||||
server: {
|
||||
cors: 'http://localhost:5173',
|
||||
allowAnySite: true,
|
||||
trustProxy: true,
|
||||
},
|
||||
logging: {
|
||||
|
@@ -8,7 +8,7 @@ const fragments = {
|
||||
dockerdev: dockerFragment,
|
||||
};
|
||||
|
||||
export const version = '1.0.0';
|
||||
export const version = process.env.npm_package_version ?? 'unknown';
|
||||
|
||||
export const conf = createConfigLoader()
|
||||
.addFromEnvironment('MWB_')
|
||||
|
20
src/config/orm.ts
Normal file
20
src/config/orm.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import { createConfigLoader } from 'neat-config';
|
||||
import { z } from 'zod';
|
||||
|
||||
export const ormConfigSchema = z.object({
|
||||
postgres: z.object({
|
||||
// connection URL for postgres database
|
||||
connection: z.string(),
|
||||
}),
|
||||
});
|
||||
|
||||
export const ormConf = createConfigLoader()
|
||||
.addFromEnvironment('MWB_')
|
||||
.addFromCLI('mwb-')
|
||||
.addFromFile('.env', {
|
||||
prefix: 'MWB_',
|
||||
})
|
||||
.addFromFile('config.json')
|
||||
.addZodSchema(ormConfigSchema)
|
||||
.freeze()
|
||||
.load();
|
@@ -9,6 +9,10 @@ export const configSchema = z.object({
|
||||
// space seperated list of allowed cors domains
|
||||
cors: z.string().default(''),
|
||||
|
||||
// disable cross origin restrictions, allow any site.
|
||||
// overwrites the cors option above
|
||||
allowAnySite: z.coerce.boolean().default(false),
|
||||
|
||||
// should it trust reverse proxy headers? (for ip gathering)
|
||||
trustProxy: z.coerce.boolean().default(false),
|
||||
|
||||
@@ -37,6 +41,10 @@ export const configSchema = z.object({
|
||||
// will always keep the database schema in sync with the connected database
|
||||
// it is extremely destructive, do not use it EVER in production
|
||||
syncSchema: z.coerce.boolean().default(false),
|
||||
|
||||
// Enable debug logging for MikroORM - Outputs queries and entity management logs
|
||||
// Do NOT use in production, leaks all sensitive data
|
||||
debugLogging: z.coerce.boolean().default(false),
|
||||
}),
|
||||
crypto: z.object({
|
||||
// session secret. used for signing session tokens
|
||||
|
@@ -184,6 +184,24 @@
|
||||
"nullable": true,
|
||||
"mappedType": "string"
|
||||
},
|
||||
"season_number": {
|
||||
"name": "season_number",
|
||||
"type": "int",
|
||||
"unsigned": false,
|
||||
"autoincrement": false,
|
||||
"primary": false,
|
||||
"nullable": true,
|
||||
"mappedType": "integer"
|
||||
},
|
||||
"episode_number": {
|
||||
"name": "episode_number",
|
||||
"type": "int",
|
||||
"unsigned": false,
|
||||
"autoincrement": false,
|
||||
"primary": false,
|
||||
"nullable": true,
|
||||
"mappedType": "integer"
|
||||
},
|
||||
"meta": {
|
||||
"name": "meta",
|
||||
"type": "jsonb",
|
||||
@@ -345,20 +363,29 @@
|
||||
},
|
||||
"error_message": {
|
||||
"name": "error_message",
|
||||
"type": "varchar(255)",
|
||||
"type": "text",
|
||||
"unsigned": false,
|
||||
"autoincrement": false,
|
||||
"primary": false,
|
||||
"nullable": true,
|
||||
"mappedType": "string"
|
||||
"mappedType": "text"
|
||||
},
|
||||
"full_error": {
|
||||
"name": "full_error",
|
||||
"type": "varchar(255)",
|
||||
"type": "text",
|
||||
"unsigned": false,
|
||||
"autoincrement": false,
|
||||
"primary": false,
|
||||
"nullable": true,
|
||||
"mappedType": "text"
|
||||
},
|
||||
"hostname": {
|
||||
"name": "hostname",
|
||||
"type": "varchar(255)",
|
||||
"unsigned": false,
|
||||
"autoincrement": false,
|
||||
"primary": false,
|
||||
"nullable": false,
|
||||
"mappedType": "string"
|
||||
}
|
||||
},
|
||||
@@ -560,12 +587,12 @@
|
||||
"columns": {
|
||||
"id": {
|
||||
"name": "id",
|
||||
"type": "uuid",
|
||||
"type": "text",
|
||||
"unsigned": false,
|
||||
"autoincrement": false,
|
||||
"primary": false,
|
||||
"nullable": false,
|
||||
"mappedType": "uuid"
|
||||
"mappedType": "text"
|
||||
},
|
||||
"application_theme": {
|
||||
"name": "application_theme",
|
||||
|
14
src/db/migrations/Migration20231105150807.ts
Normal file
14
src/db/migrations/Migration20231105150807.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import { Migration } from '@mikro-orm/migrations';
|
||||
|
||||
export class Migration20231105150807 extends Migration {
|
||||
async up(): Promise<void> {
|
||||
this.addSql(
|
||||
'alter table "progress_items" add column "season_number" int null, add column "episode_number" int null;',
|
||||
);
|
||||
}
|
||||
|
||||
async down(): Promise<void> {
|
||||
this.addSql('alter table "progress_items" drop column "season_number";');
|
||||
this.addSql('alter table "progress_items" drop column "episode_number";');
|
||||
}
|
||||
}
|
25
src/db/migrations/Migration20231111160045.ts
Normal file
25
src/db/migrations/Migration20231111160045.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import { Migration } from '@mikro-orm/migrations';
|
||||
|
||||
export class Migration20231111160045 extends Migration {
|
||||
async up(): Promise<void> {
|
||||
this.addSql(
|
||||
'alter table "provider_metrics" add column "hostname" varchar(255) not null;',
|
||||
);
|
||||
this.addSql(
|
||||
'alter table "provider_metrics" alter column "error_message" type text using ("error_message"::text);',
|
||||
);
|
||||
this.addSql(
|
||||
'alter table "provider_metrics" alter column "full_error" type text using ("full_error"::text);',
|
||||
);
|
||||
}
|
||||
|
||||
async down(): Promise<void> {
|
||||
this.addSql(
|
||||
'alter table "provider_metrics" alter column "error_message" type varchar(255) using ("error_message"::varchar(255));',
|
||||
);
|
||||
this.addSql(
|
||||
'alter table "provider_metrics" alter column "full_error" type varchar(255) using ("full_error"::varchar(255));',
|
||||
);
|
||||
this.addSql('alter table "provider_metrics" drop column "hostname";');
|
||||
}
|
||||
}
|
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);');
|
||||
}
|
||||
|
||||
}
|
@@ -6,7 +6,7 @@ export const progressMetaSchema = z.object({
|
||||
title: z.string(),
|
||||
year: z.number(),
|
||||
poster: z.string().optional(),
|
||||
type: z.string(),
|
||||
type: z.union([z.literal('show'), z.literal('movie')]),
|
||||
});
|
||||
|
||||
export type ProgressMeta = z.infer<typeof progressMetaSchema>;
|
||||
@@ -29,6 +29,12 @@ export class ProgressItem {
|
||||
@Property({ name: 'episode_id', nullable: true })
|
||||
episodeId?: string;
|
||||
|
||||
@Property({ name: 'season_number', nullable: true })
|
||||
seasonNumber?: number;
|
||||
|
||||
@Property({ name: 'episode_number', nullable: true })
|
||||
episodeNumber?: number;
|
||||
|
||||
@Property({
|
||||
name: 'meta',
|
||||
type: types.json,
|
||||
@@ -47,9 +53,16 @@ export class ProgressItem {
|
||||
}
|
||||
|
||||
export interface ProgressItemDTO {
|
||||
id: string;
|
||||
tmdbId: string;
|
||||
seasonId?: string;
|
||||
episodeId?: string;
|
||||
season: {
|
||||
id?: string;
|
||||
number?: number;
|
||||
};
|
||||
episode: {
|
||||
id?: string;
|
||||
number?: number;
|
||||
};
|
||||
meta: {
|
||||
title: string;
|
||||
year: number;
|
||||
@@ -65,9 +78,16 @@ export function formatProgressItem(
|
||||
progressItem: ProgressItem,
|
||||
): ProgressItemDTO {
|
||||
return {
|
||||
id: progressItem.id,
|
||||
tmdbId: progressItem.tmdbId,
|
||||
seasonId: progressItem.seasonId,
|
||||
episodeId: progressItem.episodeId,
|
||||
episode: {
|
||||
id: progressItem.episodeId,
|
||||
number: progressItem.episodeNumber,
|
||||
},
|
||||
season: {
|
||||
id: progressItem.seasonId,
|
||||
number: progressItem.seasonNumber,
|
||||
},
|
||||
meta: {
|
||||
title: progressItem.meta.title,
|
||||
year: progressItem.meta.year,
|
||||
|
@@ -40,9 +40,12 @@ export class ProviderMetric {
|
||||
@Property({ name: 'embed_id', nullable: true })
|
||||
embedId?: string;
|
||||
|
||||
@Property({ name: 'error_message', nullable: true })
|
||||
@Property({ name: 'error_message', nullable: true, type: 'text' })
|
||||
errorMessage?: string;
|
||||
|
||||
@Property({ name: 'full_error', nullable: true })
|
||||
@Property({ name: 'full_error', nullable: true, type: 'text' })
|
||||
fullError?: string;
|
||||
|
||||
@Property({ name: 'hostname' })
|
||||
hostname!: string;
|
||||
}
|
||||
|
@@ -1,10 +1,9 @@
|
||||
import { Entity, PrimaryKey, Property } from '@mikro-orm/core';
|
||||
import { randomUUID } from 'crypto';
|
||||
|
||||
@Entity({ tableName: 'user_settings' })
|
||||
export class UserSettings {
|
||||
@PrimaryKey({ name: 'id', type: 'uuid' })
|
||||
id: string = randomUUID();
|
||||
@PrimaryKey({ name: 'id', type: 'text' })
|
||||
id!: string;
|
||||
|
||||
@Property({ name: 'application_theme', nullable: true })
|
||||
applicationTheme?: string | null;
|
||||
|
@@ -1,4 +1,4 @@
|
||||
import { ormConf } from '@/config/orm';
|
||||
import { makeOrmConfig } from '@/modules/mikro/orm';
|
||||
import { conf } from '@/config';
|
||||
|
||||
export default makeOrmConfig(conf.postgres.connection);
|
||||
export default makeOrmConfig(ormConf.postgres.connection);
|
||||
|
@@ -56,8 +56,10 @@ export async function setupFastify(): Promise<FastifyInstance> {
|
||||
|
||||
// plugins
|
||||
log.info(`setting up plugins`, { evt: 'setup-plugins' });
|
||||
const corsDomains = conf.server.cors.split(' ').filter((v) => v.length > 0);
|
||||
const corsSetting = conf.server.allowAnySite ? true : corsDomains;
|
||||
await app.register(cors, {
|
||||
origin: conf.server.cors.split(' ').filter((v) => v.length > 0),
|
||||
origin: corsSetting,
|
||||
credentials: true,
|
||||
});
|
||||
|
||||
|
@@ -18,6 +18,7 @@ export type Metrics = {
|
||||
| 'type'
|
||||
| 'provider_id'
|
||||
| 'embed_id'
|
||||
| 'hostname'
|
||||
>;
|
||||
};
|
||||
|
||||
@@ -57,6 +58,7 @@ export async function setupMetrics(app: FastifyInstance) {
|
||||
'tmdb_id',
|
||||
'type',
|
||||
'embed_id',
|
||||
'hostname',
|
||||
],
|
||||
}),
|
||||
};
|
||||
|
@@ -14,8 +14,10 @@ export function getORM() {
|
||||
|
||||
export async function setupMikroORM() {
|
||||
log.info(`Connecting to postgres`, { evt: 'connecting' });
|
||||
const mikro = await createORM(conf.postgres.connection, (msg) =>
|
||||
log.info(msg),
|
||||
const mikro = await createORM(
|
||||
conf.postgres.connection,
|
||||
conf.postgres.debugLogging,
|
||||
(msg) => log.info(msg),
|
||||
);
|
||||
|
||||
if (conf.postgres.syncSchema) {
|
||||
|
@@ -16,9 +16,14 @@ export function makeOrmConfig(url: string): Options<PostgreSqlDriver> {
|
||||
};
|
||||
}
|
||||
|
||||
export async function createORM(url: string, log: (msg: string) => void) {
|
||||
export async function createORM(
|
||||
url: string,
|
||||
debug: boolean,
|
||||
log: (msg: string) => void,
|
||||
) {
|
||||
return await MikroORM.init<PostgreSqlDriver>({
|
||||
...makeOrmConfig(url),
|
||||
logger: log,
|
||||
debug,
|
||||
});
|
||||
}
|
||||
|
@@ -1,6 +1,6 @@
|
||||
import { ChallengeCode } from '@/db/models/ChallengeCode';
|
||||
import { formatSession } from '@/db/models/Session';
|
||||
import { User } from '@/db/models/User';
|
||||
import { User, formatUser } from '@/db/models/User';
|
||||
import { assertChallengeCode } from '@/services/challenge';
|
||||
import { StatusError } from '@/services/error';
|
||||
import { handle } from '@/services/handler';
|
||||
@@ -85,6 +85,7 @@ export const loginAuthRouter = makeRouter((app) => {
|
||||
await em.persistAndFlush([session, user]);
|
||||
|
||||
return {
|
||||
user: formatUser(user),
|
||||
session: formatSession(session),
|
||||
token: makeSessionToken(session),
|
||||
};
|
||||
|
@@ -1,17 +1,21 @@
|
||||
import { conf } from '@/config';
|
||||
import { conf, version } from '@/config';
|
||||
import { handle } from '@/services/handler';
|
||||
import { makeRouter } from '@/services/router';
|
||||
|
||||
export const metaRouter = makeRouter((app) => {
|
||||
app.get(
|
||||
'/healthcheck',
|
||||
handle(async ({ em }) => {
|
||||
handle(async ({ em, res }) => {
|
||||
const databaseConnected = await em.config
|
||||
.getDriver()
|
||||
.getConnection()
|
||||
.isConnected();
|
||||
|
||||
const healthy = databaseConnected;
|
||||
if (!healthy) res.status(503);
|
||||
|
||||
return {
|
||||
healthy: databaseConnected,
|
||||
healthy,
|
||||
databaseConnected,
|
||||
};
|
||||
}),
|
||||
@@ -22,6 +26,7 @@ export const metaRouter = makeRouter((app) => {
|
||||
return {
|
||||
name: conf.meta.name,
|
||||
description: conf.meta.description,
|
||||
version: version,
|
||||
hasCaptcha: conf.captcha.enabled,
|
||||
captchaClientKey: conf.captcha.clientKey,
|
||||
};
|
||||
|
@@ -37,19 +37,25 @@ export const metricsRouter = makeRouter((app) => {
|
||||
window: '30m',
|
||||
});
|
||||
|
||||
const hostname = req.headers.origin?.slice(0, 255) ?? 'unknown origin';
|
||||
|
||||
const entities = body.items.map((v) => {
|
||||
const errorMessage = v.errorMessage?.slice(0, 200);
|
||||
const truncatedFullError = v.fullError?.slice(0, 2000);
|
||||
|
||||
const metric = new ProviderMetric();
|
||||
em.assign(metric, {
|
||||
providerId: v.providerId,
|
||||
embedId: v.embedId,
|
||||
fullError: v.fullError,
|
||||
errorMessage: v.errorMessage,
|
||||
fullError: truncatedFullError,
|
||||
errorMessage: errorMessage,
|
||||
episodeId: v.episodeId,
|
||||
seasonId: v.seasonId,
|
||||
status: v.status,
|
||||
title: v.title,
|
||||
tmdbId: v.tmdbId,
|
||||
type: v.type,
|
||||
hostname,
|
||||
});
|
||||
return metric;
|
||||
});
|
||||
@@ -63,6 +69,7 @@ export const metricsRouter = makeRouter((app) => {
|
||||
title: entity.title,
|
||||
tmdb_id: entity.tmdbId,
|
||||
type: entity.type,
|
||||
hostname,
|
||||
});
|
||||
});
|
||||
|
||||
|
@@ -6,8 +6,14 @@ import {
|
||||
import { StatusError } from '@/services/error';
|
||||
import { handle } from '@/services/handler';
|
||||
import { makeRouter } from '@/services/router';
|
||||
import { randomUUID } from 'crypto';
|
||||
import { z } from 'zod';
|
||||
|
||||
const bookmarkDataSchema = z.object({
|
||||
tmdbId: z.string(),
|
||||
meta: bookmarkMetaSchema,
|
||||
});
|
||||
|
||||
export const userBookmarkRouter = makeRouter((app) => {
|
||||
app.get(
|
||||
'/users/:uid/bookmarks',
|
||||
@@ -40,9 +46,7 @@ export const userBookmarkRouter = makeRouter((app) => {
|
||||
uid: z.string(),
|
||||
tmdbid: z.string(),
|
||||
}),
|
||||
body: z.object({
|
||||
meta: bookmarkMetaSchema,
|
||||
}),
|
||||
body: bookmarkDataSchema,
|
||||
},
|
||||
},
|
||||
handle(async ({ auth, params, body, em }) => {
|
||||
@@ -70,6 +74,40 @@ export const userBookmarkRouter = makeRouter((app) => {
|
||||
}),
|
||||
);
|
||||
|
||||
app.put(
|
||||
'/users/:uid/bookmarks',
|
||||
{
|
||||
schema: {
|
||||
params: z.object({
|
||||
uid: z.string(),
|
||||
}),
|
||||
body: z.array(bookmarkDataSchema),
|
||||
},
|
||||
},
|
||||
handle(async ({ auth, params, body, em }) => {
|
||||
await auth.assert();
|
||||
|
||||
if (auth.user.id !== params.uid)
|
||||
throw new StatusError('Cannot modify user other than yourself', 403);
|
||||
|
||||
const bookmarks = await em.upsertMany(
|
||||
Bookmark,
|
||||
body.map((item) => ({
|
||||
userId: params.uid,
|
||||
tmdbId: item.tmdbId,
|
||||
meta: item.meta,
|
||||
updatedAt: new Date(),
|
||||
})),
|
||||
{
|
||||
onConflictFields: ['tmdbId', 'userId'],
|
||||
},
|
||||
);
|
||||
|
||||
await em.flush();
|
||||
return bookmarks.map(formatBookmark);
|
||||
}),
|
||||
);
|
||||
|
||||
app.delete(
|
||||
'/users/:uid/bookmarks/:tmdbid',
|
||||
{
|
||||
|
@@ -1,3 +1,4 @@
|
||||
import { formatSession } from '@/db/models/Session';
|
||||
import { User, formatUser } from '@/db/models/User';
|
||||
import { StatusError } from '@/services/error';
|
||||
import { handle } from '@/services/handler';
|
||||
@@ -5,6 +6,24 @@ import { makeRouter } from '@/services/router';
|
||||
import { z } from 'zod';
|
||||
|
||||
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(
|
||||
'/users/:uid',
|
||||
{
|
||||
@@ -16,16 +35,16 @@ export const userGetRouter = makeRouter((app) => {
|
||||
},
|
||||
handle(async ({ auth, params, em }) => {
|
||||
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);
|
||||
|
||||
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);
|
||||
|
||||
return formatUser(user);
|
||||
return {
|
||||
user: formatUser(user),
|
||||
};
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
@@ -6,8 +6,20 @@ import {
|
||||
import { StatusError } from '@/services/error';
|
||||
import { handle } from '@/services/handler';
|
||||
import { makeRouter } from '@/services/router';
|
||||
import { randomUUID } from 'crypto';
|
||||
import { z } from 'zod';
|
||||
|
||||
const progressItemSchema = z.object({
|
||||
meta: progressMetaSchema,
|
||||
tmdbId: z.string(),
|
||||
duration: z.number().transform((n) => Math.round(n)),
|
||||
watched: z.number().transform((n) => Math.round(n)),
|
||||
seasonId: z.string().optional(),
|
||||
episodeId: z.string().optional(),
|
||||
seasonNumber: z.number().optional(),
|
||||
episodeNumber: z.number().optional(),
|
||||
});
|
||||
|
||||
export const userProgressRouter = makeRouter((app) => {
|
||||
app.put(
|
||||
'/users/:uid/progress/:tmdbid',
|
||||
@@ -17,13 +29,7 @@ export const userProgressRouter = makeRouter((app) => {
|
||||
uid: z.string(),
|
||||
tmdbid: z.string(),
|
||||
}),
|
||||
body: z.object({
|
||||
meta: progressMetaSchema,
|
||||
seasonId: z.string().optional(),
|
||||
episodeId: z.string().optional(),
|
||||
duration: z.number(),
|
||||
watched: z.number(),
|
||||
}),
|
||||
body: progressItemSchema,
|
||||
},
|
||||
},
|
||||
handle(async ({ auth, params, body, em }) => {
|
||||
@@ -38,12 +44,15 @@ export const userProgressRouter = makeRouter((app) => {
|
||||
episodeId: body.episodeId,
|
||||
seasonId: body.seasonId,
|
||||
});
|
||||
|
||||
if (!progressItem) {
|
||||
progressItem = new ProgressItem();
|
||||
progressItem.tmdbId = params.tmdbid;
|
||||
progressItem.userId = params.uid;
|
||||
progressItem.episodeId = body.episodeId;
|
||||
progressItem.seasonId = body.seasonId;
|
||||
progressItem.episodeNumber = body.episodeNumber;
|
||||
progressItem.seasonNumber = body.seasonNumber;
|
||||
}
|
||||
|
||||
em.assign(progressItem, {
|
||||
@@ -58,6 +67,80 @@ export const userProgressRouter = makeRouter((app) => {
|
||||
}),
|
||||
);
|
||||
|
||||
app.put(
|
||||
'/users/:uid/progress/import',
|
||||
{
|
||||
schema: {
|
||||
params: z.object({
|
||||
uid: z.string(),
|
||||
}),
|
||||
body: z.array(progressItemSchema),
|
||||
},
|
||||
},
|
||||
handle(async ({ auth, params, body, em, req, limiter }) => {
|
||||
await auth.assert();
|
||||
|
||||
if (auth.user.id !== params.uid)
|
||||
throw new StatusError('Cannot modify user other than yourself', 403);
|
||||
|
||||
const itemsUpserted: ProgressItem[] = [];
|
||||
|
||||
const newItems = [...body];
|
||||
|
||||
for (const existingItem of await em.find(ProgressItem, {
|
||||
userId: params.uid,
|
||||
})) {
|
||||
const newItemIndex = newItems.findIndex(
|
||||
(item) =>
|
||||
item.tmdbId == existingItem.tmdbId &&
|
||||
item.seasonId == existingItem.seasonId &&
|
||||
item.episodeId == existingItem.episodeId,
|
||||
);
|
||||
|
||||
if (newItemIndex > -1) {
|
||||
const newItem = newItems[newItemIndex];
|
||||
if (existingItem.watched < newItem.watched) {
|
||||
existingItem.updatedAt = new Date();
|
||||
existingItem.watched = newItem.watched;
|
||||
}
|
||||
itemsUpserted.push(existingItem);
|
||||
|
||||
// Remove the item from the array, we have processed it
|
||||
newItems.splice(newItemIndex, 1);
|
||||
}
|
||||
}
|
||||
|
||||
// All unprocessed items, aka all items that don't already exist
|
||||
for (const newItem of newItems) {
|
||||
itemsUpserted.push({
|
||||
id: randomUUID(),
|
||||
duration: newItem.duration,
|
||||
episodeId: newItem.episodeId,
|
||||
episodeNumber: newItem.episodeNumber,
|
||||
meta: newItem.meta,
|
||||
seasonId: newItem.seasonId,
|
||||
seasonNumber: newItem.seasonNumber,
|
||||
tmdbId: newItem.tmdbId,
|
||||
userId: params.uid,
|
||||
watched: newItem.watched,
|
||||
updatedAt: new Date(),
|
||||
});
|
||||
}
|
||||
|
||||
const progressItems = await em.upsertMany(ProgressItem, itemsUpserted);
|
||||
|
||||
await em.flush();
|
||||
|
||||
await limiter?.assertAndBump(req, {
|
||||
id: 'progress_import',
|
||||
max: 5,
|
||||
window: '10m',
|
||||
});
|
||||
|
||||
return progressItems.map(formatProgressItem);
|
||||
}),
|
||||
);
|
||||
|
||||
app.delete(
|
||||
'/users/:uid/progress/:tmdbid',
|
||||
{
|
||||
|
@@ -64,6 +64,8 @@ export function scopedLogger(service: string, meta: object = {}) {
|
||||
return logger;
|
||||
}
|
||||
|
||||
const ignoredUrls = ['/healthcheck', '/metrics'];
|
||||
|
||||
export function makeFastifyLogger(logger: winston.Logger) {
|
||||
logger.format = winston.format.combine(
|
||||
winston.format((info) => {
|
||||
@@ -78,6 +80,9 @@ export function makeFastifyLogger(logger: winston.Logger) {
|
||||
let url = request.url;
|
||||
try {
|
||||
const pathParts = (request.url as string).split('?', 2);
|
||||
|
||||
if (ignoredUrls.includes(pathParts[0])) return false;
|
||||
|
||||
if (pathParts[1]) {
|
||||
const searchParams = new URLSearchParams(pathParts[1]);
|
||||
pathParts[1] = searchParams.toString();
|
||||
|
Reference in New Issue
Block a user