-
-
Notifications
You must be signed in to change notification settings - Fork 295
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(watchlist): Add media to watchlist #374
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5f1c10d
feat(add watchlist): adding midding functionality from overserr
yalagin b7e3d28
feat(watchlist): add translation for en
yalagin 469f64d
test(watchlist): fix broken test
yalagin c08897b
fix(watchlist): fix github code scanning
yalagin 03316c6
fix(watchlist): add validation for creation request
yalagin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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,148 @@ | ||
import TheMovieDb from '@server/api/themoviedb'; | ||
import { MediaType } from '@server/constants/media'; | ||
import { getRepository } from '@server/datasource'; | ||
import Media from '@server/entity/Media'; | ||
import { User } from '@server/entity/User'; | ||
import type { WatchlistItem } from '@server/interfaces/api/discoverInterfaces'; | ||
import logger from '@server/logger'; | ||
import { | ||
Column, | ||
CreateDateColumn, | ||
Entity, | ||
Index, | ||
ManyToOne, | ||
PrimaryGeneratedColumn, | ||
Unique, | ||
UpdateDateColumn, | ||
} from 'typeorm'; | ||
|
||
export class DuplicateWatchlistRequestError extends Error {} | ||
export class NotFoundError extends Error { | ||
constructor(message = 'Not found') { | ||
super(message); | ||
this.name = 'NotFoundError'; | ||
} | ||
} | ||
|
||
@Entity() | ||
@Unique('UNIQUE_USER_DB', ['tmdbId', 'requestedBy']) | ||
export class Watchlist implements WatchlistItem { | ||
@PrimaryGeneratedColumn() | ||
id: number; | ||
|
||
@Column({ type: 'varchar' }) | ||
public ratingKey = ''; | ||
|
||
@Column({ type: 'varchar' }) | ||
public mediaType: MediaType; | ||
|
||
@Column({ type: 'varchar' }) | ||
title = ''; | ||
|
||
@Column() | ||
@Index() | ||
public tmdbId: number; | ||
|
||
@ManyToOne(() => User, (user) => user.watchlists, { | ||
eager: true, | ||
onDelete: 'CASCADE', | ||
}) | ||
public requestedBy: User; | ||
|
||
@ManyToOne(() => Media, (media) => media.watchlists, { | ||
eager: true, | ||
onDelete: 'CASCADE', | ||
}) | ||
public media: Media; | ||
|
||
@CreateDateColumn() | ||
public createdAt: Date; | ||
|
||
@UpdateDateColumn() | ||
public updatedAt: Date; | ||
|
||
constructor(init?: Partial<Watchlist>) { | ||
Object.assign(this, init); | ||
} | ||
|
||
public static async createWatchlist( | ||
watchlistRequest: Watchlist, | ||
user: User | ||
): Promise<Watchlist> { | ||
const watchlistRepository = getRepository(this); | ||
const mediaRepository = getRepository(Media); | ||
const tmdb = new TheMovieDb(); | ||
|
||
const tmdbMedia = | ||
watchlistRequest.mediaType === MediaType.MOVIE | ||
? await tmdb.getMovie({ movieId: watchlistRequest.tmdbId }) | ||
: await tmdb.getTvShow({ tvId: watchlistRequest.tmdbId }); | ||
|
||
const existing = await watchlistRepository | ||
.createQueryBuilder('watchlist') | ||
.leftJoinAndSelect('watchlist.requestedBy', 'user') | ||
.where('user.id = :userId', { userId: user.id }) | ||
.andWhere('watchlist.tmdbId = :tmdbId', { | ||
tmdbId: watchlistRequest.tmdbId, | ||
}) | ||
.andWhere('watchlist.mediaType = :mediaType', { | ||
mediaType: watchlistRequest.mediaType, | ||
}) | ||
.getMany(); | ||
|
||
if (existing && existing.length > 0) { | ||
logger.warn('Duplicate request for watchlist blocked', { | ||
tmdbId: watchlistRequest.tmdbId, | ||
mediaType: watchlistRequest.mediaType, | ||
label: 'Watchlist', | ||
}); | ||
|
||
throw new DuplicateWatchlistRequestError(); | ||
} | ||
|
||
let media = await mediaRepository.findOne({ | ||
where: { | ||
tmdbId: watchlistRequest.tmdbId, | ||
mediaType: watchlistRequest.mediaType, | ||
}, | ||
}); | ||
|
||
if (!media) { | ||
media = new Media({ | ||
tmdbId: tmdbMedia.id, | ||
tvdbId: tmdbMedia.external_ids.tvdb_id, | ||
mediaType: watchlistRequest.mediaType, | ||
}); | ||
} | ||
|
||
const watchlist = new this({ | ||
...watchlistRequest, | ||
requestedBy: user, | ||
media, | ||
}); | ||
|
||
await mediaRepository.save(media); | ||
await watchlistRepository.save(watchlist); | ||
return watchlist; | ||
} | ||
|
||
public static async deleteWatchlist( | ||
tmdbId: Watchlist['tmdbId'], | ||
user: User | ||
): Promise<Watchlist | null> { | ||
const watchlistRepository = getRepository(this); | ||
const watchlist = await watchlistRepository.findOneBy({ | ||
tmdbId, | ||
requestedBy: { id: user.id }, | ||
}); | ||
if (!watchlist) { | ||
throw new NotFoundError('not Found'); | ||
} | ||
|
||
if (watchlist) { | ||
await watchlistRepository.delete(watchlist.id); | ||
} | ||
|
||
return watchlist; | ||
} | ||
} |
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,19 @@ | ||
import type { MigrationInterface, QueryRunner } from 'typeorm'; | ||
|
||
export class AddWatchlists1682608634546 implements MigrationInterface { | ||
name = 'AddWatchlists1682608634546'; | ||
|
||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query( | ||
`CREATE TABLE "watchlist" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "ratingKey" varchar NOT NULL, "mediaType" varchar NOT NULL, "title" varchar NOT NULL, "tmdbId" integer NOT NULL, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), "updatedAt" datetime NOT NULL DEFAULT (datetime('now')), "requestedById" integer, "mediaId" integer, CONSTRAINT "UNIQUE_USER_DB" UNIQUE ("tmdbId", "requestedById"))` | ||
); | ||
await queryRunner.query( | ||
`CREATE INDEX "IDX_939f205946256cc0d2a1ac51a8" ON "watchlist" ("tmdbId") ` | ||
); | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(`DROP INDEX "IDX_939f205946256cc0d2a1ac51a8"`); | ||
await queryRunner.query(`DROP TABLE "watchlist"`); | ||
} | ||
} |
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,11 @@ | ||
import { getRepository } from '@server/datasource'; | ||
import { Watchlist } from '@server/entity/Watchlist'; | ||
|
||
export const UserRepository = getRepository(Watchlist).extend({ | ||
// findByName(firstName: string, lastName: string) { | ||
// return this.createQueryBuilder("user") | ||
// .where("user.firstName = :firstName", { firstName }) | ||
// .andWhere("user.lastName = :lastName", { lastName }) | ||
// .getMany() | ||
// }, | ||
}); |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check failure
Code scanning / CodeQL
NoSQL database query built from user-controlled sources (experimental)