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/account/account-video-rate.ts | 29 +++++++ server/models/activitypub/actor-follow.ts | 3 +- server/models/video/video-comment.ts | 14 +++ server/models/video/video-share.ts | 13 +++ server/models/video/video.ts | 128 ++-------------------------- 5 files changed, 62 insertions(+), 125 deletions(-) (limited to 'server/models') diff --git a/server/models/account/account-video-rate.ts b/server/models/account/account-video-rate.ts index e969e4a43..508ab814f 100644 --- a/server/models/account/account-video-rate.ts +++ b/server/models/account/account-video-rate.ts @@ -6,6 +6,7 @@ import { VideoRateType } from '../../../shared/models/videos' import { VIDEO_RATE_TYPES } from '../../initializers' import { VideoModel } from '../video/video' import { AccountModel } from './account' +import { ActorModel } from '../activitypub/actor' /* Account rates per video. @@ -66,4 +67,32 @@ export class AccountVideoRateModel extends Model { return AccountVideoRateModel.findOne(options) } + + static listAndCountAccountUrlsByVideoId (rateType: VideoRateType, videoId: number, start: number, count: number, t?: Transaction) { + const query = { + start, + count, + where: { + videoId, + type: rateType + }, + transaction: t, + include: [ + { + attributes: [ 'actorId' ], + model: AccountModel.unscoped(), + required: true, + include: [ + { + attributes: [ 'url' ], + model: ActorModel.unscoped(), + required: true + } + ] + } + ] + } + + return AccountVideoRateModel.findAndCountAll(query) + } } diff --git a/server/models/activitypub/actor-follow.ts b/server/models/activitypub/actor-follow.ts index c97f4cead..b8ce6de1d 100644 --- a/server/models/activitypub/actor-follow.ts +++ b/server/models/activitypub/actor-follow.ts @@ -335,8 +335,7 @@ export class ActorFollowModel extends Model { tasks.push(ActorFollowModel.sequelize.query(query, options)) } - const [ followers, [ { total } ] ] = await - Promise.all(tasks) + const [ followers, [ { total } ] ] = await Promise.all(tasks) const urls: string[] = followers.map(f => f.url) return { 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