diff options
author | Chocobozzz <me@florianbigard.com> | 2018-05-25 16:21:16 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2018-05-25 16:21:16 +0200 |
commit | 8fffe21a7bc96d08b229293d66ddba576e609790 (patch) | |
tree | 5ebd5f5198a59084c5338ce197d7e836b39200a4 /server/models/video | |
parent | e251f170b00b2014ac4e823113c6ff40e3fb1471 (diff) | |
download | PeerTube-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/video')
-rw-r--r-- | server/models/video/video-comment.ts | 14 | ||||
-rw-r--r-- | server/models/video/video-share.ts | 13 | ||||
-rw-r--r-- | server/models/video/video.ts | 128 |
3 files changed, 32 insertions, 123 deletions
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' | |||
29 | import { VideoTorrentObject } from '../../../shared/models/activitypub/objects' | 29 | import { VideoTorrentObject } from '../../../shared/models/activitypub/objects' |
30 | import { Video, VideoDetails, VideoFile } from '../../../shared/models/videos' | 30 | import { Video, VideoDetails, VideoFile } from '../../../shared/models/videos' |
31 | import { VideoFilter } from '../../../shared/models/videos/video-query.type' | 31 | import { VideoFilter } from '../../../shared/models/videos/video-query.type' |
32 | import { activityPubCollection } from '../../helpers/activitypub' | 32 | import { activityPubCollectionPagination } from '../../helpers/activitypub' |
33 | import { | 33 | import { |
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 | ||