mirror of
https://github.com/Chocobozzz/PeerTube.git
synced 2025-10-03 01:39:37 +02:00
Optimize updating token activity
This commit is contained in:
parent
448bc823ef
commit
12c9825658
4 changed files with 34 additions and 15 deletions
|
@ -1,28 +1,45 @@
|
||||||
import { MOAuthToken } from '@server/types/models/index.js'
|
import { OAuthTokenModel } from '@server/models/oauth/oauth-token.js'
|
||||||
import { SCHEDULER_INTERVALS_MS } from '../../initializers/constants.js'
|
import { SCHEDULER_INTERVALS_MS } from '../../initializers/constants.js'
|
||||||
import { AbstractScheduler } from './abstract-scheduler.js'
|
import { AbstractScheduler } from './abstract-scheduler.js'
|
||||||
|
|
||||||
|
type UpdatePayload = {
|
||||||
|
id: number
|
||||||
|
lastActivityDate: Date
|
||||||
|
lastActivityIP: string
|
||||||
|
lastActivityDevice: string
|
||||||
|
}
|
||||||
|
|
||||||
export class UpdateTokenSessionScheduler extends AbstractScheduler {
|
export class UpdateTokenSessionScheduler extends AbstractScheduler {
|
||||||
private static instance: UpdateTokenSessionScheduler
|
private static instance: UpdateTokenSessionScheduler
|
||||||
|
|
||||||
protected schedulerIntervalMs = SCHEDULER_INTERVALS_MS.UPDATE_TOKEN_SESSION
|
protected schedulerIntervalMs = SCHEDULER_INTERVALS_MS.UPDATE_TOKEN_SESSION
|
||||||
|
|
||||||
private readonly toUpdate = new Set<MOAuthToken>()
|
private readonly toUpdate = new Set<UpdatePayload>()
|
||||||
|
|
||||||
private constructor () {
|
private constructor () {
|
||||||
super()
|
super()
|
||||||
}
|
}
|
||||||
|
|
||||||
addToUpdate (token: MOAuthToken) {
|
addToUpdate (payload: UpdatePayload) {
|
||||||
this.toUpdate.add(token)
|
this.toUpdate.add(payload)
|
||||||
}
|
}
|
||||||
|
|
||||||
protected async internalExecute () {
|
protected async internalExecute () {
|
||||||
const toUpdate = Array.from(this.toUpdate)
|
const toUpdate = Array.from(this.toUpdate)
|
||||||
this.toUpdate.clear()
|
this.toUpdate.clear()
|
||||||
|
|
||||||
for (const token of toUpdate) {
|
for (const payload of toUpdate) {
|
||||||
await token.save()
|
await OAuthTokenModel.update({
|
||||||
|
lastActivityDate: payload.lastActivityDate,
|
||||||
|
lastActivityIP: payload.lastActivityIP,
|
||||||
|
lastActivityDevice: payload.lastActivityDevice
|
||||||
|
}, {
|
||||||
|
where: {
|
||||||
|
id: payload.id
|
||||||
|
},
|
||||||
|
// Prevent tokens cache invalidation, we don't update fields that are meaningful for this cache
|
||||||
|
hooks: false
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -13,11 +13,12 @@ export function authenticate (req: express.Request, res: express.Response, next:
|
||||||
res.locals.oauth = { token }
|
res.locals.oauth = { token }
|
||||||
res.locals.authenticated = true
|
res.locals.authenticated = true
|
||||||
|
|
||||||
token.lastActivityDate = new Date()
|
UpdateTokenSessionScheduler.Instance.addToUpdate({
|
||||||
token.lastActivityIP = req.ip
|
id: token.id,
|
||||||
token.lastActivityDevice = req.header('user-agent')
|
lastActivityDate: new Date(),
|
||||||
|
lastActivityIP: req.ip,
|
||||||
UpdateTokenSessionScheduler.Instance.addToUpdate(token)
|
lastActivityDevice: req.header('user-agent')
|
||||||
|
})
|
||||||
|
|
||||||
return next()
|
return next()
|
||||||
})
|
})
|
||||||
|
|
|
@ -7,8 +7,9 @@ type Use<K extends keyof OAuthTokenModel, M> = PickWith<OAuthTokenModel, K, M>
|
||||||
// ############################################################################
|
// ############################################################################
|
||||||
|
|
||||||
export type MOAuthToken = Omit<OAuthTokenModel, 'User' | 'OAuthClients'>
|
export type MOAuthToken = Omit<OAuthTokenModel, 'User' | 'OAuthClients'>
|
||||||
|
export type MOAuthTokenLight = Omit<MOAuthToken, 'lastActivityDate' | 'lastActivityDevice' | 'lastActivityIP'>
|
||||||
|
|
||||||
export type MOAuthTokenUser =
|
export type MOAuthTokenUser =
|
||||||
& MOAuthToken
|
& MOAuthTokenLight
|
||||||
& Use<'User', MUserAccountUrl>
|
& Use<'User', MUserAccountUrl>
|
||||||
& { user?: MUserAccountUrl }
|
& { user?: MUserAccountUrl }
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import express from 'express'
|
|
||||||
import { UserAdminFlagType, UserRoleType } from '@peertube/peertube-models'
|
import { UserAdminFlagType, UserRoleType } from '@peertube/peertube-models'
|
||||||
import { MOAuthToken, MUser } from '../models/index.js'
|
import express from 'express'
|
||||||
|
import { MOAuthTokenLight, MUser } from '../models/index.js'
|
||||||
|
|
||||||
export type RegisterServerAuthOptions = RegisterServerAuthPassOptions | RegisterServerAuthExternalOptions
|
export type RegisterServerAuthOptions = RegisterServerAuthPassOptions | RegisterServerAuthExternalOptions
|
||||||
|
|
||||||
|
@ -47,7 +47,7 @@ interface RegisterServerAuthBase {
|
||||||
|
|
||||||
// Your plugin can hook PeerTube access/refresh token validity
|
// Your plugin can hook PeerTube access/refresh token validity
|
||||||
// So you can control for your plugin the user session lifetime
|
// So you can control for your plugin the user session lifetime
|
||||||
hookTokenValidity?(options: { token: MOAuthToken, type: 'access' | 'refresh' }): Promise<{ valid: boolean }>
|
hookTokenValidity?(options: { token: MOAuthTokenLight, type: 'access' | 'refresh' }): Promise<{ valid: boolean }>
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface RegisterServerAuthPassOptions extends RegisterServerAuthBase {
|
export interface RegisterServerAuthPassOptions extends RegisterServerAuthBase {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue