]>
Commit | Line | Data |
---|---|---|
bf1f6508 C |
1 | import * as Sequelize from 'sequelize' |
2 | import { ResultList } from '../../shared/models' | |
d3ea8975 | 3 | import { VideoCommentThreadTree } from '../../shared/models/videos/video-comment.model' |
bf1f6508 | 4 | import { VideoCommentModel } from '../models/video/video-comment' |
4635f59d | 5 | import { getVideoCommentActivityPubUrl } from './activitypub' |
07197db4 | 6 | import { sendCreateVideoComment } from './activitypub/send' |
453e83ea | 7 | import { MAccountDefault, MComment, MCommentOwnerVideoReply, MVideoFullLight } from '../typings/models' |
bf1f6508 C |
8 | |
9 | async function createVideoComment (obj: { | |
10 | text: string, | |
453e83ea C |
11 | inReplyToComment: MComment | null, |
12 | video: MVideoFullLight, | |
13 | account: MAccountDefault | |
bf1f6508 | 14 | }, t: Sequelize.Transaction) { |
c1e791ba RK |
15 | let originCommentId: number | null = null |
16 | let inReplyToCommentId: number | null = null | |
d3ea8975 | 17 | |
c1e791ba | 18 | if (obj.inReplyToComment && obj.inReplyToComment !== null) { |
ea44f375 | 19 | originCommentId = obj.inReplyToComment.originCommentId || obj.inReplyToComment.id |
4635f59d | 20 | inReplyToCommentId = obj.inReplyToComment.id |
bf1f6508 C |
21 | } |
22 | ||
23 | const comment = await VideoCommentModel.create({ | |
24 | text: obj.text, | |
25 | originCommentId, | |
4635f59d | 26 | inReplyToCommentId, |
bf1f6508 | 27 | videoId: obj.video.id, |
4635f59d | 28 | accountId: obj.account.id, |
970ceac0 C |
29 | url: new Date().toISOString() |
30 | }, { transaction: t, validate: false }) | |
bf1f6508 | 31 | |
970ceac0 | 32 | comment.url = getVideoCommentActivityPubUrl(obj.video, comment) |
bf1f6508 | 33 | |
453e83ea | 34 | const savedComment: MCommentOwnerVideoReply = await comment.save({ transaction: t }) |
ea44f375 C |
35 | savedComment.InReplyToVideoComment = obj.inReplyToComment |
36 | savedComment.Video = obj.video | |
4635f59d | 37 | savedComment.Account = obj.account |
ea44f375 | 38 | |
07197db4 | 39 | await sendCreateVideoComment(savedComment, t) |
ea44f375 C |
40 | |
41 | return savedComment | |
bf1f6508 C |
42 | } |
43 | ||
d3ea8975 | 44 | function buildFormattedCommentTree (resultList: ResultList<VideoCommentModel>): VideoCommentThreadTree { |
bf1f6508 C |
45 | // Comments are sorted by id ASC |
46 | const comments = resultList.data | |
47 | ||
48 | const comment = comments.shift() | |
d3ea8975 | 49 | const thread: VideoCommentThreadTree = { |
bf1f6508 C |
50 | comment: comment.toFormattedJSON(), |
51 | children: [] | |
52 | } | |
53 | const idx = { | |
54 | [comment.id]: thread | |
55 | } | |
56 | ||
57 | while (comments.length !== 0) { | |
58 | const childComment = comments.shift() | |
59 | ||
d3ea8975 | 60 | const childCommentThread: VideoCommentThreadTree = { |
bf1f6508 C |
61 | comment: childComment.toFormattedJSON(), |
62 | children: [] | |
63 | } | |
64 | ||
65 | const parentCommentThread = idx[childComment.inReplyToCommentId] | |
7ad9b984 C |
66 | // Maybe the parent comment was blocked by the admin/user |
67 | if (!parentCommentThread) continue | |
bf1f6508 C |
68 | |
69 | parentCommentThread.children.push(childCommentThread) | |
70 | idx[childComment.id] = childCommentThread | |
71 | } | |
72 | ||
73 | return thread | |
74 | } | |
75 | ||
69222afa JM |
76 | function markCommentAsDeleted (comment: MCommentOwnerVideoReply): void { |
77 | comment.text = '' | |
78 | comment.deletedAt = new Date() | |
79 | comment.accountId = null | |
80 | } | |
81 | ||
bf1f6508 C |
82 | // --------------------------------------------------------------------------- |
83 | ||
84 | export { | |
85 | createVideoComment, | |
69222afa JM |
86 | buildFormattedCommentTree, |
87 | markCommentAsDeleted | |
bf1f6508 | 88 | } |