]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/schedulers/update-videos-scheduler.ts
Add user notification base code
[github/Chocobozzz/PeerTube.git] / server / lib / schedulers / update-videos-scheduler.ts
1 import { logger } from '../../helpers/logger'
2 import { AbstractScheduler } from './abstract-scheduler'
3 import { ScheduleVideoUpdateModel } from '../../models/video/schedule-video-update'
4 import { retryTransactionWrapper } from '../../helpers/database-utils'
5 import { federateVideoIfNeeded } from '../activitypub'
6 import { SCHEDULER_INTERVALS_MS, sequelizeTypescript } from '../../initializers'
7 import { VideoPrivacy } from '../../../shared/models/videos'
8 import { Notifier } from '../notifier'
9
10 export class UpdateVideosScheduler extends AbstractScheduler {
11
12 private static instance: AbstractScheduler
13
14 protected schedulerIntervalMs = SCHEDULER_INTERVALS_MS.updateVideos
15
16 private constructor () {
17 super()
18 }
19
20 protected async internalExecute () {
21 return retryTransactionWrapper(this.updateVideos.bind(this))
22 }
23
24 private async updateVideos () {
25 if (!await ScheduleVideoUpdateModel.areVideosToUpdate()) return undefined
26
27 return sequelizeTypescript.transaction(async t => {
28 const schedules = await ScheduleVideoUpdateModel.listVideosToUpdate(t)
29
30 for (const schedule of schedules) {
31 const video = schedule.Video
32 logger.info('Executing scheduled video update on %s.', video.uuid)
33
34 if (schedule.privacy) {
35 const oldPrivacy = video.privacy
36 const isNewVideo = oldPrivacy === VideoPrivacy.PRIVATE
37
38 video.privacy = schedule.privacy
39 if (isNewVideo === true) video.publishedAt = new Date()
40
41 await video.save({ transaction: t })
42 await federateVideoIfNeeded(video, isNewVideo, t)
43
44 if (oldPrivacy === VideoPrivacy.UNLISTED || oldPrivacy === VideoPrivacy.PRIVATE) {
45 Notifier.Instance.notifyOnNewVideo(video)
46 }
47 }
48
49 await schedule.destroy({ transaction: t })
50 }
51 })
52 }
53
54 static get Instance () {
55 return this.instance || (this.instance = new this())
56 }
57 }