]>
Commit | Line | Data |
---|---|---|
d8465018 | 1 | import * as Sequelize from 'sequelize' |
2422c46b | 2 | import * as Bluebird from 'bluebird' |
4ba3b8ea C |
3 | import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Is, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript' |
4 | import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc' | |
5 | import { CONSTRAINTS_FIELDS } from '../../initializers' | |
265ba139 | 6 | import { AccountModel } from '../account/account' |
50d6de9c | 7 | import { ActorModel } from '../activitypub/actor' |
4ba3b8ea | 8 | import { throwIfNotValid } from '../utils' |
3fd3ab2d | 9 | import { VideoModel } from './video' |
265ba139 | 10 | import { VideoChannelModel } from './video-channel' |
d8465018 | 11 | |
d48ff09d C |
12 | enum ScopeNames { |
13 | FULL = 'FULL', | |
50d6de9c | 14 | WITH_ACTOR = 'WITH_ACTOR' |
d48ff09d C |
15 | } |
16 | ||
17 | @Scopes({ | |
18 | [ScopeNames.FULL]: { | |
19 | include: [ | |
20 | { | |
50d6de9c | 21 | model: () => ActorModel, |
d48ff09d C |
22 | required: true |
23 | }, | |
24 | { | |
25 | model: () => VideoModel, | |
26 | required: true | |
27 | } | |
28 | ] | |
29 | }, | |
50d6de9c | 30 | [ScopeNames.WITH_ACTOR]: { |
d48ff09d C |
31 | include: [ |
32 | { | |
50d6de9c | 33 | model: () => ActorModel, |
d48ff09d C |
34 | required: true |
35 | } | |
36 | ] | |
37 | } | |
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 | }) |
54 | export 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 | |
5c6d985f | 91 | static load (actorId: number, videoId: number, t?: Sequelize.Transaction) { |
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 | ||
0f320037 | 101 | static loadByUrl (url: string, t: Sequelize.Transaction) { |
9588d4f4 | 102 | return VideoShareModel.scope(ScopeNames.FULL).findOne({ |
0f320037 C |
103 | where: { |
104 | url | |
105 | }, | |
106 | transaction: t | |
107 | }) | |
108 | } | |
109 | ||
50d6de9c | 110 | static loadActorsByShare (videoId: number, t: Sequelize.Transaction) { |
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) |
50d6de9c | 125 | .then(res => res.map(r => r.Actor)) |
3fd3ab2d | 126 | } |
265ba139 | 127 | |
df0b219d | 128 | static loadActorsWhoSharedVideosOf (actorOwnerId: number, t: Sequelize.Transaction): Bluebird<ActorModel[]> { |
265ba139 C |
129 | const query = { |
130 | attributes: [], | |
131 | include: [ | |
132 | { | |
133 | model: ActorModel, | |
134 | required: true | |
135 | }, | |
136 | { | |
137 | attributes: [], | |
138 | model: VideoModel, | |
139 | required: true, | |
140 | include: [ | |
141 | { | |
142 | attributes: [], | |
143 | model: VideoChannelModel.unscoped(), | |
144 | required: true, | |
145 | include: [ | |
146 | { | |
147 | attributes: [], | |
148 | model: AccountModel.unscoped(), | |
149 | required: true, | |
150 | where: { | |
151 | actorId: actorOwnerId | |
152 | } | |
153 | } | |
154 | ] | |
155 | } | |
156 | ] | |
157 | } | |
158 | ], | |
159 | transaction: t | |
160 | } | |
161 | ||
162 | return VideoShareModel.scope(ScopeNames.FULL).findAll(query) | |
163 | .then(res => res.map(r => r.Actor)) | |
164 | } | |
2422c46b C |
165 | |
166 | static loadActorsByVideoChannel (videoChannelId: number, t: Sequelize.Transaction): Bluebird<ActorModel[]> { | |
167 | const query = { | |
168 | attributes: [], | |
169 | include: [ | |
170 | { | |
171 | model: ActorModel, | |
172 | required: true | |
173 | }, | |
174 | { | |
175 | attributes: [], | |
176 | model: VideoModel, | |
177 | required: true, | |
178 | where: { | |
179 | channelId: videoChannelId | |
180 | } | |
181 | } | |
182 | ], | |
183 | transaction: t | |
184 | } | |
185 | ||
186 | return VideoShareModel.scope(ScopeNames.FULL) | |
187 | .findAll(query) | |
188 | .then(res => res.map(r => r.Actor)) | |
189 | } | |
8fffe21a C |
190 | |
191 | static listAndCountByVideoId (videoId: number, start: number, count: number, t?: Sequelize.Transaction) { | |
192 | const query = { | |
9a4a9b6c C |
193 | offset: start, |
194 | limit: count, | |
8fffe21a C |
195 | where: { |
196 | videoId | |
197 | }, | |
198 | transaction: t | |
199 | } | |
200 | ||
201 | return VideoShareModel.findAndCountAll(query) | |
202 | } | |
d7d5611c | 203 | } |