1
0
Fork 0

Begin unit tests

This commit is contained in:
Chocobozzz 2017-12-22 12:10:40 +01:00
parent bf1f650817
commit d3ea897591
No known key found for this signature in database
GPG key ID: 583A612D890159BE
9 changed files with 252 additions and 42 deletions

View file

@ -1,19 +1,20 @@
import * as Sequelize from 'sequelize'
import { ResultList } from '../../shared/models'
import { VideoCommentThread } from '../../shared/models/videos/video-comment.model'
import { VideoCommentThreadTree } from '../../shared/models/videos/video-comment.model'
import { VideoModel } from '../models/video/video'
import { VideoCommentModel } from '../models/video/video-comment'
import { getVideoCommentActivityPubUrl } from './activitypub'
async function createVideoComment (obj: {
text: string,
inReplyToComment: number,
inReplyToCommentId: number,
video: VideoModel
actorId: number
accountId: number
}, t: Sequelize.Transaction) {
let originCommentId: number = null
if (obj.inReplyToComment) {
const repliedComment = await VideoCommentModel.loadById(obj.inReplyToComment)
if (obj.inReplyToCommentId) {
const repliedComment = await VideoCommentModel.loadById(obj.inReplyToCommentId)
if (!repliedComment) throw new Error('Unknown replied comment.')
originCommentId = repliedComment.originCommentId || repliedComment.id
@ -22,22 +23,23 @@ async function createVideoComment (obj: {
const comment = await VideoCommentModel.create({
text: obj.text,
originCommentId,
inReplyToComment: obj.inReplyToComment,
inReplyToCommentId: obj.inReplyToCommentId,
videoId: obj.video.id,
actorId: obj.actorId
}, { transaction: t })
accountId: obj.accountId,
url: 'fake url'
}, { transaction: t, validate: false })
comment.set('url', getVideoCommentActivityPubUrl(obj.video, comment))
return comment.save({ transaction: t })
}
function buildFormattedCommentTree (resultList: ResultList<VideoCommentModel>): VideoCommentThread {
function buildFormattedCommentTree (resultList: ResultList<VideoCommentModel>): VideoCommentThreadTree {
// Comments are sorted by id ASC
const comments = resultList.data
const comment = comments.shift()
const thread: VideoCommentThread = {
const thread: VideoCommentThreadTree = {
comment: comment.toFormattedJSON(),
children: []
}
@ -48,7 +50,7 @@ function buildFormattedCommentTree (resultList: ResultList<VideoCommentModel>):
while (comments.length !== 0) {
const childComment = comments.shift()
const childCommentThread: VideoCommentThread = {
const childCommentThread: VideoCommentThreadTree = {
comment: childComment.toFormattedJSON(),
children: []
}