X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Fvideo%2Fvideo-comment.ts;h=f84c1880c6a564fc11c61959514450e25e121e3c;hb=d9eaee3939bf2e93e5d775d32bce77842201faba;hp=47e3211a3f35304f87a09d04e000d9a7fb79a0e5;hpb=3bb6c52645af84832212c99fdec04143e4230180;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/video/video-comment.ts b/server/models/video/video-comment.ts index 47e3211a3..f84c1880c 100644 --- a/server/models/video/video-comment.ts +++ b/server/models/video/video-comment.ts @@ -108,6 +108,9 @@ enum ScopeNames { { fields: [ 'url' ], unique: true + }, + { + fields: [ 'accountId' ] } ] }) @@ -153,7 +156,7 @@ export class VideoCommentModel extends Model { as: 'InReplyToVideoComment', onDelete: 'CASCADE' }) - InReplyToVideoComment: VideoCommentModel + InReplyToVideoComment: VideoCommentModel | null @ForeignKey(() => VideoModel) @Column @@ -307,15 +310,15 @@ export class VideoCommentModel extends Model { const query = { order: [ [ 'createdAt', order ] ], where: { - [ Sequelize.Op.or ]: [ - { id: comment.getThreadId() }, - { originCommentId: comment.getThreadId() } - ], id: { + [ Sequelize.Op.in ]: Sequelize.literal('(' + + 'WITH RECURSIVE children (id, "inReplyToCommentId") AS ( ' + + 'SELECT id, "inReplyToCommentId" FROM "videoComment" WHERE id = ' + comment.id + ' UNION ' + + 'SELECT p.id, p."inReplyToCommentId" from "videoComment" p ' + + 'INNER JOIN children c ON c."inReplyToCommentId" = p.id) ' + + 'SELECT id FROM children' + + ')'), [ Sequelize.Op.ne ]: comment.id - }, - createdAt: { - [ Sequelize.Op.lt ]: comment.createdAt } }, transaction: t @@ -326,6 +329,68 @@ 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 ] ], + offset: start, + limit: count, + where: { + videoId + }, + transaction: t + } + + return VideoCommentModel.findAndCountAll(query) + } + + static listForFeed (start: number, count: number, videoId?: number) { + const query = { + order: [ [ 'createdAt', 'DESC' ] ], + offset: start, + limit: count, + where: {}, + include: [ + { + attributes: [ 'name', 'uuid' ], + model: VideoModel.unscoped(), + required: true + } + ] + } + + if (videoId) query.where['videoId'] = videoId + + return VideoCommentModel + .scope([ ScopeNames.WITH_ACCOUNT ]) + .findAll(query) + } + + static async getStats () { + const totalLocalVideoComments = await VideoCommentModel.count({ + include: [ + { + model: AccountModel, + required: true, + include: [ + { + model: ActorModel, + required: true, + where: { + serverId: null + } + } + ] + } + ] + }) + const totalVideoComments = await VideoCommentModel.count() + + return { + totalLocalVideoComments, + totalVideoComments + } + } + getThreadId (): number { return this.originCommentId || this.id }