]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/video-comment.ts
Import magnets with webtorrent
[github/Chocobozzz/PeerTube.git] / server / lib / video-comment.ts
CommitLineData
bf1f6508
C
1import * as Sequelize from 'sequelize'
2import { ResultList } from '../../shared/models'
d3ea8975 3import { VideoCommentThreadTree } from '../../shared/models/videos/video-comment.model'
4635f59d 4import { AccountModel } from '../models/account/account'
bf1f6508
C
5import { VideoModel } from '../models/video/video'
6import { VideoCommentModel } from '../models/video/video-comment'
4635f59d 7import { getVideoCommentActivityPubUrl } from './activitypub'
07197db4 8import { sendCreateVideoComment } from './activitypub/send'
bf1f6508
C
9
10async function createVideoComment (obj: {
11 text: string,
c1e791ba 12 inReplyToComment: VideoCommentModel | null,
bf1f6508 13 video: VideoModel
4635f59d 14 account: AccountModel
bf1f6508 15}, t: Sequelize.Transaction) {
c1e791ba
RK
16 let originCommentId: number | null = null
17 let inReplyToCommentId: number | null = null
d3ea8975 18
c1e791ba 19 if (obj.inReplyToComment && obj.inReplyToComment !== null) {
ea44f375 20 originCommentId = obj.inReplyToComment.originCommentId || obj.inReplyToComment.id
4635f59d 21 inReplyToCommentId = obj.inReplyToComment.id
bf1f6508
C
22 }
23
24 const comment = await VideoCommentModel.create({
25 text: obj.text,
26 originCommentId,
4635f59d 27 inReplyToCommentId,
bf1f6508 28 videoId: obj.video.id,
4635f59d 29 accountId: obj.account.id,
d3ea8975
C
30 url: 'fake url'
31 }, { transaction: t, validate: false })
bf1f6508
C
32
33 comment.set('url', getVideoCommentActivityPubUrl(obj.video, comment))
34
ea44f375
C
35 const savedComment = await comment.save({ transaction: t })
36 savedComment.InReplyToVideoComment = obj.inReplyToComment
37 savedComment.Video = obj.video
4635f59d 38 savedComment.Account = obj.account
ea44f375 39
07197db4 40 await sendCreateVideoComment(savedComment, t)
ea44f375
C
41
42 return savedComment
bf1f6508
C
43}
44
d3ea8975 45function buildFormattedCommentTree (resultList: ResultList<VideoCommentModel>): VideoCommentThreadTree {
bf1f6508
C
46 // Comments are sorted by id ASC
47 const comments = resultList.data
48
49 const comment = comments.shift()
d3ea8975 50 const thread: VideoCommentThreadTree = {
bf1f6508
C
51 comment: comment.toFormattedJSON(),
52 children: []
53 }
54 const idx = {
55 [comment.id]: thread
56 }
57
58 while (comments.length !== 0) {
59 const childComment = comments.shift()
60
d3ea8975 61 const childCommentThread: VideoCommentThreadTree = {
bf1f6508
C
62 comment: childComment.toFormattedJSON(),
63 children: []
64 }
65
66 const parentCommentThread = idx[childComment.inReplyToCommentId]
67 if (!parentCommentThread) {
68 const msg = `Cannot format video thread tree, parent ${childComment.inReplyToCommentId} not found for child ${childComment.id}`
69 throw new Error(msg)
70 }
71
72 parentCommentThread.children.push(childCommentThread)
73 idx[childComment.id] = childCommentThread
74 }
75
76 return thread
77}
78
79// ---------------------------------------------------------------------------
80
81export {
82 createVideoComment,
83 buildFormattedCommentTree
84}