]>
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 C |
4 | import { VideoModel } from '../models/video/video' |
5 | import { VideoCommentModel } from '../models/video/video-comment' | |
6 | import { getVideoCommentActivityPubUrl } from './activitypub' | |
7 | ||
8 | async function createVideoComment (obj: { | |
9 | text: string, | |
d3ea8975 | 10 | inReplyToCommentId: number, |
bf1f6508 | 11 | video: VideoModel |
d3ea8975 | 12 | accountId: number |
bf1f6508 C |
13 | }, t: Sequelize.Transaction) { |
14 | let originCommentId: number = null | |
d3ea8975 C |
15 | |
16 | if (obj.inReplyToCommentId) { | |
17 | const repliedComment = await VideoCommentModel.loadById(obj.inReplyToCommentId) | |
bf1f6508 C |
18 | if (!repliedComment) throw new Error('Unknown replied comment.') |
19 | ||
20 | originCommentId = repliedComment.originCommentId || repliedComment.id | |
21 | } | |
22 | ||
23 | const comment = await VideoCommentModel.create({ | |
24 | text: obj.text, | |
25 | originCommentId, | |
d3ea8975 | 26 | inReplyToCommentId: obj.inReplyToCommentId, |
bf1f6508 | 27 | videoId: obj.video.id, |
d3ea8975 C |
28 | accountId: obj.accountId, |
29 | url: 'fake url' | |
30 | }, { transaction: t, validate: false }) | |
bf1f6508 C |
31 | |
32 | comment.set('url', getVideoCommentActivityPubUrl(obj.video, comment)) | |
33 | ||
34 | return comment.save({ transaction: t }) | |
35 | } | |
36 | ||
d3ea8975 | 37 | function buildFormattedCommentTree (resultList: ResultList<VideoCommentModel>): VideoCommentThreadTree { |
bf1f6508 C |
38 | // Comments are sorted by id ASC |
39 | const comments = resultList.data | |
40 | ||
41 | const comment = comments.shift() | |
d3ea8975 | 42 | const thread: VideoCommentThreadTree = { |
bf1f6508 C |
43 | comment: comment.toFormattedJSON(), |
44 | children: [] | |
45 | } | |
46 | const idx = { | |
47 | [comment.id]: thread | |
48 | } | |
49 | ||
50 | while (comments.length !== 0) { | |
51 | const childComment = comments.shift() | |
52 | ||
d3ea8975 | 53 | const childCommentThread: VideoCommentThreadTree = { |
bf1f6508 C |
54 | comment: childComment.toFormattedJSON(), |
55 | children: [] | |
56 | } | |
57 | ||
58 | const parentCommentThread = idx[childComment.inReplyToCommentId] | |
59 | if (!parentCommentThread) { | |
60 | const msg = `Cannot format video thread tree, parent ${childComment.inReplyToCommentId} not found for child ${childComment.id}` | |
61 | throw new Error(msg) | |
62 | } | |
63 | ||
64 | parentCommentThread.children.push(childCommentThread) | |
65 | idx[childComment.id] = childCommentThread | |
66 | } | |
67 | ||
68 | return thread | |
69 | } | |
70 | ||
71 | // --------------------------------------------------------------------------- | |
72 | ||
73 | export { | |
74 | createVideoComment, | |
75 | buildFormattedCommentTree | |
76 | } |