diff options
author | Chocobozzz <me@florianbigard.com> | 2017-12-22 10:50:07 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2017-12-22 11:29:12 +0100 |
commit | bf1f650817dadfd5eeee9e5e0b6b6938c136e25d (patch) | |
tree | 0f1dc95d87089be177ebe60740a55dd0c96b2414 /server/models/video | |
parent | 6d8524702874120a4667269a81a61e3c7c5e300d (diff) | |
download | PeerTube-bf1f650817dadfd5eeee9e5e0b6b6938c136e25d.tar.gz PeerTube-bf1f650817dadfd5eeee9e5e0b6b6938c136e25d.tar.zst PeerTube-bf1f650817dadfd5eeee9e5e0b6b6938c136e25d.zip |
Add comments controller
Diffstat (limited to 'server/models/video')
-rw-r--r-- | server/models/video/video-comment.ts | 88 |
1 files changed, 86 insertions, 2 deletions
diff --git a/server/models/video/video-comment.ts b/server/models/video/video-comment.ts index 92c0c6112..d66f933ee 100644 --- a/server/models/video/video-comment.ts +++ b/server/models/video/video-comment.ts | |||
@@ -1,19 +1,34 @@ | |||
1 | import * as Sequelize from 'sequelize' | 1 | import * as Sequelize from 'sequelize' |
2 | import { | 2 | import { |
3 | AllowNull, BelongsTo, Column, CreatedAt, DataType, Default, ForeignKey, IFindOptions, Is, IsUUID, Model, Table, | 3 | AfterDestroy, AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, IFindOptions, Is, Model, Scopes, Table, |
4 | UpdatedAt | 4 | UpdatedAt |
5 | } from 'sequelize-typescript' | 5 | } from 'sequelize-typescript' |
6 | import { VideoComment } from '../../../shared/models/videos/video-comment.model' | ||
6 | import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub' | 7 | import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub' |
7 | import { CONSTRAINTS_FIELDS } from '../../initializers' | 8 | import { CONSTRAINTS_FIELDS } from '../../initializers' |
8 | import { ActorModel } from '../activitypub/actor' | 9 | import { ActorModel } from '../activitypub/actor' |
9 | import { throwIfNotValid } from '../utils' | 10 | import { getSort, throwIfNotValid } from '../utils' |
10 | import { VideoModel } from './video' | 11 | import { VideoModel } from './video' |
11 | 12 | ||
13 | enum ScopeNames { | ||
14 | WITH_ACTOR = 'WITH_ACTOR' | ||
15 | } | ||
16 | |||
17 | @Scopes({ | ||
18 | [ScopeNames.WITH_ACTOR]: { | ||
19 | include: [ | ||
20 | () => ActorModel | ||
21 | ] | ||
22 | } | ||
23 | }) | ||
12 | @Table({ | 24 | @Table({ |
13 | tableName: 'videoComment', | 25 | tableName: 'videoComment', |
14 | indexes: [ | 26 | indexes: [ |
15 | { | 27 | { |
16 | fields: [ 'videoId' ] | 28 | fields: [ 'videoId' ] |
29 | }, | ||
30 | { | ||
31 | fields: [ 'videoId', 'originCommentId' ] | ||
17 | } | 32 | } |
18 | ] | 33 | ] |
19 | }) | 34 | }) |
@@ -81,6 +96,24 @@ export class VideoCommentModel extends Model<VideoCommentModel> { | |||
81 | }) | 96 | }) |
82 | Actor: ActorModel | 97 | Actor: ActorModel |
83 | 98 | ||
99 | @AfterDestroy | ||
100 | static sendDeleteIfOwned (instance: VideoCommentModel) { | ||
101 | // TODO | ||
102 | return undefined | ||
103 | } | ||
104 | |||
105 | static loadById (id: number, t?: Sequelize.Transaction) { | ||
106 | const query: IFindOptions<VideoCommentModel> = { | ||
107 | where: { | ||
108 | id | ||
109 | } | ||
110 | } | ||
111 | |||
112 | if (t !== undefined) query.transaction = t | ||
113 | |||
114 | return VideoCommentModel.findOne(query) | ||
115 | } | ||
116 | |||
84 | static loadByUrl (url: string, t?: Sequelize.Transaction) { | 117 | static loadByUrl (url: string, t?: Sequelize.Transaction) { |
85 | const query: IFindOptions<VideoCommentModel> = { | 118 | const query: IFindOptions<VideoCommentModel> = { |
86 | where: { | 119 | where: { |
@@ -92,4 +125,55 @@ export class VideoCommentModel extends Model<VideoCommentModel> { | |||
92 | 125 | ||
93 | return VideoCommentModel.findOne(query) | 126 | return VideoCommentModel.findOne(query) |
94 | } | 127 | } |
128 | |||
129 | static listThreadsForApi (videoId: number, start: number, count: number, sort: string) { | ||
130 | const query = { | ||
131 | offset: start, | ||
132 | limit: count, | ||
133 | order: [ getSort(sort) ], | ||
134 | where: { | ||
135 | videoId | ||
136 | } | ||
137 | } | ||
138 | |||
139 | return VideoCommentModel | ||
140 | .scope([ ScopeNames.WITH_ACTOR ]) | ||
141 | .findAndCountAll(query) | ||
142 | .then(({ rows, count }) => { | ||
143 | return { total: count, data: rows } | ||
144 | }) | ||
145 | } | ||
146 | |||
147 | static listThreadCommentsForApi (videoId: number, threadId: number) { | ||
148 | const query = { | ||
149 | order: [ 'id', 'ASC' ], | ||
150 | where: { | ||
151 | videoId, | ||
152 | [ Sequelize.Op.or ]: [ | ||
153 | { id: threadId }, | ||
154 | { originCommentId: threadId } | ||
155 | ] | ||
156 | } | ||
157 | } | ||
158 | |||
159 | return VideoCommentModel | ||
160 | .scope([ ScopeNames.WITH_ACTOR ]) | ||
161 | .findAndCountAll(query) | ||
162 | .then(({ rows, count }) => { | ||
163 | return { total: count, data: rows } | ||
164 | }) | ||
165 | } | ||
166 | |||
167 | toFormattedJSON () { | ||
168 | return { | ||
169 | id: this.id, | ||
170 | url: this.url, | ||
171 | text: this.text, | ||
172 | threadId: this.originCommentId || this.id, | ||
173 | inReplyToCommentId: this.inReplyToCommentId, | ||
174 | videoId: this.videoId, | ||
175 | createdAt: this.createdAt, | ||
176 | updatedAt: this.updatedAt | ||
177 | } as VideoComment | ||
178 | } | ||
95 | } | 179 | } |