]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/video/video-share.ts
Refactor a little bit raw sql builders
[github/Chocobozzz/PeerTube.git] / server / models / video / video-share.ts
CommitLineData
74d249bc 1import { literal, Op, QueryTypes, Transaction } from 'sequelize'
4ba3b8ea 2import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Is, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript'
6b5f72be 3import { AttributesOnly } from '@shared/typescript-utils'
4ba3b8ea 4import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
74dc3bca 5import { CONSTRAINTS_FIELDS } from '../../initializers/constants'
b49f22d8
C
6import { MActorDefault } from '../../types/models'
7import { MVideoShareActor, MVideoShareFull } from '../../types/models/video'
7d9ba5c0 8import { ActorModel } from '../actor/actor'
6b9c966f 9import { buildLocalActorIdsIn, throwIfNotValid } from '../utils'
3fd3ab2d 10import { VideoModel } from './video'
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 53})
16c016e8 54export class VideoShareModel extends Model<Partial<AttributesOnly<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
b49f22d8 91 static load (actorId: number | string, videoId: number | string, t?: Transaction): Promise<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
b49f22d8 101 static loadByUrl (url: string, t: Transaction): Promise<MVideoShareFull> {
9588d4f4 102 return VideoShareModel.scope(ScopeNames.FULL).findOne({
0f320037
C
103 where: {
104 url
105 },
106 transaction: t
107 })
108 }
109
b49f22d8 110 static loadActorsByShare (videoId: number, t: Transaction): Promise<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
b49f22d8 128 static loadActorsWhoSharedVideosOf (actorOwnerId: number, t: Transaction): Promise<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
b49f22d8 153 static loadActorsByVideoChannel (videoChannelId: number, t: Transaction): Promise<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
d0800f76 186 return Promise.all([
187 VideoShareModel.count(query),
188 VideoShareModel.findAll(query)
189 ]).then(([ total, data ]) => ({ total, data }))
8fffe21a 190 }
2ba92871 191
74d249bc
C
192 static listRemoteShareUrlsOfLocalVideos () {
193 const query = `SELECT "videoShare".url FROM "videoShare" ` +
194 `INNER JOIN actor ON actor.id = "videoShare"."actorId" AND actor."serverId" IS NOT NULL ` +
195 `INNER JOIN video ON video.id = "videoShare"."videoId" AND video.remote IS FALSE`
196
197 return VideoShareModel.sequelize.query<{ url: string }>(query, {
198 type: QueryTypes.SELECT,
199 raw: true
200 }).then(rows => rows.map(r => r.url))
201 }
202
2ba92871
C
203 static cleanOldSharesOf (videoId: number, beforeUpdatedAt: Date) {
204 const query = {
205 where: {
206 updatedAt: {
970ceac0 207 [Op.lt]: beforeUpdatedAt
2ba92871 208 },
6b9c966f
C
209 videoId,
210 actorId: {
211 [Op.notIn]: buildLocalActorIdsIn()
970ceac0 212 }
6b9c966f 213 }
2ba92871
C
214 }
215
216 return VideoShareModel.destroy(query)
217 }
d7d5611c 218}