PeerTube/server/core/lib/schedulers/peertube-version-check-sche...

55 lines
1.8 KiB
TypeScript
Raw Normal View History

import { doJSONRequest } from '@server/helpers/requests.js'
import { ApplicationModel } from '@server/models/application/application.js'
import { compareSemVer } from '@peertube/peertube-core-utils'
import { JoinPeerTubeVersions } from '@peertube/peertube-models'
import { logger } from '../../helpers/logger.js'
import { CONFIG } from '../../initializers/config.js'
import { PEERTUBE_VERSION, SCHEDULER_INTERVALS_MS } from '../../initializers/constants.js'
import { Notifier } from '../notifier/index.js'
import { AbstractScheduler } from './abstract-scheduler.js'
2021-03-11 16:54:52 +01:00
export class PeerTubeVersionCheckScheduler extends AbstractScheduler {
private static instance: AbstractScheduler
2021-10-22 10:28:00 +02:00
protected schedulerIntervalMs = SCHEDULER_INTERVALS_MS.CHECK_PEERTUBE_VERSION
2021-03-11 16:54:52 +01:00
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.')
2024-08-14 15:32:25 +02:00
const { body } = await doJSONRequest<JoinPeerTubeVersions>(CONFIG.PEERTUBE.CHECK_LATEST_VERSION.URL, { preventSSRF: false })
2021-03-11 16:54:52 +01:00
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())
}
}