aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2018-05-25 16:21:16 +0200
committerChocobozzz <me@florianbigard.com>2018-05-25 16:21:16 +0200
commit8fffe21a7bc96d08b229293d66ddba576e609790 (patch)
tree5ebd5f5198a59084c5338ce197d7e836b39200a4 /server/models
parente251f170b00b2014ac4e823113c6ff40e3fb1471 (diff)
downloadPeerTube-8fffe21a7bc96d08b229293d66ddba576e609790.tar.gz
PeerTube-8fffe21a7bc96d08b229293d66ddba576e609790.tar.zst
PeerTube-8fffe21a7bc96d08b229293d66ddba576e609790.zip
Refractor and optimize AP collections
Only display urls in general object, and paginate video comments, shares, likes and dislikes
Diffstat (limited to 'server/models')
-rw-r--r--server/models/account/account-video-rate.ts29
-rw-r--r--server/models/activitypub/actor-follow.ts3
-rw-r--r--server/models/video/video-comment.ts14
-rw-r--r--server/models/video/video-share.ts13
-rw-r--r--server/models/video/video.ts128
5 files changed, 62 insertions, 125 deletions
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'
6import { VIDEO_RATE_TYPES } from '../../initializers' 6import { VIDEO_RATE_TYPES } from '../../initializers'
7import { VideoModel } from '../video/video' 7import { VideoModel } from '../video/video'
8import { AccountModel } from './account' 8import { AccountModel } from './account'
9import { ActorModel } from '../activitypub/actor'
9 10
10/* 11/*
11 Account rates per video. 12 Account rates per video.
@@ -66,4 +67,32 @@ export class AccountVideoRateModel extends Model<AccountVideoRateModel> {
66 67
67 return AccountVideoRateModel.findOne(options) 68 return AccountVideoRateModel.findOne(options)
68 } 69 }
70
71 static listAndCountAccountUrlsByVideoId (rateType: VideoRateType, videoId: number, start: number, count: number, t?: Transaction) {
72 const query = {
73 start,
74 count,
75 where: {
76 videoId,
77 type: rateType
78 },
79 transaction: t,
80 include: [
81 {
82 attributes: [ 'actorId' ],
83 model: AccountModel.unscoped(),
84 required: true,
85 include: [
86 {
87 attributes: [ 'url' ],
88 model: ActorModel.unscoped(),
89 required: true
90 }
91 ]
92 }
93 ]
94 }
95
96 return AccountVideoRateModel.findAndCountAll(query)
97 }
69} 98}
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<ActorFollowModel> {
335 tasks.push(ActorFollowModel.sequelize.query(query, options)) 335 tasks.push(ActorFollowModel.sequelize.query(query, options))
336 } 336 }
337 337
338 const [ followers, [ { total } ] ] = await 338 const [ followers, [ { total } ] ] = await Promise.all(tasks)
339 Promise.all(tasks)
340 const urls: string[] = followers.map(f => f.url) 339 const urls: string[] = followers.map(f => f.url)
341 340
342 return { 341 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<VideoCommentModel> {
326 .findAll(query) 326 .findAll(query)
327 } 327 }
328 328
329 static listAndCountByVideoId (videoId: number, start: number, count: number, t?: Sequelize.Transaction, order: 'ASC' | 'DESC' = 'ASC') {
330 const query = {
331 order: [ [ 'createdAt', order ] ],
332 start,
333 count,
334 where: {
335 videoId
336 },
337 transaction: t
338 }
339
340 return VideoCommentModel.findAndCountAll(query)
341 }
342
329 static async getStats () { 343 static async getStats () {
330 const totalLocalVideoComments = await VideoCommentModel.count({ 344 const totalLocalVideoComments = await VideoCommentModel.count({
331 include: [ 345 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<VideoShareModel> {
187 .findAll(query) 187 .findAll(query)
188 .then(res => res.map(r => r.Actor)) 188 .then(res => res.map(r => r.Actor))
189 } 189 }
190
191 static listAndCountByVideoId (videoId: number, start: number, count: number, t?: Sequelize.Transaction) {
192 const query = {
193 start,
194 count,
195 where: {
196 videoId
197 },
198 transaction: t
199 }
200
201 return VideoShareModel.findAndCountAll(query)
202 }
190} 203}
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'
29import { VideoTorrentObject } from '../../../shared/models/activitypub/objects' 29import { VideoTorrentObject } from '../../../shared/models/activitypub/objects'
30import { Video, VideoDetails, VideoFile } from '../../../shared/models/videos' 30import { Video, VideoDetails, VideoFile } from '../../../shared/models/videos'
31import { VideoFilter } from '../../../shared/models/videos/video-query.type' 31import { VideoFilter } from '../../../shared/models/videos/video-query.type'
32import { activityPubCollection } from '../../helpers/activitypub' 32import { activityPubCollectionPagination } from '../../helpers/activitypub'
33import { 33import {
34 createTorrentPromise, 34 createTorrentPromise,
35 peertubeTruncate, 35 peertubeTruncate,
@@ -602,18 +602,6 @@ export class VideoModel extends Model<VideoModel> {
602 attributes: [ 'id', 'url' ], 602 attributes: [ 'id', 'url' ],
603 model: VideoShareModel.unscoped(), 603 model: VideoShareModel.unscoped(),
604 required: false, 604 required: false,
605 where: {
606 [Sequelize.Op.and]: [
607 {
608 id: {
609 [Sequelize.Op.not]: null
610 }
611 },
612 {
613 actorId
614 }
615 ]
616 },
617 include: [ 605 include: [
618 { 606 {
619 attributes: [ 'id', 'url' ], 607 attributes: [ 'id', 'url' ],
@@ -644,35 +632,6 @@ export class VideoModel extends Model<VideoModel> {
644 } 632 }
645 ] 633 ]
646 }, 634 },
647 {
648 attributes: [ 'type' ],
649 model: AccountVideoRateModel,
650 required: false,
651 include: [
652 {
653 attributes: [ 'id' ],
654 model: AccountModel.unscoped(),
655 include: [
656 {
657 attributes: [ 'url' ],
658 model: ActorModel.unscoped(),
659 include: [
660 {
661 attributes: [ 'host' ],
662 model: ServerModel,
663 required: false
664 }
665 ]
666 }
667 ]
668 }
669 ]
670 },
671 {
672 attributes: [ 'url' ],
673 model: VideoCommentModel,
674 required: false
675 },
676 VideoFileModel, 635 VideoFileModel,
677 TagModel 636 TagModel
678 ] 637 ]
@@ -897,26 +856,6 @@ export class VideoModel extends Model<VideoModel> {
897 .findOne(options) 856 .findOne(options)
898 } 857 }
899 858
900 static loadAndPopulateAll (id: number) {
901 const options = {
902 order: [ [ 'Tags', 'name', 'ASC' ] ],
903 where: {
904 id
905 }
906 }
907
908 return VideoModel
909 .scope([
910 ScopeNames.WITH_RATES,
911 ScopeNames.WITH_SHARES,
912 ScopeNames.WITH_TAGS,
913 ScopeNames.WITH_FILES,
914 ScopeNames.WITH_ACCOUNT_DETAILS,
915 ScopeNames.WITH_COMMENTS
916 ])
917 .findOne(options)
918 }
919
920 static async getStats () { 859 static async getStats () {
921 const totalLocalVideos = await VideoModel.count({ 860 const totalLocalVideos = await VideoModel.count({
922 where: { 861 where: {
@@ -1203,25 +1142,6 @@ export class VideoModel extends Model<VideoModel> {
1203 } 1142 }
1204 } 1143 }
1205 1144
1206 let likesObject
1207 let dislikesObject
1208
1209 if (Array.isArray(this.AccountVideoRates)) {
1210 const res = this.toRatesActivityPubObjects()
1211 likesObject = res.likesObject
1212 dislikesObject = res.dislikesObject
1213 }
1214
1215 let sharesObject
1216 if (Array.isArray(this.VideoShares)) {
1217 sharesObject = this.toAnnouncesActivityPubObject()
1218 }
1219
1220 let commentsObject
1221 if (Array.isArray(this.VideoComments)) {
1222 commentsObject = this.toCommentsActivityPubObject()
1223 }
1224
1225 const url = [] 1145 const url = []
1226 for (const file of this.VideoFiles) { 1146 for (const file of this.VideoFiles) {
1227 url.push({ 1147 url.push({
@@ -1280,10 +1200,10 @@ export class VideoModel extends Model<VideoModel> {
1280 height: THUMBNAILS_SIZE.height 1200 height: THUMBNAILS_SIZE.height
1281 }, 1201 },
1282 url, 1202 url,
1283 likes: likesObject, 1203 likes: getVideoLikesActivityPubUrl(this),
1284 dislikes: dislikesObject, 1204 dislikes: getVideoDislikesActivityPubUrl(this),
1285 shares: sharesObject, 1205 shares: getVideoSharesActivityPubUrl(this),
1286 comments: commentsObject, 1206 comments: getVideoCommentsActivityPubUrl(this),
1287 attributedTo: [ 1207 attributedTo: [
1288 { 1208 {
1289 type: 'Person', 1209 type: 'Person',
@@ -1297,44 +1217,6 @@ export class VideoModel extends Model<VideoModel> {
1297 } 1217 }
1298 } 1218 }
1299 1219
1300 toAnnouncesActivityPubObject () {
1301 const shares: string[] = []
1302
1303 for (const videoShare of this.VideoShares) {
1304 shares.push(videoShare.url)
1305 }
1306
1307 return activityPubCollection(getVideoSharesActivityPubUrl(this), shares)
1308 }
1309
1310 toCommentsActivityPubObject () {
1311 const comments: string[] = []
1312
1313 for (const videoComment of this.VideoComments) {
1314 comments.push(videoComment.url)
1315 }
1316
1317 return activityPubCollection(getVideoCommentsActivityPubUrl(this), comments)
1318 }
1319
1320 toRatesActivityPubObjects () {
1321 const likes: string[] = []
1322 const dislikes: string[] = []
1323
1324 for (const rate of this.AccountVideoRates) {
1325 if (rate.type === 'like') {
1326 likes.push(rate.Account.Actor.url)
1327 } else if (rate.type === 'dislike') {
1328 dislikes.push(rate.Account.Actor.url)
1329 }
1330 }
1331
1332 const likesObject = activityPubCollection(getVideoLikesActivityPubUrl(this), likes)
1333 const dislikesObject = activityPubCollection(getVideoDislikesActivityPubUrl(this), dislikes)
1334
1335 return { likesObject, dislikesObject }
1336 }
1337
1338 getTruncatedDescription () { 1220 getTruncatedDescription () {
1339 if (!this.description) return null 1221 if (!this.description) return null
1340 1222