]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/video/video-share.ts
Cleanup invalid rates/comments/shares
[github/Chocobozzz/PeerTube.git] / server / models / video / video-share.ts
1 import * as Sequelize from 'sequelize'
2 import { Op } from 'sequelize'
3 import * as Bluebird from 'bluebird'
4 import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Is, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript'
5 import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
6 import { CONSTRAINTS_FIELDS } from '../../initializers'
7 import { AccountModel } from '../account/account'
8 import { ActorModel } from '../activitypub/actor'
9 import { throwIfNotValid } from '../utils'
10 import { VideoModel } from './video'
11 import { VideoChannelModel } from './video-channel'
12
13 enum ScopeNames {
14 FULL = 'FULL',
15 WITH_ACTOR = 'WITH_ACTOR'
16 }
17
18 @Scopes({
19 [ScopeNames.FULL]: {
20 include: [
21 {
22 model: () => ActorModel,
23 required: true
24 },
25 {
26 model: () => VideoModel,
27 required: true
28 }
29 ]
30 },
31 [ScopeNames.WITH_ACTOR]: {
32 include: [
33 {
34 model: () => ActorModel,
35 required: true
36 }
37 ]
38 }
39 })
40 @Table({
41 tableName: 'videoShare',
42 indexes: [
43 {
44 fields: [ 'actorId' ]
45 },
46 {
47 fields: [ 'videoId' ]
48 },
49 {
50 fields: [ 'url' ],
51 unique: true
52 }
53 ]
54 })
55 export class VideoShareModel extends Model<VideoShareModel> {
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
62 @CreatedAt
63 createdAt: Date
64
65 @UpdatedAt
66 updatedAt: Date
67
68 @ForeignKey(() => ActorModel)
69 @Column
70 actorId: number
71
72 @BelongsTo(() => ActorModel, {
73 foreignKey: {
74 allowNull: false
75 },
76 onDelete: 'cascade'
77 })
78 Actor: ActorModel
79
80 @ForeignKey(() => VideoModel)
81 @Column
82 videoId: number
83
84 @BelongsTo(() => VideoModel, {
85 foreignKey: {
86 allowNull: false
87 },
88 onDelete: 'cascade'
89 })
90 Video: VideoModel
91
92 static load (actorId: number, videoId: number, t?: Sequelize.Transaction) {
93 return VideoShareModel.scope(ScopeNames.WITH_ACTOR).findOne({
94 where: {
95 actorId,
96 videoId
97 },
98 transaction: t
99 })
100 }
101
102 static loadByUrl (url: string, t: Sequelize.Transaction) {
103 return VideoShareModel.scope(ScopeNames.FULL).findOne({
104 where: {
105 url
106 },
107 transaction: t
108 })
109 }
110
111 static loadActorsByShare (videoId: number, t: Sequelize.Transaction) {
112 const query = {
113 where: {
114 videoId
115 },
116 include: [
117 {
118 model: ActorModel,
119 required: true
120 }
121 ],
122 transaction: t
123 }
124
125 return VideoShareModel.scope(ScopeNames.FULL).findAll(query)
126 .then(res => res.map(r => r.Actor))
127 }
128
129 static loadActorsWhoSharedVideosOf (actorOwnerId: number, t: Sequelize.Transaction): Bluebird<ActorModel[]> {
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 }
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 }
191
192 static listAndCountByVideoId (videoId: number, start: number, count: number, t?: Sequelize.Transaction) {
193 const query = {
194 offset: start,
195 limit: count,
196 where: {
197 videoId
198 },
199 transaction: t
200 }
201
202 return VideoShareModel.findAndCountAll(query)
203 }
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 }
217 }