aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/video-comment.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2017-12-22 10:50:07 +0100
committerChocobozzz <me@florianbigard.com>2017-12-22 11:29:12 +0100
commitbf1f650817dadfd5eeee9e5e0b6b6938c136e25d (patch)
tree0f1dc95d87089be177ebe60740a55dd0c96b2414 /server/lib/video-comment.ts
parent6d8524702874120a4667269a81a61e3c7c5e300d (diff)
downloadPeerTube-bf1f650817dadfd5eeee9e5e0b6b6938c136e25d.tar.gz
PeerTube-bf1f650817dadfd5eeee9e5e0b6b6938c136e25d.tar.zst
PeerTube-bf1f650817dadfd5eeee9e5e0b6b6938c136e25d.zip
Add comments controller
Diffstat (limited to 'server/lib/video-comment.ts')
-rw-r--r--server/lib/video-comment.ts74
1 files changed, 74 insertions, 0 deletions
diff --git a/server/lib/video-comment.ts b/server/lib/video-comment.ts
new file mode 100644
index 000000000..edb72d4e2
--- /dev/null
+++ b/server/lib/video-comment.ts
@@ -0,0 +1,74 @@
1import * as Sequelize from 'sequelize'
2import { ResultList } from '../../shared/models'
3import { VideoCommentThread } from '../../shared/models/videos/video-comment.model'
4import { VideoModel } from '../models/video/video'
5import { VideoCommentModel } from '../models/video/video-comment'
6import { getVideoCommentActivityPubUrl } from './activitypub'
7
8async function createVideoComment (obj: {
9 text: string,
10 inReplyToComment: number,
11 video: VideoModel
12 actorId: number
13}, t: Sequelize.Transaction) {
14 let originCommentId: number = null
15 if (obj.inReplyToComment) {
16 const repliedComment = await VideoCommentModel.loadById(obj.inReplyToComment)
17 if (!repliedComment) throw new Error('Unknown replied comment.')
18
19 originCommentId = repliedComment.originCommentId || repliedComment.id
20 }
21
22 const comment = await VideoCommentModel.create({
23 text: obj.text,
24 originCommentId,
25 inReplyToComment: obj.inReplyToComment,
26 videoId: obj.video.id,
27 actorId: obj.actorId
28 }, { transaction: t })
29
30 comment.set('url', getVideoCommentActivityPubUrl(obj.video, comment))
31
32 return comment.save({ transaction: t })
33}
34
35function buildFormattedCommentTree (resultList: ResultList<VideoCommentModel>): VideoCommentThread {
36 // Comments are sorted by id ASC
37 const comments = resultList.data
38
39 const comment = comments.shift()
40 const thread: VideoCommentThread = {
41 comment: comment.toFormattedJSON(),
42 children: []
43 }
44 const idx = {
45 [comment.id]: thread
46 }
47
48 while (comments.length !== 0) {
49 const childComment = comments.shift()
50
51 const childCommentThread: VideoCommentThread = {
52 comment: childComment.toFormattedJSON(),
53 children: []
54 }
55
56 const parentCommentThread = idx[childComment.inReplyToCommentId]
57 if (!parentCommentThread) {
58 const msg = `Cannot format video thread tree, parent ${childComment.inReplyToCommentId} not found for child ${childComment.id}`
59 throw new Error(msg)
60 }
61
62 parentCommentThread.children.push(childCommentThread)
63 idx[childComment.id] = childCommentThread
64 }
65
66 return thread
67}
68
69// ---------------------------------------------------------------------------
70
71export {
72 createVideoComment,
73 buildFormattedCommentTree
74}