aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2017-12-27 16:11:53 +0100
committerChocobozzz <me@florianbigard.com>2017-12-27 16:11:53 +0100
commit4635f59d7c3fea4b97029f10886c62fdf38b2084 (patch)
treed97357a00042bbfb33c4177ee24c01171d28dfce /server/models
parentea44f375f5d3da06ca0aebfe871b9f924a26ec29 (diff)
downloadPeerTube-4635f59d7c3fea4b97029f10886c62fdf38b2084.tar.gz
PeerTube-4635f59d7c3fea4b97029f10886c62fdf38b2084.tar.zst
PeerTube-4635f59d7c3fea4b97029f10886c62fdf38b2084.zip
Add video comment components
Diffstat (limited to 'server/models')
-rw-r--r--server/models/video/video-comment.ts42
1 files changed, 37 insertions, 5 deletions
diff --git a/server/models/video/video-comment.ts b/server/models/video/video-comment.ts
index 25cd6d563..a3e8c48d4 100644
--- a/server/models/video/video-comment.ts
+++ b/server/models/video/video-comment.ts
@@ -8,18 +8,48 @@ import { VideoComment } from '../../../shared/models/videos/video-comment.model'
8import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub' 8import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub'
9import { CONSTRAINTS_FIELDS } from '../../initializers' 9import { CONSTRAINTS_FIELDS } from '../../initializers'
10import { AccountModel } from '../account/account' 10import { AccountModel } from '../account/account'
11import { ActorModel } from '../activitypub/actor'
12import { ServerModel } from '../server/server'
11import { getSort, throwIfNotValid } from '../utils' 13import { getSort, throwIfNotValid } from '../utils'
12import { VideoModel } from './video' 14import { VideoModel } from './video'
13 15
14enum ScopeNames { 16enum ScopeNames {
15 WITH_ACCOUNT = 'WITH_ACCOUNT', 17 WITH_ACCOUNT = 'WITH_ACCOUNT',
16 WITH_IN_REPLY_TO = 'WITH_IN_REPLY_TO' 18 WITH_IN_REPLY_TO = 'WITH_IN_REPLY_TO',
19 ATTRIBUTES_FOR_API = 'ATTRIBUTES_FOR_API'
17} 20}
18 21
19@Scopes({ 22@Scopes({
23 [ScopeNames.ATTRIBUTES_FOR_API]: {
24 attributes: {
25 include: [
26 [
27 Sequelize.literal(
28 '(SELECT COUNT("replies"."id") ' +
29 'FROM "videoComment" AS "replies" ' +
30 'WHERE "replies"."originCommentId" = "VideoCommentModel"."id")'
31 ),
32 'totalReplies'
33 ]
34 ]
35 }
36 },
20 [ScopeNames.WITH_ACCOUNT]: { 37 [ScopeNames.WITH_ACCOUNT]: {
21 include: [ 38 include: [
22 () => AccountModel 39 {
40 model: () => AccountModel,
41 include: [
42 {
43 model: () => ActorModel,
44 include: [
45 {
46 model: () => ServerModel,
47 required: false
48 }
49 ]
50 }
51 ]
52 }
23 ] 53 ]
24 }, 54 },
25 [ScopeNames.WITH_IN_REPLY_TO]: { 55 [ScopeNames.WITH_IN_REPLY_TO]: {
@@ -149,7 +179,7 @@ export class VideoCommentModel extends Model<VideoCommentModel> {
149 } 179 }
150 180
151 return VideoCommentModel 181 return VideoCommentModel
152 .scope([ ScopeNames.WITH_ACCOUNT ]) 182 .scope([ ScopeNames.WITH_ACCOUNT, ScopeNames.ATTRIBUTES_FOR_API ])
153 .findAndCountAll(query) 183 .findAndCountAll(query)
154 .then(({ rows, count }) => { 184 .then(({ rows, count }) => {
155 return { total: count, data: rows } 185 return { total: count, data: rows }
@@ -169,7 +199,7 @@ export class VideoCommentModel extends Model<VideoCommentModel> {
169 } 199 }
170 200
171 return VideoCommentModel 201 return VideoCommentModel
172 .scope([ ScopeNames.WITH_ACCOUNT ]) 202 .scope([ ScopeNames.WITH_ACCOUNT, ScopeNames.ATTRIBUTES_FOR_API ])
173 .findAndCountAll(query) 203 .findAndCountAll(query)
174 .then(({ rows, count }) => { 204 .then(({ rows, count }) => {
175 return { total: count, data: rows } 205 return { total: count, data: rows }
@@ -186,8 +216,10 @@ export class VideoCommentModel extends Model<VideoCommentModel> {
186 videoId: this.videoId, 216 videoId: this.videoId,
187 createdAt: this.createdAt, 217 createdAt: this.createdAt,
188 updatedAt: this.updatedAt, 218 updatedAt: this.updatedAt,
219 totalReplies: this.get('totalReplies') || 0,
189 account: { 220 account: {
190 name: this.Account.name 221 name: this.Account.name,
222 host: this.Account.Actor.getHost()
191 } 223 }
192 } as VideoComment 224 } as VideoComment
193 } 225 }