aboutsummaryrefslogtreecommitdiffhomepage
path: root/server
diff options
context:
space:
mode:
Diffstat (limited to 'server')
-rw-r--r--server/controllers/api/videos/comment.ts36
-rw-r--r--server/initializers/constants.ts3
-rw-r--r--server/middlewares/validators/sort.ts3
-rw-r--r--server/middlewares/validators/videos/video-comments.ts32
-rw-r--r--server/models/video/video-comment.ts132
-rw-r--r--server/types/models/video/video-comment.ts9
6 files changed, 206 insertions, 9 deletions
diff --git a/server/controllers/api/videos/comment.ts b/server/controllers/api/videos/comment.ts
index 45ff969d9..ccd76c093 100644
--- a/server/controllers/api/videos/comment.ts
+++ b/server/controllers/api/videos/comment.ts
@@ -1,5 +1,5 @@
1import * as express from 'express' 1import * as express from 'express'
2import { ResultList } from '../../../../shared/models' 2import { ResultList, UserRight } from '../../../../shared/models'
3import { VideoCommentCreate } from '../../../../shared/models/videos/video-comment.model' 3import { VideoCommentCreate } from '../../../../shared/models/videos/video-comment.model'
4import { auditLoggerFactory, CommentAuditView, getAuditIdFromRes } from '../../../helpers/audit-logger' 4import { auditLoggerFactory, CommentAuditView, getAuditIdFromRes } from '../../../helpers/audit-logger'
5import { getFormattedObjects } from '../../../helpers/utils' 5import { getFormattedObjects } from '../../../helpers/utils'
@@ -11,6 +11,7 @@ import {
11 asyncMiddleware, 11 asyncMiddleware,
12 asyncRetryTransactionMiddleware, 12 asyncRetryTransactionMiddleware,
13 authenticate, 13 authenticate,
14 ensureUserHasRight,
14 optionalAuthenticate, 15 optionalAuthenticate,
15 paginationValidator, 16 paginationValidator,
16 setDefaultPagination, 17 setDefaultPagination,
@@ -19,9 +20,11 @@ import {
19import { 20import {
20 addVideoCommentReplyValidator, 21 addVideoCommentReplyValidator,
21 addVideoCommentThreadValidator, 22 addVideoCommentThreadValidator,
23 listVideoCommentsValidator,
22 listVideoCommentThreadsValidator, 24 listVideoCommentThreadsValidator,
23 listVideoThreadCommentsValidator, 25 listVideoThreadCommentsValidator,
24 removeVideoCommentValidator, 26 removeVideoCommentValidator,
27 videoCommentsValidator,
25 videoCommentThreadsSortValidator 28 videoCommentThreadsSortValidator
26} from '../../../middlewares/validators' 29} from '../../../middlewares/validators'
27import { AccountModel } from '../../../models/account/account' 30import { AccountModel } from '../../../models/account/account'
@@ -61,6 +64,17 @@ videoCommentRouter.delete('/:videoId/comments/:commentId',
61 asyncRetryTransactionMiddleware(removeVideoComment) 64 asyncRetryTransactionMiddleware(removeVideoComment)
62) 65)
63 66
67videoCommentRouter.get('/comments',
68 authenticate,
69 ensureUserHasRight(UserRight.SEE_ALL_COMMENTS),
70 paginationValidator,
71 videoCommentsValidator,
72 setDefaultSort,
73 setDefaultPagination,
74 listVideoCommentsValidator,
75 asyncMiddleware(listComments)
76)
77
64// --------------------------------------------------------------------------- 78// ---------------------------------------------------------------------------
65 79
66export { 80export {
@@ -69,6 +83,26 @@ export {
69 83
70// --------------------------------------------------------------------------- 84// ---------------------------------------------------------------------------
71 85
86async function listComments (req: express.Request, res: express.Response) {
87 const options = {
88 start: req.query.start,
89 count: req.query.count,
90 sort: req.query.sort,
91
92 isLocal: req.query.isLocal,
93 search: req.query.search,
94 searchAccount: req.query.searchAccount,
95 searchVideo: req.query.searchVideo
96 }
97
98 const resultList = await VideoCommentModel.listCommentsForApi(options)
99
100 return res.json({
101 total: resultList.total,
102 data: resultList.data.map(c => c.toFormattedAdminJSON())
103 })
104}
105
72async function listVideoThreads (req: express.Request, res: express.Response) { 106async function listVideoThreads (req: express.Request, res: express.Response) {
73 const video = res.locals.onlyVideo 107 const video = res.locals.onlyVideo
74 const user = res.locals.oauth ? res.locals.oauth.token.User : undefined 108 const user = res.locals.oauth ? res.locals.oauth.token.User : undefined
diff --git a/server/initializers/constants.ts b/server/initializers/constants.ts
index 02e42a594..fde87d9f8 100644
--- a/server/initializers/constants.ts
+++ b/server/initializers/constants.ts
@@ -63,7 +63,10 @@ const SORTABLE_COLUMNS = {
63 JOBS: [ 'createdAt' ], 63 JOBS: [ 'createdAt' ],
64 VIDEO_CHANNELS: [ 'id', 'name', 'updatedAt', 'createdAt' ], 64 VIDEO_CHANNELS: [ 'id', 'name', 'updatedAt', 'createdAt' ],
65 VIDEO_IMPORTS: [ 'createdAt' ], 65 VIDEO_IMPORTS: [ 'createdAt' ],
66
66 VIDEO_COMMENT_THREADS: [ 'createdAt', 'totalReplies' ], 67 VIDEO_COMMENT_THREADS: [ 'createdAt', 'totalReplies' ],
68 VIDEO_COMMENTS: [ 'createdAt' ],
69
67 VIDEO_RATES: [ 'createdAt' ], 70 VIDEO_RATES: [ 'createdAt' ],
68 BLACKLISTS: [ 'id', 'name', 'duration', 'views', 'likes', 'dislikes', 'uuid', 'createdAt' ], 71 BLACKLISTS: [ 'id', 'name', 'duration', 'views', 'likes', 'dislikes', 'uuid', 'createdAt' ],
69 FOLLOWERS: [ 'createdAt', 'state', 'score' ], 72 FOLLOWERS: [ 'createdAt', 'state', 'score' ],
diff --git a/server/middlewares/validators/sort.ts b/server/middlewares/validators/sort.ts
index 29aba0436..e93ceb200 100644
--- a/server/middlewares/validators/sort.ts
+++ b/server/middlewares/validators/sort.ts
@@ -10,6 +10,7 @@ const SORTABLE_VIDEOS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEOS)
10const SORTABLE_VIDEOS_SEARCH_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEOS_SEARCH) 10const SORTABLE_VIDEOS_SEARCH_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEOS_SEARCH)
11const SORTABLE_VIDEO_CHANNELS_SEARCH_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEO_CHANNELS_SEARCH) 11const SORTABLE_VIDEO_CHANNELS_SEARCH_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEO_CHANNELS_SEARCH)
12const SORTABLE_VIDEO_IMPORTS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEO_IMPORTS) 12const SORTABLE_VIDEO_IMPORTS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEO_IMPORTS)
13const SORTABLE_VIDEO_COMMENTS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEO_COMMENT_THREADS)
13const SORTABLE_VIDEO_COMMENT_THREADS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEO_COMMENT_THREADS) 14const SORTABLE_VIDEO_COMMENT_THREADS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEO_COMMENT_THREADS)
14const SORTABLE_VIDEO_RATES_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEO_RATES) 15const SORTABLE_VIDEO_RATES_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEO_RATES)
15const SORTABLE_BLACKLISTS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.BLACKLISTS) 16const SORTABLE_BLACKLISTS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.BLACKLISTS)
@@ -33,6 +34,7 @@ const videosSortValidator = checkSort(SORTABLE_VIDEOS_COLUMNS)
33const videoImportsSortValidator = checkSort(SORTABLE_VIDEO_IMPORTS_COLUMNS) 34const videoImportsSortValidator = checkSort(SORTABLE_VIDEO_IMPORTS_COLUMNS)
34const videosSearchSortValidator = checkSort(SORTABLE_VIDEOS_SEARCH_COLUMNS) 35const videosSearchSortValidator = checkSort(SORTABLE_VIDEOS_SEARCH_COLUMNS)
35const videoChannelsSearchSortValidator = checkSort(SORTABLE_VIDEO_CHANNELS_SEARCH_COLUMNS) 36const videoChannelsSearchSortValidator = checkSort(SORTABLE_VIDEO_CHANNELS_SEARCH_COLUMNS)
37const videoCommentsValidator = checkSort(SORTABLE_VIDEO_COMMENTS_COLUMNS)
36const videoCommentThreadsSortValidator = checkSort(SORTABLE_VIDEO_COMMENT_THREADS_COLUMNS) 38const videoCommentThreadsSortValidator = checkSort(SORTABLE_VIDEO_COMMENT_THREADS_COLUMNS)
37const videoRatesSortValidator = checkSort(SORTABLE_VIDEO_RATES_COLUMNS) 39const videoRatesSortValidator = checkSort(SORTABLE_VIDEO_RATES_COLUMNS)
38const blacklistSortValidator = checkSort(SORTABLE_BLACKLISTS_COLUMNS) 40const blacklistSortValidator = checkSort(SORTABLE_BLACKLISTS_COLUMNS)
@@ -55,6 +57,7 @@ export {
55 abusesSortValidator, 57 abusesSortValidator,
56 videoChannelsSortValidator, 58 videoChannelsSortValidator,
57 videoImportsSortValidator, 59 videoImportsSortValidator,
60 videoCommentsValidator,
58 videosSearchSortValidator, 61 videosSearchSortValidator,
59 videosSortValidator, 62 videosSortValidator,
60 blacklistSortValidator, 63 blacklistSortValidator,
diff --git a/server/middlewares/validators/videos/video-comments.ts b/server/middlewares/validators/videos/video-comments.ts
index 77f5c6ff3..55fb60b98 100644
--- a/server/middlewares/validators/videos/video-comments.ts
+++ b/server/middlewares/validators/videos/video-comments.ts
@@ -1,8 +1,8 @@
1import * as express from 'express' 1import * as express from 'express'
2import { body, param } from 'express-validator' 2import { body, param, query } from 'express-validator'
3import { MUserAccountUrl } from '@server/types/models' 3import { MUserAccountUrl } from '@server/types/models'
4import { UserRight } from '../../../../shared' 4import { UserRight } from '../../../../shared'
5import { isIdOrUUIDValid, isIdValid } from '../../../helpers/custom-validators/misc' 5import { exists, isBooleanValid, isIdOrUUIDValid, isIdValid } from '../../../helpers/custom-validators/misc'
6import { 6import {
7 doesVideoCommentExist, 7 doesVideoCommentExist,
8 doesVideoCommentThreadExist, 8 doesVideoCommentThreadExist,
@@ -15,6 +15,33 @@ import { Hooks } from '../../../lib/plugins/hooks'
15import { MCommentOwnerVideoReply, MVideo, MVideoFullLight } from '../../../types/models/video' 15import { MCommentOwnerVideoReply, MVideo, MVideoFullLight } from '../../../types/models/video'
16import { areValidationErrors } from '../utils' 16import { areValidationErrors } from '../utils'
17 17
18const listVideoCommentsValidator = [
19 query('isLocal')
20 .optional()
21 .custom(isBooleanValid)
22 .withMessage('Should have a valid is local boolean'),
23
24 query('search')
25 .optional()
26 .custom(exists).withMessage('Should have a valid search'),
27
28 query('searchAccount')
29 .optional()
30 .custom(exists).withMessage('Should have a valid account search'),
31
32 query('searchVideo')
33 .optional()
34 .custom(exists).withMessage('Should have a valid video search'),
35
36 (req: express.Request, res: express.Response, next: express.NextFunction) => {
37 logger.debug('Checking listVideoCommentsValidator parameters.', { parameters: req.query })
38
39 if (areValidationErrors(req, res)) return
40
41 return next()
42 }
43]
44
18const listVideoCommentThreadsValidator = [ 45const listVideoCommentThreadsValidator = [
19 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'), 46 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
20 47
@@ -116,6 +143,7 @@ export {
116 listVideoCommentThreadsValidator, 143 listVideoCommentThreadsValidator,
117 listVideoThreadCommentsValidator, 144 listVideoThreadCommentsValidator,
118 addVideoCommentThreadValidator, 145 addVideoCommentThreadValidator,
146 listVideoCommentsValidator,
119 addVideoCommentReplyValidator, 147 addVideoCommentReplyValidator,
120 videoCommentGetValidator, 148 videoCommentGetValidator,
121 removeVideoCommentValidator 149 removeVideoCommentValidator
diff --git a/server/models/video/video-comment.ts b/server/models/video/video-comment.ts
index de27b3d87..70aed75d6 100644
--- a/server/models/video/video-comment.ts
+++ b/server/models/video/video-comment.ts
@@ -1,6 +1,6 @@
1import * as Bluebird from 'bluebird' 1import * as Bluebird from 'bluebird'
2import { uniq } from 'lodash' 2import { uniq } from 'lodash'
3import { FindOptions, Op, Order, ScopeOptions, Sequelize, Transaction, WhereOptions } from 'sequelize' 3import { FindAndCountOptions, FindOptions, Op, Order, ScopeOptions, Sequelize, Transaction, WhereOptions } from 'sequelize'
4import { 4import {
5 AllowNull, 5 AllowNull,
6 BelongsTo, 6 BelongsTo,
@@ -20,13 +20,14 @@ import { MAccount, MAccountId, MUserAccountId } from '@server/types/models'
20import { VideoPrivacy } from '@shared/models' 20import { VideoPrivacy } from '@shared/models'
21import { ActivityTagObject, ActivityTombstoneObject } from '../../../shared/models/activitypub/objects/common-objects' 21import { ActivityTagObject, ActivityTombstoneObject } from '../../../shared/models/activitypub/objects/common-objects'
22import { VideoCommentObject } from '../../../shared/models/activitypub/objects/video-comment-object' 22import { VideoCommentObject } from '../../../shared/models/activitypub/objects/video-comment-object'
23import { VideoComment } from '../../../shared/models/videos/video-comment.model' 23import { VideoComment, VideoCommentAdmin } from '../../../shared/models/videos/video-comment.model'
24import { actorNameAlphabet } from '../../helpers/custom-validators/activitypub/actor' 24import { actorNameAlphabet } from '../../helpers/custom-validators/activitypub/actor'
25import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc' 25import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
26import { regexpCapture } from '../../helpers/regexp' 26import { regexpCapture } from '../../helpers/regexp'
27import { CONSTRAINTS_FIELDS, WEBSERVER } from '../../initializers/constants' 27import { CONSTRAINTS_FIELDS, WEBSERVER } from '../../initializers/constants'
28import { 28import {
29 MComment, 29 MComment,
30 MCommentAdminFormattable,
30 MCommentAP, 31 MCommentAP,
31 MCommentFormattable, 32 MCommentFormattable,
32 MCommentId, 33 MCommentId,
@@ -40,7 +41,14 @@ import {
40import { VideoCommentAbuseModel } from '../abuse/video-comment-abuse' 41import { VideoCommentAbuseModel } from '../abuse/video-comment-abuse'
41import { AccountModel } from '../account/account' 42import { AccountModel } from '../account/account'
42import { ActorModel, unusedActorAttributesForAPI } from '../activitypub/actor' 43import { ActorModel, unusedActorAttributesForAPI } from '../activitypub/actor'
43import { buildBlockedAccountSQL, buildBlockedAccountSQLOptimized, buildLocalAccountIdsIn, getCommentSort, throwIfNotValid } from '../utils' 44import {
45 buildBlockedAccountSQL,
46 buildBlockedAccountSQLOptimized,
47 buildLocalAccountIdsIn,
48 getCommentSort,
49 searchAttribute,
50 throwIfNotValid
51} from '../utils'
44import { VideoModel } from './video' 52import { VideoModel } from './video'
45import { VideoChannelModel } from './video-channel' 53import { VideoChannelModel } from './video-channel'
46 54
@@ -303,6 +311,90 @@ export class VideoCommentModel extends Model<VideoCommentModel> {
303 return VideoCommentModel.scope([ ScopeNames.WITH_IN_REPLY_TO, ScopeNames.WITH_ACCOUNT ]).findOne(query) 311 return VideoCommentModel.scope([ ScopeNames.WITH_IN_REPLY_TO, ScopeNames.WITH_ACCOUNT ]).findOne(query)
304 } 312 }
305 313
314 static listCommentsForApi (parameters: {
315 start: number
316 count: number
317 sort: string
318
319 isLocal?: boolean
320 search?: string
321 searchAccount?: string
322 searchVideo?: string
323 }) {
324 const { start, count, sort, isLocal, search, searchAccount, searchVideo } = parameters
325
326 const query: FindAndCountOptions = {
327 offset: start,
328 limit: count,
329 order: getCommentSort(sort)
330 }
331
332 const where: WhereOptions = {
333 isDeleted: false
334 }
335
336 const whereAccount: WhereOptions = {}
337 const whereActor: WhereOptions = {}
338 const whereVideo: WhereOptions = {}
339
340 if (isLocal === true) {
341 Object.assign(where, {
342 serverId: null
343 })
344 } else if (isLocal === false) {
345 Object.assign(where, {
346 serverId: {
347 [Op.ne]: null
348 }
349 })
350 }
351
352 if (search) {
353 Object.assign(where, searchAttribute(search, 'text'))
354 Object.assign(whereActor, searchAttribute(search, 'preferredUsername'))
355 Object.assign(whereAccount, searchAttribute(search, 'name'))
356 Object.assign(whereVideo, searchAttribute(search, 'name'))
357 }
358
359 if (searchAccount) {
360 Object.assign(whereActor, searchAttribute(search, 'preferredUsername'))
361 Object.assign(whereAccount, searchAttribute(search, 'name'))
362 }
363
364 if (searchVideo) {
365 Object.assign(whereVideo, searchAttribute(search, 'name'))
366 }
367
368 query.include = [
369 {
370 model: AccountModel.unscoped(),
371 required: !!searchAccount,
372 where: whereAccount,
373 include: [
374 {
375 attributes: {
376 exclude: unusedActorAttributesForAPI
377 },
378 model: ActorModel, // Default scope includes avatar and server
379 required: true,
380 where: whereActor
381 }
382 ]
383 },
384 {
385 model: VideoModel.unscoped(),
386 required: true,
387 where: whereVideo
388 }
389 ]
390
391 return VideoCommentModel
392 .findAndCountAll(query)
393 .then(({ rows, count }) => {
394 return { total: count, data: rows }
395 })
396 }
397
306 static async listThreadsForApi (parameters: { 398 static async listThreadsForApi (parameters: {
307 videoId: number 399 videoId: number
308 isVideoOwned: boolean 400 isVideoOwned: boolean
@@ -656,19 +748,51 @@ export class VideoCommentModel extends Model<VideoCommentModel> {
656 id: this.id, 748 id: this.id,
657 url: this.url, 749 url: this.url,
658 text: this.text, 750 text: this.text,
751
659 threadId: this.getThreadId(), 752 threadId: this.getThreadId(),
660 inReplyToCommentId: this.inReplyToCommentId || null, 753 inReplyToCommentId: this.inReplyToCommentId || null,
661 videoId: this.videoId, 754 videoId: this.videoId,
755
662 createdAt: this.createdAt, 756 createdAt: this.createdAt,
663 updatedAt: this.updatedAt, 757 updatedAt: this.updatedAt,
664 deletedAt: this.deletedAt, 758 deletedAt: this.deletedAt,
759
665 isDeleted: this.isDeleted(), 760 isDeleted: this.isDeleted(),
761
666 totalRepliesFromVideoAuthor: this.get('totalRepliesFromVideoAuthor') || 0, 762 totalRepliesFromVideoAuthor: this.get('totalRepliesFromVideoAuthor') || 0,
667 totalReplies: this.get('totalReplies') || 0, 763 totalReplies: this.get('totalReplies') || 0,
668 account: this.Account ? this.Account.toFormattedJSON() : null 764
765 account: this.Account
766 ? this.Account.toFormattedJSON()
767 : null
669 } as VideoComment 768 } as VideoComment
670 } 769 }
671 770
771 toFormattedAdminJSON (this: MCommentAdminFormattable) {
772 return {
773 id: this.id,
774 url: this.url,
775 text: this.text,
776
777 threadId: this.getThreadId(),
778 inReplyToCommentId: this.inReplyToCommentId || null,
779 videoId: this.videoId,
780
781 createdAt: this.createdAt,
782 updatedAt: this.updatedAt,
783
784 video: {
785 id: this.Video.id,
786 uuid: this.Video.uuid,
787 name: this.Video.name
788 },
789
790 account: this.Account
791 ? this.Account.toFormattedJSON()
792 : null
793 } as VideoCommentAdmin
794 }
795
672 toActivityPubObject (this: MCommentAP, threadParentComments: MCommentOwner[]): VideoCommentObject | ActivityTombstoneObject { 796 toActivityPubObject (this: MCommentAP, threadParentComments: MCommentOwner[]): VideoCommentObject | ActivityTombstoneObject {
673 let inReplyTo: string 797 let inReplyTo: string
674 // New thread, so in AS we reply to the video 798 // New thread, so in AS we reply to the video
diff --git a/server/types/models/video/video-comment.ts b/server/types/models/video/video-comment.ts
index f1c50c753..83479e7b2 100644
--- a/server/types/models/video/video-comment.ts
+++ b/server/types/models/video/video-comment.ts
@@ -1,7 +1,7 @@
1import { VideoCommentModel } from '../../../models/video/video-comment'
2import { PickWith, PickWithOpt } from '@shared/core-utils' 1import { PickWith, PickWithOpt } from '@shared/core-utils'
2import { VideoCommentModel } from '../../../models/video/video-comment'
3import { MAccountDefault, MAccountFormattable, MAccountUrl } from '../account' 3import { MAccountDefault, MAccountFormattable, MAccountUrl } from '../account'
4import { MVideoAccountLight, MVideoFeed, MVideoIdUrl, MVideoUrl } from './video' 4import { MVideo, MVideoAccountLight, MVideoFeed, MVideoIdUrl, MVideoUrl } from './video'
5 5
6type Use<K extends keyof VideoCommentModel, M> = PickWith<VideoCommentModel, K, M> 6type Use<K extends keyof VideoCommentModel, M> = PickWith<VideoCommentModel, K, M>
7 7
@@ -59,6 +59,11 @@ export type MCommentFormattable =
59 MCommentTotalReplies & 59 MCommentTotalReplies &
60 Use<'Account', MAccountFormattable> 60 Use<'Account', MAccountFormattable>
61 61
62export type MCommentAdminFormattable =
63 MComment &
64 Use<'Account', MAccountFormattable> &
65 Use<'Video', MVideo>
66
62export type MCommentAP = 67export type MCommentAP =
63 MComment & 68 MComment &
64 Use<'Account', MAccountUrl> & 69 Use<'Account', MAccountUrl> &