1
0
Fork 0
peertube/server/models/video/video-comment.ts

570 lines
15 KiB
TypeScript
Raw Normal View History

2019-08-15 05:53:26 -04:00
import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Is, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript'
import { ActivityTagObject, ActivityTombstoneObject } from '../../../shared/models/activitypub/objects/common-objects'
import { VideoCommentObject } from '../../../shared/models/activitypub/objects/video-comment-object'
2017-12-22 04:50:07 -05:00
import { VideoComment } from '../../../shared/models/videos/video-comment.model'
2017-12-28 05:16:08 -05:00
import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
import { CONSTRAINTS_FIELDS, WEBSERVER } from '../../initializers/constants'
2017-12-22 06:10:40 -05:00
import { AccountModel } from '../account/account'
2017-12-27 10:11:53 -05:00
import { ActorModel } from '../activitypub/actor'
2019-12-27 11:02:34 -05:00
import { buildBlockedAccountSQL, buildLocalAccountIdsIn, getCommentSort, throwIfNotValid } from '../utils'
import { VideoModel } from './video'
2018-01-04 05:19:16 -05:00
import { VideoChannelModel } from './video-channel'
import { getServerActor } from '../../helpers/utils'
import { actorNameAlphabet } from '../../helpers/custom-validators/activitypub/actor'
import { regexpCapture } from '../../helpers/regexp'
import { uniq } from 'lodash'
2019-08-15 05:53:26 -04:00
import { FindOptions, Op, Order, ScopeOptions, Sequelize, Transaction } from 'sequelize'
import * as Bluebird from 'bluebird'
import {
MComment,
2019-08-21 08:31:57 -04:00
MCommentAP,
2019-08-20 13:05:31 -04:00
MCommentFormattable,
2019-08-15 05:53:26 -04:00
MCommentId,
MCommentOwner,
MCommentOwnerReplyVideoLight,
MCommentOwnerVideo,
MCommentOwnerVideoFeed,
MCommentOwnerVideoReply
} from '../../typings/models/video'
import { MUserAccountId } from '@server/typings/models'
2017-12-22 04:50:07 -05:00
enum ScopeNames {
WITH_ACCOUNT = 'WITH_ACCOUNT',
2017-12-27 10:11:53 -05:00
WITH_IN_REPLY_TO = 'WITH_IN_REPLY_TO',
2017-12-28 05:16:08 -05:00
WITH_VIDEO = 'WITH_VIDEO',
2017-12-27 10:11:53 -05:00
ATTRIBUTES_FOR_API = 'ATTRIBUTES_FOR_API'
2017-12-22 04:50:07 -05:00
}
2019-04-23 03:50:57 -04:00
@Scopes(() => ({
[ScopeNames.ATTRIBUTES_FOR_API]: (serverAccountId: number, userAccountId?: number) => {
return {
attributes: {
include: [
[
Sequelize.literal(
'(' +
'WITH "blocklist" AS (' + buildBlockedAccountSQL(serverAccountId, userAccountId) + ')' +
'SELECT COUNT("replies"."id") - (' +
'SELECT COUNT("replies"."id") ' +
'FROM "videoComment" AS "replies" ' +
'WHERE "replies"."originCommentId" = "VideoCommentModel"."id" ' +
'AND "accountId" IN (SELECT "id" FROM "blocklist")' +
')' +
'FROM "videoComment" AS "replies" ' +
'WHERE "replies"."originCommentId" = "VideoCommentModel"."id" ' +
'AND "accountId" NOT IN (SELECT "id" FROM "blocklist")' +
')'
),
'totalReplies'
],
[
Sequelize.literal(
'(' +
'SELECT COUNT("replies"."id") ' +
'FROM "videoComment" AS "replies" ' +
'INNER JOIN "video" ON "video"."id" = "replies"."videoId" ' +
'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' +
'WHERE "replies"."originCommentId" = "VideoCommentModel"."id" ' +
'AND "replies"."accountId" = "videoChannel"."accountId"' +
')'
),
'totalRepliesFromVideoAuthor'
]
2017-12-27 10:11:53 -05:00
]
}
2019-04-23 03:50:57 -04:00
} as FindOptions
2017-12-27 10:11:53 -05:00
},
2017-12-22 06:10:40 -05:00
[ScopeNames.WITH_ACCOUNT]: {
2017-12-22 04:50:07 -05:00
include: [
2017-12-27 10:11:53 -05:00
{
2019-08-15 05:53:26 -04:00
model: AccountModel
2017-12-27 10:11:53 -05:00
}
2019-04-23 03:50:57 -04:00
]
},
[ScopeNames.WITH_IN_REPLY_TO]: {
include: [
{
2019-04-23 03:50:57 -04:00
model: VideoCommentModel,
2017-12-28 05:16:08 -05:00
as: 'InReplyToVideoComment'
}
]
},
[ScopeNames.WITH_VIDEO]: {
include: [
{
2019-04-23 03:50:57 -04:00
model: VideoModel,
2018-01-04 05:19:16 -05:00
required: true,
include: [
{
2019-08-15 05:53:26 -04:00
model: VideoChannelModel,
2018-01-04 05:19:16 -05:00
required: true,
include: [
{
2019-04-23 03:50:57 -04:00
model: AccountModel,
2019-08-15 05:53:26 -04:00
required: true
2018-01-04 05:19:16 -05:00
}
]
}
]
}
2019-04-23 03:50:57 -04:00
]
2017-12-22 04:50:07 -05:00
}
2019-04-23 03:50:57 -04:00
}))
@Table({
tableName: 'videoComment',
indexes: [
{
fields: [ 'videoId' ]
2017-12-22 04:50:07 -05:00
},
{
fields: [ 'videoId', 'originCommentId' ]
2018-01-26 08:14:43 -05:00
},
{
fields: [ 'url' ],
unique: true
2018-07-23 14:13:30 -04:00
},
{
fields: [ 'accountId' ]
}
]
})
export class VideoCommentModel extends Model<VideoCommentModel> {
@CreatedAt
createdAt: Date
@UpdatedAt
updatedAt: Date
@AllowNull(true)
@Column(DataType.DATE)
deletedAt: Date
@AllowNull(false)
@Is('VideoCommentUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
@Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS.URL.max))
url: string
@AllowNull(false)
@Column(DataType.TEXT)
text: string
@ForeignKey(() => VideoCommentModel)
@Column
originCommentId: number
@BelongsTo(() => VideoCommentModel, {
foreignKey: {
2017-12-28 05:45:10 -05:00
name: 'originCommentId',
allowNull: true
},
2017-12-28 05:45:10 -05:00
as: 'OriginVideoComment',
onDelete: 'CASCADE'
})
OriginVideoComment: VideoCommentModel
@ForeignKey(() => VideoCommentModel)
@Column
inReplyToCommentId: number
@BelongsTo(() => VideoCommentModel, {
foreignKey: {
2017-12-28 05:45:10 -05:00
name: 'inReplyToCommentId',
allowNull: true
},
2017-12-28 05:16:08 -05:00
as: 'InReplyToVideoComment',
onDelete: 'CASCADE'
})
InReplyToVideoComment: VideoCommentModel | null
@ForeignKey(() => VideoModel)
@Column
videoId: number
@BelongsTo(() => VideoModel, {
foreignKey: {
allowNull: false
},
onDelete: 'CASCADE'
})
Video: VideoModel
2017-12-22 06:10:40 -05:00
@ForeignKey(() => AccountModel)
@Column
2017-12-22 06:10:40 -05:00
accountId: number
2017-12-22 06:10:40 -05:00
@BelongsTo(() => AccountModel, {
foreignKey: {
allowNull: true
},
onDelete: 'CASCADE'
})
2017-12-22 06:10:40 -05:00
Account: AccountModel
2019-08-15 05:53:26 -04:00
static loadById (id: number, t?: Transaction): Bluebird<MComment> {
2019-04-18 05:28:17 -04:00
const query: FindOptions = {
2017-12-22 04:50:07 -05:00
where: {
id
}
}
if (t !== undefined) query.transaction = t
return VideoCommentModel.findOne(query)
}
2019-08-15 05:53:26 -04:00
static loadByIdAndPopulateVideoAndAccountAndReply (id: number, t?: Transaction): Bluebird<MCommentOwnerVideoReply> {
2019-04-18 05:28:17 -04:00
const query: FindOptions = {
2017-12-28 05:16:08 -05:00
where: {
id
}
}
if (t !== undefined) query.transaction = t
return VideoCommentModel
.scope([ ScopeNames.WITH_VIDEO, ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_IN_REPLY_TO ])
.findOne(query)
}
2019-08-15 05:53:26 -04:00
static loadByUrlAndPopulateAccountAndVideo (url: string, t?: Transaction): Bluebird<MCommentOwnerVideo> {
2019-04-18 05:28:17 -04:00
const query: FindOptions = {
where: {
url
}
}
if (t !== undefined) query.transaction = t
return VideoCommentModel.scope([ ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_VIDEO ]).findOne(query)
}
2017-12-22 04:50:07 -05:00
2019-08-15 05:53:26 -04:00
static loadByUrlAndPopulateReplyAndVideoUrlAndAccount (url: string, t?: Transaction): Bluebird<MCommentOwnerReplyVideoLight> {
2019-04-18 05:28:17 -04:00
const query: FindOptions = {
2018-01-04 05:19:16 -05:00
where: {
url
2019-08-06 11:19:53 -04:00
},
include: [
{
attributes: [ 'id', 'url' ],
model: VideoModel.unscoped()
}
]
2018-01-04 05:19:16 -05:00
}
if (t !== undefined) query.transaction = t
2019-08-06 11:19:53 -04:00
return VideoCommentModel.scope([ ScopeNames.WITH_IN_REPLY_TO, ScopeNames.WITH_ACCOUNT ]).findOne(query)
2018-01-04 05:19:16 -05:00
}
2019-07-18 08:28:37 -04:00
static async listThreadsForApi (parameters: {
2020-01-31 10:56:52 -05:00
videoId: number
start: number
count: number
sort: string
2019-08-15 05:53:26 -04:00
user?: MUserAccountId
2019-07-18 08:28:37 -04:00
}) {
const { videoId, start, count, sort, user } = parameters
const serverActor = await getServerActor()
const serverAccountId = serverActor.Account.id
const userAccountId = user ? user.Account.id : undefined
2017-12-22 04:50:07 -05:00
const query = {
offset: start,
limit: count,
2019-12-27 11:02:34 -05:00
order: getCommentSort(sort),
2017-12-22 04:50:07 -05:00
where: {
2017-12-22 06:10:40 -05:00
videoId,
inReplyToCommentId: null,
accountId: {
2019-04-18 05:28:17 -04:00
[Op.notIn]: Sequelize.literal(
'(' + buildBlockedAccountSQL(serverAccountId, userAccountId) + ')'
)
}
2017-12-22 04:50:07 -05:00
}
}
2019-04-23 03:50:57 -04:00
const scopes: (string | ScopeOptions)[] = [
ScopeNames.WITH_ACCOUNT,
{
method: [ ScopeNames.ATTRIBUTES_FOR_API, serverAccountId, userAccountId ]
}
]
2017-12-22 04:50:07 -05:00
return VideoCommentModel
.scope(scopes)
2017-12-22 04:50:07 -05:00
.findAndCountAll(query)
.then(({ rows, count }) => {
return { total: count, data: rows }
})
}
2019-07-18 08:28:37 -04:00
static async listThreadCommentsForApi (parameters: {
2020-01-31 10:56:52 -05:00
videoId: number
threadId: number
2019-08-15 05:53:26 -04:00
user?: MUserAccountId
2019-07-18 08:28:37 -04:00
}) {
const { videoId, threadId, user } = parameters
const serverActor = await getServerActor()
const serverAccountId = serverActor.Account.id
const userAccountId = user ? user.Account.id : undefined
2017-12-22 04:50:07 -05:00
const query = {
2019-04-18 05:28:17 -04:00
order: [ [ 'createdAt', 'ASC' ], [ 'updatedAt', 'ASC' ] ] as Order,
2017-12-22 04:50:07 -05:00
where: {
videoId,
2020-01-31 10:56:52 -05:00
[Op.or]: [
2017-12-22 04:50:07 -05:00
{ id: threadId },
{ originCommentId: threadId }
],
accountId: {
2019-04-18 05:28:17 -04:00
[Op.notIn]: Sequelize.literal(
'(' + buildBlockedAccountSQL(serverAccountId, userAccountId) + ')'
)
}
2017-12-22 04:50:07 -05:00
}
}
const scopes: any[] = [
ScopeNames.WITH_ACCOUNT,
{
method: [ ScopeNames.ATTRIBUTES_FOR_API, serverAccountId, userAccountId ]
}
]
2017-12-22 04:50:07 -05:00
return VideoCommentModel
.scope(scopes)
2017-12-22 04:50:07 -05:00
.findAndCountAll(query)
.then(({ rows, count }) => {
return { total: count, data: rows }
})
}
2019-08-15 05:53:26 -04:00
static listThreadParentComments (comment: MCommentId, t: Transaction, order: 'ASC' | 'DESC' = 'ASC'): Bluebird<MCommentOwner[]> {
2018-01-05 05:19:25 -05:00
const query = {
2019-04-18 05:28:17 -04:00
order: [ [ 'createdAt', order ] ] as Order,
2018-01-05 05:19:25 -05:00
where: {
id: {
2020-01-31 10:56:52 -05:00
[Op.in]: Sequelize.literal('(' +
2018-03-21 06:17:01 -04:00
'WITH RECURSIVE children (id, "inReplyToCommentId") AS ( ' +
`SELECT id, "inReplyToCommentId" FROM "videoComment" WHERE id = ${comment.id} ` +
'UNION ' +
'SELECT "parent"."id", "parent"."inReplyToCommentId" FROM "videoComment" "parent" ' +
'INNER JOIN "children" ON "children"."inReplyToCommentId" = "parent"."id"' +
') ' +
2018-03-21 06:17:01 -04:00
'SELECT id FROM children' +
')'),
2020-01-31 10:56:52 -05:00
[Op.ne]: comment.id
2018-01-05 05:19:25 -05:00
}
},
transaction: t
}
return VideoCommentModel
.scope([ ScopeNames.WITH_ACCOUNT ])
.findAll(query)
}
2019-04-18 05:28:17 -04:00
static listAndCountByVideoId (videoId: number, start: number, count: number, t?: Transaction, order: 'ASC' | 'DESC' = 'ASC') {
const query = {
2019-04-18 05:28:17 -04:00
order: [ [ 'createdAt', order ] ] as Order,
offset: start,
limit: count,
where: {
videoId
},
transaction: t
}
2019-08-15 05:53:26 -04:00
return VideoCommentModel.findAndCountAll<MComment>(query)
}
2019-08-15 05:53:26 -04:00
static listForFeed (start: number, count: number, videoId?: number): Bluebird<MCommentOwnerVideoFeed[]> {
2018-06-08 14:34:37 -04:00
const query = {
2019-04-18 05:28:17 -04:00
order: [ [ 'createdAt', 'DESC' ] ] as Order,
offset: start,
limit: count,
2018-06-08 14:34:37 -04:00
where: {},
include: [
{
2018-06-14 05:25:19 -04:00
attributes: [ 'name', 'uuid' ],
2018-06-08 14:34:37 -04:00
model: VideoModel.unscoped(),
required: true
}
]
}
if (videoId) query.where['videoId'] = videoId
return VideoCommentModel
.scope([ ScopeNames.WITH_ACCOUNT ])
.findAll(query)
}
2018-02-28 12:04:46 -05:00
static async getStats () {
const totalLocalVideoComments = await VideoCommentModel.count({
include: [
{
model: AccountModel,
required: true,
include: [
{
model: ActorModel,
required: true,
where: {
serverId: null
}
}
]
}
]
})
const totalVideoComments = await VideoCommentModel.count()
return {
totalLocalVideoComments,
totalVideoComments
}
}
2019-03-19 11:23:02 -04:00
static cleanOldCommentsOf (videoId: number, beforeUpdatedAt: Date) {
const query = {
where: {
updatedAt: {
2019-04-18 05:28:17 -04:00
[Op.lt]: beforeUpdatedAt
2019-03-19 11:23:02 -04:00
},
2019-08-06 11:19:53 -04:00
videoId,
accountId: {
[Op.notIn]: buildLocalAccountIdsIn()
2019-08-01 04:15:28 -04:00
}
2019-08-06 11:19:53 -04:00
}
2019-03-19 11:23:02 -04:00
}
return VideoCommentModel.destroy(query)
}
2018-12-26 04:36:24 -05:00
getCommentStaticPath () {
return this.Video.getWatchStaticPath() + ';threadId=' + this.getThreadId()
}
2018-01-05 05:19:25 -05:00
getThreadId (): number {
return this.originCommentId || this.id
}
2018-01-04 05:19:16 -05:00
isOwned () {
if (!this.Account) {
return false
}
2018-01-04 05:19:16 -05:00
return this.Account.isOwned()
}
isDeleted () {
2020-01-31 10:56:52 -05:00
return this.deletedAt !== null
}
extractMentions () {
let result: string[] = []
const localMention = `@(${actorNameAlphabet}+)`
2019-04-11 05:33:44 -04:00
const remoteMention = `${localMention}@${WEBSERVER.HOST}`
const mentionRegex = this.isOwned()
? '(?:(?:' + remoteMention + ')|(?:' + localMention + '))' // Include local mentions?
: '(?:' + remoteMention + ')'
const firstMentionRegex = new RegExp(`^${mentionRegex} `, 'g')
const endMentionRegex = new RegExp(` ${mentionRegex}$`, 'g')
const remoteMentionsRegex = new RegExp(' ' + remoteMention + ' ', 'g')
result = result.concat(
regexpCapture(this.text, firstMentionRegex)
.map(([ , username1, username2 ]) => username1 || username2),
regexpCapture(this.text, endMentionRegex)
.map(([ , username1, username2 ]) => username1 || username2),
regexpCapture(this.text, remoteMentionsRegex)
.map(([ , username ]) => username)
)
// Include local mentions
if (this.isOwned()) {
const localMentionsRegex = new RegExp(' ' + localMention + ' ', 'g')
result = result.concat(
regexpCapture(this.text, localMentionsRegex)
.map(([ , username ]) => username)
)
}
return uniq(result)
}
2019-08-20 13:05:31 -04:00
toFormattedJSON (this: MCommentFormattable) {
2017-12-22 04:50:07 -05:00
return {
id: this.id,
url: this.url,
text: this.text,
threadId: this.originCommentId || this.id,
2017-12-27 14:03:37 -05:00
inReplyToCommentId: this.inReplyToCommentId || null,
2017-12-22 04:50:07 -05:00
videoId: this.videoId,
createdAt: this.createdAt,
2017-12-22 06:10:40 -05:00
updatedAt: this.updatedAt,
deletedAt: this.deletedAt,
isDeleted: this.isDeleted(),
totalRepliesFromVideoAuthor: this.get('totalRepliesFromVideoAuthor') || 0,
2017-12-27 10:11:53 -05:00
totalReplies: this.get('totalReplies') || 0,
account: this.Account ? this.Account.toFormattedJSON() : null
2017-12-22 04:50:07 -05:00
} as VideoComment
}
toActivityPubObject (this: MCommentAP, threadParentComments: MCommentOwner[]): VideoCommentObject | ActivityTombstoneObject {
let inReplyTo: string
// New thread, so in AS we reply to the video
if (this.inReplyToCommentId === null) {
inReplyTo = this.Video.url
} else {
inReplyTo = this.InReplyToVideoComment.url
}
if (this.isDeleted()) {
return {
id: this.url,
type: 'Tombstone',
formerType: 'Note',
inReplyTo,
published: this.createdAt.toISOString(),
updated: this.updatedAt.toISOString(),
deleted: this.deletedAt.toISOString()
}
}
2018-01-05 05:19:25 -05:00
const tag: ActivityTagObject[] = []
for (const parentComment of threadParentComments) {
if (!parentComment.Account) continue
2018-01-05 05:19:25 -05:00
const actor = parentComment.Account.Actor
tag.push({
type: 'Mention',
href: actor.url,
name: `@${actor.preferredUsername}@${actor.getHost()}`
})
}
return {
type: 'Note' as 'Note',
id: this.url,
content: this.text,
inReplyTo,
2017-12-28 05:16:08 -05:00
updated: this.updatedAt.toISOString(),
published: this.createdAt.toISOString(),
2017-12-28 05:16:08 -05:00
url: this.url,
2018-01-05 05:19:25 -05:00
attributedTo: this.Account.Actor.url,
tag
}
}
}