import { videoPlaylistElementAPGetValidator, videoPlaylistsGetValidator } from '../../middlewares/validators/videos/video-playlists'
import { VideoPlaylistModel } from '../../models/video/video-playlist'
import { VideoPlaylistPrivacy } from '../../../shared/models/videos/playlist/video-playlist-privacy.model'
-import { MAccountId, MActorId, MVideo, MVideoAPWithoutCaption } from '@server/typings/models'
+import { MAccountId, MActorId, MVideo, MVideoAPWithoutCaption, MVideoId } from '@server/typings/models'
const activityPubClientRouter = express.Router()
)
activityPubClientRouter.get('/videos/watch/:id/announces',
executeIfActivityPub,
- asyncMiddleware(videosCustomGetValidator('only-video')),
+ asyncMiddleware(videosCustomGetValidator('only-immutable-attributes')),
asyncMiddleware(videoAnnouncesController)
)
activityPubClientRouter.get('/videos/watch/:id/announces/:actorId',
)
activityPubClientRouter.get('/videos/watch/:id/likes',
executeIfActivityPub,
- asyncMiddleware(videosCustomGetValidator('only-video')),
+ asyncMiddleware(videosCustomGetValidator('only-immutable-attributes')),
asyncMiddleware(videoLikesController)
)
activityPubClientRouter.get('/videos/watch/:id/dislikes',
executeIfActivityPub,
- asyncMiddleware(videosCustomGetValidator('only-video')),
+ asyncMiddleware(videosCustomGetValidator('only-immutable-attributes')),
asyncMiddleware(videoDislikesController)
)
activityPubClientRouter.get('/videos/watch/:id/comments',
executeIfActivityPub,
- asyncMiddleware(videosCustomGetValidator('only-video')),
+ asyncMiddleware(videosCustomGetValidator('only-immutable-attributes')),
asyncMiddleware(videoCommentsController)
)
activityPubClientRouter.get('/videos/watch/:videoId/comments/:commentId',
}
async function videoAnnouncesController (req: express.Request, res: express.Response) {
- const video = res.locals.onlyVideo
+ const video = res.locals.onlyImmutableVideo
const handler = async (start: number, count: number) => {
const result = await VideoShareModel.listAndCountByVideoId(video.id, start, count)
}
async function videoLikesController (req: express.Request, res: express.Response) {
- const video = res.locals.onlyVideo
+ const video = res.locals.onlyImmutableVideo
const json = await videoRates(req, 'like', video, getVideoLikesActivityPubUrl(video))
return activityPubResponse(activityPubContextify(json), res)
}
async function videoDislikesController (req: express.Request, res: express.Response) {
- const video = res.locals.onlyVideo
+ const video = res.locals.onlyImmutableVideo
const json = await videoRates(req, 'dislike', video, getVideoDislikesActivityPubUrl(video))
return activityPubResponse(activityPubContextify(json), res)
}
async function videoCommentsController (req: express.Request, res: express.Response) {
- const video = res.locals.onlyVideo
+ const video = res.locals.onlyImmutableVideo
const handler = async (start: number, count: number) => {
const result = await VideoCommentModel.listAndCountByVideoId(video.id, start, count)
return activityPubCollectionPagination(WEBSERVER.URL + req.path, handler, req.query.page)
}
-function videoRates (req: express.Request, rateType: VideoRateType, video: MVideo, url: string) {
+function videoRates (req: express.Request, rateType: VideoRateType, video: MVideoId, url: string) {
const handler = async (start: number, count: number) => {
const result = await AccountVideoRateModel.listAndCountAccountUrlsByVideoId(rateType, video.id, start, count)
return {
import { fetchVideo, VideoFetchType } from '../video'
import { UserRight } from '../../../shared/models/users'
import { VideoChannelModel } from '../../models/video/video-channel'
-import { MUser, MUserAccountId, MVideoAccountLight, MVideoFullLight, MVideoThumbnail, MVideoWithRights } from '@server/typings/models'
+import {
+ MUser,
+ MUserAccountId,
+ MVideoAccountLight,
+ MVideoFullLight,
+ MVideoIdThumbnail,
+ MVideoImmutable,
+ MVideoThumbnail,
+ MVideoWithRights
+} from '@server/typings/models'
async function doesVideoExist (id: number | string, res: Response, fetchType: VideoFetchType = 'all') {
const userId = res.locals.oauth ? res.locals.oauth.token.User.id : undefined
res.locals.videoAll = video as MVideoFullLight
break
+ case 'only-immutable-attributes':
+ res.locals.onlyImmutableVideo = video as MVideoImmutable
+ break
+
case 'id':
- res.locals.videoId = video
+ res.locals.videoId = video as MVideoIdThumbnail
break
case 'only-video':
MVideoFullLight,
MVideoIdThumbnail,
MVideoThumbnail,
- MVideoWithRights
+ MVideoWithRights,
+ MVideoImmutable
} from '@server/typings/models'
import { Response } from 'express'
-type VideoFetchType = 'all' | 'only-video' | 'only-video-with-rights' | 'id' | 'none'
+type VideoFetchType = 'all' | 'only-video' | 'only-video-with-rights' | 'id' | 'none' | 'only-immutable-attributes'
function fetchVideo (id: number | string, fetchType: 'all', userId?: number): Bluebird<MVideoFullLight>
+function fetchVideo (id: number | string, fetchType: 'only-immutable-attributes'): Bluebird<MVideoImmutable>
function fetchVideo (id: number | string, fetchType: 'only-video', userId?: number): Bluebird<MVideoThumbnail>
function fetchVideo (id: number | string, fetchType: 'only-video-with-rights', userId?: number): Bluebird<MVideoWithRights>
function fetchVideo (id: number | string, fetchType: 'id' | 'none', userId?: number): Bluebird<MVideoIdThumbnail>
id: number | string,
fetchType: VideoFetchType,
userId?: number
-): Bluebird<MVideoFullLight | MVideoThumbnail | MVideoWithRights | MVideoIdThumbnail>
+): Bluebird<MVideoFullLight | MVideoThumbnail | MVideoWithRights | MVideoIdThumbnail | MVideoImmutable>
function fetchVideo (
id: number | string,
fetchType: VideoFetchType,
userId?: number
-): Bluebird<MVideoFullLight | MVideoThumbnail | MVideoWithRights | MVideoIdThumbnail> {
+): Bluebird<MVideoFullLight | MVideoThumbnail | MVideoWithRights | MVideoIdThumbnail | MVideoImmutable> {
if (fetchType === 'all') return VideoModel.loadAndPopulateAccountAndServerAndTags(id, undefined, userId)
+ if (fetchType === 'only-immutable-attributes') return VideoModel.loadImmutableAttributes(id)
+
if (fetchType === 'only-video-with-rights') return VideoModel.loadWithRights(id)
if (fetchType === 'only-video') return VideoModel.load(id)
})
}
-const videosCustomGetValidator = (fetchType: 'all' | 'only-video' | 'only-video-with-rights', authenticateInQuery = false) => {
+const videosCustomGetValidator = (
+ fetchType: 'all' | 'only-video' | 'only-video-with-rights' | 'only-immutable-attributes',
+ authenticateInQuery = false
+) => {
return [
param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'),
'local-account-name'
| 'local-actor-name'
| 'local-actor-url'
+ | 'video-immutable'
+
+type DeleteKey =
+ 'video'
class ModelCache {
private readonly localCache: { [id in ModelCacheType]: Map<string, any> } = {
'local-account-name': new Map(),
'local-actor-name': new Map(),
- 'local-actor-url': new Map()
+ 'local-actor-url': new Map(),
+ 'video-immutable': new Map()
+ }
+
+ private readonly deleteIds: {
+ [deleteKey in DeleteKey]: Map<number, { cacheType: ModelCacheType, key: string }[]>
+ } = {
+ video: new Map()
}
private constructor () {
key: string
fun: () => Bluebird<T>
whitelist?: () => boolean
+ deleteKey?: DeleteKey
}) {
- const { cacheType, key, fun, whitelist } = options
+ const { cacheType, key, fun, whitelist, deleteKey } = options
if (whitelist && whitelist() !== true) return fun()
}
return fun().then(m => {
+ if (!m) return m
+
if (!whitelist || whitelist()) cache.set(key, m)
+ if (deleteKey) {
+ const map = this.deleteIds[deleteKey]
+ if (!map.has(m.id)) map.set(m.id, [])
+
+ const a = map.get(m.id)
+ a.push({ cacheType, key })
+ }
+
return m
})
}
+
+ invalidateCache (deleteKey: DeleteKey, modelId: number) {
+ const map = this.deleteIds[deleteKey]
+
+ if (!map.has(modelId)) return
+
+ for (const toDelete of map.get(modelId)) {
+ logger.debug('Removing %s -> %d of model cache %s -> %s.', deleteKey, modelId, toDelete.cacheType, toDelete.key)
+ this.localCache[toDelete.cacheType].delete(toDelete.key)
+ }
+
+ map.delete(modelId)
+ }
}
export {
MVideoFormattableDetails,
MVideoForUser,
MVideoFullLight,
- MVideoIdThumbnail,
+ MVideoIdThumbnail, MVideoImmutable,
MVideoThumbnail,
MVideoThumbnailBlacklist,
MVideoWithAllFiles,
import { VideoFile } from '@shared/models/videos/video-file.model'
import { getHLSDirectory, getTorrentFileName, getTorrentFilePath, getVideoFilename, getVideoFilePath } from '@server/lib/video-paths'
import validator from 'validator'
+import { ModelCache } from '@server/models/model-cache'
export enum ScopeNames {
AVAILABLE_FOR_LIST_IDS = 'AVAILABLE_FOR_LIST_IDS',
return undefined
}
+ @BeforeDestroy
+ static invalidateCache (instance: VideoModel) {
+ ModelCache.Instance.invalidateCache('video', instance.id)
+ }
+
static listLocal (): Bluebird<MVideoWithAllFiles[]> {
const query = {
where: {
]).findOne(options)
}
+ static loadImmutableAttributes (id: number | string, t?: Transaction): Bluebird<MVideoImmutable> {
+ const fun = () => {
+ const where = buildWhereIdOrUUID(id)
+ const options = {
+ attributes: [
+ 'id', 'url', 'uuid'
+ ],
+ where,
+ transaction: t
+ }
+
+ return VideoModel.unscoped().findOne(options)
+ }
+
+ return ModelCache.Instance.doCache({
+ cacheType: 'video-immutable',
+ key: '' + id,
+ deleteKey: 'video',
+ fun
+ })
+ }
+
static loadWithRights (id: number | string, t?: Transaction): Bluebird<MVideoWithRights> {
const where = buildWhereIdOrUUID(id)
const options = {
} from './models'
import { MVideoPlaylistFull, MVideoPlaylistFullSummary } from './models/video/video-playlist'
import { MVideoImportDefault } from '@server/typings/models/video/video-import'
-import { MAccountBlocklist, MActorUrl, MStreamingPlaylist, MVideoFile } from '@server/typings/models'
+import { MAccountBlocklist, MActorUrl, MStreamingPlaylist, MVideoFile, MVideoImmutable } from '@server/typings/models'
import { MVideoPlaylistElement, MVideoPlaylistElementVideoUrlPlaylistPrivacy } from '@server/typings/models/video/video-playlist-element'
import { MAccountVideoRateAccountVideo } from '@server/typings/models/video/video-rate'
import { MVideoChangeOwnershipFull } from './models/video/video-change-ownership'
locals: {
videoAll?: MVideoFullLight
+ onlyImmutableVideo?: MVideoImmutable
onlyVideo?: MVideoThumbnail
onlyVideoWithRights?: MVideoWithRights
videoId?: MVideoIdThumbnail
export type MVideoUrl = Pick<MVideo, 'url'>
export type MVideoUUID = Pick<MVideo, 'uuid'>
+export type MVideoImmutable = Pick<MVideo, 'id' | 'url' | 'uuid'>
export type MVideoIdUrl = MVideoId & MVideoUrl
export type MVideoFeed = Pick<MVideo, 'name' | 'uuid'>