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