]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/video/video-share.ts
Begin unit tests
[github/Chocobozzz/PeerTube.git] / server / models / video / video-share.ts
CommitLineData
d8465018 1import * as Sequelize from 'sequelize'
d48ff09d 2import { BelongsTo, Column, CreatedAt, ForeignKey, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript'
50d6de9c 3import { ActorModel } from '../activitypub/actor'
3fd3ab2d 4import { VideoModel } from './video'
d8465018 5
d48ff09d
C
6enum ScopeNames {
7 FULL = 'FULL',
50d6de9c 8 WITH_ACTOR = 'WITH_ACTOR'
d48ff09d
C
9}
10
11@Scopes({
12 [ScopeNames.FULL]: {
13 include: [
14 {
50d6de9c 15 model: () => ActorModel,
d48ff09d
C
16 required: true
17 },
18 {
19 model: () => VideoModel,
20 required: true
21 }
22 ]
23 },
50d6de9c 24 [ScopeNames.WITH_ACTOR]: {
d48ff09d
C
25 include: [
26 {
50d6de9c 27 model: () => ActorModel,
d48ff09d
C
28 required: true
29 }
30 ]
31 }
32})
3fd3ab2d
C
33@Table({
34 tableName: 'videoShare',
35 indexes: [
d8465018 36 {
50d6de9c 37 fields: [ 'actorId' ]
3fd3ab2d
C
38 },
39 {
40 fields: [ 'videoId' ]
d8465018 41 }
d8465018 42 ]
3fd3ab2d
C
43})
44export class VideoShareModel extends Model<VideoShareModel> {
45 @CreatedAt
46 createdAt: Date
d8465018 47
3fd3ab2d
C
48 @UpdatedAt
49 updatedAt: Date
d8465018 50
50d6de9c 51 @ForeignKey(() => ActorModel)
3fd3ab2d 52 @Column
50d6de9c 53 actorId: number
d8465018 54
50d6de9c 55 @BelongsTo(() => ActorModel, {
d8465018 56 foreignKey: {
d8465018
C
57 allowNull: false
58 },
59 onDelete: 'cascade'
60 })
50d6de9c 61 Actor: ActorModel
d8465018 62
3fd3ab2d
C
63 @ForeignKey(() => VideoModel)
64 @Column
65 videoId: number
66
67 @BelongsTo(() => VideoModel, {
d8465018 68 foreignKey: {
3fd3ab2d 69 allowNull: false
d8465018
C
70 },
71 onDelete: 'cascade'
72 })
3fd3ab2d 73 Video: VideoModel
4e50b6a1 74
50d6de9c
C
75 static load (actorId: number, videoId: number, t: Sequelize.Transaction) {
76 return VideoShareModel.scope(ScopeNames.WITH_ACTOR).findOne({
3fd3ab2d 77 where: {
50d6de9c 78 actorId,
3fd3ab2d
C
79 videoId
80 },
3fd3ab2d
C
81 transaction: t
82 })
d7d5611c
C
83 }
84
50d6de9c 85 static loadActorsByShare (videoId: number, t: Sequelize.Transaction) {
3fd3ab2d
C
86 const query = {
87 where: {
88 videoId
89 },
90 include: [
91 {
50d6de9c 92 model: ActorModel,
3fd3ab2d
C
93 required: true
94 }
95 ],
96 transaction: t
97 }
98
d48ff09d 99 return VideoShareModel.scope(ScopeNames.FULL).findAll(query)
50d6de9c 100 .then(res => res.map(r => r.Actor))
3fd3ab2d 101 }
d7d5611c 102}