From bf1f650817dadfd5eeee9e5e0b6b6938c136e25d Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 22 Dec 2017 10:50:07 +0100 Subject: Add comments controller --- server/models/video/video-comment.ts | 88 +++++++++++++++++++++++++++++++++++- 1 file changed, 86 insertions(+), 2 deletions(-) (limited to 'server/models/video') diff --git a/server/models/video/video-comment.ts b/server/models/video/video-comment.ts index 92c0c6112..d66f933ee 100644 --- a/server/models/video/video-comment.ts +++ b/server/models/video/video-comment.ts @@ -1,19 +1,34 @@ import * as Sequelize from 'sequelize' import { - AllowNull, BelongsTo, Column, CreatedAt, DataType, Default, ForeignKey, IFindOptions, Is, IsUUID, Model, Table, + AfterDestroy, AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, IFindOptions, Is, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript' +import { VideoComment } from '../../../shared/models/videos/video-comment.model' import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub' import { CONSTRAINTS_FIELDS } from '../../initializers' import { ActorModel } from '../activitypub/actor' -import { throwIfNotValid } from '../utils' +import { getSort, throwIfNotValid } from '../utils' import { VideoModel } from './video' +enum ScopeNames { + WITH_ACTOR = 'WITH_ACTOR' +} + +@Scopes({ + [ScopeNames.WITH_ACTOR]: { + include: [ + () => ActorModel + ] + } +}) @Table({ tableName: 'videoComment', indexes: [ { fields: [ 'videoId' ] + }, + { + fields: [ 'videoId', 'originCommentId' ] } ] }) @@ -81,6 +96,24 @@ export class VideoCommentModel extends Model { }) Actor: ActorModel + @AfterDestroy + static sendDeleteIfOwned (instance: VideoCommentModel) { + // TODO + return undefined + } + + static loadById (id: number, t?: Sequelize.Transaction) { + const query: IFindOptions = { + where: { + id + } + } + + if (t !== undefined) query.transaction = t + + return VideoCommentModel.findOne(query) + } + static loadByUrl (url: string, t?: Sequelize.Transaction) { const query: IFindOptions = { where: { @@ -92,4 +125,55 @@ export class VideoCommentModel extends Model { return VideoCommentModel.findOne(query) } + + static listThreadsForApi (videoId: number, start: number, count: number, sort: string) { + const query = { + offset: start, + limit: count, + order: [ getSort(sort) ], + where: { + videoId + } + } + + return VideoCommentModel + .scope([ ScopeNames.WITH_ACTOR ]) + .findAndCountAll(query) + .then(({ rows, count }) => { + return { total: count, data: rows } + }) + } + + static listThreadCommentsForApi (videoId: number, threadId: number) { + const query = { + order: [ 'id', 'ASC' ], + where: { + videoId, + [ Sequelize.Op.or ]: [ + { id: threadId }, + { originCommentId: threadId } + ] + } + } + + return VideoCommentModel + .scope([ ScopeNames.WITH_ACTOR ]) + .findAndCountAll(query) + .then(({ rows, count }) => { + return { total: count, data: rows } + }) + } + + toFormattedJSON () { + return { + id: this.id, + url: this.url, + text: this.text, + threadId: this.originCommentId || this.id, + inReplyToCommentId: this.inReplyToCommentId, + videoId: this.videoId, + createdAt: this.createdAt, + updatedAt: this.updatedAt + } as VideoComment + } } -- cgit v1.2.3