From 9f79ade627f0044606a9fbbe16ca0154661d12b9 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 19 Mar 2019 14:13:53 +0100 Subject: Refresh playlists --- server/models/activitypub/actor.ts | 9 ++------- server/models/utils.ts | 12 +++++++++++- server/models/video/video-playlist.ts | 15 ++++++++++++++- server/models/video/video.ts | 11 +++-------- 4 files changed, 30 insertions(+), 17 deletions(-) (limited to 'server/models') diff --git a/server/models/activitypub/actor.ts b/server/models/activitypub/actor.ts index 2fceb21dd..7d91e8a4a 100644 --- a/server/models/activitypub/actor.ts +++ b/server/models/activitypub/actor.ts @@ -34,7 +34,7 @@ import { ACTIVITY_PUB, ACTIVITY_PUB_ACTOR_TYPES, CONFIG, CONSTRAINTS_FIELDS } fr import { AccountModel } from '../account/account' import { AvatarModel } from '../avatar/avatar' import { ServerModel } from '../server/server' -import { throwIfNotValid } from '../utils' +import { isOutdated, throwIfNotValid } from '../utils' import { VideoChannelModel } from '../video/video-channel' import { ActorFollowModel } from './actor-follow' import { VideoModel } from '../video/video' @@ -532,11 +532,6 @@ export class ActorModel extends Model { isOutdated () { if (this.isOwned()) return false - const now = Date.now() - const createdAtTime = this.createdAt.getTime() - const updatedAtTime = this.updatedAt.getTime() - - return (now - createdAtTime) > ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL && - (now - updatedAtTime) > ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL + return isOutdated(this, ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL) } } diff --git a/server/models/utils.ts b/server/models/utils.ts index 4ebd07dab..f8a71b270 100644 --- a/server/models/utils.ts +++ b/server/models/utils.ts @@ -1,5 +1,6 @@ import { Sequelize } from 'sequelize-typescript' import * as validator from 'validator' +import { ACTIVITY_PUB } from '../initializers' type SortType = { sortModel: any, sortValue: string } @@ -44,6 +45,14 @@ function getSortOnModel (model: any, value: string, lastSort: string[] = [ 'id', return [ firstSort, lastSort ] } +function isOutdated (model: { createdAt: Date, updatedAt: Date }, refreshInterval: number) { + const now = Date.now() + const createdAtTime = model.createdAt.getTime() + const updatedAtTime = model.updatedAt.getTime() + + return (now - createdAtTime) > refreshInterval && (now - updatedAtTime) > refreshInterval +} + function throwIfNotValid (value: any, validator: (value: any) => boolean, fieldName = 'value') { if (validator(value) === false) { throw new Error(`"${value}" is not a valid ${fieldName}.`) @@ -108,7 +117,8 @@ export { throwIfNotValid, buildServerIdsFollowedBy, buildTrigramSearchIndex, - buildWhereIdOrUUID + buildWhereIdOrUUID, + isOutdated } // --------------------------------------------------------------------------- diff --git a/server/models/video/video-playlist.ts b/server/models/video/video-playlist.ts index 7dbe4ce8d..08e4d32c8 100644 --- a/server/models/video/video-playlist.ts +++ b/server/models/video/video-playlist.ts @@ -17,7 +17,7 @@ import { } from 'sequelize-typescript' import * as Sequelize from 'sequelize' import { VideoPlaylistPrivacy } from '../../../shared/models/videos/playlist/video-playlist-privacy.model' -import { buildServerIdsFollowedBy, buildWhereIdOrUUID, getSort, throwIfNotValid } from '../utils' +import { buildServerIdsFollowedBy, buildWhereIdOrUUID, getSort, isOutdated, throwIfNotValid } from '../utils' import { isVideoPlaylistDescriptionValid, isVideoPlaylistNameValid, @@ -25,6 +25,7 @@ import { } from '../../helpers/custom-validators/video-playlists' import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc' import { + ACTIVITY_PUB, CONFIG, CONSTRAINTS_FIELDS, STATIC_PATHS, @@ -429,10 +430,22 @@ export class VideoPlaylistModel extends Model { .catch(err => logger.warn('Cannot delete thumbnail %s.', thumbnailPath, { err })) } + setAsRefreshed () { + this.changed('updatedAt', true) + + return this.save() + } + isOwned () { return this.OwnerAccount.isOwned() } + isOutdated () { + if (this.isOwned()) return false + + return isOutdated(this, ACTIVITY_PUB.VIDEO_PLAYLIST_REFRESH_INTERVAL) + } + toFormattedJSON (): VideoPlaylist { return { id: this.id, diff --git a/server/models/video/video.ts b/server/models/video/video.ts index 946be6095..fb037c21a 100644 --- a/server/models/video/video.ts +++ b/server/models/video/video.ts @@ -77,7 +77,7 @@ import { buildTrigramSearchIndex, buildWhereIdOrUUID, createSimilarityAttribute, - getVideoSort, + getVideoSort, isOutdated, throwIfNotValid } from '../utils' import { TagModel } from './tag' @@ -1547,7 +1547,7 @@ export class VideoModel extends Model { attributes: query.attributes, order: [ // Keep original order Sequelize.literal( - ids.map(id => `"VideoModel".id = ${id} DESC`).join(', ') + ids.map(id => `"VideoModel".id = ${id}`).join(', ') ) ] } @@ -1767,12 +1767,7 @@ export class VideoModel extends Model { isOutdated () { if (this.isOwned()) return false - const now = Date.now() - const createdAtTime = this.createdAt.getTime() - const updatedAtTime = this.updatedAt.getTime() - - return (now - createdAtTime) > ACTIVITY_PUB.VIDEO_REFRESH_INTERVAL && - (now - updatedAtTime) > ACTIVITY_PUB.VIDEO_REFRESH_INTERVAL + return isOutdated(this, ACTIVITY_PUB.VIDEO_REFRESH_INTERVAL) } setAsRefreshed () { -- cgit v1.2.3