aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/video/video-channel.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2017-12-14 10:07:57 +0100
committerChocobozzz <me@florianbigard.com>2017-12-14 10:07:57 +0100
commitd48ff09d27d234425c3e9f091ae9072d8e6d8b7a (patch)
tree3a842b79aec40ca55d68c1cb6cf9b8aadcf2a1d1 /server/models/video/video-channel.ts
parent94edfc3b2a9cf83f1c9c470a76e4769bc37aad14 (diff)
downloadPeerTube-d48ff09d27d234425c3e9f091ae9072d8e6d8b7a.tar.gz
PeerTube-d48ff09d27d234425c3e9f091ae9072d8e6d8b7a.tar.zst
PeerTube-d48ff09d27d234425c3e9f091ae9072d8e6d8b7a.zip
Use sequelize scopes
Diffstat (limited to 'server/models/video/video-channel.ts')
-rw-r--r--server/models/video/video-channel.ts113
1 files changed, 31 insertions, 82 deletions
diff --git a/server/models/video/video-channel.ts b/server/models/video/video-channel.ts
index 9b545a4ef..068c8029d 100644
--- a/server/models/video/video-channel.ts
+++ b/server/models/video/video-channel.ts
@@ -11,7 +11,7 @@ import {
11 HasMany, 11 HasMany,
12 Is, 12 Is,
13 IsUUID, 13 IsUUID,
14 Model, 14 Model, Scopes,
15 Table, 15 Table,
16 UpdatedAt 16 UpdatedAt
17} from 'sequelize-typescript' 17} from 'sequelize-typescript'
@@ -28,6 +28,26 @@ import { getSort, throwIfNotValid } from '../utils'
28import { VideoModel } from './video' 28import { VideoModel } from './video'
29import { VideoChannelShareModel } from './video-channel-share' 29import { VideoChannelShareModel } from './video-channel-share'
30 30
31enum ScopeNames {
32 WITH_ACCOUNT = 'WITH_ACCOUNT',
33 WITH_VIDEOS = 'WITH_VIDEOS'
34}
35
36@Scopes({
37 [ScopeNames.WITH_ACCOUNT]: {
38 include: [
39 {
40 model: () => AccountModel,
41 include: [ { model: () => ServerModel, required: false } ]
42 }
43 ]
44 },
45 [ScopeNames.WITH_VIDEOS]: {
46 include: [
47 () => VideoModel
48 ]
49 }
50})
31@Table({ 51@Table({
32 tableName: 'videoChannel', 52 tableName: 'videoChannel',
33 indexes: [ 53 indexes: [
@@ -122,17 +142,10 @@ export class VideoChannelModel extends Model<VideoChannelModel> {
122 const query = { 142 const query = {
123 offset: start, 143 offset: start,
124 limit: count, 144 limit: count,
125 order: [ getSort(sort) ], 145 order: [ getSort(sort) ]
126 include: [
127 {
128 model: AccountModel,
129 required: true,
130 include: [ { model: ServerModel, required: false } ]
131 }
132 ]
133 } 146 }
134 147
135 return VideoChannelModel.findAndCountAll(query) 148 return VideoChannelModel.scope(ScopeNames.WITH_ACCOUNT).findAndCountAll(query)
136 .then(({ rows, count }) => { 149 .then(({ rows, count }) => {
137 return { total: count, data: rows } 150 return { total: count, data: rows }
138 }) 151 })
@@ -159,29 +172,16 @@ export class VideoChannelModel extends Model<VideoChannelModel> {
159 }) 172 })
160 } 173 }
161 174
162 static loadByUUID (uuid: string, t?: Sequelize.Transaction) {
163 const query: IFindOptions<VideoChannelModel> = {
164 where: {
165 uuid
166 }
167 }
168
169 if (t !== undefined) query.transaction = t
170
171 return VideoChannelModel.findOne(query)
172 }
173
174 static loadByUrl (url: string, t?: Sequelize.Transaction) { 175 static loadByUrl (url: string, t?: Sequelize.Transaction) {
175 const query: IFindOptions<VideoChannelModel> = { 176 const query: IFindOptions<VideoChannelModel> = {
176 where: { 177 where: {
177 url 178 url
178 }, 179 }
179 include: [ AccountModel ]
180 } 180 }
181 181
182 if (t !== undefined) query.transaction = t 182 if (t !== undefined) query.transaction = t
183 183
184 return VideoChannelModel.findOne(query) 184 return VideoChannelModel.scope(ScopeNames.WITH_ACCOUNT).findOne(query)
185 } 185 }
186 186
187 static loadByUUIDOrUrl (uuid: string, url: string, t?: Sequelize.Transaction) { 187 static loadByUUIDOrUrl (uuid: string, url: string, t?: Sequelize.Transaction) {
@@ -199,90 +199,39 @@ export class VideoChannelModel extends Model<VideoChannelModel> {
199 return VideoChannelModel.findOne(query) 199 return VideoChannelModel.findOne(query)
200 } 200 }
201 201
202 static loadByHostAndUUID (fromHost: string, uuid: string, t?: Sequelize.Transaction) {
203 const query: IFindOptions<VideoChannelModel> = {
204 where: {
205 uuid
206 },
207 include: [
208 {
209 model: AccountModel,
210 include: [
211 {
212 model: ServerModel,
213 required: true,
214 where: {
215 host: fromHost
216 }
217 }
218 ]
219 }
220 ]
221 }
222
223 if (t !== undefined) query.transaction = t
224
225 return VideoChannelModel.findOne(query)
226 }
227
228 static loadByIdAndAccount (id: number, accountId: number) { 202 static loadByIdAndAccount (id: number, accountId: number) {
229 const options = { 203 const options = {
230 where: { 204 where: {
231 id, 205 id,
232 accountId 206 accountId
233 }, 207 }
234 include: [
235 {
236 model: AccountModel,
237 include: [ { model: ServerModel, required: false } ]
238 }
239 ]
240 } 208 }
241 209
242 return VideoChannelModel.findOne(options) 210 return VideoChannelModel.scope(ScopeNames.WITH_ACCOUNT).findOne(options)
243 } 211 }
244 212
245 static loadAndPopulateAccount (id: number) { 213 static loadAndPopulateAccount (id: number) {
246 const options = { 214 return VideoChannelModel.scope(ScopeNames.WITH_ACCOUNT).findById(id)
247 include: [
248 {
249 model: AccountModel,
250 include: [ { model: ServerModel, required: false } ]
251 }
252 ]
253 }
254
255 return VideoChannelModel.findById(id, options)
256 } 215 }
257 216
258 static loadByUUIDAndPopulateAccount (uuid: string) { 217 static loadByUUIDAndPopulateAccount (uuid: string) {
259 const options = { 218 const options = {
260 where: { 219 where: {
261 uuid 220 uuid
262 }, 221 }
263 include: [
264 {
265 model: AccountModel,
266 include: [ { model: ServerModel, required: false } ]
267 }
268 ]
269 } 222 }
270 223
271 return VideoChannelModel.findOne(options) 224 return VideoChannelModel.scope(ScopeNames.WITH_ACCOUNT).findOne(options)
272 } 225 }
273 226
274 static loadAndPopulateAccountAndVideos (id: number) { 227 static loadAndPopulateAccountAndVideos (id: number) {
275 const options = { 228 const options = {
276 include: [ 229 include: [
277 {
278 model: AccountModel,
279 include: [ { model: ServerModel, required: false } ]
280 },
281 VideoModel 230 VideoModel
282 ] 231 ]
283 } 232 }
284 233
285 return VideoChannelModel.findById(id, options) 234 return VideoChannelModel.scope([ ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_VIDEOS ]).findById(id, options)
286 } 235 }
287 236
288 isOwned () { 237 isOwned () {