]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/cache-file.ts
Fix videos list user NSFW policy
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / cache-file.ts
CommitLineData
c48e82b5
C
1import { CacheFileObject } from '../../../shared/index'
2import { VideoModel } from '../../models/video/video'
c48e82b5
C
3import { sequelizeTypescript } from '../../initializers'
4import { VideoRedundancyModel } from '../../models/redundancy/video-redundancy'
5
e587e0ec 6function cacheFileActivityObjectToDBAttributes (cacheFileObject: CacheFileObject, video: VideoModel, byActor: { id?: number }) {
c48e82b5
C
7 const url = cacheFileObject.url
8
9 const videoFile = video.VideoFiles.find(f => {
10 return f.resolution === url.height && f.fps === url.fps
11 })
12
13 if (!videoFile) throw new Error(`Cannot find video file ${url.height} ${url.fps} of video ${video.url}`)
14
15 return {
16 expiresOn: new Date(cacheFileObject.expires),
17 url: cacheFileObject.id,
18 fileUrl: cacheFileObject.url.href,
19 strategy: null,
20 videoFileId: videoFile.id,
21 actorId: byActor.id
22 }
23}
24
e587e0ec 25function createCacheFile (cacheFileObject: CacheFileObject, video: VideoModel, byActor: { id?: number }) {
c48e82b5
C
26 return sequelizeTypescript.transaction(async t => {
27 const attributes = cacheFileActivityObjectToDBAttributes(cacheFileObject, video, byActor)
28
29 return VideoRedundancyModel.create(attributes, { transaction: t })
30 })
31}
32
e587e0ec 33function updateCacheFile (cacheFileObject: CacheFileObject, redundancyModel: VideoRedundancyModel, byActor: { id?: number }) {
12ba460e
C
34 if (redundancyModel.actorId !== byActor.id) {
35 throw new Error('Cannot update redundancy ' + redundancyModel.url + ' of another actor.')
36 }
37
c48e82b5
C
38 const attributes = cacheFileActivityObjectToDBAttributes(cacheFileObject, redundancyModel.VideoFile.Video, byActor)
39
40 redundancyModel.set('expires', attributes.expiresOn)
41 redundancyModel.set('fileUrl', attributes.fileUrl)
42
43 return redundancyModel.save()
44}
45
46export {
47 createCacheFile,
48 updateCacheFile,
49 cacheFileActivityObjectToDBAttributes
50}