From 70c065d64c330196d371941d9294a55da6e3aa37 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 16 Jun 2017 09:54:59 +0200 Subject: Add this context to instance model functions --- server/models/video/video-abuse-interface.ts | 5 +++++ server/models/video/video-abuse.ts | 2 +- server/models/video/video-blacklist-interface.ts | 3 ++- server/models/video/video-blacklist.ts | 2 +- server/models/video/video-interface.ts | 20 ++++++++++---------- server/models/video/video.ts | 22 +++++++++++----------- 6 files changed, 30 insertions(+), 24 deletions(-) (limited to 'server/models/video') diff --git a/server/models/video/video-abuse-interface.ts b/server/models/video/video-abuse-interface.ts index 4b7f2a2ec..f3e32f79c 100644 --- a/server/models/video/video-abuse-interface.ts +++ b/server/models/video/video-abuse-interface.ts @@ -1,5 +1,7 @@ import * as Sequelize from 'sequelize' +import { PodInstance } from '../pod' + // Don't use barrel, import just what we need import { VideoAbuse as FormatedVideoAbuse } from '../../../shared/models/video-abuse.model' @@ -17,12 +19,15 @@ export interface VideoAbuseClass { export interface VideoAbuseAttributes { reporterUsername: string reason: string + videoId: string } export interface VideoAbuseInstance extends VideoAbuseClass, VideoAbuseAttributes, Sequelize.Instance { id: number createdAt: Date updatedAt: Date + + Pod: PodInstance } export interface VideoAbuseModel extends VideoAbuseClass, Sequelize.Model {} diff --git a/server/models/video/video-abuse.ts b/server/models/video/video-abuse.ts index e0e0bcfe6..f5b4debe6 100644 --- a/server/models/video/video-abuse.ts +++ b/server/models/video/video-abuse.ts @@ -66,7 +66,7 @@ export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.Da // ------------------------------ METHODS ------------------------------ -function toFormatedJSON () { +function toFormatedJSON (this: VideoAbuseInstance) { let reporterPodHost if (this.Pod) { diff --git a/server/models/video/video-blacklist-interface.ts b/server/models/video/video-blacklist-interface.ts index 37f579422..c34e7fb09 100644 --- a/server/models/video/video-blacklist-interface.ts +++ b/server/models/video/video-blacklist-interface.ts @@ -4,7 +4,7 @@ import * as Sequelize from 'sequelize' import { BlacklistedVideo as FormatedBlacklistedVideo } from '../../../shared/models/video-blacklist.model' export namespace BlacklistedVideoMethods { - export type ToFormatedJSON = () => FormatedBlacklistedVideo + export type ToFormatedJSON = (this: BlacklistedVideoInstance) => FormatedBlacklistedVideo export type CountTotalCallback = (err: Error, total: number) => void export type CountTotal = (callback: CountTotalCallback) => void @@ -32,6 +32,7 @@ export interface BlacklistedVideoClass { } export interface BlacklistedVideoAttributes { + videoId: string } export interface BlacklistedVideoInstance extends BlacklistedVideoClass, BlacklistedVideoAttributes, Sequelize.Instance { diff --git a/server/models/video/video-blacklist.ts b/server/models/video/video-blacklist.ts index f4479986c..3576c96f6 100644 --- a/server/models/video/video-blacklist.ts +++ b/server/models/video/video-blacklist.ts @@ -49,7 +49,7 @@ export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.Da // ------------------------------ METHODS ------------------------------ -toFormatedJSON = function () { +toFormatedJSON = function (this: BlacklistedVideoInstance) { return { id: this.id, videoId: this.videoId, diff --git a/server/models/video/video-interface.ts b/server/models/video/video-interface.ts index 71b9b0a69..5fefc2bb1 100644 --- a/server/models/video/video-interface.ts +++ b/server/models/video/video-interface.ts @@ -48,21 +48,21 @@ export type FormatedUpdateRemoteVideo = { } export namespace VideoMethods { - export type GenerateMagnetUri = () => string - export type GetVideoFilename = () => string - export type GetThumbnailName = () => string - export type GetPreviewName = () => string - export type GetTorrentName = () => string - export type IsOwned = () => boolean - export type ToFormatedJSON = () => FormatedVideo + export type GenerateMagnetUri = (this: VideoInstance) => string + export type GetVideoFilename = (this: VideoInstance) => string + export type GetThumbnailName = (this: VideoInstance) => string + export type GetPreviewName = (this: VideoInstance) => string + export type GetTorrentName = (this: VideoInstance) => string + export type IsOwned = (this: VideoInstance) => boolean + export type ToFormatedJSON = (this: VideoInstance) => FormatedVideo export type ToAddRemoteJSONCallback = (err: Error, videoFormated?: FormatedAddRemoteVideo) => void - export type ToAddRemoteJSON = (callback: ToAddRemoteJSONCallback) => void + export type ToAddRemoteJSON = (this: VideoInstance, callback: ToAddRemoteJSONCallback) => void - export type ToUpdateRemoteJSON = () => FormatedUpdateRemoteVideo + export type ToUpdateRemoteJSON = (this: VideoInstance) => FormatedUpdateRemoteVideo export type TranscodeVideofileCallback = (err: Error) => void - export type TranscodeVideofile = (callback: TranscodeVideofileCallback) => void + export type TranscodeVideofile = (this: VideoInstance, callback: TranscodeVideofileCallback) => void export type GenerateThumbnailFromDataCallback = (err: Error, thumbnailName?: string) => void export type GenerateThumbnailFromData = (video: VideoInstance, thumbnailData: string, callback: GenerateThumbnailFromDataCallback) => void diff --git a/server/models/video/video.ts b/server/models/video/video.ts index 866b380cc..e66ebee2d 100644 --- a/server/models/video/video.ts +++ b/server/models/video/video.ts @@ -247,7 +247,8 @@ export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.Da loadByHostAndRemoteId, loadAndPopulateAuthor, loadAndPopulateAuthorAndPodAndTags, - searchAndPopulateAuthorAndPodAndTags + searchAndPopulateAuthorAndPodAndTags, + removeFromBlacklist ] const instanceMethods = [ generateMagnetUri, @@ -260,7 +261,6 @@ export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.Da toAddRemoteJSON, toUpdateRemoteJSON, transcodeVideofile, - removeFromBlacklist ] addMethodsToModel(Video, classMethods, instanceMethods) @@ -389,7 +389,7 @@ function associate (models) { }) } -generateMagnetUri = function () { +generateMagnetUri = function (this: VideoInstance) { let baseUrlHttp let baseUrlWs @@ -416,18 +416,18 @@ generateMagnetUri = function () { return magnetUtil.encode(magnetHash) } -getVideoFilename = function () { +getVideoFilename = function (this: VideoInstance) { if (this.isOwned()) return this.id + this.extname return this.remoteId + this.extname } -getThumbnailName = function () { +getThumbnailName = function (this: VideoInstance) { // We always have a copy of the thumbnail return this.id + '.jpg' } -getPreviewName = function () { +getPreviewName = function (this: VideoInstance) { const extension = '.jpg' if (this.isOwned()) return this.id + extension @@ -435,7 +435,7 @@ getPreviewName = function () { return this.remoteId + extension } -getTorrentName = function () { +getTorrentName = function (this: VideoInstance) { const extension = '.torrent' if (this.isOwned()) return this.id + extension @@ -443,7 +443,7 @@ getTorrentName = function () { return this.remoteId + extension } -isOwned = function () { +isOwned = function (this: VideoInstance) { return this.remoteId === null } @@ -497,7 +497,7 @@ toFormatedJSON = function (this: VideoInstance) { return json } -toAddRemoteJSON = function (callback: VideoMethods.ToAddRemoteJSONCallback) { +toAddRemoteJSON = function (this: VideoInstance, callback: VideoMethods.ToAddRemoteJSONCallback) { // Get thumbnail data to send to the other pod const thumbnailPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, this.getThumbnailName()) fs.readFile(thumbnailPath, (err, thumbnailData) => { @@ -531,7 +531,7 @@ toAddRemoteJSON = function (callback: VideoMethods.ToAddRemoteJSONCallback) { }) } -toUpdateRemoteJSON = function () { +toUpdateRemoteJSON = function (this: VideoInstance) { const json = { name: this.name, category: this.category, @@ -555,7 +555,7 @@ toUpdateRemoteJSON = function () { return json } -transcodeVideofile = function (finalCallback: VideoMethods.TranscodeVideofileCallback) { +transcodeVideofile = function (this: VideoInstance, finalCallback: VideoMethods.TranscodeVideofileCallback) { const video = this const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR -- cgit v1.2.3