]>
Commit | Line | Data |
---|---|---|
74d249bc | 1 | import { literal, Op, QueryTypes, Transaction } from 'sequelize' |
4ba3b8ea | 2 | import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Is, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript' |
6b5f72be | 3 | import { AttributesOnly } from '@shared/typescript-utils' |
4ba3b8ea | 4 | import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc' |
74dc3bca | 5 | import { CONSTRAINTS_FIELDS } from '../../initializers/constants' |
b49f22d8 C |
6 | import { MActorDefault } from '../../types/models' |
7 | import { MVideoShareActor, MVideoShareFull } from '../../types/models/video' | |
7d9ba5c0 | 8 | import { ActorModel } from '../actor/actor' |
6b9c966f | 9 | import { buildLocalActorIdsIn, throwIfNotValid } from '../utils' |
3fd3ab2d | 10 | import { VideoModel } from './video' |
d8465018 | 11 | |
d48ff09d C |
12 | enum 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 | 54 | export 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 | ||
186 | return VideoShareModel.findAndCountAll(query) | |
187 | } | |
2ba92871 | 188 | |
74d249bc C |
189 | static listRemoteShareUrlsOfLocalVideos () { |
190 | const query = `SELECT "videoShare".url FROM "videoShare" ` + | |
191 | `INNER JOIN actor ON actor.id = "videoShare"."actorId" AND actor."serverId" IS NOT NULL ` + | |
192 | `INNER JOIN video ON video.id = "videoShare"."videoId" AND video.remote IS FALSE` | |
193 | ||
194 | return VideoShareModel.sequelize.query<{ url: string }>(query, { | |
195 | type: QueryTypes.SELECT, | |
196 | raw: true | |
197 | }).then(rows => rows.map(r => r.url)) | |
198 | } | |
199 | ||
2ba92871 C |
200 | static cleanOldSharesOf (videoId: number, beforeUpdatedAt: Date) { |
201 | const query = { | |
202 | where: { | |
203 | updatedAt: { | |
970ceac0 | 204 | [Op.lt]: beforeUpdatedAt |
2ba92871 | 205 | }, |
6b9c966f C |
206 | videoId, |
207 | actorId: { | |
208 | [Op.notIn]: buildLocalActorIdsIn() | |
970ceac0 | 209 | } |
6b9c966f | 210 | } |
2ba92871 C |
211 | } |
212 | ||
213 | return VideoShareModel.destroy(query) | |
214 | } | |
d7d5611c | 215 | } |