Basic api documentation #7 (#220)
[oweals/peertube.git] / server / models / video / video-share.ts
1 import * as Sequelize from 'sequelize'
2 import { BelongsTo, Column, CreatedAt, ForeignKey, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript'
3 import { AccountModel } from '../account/account'
4 import { ActorModel } from '../activitypub/actor'
5 import { VideoModel } from './video'
6 import { VideoChannelModel } from './video-channel'
7
8 enum ScopeNames {
9   FULL = 'FULL',
10   WITH_ACTOR = 'WITH_ACTOR'
11 }
12
13 @Scopes({
14   [ScopeNames.FULL]: {
15     include: [
16       {
17         model: () => ActorModel,
18         required: true
19       },
20       {
21         model: () => VideoModel,
22         required: true
23       }
24     ]
25   },
26   [ScopeNames.WITH_ACTOR]: {
27     include: [
28       {
29         model: () => ActorModel,
30         required: true
31       }
32     ]
33   }
34 })
35 @Table({
36   tableName: 'videoShare',
37   indexes: [
38     {
39       fields: [ 'actorId' ]
40     },
41     {
42       fields: [ 'videoId' ]
43     }
44   ]
45 })
46 export class VideoShareModel extends Model<VideoShareModel> {
47   @CreatedAt
48   createdAt: Date
49
50   @UpdatedAt
51   updatedAt: Date
52
53   @ForeignKey(() => ActorModel)
54   @Column
55   actorId: number
56
57   @BelongsTo(() => ActorModel, {
58     foreignKey: {
59       allowNull: false
60     },
61     onDelete: 'cascade'
62   })
63   Actor: ActorModel
64
65   @ForeignKey(() => VideoModel)
66   @Column
67   videoId: number
68
69   @BelongsTo(() => VideoModel, {
70     foreignKey: {
71       allowNull: false
72     },
73     onDelete: 'cascade'
74   })
75   Video: VideoModel
76
77   static load (actorId: number, videoId: number, t: Sequelize.Transaction) {
78     return VideoShareModel.scope(ScopeNames.WITH_ACTOR).findOne({
79       where: {
80         actorId,
81         videoId
82       },
83       transaction: t
84     })
85   }
86
87   static loadActorsByShare (videoId: number, t: Sequelize.Transaction) {
88     const query = {
89       where: {
90         videoId
91       },
92       include: [
93         {
94           model: ActorModel,
95           required: true
96         }
97       ],
98       transaction: t
99     }
100
101     return VideoShareModel.scope(ScopeNames.FULL).findAll(query)
102       .then(res => res.map(r => r.Actor))
103   }
104
105   static loadActorsByVideoOwner (actorOwnerId: number, t: Sequelize.Transaction) {
106     const query = {
107       attributes: [],
108       include: [
109         {
110           model: ActorModel,
111           required: true
112         },
113         {
114           attributes: [],
115           model: VideoModel,
116           required: true,
117           include: [
118             {
119               attributes: [],
120               model: VideoChannelModel.unscoped(),
121               required: true,
122               include: [
123                 {
124                   attributes: [],
125                   model: AccountModel.unscoped(),
126                   required: true,
127                   where: {
128                     actorId: actorOwnerId
129                   }
130                 }
131               ]
132             }
133           ]
134         }
135       ],
136       transaction: t
137     }
138
139     return VideoShareModel.scope(ScopeNames.FULL).findAll(query)
140       .then(res => res.map(r => r.Actor))
141   }
142 }