From 8fffe21a7bc96d08b229293d66ddba576e609790 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 25 May 2018 16:21:16 +0200 Subject: Refractor and optimize AP collections Only display urls in general object, and paginate video comments, shares, likes and dislikes --- server/models/video/video-comment.ts | 14 ++++ server/models/video/video-share.ts | 13 ++++ server/models/video/video.ts | 128 ++--------------------------------- 3 files changed, 32 insertions(+), 123 deletions(-) (limited to 'server/models/video') diff --git a/server/models/video/video-comment.ts b/server/models/video/video-comment.ts index 5386a10aa..18398905e 100644 --- a/server/models/video/video-comment.ts +++ b/server/models/video/video-comment.ts @@ -326,6 +326,20 @@ export class VideoCommentModel extends Model { .findAll(query) } + static listAndCountByVideoId (videoId: number, start: number, count: number, t?: Sequelize.Transaction, order: 'ASC' | 'DESC' = 'ASC') { + const query = { + order: [ [ 'createdAt', order ] ], + start, + count, + where: { + videoId + }, + transaction: t + } + + return VideoCommentModel.findAndCountAll(query) + } + static async getStats () { const totalLocalVideoComments = await VideoCommentModel.count({ include: [ diff --git a/server/models/video/video-share.ts b/server/models/video/video-share.ts index 602cc69b9..adadf5dea 100644 --- a/server/models/video/video-share.ts +++ b/server/models/video/video-share.ts @@ -187,4 +187,17 @@ export class VideoShareModel extends Model { .findAll(query) .then(res => res.map(r => r.Actor)) } + + static listAndCountByVideoId (videoId: number, start: number, count: number, t?: Sequelize.Transaction) { + const query = { + start, + count, + where: { + videoId + }, + transaction: t + } + + return VideoShareModel.findAndCountAll(query) + } } diff --git a/server/models/video/video.ts b/server/models/video/video.ts index 012a758ee..f4689fe12 100644 --- a/server/models/video/video.ts +++ b/server/models/video/video.ts @@ -29,7 +29,7 @@ import { VideoPrivacy, VideoResolution } from '../../../shared' import { VideoTorrentObject } from '../../../shared/models/activitypub/objects' import { Video, VideoDetails, VideoFile } from '../../../shared/models/videos' import { VideoFilter } from '../../../shared/models/videos/video-query.type' -import { activityPubCollection } from '../../helpers/activitypub' +import { activityPubCollectionPagination } from '../../helpers/activitypub' import { createTorrentPromise, peertubeTruncate, @@ -602,18 +602,6 @@ export class VideoModel extends Model { attributes: [ 'id', 'url' ], model: VideoShareModel.unscoped(), required: false, - where: { - [Sequelize.Op.and]: [ - { - id: { - [Sequelize.Op.not]: null - } - }, - { - actorId - } - ] - }, include: [ { attributes: [ 'id', 'url' ], @@ -644,35 +632,6 @@ export class VideoModel extends Model { } ] }, - { - attributes: [ 'type' ], - model: AccountVideoRateModel, - required: false, - include: [ - { - attributes: [ 'id' ], - model: AccountModel.unscoped(), - include: [ - { - attributes: [ 'url' ], - model: ActorModel.unscoped(), - include: [ - { - attributes: [ 'host' ], - model: ServerModel, - required: false - } - ] - } - ] - } - ] - }, - { - attributes: [ 'url' ], - model: VideoCommentModel, - required: false - }, VideoFileModel, TagModel ] @@ -897,26 +856,6 @@ export class VideoModel extends Model { .findOne(options) } - static loadAndPopulateAll (id: number) { - const options = { - order: [ [ 'Tags', 'name', 'ASC' ] ], - where: { - id - } - } - - return VideoModel - .scope([ - ScopeNames.WITH_RATES, - ScopeNames.WITH_SHARES, - ScopeNames.WITH_TAGS, - ScopeNames.WITH_FILES, - ScopeNames.WITH_ACCOUNT_DETAILS, - ScopeNames.WITH_COMMENTS - ]) - .findOne(options) - } - static async getStats () { const totalLocalVideos = await VideoModel.count({ where: { @@ -1203,25 +1142,6 @@ export class VideoModel extends Model { } } - let likesObject - let dislikesObject - - if (Array.isArray(this.AccountVideoRates)) { - const res = this.toRatesActivityPubObjects() - likesObject = res.likesObject - dislikesObject = res.dislikesObject - } - - let sharesObject - if (Array.isArray(this.VideoShares)) { - sharesObject = this.toAnnouncesActivityPubObject() - } - - let commentsObject - if (Array.isArray(this.VideoComments)) { - commentsObject = this.toCommentsActivityPubObject() - } - const url = [] for (const file of this.VideoFiles) { url.push({ @@ -1280,10 +1200,10 @@ export class VideoModel extends Model { height: THUMBNAILS_SIZE.height }, url, - likes: likesObject, - dislikes: dislikesObject, - shares: sharesObject, - comments: commentsObject, + likes: getVideoLikesActivityPubUrl(this), + dislikes: getVideoDislikesActivityPubUrl(this), + shares: getVideoSharesActivityPubUrl(this), + comments: getVideoCommentsActivityPubUrl(this), attributedTo: [ { type: 'Person', @@ -1297,44 +1217,6 @@ export class VideoModel extends Model { } } - toAnnouncesActivityPubObject () { - const shares: string[] = [] - - for (const videoShare of this.VideoShares) { - shares.push(videoShare.url) - } - - return activityPubCollection(getVideoSharesActivityPubUrl(this), shares) - } - - toCommentsActivityPubObject () { - const comments: string[] = [] - - for (const videoComment of this.VideoComments) { - comments.push(videoComment.url) - } - - return activityPubCollection(getVideoCommentsActivityPubUrl(this), comments) - } - - toRatesActivityPubObjects () { - const likes: string[] = [] - const dislikes: string[] = [] - - for (const rate of this.AccountVideoRates) { - if (rate.type === 'like') { - likes.push(rate.Account.Actor.url) - } else if (rate.type === 'dislike') { - dislikes.push(rate.Account.Actor.url) - } - } - - const likesObject = activityPubCollection(getVideoLikesActivityPubUrl(this), likes) - const dislikesObject = activityPubCollection(getVideoDislikesActivityPubUrl(this), dislikes) - - return { likesObject, dislikesObject } - } - getTruncatedDescription () { if (!this.description) return null -- cgit v1.2.3