]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/video/video-share.ts
disable x-powered-by even with csp disabled
[github/Chocobozzz/PeerTube.git] / server / models / video / video-share.ts
CommitLineData
2422c46b 1import * as Bluebird from 'bluebird'
4ba3b8ea
C
2import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Is, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript'
3import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
74dc3bca 4import { CONSTRAINTS_FIELDS } from '../../initializers/constants'
50d6de9c 5import { ActorModel } from '../activitypub/actor'
6b9c966f 6import { buildLocalActorIdsIn, throwIfNotValid } from '../utils'
3fd3ab2d 7import { VideoModel } from './video'
891a8196 8import { literal, Op, Transaction } from 'sequelize'
26d6bf65
C
9import { MVideoShareActor, MVideoShareFull } from '../../types/models/video'
10import { MActorDefault } from '../../types/models'
d8465018 11
d48ff09d
C
12enum ScopeNames {
13 FULL = 'FULL',
50d6de9c 14 WITH_ACTOR = 'WITH_ACTOR'
d48ff09d
C
15}
16
3acc5084 17@Scopes(() => ({
d48ff09d
C
18 [ScopeNames.FULL]: {
19 include: [
20 {
3acc5084 21 model: ActorModel,
d48ff09d
C
22 required: true
23 },
24 {
3acc5084 25 model: VideoModel,
d48ff09d
C
26 required: true
27 }
28 ]
29 },
50d6de9c 30 [ScopeNames.WITH_ACTOR]: {
d48ff09d
C
31 include: [
32 {
3acc5084 33 model: ActorModel,
d48ff09d
C
34 required: true
35 }
36 ]
37 }
3acc5084 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
d5d9b6d7 91 static load (actorId: number | string, videoId: number | string, t?: Transaction): Bluebird<MVideoShareActor> {
50d6de9c 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
453e83ea 101 static loadByUrl (url: string, t: Transaction): Bluebird<MVideoShareFull> {
9588d4f4 102 return VideoShareModel.scope(ScopeNames.FULL).findOne({
0f320037
C
103 where: {
104 url
105 },
106 transaction: t
107 })
108 }
109
453e83ea 110 static loadActorsByShare (videoId: number, t: Transaction): Bluebird<MActorDefault[]> {
3fd3ab2d
C
111 const query = {
112 where: {
113 videoId
114 },
115 include: [
116 {
50d6de9c 117 model: ActorModel,
3fd3ab2d
C
118 required: true
119 }
120 ],
121 transaction: t
122 }
123
d48ff09d 124 return VideoShareModel.scope(ScopeNames.FULL).findAll(query)
891a8196 125 .then((res: MVideoShareFull[]) => res.map(r => r.Actor))
3fd3ab2d 126 }
265ba139 127
453e83ea 128 static loadActorsWhoSharedVideosOf (actorOwnerId: number, t: Transaction): Bluebird<MActorDefault[]> {
891a8196
C
129 const safeOwnerId = parseInt(actorOwnerId + '', 10)
130
131 // /!\ On actor model
265ba139 132 const query = {
891a8196
C
133 where: {
134 [Op.and]: [
135 literal(
136 `EXISTS (` +
137 ` SELECT 1 FROM "videoShare" ` +
138 ` INNER JOIN "video" ON "videoShare"."videoId" = "video"."id" ` +
139 ` INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ` +
140 ` INNER JOIN "account" ON "account"."id" = "videoChannel"."accountId" ` +
141 ` WHERE "videoShare"."actorId" = "ActorModel"."id" AND "account"."actorId" = ${safeOwnerId} ` +
142 ` LIMIT 1` +
143 `)`
144 )
145 ]
146 },
265ba139
C
147 transaction: t
148 }
149
891a8196 150 return ActorModel.findAll(query)
265ba139 151 }
2422c46b 152
453e83ea 153 static loadActorsByVideoChannel (videoChannelId: number, t: Transaction): Bluebird<MActorDefault[]> {
891a8196
C
154 const safeChannelId = parseInt(videoChannelId + '', 10)
155
156 // /!\ On actor model
2422c46b 157 const query = {
891a8196
C
158 where: {
159 [Op.and]: [
160 literal(
161 `EXISTS (` +
162 ` SELECT 1 FROM "videoShare" ` +
163 ` INNER JOIN "video" ON "videoShare"."videoId" = "video"."id" ` +
164 ` WHERE "videoShare"."actorId" = "ActorModel"."id" AND "video"."channelId" = ${safeChannelId} ` +
165 ` LIMIT 1` +
166 `)`
167 )
168 ]
169 },
2422c46b
C
170 transaction: t
171 }
172
891a8196 173 return ActorModel.findAll(query)
2422c46b 174 }
8fffe21a 175
970ceac0 176 static listAndCountByVideoId (videoId: number, start: number, count: number, t?: Transaction) {
8fffe21a 177 const query = {
9a4a9b6c
C
178 offset: start,
179 limit: count,
8fffe21a
C
180 where: {
181 videoId
182 },
183 transaction: t
184 }
185
186 return VideoShareModel.findAndCountAll(query)
187 }
2ba92871
C
188
189 static cleanOldSharesOf (videoId: number, beforeUpdatedAt: Date) {
190 const query = {
191 where: {
192 updatedAt: {
970ceac0 193 [Op.lt]: beforeUpdatedAt
2ba92871 194 },
6b9c966f
C
195 videoId,
196 actorId: {
197 [Op.notIn]: buildLocalActorIdsIn()
970ceac0 198 }
6b9c966f 199 }
2ba92871
C
200 }
201
202 return VideoShareModel.destroy(query)
203 }
d7d5611c 204}