Add this context to instance model functions

This commit is contained in:
Chocobozzz 2017-06-16 09:54:59 +02:00
parent 74889a71fe
commit 70c065d64c
11 changed files with 39 additions and 33 deletions

View File

@ -4,7 +4,7 @@ import * as Sequelize from 'sequelize'
import { Pod as FormatedPod } from '../../../shared/models/pod.model' import { Pod as FormatedPod } from '../../../shared/models/pod.model'
export namespace PodMethods { export namespace PodMethods {
export type ToFormatedJSON = () => FormatedPod export type ToFormatedJSON = (this: PodInstance) => FormatedPod
export type CountAllCallback = (err: Error, total: number) => void export type CountAllCallback = (err: Error, total: number) => void
export type CountAll = (callback) => void export type CountAll = (callback) => void

View File

@ -96,12 +96,12 @@ export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.Da
// ------------------------------ METHODS ------------------------------ // ------------------------------ METHODS ------------------------------
toFormatedJSON = function () { toFormatedJSON = function (this: PodInstance) {
const json = { const json = {
id: this.id, id: this.id,
host: this.host, host: this.host,
email: this.email, email: this.email,
score: this.score, score: this.score as number,
createdAt: this.createdAt createdAt: this.createdAt
} }

View File

@ -6,10 +6,10 @@ import { User as FormatedUser } from '../../../shared/models/user.model'
export namespace UserMethods { export namespace UserMethods {
export type IsPasswordMatchCallback = (err: Error, same: boolean) => void export type IsPasswordMatchCallback = (err: Error, same: boolean) => void
export type IsPasswordMatch = (password: string, callback: IsPasswordMatchCallback) => void export type IsPasswordMatch = (this: UserInstance, password: string, callback: IsPasswordMatchCallback) => void
export type ToFormatedJSON = () => FormatedUser export type ToFormatedJSON = (this: UserInstance) => FormatedUser
export type IsAdmin = () => boolean export type IsAdmin = (this: UserInstance) => boolean
export type CountTotalCallback = (err: Error, total: number) => void export type CountTotalCallback = (err: Error, total: number) => void
export type CountTotal = (callback: CountTotalCallback) => void export type CountTotal = (callback: CountTotalCallback) => void

View File

@ -131,7 +131,7 @@ function beforeCreateOrUpdate (user: UserInstance) {
// ------------------------------ METHODS ------------------------------ // ------------------------------ METHODS ------------------------------
isPasswordMatch = function (password: string, callback: UserMethods.IsPasswordMatchCallback) { isPasswordMatch = function (this: UserInstance, password: string, callback: UserMethods.IsPasswordMatchCallback) {
return comparePassword(password, this.password, callback) return comparePassword(password, this.password, callback)
} }
@ -146,7 +146,7 @@ toFormatedJSON = function (this: UserInstance) {
} }
} }
isAdmin = function () { isAdmin = function (this: UserInstance) {
return this.role === USER_ROLES.ADMIN return this.role === USER_ROLES.ADMIN
} }

View File

@ -1,5 +1,7 @@
import * as Sequelize from 'sequelize' import * as Sequelize from 'sequelize'
import { PodInstance } from '../pod'
// Don't use barrel, import just what we need // Don't use barrel, import just what we need
import { VideoAbuse as FormatedVideoAbuse } from '../../../shared/models/video-abuse.model' import { VideoAbuse as FormatedVideoAbuse } from '../../../shared/models/video-abuse.model'
@ -17,12 +19,15 @@ export interface VideoAbuseClass {
export interface VideoAbuseAttributes { export interface VideoAbuseAttributes {
reporterUsername: string reporterUsername: string
reason: string reason: string
videoId: string
} }
export interface VideoAbuseInstance extends VideoAbuseClass, VideoAbuseAttributes, Sequelize.Instance<VideoAbuseAttributes> { export interface VideoAbuseInstance extends VideoAbuseClass, VideoAbuseAttributes, Sequelize.Instance<VideoAbuseAttributes> {
id: number id: number
createdAt: Date createdAt: Date
updatedAt: Date updatedAt: Date
Pod: PodInstance
} }
export interface VideoAbuseModel extends VideoAbuseClass, Sequelize.Model<VideoAbuseInstance, VideoAbuseAttributes> {} export interface VideoAbuseModel extends VideoAbuseClass, Sequelize.Model<VideoAbuseInstance, VideoAbuseAttributes> {}

View File

@ -66,7 +66,7 @@ export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.Da
// ------------------------------ METHODS ------------------------------ // ------------------------------ METHODS ------------------------------
function toFormatedJSON () { function toFormatedJSON (this: VideoAbuseInstance) {
let reporterPodHost let reporterPodHost
if (this.Pod) { if (this.Pod) {

View File

@ -4,7 +4,7 @@ import * as Sequelize from 'sequelize'
import { BlacklistedVideo as FormatedBlacklistedVideo } from '../../../shared/models/video-blacklist.model' import { BlacklistedVideo as FormatedBlacklistedVideo } from '../../../shared/models/video-blacklist.model'
export namespace BlacklistedVideoMethods { export namespace BlacklistedVideoMethods {
export type ToFormatedJSON = () => FormatedBlacklistedVideo export type ToFormatedJSON = (this: BlacklistedVideoInstance) => FormatedBlacklistedVideo
export type CountTotalCallback = (err: Error, total: number) => void export type CountTotalCallback = (err: Error, total: number) => void
export type CountTotal = (callback: CountTotalCallback) => void export type CountTotal = (callback: CountTotalCallback) => void
@ -32,6 +32,7 @@ export interface BlacklistedVideoClass {
} }
export interface BlacklistedVideoAttributes { export interface BlacklistedVideoAttributes {
videoId: string
} }
export interface BlacklistedVideoInstance extends BlacklistedVideoClass, BlacklistedVideoAttributes, Sequelize.Instance<BlacklistedVideoAttributes> { export interface BlacklistedVideoInstance extends BlacklistedVideoClass, BlacklistedVideoAttributes, Sequelize.Instance<BlacklistedVideoAttributes> {

View File

@ -49,7 +49,7 @@ export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.Da
// ------------------------------ METHODS ------------------------------ // ------------------------------ METHODS ------------------------------
toFormatedJSON = function () { toFormatedJSON = function (this: BlacklistedVideoInstance) {
return { return {
id: this.id, id: this.id,
videoId: this.videoId, videoId: this.videoId,

View File

@ -48,21 +48,21 @@ export type FormatedUpdateRemoteVideo = {
} }
export namespace VideoMethods { export namespace VideoMethods {
export type GenerateMagnetUri = () => string export type GenerateMagnetUri = (this: VideoInstance) => string
export type GetVideoFilename = () => string export type GetVideoFilename = (this: VideoInstance) => string
export type GetThumbnailName = () => string export type GetThumbnailName = (this: VideoInstance) => string
export type GetPreviewName = () => string export type GetPreviewName = (this: VideoInstance) => string
export type GetTorrentName = () => string export type GetTorrentName = (this: VideoInstance) => string
export type IsOwned = () => boolean export type IsOwned = (this: VideoInstance) => boolean
export type ToFormatedJSON = () => FormatedVideo export type ToFormatedJSON = (this: VideoInstance) => FormatedVideo
export type ToAddRemoteJSONCallback = (err: Error, videoFormated?: FormatedAddRemoteVideo) => void 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 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 GenerateThumbnailFromDataCallback = (err: Error, thumbnailName?: string) => void
export type GenerateThumbnailFromData = (video: VideoInstance, thumbnailData: string, callback: GenerateThumbnailFromDataCallback) => void export type GenerateThumbnailFromData = (video: VideoInstance, thumbnailData: string, callback: GenerateThumbnailFromDataCallback) => void

View File

@ -247,7 +247,8 @@ export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.Da
loadByHostAndRemoteId, loadByHostAndRemoteId,
loadAndPopulateAuthor, loadAndPopulateAuthor,
loadAndPopulateAuthorAndPodAndTags, loadAndPopulateAuthorAndPodAndTags,
searchAndPopulateAuthorAndPodAndTags searchAndPopulateAuthorAndPodAndTags,
removeFromBlacklist
] ]
const instanceMethods = [ const instanceMethods = [
generateMagnetUri, generateMagnetUri,
@ -260,7 +261,6 @@ export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.Da
toAddRemoteJSON, toAddRemoteJSON,
toUpdateRemoteJSON, toUpdateRemoteJSON,
transcodeVideofile, transcodeVideofile,
removeFromBlacklist
] ]
addMethodsToModel(Video, classMethods, instanceMethods) addMethodsToModel(Video, classMethods, instanceMethods)
@ -389,7 +389,7 @@ function associate (models) {
}) })
} }
generateMagnetUri = function () { generateMagnetUri = function (this: VideoInstance) {
let baseUrlHttp let baseUrlHttp
let baseUrlWs let baseUrlWs
@ -416,18 +416,18 @@ generateMagnetUri = function () {
return magnetUtil.encode(magnetHash) return magnetUtil.encode(magnetHash)
} }
getVideoFilename = function () { getVideoFilename = function (this: VideoInstance) {
if (this.isOwned()) return this.id + this.extname if (this.isOwned()) return this.id + this.extname
return this.remoteId + this.extname return this.remoteId + this.extname
} }
getThumbnailName = function () { getThumbnailName = function (this: VideoInstance) {
// We always have a copy of the thumbnail // We always have a copy of the thumbnail
return this.id + '.jpg' return this.id + '.jpg'
} }
getPreviewName = function () { getPreviewName = function (this: VideoInstance) {
const extension = '.jpg' const extension = '.jpg'
if (this.isOwned()) return this.id + extension if (this.isOwned()) return this.id + extension
@ -435,7 +435,7 @@ getPreviewName = function () {
return this.remoteId + extension return this.remoteId + extension
} }
getTorrentName = function () { getTorrentName = function (this: VideoInstance) {
const extension = '.torrent' const extension = '.torrent'
if (this.isOwned()) return this.id + extension if (this.isOwned()) return this.id + extension
@ -443,7 +443,7 @@ getTorrentName = function () {
return this.remoteId + extension return this.remoteId + extension
} }
isOwned = function () { isOwned = function (this: VideoInstance) {
return this.remoteId === null return this.remoteId === null
} }
@ -497,7 +497,7 @@ toFormatedJSON = function (this: VideoInstance) {
return json return json
} }
toAddRemoteJSON = function (callback: VideoMethods.ToAddRemoteJSONCallback) { toAddRemoteJSON = function (this: VideoInstance, callback: VideoMethods.ToAddRemoteJSONCallback) {
// Get thumbnail data to send to the other pod // Get thumbnail data to send to the other pod
const thumbnailPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, this.getThumbnailName()) const thumbnailPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, this.getThumbnailName())
fs.readFile(thumbnailPath, (err, thumbnailData) => { fs.readFile(thumbnailPath, (err, thumbnailData) => {
@ -531,7 +531,7 @@ toAddRemoteJSON = function (callback: VideoMethods.ToAddRemoteJSONCallback) {
}) })
} }
toUpdateRemoteJSON = function () { toUpdateRemoteJSON = function (this: VideoInstance) {
const json = { const json = {
name: this.name, name: this.name,
category: this.category, category: this.category,
@ -555,7 +555,7 @@ toUpdateRemoteJSON = function () {
return json return json
} }
transcodeVideofile = function (finalCallback: VideoMethods.TranscodeVideofileCallback) { transcodeVideofile = function (this: VideoInstance, finalCallback: VideoMethods.TranscodeVideofileCallback) {
const video = this const video = this
const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR const videosDirectory = CONFIG.STORAGE.VIDEOS_DIR

View File

@ -1,5 +1,5 @@
export interface BlacklistedVideo { export interface BlacklistedVideo {
id: number id: number
videoId: number videoId: string
createdAt: Date createdAt: Date
} }