]>
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' |
4635f59d | 4 | import { AccountModel } from '../models/account/account' |
bf1f6508 C |
5 | import { VideoModel } from '../models/video/video' |
6 | import { VideoCommentModel } from '../models/video/video-comment' | |
4635f59d | 7 | import { getVideoCommentActivityPubUrl } from './activitypub' |
ea44f375 | 8 | import { sendCreateVideoCommentToOrigin, sendCreateVideoCommentToVideoFollowers } from './activitypub/send' |
bf1f6508 C |
9 | |
10 | async function createVideoComment (obj: { | |
11 | text: string, | |
ea44f375 | 12 | inReplyToComment: VideoCommentModel, |
bf1f6508 | 13 | video: VideoModel |
4635f59d | 14 | account: AccountModel |
bf1f6508 C |
15 | }, t: Sequelize.Transaction) { |
16 | let originCommentId: number = null | |
4635f59d | 17 | let inReplyToCommentId: number = null |
d3ea8975 | 18 | |
ea44f375 C |
19 | if (obj.inReplyToComment) { |
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 C |
39 | |
40 | if (savedComment.Video.isOwned()) { | |
41 | await sendCreateVideoCommentToVideoFollowers(savedComment, t) | |
42 | } else { | |
43 | await sendCreateVideoCommentToOrigin(savedComment, t) | |
44 | } | |
45 | ||
46 | return savedComment | |
bf1f6508 C |
47 | } |
48 | ||
d3ea8975 | 49 | function buildFormattedCommentTree (resultList: ResultList<VideoCommentModel>): VideoCommentThreadTree { |
bf1f6508 C |
50 | // Comments are sorted by id ASC |
51 | const comments = resultList.data | |
52 | ||
53 | const comment = comments.shift() | |
d3ea8975 | 54 | const thread: VideoCommentThreadTree = { |
bf1f6508 C |
55 | comment: comment.toFormattedJSON(), |
56 | children: [] | |
57 | } | |
58 | const idx = { | |
59 | [comment.id]: thread | |
60 | } | |
61 | ||
62 | while (comments.length !== 0) { | |
63 | const childComment = comments.shift() | |
64 | ||
d3ea8975 | 65 | const childCommentThread: VideoCommentThreadTree = { |
bf1f6508 C |
66 | comment: childComment.toFormattedJSON(), |
67 | children: [] | |
68 | } | |
69 | ||
70 | const parentCommentThread = idx[childComment.inReplyToCommentId] | |
71 | if (!parentCommentThread) { | |
72 | const msg = `Cannot format video thread tree, parent ${childComment.inReplyToCommentId} not found for child ${childComment.id}` | |
73 | throw new Error(msg) | |
74 | } | |
75 | ||
76 | parentCommentThread.children.push(childCommentThread) | |
77 | idx[childComment.id] = childCommentThread | |
78 | } | |
79 | ||
80 | return thread | |
81 | } | |
82 | ||
83 | // --------------------------------------------------------------------------- | |
84 | ||
85 | export { | |
86 | createVideoComment, | |
87 | buildFormattedCommentTree | |
88 | } |