]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/video/video-share.ts
Fix job panel sorting in administration
[github/Chocobozzz/PeerTube.git] / server / models / video / video-share.ts
CommitLineData
d8465018 1import * as Sequelize from 'sequelize'
2422c46b 2import * as Bluebird from 'bluebird'
4ba3b8ea
C
3import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Is, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript'
4import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
5import { CONSTRAINTS_FIELDS } from '../../initializers'
265ba139 6import { AccountModel } from '../account/account'
50d6de9c 7import { ActorModel } from '../activitypub/actor'
4ba3b8ea 8import { throwIfNotValid } from '../utils'
3fd3ab2d 9import { VideoModel } from './video'
265ba139 10import { VideoChannelModel } from './video-channel'
d8465018 11
d48ff09d
C
12enum ScopeNames {
13 FULL = 'FULL',
50d6de9c 14 WITH_ACTOR = 'WITH_ACTOR'
d48ff09d
C
15}
16
17@Scopes({
18 [ScopeNames.FULL]: {
19 include: [
20 {
50d6de9c 21 model: () => ActorModel,
d48ff09d
C
22 required: true
23 },
24 {
25 model: () => VideoModel,
26 required: true
27 }
28 ]
29 },
50d6de9c 30 [ScopeNames.WITH_ACTOR]: {
d48ff09d
C
31 include: [
32 {
50d6de9c 33 model: () => ActorModel,
d48ff09d
C
34 required: true
35 }
36 ]
37 }
38})
3fd3ab2d
C
39@Table({
40 tableName: 'videoShare',
41 indexes: [
d8465018 42 {
50d6de9c 43 fields: [ 'actorId' ]
3fd3ab2d
C
44 },
45 {
46 fields: [ 'videoId' ]
4ba3b8ea
C
47 },
48 {
49 fields: [ 'url' ],
50 unique: true
d8465018 51 }
d8465018 52 ]
3fd3ab2d
C
53})
54export class VideoShareModel extends Model<VideoShareModel> {
4ba3b8ea
C
55
56 @AllowNull(false)
57 @Is('VideoShareUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
58 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_SHARE.URL.max))
59 url: string
60
3fd3ab2d
C
61 @CreatedAt
62 createdAt: Date
d8465018 63
3fd3ab2d
C
64 @UpdatedAt
65 updatedAt: Date
d8465018 66
50d6de9c 67 @ForeignKey(() => ActorModel)
3fd3ab2d 68 @Column
50d6de9c 69 actorId: number
d8465018 70
50d6de9c 71 @BelongsTo(() => ActorModel, {
d8465018 72 foreignKey: {
d8465018
C
73 allowNull: false
74 },
75 onDelete: 'cascade'
76 })
50d6de9c 77 Actor: ActorModel
d8465018 78
3fd3ab2d
C
79 @ForeignKey(() => VideoModel)
80 @Column
81 videoId: number
82
83 @BelongsTo(() => VideoModel, {
d8465018 84 foreignKey: {
3fd3ab2d 85 allowNull: false
d8465018
C
86 },
87 onDelete: 'cascade'
88 })
3fd3ab2d 89 Video: VideoModel
4e50b6a1 90
50d6de9c
C
91 static load (actorId: number, videoId: number, t: Sequelize.Transaction) {
92 return VideoShareModel.scope(ScopeNames.WITH_ACTOR).findOne({
3fd3ab2d 93 where: {
50d6de9c 94 actorId,
3fd3ab2d
C
95 videoId
96 },
3fd3ab2d
C
97 transaction: t
98 })
d7d5611c
C
99 }
100
50d6de9c 101 static loadActorsByShare (videoId: number, t: Sequelize.Transaction) {
3fd3ab2d
C
102 const query = {
103 where: {
104 videoId
105 },
106 include: [
107 {
50d6de9c 108 model: ActorModel,
3fd3ab2d
C
109 required: true
110 }
111 ],
112 transaction: t
113 }
114
d48ff09d 115 return VideoShareModel.scope(ScopeNames.FULL).findAll(query)
50d6de9c 116 .then(res => res.map(r => r.Actor))
3fd3ab2d 117 }
265ba139 118
2422c46b 119 static loadActorsByVideoOwner (actorOwnerId: number, t: Sequelize.Transaction): Bluebird<ActorModel[]> {
265ba139
C
120 const query = {
121 attributes: [],
122 include: [
123 {
124 model: ActorModel,
125 required: true
126 },
127 {
128 attributes: [],
129 model: VideoModel,
130 required: true,
131 include: [
132 {
133 attributes: [],
134 model: VideoChannelModel.unscoped(),
135 required: true,
136 include: [
137 {
138 attributes: [],
139 model: AccountModel.unscoped(),
140 required: true,
141 where: {
142 actorId: actorOwnerId
143 }
144 }
145 ]
146 }
147 ]
148 }
149 ],
150 transaction: t
151 }
152
153 return VideoShareModel.scope(ScopeNames.FULL).findAll(query)
154 .then(res => res.map(r => r.Actor))
155 }
2422c46b
C
156
157 static loadActorsByVideoChannel (videoChannelId: number, t: Sequelize.Transaction): Bluebird<ActorModel[]> {
158 const query = {
159 attributes: [],
160 include: [
161 {
162 model: ActorModel,
163 required: true
164 },
165 {
166 attributes: [],
167 model: VideoModel,
168 required: true,
169 where: {
170 channelId: videoChannelId
171 }
172 }
173 ],
174 transaction: t
175 }
176
177 return VideoShareModel.scope(ScopeNames.FULL)
178 .findAll(query)
179 .then(res => res.map(r => r.Actor))
180 }
d7d5611c 181}