aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/video/video-password.ts
blob: 648366c3b78d7477c10cf30e1ba2e7fd89d944d1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import { AllowNull, BelongsTo, Column, CreatedAt, DefaultScope, ForeignKey, Is, Model, Table, UpdatedAt } from 'sequelize-typescript'
import { VideoModel } from './video'
import { AttributesOnly } from '@shared/typescript-utils'
import { ResultList, VideoPassword } from '@shared/models'
import { getSort, throwIfNotValid } from '../shared'
import { FindOptions, Transaction } from 'sequelize'
import { MVideoPassword } from '@server/types/models'
import { isPasswordValid } from '@server/helpers/custom-validators/videos'
import { pick } from '@shared/core-utils'

@DefaultScope(() => ({
  include: [
    {
      model: VideoModel.unscoped(),
      required: true
    }
  ]
}))
@Table({
  tableName: 'videoPassword',
  indexes: [
    {
      fields: [ 'videoId', 'password' ],
      unique: true
    }
  ]
})
export class VideoPasswordModel extends Model<Partial<AttributesOnly<VideoPasswordModel>>> {

  @AllowNull(false)
  @Is('VideoPassword', value => throwIfNotValid(value, isPasswordValid, 'videoPassword'))
  @Column
  password: string

  @CreatedAt
  createdAt: Date

  @UpdatedAt
  updatedAt: Date

  @ForeignKey(() => VideoModel)
  @Column
  videoId: number

  @BelongsTo(() => VideoModel, {
    foreignKey: {
      allowNull: false
    },
    onDelete: 'cascade'
  })
  Video: VideoModel

  static async countByVideoId (videoId: number, t?: Transaction) {
    const query: FindOptions = {
      where: {
        videoId
      },
      transaction: t
    }

    return VideoPasswordModel.count(query)
  }

  static async loadByIdAndVideo (options: { id: number, videoId: number, t?: Transaction }): Promise<MVideoPassword> {
    const { id, videoId, t } = options
    const query: FindOptions = {
      where: {
        id,
        videoId
      },
      transaction: t
    }

    return VideoPasswordModel.findOne(query)
  }

  static async listPasswords (options: {
    start: number
    count: number
    sort: string
    videoId: number
  }): Promise<ResultList<MVideoPassword>> {
    const { start, count, sort, videoId } = options

    const { count: total, rows: data } = await VideoPasswordModel.findAndCountAll({
      where: { videoId },
      order: getSort(sort),
      offset: start,
      limit: count
    })

    return { total, data }
  }

  static async addPasswords (passwords: string[], videoId: number, transaction?: Transaction): Promise<void> {
    for (const password of passwords) {
      await VideoPasswordModel.create({
        password,
        videoId
      }, { transaction })
    }
  }

  static async deleteAllPasswords (videoId: number, transaction?: Transaction) {
    await VideoPasswordModel.destroy({
      where: { videoId },
      transaction
    })
  }

  static async deletePassword (passwordId: number, transaction?: Transaction) {
    await VideoPasswordModel.destroy({
      where: { id: passwordId },
      transaction
    })
  }

  static async isACorrectPassword (options: {
    videoId: number
    password: string
  }) {
    const query = {
      where: pick(options, [ 'videoId', 'password' ])
    }
    return VideoPasswordModel.findOne(query)
  }

  toFormattedJSON (): VideoPassword {
    return {
      id: this.id,
      password: this.password,
      videoId: this.videoId,
      createdAt: this.createdAt,
      updatedAt: this.updatedAt
    }
  }
}