]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/lib/video-comment.ts
Fix E2E tests
[github/Chocobozzz/PeerTube.git] / server / lib / video-comment.ts
... / ...
CommitLineData
1import { cloneDeep } from 'lodash'
2import * as Sequelize from 'sequelize'
3import { logger } from '@server/helpers/logger'
4import { sequelizeTypescript } from '@server/initializers/database'
5import { ResultList } from '../../shared/models'
6import { VideoCommentThreadTree } from '../../shared/models/videos/comment/video-comment.model'
7import { VideoCommentModel } from '../models/video/video-comment'
8import { MAccountDefault, MComment, MCommentOwnerVideo, MCommentOwnerVideoReply, MVideoFullLight } from '../types/models'
9import { sendCreateVideoComment, sendDeleteVideoComment } from './activitypub/send'
10import { getLocalVideoCommentActivityPubUrl } from './activitypub/url'
11import { Hooks } from './plugins/hooks'
12
13async function removeComment (videoCommentInstance: MCommentOwnerVideo) {
14 const videoCommentInstanceBefore = cloneDeep(videoCommentInstance)
15
16 await sequelizeTypescript.transaction(async t => {
17 if (videoCommentInstance.isOwned() || videoCommentInstance.Video.isOwned()) {
18 await sendDeleteVideoComment(videoCommentInstance, t)
19 }
20
21 videoCommentInstance.markAsDeleted()
22
23 await videoCommentInstance.save({ transaction: t })
24 })
25
26 logger.info('Video comment %d deleted.', videoCommentInstance.id)
27
28 Hooks.runAction('action:api.video-comment.deleted', { comment: videoCommentInstanceBefore })
29}
30
31async function createVideoComment (obj: {
32 text: string
33 inReplyToComment: MComment | null
34 video: MVideoFullLight
35 account: MAccountDefault
36}, t: Sequelize.Transaction) {
37 let originCommentId: number | null = null
38 let inReplyToCommentId: number | null = null
39
40 if (obj.inReplyToComment && obj.inReplyToComment !== null) {
41 originCommentId = obj.inReplyToComment.originCommentId || obj.inReplyToComment.id
42 inReplyToCommentId = obj.inReplyToComment.id
43 }
44
45 const comment = await VideoCommentModel.create({
46 text: obj.text,
47 originCommentId,
48 inReplyToCommentId,
49 videoId: obj.video.id,
50 accountId: obj.account.id,
51 url: new Date().toISOString()
52 }, { transaction: t, validate: false })
53
54 comment.url = getLocalVideoCommentActivityPubUrl(obj.video, comment)
55
56 const savedComment: MCommentOwnerVideoReply = await comment.save({ transaction: t })
57 savedComment.InReplyToVideoComment = obj.inReplyToComment
58 savedComment.Video = obj.video
59 savedComment.Account = obj.account
60
61 await sendCreateVideoComment(savedComment, t)
62
63 return savedComment
64}
65
66function buildFormattedCommentTree (resultList: ResultList<VideoCommentModel>): VideoCommentThreadTree {
67 // Comments are sorted by id ASC
68 const comments = resultList.data
69
70 const comment = comments.shift()
71 const thread: VideoCommentThreadTree = {
72 comment: comment.toFormattedJSON(),
73 children: []
74 }
75 const idx = {
76 [comment.id]: thread
77 }
78
79 while (comments.length !== 0) {
80 const childComment = comments.shift()
81
82 const childCommentThread: VideoCommentThreadTree = {
83 comment: childComment.toFormattedJSON(),
84 children: []
85 }
86
87 const parentCommentThread = idx[childComment.inReplyToCommentId]
88 // Maybe the parent comment was blocked by the admin/user
89 if (!parentCommentThread) continue
90
91 parentCommentThread.children.push(childCommentThread)
92 idx[childComment.id] = childCommentThread
93 }
94
95 return thread
96}
97
98// ---------------------------------------------------------------------------
99
100export {
101 removeComment,
102 createVideoComment,
103 buildFormattedCommentTree
104}