forked from Fallenbagel/jellyseerr
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
JoaquinOlivero
committed
Aug 16, 2024
1 parent
bd4da6d
commit 4dc3990
Showing
10 changed files
with
407 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import type { MediaType } from '@server/constants/media'; | ||
import { getRepository } from '@server/datasource'; | ||
import type { BlacklistItem } from '@server/interfaces/api/discoverInterfaces'; | ||
import { | ||
Column, | ||
CreateDateColumn, | ||
Entity, | ||
PrimaryGeneratedColumn, | ||
Unique, | ||
} from 'typeorm'; | ||
import type { ZodNumber, ZodOptional, ZodString } from 'zod'; | ||
|
||
@Entity() | ||
@Unique(['tmdbId']) | ||
export class Blacklist implements BlacklistItem { | ||
@PrimaryGeneratedColumn() | ||
public id: number; | ||
|
||
@Column({ type: 'varchar' }) | ||
public mediaType: MediaType; | ||
|
||
@Column({ nullable: true, type: 'varchar' }) | ||
title?: string; | ||
|
||
@Column() | ||
public tmdbId: number; | ||
|
||
@CreateDateColumn() | ||
public createdAt: Date; | ||
|
||
constructor(init?: Partial<Blacklist>) { | ||
Object.assign(this, init); | ||
} | ||
|
||
public static async addToBlacklist({ | ||
blacklistRequest, | ||
}: { | ||
blacklistRequest: { | ||
mediaType: MediaType; | ||
title?: ZodOptional<ZodString>['_output']; | ||
tmdbId: ZodNumber['_output']; | ||
}; | ||
}): Promise<void> { | ||
const blacklistRepository = getRepository(this); | ||
|
||
const blacklistItem = new this({ | ||
...blacklistRequest, | ||
}); | ||
|
||
await blacklistRepository.save(blacklistItem); | ||
return; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { MediaType } from '@server/constants/media'; | ||
import { z } from 'zod'; | ||
|
||
export const blacklistAdd = z.object({ | ||
tmdbId: z.coerce.number(), | ||
mediaType: z.nativeEnum(MediaType), | ||
title: z.coerce.string().optional(), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import type { MigrationInterface, QueryRunner } from 'typeorm'; | ||
|
||
export class AddBlacklist1699901142442 implements MigrationInterface { | ||
name = 'AddBlacklist1699901142442'; | ||
|
||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query( | ||
`CREATE TABLE "blacklist" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "mediaType" varchar NOT NULL, "title" varchar, "tmdbId" integer NOT NULL, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), CONSTRAINT "UQ_6bbafa28411e6046421991ea21c" UNIQUE ("tmdbId"))` | ||
); | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(`DROP TABLE "blacklist"`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { Blacklist } from '@server/entity/Blacklist'; | ||
import { blacklistAdd } from '@server/interfaces/api/blacklistAdd'; | ||
import logger from '@server/logger'; | ||
import { Router } from 'express'; | ||
import { QueryFailedError } from 'typeorm'; | ||
|
||
const blacklistRoutes = Router(); | ||
|
||
blacklistRoutes.post('/', async (req, res, next) => { | ||
try { | ||
if (!req.user) { | ||
return next({ | ||
status: 401, | ||
message: 'You must be logged in to blacklist an item.', | ||
}); | ||
} | ||
|
||
const values = blacklistAdd.parse(req.body); | ||
|
||
await Blacklist.addToBlacklist({ | ||
blacklistRequest: values, | ||
}); | ||
|
||
return res.status(201).send(); | ||
} catch (error) { | ||
if (!(error instanceof Error)) { | ||
return; | ||
} | ||
|
||
if (error instanceof QueryFailedError) { | ||
switch (error.driverError.errno) { | ||
case 19: | ||
return next({ status: 412, message: 'Item already blacklisted' }); | ||
default: | ||
logger.warn('Something wrong with data blacklist', { | ||
tmdbId: req.body.tmdbId, | ||
mediaType: req.body.mediaType, | ||
label: 'Blacklist', | ||
}); | ||
return next({ status: 409, message: 'Something wrong' }); | ||
} | ||
} | ||
|
||
return next({ status: 500, message: error.message }); | ||
} | ||
}); | ||
|
||
export default blacklistRoutes; |
Oops, something went wrong.