1 import { Model, Sequelize, Transaction } from 'sequelize'
2 import { AbstractRunQuery, ModelBuilder } from '@server/models/shared'
3 import { createSafeIn, getCommentSort, parseRowCountResult } from '@server/models/utils'
4 import { ActorImageType, VideoPrivacy } from '@shared/models'
5 import { VideoCommentTableAttributes } from './video-comment-table-attributes'
7 export interface ListVideoCommentsOptions {
8 selectType: 'api' | 'feed' | 'comment-only'
17 videoChannelId?: number
19 blockerAccountIds?: number[]
24 onLocalVideo?: boolean
25 onPublicVideo?: boolean
26 videoAccountOwnerId?: boolean
29 searchAccount?: string
32 includeReplyCounters?: boolean
34 transaction?: Transaction
37 export class VideoCommentListQueryBuilder extends AbstractRunQuery {
38 private readonly tableAttributes = new VideoCommentTableAttributes()
40 private innerQuery: string
45 private innerSelect = ''
46 private innerJoins = ''
47 private innerWhere = ''
49 private readonly built = {
53 videoChannelJoin: false,
58 protected readonly sequelize: Sequelize,
59 private readonly options: ListVideoCommentsOptions
64 async listComments <T extends Model> () {
67 const results = await this.runQuery({ nest: true, transaction: this.options.transaction })
68 const modelBuilder = new ModelBuilder<T>(this.sequelize)
70 return modelBuilder.createModels(results, 'VideoComment')
73 async countComments () {
74 this.buildCountQuery()
76 const result = await this.runQuery({ transaction: this.options.transaction })
78 return parseRowCountResult(result)
81 // ---------------------------------------------------------------------------
83 private buildListQuery () {
84 this.buildInnerListQuery()
85 this.buildListSelect()
87 this.query = `${this.select} ` +
88 `FROM (${this.innerQuery}) AS "VideoCommentModel" ` +
90 `${this.getOrder()} ` +
94 private buildInnerListQuery () {
96 this.buildInnerListSelect()
98 this.innerQuery = `${this.innerSelect} ` +
99 `FROM "videoComment" AS "VideoCommentModel" ` +
100 `${this.innerJoins} ` +
101 `${this.innerWhere} ` +
102 `${this.getOrder()} ` +
103 `${this.getInnerLimit()}`
106 // ---------------------------------------------------------------------------
108 private buildCountQuery () {
111 this.query = `SELECT COUNT(*) AS "total" ` +
112 `FROM "videoComment" AS "VideoCommentModel" ` +
113 `${this.innerJoins} ` +
117 // ---------------------------------------------------------------------------
119 private buildWhere () {
120 let where: string[] = []
122 if (this.options.videoId) {
123 this.replacements.videoId = this.options.videoId
125 where.push('"VideoCommentModel"."videoId" = :videoId')
128 if (this.options.threadId) {
129 this.replacements.threadId = this.options.threadId
131 where.push('("VideoCommentModel"."id" = :threadId OR "VideoCommentModel"."originCommentId" = :threadId)')
134 if (this.options.accountId) {
135 this.replacements.accountId = this.options.accountId
137 where.push('"VideoCommentModel"."accountId" = :accountId')
140 if (this.options.videoChannelId) {
141 this.buildVideoChannelJoin()
143 this.replacements.videoChannelId = this.options.videoChannelId
145 where.push('"Account->VideoChannel"."id" = :videoChannelId')
148 if (this.options.blockerAccountIds) {
149 this.buildVideoChannelJoin()
151 where = where.concat(this.getBlockWhere('VideoCommentModel', 'Video->VideoChannel'))
154 if (this.options.isThread === true) {
155 where.push('"VideoCommentModel"."inReplyToCommentId" IS NULL')
158 if (this.options.notDeleted === true) {
159 where.push('"VideoCommentModel"."deletedAt" IS NULL')
162 if (this.options.isLocal === true) {
163 this.buildAccountJoin()
165 where.push('"Account->Actor"."serverId" IS NULL')
166 } else if (this.options.isLocal === false) {
167 this.buildAccountJoin()
169 where.push('"Account->Actor"."serverId" IS NOT NULL')
172 if (this.options.onLocalVideo === true) {
173 this.buildVideoJoin()
175 where.push('"Video"."remote" IS FALSE')
176 } else if (this.options.onLocalVideo === false) {
177 this.buildVideoJoin()
179 where.push('"Video"."remote" IS TRUE')
182 if (this.options.onPublicVideo === true) {
183 this.buildVideoJoin()
185 where.push(`"Video"."privacy" = ${VideoPrivacy.PUBLIC}`)
188 if (this.options.videoAccountOwnerId) {
189 this.buildVideoChannelJoin()
191 this.replacements.videoAccountOwnerId = this.options.videoAccountOwnerId
193 where.push(`"Video->VideoChannel"."accountId" = :videoAccountOwnerId`)
196 if (this.options.search) {
197 this.buildVideoJoin()
198 this.buildAccountJoin()
200 const escapedLikeSearch = this.sequelize.escape('%' + this.options.search + '%')
204 `"VideoCommentModel"."text" ILIKE ${escapedLikeSearch} OR ` +
205 `"Account->Actor"."preferredUsername" ILIKE ${escapedLikeSearch} OR ` +
206 `"Account"."name" ILIKE ${escapedLikeSearch} OR ` +
207 `"Video"."name" ILIKE ${escapedLikeSearch} ` +
212 if (this.options.searchAccount) {
213 this.buildAccountJoin()
215 const escapedLikeSearch = this.sequelize.escape('%' + this.options.searchAccount + '%')
219 `"Account->Actor"."preferredUsername" ILIKE ${escapedLikeSearch} OR ` +
220 `"Account"."name" ILIKE ${escapedLikeSearch} ` +
225 if (this.options.searchVideo) {
226 this.buildVideoJoin()
228 const escapedLikeSearch = this.sequelize.escape('%' + this.options.searchVideo + '%')
230 where.push(`"Video"."name" ILIKE ${escapedLikeSearch}`)
233 if (where.length !== 0) {
234 this.innerWhere = `WHERE ${where.join(' AND ')}`
238 private buildAccountJoin () {
239 if (this.built.accountJoin) return
241 this.innerJoins += ' LEFT JOIN "account" "Account" ON "Account"."id" = "VideoCommentModel"."accountId" ' +
242 'LEFT JOIN "actor" "Account->Actor" ON "Account->Actor"."id" = "Account"."actorId" ' +
243 'LEFT JOIN "server" "Account->Actor->Server" ON "Account->Actor"."serverId" = "Account->Actor->Server"."id" '
245 this.built.accountJoin = true
248 private buildVideoJoin () {
249 if (this.built.videoJoin) return
251 this.innerJoins += ' LEFT JOIN "video" "Video" ON "Video"."id" = "VideoCommentModel"."videoId" '
253 this.built.videoJoin = true
256 private buildVideoChannelJoin () {
257 if (this.built.videoChannelJoin) return
259 this.buildVideoJoin()
261 this.innerJoins += ' LEFT JOIN "videoChannel" "Video->VideoChannel" ON "Video"."channelId" = "Video->VideoChannel"."id" '
263 this.built.videoChannelJoin = true
266 private buildAvatarsJoin () {
267 if (this.options.selectType !== 'api' && this.options.selectType !== 'feed') return ''
268 if (this.built.avatarJoin) return
270 this.joins += `LEFT JOIN "actorImage" "Account->Actor->Avatars" ` +
271 `ON "VideoCommentModel"."Account.Actor.id" = "Account->Actor->Avatars"."actorId" ` +
272 `AND "Account->Actor->Avatars"."type" = ${ActorImageType.AVATAR}`
274 this.built.avatarJoin = true
277 // ---------------------------------------------------------------------------
279 private buildListSelect () {
280 const toSelect = [ '"VideoCommentModel".*' ]
282 if (this.options.selectType === 'api' || this.options.selectType === 'feed') {
283 this.buildAvatarsJoin()
285 toSelect.push(this.tableAttributes.getAvatarAttributes())
288 if (this.options.includeReplyCounters === true) {
289 toSelect.push(this.getTotalRepliesSelect())
290 toSelect.push(this.getAuthorTotalRepliesSelect())
293 this.select = this.buildSelect(toSelect)
296 private buildInnerListSelect () {
297 let toSelect = [ this.tableAttributes.getVideoCommentAttributes() ]
299 if (this.options.selectType === 'api' || this.options.selectType === 'feed') {
300 this.buildAccountJoin()
301 this.buildVideoJoin()
303 toSelect = toSelect.concat([
304 this.tableAttributes.getVideoAttributes(),
305 this.tableAttributes.getAccountAttributes(),
306 this.tableAttributes.getActorAttributes(),
307 this.tableAttributes.getServerAttributes()
311 this.innerSelect = this.buildSelect(toSelect)
314 // ---------------------------------------------------------------------------
316 private getBlockWhere (commentTableName: string, channelTableName: string) {
317 const where: string[] = []
319 const blockerIdsString = createSafeIn(
321 this.options.blockerAccountIds,
322 [ `"${channelTableName}"."accountId"` ]
327 `SELECT 1 FROM "accountBlocklist" ` +
328 `WHERE "targetAccountId" = "${commentTableName}"."accountId" ` +
329 `AND "accountId" IN (${blockerIdsString})` +
335 `SELECT 1 FROM "account" ` +
336 `INNER JOIN "actor" ON account."actorId" = actor.id ` +
337 `INNER JOIN "serverBlocklist" ON "actor"."serverId" = "serverBlocklist"."targetServerId" ` +
338 `WHERE "account"."id" = "${commentTableName}"."accountId" ` +
339 `AND "serverBlocklist"."accountId" IN (${blockerIdsString})` +
346 // ---------------------------------------------------------------------------
348 private getTotalRepliesSelect () {
349 const blockWhereString = this.getBlockWhere('replies', 'videoChannel').join(' AND ')
352 `SELECT COUNT("replies"."id") FROM "videoComment" AS "replies" ` +
353 `LEFT JOIN "video" ON "video"."id" = "replies"."videoId" ` +
354 `LEFT JOIN "videoChannel" ON "video"."channelId" = "videoChannel"."id" ` +
355 `WHERE "replies"."originCommentId" = "VideoCommentModel"."id" ` +
356 `AND "deletedAt" IS NULL ` +
357 `AND ${blockWhereString} ` +
358 `) AS "totalReplies"`
361 private getAuthorTotalRepliesSelect () {
363 `SELECT COUNT("replies"."id") FROM "videoComment" AS "replies" ` +
364 `INNER JOIN "video" ON "video"."id" = "replies"."videoId" ` +
365 `INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ` +
366 `WHERE "replies"."originCommentId" = "VideoCommentModel"."id" AND "replies"."accountId" = "videoChannel"."accountId"` +
367 `) AS "totalRepliesFromVideoAuthor"`
370 private getOrder () {
371 if (!this.options.sort) return ''
373 const orders = getCommentSort(this.options.sort)
375 return 'ORDER BY ' + orders.map(o => `"${o[0]}" ${o[1]}`).join(', ')
378 private getLimit () {
379 if (!this.options.count) return ''
381 this.replacements.limit = this.options.count
383 return `LIMIT :limit `
386 private getInnerLimit () {
387 if (!this.options.count) return ''
389 this.replacements.limit = this.options.count
390 this.replacements.offset = this.options.start || 0
392 return `LIMIT :limit OFFSET :offset `