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