aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/schedulers/peertube-version-check-scheduler.ts
blob: c8960465c29129ec079ff043ec6e8ffbfd130845 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { doJSONRequest } from '@server/helpers/requests'
import { ApplicationModel } from '@server/models/application/application'
import { compareSemVer } from '@shared/core-utils'
import { JoinPeerTubeVersions } from '@shared/models'
import { logger } from '../../helpers/logger'
import { CONFIG } from '../../initializers/config'
import { PEERTUBE_VERSION, SCHEDULER_INTERVALS_MS } from '../../initializers/constants'
import { Notifier } from '../notifier'
import { AbstractScheduler } from './abstract-scheduler'

export class PeerTubeVersionCheckScheduler extends AbstractScheduler {

  private static instance: AbstractScheduler

  protected schedulerIntervalMs = SCHEDULER_INTERVALS_MS.checkPeerTubeVersion

  private constructor () {
    super()
  }

  protected async internalExecute () {
    return this.checkLatestVersion()
  }

  private async checkLatestVersion () {
    if (CONFIG.PEERTUBE.CHECK_LATEST_VERSION.ENABLED === false) return

    logger.info('Checking latest PeerTube version.')

    const { body } = await doJSONRequest<JoinPeerTubeVersions>(CONFIG.PEERTUBE.CHECK_LATEST_VERSION.URL)

    if (!body?.peertube?.latestVersion) {
      logger.warn('Cannot check latest PeerTube version: body is invalid.', { body })
      return
    }

    const latestVersion = body.peertube.latestVersion
    const application = await ApplicationModel.load()

    // Already checked this version
    if (application.latestPeerTubeVersion === latestVersion) return

    if (compareSemVer(PEERTUBE_VERSION, latestVersion) < 0) {
      application.latestPeerTubeVersion = latestVersion
      await application.save()

      Notifier.Instance.notifyOfNewPeerTubeVersion(application, latestVersion)
    }
  }

  static get Instance () {
    return this.instance || (this.instance = new this())
  }
}