はじまりの大地

このコミットが含まれているのは:
2024-07-15 09:14:04 +09:00
コミット 6632905f32
3501個のファイルの変更1439465行の追加0行の削除
+113
ファイルの表示
@@ -0,0 +1,113 @@
import { FindOptions } from 'sequelize'
import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Is, Table, UpdatedAt } from 'sequelize-typescript'
import { isAbuseMessageValid } from '@server/helpers/custom-validators/abuses.js'
import { MAbuseMessage, MAbuseMessageFormattable } from '@server/types/models/index.js'
import { AbuseMessage } from '@peertube/peertube-models'
import { AccountModel, ScopeNames as AccountScopeNames } from '../account/account.js'
import { SequelizeModel, getSort, throwIfNotValid } from '../shared/index.js'
import { AbuseModel } from './abuse.js'
@Table({
tableName: 'abuseMessage',
indexes: [
{
fields: [ 'abuseId' ]
},
{
fields: [ 'accountId' ]
}
]
})
export class AbuseMessageModel extends SequelizeModel<AbuseMessageModel> {
@AllowNull(false)
@Is('AbuseMessage', value => throwIfNotValid(value, isAbuseMessageValid, 'message'))
@Column(DataType.TEXT)
message: string
@AllowNull(false)
@Column
byModerator: boolean
@CreatedAt
createdAt: Date
@UpdatedAt
updatedAt: Date
@ForeignKey(() => AccountModel)
@Column
accountId: number
@BelongsTo(() => AccountModel, {
foreignKey: {
name: 'accountId',
allowNull: true
},
onDelete: 'set null'
})
Account: Awaited<AccountModel>
@ForeignKey(() => AbuseModel)
@Column
abuseId: number
@BelongsTo(() => AbuseModel, {
foreignKey: {
name: 'abuseId',
allowNull: false
},
onDelete: 'cascade'
})
Abuse: Awaited<AbuseModel>
static listForApi (abuseId: number) {
const getQuery = (forCount: boolean) => {
const query: FindOptions = {
where: { abuseId },
order: getSort('createdAt')
}
if (forCount !== true) {
query.include = [
{
model: AccountModel.scope(AccountScopeNames.SUMMARY),
required: false
}
]
}
return query
}
return Promise.all([
AbuseMessageModel.count(getQuery(true)),
AbuseMessageModel.findAll(getQuery(false))
]).then(([ total, data ]) => ({ total, data }))
}
static loadByIdAndAbuseId (messageId: number, abuseId: number): Promise<MAbuseMessage> {
return AbuseMessageModel.findOne({
where: {
id: messageId,
abuseId
}
})
}
toFormattedJSON (this: MAbuseMessageFormattable): AbuseMessage {
const account = this.Account
? this.Account.toFormattedSummaryJSON()
: null
return {
id: this.id,
createdAt: this.createdAt,
byModerator: this.byModerator,
message: this.message,
account
}
}
}
+664
ファイルの表示
@@ -0,0 +1,664 @@
import { abusePredefinedReasonsMap, forceNumber } from '@peertube/peertube-core-utils'
import {
AbuseFilter,
AbuseObject,
AbusePredefinedReasonsString,
AbusePredefinedReasonsType,
AbuseVideoIs,
AdminAbuse,
AdminVideoAbuse,
AdminVideoCommentAbuse,
UserAbuse,
UserVideoAbuse,
type AbuseStateType,
AbuseState
} from '@peertube/peertube-models'
import { isAbuseModerationCommentValid, isAbuseReasonValid, isAbuseStateValid } from '@server/helpers/custom-validators/abuses.js'
import invert from 'lodash-es/invert.js'
import { Op, QueryTypes, literal } from 'sequelize'
import {
AllowNull,
BelongsTo,
Column,
CreatedAt,
DataType,
Default,
ForeignKey,
HasOne,
Is, Scopes,
Table,
UpdatedAt
} from 'sequelize-typescript'
import { ABUSE_STATES, CONSTRAINTS_FIELDS } from '../../initializers/constants.js'
import {
MAbuseAP,
MAbuseAdminFormattable,
MAbuseFull,
MAbuseReporter,
MAbuseUserFormattable,
MUserAccountId
} from '../../types/models/index.js'
import { AccountModel, ScopeNames as AccountScopeNames, SummaryOptions as AccountSummaryOptions } from '../account/account.js'
import { SequelizeModel, getSort, parseAggregateResult, throwIfNotValid } from '../shared/index.js'
import { ThumbnailModel } from '../video/thumbnail.js'
import { VideoBlacklistModel } from '../video/video-blacklist.js'
import { SummaryOptions as ChannelSummaryOptions, VideoChannelModel, ScopeNames as VideoChannelScopeNames } from '../video/video-channel.js'
import { ScopeNames as CommentScopeNames, VideoCommentModel } from '../video/video-comment.js'
import { VideoModel, ScopeNames as VideoScopeNames } from '../video/video.js'
import { BuildAbusesQueryOptions, buildAbuseListQuery } from './sql/abuse-query-builder.js'
import { VideoAbuseModel } from './video-abuse.js'
import { VideoCommentAbuseModel } from './video-comment-abuse.js'
export enum ScopeNames {
FOR_API = 'FOR_API'
}
@Scopes(() => ({
[ScopeNames.FOR_API]: () => {
return {
attributes: {
include: [
[
literal(
'(' +
'SELECT count(*) ' +
'FROM "abuseMessage" ' +
'WHERE "abuseId" = "AbuseModel"."id"' +
')'
),
'countMessages'
],
[
// we don't care about this count for deleted videos, so there are not included
literal(
'(' +
'SELECT count(*) ' +
'FROM "videoAbuse" ' +
'WHERE "videoId" IN (SELECT "videoId" FROM "videoAbuse" WHERE "abuseId" = "AbuseModel"."id") ' +
'AND "videoId" IS NOT NULL' +
')'
),
'countReportsForVideo'
],
[
// we don't care about this count for deleted videos, so there are not included
literal(
'(' +
'SELECT t.nth ' +
'FROM ( ' +
'SELECT id, "abuseId", row_number() OVER (PARTITION BY "videoId" ORDER BY "createdAt") AS nth ' +
'FROM "videoAbuse" ' +
') t ' +
'WHERE t."abuseId" = "AbuseModel"."id" ' +
')'
),
'nthReportForVideo'
],
[
literal(
'(' +
'SELECT count("abuse"."id") ' +
'FROM "abuse" ' +
'WHERE "abuse"."reporterAccountId" = "AbuseModel"."reporterAccountId"' +
')'
),
'countReportsForReporter'
],
[
literal(
'(' +
'SELECT count("abuse"."id") ' +
'FROM "abuse" ' +
'WHERE "abuse"."flaggedAccountId" = "AbuseModel"."flaggedAccountId"' +
')'
),
'countReportsForReportee'
]
]
},
include: [
{
model: AccountModel.scope({
method: [
AccountScopeNames.SUMMARY,
{ actorRequired: false } as AccountSummaryOptions
]
}),
as: 'ReporterAccount'
},
{
model: AccountModel.scope({
method: [
AccountScopeNames.SUMMARY,
{ actorRequired: false } as AccountSummaryOptions
]
}),
as: 'FlaggedAccount'
},
{
model: VideoCommentAbuseModel.unscoped(),
include: [
{
model: VideoCommentModel.unscoped(),
include: [
{
model: VideoModel.unscoped(),
attributes: [ 'name', 'id', 'uuid' ]
}
]
}
]
},
{
model: VideoAbuseModel.unscoped(),
include: [
{
attributes: [ 'id', 'uuid', 'name', 'nsfw' ],
model: VideoModel.unscoped(),
include: [
{
attributes: [ 'filename', 'fileUrl', 'type' ],
model: ThumbnailModel
},
{
model: VideoChannelModel.scope({
method: [
VideoChannelScopeNames.SUMMARY,
{ withAccount: false, actorRequired: false } as ChannelSummaryOptions
]
}),
required: false
},
{
attributes: [ 'id', 'reason', 'unfederated' ],
required: false,
model: VideoBlacklistModel
}
]
}
]
}
]
}
}
}))
@Table({
tableName: 'abuse',
indexes: [
{
fields: [ 'reporterAccountId' ]
},
{
fields: [ 'flaggedAccountId' ]
}
]
})
export class AbuseModel extends SequelizeModel<AbuseModel> {
@AllowNull(false)
@Default(null)
@Is('AbuseReason', value => throwIfNotValid(value, isAbuseReasonValid, 'reason'))
@Column(DataType.STRING(CONSTRAINTS_FIELDS.ABUSES.REASON.max))
reason: string
@AllowNull(false)
@Default(null)
@Is('AbuseState', value => throwIfNotValid(value, isAbuseStateValid, 'state'))
@Column
state: AbuseStateType
@AllowNull(true)
@Default(null)
@Is('AbuseModerationComment', value => throwIfNotValid(value, isAbuseModerationCommentValid, 'moderationComment', true))
@Column(DataType.STRING(CONSTRAINTS_FIELDS.ABUSES.MODERATION_COMMENT.max))
moderationComment: string
@AllowNull(true)
@Default(null)
@Column(DataType.ARRAY(DataType.INTEGER))
predefinedReasons: AbusePredefinedReasonsType[]
@AllowNull(true)
@Column
processedAt: Date
@CreatedAt
createdAt: Date
@UpdatedAt
updatedAt: Date
@ForeignKey(() => AccountModel)
@Column
reporterAccountId: number
@BelongsTo(() => AccountModel, {
foreignKey: {
name: 'reporterAccountId',
allowNull: true
},
as: 'ReporterAccount',
onDelete: 'set null'
})
ReporterAccount: Awaited<AccountModel>
@ForeignKey(() => AccountModel)
@Column
flaggedAccountId: number
@BelongsTo(() => AccountModel, {
foreignKey: {
name: 'flaggedAccountId',
allowNull: true
},
as: 'FlaggedAccount',
onDelete: 'set null'
})
FlaggedAccount: Awaited<AccountModel>
@HasOne(() => VideoCommentAbuseModel, {
foreignKey: {
name: 'abuseId',
allowNull: false
},
onDelete: 'cascade'
})
VideoCommentAbuse: Awaited<VideoCommentAbuseModel>
@HasOne(() => VideoAbuseModel, {
foreignKey: {
name: 'abuseId',
allowNull: false
},
onDelete: 'cascade'
})
VideoAbuse: Awaited<VideoAbuseModel>
static loadByIdWithReporter (id: number): Promise<MAbuseReporter> {
const query = {
where: {
id
},
include: [
{
model: AccountModel,
as: 'ReporterAccount'
}
]
}
return AbuseModel.findOne(query)
}
static loadFull (id: number): Promise<MAbuseFull> {
const query = {
where: {
id
},
include: [
{
model: AccountModel.scope(AccountScopeNames.SUMMARY),
required: false,
as: 'ReporterAccount'
},
{
model: AccountModel.scope(AccountScopeNames.SUMMARY),
as: 'FlaggedAccount'
},
{
model: VideoAbuseModel,
required: false,
include: [
{
model: VideoModel.scope([ VideoScopeNames.WITH_ACCOUNT_DETAILS ])
}
]
},
{
model: VideoCommentAbuseModel,
required: false,
include: [
{
model: VideoCommentModel.scope([
CommentScopeNames.WITH_ACCOUNT
]),
include: [
{
model: VideoModel
}
]
}
]
}
]
}
return AbuseModel.findOne(query)
}
static async listForAdminApi (parameters: {
start: number
count: number
sort: string
filter?: AbuseFilter
serverAccountId: number
user?: MUserAccountId
id?: number
predefinedReason?: AbusePredefinedReasonsString
state?: AbuseStateType
videoIs?: AbuseVideoIs
search?: string
searchReporter?: string
searchReportee?: string
searchVideo?: string
searchVideoChannel?: string
}) {
const {
start,
count,
sort,
search,
user,
serverAccountId,
state,
videoIs,
predefinedReason,
searchReportee,
searchVideo,
filter,
searchVideoChannel,
searchReporter,
id
} = parameters
const userAccountId = user ? user.Account.id : undefined
const predefinedReasonId = predefinedReason ? abusePredefinedReasonsMap[predefinedReason] : undefined
const queryOptions: BuildAbusesQueryOptions = {
start,
count,
sort,
id,
filter,
predefinedReasonId,
search,
state,
videoIs,
searchReportee,
searchVideo,
searchVideoChannel,
searchReporter,
serverAccountId,
userAccountId
}
const [ total, data ] = await Promise.all([
AbuseModel.internalCountForApi(queryOptions),
AbuseModel.internalListForApi(queryOptions)
])
return { total, data }
}
static async listForUserApi (parameters: {
user: MUserAccountId
start: number
count: number
sort: string
id?: number
search?: string
state?: AbuseStateType
}) {
const {
start,
count,
sort,
search,
user,
state,
id
} = parameters
const queryOptions: BuildAbusesQueryOptions = {
start,
count,
sort,
id,
search,
state,
reporterAccountId: user.Account.id
}
const [ total, data ] = await Promise.all([
AbuseModel.internalCountForApi(queryOptions),
AbuseModel.internalListForApi(queryOptions)
])
return { total, data }
}
// ---------------------------------------------------------------------------
static getStats () {
const query = `SELECT ` +
`AVG(EXTRACT(EPOCH FROM ("processedAt" - "createdAt") * 1000)) ` +
`FILTER (WHERE "processedAt" IS NOT NULL AND "createdAt" > CURRENT_DATE - INTERVAL '3 months')` +
`AS "avgResponseTime", ` +
// "processedAt" has been introduced in PeerTube 6.1 so also check the abuse state to check processed abuses
`COUNT(*) FILTER (WHERE "processedAt" IS NOT NULL OR "state" != ${AbuseState.PENDING}) AS "processedAbuses", ` +
`COUNT(*) AS "totalAbuses" ` +
`FROM "abuse"`
return AbuseModel.sequelize.query<any>(query, {
type: QueryTypes.SELECT,
raw: true
}).then(([ row ]) => {
return {
totalAbuses: parseAggregateResult(row.totalAbuses),
totalAbusesProcessed: parseAggregateResult(row.processedAbuses),
averageAbuseResponseTimeMs: row?.avgResponseTime
? forceNumber(row.avgResponseTime)
: null
}
})
}
// ---------------------------------------------------------------------------
buildBaseVideoCommentAbuse (this: MAbuseUserFormattable) {
// Associated video comment could have been destroyed if the video has been deleted
if (!this.VideoCommentAbuse?.VideoComment) return null
const entity = this.VideoCommentAbuse.VideoComment
return {
id: entity.id,
threadId: entity.getThreadId(),
text: entity.text ?? '',
deleted: entity.isDeleted(),
video: {
id: entity.Video.id,
name: entity.Video.name,
uuid: entity.Video.uuid
}
}
}
buildBaseVideoAbuse (this: MAbuseUserFormattable): UserVideoAbuse {
if (!this.VideoAbuse) return null
const abuseModel = this.VideoAbuse
const entity = abuseModel.Video || abuseModel.deletedVideo
return {
id: entity.id,
uuid: entity.uuid,
name: entity.name,
nsfw: entity.nsfw,
startAt: abuseModel.startAt,
endAt: abuseModel.endAt,
deleted: !abuseModel.Video,
blacklisted: abuseModel.Video?.isBlacklisted() || false,
thumbnailPath: abuseModel.Video?.getMiniatureStaticPath(),
channel: abuseModel.Video?.VideoChannel.toFormattedJSON() || abuseModel.deletedVideo?.channel
}
}
buildBaseAbuse (this: MAbuseUserFormattable, countMessages: number): UserAbuse {
const predefinedReasons = AbuseModel.getPredefinedReasonsStrings(this.predefinedReasons)
return {
id: this.id,
reason: this.reason,
predefinedReasons,
flaggedAccount: this.FlaggedAccount
? this.FlaggedAccount.toFormattedJSON()
: null,
state: {
id: this.state,
label: AbuseModel.getStateLabel(this.state)
},
countMessages,
createdAt: this.createdAt,
updatedAt: this.updatedAt
}
}
toFormattedAdminJSON (this: MAbuseAdminFormattable): AdminAbuse {
const countReportsForVideo = this.get('countReportsForVideo') as number
const nthReportForVideo = this.get('nthReportForVideo') as number
const countReportsForReporter = this.get('countReportsForReporter') as number
const countReportsForReportee = this.get('countReportsForReportee') as number
const countMessages = this.get('countMessages') as number
const baseVideo = this.buildBaseVideoAbuse()
const video: AdminVideoAbuse = baseVideo
? Object.assign(baseVideo, {
countReports: countReportsForVideo,
nthReport: nthReportForVideo
})
: null
const comment: AdminVideoCommentAbuse = this.buildBaseVideoCommentAbuse()
const abuse = this.buildBaseAbuse(countMessages || 0)
return Object.assign(abuse, {
video,
comment,
moderationComment: this.moderationComment,
reporterAccount: this.ReporterAccount
? this.ReporterAccount.toFormattedJSON()
: null,
countReportsForReporter: (countReportsForReporter || 0),
countReportsForReportee: (countReportsForReportee || 0)
})
}
toFormattedUserJSON (this: MAbuseUserFormattable): UserAbuse {
const countMessages = this.get('countMessages') as number
const video = this.buildBaseVideoAbuse()
const comment = this.buildBaseVideoCommentAbuse()
const abuse = this.buildBaseAbuse(countMessages || 0)
return Object.assign(abuse, {
video,
comment
})
}
toActivityPubObject (this: MAbuseAP): AbuseObject {
const predefinedReasons = AbuseModel.getPredefinedReasonsStrings(this.predefinedReasons)
const object = this.VideoAbuse?.Video?.url || this.VideoCommentAbuse?.VideoComment?.url || this.FlaggedAccount.Actor.url
const startAt = this.VideoAbuse?.startAt
const endAt = this.VideoAbuse?.endAt
return {
type: 'Flag' as 'Flag',
content: this.reason,
mediaType: 'text/markdown',
object,
tag: predefinedReasons.map(r => ({
type: 'Hashtag' as 'Hashtag',
name: r
})),
startAt,
endAt
}
}
private static async internalCountForApi (parameters: BuildAbusesQueryOptions) {
const { query, replacements } = buildAbuseListQuery(parameters, 'count')
const options = {
type: QueryTypes.SELECT as QueryTypes.SELECT,
replacements
}
const [ { total } ] = await AbuseModel.sequelize.query<{ total: string }>(query, options)
if (total === null) return 0
return parseInt(total, 10)
}
private static async internalListForApi (parameters: BuildAbusesQueryOptions) {
const { query, replacements } = buildAbuseListQuery(parameters, 'id')
const options = {
type: QueryTypes.SELECT as QueryTypes.SELECT,
replacements
}
const rows = await AbuseModel.sequelize.query<{ id: string }>(query, options)
const ids = rows.map(r => r.id)
if (ids.length === 0) return []
return AbuseModel.scope(ScopeNames.FOR_API)
.findAll({
order: getSort(parameters.sort),
where: {
id: {
[Op.in]: ids
}
},
limit: parameters.count
})
}
private static getStateLabel (id: number) {
return ABUSE_STATES[id] || 'Unknown'
}
private static getPredefinedReasonsStrings (predefinedReasons: AbusePredefinedReasonsType[]): AbusePredefinedReasonsString[] {
const invertedPredefinedReasons = invert(abusePredefinedReasonsMap)
return (predefinedReasons || [])
.map(r => invertedPredefinedReasons[r] as AbusePredefinedReasonsString)
.filter(v => !!v)
}
}
+166
ファイルの表示
@@ -0,0 +1,166 @@
import { forceNumber } from '@peertube/peertube-core-utils'
import { AbuseFilter, AbuseStateType, AbuseVideoIs } from '@peertube/peertube-models'
import { exists } from '@server/helpers/custom-validators/misc.js'
import { buildBlockedAccountSQL, buildSortDirectionAndField } from '../../shared/index.js'
export type BuildAbusesQueryOptions = {
start: number
count: number
sort: string
// search
search?: string
searchReporter?: string
searchReportee?: string
// video related
searchVideo?: string
searchVideoChannel?: string
videoIs?: AbuseVideoIs
// filters
id?: number
predefinedReasonId?: number
filter?: AbuseFilter
state?: AbuseStateType
// accountIds
serverAccountId?: number
userAccountId?: number
reporterAccountId?: number
}
function buildAbuseListQuery (options: BuildAbusesQueryOptions, type: 'count' | 'id') {
const whereAnd: string[] = []
const replacements: any = {}
const joins = [
'LEFT JOIN "videoAbuse" ON "videoAbuse"."abuseId" = "abuse"."id"',
'LEFT JOIN "video" ON "videoAbuse"."videoId" = "video"."id"',
'LEFT JOIN "videoBlacklist" ON "videoBlacklist"."videoId" = "video"."id"',
'LEFT JOIN "videoChannel" ON "video"."channelId" = "videoChannel"."id"',
'LEFT JOIN "account" "reporterAccount" ON "reporterAccount"."id" = "abuse"."reporterAccountId"',
'LEFT JOIN "account" "flaggedAccount" ON "flaggedAccount"."id" = "abuse"."flaggedAccountId"',
'LEFT JOIN "commentAbuse" ON "commentAbuse"."abuseId" = "abuse"."id"',
'LEFT JOIN "videoComment" ON "commentAbuse"."videoCommentId" = "videoComment"."id"'
]
if (options.serverAccountId || options.userAccountId) {
whereAnd.push(
'"abuse"."reporterAccountId" IS NULL OR ' +
'"abuse"."reporterAccountId" NOT IN (' + buildBlockedAccountSQL([ options.serverAccountId, options.userAccountId ]) + ')'
)
}
if (options.reporterAccountId) {
whereAnd.push('"abuse"."reporterAccountId" = :reporterAccountId')
replacements.reporterAccountId = options.reporterAccountId
}
if (options.search) {
const searchWhereOr = [
'"video"."name" ILIKE :search',
'"videoChannel"."name" ILIKE :search',
`"videoAbuse"."deletedVideo"->>'name' ILIKE :search`,
`"videoAbuse"."deletedVideo"->'channel'->>'displayName' ILIKE :search`,
'"reporterAccount"."name" ILIKE :search',
'"flaggedAccount"."name" ILIKE :search'
]
replacements.search = `%${options.search}%`
whereAnd.push('(' + searchWhereOr.join(' OR ') + ')')
}
if (options.searchVideo) {
whereAnd.push('"video"."name" ILIKE :searchVideo')
replacements.searchVideo = `%${options.searchVideo}%`
}
if (options.searchVideoChannel) {
whereAnd.push('"videoChannel"."name" ILIKE :searchVideoChannel')
replacements.searchVideoChannel = `%${options.searchVideoChannel}%`
}
if (options.id) {
whereAnd.push('"abuse"."id" = :id')
replacements.id = options.id
}
if (options.state) {
whereAnd.push('"abuse"."state" = :state')
replacements.state = options.state
}
if (options.videoIs === 'deleted') {
whereAnd.push('"videoAbuse"."deletedVideo" IS NOT NULL')
} else if (options.videoIs === 'blacklisted') {
whereAnd.push('"videoBlacklist"."id" IS NOT NULL')
}
if (options.predefinedReasonId) {
whereAnd.push(':predefinedReasonId = ANY("abuse"."predefinedReasons")')
replacements.predefinedReasonId = options.predefinedReasonId
}
if (options.filter === 'video') {
whereAnd.push('"videoAbuse"."id" IS NOT NULL')
} else if (options.filter === 'comment') {
whereAnd.push('"commentAbuse"."id" IS NOT NULL')
} else if (options.filter === 'account') {
whereAnd.push('"videoAbuse"."id" IS NULL AND "commentAbuse"."id" IS NULL')
}
if (options.searchReporter) {
whereAnd.push('"reporterAccount"."name" ILIKE :searchReporter')
replacements.searchReporter = `%${options.searchReporter}%`
}
if (options.searchReportee) {
whereAnd.push('"flaggedAccount"."name" ILIKE :searchReportee')
replacements.searchReportee = `%${options.searchReportee}%`
}
const prefix = type === 'count'
? 'SELECT COUNT("abuse"."id") AS "total"'
: 'SELECT "abuse"."id" '
let suffix = ''
if (type !== 'count') {
if (options.sort) {
const order = buildAbuseOrder(options.sort)
suffix += `${order} `
}
if (exists(options.count)) {
const count = forceNumber(options.count)
suffix += `LIMIT ${count} `
}
if (exists(options.start)) {
const start = forceNumber(options.start)
suffix += `OFFSET ${start} `
}
}
const where = whereAnd.length !== 0
? `WHERE ${whereAnd.join(' AND ')}`
: ''
return {
query: `${prefix} FROM "abuse" ${joins.join(' ')} ${where} ${suffix}`,
replacements
}
}
function buildAbuseOrder (value: string) {
const { direction, field } = buildSortDirectionAndField(value)
return `ORDER BY "abuse"."${field}" ${direction}`
}
export {
buildAbuseListQuery
}
+64
ファイルの表示
@@ -0,0 +1,64 @@
import { type VideoDetails } from '@peertube/peertube-models'
import { AllowNull, BelongsTo, Column, CreatedAt, DataType, Default, ForeignKey, Table, UpdatedAt } from 'sequelize-typescript'
import { VideoModel } from '../video/video.js'
import { AbuseModel } from './abuse.js'
import { SequelizeModel } from '../shared/index.js'
@Table({
tableName: 'videoAbuse',
indexes: [
{
fields: [ 'abuseId' ]
},
{
fields: [ 'videoId' ]
}
]
})
export class VideoAbuseModel extends SequelizeModel<VideoAbuseModel> {
@CreatedAt
createdAt: Date
@UpdatedAt
updatedAt: Date
@AllowNull(true)
@Default(null)
@Column
startAt: number
@AllowNull(true)
@Default(null)
@Column
endAt: number
@AllowNull(true)
@Default(null)
@Column(DataType.JSONB)
deletedVideo: VideoDetails
@ForeignKey(() => AbuseModel)
@Column
abuseId: number
@BelongsTo(() => AbuseModel, {
foreignKey: {
allowNull: false
},
onDelete: 'cascade'
})
Abuse: Awaited<AbuseModel>
@ForeignKey(() => VideoModel)
@Column
videoId: number
@BelongsTo(() => VideoModel, {
foreignKey: {
allowNull: true
},
onDelete: 'set null'
})
Video: Awaited<VideoModel>
}
+48
ファイルの表示
@@ -0,0 +1,48 @@
import { BelongsTo, Column, CreatedAt, ForeignKey, Table, UpdatedAt } from 'sequelize-typescript'
import { VideoCommentModel } from '../video/video-comment.js'
import { AbuseModel } from './abuse.js'
import { SequelizeModel } from '../shared/index.js'
@Table({
tableName: 'commentAbuse',
indexes: [
{
fields: [ 'abuseId' ]
},
{
fields: [ 'videoCommentId' ]
}
]
})
export class VideoCommentAbuseModel extends SequelizeModel<VideoCommentAbuseModel> {
@CreatedAt
createdAt: Date
@UpdatedAt
updatedAt: Date
@ForeignKey(() => AbuseModel)
@Column
abuseId: number
@BelongsTo(() => AbuseModel, {
foreignKey: {
allowNull: false
},
onDelete: 'cascade'
})
Abuse: Awaited<AbuseModel>
@ForeignKey(() => VideoCommentModel)
@Column
videoCommentId: number
@BelongsTo(() => VideoCommentModel, {
foreignKey: {
allowNull: true
},
onDelete: 'set null'
})
VideoComment: Awaited<VideoCommentModel>
}
+242
ファイルの表示
@@ -0,0 +1,242 @@
import { FindOptions, Op, QueryTypes } from 'sequelize'
import { BelongsTo, Column, CreatedAt, ForeignKey, Table, UpdatedAt } from 'sequelize-typescript'
import { AccountBlock } from '@peertube/peertube-models'
import { handlesToNameAndHost } from '@server/helpers/actors.js'
import { MAccountBlocklist, MAccountBlocklistFormattable } from '@server/types/models/index.js'
import { ActorModel } from '../actor/actor.js'
import { ServerModel } from '../server/server.js'
import { SequelizeModel, createSafeIn, getSort, searchAttribute } from '../shared/index.js'
import { AccountModel } from './account.js'
import { WEBSERVER } from '@server/initializers/constants.js'
@Table({
tableName: 'accountBlocklist',
indexes: [
{
fields: [ 'accountId', 'targetAccountId' ],
unique: true
},
{
fields: [ 'targetAccountId' ]
}
]
})
export class AccountBlocklistModel extends SequelizeModel<AccountBlocklistModel> {
@CreatedAt
createdAt: Date
@UpdatedAt
updatedAt: Date
@ForeignKey(() => AccountModel)
@Column
accountId: number
@BelongsTo(() => AccountModel, {
foreignKey: {
name: 'accountId',
allowNull: false
},
as: 'ByAccount',
onDelete: 'CASCADE'
})
ByAccount: Awaited<AccountModel>
@ForeignKey(() => AccountModel)
@Column
targetAccountId: number
@BelongsTo(() => AccountModel, {
foreignKey: {
name: 'targetAccountId',
allowNull: false
},
as: 'BlockedAccount',
onDelete: 'CASCADE'
})
BlockedAccount: Awaited<AccountModel>
static isAccountMutedByAccounts (accountIds: number[], targetAccountId: number) {
const query = {
attributes: [ 'accountId', 'id' ],
where: {
accountId: {
[Op.in]: accountIds
},
targetAccountId
},
raw: true
}
return AccountBlocklistModel.unscoped()
.findAll(query)
.then(rows => {
const result: { [accountId: number]: boolean } = {}
for (const accountId of accountIds) {
result[accountId] = !!rows.find(r => r.accountId === accountId)
}
return result
})
}
static loadByAccountAndTarget (accountId: number, targetAccountId: number): Promise<MAccountBlocklist> {
const query = {
where: {
accountId,
targetAccountId
}
}
return AccountBlocklistModel.findOne(query)
}
static listForApi (parameters: {
start: number
count: number
sort: string
search?: string
accountId: number
}) {
const { start, count, sort, search, accountId } = parameters
const getQuery = (forCount: boolean) => {
const query: FindOptions = {
offset: start,
limit: count,
order: getSort(sort),
where: { accountId }
}
if (search) {
Object.assign(query.where, {
[Op.or]: [
searchAttribute(search, '$BlockedAccount.name$'),
searchAttribute(search, '$BlockedAccount.Actor.url$')
]
})
}
if (forCount !== true) {
query.include = [
{
model: AccountModel,
required: true,
as: 'ByAccount'
},
{
model: AccountModel,
required: true,
as: 'BlockedAccount'
}
]
} else if (search) { // We need some joins when counting with search
query.include = [
{
model: AccountModel.unscoped(),
required: true,
as: 'BlockedAccount',
include: [
{
model: ActorModel.unscoped(),
required: true
}
]
}
]
}
return query
}
return Promise.all([
AccountBlocklistModel.count(getQuery(true)),
AccountBlocklistModel.findAll(getQuery(false))
]).then(([ total, data ]) => ({ total, data }))
}
static listHandlesBlockedBy (accountIds: number[]): Promise<string[]> {
const query = {
attributes: [ 'id' ],
where: {
accountId: {
[Op.in]: accountIds
}
},
include: [
{
attributes: [ 'id' ],
model: AccountModel.unscoped(),
required: true,
as: 'BlockedAccount',
include: [
{
attributes: [ 'preferredUsername' ],
model: ActorModel.unscoped(),
required: true,
include: [
{
attributes: [ 'host' ],
model: ServerModel.unscoped(),
required: false
}
]
}
]
}
]
}
return AccountBlocklistModel.findAll(query)
.then(entries => {
return entries.map(e => {
const host = e.BlockedAccount.Actor.Server?.host ?? WEBSERVER.HOST
return `${e.BlockedAccount.Actor.preferredUsername}@${host}`
})
})
}
static getBlockStatus (byAccountIds: number[], handles: string[]): Promise<{ name: string, host: string, accountId: number }[]> {
const sanitizedHandles = handlesToNameAndHost(handles)
const localHandles = sanitizedHandles.filter(h => !h.host)
.map(h => h.name)
const remoteHandles = sanitizedHandles.filter(h => !!h.host)
.map(h => ([ h.name, h.host ]))
const handlesWhere: string[] = []
if (localHandles.length !== 0) {
handlesWhere.push(`("actor"."preferredUsername" IN (:localHandles) AND "server"."id" IS NULL)`)
}
if (remoteHandles.length !== 0) {
handlesWhere.push(`(("actor"."preferredUsername", "server"."host") IN (:remoteHandles))`)
}
const rawQuery = `SELECT "accountBlocklist"."accountId", "actor"."preferredUsername" AS "name", "server"."host" ` +
`FROM "accountBlocklist" ` +
`INNER JOIN "account" ON "account"."id" = "accountBlocklist"."targetAccountId" ` +
`INNER JOIN "actor" ON "actor"."id" = "account"."actorId" ` +
`LEFT JOIN "server" ON "server"."id" = "actor"."serverId" ` +
`WHERE "accountBlocklist"."accountId" IN (${createSafeIn(AccountBlocklistModel.sequelize, byAccountIds)}) ` +
`AND (${handlesWhere.join(' OR ')})`
return AccountBlocklistModel.sequelize.query(rawQuery, {
type: QueryTypes.SELECT as QueryTypes.SELECT,
replacements: { byAccountIds, localHandles, remoteHandles }
})
}
toFormattedJSON (this: MAccountBlocklistFormattable): AccountBlock {
return {
byAccount: this.ByAccount.toFormattedJSON(),
blockedAccount: this.BlockedAccount.toFormattedJSON(),
createdAt: this.createdAt
}
}
}
+280
ファイルの表示
@@ -0,0 +1,280 @@
import { AccountVideoRate, type VideoRateType } from '@peertube/peertube-models'
import {
MAccountVideoRate,
MAccountVideoRateAccountUrl,
MAccountVideoRateAccountVideo,
MAccountVideoRateFormattable,
MAccountVideoRateVideoUrl
} from '@server/types/models/index.js'
import { FindOptions, Op, QueryTypes, Transaction } from 'sequelize'
import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Is, Table, UpdatedAt } from 'sequelize-typescript'
import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc.js'
import { CONSTRAINTS_FIELDS, USER_EXPORT_MAX_ITEMS, VIDEO_RATE_TYPES } from '../../initializers/constants.js'
import { ActorModel } from '../actor/actor.js'
import { SequelizeModel, getSort, throwIfNotValid } from '../shared/index.js'
import { SummaryOptions, VideoChannelModel, ScopeNames as VideoChannelScopeNames } from '../video/video-channel.js'
import { VideoModel } from '../video/video.js'
import { AccountModel } from './account.js'
/*
Account rates per video.
*/
@Table({
tableName: 'accountVideoRate',
indexes: [
{
fields: [ 'videoId', 'accountId' ],
unique: true
},
{
fields: [ 'videoId' ]
},
{
fields: [ 'accountId' ]
},
{
fields: [ 'videoId', 'type' ]
},
{
fields: [ 'url' ],
unique: true
}
]
})
export class AccountVideoRateModel extends SequelizeModel<AccountVideoRateModel> {
@AllowNull(false)
@Column(DataType.ENUM(...Object.values(VIDEO_RATE_TYPES)))
type: VideoRateType
@AllowNull(false)
@Is('AccountVideoRateUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
@Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_RATES.URL.max))
url: string
@CreatedAt
createdAt: Date
@UpdatedAt
updatedAt: Date
@ForeignKey(() => VideoModel)
@Column
videoId: number
@BelongsTo(() => VideoModel, {
foreignKey: {
allowNull: false
},
onDelete: 'CASCADE'
})
Video: Awaited<VideoModel>
@ForeignKey(() => AccountModel)
@Column
accountId: number
@BelongsTo(() => AccountModel, {
foreignKey: {
allowNull: false
},
onDelete: 'CASCADE'
})
Account: Awaited<AccountModel>
static load (accountId: number, videoId: number, transaction?: Transaction): Promise<MAccountVideoRate> {
const options: FindOptions = {
where: {
accountId,
videoId
}
}
if (transaction) options.transaction = transaction
return AccountVideoRateModel.findOne(options)
}
static loadByAccountAndVideoOrUrl (accountId: number, videoId: number, url: string, t?: Transaction): Promise<MAccountVideoRate> {
const options: FindOptions = {
where: {
[Op.or]: [
{
accountId,
videoId
},
{
url
}
]
}
}
if (t) options.transaction = t
return AccountVideoRateModel.findOne(options)
}
static loadLocalAndPopulateVideo (
rateType: VideoRateType,
accountName: string,
videoId: number,
t?: Transaction
): Promise<MAccountVideoRateAccountVideo> {
const options: FindOptions = {
where: {
videoId,
type: rateType
},
include: [
{
model: AccountModel.unscoped(),
required: true,
include: [
{
attributes: [ 'id', 'url', 'followersUrl', 'preferredUsername' ],
model: ActorModel.unscoped(),
required: true,
where: {
[Op.and]: [
ActorModel.wherePreferredUsername(accountName),
{ serverId: null }
]
}
}
]
},
{
model: VideoModel.unscoped(),
required: true
}
]
}
if (t) options.transaction = t
return AccountVideoRateModel.findOne(options)
}
static loadByUrl (url: string, transaction: Transaction) {
const options: FindOptions = {
where: {
url
}
}
if (transaction) options.transaction = transaction
return AccountVideoRateModel.findOne(options)
}
// ---------------------------------------------------------------------------
static listByAccountForApi (options: {
start: number
count: number
sort: string
type?: string
accountId: number
}) {
const getQuery = (forCount: boolean) => {
const query: FindOptions = {
offset: options.start,
limit: options.count,
order: getSort(options.sort),
where: {
accountId: options.accountId
}
}
if (options.type) query.where['type'] = options.type
if (forCount !== true) {
query.include = [
{
model: VideoModel,
required: true,
include: [
{
model: VideoChannelModel.scope({ method: [ VideoChannelScopeNames.SUMMARY, { withAccount: true } as SummaryOptions ] }),
required: true
}
]
}
]
}
return query
}
return Promise.all([
AccountVideoRateModel.count(getQuery(true)),
AccountVideoRateModel.findAll(getQuery(false))
]).then(([ total, data ]) => ({ total, data }))
}
static listRemoteRateUrlsOfLocalVideos () {
const query = `SELECT "accountVideoRate".url FROM "accountVideoRate" ` +
`INNER JOIN account ON account.id = "accountVideoRate"."accountId" ` +
`INNER JOIN actor ON actor.id = account."actorId" AND actor."serverId" IS NOT NULL ` +
`INNER JOIN video ON video.id = "accountVideoRate"."videoId" AND video.remote IS FALSE`
return AccountVideoRateModel.sequelize.query<{ url: string }>(query, {
type: QueryTypes.SELECT,
raw: true
}).then(rows => rows.map(r => r.url))
}
static listAndCountAccountUrlsByVideoId (rateType: VideoRateType, videoId: number, start: number, count: number, t?: Transaction) {
const query = {
offset: start,
limit: count,
where: {
videoId,
type: rateType
},
transaction: t,
include: [
{
attributes: [ 'actorId' ],
model: AccountModel.unscoped(),
required: true,
include: [
{
attributes: [ 'url' ],
model: ActorModel.unscoped(),
required: true
}
]
}
]
}
return Promise.all([
AccountVideoRateModel.count(query),
AccountVideoRateModel.findAll<MAccountVideoRateAccountUrl>(query)
]).then(([ total, data ]) => ({ total, data }))
}
static listRatesOfAccountIdForExport (accountId: number, rateType: VideoRateType): Promise<MAccountVideoRateVideoUrl[]> {
return AccountVideoRateModel.findAll({
where: {
accountId,
type: rateType
},
include: [
{
attributes: [ 'url' ],
model: VideoModel,
required: true
}
],
limit: USER_EXPORT_MAX_ITEMS
})
}
// ---------------------------------------------------------------------------
toFormattedJSON (this: MAccountVideoRateFormattable): AccountVideoRate {
return {
video: this.Video.toFormattedJSON(),
rating: this.type
}
}
}
+486
ファイルの表示
@@ -0,0 +1,486 @@
import { Account, AccountSummary } from '@peertube/peertube-models'
import { ModelCache } from '@server/models/shared/model-cache.js'
import { FindOptions, IncludeOptions, Includeable, Op, Transaction, WhereOptions } from 'sequelize'
import {
AllowNull,
BeforeDestroy,
BelongsTo, Column,
CreatedAt,
DataType,
Default,
DefaultScope,
ForeignKey,
HasMany,
Is, Scopes,
Table,
UpdatedAt
} from 'sequelize-typescript'
import { isAccountDescriptionValid } from '../../helpers/custom-validators/accounts.js'
import { CONSTRAINTS_FIELDS, SERVER_ACTOR_NAME, WEBSERVER } from '../../initializers/constants.js'
import { sendDeleteActor } from '../../lib/activitypub/send/send-delete.js'
import {
MAccount, MAccountAP,
MAccountDefault,
MAccountFormattable,
MAccountHost,
MAccountSummaryFormattable,
MChannelHost
} from '../../types/models/index.js'
import { ActorFollowModel } from '../actor/actor-follow.js'
import { ActorImageModel } from '../actor/actor-image.js'
import { ActorModel } from '../actor/actor.js'
import { ApplicationModel } from '../application/application.js'
import { AccountAutomaticTagPolicyModel } from '../automatic-tag/account-automatic-tag-policy.js'
import { CommentAutomaticTagModel } from '../automatic-tag/comment-automatic-tag.js'
import { VideoAutomaticTagModel } from '../automatic-tag/video-automatic-tag.js'
import { ServerBlocklistModel } from '../server/server-blocklist.js'
import { ServerModel } from '../server/server.js'
import { SequelizeModel, buildSQLAttributes, getSort, throwIfNotValid } from '../shared/index.js'
import { UserModel } from '../user/user.js'
import { VideoChannelModel } from '../video/video-channel.js'
import { VideoCommentModel } from '../video/video-comment.js'
import { VideoPlaylistModel } from '../video/video-playlist.js'
import { VideoModel } from '../video/video.js'
import { AccountBlocklistModel } from './account-blocklist.js'
export enum ScopeNames {
SUMMARY = 'SUMMARY'
}
export type SummaryOptions = {
actorRequired?: boolean // Default: true
whereActor?: WhereOptions
whereServer?: WhereOptions
withAccountBlockerIds?: number[]
forCount?: boolean
}
@DefaultScope(() => ({
include: [
{
model: ActorModel, // Default scope includes avatar and server
required: true
}
]
}))
@Scopes(() => ({
[ScopeNames.SUMMARY]: (options: SummaryOptions = {}) => {
const serverInclude: IncludeOptions = {
attributes: [ 'host' ],
model: ServerModel.unscoped(),
required: !!options.whereServer,
where: options.whereServer
}
const actorInclude: Includeable = {
attributes: [ 'id', 'preferredUsername', 'url', 'serverId' ],
model: ActorModel.unscoped(),
required: options.actorRequired ?? true,
where: options.whereActor,
include: [ serverInclude ]
}
if (options.forCount !== true) {
actorInclude.include.push({
model: ActorImageModel,
as: 'Avatars',
required: false
})
}
const queryInclude: Includeable[] = [
actorInclude
]
const query: FindOptions = {
attributes: [ 'id', 'name', 'actorId' ]
}
if (options.withAccountBlockerIds) {
queryInclude.push({
attributes: [ 'id' ],
model: AccountBlocklistModel.unscoped(),
as: 'BlockedBy',
required: false,
where: {
accountId: {
[Op.in]: options.withAccountBlockerIds
}
}
})
serverInclude.include = [
{
attributes: [ 'id' ],
model: ServerBlocklistModel.unscoped(),
required: false,
where: {
accountId: {
[Op.in]: options.withAccountBlockerIds
}
}
}
]
}
query.include = queryInclude
return query
}
}))
@Table({
tableName: 'account',
indexes: [
{
fields: [ 'actorId' ],
unique: true
},
{
fields: [ 'applicationId' ]
},
{
fields: [ 'userId' ]
}
]
})
export class AccountModel extends SequelizeModel<AccountModel> {
@AllowNull(false)
@Column
name: string
@AllowNull(true)
@Default(null)
@Is('AccountDescription', value => throwIfNotValid(value, isAccountDescriptionValid, 'description', true))
@Column(DataType.STRING(CONSTRAINTS_FIELDS.USERS.DESCRIPTION.max))
description: string
@CreatedAt
createdAt: Date
@UpdatedAt
updatedAt: Date
@ForeignKey(() => ActorModel)
@Column
actorId: number
@BelongsTo(() => ActorModel, {
foreignKey: {
allowNull: false
},
onDelete: 'cascade'
})
Actor: Awaited<ActorModel>
@ForeignKey(() => UserModel)
@Column
userId: number
@BelongsTo(() => UserModel, {
foreignKey: {
allowNull: true
},
onDelete: 'cascade'
})
User: Awaited<UserModel>
@ForeignKey(() => ApplicationModel)
@Column
applicationId: number
@BelongsTo(() => ApplicationModel, {
foreignKey: {
allowNull: true
},
onDelete: 'cascade'
})
Application: Awaited<ApplicationModel>
@HasMany(() => VideoChannelModel, {
foreignKey: {
allowNull: false
},
onDelete: 'cascade',
hooks: true
})
VideoChannels: Awaited<VideoChannelModel>[]
@HasMany(() => VideoPlaylistModel, {
foreignKey: {
allowNull: false
},
onDelete: 'cascade',
hooks: true
})
VideoPlaylists: Awaited<VideoPlaylistModel>[]
@HasMany(() => VideoCommentModel, {
foreignKey: {
allowNull: true
},
onDelete: 'cascade',
hooks: true
})
VideoComments: Awaited<VideoCommentModel>[]
@HasMany(() => AccountBlocklistModel, {
foreignKey: {
name: 'targetAccountId',
allowNull: false
},
as: 'BlockedBy',
onDelete: 'CASCADE'
})
BlockedBy: Awaited<AccountBlocklistModel>[]
@HasMany(() => AccountAutomaticTagPolicyModel, {
foreignKey: {
name: 'accountId',
allowNull: false
},
onDelete: 'cascade'
})
AccountAutomaticTagPolicies: Awaited<AccountAutomaticTagPolicyModel>[]
@HasMany(() => CommentAutomaticTagModel, {
foreignKey: 'accountId',
onDelete: 'CASCADE'
})
CommentAutomaticTags: Awaited<CommentAutomaticTagModel>[]
@HasMany(() => VideoAutomaticTagModel, {
foreignKey: 'accountId',
onDelete: 'CASCADE'
})
VideoAutomaticTags: Awaited<VideoAutomaticTagModel>[]
@BeforeDestroy
static async sendDeleteIfOwned (instance: AccountModel, options) {
if (!instance.Actor) {
instance.Actor = await instance.$get('Actor', { transaction: options.transaction })
}
await ActorFollowModel.removeFollowsOf(instance.Actor.id, options.transaction)
if (instance.isOwned()) {
return sendDeleteActor(instance.Actor, options.transaction)
}
return undefined
}
// ---------------------------------------------------------------------------
static getSQLAttributes (tableName: string, aliasPrefix = '') {
return buildSQLAttributes({
model: this,
tableName,
aliasPrefix
})
}
// ---------------------------------------------------------------------------
static load (id: number, transaction?: Transaction): Promise<MAccountDefault> {
return AccountModel.findByPk(id, { transaction })
}
static loadByNameWithHost (nameWithHost: string): Promise<MAccountDefault> {
const [ accountName, host ] = nameWithHost.split('@')
if (!host || host === WEBSERVER.HOST) return AccountModel.loadLocalByName(accountName)
return AccountModel.loadByNameAndHost(accountName, host)
}
static loadLocalByName (name: string): Promise<MAccountDefault> {
const fun = () => {
const query = {
where: {
[Op.or]: [
{
userId: {
[Op.ne]: null
}
},
{
applicationId: {
[Op.ne]: null
}
}
]
},
include: [
{
model: ActorModel,
required: true,
where: ActorModel.wherePreferredUsername(name)
}
]
}
return AccountModel.findOne(query)
}
return ModelCache.Instance.doCache({
cacheType: 'server-account',
key: name,
fun,
// The server actor never change, so we can easily cache it
whitelist: () => name === SERVER_ACTOR_NAME
})
}
static loadByNameAndHost (name: string, host: string): Promise<MAccountDefault> {
const query = {
include: [
{
model: ActorModel,
required: true,
where: ActorModel.wherePreferredUsername(name),
include: [
{
model: ServerModel,
required: true,
where: {
host
}
}
]
}
]
}
return AccountModel.findOne(query)
}
static loadByUrl (url: string, transaction?: Transaction): Promise<MAccountDefault> {
const query = {
include: [
{
model: ActorModel,
required: true,
where: {
url
}
}
],
transaction
}
return AccountModel.findOne(query)
}
static listForApi (start: number, count: number, sort: string) {
const query = {
offset: start,
limit: count,
order: getSort(sort)
}
return Promise.all([
AccountModel.count(),
AccountModel.findAll(query)
]).then(([ total, data ]) => ({ total, data }))
}
static loadAccountIdFromVideo (videoId: number): Promise<MAccount> {
const query = {
include: [
{
attributes: [ 'id', 'accountId' ],
model: VideoChannelModel.unscoped(),
required: true,
include: [
{
attributes: [ 'id', 'channelId' ],
model: VideoModel.unscoped(),
where: {
id: videoId
}
}
]
}
]
}
return AccountModel.findOne(query)
}
static listLocalsForSitemap (sort: string): Promise<MAccountHost[]> {
const query = {
attributes: [ ],
offset: 0,
order: getSort(sort),
include: [
{
attributes: [ 'preferredUsername', 'serverId' ],
model: ActorModel.unscoped(),
where: {
serverId: null
}
}
]
}
return AccountModel
.unscoped()
.findAll(query)
}
toFormattedJSON (this: MAccountFormattable): Account {
return {
...this.Actor.toFormattedJSON(false),
id: this.id,
displayName: this.getDisplayName(),
description: this.description,
updatedAt: this.updatedAt,
userId: this.userId ?? undefined
}
}
toFormattedSummaryJSON (this: MAccountSummaryFormattable): AccountSummary {
const actor = this.Actor.toFormattedSummaryJSON()
return {
id: this.id,
displayName: this.getDisplayName(),
name: actor.name,
url: actor.url,
host: actor.host,
avatars: actor.avatars
}
}
async toActivityPubObject (this: MAccountAP) {
const obj = await this.Actor.toActivityPubObject(this.name)
return Object.assign(obj, {
summary: this.description
})
}
isOwned () {
return this.Actor.isOwned()
}
isOutdated () {
return this.Actor.isOutdated()
}
getDisplayName () {
return this.name
}
// Avoid error when running this method on MAccount... | MChannel...
getClientUrl (this: MAccountHost | MChannelHost) {
return WEBSERVER.URL + '/a/' + this.Actor.getIdentifier() + '/video-channels'
}
isBlocked () {
return this.BlockedBy && this.BlockedBy.length !== 0
}
}
+70
ファイルの表示
@@ -0,0 +1,70 @@
import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Table, UpdatedAt } from 'sequelize-typescript'
import { CustomPage } from '@peertube/peertube-models'
import { ActorModel } from '../actor/actor.js'
import { getServerActor } from '../application/application.js'
import { SequelizeModel } from '../shared/index.js'
@Table({
tableName: 'actorCustomPage',
indexes: [
{
fields: [ 'actorId', 'type' ],
unique: true
}
]
})
export class ActorCustomPageModel extends SequelizeModel<ActorCustomPageModel> {
@AllowNull(true)
@Column(DataType.TEXT)
content: string
@AllowNull(false)
@Column
type: 'homepage'
@CreatedAt
createdAt: Date
@UpdatedAt
updatedAt: Date
@ForeignKey(() => ActorModel)
@Column
actorId: number
@BelongsTo(() => ActorModel, {
foreignKey: {
name: 'actorId',
allowNull: false
},
onDelete: 'cascade'
})
Actor: Awaited<ActorModel>
static async updateInstanceHomepage (content: string) {
const serverActor = await getServerActor()
return ActorCustomPageModel.upsert({
content,
actorId: serverActor.id,
type: 'homepage'
})
}
static async loadInstanceHomepage () {
const serverActor = await getServerActor()
return ActorCustomPageModel.findOne({
where: {
actorId: serverActor.id
}
})
}
toFormattedJSON (): CustomPage {
return {
content: this.content
}
}
}
+745
ファイルの表示
@@ -0,0 +1,745 @@
import { ActorFollow, type FollowState } from '@peertube/peertube-models'
import { isActivityPubUrlValid } from '@server/helpers/custom-validators/activitypub/misc.js'
import { afterCommitIfTransaction } from '@server/helpers/database-utils.js'
import { getServerActor } from '@server/models/application/application.js'
import {
MActor,
MActorFollowActors,
MActorFollowActorsDefault,
MActorFollowActorsDefaultSubscription,
MActorFollowFollowingHost,
MActorFollowFormattable,
MActorFollowSubscriptions
} from '@server/types/models/index.js'
import difference from 'lodash-es/difference.js'
import { Attributes, FindOptions, IncludeOptions, Includeable, Op, QueryTypes, Transaction, WhereAttributeHash } from 'sequelize'
import {
AfterCreate,
AfterDestroy,
AfterUpdate,
AllowNull,
BelongsTo,
Column,
CreatedAt,
DataType,
Default,
ForeignKey,
Is,
IsInt,
Max, Table,
UpdatedAt
} from 'sequelize-typescript'
import { logger } from '../../helpers/logger.js'
import {
ACTOR_FOLLOW_SCORE,
CONSTRAINTS_FIELDS,
FOLLOW_STATES,
SERVER_ACTOR_NAME,
SORTABLE_COLUMNS,
USER_EXPORT_MAX_ITEMS
} from '../../initializers/constants.js'
import { AccountModel } from '../account/account.js'
import { ServerModel } from '../server/server.js'
import { SequelizeModel, buildSQLAttributes, createSafeIn, getSort, searchAttribute, throwIfNotValid } from '../shared/index.js'
import { doesExist } from '../shared/query.js'
import { VideoChannelModel } from '../video/video-channel.js'
import { ActorModel, unusedActorAttributesForAPI } from './actor.js'
import { InstanceListFollowersQueryBuilder, ListFollowersOptions } from './sql/instance-list-followers-query-builder.js'
import { InstanceListFollowingQueryBuilder, ListFollowingOptions } from './sql/instance-list-following-query-builder.js'
@Table({
tableName: 'actorFollow',
indexes: [
{
fields: [ 'actorId' ]
},
{
fields: [ 'targetActorId' ]
},
{
fields: [ 'actorId', 'targetActorId' ],
unique: true
},
{
fields: [ 'score' ]
},
{
fields: [ 'url' ],
unique: true
}
]
})
export class ActorFollowModel extends SequelizeModel<ActorFollowModel> {
@AllowNull(false)
@Column(DataType.ENUM(...Object.values(FOLLOW_STATES)))
state: FollowState
@AllowNull(false)
@Default(ACTOR_FOLLOW_SCORE.BASE)
@IsInt
@Max(ACTOR_FOLLOW_SCORE.MAX)
@Column
score: number
// Allow null because we added this column in PeerTube v3, and don't want to generate fake URLs of remote follows
@AllowNull(true)
@Is('ActorFollowUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
@Column(DataType.STRING(CONSTRAINTS_FIELDS.COMMONS.URL.max))
url: string
@CreatedAt
createdAt: Date
@UpdatedAt
updatedAt: Date
@ForeignKey(() => ActorModel)
@Column
actorId: number
@BelongsTo(() => ActorModel, {
foreignKey: {
name: 'actorId',
allowNull: false
},
as: 'ActorFollower',
onDelete: 'CASCADE'
})
ActorFollower: Awaited<ActorModel>
@ForeignKey(() => ActorModel)
@Column
targetActorId: number
@BelongsTo(() => ActorModel, {
foreignKey: {
name: 'targetActorId',
allowNull: false
},
as: 'ActorFollowing',
onDelete: 'CASCADE'
})
ActorFollowing: Awaited<ActorModel>
@AfterCreate
@AfterUpdate
static incrementFollowerAndFollowingCount (instance: ActorFollowModel, options: any) {
return afterCommitIfTransaction(options.transaction, () => {
return Promise.all([
ActorModel.rebuildFollowsCount(instance.actorId, 'following'),
ActorModel.rebuildFollowsCount(instance.targetActorId, 'followers')
])
})
}
@AfterDestroy
static decrementFollowerAndFollowingCount (instance: ActorFollowModel, options: any) {
return afterCommitIfTransaction(options.transaction, () => {
return Promise.all([
ActorModel.rebuildFollowsCount(instance.actorId, 'following'),
ActorModel.rebuildFollowsCount(instance.targetActorId, 'followers')
])
})
}
// ---------------------------------------------------------------------------
static getSQLAttributes (tableName: string, aliasPrefix = '') {
return buildSQLAttributes({
model: this,
tableName,
aliasPrefix
})
}
// ---------------------------------------------------------------------------
/*
* @deprecated Use `findOrCreateCustom` instead
*/
static findOrCreate (): any {
throw new Error('Must not be called')
}
// findOrCreate has issues with actor follow hooks
static async findOrCreateCustom (options: {
byActor: MActor
targetActor: MActor
activityId: string
state: FollowState
transaction: Transaction
}): Promise<[ MActorFollowActors, boolean ]> {
const { byActor, targetActor, activityId, state, transaction } = options
let created = false
let actorFollow: MActorFollowActors = await ActorFollowModel.loadByActorAndTarget(byActor.id, targetActor.id, transaction)
if (!actorFollow) {
created = true
actorFollow = await ActorFollowModel.create({
actorId: byActor.id,
targetActorId: targetActor.id,
url: activityId,
state
}, { transaction })
actorFollow.ActorFollowing = targetActor
actorFollow.ActorFollower = byActor
}
return [ actorFollow, created ]
}
static removeFollowsOf (actorId: number, t?: Transaction) {
const query = {
where: {
[Op.or]: [
{
actorId
},
{
targetActorId: actorId
}
]
},
transaction: t
}
return ActorFollowModel.destroy(query)
}
// Remove actor follows with a score of 0 (too many requests where they were unreachable)
static async removeBadActorFollows () {
const actorFollows = await ActorFollowModel.listBadActorFollows()
const actorFollowsRemovePromises = actorFollows.map(actorFollow => actorFollow.destroy())
await Promise.all(actorFollowsRemovePromises)
const numberOfActorFollowsRemoved = actorFollows.length
if (numberOfActorFollowsRemoved) logger.info('Removed bad %d actor follows.', numberOfActorFollowsRemoved)
}
static isFollowedBy (actorId: number, followerActorId: number) {
const query = `SELECT 1 FROM "actorFollow" ` +
`WHERE "actorId" = $followerActorId AND "targetActorId" = $actorId AND "state" = 'accepted' ` +
`LIMIT 1`
return doesExist({ sequelize: this.sequelize, query, bind: { actorId, followerActorId } })
}
static loadByActorAndTarget (actorId: number, targetActorId: number, t?: Transaction): Promise<MActorFollowActorsDefault> {
const query = {
where: {
actorId,
targetActorId
},
include: [
{
model: ActorModel,
required: true,
as: 'ActorFollower'
},
{
model: ActorModel,
required: true,
as: 'ActorFollowing'
}
],
transaction: t
}
return ActorFollowModel.findOne(query)
}
static loadByActorAndTargetNameAndHostForAPI (options: {
actorId: number
targetName: string
targetHost: string
state?: FollowState
transaction?: Transaction
}): Promise<MActorFollowActorsDefaultSubscription> {
const { actorId, targetHost, targetName, state, transaction } = options
const actorFollowingPartInclude: IncludeOptions = {
model: ActorModel,
required: true,
as: 'ActorFollowing',
where: ActorModel.wherePreferredUsername(targetName),
include: [
{
model: VideoChannelModel.unscoped(),
required: false
}
]
}
if (targetHost === null) {
actorFollowingPartInclude.where['serverId'] = null
} else {
actorFollowingPartInclude.include.push({
model: ServerModel,
required: true,
where: {
host: targetHost
}
})
}
const where: WhereAttributeHash<Attributes<ActorFollowModel>> = { actorId }
if (state) where.state = state
const query: FindOptions<Attributes<ActorFollowModel>> = {
where,
include: [
actorFollowingPartInclude,
{
model: ActorModel,
required: true,
as: 'ActorFollower'
}
],
transaction
}
return ActorFollowModel.findOne(query)
}
static listSubscriptionsOf (actorId: number, targets: { name: string, host?: string }[]): Promise<MActorFollowFollowingHost[]> {
const whereTab = targets
.map(t => {
if (t.host) {
return {
[Op.and]: [
ActorModel.wherePreferredUsername(t.name),
{ $host$: t.host }
]
}
}
return {
[Op.and]: [
ActorModel.wherePreferredUsername(t.name),
{ $serverId$: null }
]
}
})
const query = {
attributes: [ 'id' ],
where: {
[Op.and]: [
{
[Op.or]: whereTab
},
{
state: 'accepted',
actorId
}
]
},
include: [
{
attributes: [ 'preferredUsername' ],
model: ActorModel.unscoped(),
required: true,
as: 'ActorFollowing',
include: [
{
attributes: [ 'host' ],
model: ServerModel.unscoped(),
required: false
}
]
}
]
}
return ActorFollowModel.findAll(query)
}
static listInstanceFollowingForApi (options: ListFollowingOptions) {
return Promise.all([
new InstanceListFollowingQueryBuilder(this.sequelize, options).countFollowing(),
new InstanceListFollowingQueryBuilder(this.sequelize, options).listFollowing()
]).then(([ total, data ]) => ({ total, data }))
}
static listFollowersForApi (options: ListFollowersOptions) {
return Promise.all([
new InstanceListFollowersQueryBuilder(this.sequelize, options).countFollowers(),
new InstanceListFollowersQueryBuilder(this.sequelize, options).listFollowers()
]).then(([ total, data ]) => ({ total, data }))
}
static listSubscriptionsForApi (options: {
actorId: number
start: number
count: number
sort: string
search?: string
}) {
const { actorId, start, count, sort } = options
const where = {
state: 'accepted',
actorId
}
if (options.search) {
Object.assign(where, {
[Op.or]: [
searchAttribute(options.search, '$ActorFollowing.preferredUsername$'),
searchAttribute(options.search, '$ActorFollowing.VideoChannel.name$')
]
})
}
const getQuery = (forCount: boolean) => {
let channelInclude: Includeable[] = []
if (forCount !== true) {
channelInclude = [
{
attributes: {
exclude: unusedActorAttributesForAPI
},
model: ActorModel,
required: true
},
{
model: AccountModel.unscoped(),
required: true,
include: [
{
attributes: {
exclude: unusedActorAttributesForAPI
},
model: ActorModel,
required: true
}
]
}
]
}
return {
attributes: forCount === true
? []
: SORTABLE_COLUMNS.USER_SUBSCRIPTIONS,
distinct: true,
offset: start,
limit: count,
order: getSort(sort),
where,
include: [
{
attributes: [ 'id' ],
model: ActorModel.unscoped(),
as: 'ActorFollowing',
required: true,
include: [
{
model: VideoChannelModel.unscoped(),
required: true,
include: channelInclude
}
]
}
]
}
}
return Promise.all([
ActorFollowModel.count(getQuery(true)),
ActorFollowModel.findAll<MActorFollowSubscriptions>(getQuery(false))
]).then(([ total, rows ]) => ({
total,
data: rows.map(r => r.ActorFollowing.VideoChannel)
}))
}
static async keepUnfollowedInstance (hosts: string[]) {
const followerId = (await getServerActor()).id
const query = {
attributes: [ 'id' ],
where: {
actorId: followerId
},
include: [
{
attributes: [ 'id' ],
model: ActorModel.unscoped(),
required: true,
as: 'ActorFollowing',
where: {
preferredUsername: SERVER_ACTOR_NAME
},
include: [
{
attributes: [ 'host' ],
model: ServerModel.unscoped(),
required: true,
where: {
host: {
[Op.in]: hosts
}
}
}
]
}
]
}
const res = await ActorFollowModel.findAll(query)
const followedHosts = res.map(row => row.ActorFollowing.Server.host)
return difference(hosts, followedHosts)
}
// ---------------------------------------------------------------------------
static listAcceptedFollowerUrlsForAP (actorIds: number[], t: Transaction, start?: number, count?: number) {
return ActorFollowModel.createListAcceptedFollowForApiQuery({ type: 'followers', actorIds, t, start, count })
.then(({ data, total }) => ({ total, data: data.map(d => d.selectionUrl) }))
}
static listAcceptedFollowerSharedInboxUrls (actorIds: number[], t: Transaction) {
return ActorFollowModel.createListAcceptedFollowForApiQuery({
type: 'followers',
actorIds,
t,
columnUrl: 'sharedInboxUrl',
distinct: true
}).then(({ data, total }) => ({ total, data: data.map(d => d.selectionUrl) }))
}
static async listAcceptedFollowersForExport (targetActorId: number) {
const data = await ActorFollowModel.findAll({
where: {
state: 'accepted',
targetActorId
},
include: [
{
attributes: [ 'preferredUsername', 'url' ],
model: ActorModel.unscoped(),
required: true,
as: 'ActorFollower',
include: [
{
attributes: [ 'host' ],
model: ServerModel.unscoped(),
required: false
}
]
}
],
limit: USER_EXPORT_MAX_ITEMS
})
return data.map(f => ({
createdAt: f.createdAt,
followerHandle: f.ActorFollower.getFullIdentifier(),
followerUrl: f.ActorFollower.url
}))
}
// ---------------------------------------------------------------------------
static listAcceptedFollowingUrlsForApi (actorIds: number[], t: Transaction, start?: number, count?: number) {
return ActorFollowModel.createListAcceptedFollowForApiQuery({ type: 'following', actorIds, t, start, count })
.then(({ data, total }) => ({ total, data: data.map(d => d.selectionUrl) }))
}
static async listAcceptedFollowingForExport (actorId: number) {
const data = await ActorFollowModel.findAll({
where: {
state: 'accepted',
actorId
},
include: [
{
attributes: [ 'preferredUsername', 'url' ],
model: ActorModel.unscoped(),
required: true,
as: 'ActorFollowing',
include: [
{
attributes: [ 'host' ],
model: ServerModel.unscoped(),
required: false
}
]
}
],
limit: USER_EXPORT_MAX_ITEMS
})
return data.map(f => ({
createdAt: f.createdAt,
followingHandle: f.ActorFollowing.getFullIdentifier(),
followingUrl: f.ActorFollowing.url
}))
}
// ---------------------------------------------------------------------------
static async getStats () {
const serverActor = await getServerActor()
const totalInstanceFollowing = await ActorFollowModel.count({
where: {
actorId: serverActor.id,
state: 'accepted'
}
})
const totalInstanceFollowers = await ActorFollowModel.count({
where: {
targetActorId: serverActor.id,
state: 'accepted'
}
})
return {
totalInstanceFollowing,
totalInstanceFollowers
}
}
static updateScore (inboxUrl: string, value: number, t?: Transaction) {
const query = `UPDATE "actorFollow" SET "score" = LEAST("score" + ${value}, ${ACTOR_FOLLOW_SCORE.MAX}) ` +
'WHERE id IN (' +
'SELECT "actorFollow"."id" FROM "actorFollow" ' +
'INNER JOIN "actor" ON "actor"."id" = "actorFollow"."actorId" ' +
`WHERE "actor"."inboxUrl" = '${inboxUrl}' OR "actor"."sharedInboxUrl" = '${inboxUrl}'` +
')'
const options = {
type: QueryTypes.BULKUPDATE,
transaction: t
}
return ActorFollowModel.sequelize.query(query, options)
}
static async updateScoreByFollowingServers (serverIds: number[], value: number, t?: Transaction) {
if (serverIds.length === 0) return
const me = await getServerActor()
const serverIdsString = createSafeIn(ActorFollowModel.sequelize, serverIds)
const query = `UPDATE "actorFollow" SET "score" = LEAST("score" + ${value}, ${ACTOR_FOLLOW_SCORE.MAX}) ` +
'WHERE id IN (' +
'SELECT "actorFollow"."id" FROM "actorFollow" ' +
'INNER JOIN "actor" ON "actor"."id" = "actorFollow"."targetActorId" ' +
`WHERE "actorFollow"."actorId" = ${me.Account.actorId} ` + // I'm the follower
`AND "actor"."serverId" IN (${serverIdsString})` + // Criteria on followings
')'
const options = {
type: QueryTypes.BULKUPDATE,
transaction: t
}
return ActorFollowModel.sequelize.query(query, options)
}
private static async createListAcceptedFollowForApiQuery (options: {
type: 'followers' | 'following'
actorIds: number[]
t: Transaction
start?: number
count?: number
columnUrl?: string // Default 'url'
distinct?: boolean // Default false
selectTotal?: boolean // Default true
}) {
const { type, actorIds, t, start, count, columnUrl = 'url', distinct = false, selectTotal = true } = options
let firstJoin: string
let secondJoin: string
if (type === 'followers') {
firstJoin = 'targetActorId'
secondJoin = 'actorId'
} else {
firstJoin = 'actorId'
secondJoin = 'targetActorId'
}
const selections: string[] = []
selections.push(
distinct === true
? `DISTINCT("Follows"."${columnUrl}") AS "selectionUrl"`
: `"Follows"."${columnUrl}" AS "selectionUrl"`
)
if (selectTotal) selections.push('COUNT(*) AS "total"')
const tasks: Promise<any>[] = []
for (const selection of selections) {
let query = 'SELECT ' + selection + ' FROM "actor" ' +
'INNER JOIN "actorFollow" ON "actorFollow"."' + firstJoin + '" = "actor"."id" ' +
'INNER JOIN "actor" AS "Follows" ON "actorFollow"."' + secondJoin + '" = "Follows"."id" ' +
`WHERE "actor"."id" = ANY ($actorIds) AND "actorFollow"."state" = 'accepted' AND "Follows"."${columnUrl}" IS NOT NULL `
if (count !== undefined) query += 'LIMIT ' + count
if (start !== undefined) query += ' OFFSET ' + start
const options = {
bind: { actorIds },
type: QueryTypes.SELECT,
transaction: t
}
tasks.push(ActorFollowModel.sequelize.query(query, options))
}
const [ followers, resDataTotal ] = await Promise.all(tasks)
return {
data: followers.map(f => ({ selectionUrl: f.selectionUrl, createdAt: f.createdAt })) as { selectionUrl: string, createdAt: string }[],
total: selectTotal
? parseInt(resDataTotal?.[0]?.total || 0, 10)
: undefined
}
}
private static listBadActorFollows () {
const query = {
where: {
score: {
[Op.lte]: 0
}
},
logging: false
}
return ActorFollowModel.findAll(query)
}
toFormattedJSON (this: MActorFollowFormattable): ActorFollow {
const follower = this.ActorFollower.toFormattedJSON()
const following = this.ActorFollowing.toFormattedJSON()
return {
id: this.id,
follower,
following,
score: this.score,
state: this.state,
createdAt: this.createdAt,
updatedAt: this.updatedAt
}
}
}
+206
ファイルの表示
@@ -0,0 +1,206 @@
import { ActivityIconObject, ActorImage, ActorImageType, type ActorImageType_Type } from '@peertube/peertube-models'
import { getLowercaseExtension } from '@peertube/peertube-node-utils'
import { MActorId, MActorImage, MActorImageFormattable } from '@server/types/models/index.js'
import { remove } from 'fs-extra/esm'
import { join } from 'path'
import { Op } from 'sequelize'
import {
AfterDestroy,
AllowNull,
BelongsTo,
Column,
CreatedAt,
Default,
ForeignKey, Table,
UpdatedAt
} from 'sequelize-typescript'
import { logger } from '../../helpers/logger.js'
import { CONFIG } from '../../initializers/config.js'
import { LAZY_STATIC_PATHS, MIMETYPES, WEBSERVER } from '../../initializers/constants.js'
import { SequelizeModel, buildSQLAttributes } from '../shared/index.js'
import { ActorModel } from './actor.js'
@Table({
tableName: 'actorImage',
indexes: [
{
fields: [ 'filename' ],
unique: true
},
{
fields: [ 'actorId', 'type', 'width' ],
unique: true
}
]
})
export class ActorImageModel extends SequelizeModel<ActorImageModel> {
@AllowNull(false)
@Column
filename: string
@AllowNull(true)
@Default(null)
@Column
height: number
@AllowNull(true)
@Default(null)
@Column
width: number
@AllowNull(true)
@Column
fileUrl: string
@AllowNull(false)
@Column
onDisk: boolean
@AllowNull(false)
@Column
type: ActorImageType_Type
@CreatedAt
createdAt: Date
@UpdatedAt
updatedAt: Date
@ForeignKey(() => ActorModel)
@Column
actorId: number
@BelongsTo(() => ActorModel, {
foreignKey: {
allowNull: false
},
onDelete: 'CASCADE'
})
Actor: Awaited<ActorModel> // TODO: Remove awaited: https://github.com/sequelize/sequelize-typescript/issues/825
@AfterDestroy
static removeFile (instance: ActorImageModel) {
logger.info('Removing actor image file %s.', instance.filename)
// Don't block the transaction
instance.removeImage()
.catch(err => logger.error('Cannot remove actor image file %s.', instance.filename, { err }))
}
// ---------------------------------------------------------------------------
static getSQLAttributes (tableName: string, aliasPrefix = '') {
return buildSQLAttributes({
model: this,
tableName,
aliasPrefix
})
}
// ---------------------------------------------------------------------------
static loadByFilename (filename: string) {
const query = {
where: {
filename
}
}
return ActorImageModel.findOne(query)
}
static listByActor (actor: MActorId, type: ActorImageType_Type) {
const query = {
where: {
actorId: actor.id,
type
}
}
return ActorImageModel.findAll(query)
}
static async listActorImages (actor: MActorId) {
const promises = [ ActorImageType.AVATAR, ActorImageType.BANNER ].map(type => ActorImageModel.listByActor(actor, type))
const [ avatars, banners ] = await Promise.all(promises)
return { avatars, banners }
}
static listRemoteOnDisk () {
return this.findAll<MActorImage>({
where: {
onDisk: true
},
include: [
{
attributes: [ 'id' ],
model: ActorModel.unscoped(),
required: true,
where: {
serverId: {
[Op.ne]: null
}
}
}
]
})
}
static getImageUrl (image: MActorImage) {
if (!image) return undefined
return WEBSERVER.URL + image.getStaticPath()
}
// ---------------------------------------------------------------------------
toFormattedJSON (this: MActorImageFormattable): ActorImage {
return {
width: this.width,
path: this.getStaticPath(),
createdAt: this.createdAt,
updatedAt: this.updatedAt
}
}
toActivityPubObject (): ActivityIconObject {
const extension = getLowercaseExtension(this.filename)
return {
type: 'Image',
mediaType: MIMETYPES.IMAGE.EXT_MIMETYPE[extension],
height: this.height,
width: this.width,
url: ActorImageModel.getImageUrl(this)
}
}
getStaticPath () {
switch (this.type) {
case ActorImageType.AVATAR:
return join(LAZY_STATIC_PATHS.AVATARS, this.filename)
case ActorImageType.BANNER:
return join(LAZY_STATIC_PATHS.BANNERS, this.filename)
default:
throw new Error('Unknown actor image type: ' + this.type)
}
}
getPath () {
return join(CONFIG.STORAGE.ACTOR_IMAGES_DIR, this.filename)
}
removeImage () {
const imagePath = join(CONFIG.STORAGE.ACTOR_IMAGES_DIR, this.filename)
return remove(imagePath)
}
isOwned () {
return !this.fileUrl
}
}
+696
ファイルの表示
@@ -0,0 +1,696 @@
import { forceNumber, maxBy } from '@peertube/peertube-core-utils'
import { ActivityIconObject, ActorImageType, ActorImageType_Type, type ActivityPubActorType } from '@peertube/peertube-models'
import { AttributesOnly } from '@peertube/peertube-typescript-utils'
import { activityPubContextify } from '@server/helpers/activity-pub-utils.js'
import { getContextFilter } from '@server/lib/activitypub/context.js'
import { ModelCache } from '@server/models/shared/model-cache.js'
import { Op, QueryTypes, Transaction, col, fn, literal, where } from 'sequelize'
import {
AllowNull,
BelongsTo,
Column,
CreatedAt,
DataType,
DefaultScope,
ForeignKey,
HasMany,
HasOne,
Is, Scopes,
Table,
UpdatedAt
} from 'sequelize-typescript'
import { Where } from 'sequelize/types/utils'
import {
isActorFollowersCountValid,
isActorFollowingCountValid,
isActorPreferredUsernameValid,
isActorPrivateKeyValid,
isActorPublicKeyValid
} from '../../helpers/custom-validators/activitypub/actor.js'
import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc.js'
import {
ACTIVITY_PUB,
ACTIVITY_PUB_ACTOR_TYPES,
CONSTRAINTS_FIELDS, SERVER_ACTOR_NAME,
WEBSERVER
} from '../../initializers/constants.js'
import {
MActor,
MActorAPAccount,
MActorAPChannel,
MActorAccountChannelId,
MActorFollowersUrl,
MActorFormattable,
MActorFull,
MActorHost,
MActorHostOnly,
MActorId,
MActorSummaryFormattable,
MActorUrl,
MActorWithInboxes
} from '../../types/models/index.js'
import { AccountModel } from '../account/account.js'
import { getServerActor } from '../application/application.js'
import { ServerModel } from '../server/server.js'
import { SequelizeModel, buildSQLAttributes, isOutdated, throwIfNotValid } from '../shared/index.js'
import { VideoChannelModel } from '../video/video-channel.js'
import { VideoModel } from '../video/video.js'
import { ActorFollowModel } from './actor-follow.js'
import { ActorImageModel } from './actor-image.js'
enum ScopeNames {
FULL = 'FULL'
}
export const unusedActorAttributesForAPI: (keyof AttributesOnly<ActorModel>)[] = [
'publicKey',
'privateKey',
'inboxUrl',
'outboxUrl',
'sharedInboxUrl',
'followersUrl',
'followingUrl'
]
@DefaultScope(() => ({
include: [
{
model: ServerModel,
required: false
},
{
model: ActorImageModel,
as: 'Avatars',
required: false
}
]
}))
@Scopes(() => ({
[ScopeNames.FULL]: {
include: [
{
model: AccountModel.unscoped(),
required: false
},
{
model: VideoChannelModel.unscoped(),
required: false,
include: [
{
model: AccountModel,
required: true
}
]
},
{
model: ServerModel,
required: false
},
{
model: ActorImageModel,
as: 'Avatars',
required: false
},
{
model: ActorImageModel,
as: 'Banners',
required: false
}
]
}
}))
@Table({
tableName: 'actor',
indexes: [
{
fields: [ 'url' ],
unique: true
},
{
fields: [ fn('lower', col('preferredUsername')), 'serverId' ],
name: 'actor_preferred_username_lower_server_id',
unique: true,
where: {
serverId: {
[Op.ne]: null
}
}
},
{
fields: [ fn('lower', col('preferredUsername')) ],
name: 'actor_preferred_username_lower',
unique: true,
where: {
serverId: null
}
},
{
fields: [ 'inboxUrl', 'sharedInboxUrl' ]
},
{
fields: [ 'sharedInboxUrl' ]
},
{
fields: [ 'serverId' ]
},
{
fields: [ 'followersUrl' ]
}
]
})
export class ActorModel extends SequelizeModel<ActorModel> {
@AllowNull(false)
@Column(DataType.ENUM(...Object.values(ACTIVITY_PUB_ACTOR_TYPES)))
type: ActivityPubActorType
@AllowNull(false)
@Is('ActorPreferredUsername', value => throwIfNotValid(value, isActorPreferredUsernameValid, 'actor preferred username'))
@Column
preferredUsername: string
@AllowNull(false)
@Is('ActorUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
@Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
url: string
@AllowNull(true)
@Is('ActorPublicKey', value => throwIfNotValid(value, isActorPublicKeyValid, 'public key', true))
@Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.PUBLIC_KEY.max))
publicKey: string
@AllowNull(true)
@Is('ActorPublicKey', value => throwIfNotValid(value, isActorPrivateKeyValid, 'private key', true))
@Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.PRIVATE_KEY.max))
privateKey: string
@AllowNull(false)
@Is('ActorFollowersCount', value => throwIfNotValid(value, isActorFollowersCountValid, 'followers count'))
@Column
followersCount: number
@AllowNull(false)
@Is('ActorFollowersCount', value => throwIfNotValid(value, isActorFollowingCountValid, 'following count'))
@Column
followingCount: number
@AllowNull(false)
@Is('ActorInboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'inbox url'))
@Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
inboxUrl: string
@AllowNull(true)
@Is('ActorOutboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'outbox url', true))
@Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
outboxUrl: string
@AllowNull(true)
@Is('ActorSharedInboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'shared inbox url', true))
@Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
sharedInboxUrl: string
@AllowNull(true)
@Is('ActorFollowersUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'followers url', true))
@Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
followersUrl: string
@AllowNull(true)
@Is('ActorFollowingUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'following url', true))
@Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
followingUrl: string
@AllowNull(true)
@Column
remoteCreatedAt: Date
@CreatedAt
createdAt: Date
@UpdatedAt
updatedAt: Date
@HasMany(() => ActorImageModel, {
as: 'Avatars',
onDelete: 'cascade',
hooks: true,
foreignKey: {
allowNull: false
},
scope: {
type: ActorImageType.AVATAR
}
})
Avatars: Awaited<ActorImageModel>[]
@HasMany(() => ActorImageModel, {
as: 'Banners',
onDelete: 'cascade',
hooks: true,
foreignKey: {
allowNull: false
},
scope: {
type: ActorImageType.BANNER
}
})
Banners: Awaited<ActorImageModel>[]
@HasMany(() => ActorFollowModel, {
foreignKey: {
name: 'actorId',
allowNull: false
},
as: 'ActorFollowings',
onDelete: 'cascade'
})
ActorFollowing: Awaited<ActorFollowModel>[]
@HasMany(() => ActorFollowModel, {
foreignKey: {
name: 'targetActorId',
allowNull: false
},
as: 'ActorFollowers',
onDelete: 'cascade'
})
ActorFollowers: Awaited<ActorFollowModel>[]
@ForeignKey(() => ServerModel)
@Column
serverId: number
@BelongsTo(() => ServerModel, {
foreignKey: {
allowNull: true
},
onDelete: 'cascade'
})
Server: Awaited<ServerModel>
@HasOne(() => AccountModel, {
foreignKey: {
allowNull: true
},
onDelete: 'cascade',
hooks: true
})
Account: Awaited<AccountModel>
@HasOne(() => VideoChannelModel, {
foreignKey: {
allowNull: true
},
onDelete: 'cascade',
hooks: true
})
VideoChannel: Awaited<VideoChannelModel>
// ---------------------------------------------------------------------------
static getSQLAttributes (tableName: string, aliasPrefix = '') {
return buildSQLAttributes({
model: this,
tableName,
aliasPrefix
})
}
static getSQLAPIAttributes (tableName: string, aliasPrefix = '') {
return buildSQLAttributes({
model: this,
tableName,
aliasPrefix,
excludeAttributes: unusedActorAttributesForAPI
})
}
// ---------------------------------------------------------------------------
// FIXME: have to specify the result type to not break peertube typings generation
static wherePreferredUsername (preferredUsername: string, colName = 'preferredUsername'): Where {
return where(fn('lower', col(colName)), preferredUsername.toLowerCase())
}
// ---------------------------------------------------------------------------
static async load (id: number): Promise<MActor> {
const actorServer = await getServerActor()
if (id === actorServer.id) return actorServer
return ActorModel.unscoped().findByPk(id)
}
static loadFull (id: number): Promise<MActorFull> {
return ActorModel.scope(ScopeNames.FULL).findByPk(id)
}
static loadAccountActorFollowerUrlByVideoId (videoId: number, transaction: Transaction) {
const query = `SELECT "actor"."id" AS "id", "actor"."followersUrl" AS "followersUrl" ` +
`FROM "actor" ` +
`INNER JOIN "account" ON "actor"."id" = "account"."actorId" ` +
`INNER JOIN "videoChannel" ON "videoChannel"."accountId" = "account"."id" ` +
`INNER JOIN "video" ON "video"."channelId" = "videoChannel"."id" AND "video"."id" = :videoId`
const options = {
type: QueryTypes.SELECT as QueryTypes.SELECT,
replacements: { videoId },
plain: true,
transaction
}
return ActorModel.sequelize.query<MActorId & MActorFollowersUrl>(query, options)
.then(res => {
if (res && res.length !== 0) return res[0]
return undefined
})
}
static listByFollowersUrls (followersUrls: string[], transaction?: Transaction): Promise<MActorFull[]> {
const query = {
where: {
followersUrl: {
[Op.in]: followersUrls
}
},
transaction
}
return ActorModel.scope(ScopeNames.FULL).findAll(query)
}
static loadLocalByName (preferredUsername: string, transaction?: Transaction): Promise<MActorFull> {
const fun = () => {
const query = {
where: {
[Op.and]: [
this.wherePreferredUsername(preferredUsername, '"ActorModel"."preferredUsername"'),
{
serverId: null
}
]
},
transaction
}
return ActorModel.scope(ScopeNames.FULL).findOne(query)
}
return ModelCache.Instance.doCache({
cacheType: 'local-actor-name',
key: preferredUsername,
// The server actor never change, so we can easily cache it
whitelist: () => preferredUsername === SERVER_ACTOR_NAME,
fun
})
}
static loadLocalUrlByName (preferredUsername: string, transaction?: Transaction): Promise<MActorUrl> {
const fun = () => {
const query = {
attributes: [ 'url' ],
where: {
[Op.and]: [
this.wherePreferredUsername(preferredUsername),
{
serverId: null
}
]
},
transaction
}
return ActorModel.unscoped().findOne(query)
}
return ModelCache.Instance.doCache({
cacheType: 'local-actor-url',
key: preferredUsername,
// The server actor never change, so we can easily cache it
whitelist: () => preferredUsername === SERVER_ACTOR_NAME,
fun
})
}
static loadByNameAndHost (preferredUsername: string, host: string): Promise<MActorFull> {
const query = {
where: this.wherePreferredUsername(preferredUsername, '"ActorModel"."preferredUsername"'),
include: [
{
model: ServerModel,
required: true,
where: {
host
}
}
]
}
return ActorModel.scope(ScopeNames.FULL).findOne(query)
}
static loadByUrl (url: string, transaction?: Transaction): Promise<MActorAccountChannelId> {
const query = {
where: {
url
},
transaction,
include: [
{
attributes: [ 'id' ],
model: AccountModel.unscoped(),
required: false
},
{
attributes: [ 'id' ],
model: VideoChannelModel.unscoped(),
required: false
}
]
}
return ActorModel.unscoped().findOne(query)
}
static loadByUrlAndPopulateAccountAndChannel (url: string, transaction?: Transaction): Promise<MActorFull> {
const query = {
where: {
url
},
transaction
}
return ActorModel.scope(ScopeNames.FULL).findOne(query)
}
static rebuildFollowsCount (ofId: number, type: 'followers' | 'following', transaction?: Transaction) {
const sanitizedOfId = forceNumber(ofId)
const where = { id: sanitizedOfId }
let columnToUpdate: string
let columnOfCount: string
if (type === 'followers') {
columnToUpdate = 'followersCount'
columnOfCount = 'targetActorId'
} else {
columnToUpdate = 'followingCount'
columnOfCount = 'actorId'
}
return ActorModel.update({
[columnToUpdate]: literal(`(SELECT COUNT(*) FROM "actorFollow" WHERE "${columnOfCount}" = ${sanitizedOfId} AND "state" = 'accepted')`)
}, { where, transaction })
}
static loadAccountActorByVideoId (videoId: number, transaction: Transaction): Promise<MActor> {
const query = {
include: [
{
attributes: [ 'id' ],
model: AccountModel.unscoped(),
required: true,
include: [
{
attributes: [ 'id', 'accountId' ],
model: VideoChannelModel.unscoped(),
required: true,
include: [
{
attributes: [ 'id', 'channelId' ],
model: VideoModel.unscoped(),
where: {
id: videoId
}
}
]
}
]
}
],
transaction
}
return ActorModel.unscoped().findOne(query)
}
getSharedInbox (this: MActorWithInboxes) {
return this.sharedInboxUrl || this.inboxUrl
}
toFormattedSummaryJSON (this: MActorSummaryFormattable) {
return {
url: this.url,
name: this.preferredUsername,
host: this.getHost(),
avatars: (this.Avatars || []).map(a => a.toFormattedJSON())
}
}
toFormattedJSON (this: MActorFormattable, includeBanner = true) {
return {
...this.toFormattedSummaryJSON(),
id: this.id,
hostRedundancyAllowed: this.getRedundancyAllowed(),
followingCount: this.followingCount,
followersCount: this.followersCount,
createdAt: this.getCreatedAt(),
banners: includeBanner
? (this.Banners || []).map(b => b.toFormattedJSON())
: undefined
}
}
toActivityPubObject (this: MActorAPChannel | MActorAPAccount, name: string) {
let icon: ActivityIconObject[] // Avatars
let image: ActivityIconObject[] // Banners
if (this.hasImage(ActorImageType.AVATAR)) {
icon = this.Avatars.map(a => a.toActivityPubObject())
}
if (this.hasImage(ActorImageType.BANNER)) {
image = (this as MActorAPChannel).Banners.map(b => b.toActivityPubObject())
}
const json = {
type: this.type,
id: this.url,
following: this.getFollowingUrl(),
followers: this.getFollowersUrl(),
playlists: this.getPlaylistsUrl(),
inbox: this.inboxUrl,
outbox: this.outboxUrl,
preferredUsername: this.preferredUsername,
url: this.url,
name,
endpoints: {
sharedInbox: this.sharedInboxUrl
},
publicKey: {
id: this.getPublicKeyUrl(),
owner: this.url,
publicKeyPem: this.publicKey
},
published: this.getCreatedAt().toISOString(),
icon,
image
}
return activityPubContextify(json, 'Actor', getContextFilter())
}
getFollowerSharedInboxUrls (t: Transaction) {
const query = {
attributes: [ 'sharedInboxUrl' ],
include: [
{
attribute: [],
model: ActorFollowModel.unscoped(),
required: true,
as: 'ActorFollowing',
where: {
state: 'accepted',
targetActorId: this.id
}
}
],
transaction: t
}
return ActorModel.findAll(query)
.then(accounts => accounts.map(a => a.sharedInboxUrl))
}
getFollowingUrl () {
return this.url + '/following'
}
getFollowersUrl () {
return this.url + '/followers'
}
getPlaylistsUrl () {
return this.url + '/playlists'
}
getPublicKeyUrl () {
return this.url + '#main-key'
}
isOwned () {
return this.serverId === null
}
getWebfingerUrl (this: MActorHost) {
return 'acct:' + this.preferredUsername + '@' + this.getHost()
}
getIdentifier (this: MActorHost) {
return this.Server ? `${this.preferredUsername}@${this.Server.host}` : this.preferredUsername
}
getFullIdentifier (this: MActorHost) {
return `${this.preferredUsername}@${this.getHost()}`
}
getHost (this: MActorHostOnly) {
return this.Server ? this.Server.host : WEBSERVER.HOST
}
getRedundancyAllowed () {
return this.Server ? this.Server.redundancyAllowed : false
}
hasImage (type: ActorImageType_Type) {
const images = type === ActorImageType.AVATAR
? this.Avatars
: this.Banners
return Array.isArray(images) && images.length !== 0
}
getMaxQualityImage (type: ActorImageType_Type) {
if (!this.hasImage(type)) return undefined
const images = type === ActorImageType.AVATAR
? this.Avatars
: this.Banners
return maxBy(images, 'height')
}
isOutdated () {
if (this.isOwned()) return false
return isOutdated(this, ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL)
}
getCreatedAt (this: MActorAPChannel | MActorAPAccount | MActorFormattable) {
return this.remoteCreatedAt || this.createdAt
}
}
+69
ファイルの表示
@@ -0,0 +1,69 @@
import { Sequelize } from 'sequelize'
import { ModelBuilder } from '@server/models/shared/index.js'
import { MActorFollowActorsDefault } from '@server/types/models/index.js'
import { ActivityPubActorType, FollowState } from '@peertube/peertube-models'
import { parseRowCountResult } from '../../shared/index.js'
import { InstanceListFollowsQueryBuilder } from './shared/instance-list-follows-query-builder.js'
export interface ListFollowersOptions {
actorIds: number[]
start: number
count: number
sort: string
state?: FollowState
actorType?: ActivityPubActorType
search?: string
}
export class InstanceListFollowersQueryBuilder extends InstanceListFollowsQueryBuilder <ListFollowersOptions> {
constructor (
protected readonly sequelize: Sequelize,
protected readonly options: ListFollowersOptions
) {
super(sequelize, options)
}
async listFollowers () {
this.buildListQuery()
const results = await this.runQuery({ nest: true })
const modelBuilder = new ModelBuilder<MActorFollowActorsDefault>(this.sequelize)
return modelBuilder.createModels(results, 'ActorFollow')
}
async countFollowers () {
this.buildCountQuery()
const result = await this.runQuery()
return parseRowCountResult(result)
}
protected getWhere () {
let where = 'WHERE "ActorFollowing"."id" IN (:actorIds) '
this.replacements.actorIds = this.options.actorIds
if (this.options.state) {
where += 'AND "ActorFollowModel"."state" = :state '
this.replacements.state = this.options.state
}
if (this.options.search) {
const escapedLikeSearch = this.sequelize.escape('%' + this.options.search + '%')
where += `AND (` +
`"ActorFollower->Server"."host" ILIKE ${escapedLikeSearch} ` +
`OR "ActorFollower"."preferredUsername" ILIKE ${escapedLikeSearch} ` +
`)`
}
if (this.options.actorType) {
where += `AND "ActorFollower"."type" = :actorType `
this.replacements.actorType = this.options.actorType
}
return where
}
}
+69
ファイルの表示
@@ -0,0 +1,69 @@
import { Sequelize } from 'sequelize'
import { ModelBuilder } from '@server/models/shared/index.js'
import { MActorFollowActorsDefault } from '@server/types/models/index.js'
import { ActivityPubActorType, FollowState } from '@peertube/peertube-models'
import { parseRowCountResult } from '../../shared/index.js'
import { InstanceListFollowsQueryBuilder } from './shared/instance-list-follows-query-builder.js'
export interface ListFollowingOptions {
followerId: number
start: number
count: number
sort: string
state?: FollowState
actorType?: ActivityPubActorType
search?: string
}
export class InstanceListFollowingQueryBuilder extends InstanceListFollowsQueryBuilder <ListFollowingOptions> {
constructor (
protected readonly sequelize: Sequelize,
protected readonly options: ListFollowingOptions
) {
super(sequelize, options)
}
async listFollowing () {
this.buildListQuery()
const results = await this.runQuery({ nest: true })
const modelBuilder = new ModelBuilder<MActorFollowActorsDefault>(this.sequelize)
return modelBuilder.createModels(results, 'ActorFollow')
}
async countFollowing () {
this.buildCountQuery()
const result = await this.runQuery()
return parseRowCountResult(result)
}
protected getWhere () {
let where = 'WHERE "ActorFollowModel"."actorId" = :followerId '
this.replacements.followerId = this.options.followerId
if (this.options.state) {
where += 'AND "ActorFollowModel"."state" = :state '
this.replacements.state = this.options.state
}
if (this.options.search) {
const escapedLikeSearch = this.sequelize.escape('%' + this.options.search + '%')
where += `AND (` +
`"ActorFollowing->Server"."host" ILIKE ${escapedLikeSearch} ` +
`OR "ActorFollowing"."preferredUsername" ILIKE ${escapedLikeSearch} ` +
`)`
}
if (this.options.actorType) {
where += `AND "ActorFollowing"."type" = :actorType `
this.replacements.actorType = this.options.actorType
}
return where
}
}
+28
ファイルの表示
@@ -0,0 +1,28 @@
import { Memoize } from '@server/helpers/memoize.js'
import { ServerModel } from '@server/models/server/server.js'
import { ActorModel } from '../../actor.js'
import { ActorFollowModel } from '../../actor-follow.js'
import { ActorImageModel } from '../../actor-image.js'
export class ActorFollowTableAttributes {
@Memoize()
getFollowAttributes () {
return ActorFollowModel.getSQLAttributes('ActorFollowModel').join(', ')
}
@Memoize()
getActorAttributes (actorTableName: string) {
return ActorModel.getSQLAttributes(actorTableName, `${actorTableName}.`).join(', ')
}
@Memoize()
getServerAttributes (actorTableName: string) {
return ServerModel.getSQLAttributes(`${actorTableName}->Server`, `${actorTableName}.Server.`).join(', ')
}
@Memoize()
getAvatarAttributes (actorTableName: string) {
return ActorImageModel.getSQLAttributes(`${actorTableName}->Avatars`, `${actorTableName}.Avatars.`).join(', ')
}
}
+97
ファイルの表示
@@ -0,0 +1,97 @@
import { Sequelize } from 'sequelize'
import { AbstractRunQuery } from '@server/models/shared/index.js'
import { ActorImageType } from '@peertube/peertube-models'
import { getInstanceFollowsSort } from '../../../shared/index.js'
import { ActorFollowTableAttributes } from './actor-follow-table-attributes.js'
type BaseOptions = {
sort: string
count: number
start: number
}
export abstract class InstanceListFollowsQueryBuilder <T extends BaseOptions> extends AbstractRunQuery {
protected readonly tableAttributes = new ActorFollowTableAttributes()
protected innerQuery: string
constructor (
protected readonly sequelize: Sequelize,
protected readonly options: T
) {
super(sequelize)
}
protected abstract getWhere (): string
protected getJoins () {
return 'INNER JOIN "actor" "ActorFollower" ON "ActorFollower"."id" = "ActorFollowModel"."actorId" ' +
'INNER JOIN "actor" "ActorFollowing" ON "ActorFollowing"."id" = "ActorFollowModel"."targetActorId" '
}
protected getServerJoin (actorName: string) {
return `LEFT JOIN "server" "${actorName}->Server" ON "${actorName}"."serverId" = "${actorName}->Server"."id" `
}
protected getAvatarsJoin (actorName: string) {
return `LEFT JOIN "actorImage" "${actorName}->Avatars" ON "${actorName}.id" = "${actorName}->Avatars"."actorId" ` +
`AND "${actorName}->Avatars"."type" = ${ActorImageType.AVATAR}`
}
private buildInnerQuery () {
this.innerQuery = `${this.getInnerSelect()} ` +
`FROM "actorFollow" AS "ActorFollowModel" ` +
`${this.getJoins()} ` +
`${this.getServerJoin('ActorFollowing')} ` +
`${this.getServerJoin('ActorFollower')} ` +
`${this.getWhere()} ` +
`${this.getOrder()} ` +
`LIMIT :limit OFFSET :offset `
this.replacements.limit = this.options.count
this.replacements.offset = this.options.start
}
protected buildListQuery () {
this.buildInnerQuery()
this.query = `${this.getSelect()} ` +
`FROM (${this.innerQuery}) AS "ActorFollowModel" ` +
`${this.getAvatarsJoin('ActorFollower')} ` +
`${this.getAvatarsJoin('ActorFollowing')} ` +
`${this.getOrder()}`
}
protected buildCountQuery () {
this.query = `SELECT COUNT(*) AS "total" ` +
`FROM "actorFollow" AS "ActorFollowModel" ` +
`${this.getJoins()} ` +
`${this.getServerJoin('ActorFollowing')} ` +
`${this.getServerJoin('ActorFollower')} ` +
`${this.getWhere()}`
}
private getInnerSelect () {
return this.buildSelect([
this.tableAttributes.getFollowAttributes(),
this.tableAttributes.getActorAttributes('ActorFollower'),
this.tableAttributes.getActorAttributes('ActorFollowing'),
this.tableAttributes.getServerAttributes('ActorFollower'),
this.tableAttributes.getServerAttributes('ActorFollowing')
])
}
private getSelect () {
return this.buildSelect([
'"ActorFollowModel".*',
this.tableAttributes.getAvatarAttributes('ActorFollower'),
this.tableAttributes.getAvatarAttributes('ActorFollowing')
])
}
private getOrder () {
const orders = getInstanceFollowsSort(this.options.sort)
return 'ORDER BY ' + orders.map(o => `"${o[0]}" ${o[1]}`).join(', ')
}
}
+84
ファイルの表示
@@ -0,0 +1,84 @@
import { getNodeABIVersion } from '@server/helpers/version.js'
import memoizee from 'memoizee'
import { AllowNull, Column, Default, DefaultScope, HasOne, IsInt, Table } from 'sequelize-typescript'
import { AccountModel } from '../account/account.js'
import { ActorImageModel } from '../actor/actor-image.js'
import { SequelizeModel } from '../shared/index.js'
export const getServerActor = memoizee(async function () {
const application = await ApplicationModel.load()
if (!application) throw Error('Could not load Application from database.')
const actor = application.Account.Actor
actor.Account = application.Account
const { avatars, banners } = await ActorImageModel.listActorImages(actor)
actor.Avatars = avatars
actor.Banners = banners
return actor
}, { promise: true })
@DefaultScope(() => ({
include: [
{
model: AccountModel,
required: true
}
]
}))
@Table({
tableName: 'application',
timestamps: false
})
export class ApplicationModel extends SequelizeModel<ApplicationModel> {
@AllowNull(false)
@Default(0)
@IsInt
@Column
migrationVersion: number
@AllowNull(true)
@Column
latestPeerTubeVersion: string
@AllowNull(false)
@Column
nodeVersion: string
@AllowNull(false)
@Column
nodeABIVersion: number
@HasOne(() => AccountModel, {
foreignKey: {
allowNull: true
},
onDelete: 'cascade'
})
Account: Awaited<AccountModel>
static countTotal () {
return ApplicationModel.count()
}
static load () {
return ApplicationModel.findOne()
}
static async nodeABIChanged () {
const application = await this.load()
return application.nodeABIVersion !== getNodeABIVersion()
}
static async updateNodeVersions () {
const application = await this.load()
application.nodeABIVersion = getNodeABIVersion()
application.nodeVersion = process.version
await application.save()
}
}
+96
ファイルの表示
@@ -0,0 +1,96 @@
import { type AutomaticTagPolicyType } from '@peertube/peertube-models'
import { MAccountId } from '@server/types/models/index.js'
import { Transaction } from 'sequelize'
import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Table, UpdatedAt } from 'sequelize-typescript'
import { AccountModel } from '../account/account.js'
import { SequelizeModel, createSafeIn, doesExist } from '../shared/index.js'
import { AutomaticTagModel } from './automatic-tag.js'
@Table({
tableName: 'accountAutomaticTagPolicy',
indexes: [
{
fields: [ 'accountId', 'policy', 'automaticTagId' ],
unique: true
}
]
})
export class AccountAutomaticTagPolicyModel extends SequelizeModel<AccountAutomaticTagPolicyModel> {
@CreatedAt
createdAt: Date
@UpdatedAt
updatedAt: Date
@AllowNull(true)
@Column(DataType.INTEGER)
policy: AutomaticTagPolicyType
@ForeignKey(() => AccountModel)
@Column
accountId: number
@BelongsTo(() => AccountModel, {
foreignKey: {
allowNull: false
},
onDelete: 'cascade'
})
Account: Awaited<AccountModel>
@ForeignKey(() => AutomaticTagModel)
@Column
automaticTagId: number
@BelongsTo(() => AutomaticTagModel, {
foreignKey: {
allowNull: false
},
onDelete: 'cascade'
})
AutomaticTag: Awaited<AutomaticTagModel>
static async listOfAccount (account: MAccountId) {
const rows = await this.findAll({
where: { accountId: account.id },
include: [
{
model: AutomaticTagModel,
required: true
}
]
})
return rows.map(r => ({ name: r.AutomaticTag.name, policy: r.policy }))
}
static deleteOfAccount (options: {
account: MAccountId
policy: AutomaticTagPolicyType
transaction?: Transaction
}) {
const { account, policy, transaction } = options
return this.destroy({
where: { accountId: account.id, policy },
transaction
})
}
static hasPolicyOnTags (options: {
accountId: number
tags: string[]
policy: AutomaticTagPolicyType
transaction: Transaction
}) {
const { accountId, tags, policy, transaction } = options
const query = `SELECT 1 FROM "accountAutomaticTagPolicy" ` +
`INNER JOIN "automaticTag" ON "automaticTag"."id" = "accountAutomaticTagPolicy"."automaticTagId" ` +
`WHERE "accountId" = $accountId AND "accountAutomaticTagPolicy"."policy" = $policy AND ` +
`"automaticTag"."name" IN (${createSafeIn(this.sequelize, tags)}) ` +
`LIMIT 1`
return doesExist({ sequelize: this.sequelize, query, bind: { accountId, policy }, transaction })
}
}
+69
ファイルの表示
@@ -0,0 +1,69 @@
import { MAutomaticTag } from '@server/types/models/index.js'
import { Transaction, col, fn } from 'sequelize'
import { AllowNull, Column, HasMany, Table } from 'sequelize-typescript'
import { SequelizeModel } from '../shared/index.js'
import { AccountAutomaticTagPolicyModel } from './account-automatic-tag-policy.js'
import { CommentAutomaticTagModel } from './comment-automatic-tag.js'
import { VideoAutomaticTagModel } from './video-automatic-tag.js'
@Table({
tableName: 'automaticTag',
timestamps: false,
indexes: [
{
fields: [ 'name' ],
unique: true
},
{
name: 'automatic_tag_lower_name',
fields: [ fn('lower', col('name')) ]
}
]
})
export class AutomaticTagModel extends SequelizeModel<AutomaticTagModel> {
@AllowNull(false)
@Column
name: string
@HasMany(() => CommentAutomaticTagModel, {
foreignKey: 'automaticTagId',
onDelete: 'CASCADE'
})
CommentAutomaticTags: Awaited<CommentAutomaticTagModel>[]
@HasMany(() => VideoAutomaticTagModel, {
foreignKey: 'automaticTagId',
onDelete: 'CASCADE'
})
VideoAutomaticTags: Awaited<VideoAutomaticTagModel>[]
@HasMany(() => AccountAutomaticTagPolicyModel, {
foreignKey: {
name: 'automaticTagId',
allowNull: false
},
onDelete: 'cascade'
})
AccountAutomaticTagPolicies: Awaited<AccountAutomaticTagPolicyModel>[]
static findOrCreateAutomaticTag (options: {
tag: string
transaction?: Transaction
}): Promise<MAutomaticTag> {
const { tag, transaction } = options
const query = {
where: {
name: tag
},
defaults: {
name: tag
},
transaction
}
return this.findOrCreate(query)
.then(([ tagInstance ]) => tagInstance)
}
}
+76
ファイルの表示
@@ -0,0 +1,76 @@
import { BelongsTo, Column, CreatedAt, ForeignKey, PrimaryKey, Table, UpdatedAt } from 'sequelize-typescript'
import { AccountModel } from '../account/account.js'
import { SequelizeModel } from '../shared/index.js'
import { VideoCommentModel } from '../video/video-comment.js'
import { AutomaticTagModel } from './automatic-tag.js'
import { Transaction } from 'sequelize'
/**
*
* Sequelize doesn't seem to support many to many relation using BelongsToMany with 3 tables
* So we reproduce the behaviour with classic BelongsTo/HasMany relations
*
*/
@Table({
tableName: 'commentAutomaticTag'
})
export class CommentAutomaticTagModel extends SequelizeModel<CommentAutomaticTagModel> {
@CreatedAt
createdAt: Date
@UpdatedAt
updatedAt: Date
@ForeignKey(() => VideoCommentModel)
@PrimaryKey
@Column
commentId: number
@ForeignKey(() => AutomaticTagModel)
@PrimaryKey
@Column
automaticTagId: number
@ForeignKey(() => AccountModel)
@PrimaryKey
@Column
accountId: number
@BelongsTo(() => AccountModel, {
foreignKey: {
allowNull: false
},
onDelete: 'CASCADE'
})
Account: Awaited<AccountModel>
@BelongsTo(() => AutomaticTagModel, {
foreignKey: {
allowNull: false
},
onDelete: 'CASCADE'
})
AutomaticTag: Awaited<AutomaticTagModel>
@BelongsTo(() => VideoCommentModel, {
foreignKey: {
allowNull: false
},
onDelete: 'CASCADE'
})
VideoComment: Awaited<VideoCommentModel>
static deleteAllOfAccountAndComment (options: {
accountId: number
commentId: number
transaction: Transaction
}) {
const { accountId, commentId, transaction } = options
return this.destroy({
where: { accountId, commentId },
transaction
})
}
}
+76
ファイルの表示
@@ -0,0 +1,76 @@
import { Transaction } from 'sequelize'
import { BelongsTo, Column, CreatedAt, ForeignKey, PrimaryKey, Table, UpdatedAt } from 'sequelize-typescript'
import { AccountModel } from '../account/account.js'
import { SequelizeModel } from '../shared/index.js'
import { VideoModel } from '../video/video.js'
import { AutomaticTagModel } from './automatic-tag.js'
/**
*
* Sequelize doesn't seem to support many to many relation using BelongsToMany with 3 tables
* So we reproduce the behaviour with classic BelongsTo/HasMany relations
*
*/
@Table({
tableName: 'videoAutomaticTag'
})
export class VideoAutomaticTagModel extends SequelizeModel<VideoAutomaticTagModel> {
@CreatedAt
createdAt: Date
@UpdatedAt
updatedAt: Date
@ForeignKey(() => VideoModel)
@PrimaryKey
@Column
videoId: number
@ForeignKey(() => AutomaticTagModel)
@PrimaryKey
@Column
automaticTagId: number
@ForeignKey(() => AccountModel)
@PrimaryKey
@Column
accountId: number
@BelongsTo(() => AccountModel, {
foreignKey: {
allowNull: false
},
onDelete: 'CASCADE'
})
Account: Awaited<AccountModel>
@BelongsTo(() => AutomaticTagModel, {
foreignKey: {
allowNull: false
},
onDelete: 'CASCADE'
})
AutomaticTag: Awaited<AutomaticTagModel>
@BelongsTo(() => VideoModel, {
foreignKey: {
allowNull: false
},
onDelete: 'CASCADE'
})
Video: Awaited<VideoModel>
static deleteAllOfAccountAndVideo (options: {
accountId: number
videoId: number
transaction: Transaction
}) {
const { accountId, videoId, transaction } = options
return this.destroy({
where: { accountId, videoId },
transaction
})
}
}
+63
ファイルの表示
@@ -0,0 +1,63 @@
import { AllowNull, Column, CreatedAt, DataType, HasMany, Table, UpdatedAt } from 'sequelize-typescript'
import { OAuthTokenModel } from './oauth-token.js'
import { SequelizeModel } from '../shared/index.js'
@Table({
tableName: 'oAuthClient',
indexes: [
{
fields: [ 'clientId' ],
unique: true
},
{
fields: [ 'clientId', 'clientSecret' ],
unique: true
}
]
})
export class OAuthClientModel extends SequelizeModel<OAuthClientModel> {
@AllowNull(false)
@Column
clientId: string
@AllowNull(false)
@Column
clientSecret: string
@Column(DataType.ARRAY(DataType.STRING))
grants: string[]
@Column(DataType.ARRAY(DataType.STRING))
redirectUris: string[]
@CreatedAt
createdAt: Date
@UpdatedAt
updatedAt: Date
@HasMany(() => OAuthTokenModel, {
onDelete: 'cascade'
})
OAuthTokens: Awaited<OAuthTokenModel>[]
static countTotal () {
return OAuthClientModel.count()
}
static loadFirstClient () {
return OAuthClientModel.findOne()
}
static getByIdAndSecret (clientId: string, clientSecret: string) {
const query = {
where: {
clientId,
clientSecret
}
}
return OAuthClientModel.findOne(query)
}
}
+220
ファイルの表示
@@ -0,0 +1,220 @@
import { Transaction } from 'sequelize'
import {
AfterDestroy,
AfterUpdate,
AllowNull,
BelongsTo,
Column,
CreatedAt,
ForeignKey, Scopes,
Table,
UpdatedAt
} from 'sequelize-typescript'
import { TokensCache } from '@server/lib/auth/tokens-cache.js'
import { MUserAccountId } from '@server/types/models/index.js'
import { MOAuthTokenUser } from '@server/types/models/oauth/oauth-token.js'
import { logger } from '../../helpers/logger.js'
import { AccountModel } from '../account/account.js'
import { ActorModel } from '../actor/actor.js'
import { UserModel } from '../user/user.js'
import { OAuthClientModel } from './oauth-client.js'
import { SequelizeModel } from '../shared/index.js'
export type OAuthTokenInfo = {
refreshToken: string
refreshTokenExpiresAt: Date
client: {
id: number
grants: string[]
}
user: MUserAccountId
token: MOAuthTokenUser
}
enum ScopeNames {
WITH_USER = 'WITH_USER'
}
@Scopes(() => ({
[ScopeNames.WITH_USER]: {
include: [
{
model: UserModel.unscoped(),
required: true,
include: [
{
attributes: [ 'id' ],
model: AccountModel.unscoped(),
required: true,
include: [
{
attributes: [ 'id', 'url' ],
model: ActorModel.unscoped(),
required: true
}
]
}
]
}
]
}
}))
@Table({
tableName: 'oAuthToken',
indexes: [
{
fields: [ 'refreshToken' ],
unique: true
},
{
fields: [ 'accessToken' ],
unique: true
},
{
fields: [ 'userId' ]
},
{
fields: [ 'oAuthClientId' ]
}
]
})
export class OAuthTokenModel extends SequelizeModel<OAuthTokenModel> {
@AllowNull(false)
@Column
accessToken: string
@AllowNull(false)
@Column
accessTokenExpiresAt: Date
@AllowNull(false)
@Column
refreshToken: string
@AllowNull(false)
@Column
refreshTokenExpiresAt: Date
@Column
authName: string
@CreatedAt
createdAt: Date
@UpdatedAt
updatedAt: Date
@ForeignKey(() => UserModel)
@Column
userId: number
@BelongsTo(() => UserModel, {
foreignKey: {
allowNull: false
},
onDelete: 'cascade'
})
User: Awaited<UserModel>
@ForeignKey(() => OAuthClientModel)
@Column
oAuthClientId: number
@BelongsTo(() => OAuthClientModel, {
foreignKey: {
allowNull: false
},
onDelete: 'cascade'
})
OAuthClients: Awaited<OAuthClientModel>[]
@AfterUpdate
@AfterDestroy
static removeTokenCache (token: OAuthTokenModel) {
return TokensCache.Instance.clearCacheByToken(token.accessToken)
}
static loadByRefreshToken (refreshToken: string) {
const query = {
where: { refreshToken }
}
return OAuthTokenModel.findOne(query)
}
static getByRefreshTokenAndPopulateClient (refreshToken: string) {
const query = {
where: {
refreshToken
},
include: [ OAuthClientModel ]
}
return OAuthTokenModel.scope(ScopeNames.WITH_USER)
.findOne(query)
.then(token => {
if (!token) return null
return {
refreshToken: token.refreshToken,
refreshTokenExpiresAt: token.refreshTokenExpiresAt,
client: {
id: token.oAuthClientId,
grants: []
},
user: token.User,
token
} as OAuthTokenInfo
})
.catch(err => {
logger.error('getRefreshToken error.', { err })
throw err
})
}
static getByTokenAndPopulateUser (bearerToken: string): Promise<MOAuthTokenUser> {
const query = {
where: {
accessToken: bearerToken
}
}
return OAuthTokenModel.scope(ScopeNames.WITH_USER)
.findOne(query)
.then(token => {
if (!token) return null
return Object.assign(token, { user: token.User })
})
}
static getByRefreshTokenAndPopulateUser (refreshToken: string): Promise<MOAuthTokenUser> {
const query = {
where: {
refreshToken
}
}
return OAuthTokenModel.scope(ScopeNames.WITH_USER)
.findOne(query)
.then(token => {
if (!token) return undefined
return Object.assign(token, { user: token.User })
})
}
static deleteUserToken (userId: number, t?: Transaction) {
TokensCache.Instance.deleteUserToken(userId)
const query = {
where: {
userId
},
transaction: t
}
return OAuthTokenModel.destroy(query)
}
}
+795
ファイルの表示
@@ -0,0 +1,795 @@
import {
ActivityVideoUrlObject,
CacheFileObject,
FileRedundancyInformation,
StreamingPlaylistRedundancyInformation,
VideoPrivacy,
VideoRedundanciesTarget,
VideoRedundancy,
VideoRedundancyStrategy,
VideoRedundancyStrategyWithManual
} from '@peertube/peertube-models'
import { isTestInstance } from '@peertube/peertube-node-utils'
import { getVideoFileMimeType } from '@server/lib/video-file.js'
import { getServerActor } from '@server/models/application/application.js'
import { MActor, MVideoForRedundancyAPI, MVideoRedundancy, MVideoRedundancyAP, MVideoRedundancyVideo } from '@server/types/models/index.js'
import sample from 'lodash-es/sample.js'
import { literal, Op, QueryTypes, Transaction, WhereOptions } from 'sequelize'
import {
AllowNull,
BeforeDestroy,
BelongsTo,
Column,
CreatedAt,
DataType,
ForeignKey,
Is, Scopes,
Table,
UpdatedAt
} from 'sequelize-typescript'
import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc.js'
import { logger } from '../../helpers/logger.js'
import { CONFIG } from '../../initializers/config.js'
import { CONSTRAINTS_FIELDS } from '../../initializers/constants.js'
import { ActorModel } from '../actor/actor.js'
import { ServerModel } from '../server/server.js'
import { getSort, getVideoSort, parseAggregateResult, SequelizeModel, throwIfNotValid } from '../shared/index.js'
import { ScheduleVideoUpdateModel } from '../video/schedule-video-update.js'
import { VideoChannelModel } from '../video/video-channel.js'
import { VideoFileModel } from '../video/video-file.js'
import { VideoStreamingPlaylistModel } from '../video/video-streaming-playlist.js'
import { VideoModel } from '../video/video.js'
export enum ScopeNames {
WITH_VIDEO = 'WITH_VIDEO'
}
@Scopes(() => ({
[ScopeNames.WITH_VIDEO]: {
include: [
{
model: VideoFileModel,
required: false,
include: [
{
model: VideoModel,
required: true
}
]
},
{
model: VideoStreamingPlaylistModel,
required: false,
include: [
{
model: VideoModel,
required: true
}
]
}
]
}
}))
@Table({
tableName: 'videoRedundancy',
indexes: [
{
fields: [ 'videoFileId' ]
},
{
fields: [ 'actorId' ]
},
{
fields: [ 'expiresOn' ]
},
{
fields: [ 'url' ],
unique: true
}
]
})
export class VideoRedundancyModel extends SequelizeModel<VideoRedundancyModel> {
@CreatedAt
createdAt: Date
@UpdatedAt
updatedAt: Date
@AllowNull(true)
@Column
expiresOn: Date
@AllowNull(false)
@Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS_REDUNDANCY.URL.max))
fileUrl: string
@AllowNull(false)
@Is('VideoRedundancyUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
@Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS_REDUNDANCY.URL.max))
url: string
@AllowNull(true)
@Column
strategy: string // Only used by us
@ForeignKey(() => VideoFileModel)
@Column
videoFileId: number
@BelongsTo(() => VideoFileModel, {
foreignKey: {
allowNull: true
},
onDelete: 'cascade'
})
VideoFile: Awaited<VideoFileModel>
@ForeignKey(() => VideoStreamingPlaylistModel)
@Column
videoStreamingPlaylistId: number
@BelongsTo(() => VideoStreamingPlaylistModel, {
foreignKey: {
allowNull: true
},
onDelete: 'cascade'
})
VideoStreamingPlaylist: Awaited<VideoStreamingPlaylistModel>
@ForeignKey(() => ActorModel)
@Column
actorId: number
@BelongsTo(() => ActorModel, {
foreignKey: {
allowNull: false
},
onDelete: 'cascade'
})
Actor: Awaited<ActorModel>
@BeforeDestroy
static async removeFile (instance: VideoRedundancyModel) {
if (!instance.isOwned()) return
if (instance.videoFileId) {
const videoFile = await VideoFileModel.loadWithVideo(instance.videoFileId)
const logIdentifier = `${videoFile.Video.uuid}-${videoFile.resolution}`
logger.info('Removing duplicated video file %s.', logIdentifier)
videoFile.Video.removeWebVideoFile(videoFile, true)
.catch(err => logger.error('Cannot delete %s files.', logIdentifier, { err }))
}
if (instance.videoStreamingPlaylistId) {
const videoStreamingPlaylist = await VideoStreamingPlaylistModel.loadWithVideo(instance.videoStreamingPlaylistId)
const videoUUID = videoStreamingPlaylist.Video.uuid
logger.info('Removing duplicated video streaming playlist %s.', videoUUID)
videoStreamingPlaylist.Video.removeStreamingPlaylistFiles(videoStreamingPlaylist, true)
.catch(err => logger.error('Cannot delete video streaming playlist files of %s.', videoUUID, { err }))
}
return undefined
}
static async loadLocalByFileId (videoFileId: number): Promise<MVideoRedundancyVideo> {
const actor = await getServerActor()
const query = {
where: {
actorId: actor.id,
videoFileId
}
}
return VideoRedundancyModel.scope(ScopeNames.WITH_VIDEO).findOne(query)
}
static async listLocalByVideoId (videoId: number): Promise<MVideoRedundancyVideo[]> {
const actor = await getServerActor()
const queryStreamingPlaylist = {
where: {
actorId: actor.id
},
include: [
{
model: VideoStreamingPlaylistModel.unscoped(),
required: true,
include: [
{
model: VideoModel.unscoped(),
required: true,
where: {
id: videoId
}
}
]
}
]
}
const queryFiles = {
where: {
actorId: actor.id
},
include: [
{
model: VideoFileModel,
required: true,
include: [
{
model: VideoModel,
required: true,
where: {
id: videoId
}
}
]
}
]
}
return Promise.all([
VideoRedundancyModel.findAll(queryStreamingPlaylist),
VideoRedundancyModel.findAll(queryFiles)
]).then(([ r1, r2 ]) => r1.concat(r2))
}
static async loadLocalByStreamingPlaylistId (videoStreamingPlaylistId: number): Promise<MVideoRedundancyVideo> {
const actor = await getServerActor()
const query = {
where: {
actorId: actor.id,
videoStreamingPlaylistId
}
}
return VideoRedundancyModel.scope(ScopeNames.WITH_VIDEO).findOne(query)
}
static loadByIdWithVideo (id: number, transaction?: Transaction): Promise<MVideoRedundancyVideo> {
const query = {
where: { id },
transaction
}
return VideoRedundancyModel.scope(ScopeNames.WITH_VIDEO).findOne(query)
}
static loadByUrl (url: string, transaction?: Transaction): Promise<MVideoRedundancy> {
const query = {
where: {
url
},
transaction
}
return VideoRedundancyModel.findOne(query)
}
static async isLocalByVideoUUIDExists (uuid: string) {
const actor = await getServerActor()
const query = {
raw: true,
attributes: [ 'id' ],
where: {
actorId: actor.id
},
include: [
{
attributes: [],
model: VideoFileModel,
required: true,
include: [
{
attributes: [],
model: VideoModel,
required: true,
where: {
uuid
}
}
]
}
]
}
return VideoRedundancyModel.findOne(query)
.then(r => !!r)
}
static async getVideoSample (p: Promise<VideoModel[]>) {
const rows = await p
if (rows.length === 0) return undefined
const ids = rows.map(r => r.id)
const id = sample(ids)
return VideoModel.loadWithFiles(id, undefined, !isTestInstance())
}
static async findMostViewToDuplicate (randomizedFactor: number) {
const peertubeActor = await getServerActor()
// On VideoModel!
const query = {
attributes: [ 'id', 'views' ],
limit: randomizedFactor,
order: getVideoSort('-views'),
where: {
privacy: VideoPrivacy.PUBLIC,
isLive: false,
...this.buildVideoIdsForDuplication(peertubeActor)
},
include: [
VideoRedundancyModel.buildServerRedundancyInclude()
]
}
return VideoRedundancyModel.getVideoSample(VideoModel.unscoped().findAll(query))
}
static async findTrendingToDuplicate (randomizedFactor: number) {
const peertubeActor = await getServerActor()
// On VideoModel!
const query = {
attributes: [ 'id', 'views' ],
subQuery: false,
group: 'VideoModel.id',
limit: randomizedFactor,
order: getVideoSort('-trending'),
where: {
privacy: VideoPrivacy.PUBLIC,
isLive: false,
...this.buildVideoIdsForDuplication(peertubeActor)
},
include: [
VideoRedundancyModel.buildServerRedundancyInclude(),
VideoModel.buildTrendingQuery(CONFIG.TRENDING.VIDEOS.INTERVAL_DAYS)
]
}
return VideoRedundancyModel.getVideoSample(VideoModel.unscoped().findAll(query))
}
static async findRecentlyAddedToDuplicate (randomizedFactor: number, minViews: number) {
const peertubeActor = await getServerActor()
// On VideoModel!
const query = {
attributes: [ 'id', 'publishedAt' ],
limit: randomizedFactor,
order: getVideoSort('-publishedAt'),
where: {
privacy: VideoPrivacy.PUBLIC,
isLive: false,
views: {
[Op.gte]: minViews
},
...this.buildVideoIdsForDuplication(peertubeActor)
},
include: [
VideoRedundancyModel.buildServerRedundancyInclude(),
// Required by publishedAt sort
{
model: ScheduleVideoUpdateModel.unscoped(),
required: false
}
]
}
return VideoRedundancyModel.getVideoSample(VideoModel.unscoped().findAll(query))
}
static async loadOldestLocalExpired (strategy: VideoRedundancyStrategy, expiresAfterMs: number): Promise<MVideoRedundancyVideo> {
const expiredDate = new Date()
expiredDate.setMilliseconds(expiredDate.getMilliseconds() - expiresAfterMs)
const actor = await getServerActor()
const query = {
where: {
actorId: actor.id,
strategy,
createdAt: {
[Op.lt]: expiredDate
}
}
}
return VideoRedundancyModel.scope([ ScopeNames.WITH_VIDEO ]).findOne(query)
}
static async listLocalExpired (): Promise<MVideoRedundancyVideo[]> {
const actor = await getServerActor()
const query = {
where: {
actorId: actor.id,
expiresOn: {
[Op.lt]: new Date()
}
}
}
return VideoRedundancyModel.scope([ ScopeNames.WITH_VIDEO ]).findAll(query)
}
static async listRemoteExpired () {
const actor = await getServerActor()
const query = {
where: {
actorId: {
[Op.ne]: actor.id
},
expiresOn: {
[Op.lt]: new Date(),
[Op.ne]: null
}
}
}
return VideoRedundancyModel.scope([ ScopeNames.WITH_VIDEO ]).findAll(query)
}
static async listLocalOfServer (serverId: number) {
const actor = await getServerActor()
const buildVideoInclude = () => ({
model: VideoModel,
required: true,
include: [
{
attributes: [],
model: VideoChannelModel.unscoped(),
required: true,
include: [
{
attributes: [],
model: ActorModel.unscoped(),
required: true,
where: {
serverId
}
}
]
}
]
})
const query = {
where: {
[Op.and]: [
{
actorId: actor.id
},
{
[Op.or]: [
{
'$VideoStreamingPlaylist.id$': {
[Op.ne]: null
}
},
{
'$VideoFile.id$': {
[Op.ne]: null
}
}
]
}
]
},
include: [
{
model: VideoFileModel.unscoped(),
required: false,
include: [ buildVideoInclude() ]
},
{
model: VideoStreamingPlaylistModel.unscoped(),
required: false,
include: [ buildVideoInclude() ]
}
]
}
return VideoRedundancyModel.findAll(query)
}
static listForApi (options: {
start: number
count: number
sort: string
target: VideoRedundanciesTarget
strategy?: string
}) {
const { start, count, sort, target, strategy } = options
const redundancyWhere: WhereOptions = {}
const videosWhere: WhereOptions = {}
let redundancySqlSuffix = ''
if (target === 'my-videos') {
Object.assign(videosWhere, { remote: false })
} else if (target === 'remote-videos') {
Object.assign(videosWhere, { remote: true })
Object.assign(redundancyWhere, { strategy: { [Op.ne]: null } })
redundancySqlSuffix = ' AND "videoRedundancy"."strategy" IS NOT NULL'
}
if (strategy) {
Object.assign(redundancyWhere, { strategy })
}
const videoFilterWhere = {
[Op.and]: [
{
[Op.or]: [
{
id: {
[Op.in]: literal(
'(' +
'SELECT "videoId" FROM "videoFile" ' +
'INNER JOIN "videoRedundancy" ON "videoRedundancy"."videoFileId" = "videoFile".id' +
redundancySqlSuffix +
')'
)
}
},
{
id: {
[Op.in]: literal(
'(' +
'select "videoId" FROM "videoStreamingPlaylist" ' +
'INNER JOIN "videoRedundancy" ON "videoRedundancy"."videoStreamingPlaylistId" = "videoStreamingPlaylist".id' +
redundancySqlSuffix +
')'
)
}
}
]
},
videosWhere
]
}
// /!\ On video model /!\
const findOptions = {
offset: start,
limit: count,
order: getSort(sort),
include: [
{
required: false,
model: VideoFileModel,
include: [
{
model: VideoRedundancyModel.unscoped(),
required: false,
where: redundancyWhere
}
]
},
{
required: false,
model: VideoStreamingPlaylistModel.unscoped(),
include: [
{
model: VideoRedundancyModel.unscoped(),
required: false,
where: redundancyWhere
},
{
model: VideoFileModel,
required: false
}
]
}
],
where: videoFilterWhere
}
// /!\ On video model /!\
const countOptions = {
where: videoFilterWhere
}
return Promise.all([
VideoModel.findAll(findOptions),
VideoModel.count(countOptions)
]).then(([ data, total ]) => ({ total, data }))
}
static async getStats (strategy: VideoRedundancyStrategyWithManual) {
const actor = await getServerActor()
const sql = `WITH "tmp" AS ` +
`(` +
`SELECT "videoFile"."size" AS "videoFileSize", "videoStreamingFile"."size" AS "videoStreamingFileSize", ` +
`"videoFile"."videoId" AS "videoFileVideoId", "videoStreamingPlaylist"."videoId" AS "videoStreamingVideoId"` +
`FROM "videoRedundancy" AS "videoRedundancy" ` +
`LEFT JOIN "videoFile" AS "videoFile" ON "videoRedundancy"."videoFileId" = "videoFile"."id" ` +
`LEFT JOIN "videoStreamingPlaylist" ON "videoRedundancy"."videoStreamingPlaylistId" = "videoStreamingPlaylist"."id" ` +
`LEFT JOIN "videoFile" AS "videoStreamingFile" ` +
`ON "videoStreamingPlaylist"."id" = "videoStreamingFile"."videoStreamingPlaylistId" ` +
`WHERE "videoRedundancy"."strategy" = :strategy AND "videoRedundancy"."actorId" = :actorId` +
`), ` +
`"videoIds" AS (` +
`SELECT "videoFileVideoId" AS "videoId" FROM "tmp" ` +
`UNION SELECT "videoStreamingVideoId" AS "videoId" FROM "tmp" ` +
`) ` +
`SELECT ` +
`COALESCE(SUM("videoFileSize"), '0') + COALESCE(SUM("videoStreamingFileSize"), '0') AS "totalUsed", ` +
`(SELECT COUNT("videoIds"."videoId") FROM "videoIds") AS "totalVideos", ` +
`COUNT(*) AS "totalVideoFiles" ` +
`FROM "tmp"`
return VideoRedundancyModel.sequelize.query<any>(sql, {
replacements: { strategy, actorId: actor.id },
type: QueryTypes.SELECT
}).then(([ row ]) => ({
totalUsed: parseAggregateResult(row.totalUsed),
totalVideos: row.totalVideos,
totalVideoFiles: row.totalVideoFiles
}))
}
static toFormattedJSONStatic (video: MVideoForRedundancyAPI): VideoRedundancy {
const filesRedundancies: FileRedundancyInformation[] = []
const streamingPlaylistsRedundancies: StreamingPlaylistRedundancyInformation[] = []
for (const file of video.VideoFiles) {
for (const redundancy of file.RedundancyVideos) {
filesRedundancies.push({
id: redundancy.id,
fileUrl: redundancy.fileUrl,
strategy: redundancy.strategy,
createdAt: redundancy.createdAt,
updatedAt: redundancy.updatedAt,
expiresOn: redundancy.expiresOn,
size: file.size
})
}
}
for (const playlist of video.VideoStreamingPlaylists) {
const size = playlist.VideoFiles.reduce((a, b) => a + b.size, 0)
for (const redundancy of playlist.RedundancyVideos) {
streamingPlaylistsRedundancies.push({
id: redundancy.id,
fileUrl: redundancy.fileUrl,
strategy: redundancy.strategy,
createdAt: redundancy.createdAt,
updatedAt: redundancy.updatedAt,
expiresOn: redundancy.expiresOn,
size
})
}
}
return {
id: video.id,
name: video.name,
url: video.url,
uuid: video.uuid,
redundancies: {
files: filesRedundancies,
streamingPlaylists: streamingPlaylistsRedundancies
}
}
}
getVideo () {
if (this.VideoFile?.Video) return this.VideoFile.Video
if (this.VideoStreamingPlaylist?.Video) return this.VideoStreamingPlaylist.Video
return undefined
}
getVideoUUID () {
const video = this.getVideo()
if (!video) return undefined
return video.uuid
}
isOwned () {
return !!this.strategy
}
toActivityPubObject (this: MVideoRedundancyAP): CacheFileObject {
if (this.VideoStreamingPlaylist) {
return {
id: this.url,
type: 'CacheFile' as 'CacheFile',
object: this.VideoStreamingPlaylist.Video.url,
expires: this.expiresOn ? this.expiresOn.toISOString() : null,
url: {
type: 'Link',
mediaType: 'application/x-mpegURL',
href: this.fileUrl
}
}
}
return {
id: this.url,
type: 'CacheFile' as 'CacheFile',
object: this.VideoFile.Video.url,
expires: this.expiresOn
? this.expiresOn.toISOString()
: null,
url: {
type: 'Link',
mediaType: getVideoFileMimeType(this.VideoFile.extname, this.VideoFile.isAudio()),
href: this.fileUrl,
height: this.VideoFile.resolution,
size: this.VideoFile.size,
fps: this.VideoFile.fps
} as ActivityVideoUrlObject
}
}
// Don't include video files we already duplicated
private static buildVideoIdsForDuplication (peertubeActor: MActor) {
const notIn = literal(
'(' +
`SELECT "videoFile"."videoId" AS "videoId" FROM "videoRedundancy" ` +
`INNER JOIN "videoFile" ON "videoFile"."id" = "videoRedundancy"."videoFileId" ` +
`WHERE "videoRedundancy"."actorId" = ${peertubeActor.id} ` +
`UNION ` +
`SELECT "videoStreamingPlaylist"."videoId" AS "videoId" FROM "videoRedundancy" ` +
`INNER JOIN "videoStreamingPlaylist" ON "videoStreamingPlaylist"."id" = "videoRedundancy"."videoStreamingPlaylistId" ` +
`WHERE "videoRedundancy"."actorId" = ${peertubeActor.id} ` +
')'
)
return {
id: {
[Op.notIn]: notIn
}
}
}
private static buildServerRedundancyInclude () {
return {
attributes: [],
model: VideoChannelModel.unscoped(),
required: true,
include: [
{
attributes: [],
model: ActorModel.unscoped(),
required: true,
include: [
{
attributes: [],
model: ServerModel.unscoped(),
required: true,
where: {
redundancyAllowed: true
}
}
]
}
]
}
}
}
+366
ファイルの表示
@@ -0,0 +1,366 @@
import {
RunnerJob,
RunnerJobAdmin,
RunnerJobState,
type RunnerJobPayload,
type RunnerJobPrivatePayload,
type RunnerJobStateType,
type RunnerJobType
} from '@peertube/peertube-models'
import { isArray, isUUIDValid } from '@server/helpers/custom-validators/misc.js'
import { CONSTRAINTS_FIELDS, RUNNER_JOB_STATES } from '@server/initializers/constants.js'
import { MRunnerJob, MRunnerJobRunner, MRunnerJobRunnerParent } from '@server/types/models/runners/index.js'
import { Op, Transaction } from 'sequelize'
import {
AllowNull,
BelongsTo,
Column,
CreatedAt,
DataType,
Default,
ForeignKey,
IsUUID, Scopes,
Table,
UpdatedAt
} from 'sequelize-typescript'
import { SequelizeModel, getSort, searchAttribute } from '../shared/index.js'
import { RunnerModel } from './runner.js'
enum ScopeNames {
WITH_RUNNER = 'WITH_RUNNER',
WITH_PARENT = 'WITH_PARENT'
}
@Scopes(() => ({
[ScopeNames.WITH_RUNNER]: {
include: [
{
model: RunnerModel.unscoped(),
required: false
}
]
},
[ScopeNames.WITH_PARENT]: {
include: [
{
model: RunnerJobModel.unscoped(),
required: false
}
]
}
}))
@Table({
tableName: 'runnerJob',
indexes: [
{
fields: [ 'uuid' ],
unique: true
},
{
fields: [ 'processingJobToken' ],
unique: true
},
{
fields: [ 'runnerId' ]
}
]
})
export class RunnerJobModel extends SequelizeModel<RunnerJobModel> {
@AllowNull(false)
@IsUUID(4)
@Column(DataType.UUID)
uuid: string
@AllowNull(false)
@Column
type: RunnerJobType
@AllowNull(false)
@Column(DataType.JSONB)
payload: RunnerJobPayload
@AllowNull(false)
@Column(DataType.JSONB)
privatePayload: RunnerJobPrivatePayload
@AllowNull(false)
@Column
state: RunnerJobStateType
@AllowNull(false)
@Default(0)
@Column
failures: number
@AllowNull(true)
@Column(DataType.STRING(CONSTRAINTS_FIELDS.RUNNER_JOBS.ERROR_MESSAGE.max))
error: string
// Less has priority
@AllowNull(false)
@Column
priority: number
// Used to fetch the appropriate job when the runner wants to post the result
@AllowNull(true)
@Column
processingJobToken: string
@AllowNull(true)
@Column
progress: number
@AllowNull(true)
@Column
startedAt: Date
@AllowNull(true)
@Column
finishedAt: Date
@CreatedAt
createdAt: Date
@UpdatedAt
updatedAt: Date
@ForeignKey(() => RunnerJobModel)
@Column
dependsOnRunnerJobId: number
@BelongsTo(() => RunnerJobModel, {
foreignKey: {
name: 'dependsOnRunnerJobId',
allowNull: true
},
onDelete: 'cascade'
})
DependsOnRunnerJob: Awaited<RunnerJobModel>
@ForeignKey(() => RunnerModel)
@Column
runnerId: number
@BelongsTo(() => RunnerModel, {
foreignKey: {
name: 'runnerId',
allowNull: true
},
onDelete: 'SET NULL'
})
Runner: Awaited<RunnerModel>
// ---------------------------------------------------------------------------
static loadWithRunner (uuid: string) {
const query = {
where: { uuid }
}
return RunnerJobModel.scope(ScopeNames.WITH_RUNNER).findOne<MRunnerJobRunner>(query)
}
static loadByRunnerAndJobTokensWithRunner (options: {
uuid: string
runnerToken: string
jobToken: string
}) {
const { uuid, runnerToken, jobToken } = options
const query = {
where: {
uuid,
processingJobToken: jobToken
},
include: {
model: RunnerModel.unscoped(),
required: true,
where: {
runnerToken
}
}
}
return RunnerJobModel.findOne<MRunnerJobRunner>(query)
}
static listAvailableJobs () {
const query = {
limit: 10,
order: getSort('priority'),
where: {
state: RunnerJobState.PENDING
}
}
return RunnerJobModel.findAll<MRunnerJob>(query)
}
static listStalledJobs (options: {
staleTimeMS: number
types: RunnerJobType[]
}) {
const before = new Date(Date.now() - options.staleTimeMS)
return RunnerJobModel.findAll<MRunnerJob>({
where: {
type: {
[Op.in]: options.types
},
state: RunnerJobState.PROCESSING,
updatedAt: {
[Op.lt]: before
}
}
})
}
static listChildrenOf (job: MRunnerJob, transaction?: Transaction) {
const query = {
where: {
dependsOnRunnerJobId: job.id
},
transaction
}
return RunnerJobModel.findAll<MRunnerJob>(query)
}
static listForApi (options: {
start: number
count: number
sort: string
search?: string
stateOneOf?: RunnerJobStateType[]
}) {
const { start, count, sort, search, stateOneOf } = options
const query = {
offset: start,
limit: count,
order: getSort(sort),
where: []
}
if (search) {
if (isUUIDValid(search)) {
query.where.push({ uuid: search })
} else {
query.where.push({
[Op.or]: [
searchAttribute(search, 'type'),
searchAttribute(search, '$Runner.name$')
]
})
}
}
if (isArray(stateOneOf) && stateOneOf.length !== 0) {
query.where.push({
state: {
[Op.in]: stateOneOf
}
})
}
return Promise.all([
RunnerJobModel.scope([ ScopeNames.WITH_RUNNER ]).count(query),
RunnerJobModel.scope([ ScopeNames.WITH_RUNNER, ScopeNames.WITH_PARENT ]).findAll<MRunnerJobRunnerParent>(query)
]).then(([ total, data ]) => ({ total, data }))
}
static updateDependantJobsOf (runnerJob: MRunnerJob) {
const where = {
dependsOnRunnerJobId: runnerJob.id
}
return RunnerJobModel.update({ state: RunnerJobState.PENDING }, { where })
}
static cancelAllNonFinishedJobs (options: { type: RunnerJobType }) {
const where = {
type: options.type,
state: {
[Op.in]: [ RunnerJobState.COMPLETING, RunnerJobState.PENDING, RunnerJobState.PROCESSING, RunnerJobState.WAITING_FOR_PARENT_JOB ]
}
}
return RunnerJobModel.update({ state: RunnerJobState.CANCELLED }, { where })
}
// ---------------------------------------------------------------------------
resetToPending () {
this.state = RunnerJobState.PENDING
this.processingJobToken = null
this.progress = null
this.startedAt = null
this.runnerId = null
}
setToErrorOrCancel (
// eslint-disable-next-line max-len
state: typeof RunnerJobState.PARENT_ERRORED | typeof RunnerJobState.ERRORED | typeof RunnerJobState.CANCELLED | typeof RunnerJobState.PARENT_CANCELLED
) {
this.state = state
this.processingJobToken = null
this.finishedAt = new Date()
}
toFormattedJSON (this: MRunnerJobRunnerParent): RunnerJob {
const runner = this.Runner
? {
id: this.Runner.id,
name: this.Runner.name,
description: this.Runner.description
}
: null
const parent = this.DependsOnRunnerJob
? {
id: this.DependsOnRunnerJob.id,
uuid: this.DependsOnRunnerJob.uuid,
type: this.DependsOnRunnerJob.type,
state: {
id: this.DependsOnRunnerJob.state,
label: RUNNER_JOB_STATES[this.DependsOnRunnerJob.state]
}
}
: undefined
return {
uuid: this.uuid,
type: this.type,
state: {
id: this.state,
label: RUNNER_JOB_STATES[this.state]
},
progress: this.progress,
priority: this.priority,
failures: this.failures,
error: this.error,
payload: this.payload,
startedAt: this.startedAt?.toISOString(),
finishedAt: this.finishedAt?.toISOString(),
createdAt: this.createdAt.toISOString(),
updatedAt: this.updatedAt.toISOString(),
parent,
runner
}
}
toFormattedAdminJSON (this: MRunnerJobRunnerParent): RunnerJobAdmin {
return {
...this.toFormattedJSON(),
privatePayload: this.privatePayload
}
}
}
+102
ファイルの表示
@@ -0,0 +1,102 @@
import { FindOptions, literal } from 'sequelize'
import { AllowNull, Column, CreatedAt, HasMany, Table, UpdatedAt } from 'sequelize-typescript'
import { MRunnerRegistrationToken } from '@server/types/models/runners/index.js'
import { RunnerRegistrationToken } from '@peertube/peertube-models'
import { SequelizeModel, getSort } from '../shared/index.js'
import { RunnerModel } from './runner.js'
/**
*
* Tokens used by PeerTube runners to register themselves to the PeerTube instance
*
*/
@Table({
tableName: 'runnerRegistrationToken',
indexes: [
{
fields: [ 'registrationToken' ],
unique: true
}
]
})
export class RunnerRegistrationTokenModel extends SequelizeModel<RunnerRegistrationTokenModel> {
@AllowNull(false)
@Column
registrationToken: string
@CreatedAt
createdAt: Date
@UpdatedAt
updatedAt: Date
@HasMany(() => RunnerModel, {
foreignKey: {
allowNull: true
},
onDelete: 'cascade'
})
Runners: Awaited<RunnerModel>[]
static load (id: number) {
return RunnerRegistrationTokenModel.findByPk(id)
}
static loadByRegistrationToken (registrationToken: string) {
const query = {
where: { registrationToken }
}
return RunnerRegistrationTokenModel.findOne(query)
}
static countTotal () {
return RunnerRegistrationTokenModel.unscoped().count()
}
static listForApi (options: {
start: number
count: number
sort: string
}) {
const { start, count, sort } = options
const query: FindOptions = {
attributes: {
include: [
[
literal('(SELECT COUNT(*) FROM "runner" WHERE "runner"."runnerRegistrationTokenId" = "RunnerRegistrationTokenModel"."id")'),
'registeredRunnersCount'
]
]
},
offset: start,
limit: count,
order: getSort(sort)
}
return Promise.all([
RunnerRegistrationTokenModel.count(query),
RunnerRegistrationTokenModel.findAll<MRunnerRegistrationToken>(query)
]).then(([ total, data ]) => ({ total, data }))
}
// ---------------------------------------------------------------------------
toFormattedJSON (this: MRunnerRegistrationToken): RunnerRegistrationToken {
const registeredRunnersCount = this.get('registeredRunnersCount') as number
return {
id: this.id,
registrationToken: this.registrationToken,
createdAt: this.createdAt,
updatedAt: this.updatedAt,
registeredRunnersCount
}
}
}
+123
ファイルの表示
@@ -0,0 +1,123 @@
import { FindOptions } from 'sequelize'
import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Table, UpdatedAt } from 'sequelize-typescript'
import { MRunner } from '@server/types/models/runners/index.js'
import { Runner } from '@peertube/peertube-models'
import { SequelizeModel, getSort } from '../shared/index.js'
import { RunnerRegistrationTokenModel } from './runner-registration-token.js'
import { CONSTRAINTS_FIELDS } from '@server/initializers/constants.js'
@Table({
tableName: 'runner',
indexes: [
{
fields: [ 'runnerToken' ],
unique: true
},
{
fields: [ 'runnerRegistrationTokenId' ]
},
{
fields: [ 'name' ],
unique: true
}
]
})
export class RunnerModel extends SequelizeModel<RunnerModel> {
// Used to identify the appropriate runner when it uses the runner REST API
@AllowNull(false)
@Column
runnerToken: string
@AllowNull(false)
@Column
name: string
@AllowNull(true)
@Column(DataType.STRING(CONSTRAINTS_FIELDS.RUNNERS.DESCRIPTION.max))
description: string
@AllowNull(false)
@Column
lastContact: Date
@AllowNull(false)
@Column
ip: string
@CreatedAt
createdAt: Date
@UpdatedAt
updatedAt: Date
@ForeignKey(() => RunnerRegistrationTokenModel)
@Column
runnerRegistrationTokenId: number
@BelongsTo(() => RunnerRegistrationTokenModel, {
foreignKey: {
allowNull: false
},
onDelete: 'cascade'
})
RunnerRegistrationToken: Awaited<RunnerRegistrationTokenModel>
// ---------------------------------------------------------------------------
static load (id: number) {
return RunnerModel.findByPk(id)
}
static loadByToken (runnerToken: string) {
const query = {
where: { runnerToken }
}
return RunnerModel.findOne(query)
}
static loadByName (name: string) {
const query = {
where: { name }
}
return RunnerModel.findOne(query)
}
static listForApi (options: {
start: number
count: number
sort: string
}) {
const { start, count, sort } = options
const query: FindOptions = {
offset: start,
limit: count,
order: getSort(sort)
}
return Promise.all([
RunnerModel.count(query),
RunnerModel.findAll<MRunner>(query)
]).then(([ total, data ]) => ({ total, data }))
}
// ---------------------------------------------------------------------------
toFormattedJSON (this: MRunner): Runner {
return {
id: this.id,
name: this.name,
description: this.description,
ip: this.ip,
lastContact: this.lastContact,
createdAt: this.createdAt,
updatedAt: this.updatedAt
}
}
}
+316
ファイルの表示
@@ -0,0 +1,316 @@
import {
PeerTubePlugin,
PluginType,
RegisterServerSettingOptions,
SettingEntries,
SettingValue,
type PluginType_Type
} from '@peertube/peertube-models'
import { MPlugin, MPluginFormattable } from '@server/types/models/index.js'
import { FindAndCountOptions, QueryTypes, json } from 'sequelize'
import { AllowNull, Column, CreatedAt, DataType, DefaultScope, Is, Table, UpdatedAt } from 'sequelize-typescript'
import {
isPluginDescriptionValid,
isPluginHomepage,
isPluginNameValid,
isPluginStableOrUnstableVersionValid,
isPluginStableVersionValid,
isPluginTypeValid
} from '../../helpers/custom-validators/plugins.js'
import { SequelizeModel, getSort, throwIfNotValid } from '../shared/index.js'
@DefaultScope(() => ({
attributes: {
exclude: [ 'storage' ]
}
}))
@Table({
tableName: 'plugin',
indexes: [
{
fields: [ 'name', 'type' ],
unique: true
}
]
})
export class PluginModel extends SequelizeModel<PluginModel> {
@AllowNull(false)
@Is('PluginName', value => throwIfNotValid(value, isPluginNameValid, 'name'))
@Column
name: string
@AllowNull(false)
@Is('PluginType', value => throwIfNotValid(value, isPluginTypeValid, 'type'))
@Column
type: PluginType_Type
@AllowNull(false)
@Is('PluginVersion', value => throwIfNotValid(value, isPluginStableOrUnstableVersionValid, 'version'))
@Column
version: string
@AllowNull(true)
@Is('PluginLatestVersion', value => throwIfNotValid(value, isPluginStableVersionValid, 'version'))
@Column
latestVersion: string
@AllowNull(false)
@Column
enabled: boolean
@AllowNull(false)
@Column
uninstalled: boolean
@AllowNull(false)
@Column
peertubeEngine: string
@AllowNull(true)
@Is('PluginDescription', value => throwIfNotValid(value, isPluginDescriptionValid, 'description'))
@Column
description: string
@AllowNull(false)
@Is('PluginHomepage', value => throwIfNotValid(value, isPluginHomepage, 'homepage'))
@Column
homepage: string
@AllowNull(true)
@Column(DataType.JSONB)
settings: any
@AllowNull(true)
@Column(DataType.JSONB)
storage: any
@CreatedAt
createdAt: Date
@UpdatedAt
updatedAt: Date
static listEnabledPluginsAndThemes (): Promise<MPlugin[]> {
const query = {
where: {
enabled: true,
uninstalled: false
}
}
return PluginModel.findAll(query)
}
static loadByNpmName (npmName: string): Promise<MPlugin> {
const name = this.normalizePluginName(npmName)
const type = this.getTypeFromNpmName(npmName)
const query = {
where: {
name,
type
}
}
return PluginModel.findOne(query)
}
static getSetting (
pluginName: string,
pluginType: PluginType_Type,
settingName: string,
registeredSettings: RegisterServerSettingOptions[]
) {
const query = {
attributes: [ 'settings' ],
where: {
name: pluginName,
type: pluginType
}
}
return PluginModel.findOne(query)
.then(p => {
if (!p?.settings || p.settings === undefined) {
const registered = registeredSettings.find(s => s.name === settingName)
if (!registered || registered.default === undefined) return undefined
return registered.default
}
return p.settings[settingName]
})
}
static getSettings (
pluginName: string,
pluginType: PluginType_Type,
settingNames: string[],
registeredSettings: RegisterServerSettingOptions[]
) {
const query = {
attributes: [ 'settings' ],
where: {
name: pluginName,
type: pluginType
}
}
return PluginModel.findOne(query)
.then(p => {
const result: SettingEntries = {}
for (const name of settingNames) {
if (!p?.settings || p.settings[name] === undefined) {
const registered = registeredSettings.find(s => s.name === name)
if (registered?.default !== undefined) {
result[name] = registered.default
}
} else {
result[name] = p.settings[name]
}
}
return result
})
}
static setSetting (pluginName: string, pluginType: PluginType_Type, settingName: string, settingValue: SettingValue) {
const query = {
where: {
name: pluginName,
type: pluginType
}
}
const toSave = {
[`settings.${settingName}`]: settingValue
}
return PluginModel.update(toSave, query)
.then(() => undefined)
}
static getData (pluginName: string, pluginType: PluginType_Type, key: string) {
const query = {
raw: true,
attributes: [ [ json('storage.' + key), 'value' ] as any ], // FIXME: typings
where: {
name: pluginName,
type: pluginType
}
}
return PluginModel.findOne(query)
.then((c: any) => {
if (!c) return undefined
const value = c.value
try {
return JSON.parse(value)
} catch {
return value
}
})
}
static storeData (pluginName: string, pluginType: PluginType_Type, key: string, data: any) {
const query = 'UPDATE "plugin" SET "storage" = jsonb_set(coalesce("storage", \'{}\'), :key, :data::jsonb) ' +
'WHERE "name" = :pluginName AND "type" = :pluginType'
const jsonPath = '{' + key + '}'
const options = {
replacements: { pluginName, pluginType, key: jsonPath, data: JSON.stringify(data) },
type: QueryTypes.UPDATE
}
return PluginModel.sequelize.query(query, options)
.then(() => undefined)
}
static listForApi (options: {
pluginType?: PluginType_Type
uninstalled?: boolean
start: number
count: number
sort: string
}) {
const { uninstalled = false } = options
const query: FindAndCountOptions = {
offset: options.start,
limit: options.count,
order: getSort(options.sort),
where: {
uninstalled
}
}
if (options.pluginType) query.where['type'] = options.pluginType
return Promise.all([
PluginModel.count(query),
PluginModel.findAll<MPlugin>(query)
]).then(([ total, data ]) => ({ total, data }))
}
static listInstalled (): Promise<MPlugin[]> {
const query = {
where: {
uninstalled: false
}
}
return PluginModel.findAll(query)
}
static normalizePluginName (npmName: string) {
return npmName.replace(/^peertube-((theme)|(plugin))-/, '')
}
static getTypeFromNpmName (npmName: string) {
return npmName.startsWith('peertube-plugin-')
? PluginType.PLUGIN
: PluginType.THEME
}
static buildNpmName (name: string, type: PluginType_Type) {
if (type === PluginType.THEME) return 'peertube-theme-' + name
return 'peertube-plugin-' + name
}
getPublicSettings (registeredSettings: RegisterServerSettingOptions[]) {
const result: SettingEntries = {}
const settings = this.settings || {}
for (const r of registeredSettings) {
if (r.private !== false) continue
result[r.name] = settings[r.name] ?? r.default ?? null
}
return result
}
toFormattedJSON (this: MPluginFormattable): PeerTubePlugin {
return {
name: this.name,
type: this.type,
version: this.version,
latestVersion: this.latestVersion,
enabled: this.enabled,
uninstalled: this.uninstalled,
peertubeEngine: this.peertubeEngine,
description: this.description,
homepage: this.homepage,
settings: this.settings,
createdAt: this.createdAt,
updatedAt: this.updatedAt
}
}
}
+189
ファイルの表示
@@ -0,0 +1,189 @@
import { Op, QueryTypes } from 'sequelize'
import { BelongsTo, Column, CreatedAt, ForeignKey, Scopes, Table, UpdatedAt } from 'sequelize-typescript'
import { MServerBlocklist, MServerBlocklistAccountServer, MServerBlocklistFormattable } from '@server/types/models/index.js'
import { ServerBlock } from '@peertube/peertube-models'
import { AccountModel } from '../account/account.js'
import { SequelizeModel, createSafeIn, getSort, searchAttribute } from '../shared/index.js'
import { ServerModel } from './server.js'
enum ScopeNames {
WITH_ACCOUNT = 'WITH_ACCOUNT',
WITH_SERVER = 'WITH_SERVER'
}
@Scopes(() => ({
[ScopeNames.WITH_ACCOUNT]: {
include: [
{
model: AccountModel,
required: true
}
]
},
[ScopeNames.WITH_SERVER]: {
include: [
{
model: ServerModel,
required: true
}
]
}
}))
@Table({
tableName: 'serverBlocklist',
indexes: [
{
fields: [ 'accountId', 'targetServerId' ],
unique: true
},
{
fields: [ 'targetServerId' ]
}
]
})
export class ServerBlocklistModel extends SequelizeModel<ServerBlocklistModel> {
@CreatedAt
createdAt: Date
@UpdatedAt
updatedAt: Date
@ForeignKey(() => AccountModel)
@Column
accountId: number
@BelongsTo(() => AccountModel, {
foreignKey: {
name: 'accountId',
allowNull: false
},
onDelete: 'CASCADE'
})
ByAccount: Awaited<AccountModel>
@ForeignKey(() => ServerModel)
@Column
targetServerId: number
@BelongsTo(() => ServerModel, {
foreignKey: {
allowNull: false
},
onDelete: 'CASCADE'
})
BlockedServer: Awaited<ServerModel>
static isServerMutedByAccounts (accountIds: number[], targetServerId: number) {
const query = {
attributes: [ 'accountId', 'id' ],
where: {
accountId: {
[Op.in]: accountIds
},
targetServerId
},
raw: true
}
return ServerBlocklistModel.unscoped()
.findAll(query)
.then(rows => {
const result: { [accountId: number]: boolean } = {}
for (const accountId of accountIds) {
result[accountId] = !!rows.find(r => r.accountId === accountId)
}
return result
})
}
static loadByAccountAndHost (accountId: number, host: string): Promise<MServerBlocklist> {
const query = {
where: {
accountId
},
include: [
{
model: ServerModel,
where: {
host
},
required: true
}
]
}
return ServerBlocklistModel.findOne(query)
}
static listHostsBlockedBy (accountIds: number[]): Promise<string[]> {
const query = {
attributes: [ ],
where: {
accountId: {
[Op.in]: accountIds
}
},
include: [
{
attributes: [ 'host' ],
model: ServerModel.unscoped(),
required: true
}
]
}
return ServerBlocklistModel.findAll(query)
.then(entries => entries.map(e => e.BlockedServer.host))
}
static getBlockStatus (byAccountIds: number[], hosts: string[]): Promise<{ host: string, accountId: number }[]> {
const rawQuery = `SELECT "server"."host", "serverBlocklist"."accountId" ` +
`FROM "serverBlocklist" ` +
`INNER JOIN "server" ON "server"."id" = "serverBlocklist"."targetServerId" ` +
`WHERE "server"."host" IN (:hosts) ` +
`AND "serverBlocklist"."accountId" IN (${createSafeIn(ServerBlocklistModel.sequelize, byAccountIds)})`
return ServerBlocklistModel.sequelize.query(rawQuery, {
type: QueryTypes.SELECT as QueryTypes.SELECT,
replacements: { hosts }
})
}
static listForApi (parameters: {
start: number
count: number
sort: string
search?: string
accountId: number
}) {
const { start, count, sort, search, accountId } = parameters
const query = {
offset: start,
limit: count,
order: getSort(sort),
where: {
accountId,
...searchAttribute(search, '$BlockedServer.host$')
}
}
return Promise.all([
ServerBlocklistModel.scope(ScopeNames.WITH_SERVER).count(query),
ServerBlocklistModel.scope([ ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_SERVER ]).findAll<MServerBlocklistAccountServer>(query)
]).then(([ total, data ]) => ({ total, data }))
}
toFormattedJSON (this: MServerBlocklistFormattable): ServerBlock {
return {
byAccount: this.ByAccount.toFormattedJSON(),
blockedServer: this.BlockedServer.toFormattedJSON(),
createdAt: this.createdAt
}
}
}
+103
ファイルの表示
@@ -0,0 +1,103 @@
import { Transaction } from 'sequelize'
import { AllowNull, Column, CreatedAt, Default, HasMany, Is, Table, UpdatedAt } from 'sequelize-typescript'
import { MServer, MServerFormattable } from '@server/types/models/server/index.js'
import { isHostValid } from '../../helpers/custom-validators/servers.js'
import { ActorModel } from '../actor/actor.js'
import { SequelizeModel, buildSQLAttributes, throwIfNotValid } from '../shared/index.js'
import { ServerBlocklistModel } from './server-blocklist.js'
@Table({
tableName: 'server',
indexes: [
{
fields: [ 'host' ],
unique: true
}
]
})
export class ServerModel extends SequelizeModel<ServerModel> {
@AllowNull(false)
@Is('Host', value => throwIfNotValid(value, isHostValid, 'valid host'))
@Column
host: string
@AllowNull(false)
@Default(false)
@Column
redundancyAllowed: boolean
@CreatedAt
createdAt: Date
@UpdatedAt
updatedAt: Date
@HasMany(() => ActorModel, {
foreignKey: {
name: 'serverId',
allowNull: true
},
onDelete: 'CASCADE',
hooks: true
})
Actors: Awaited<ActorModel>[]
@HasMany(() => ServerBlocklistModel, {
foreignKey: {
allowNull: false
},
onDelete: 'CASCADE'
})
BlockedBy: Awaited<ServerBlocklistModel>[]
// ---------------------------------------------------------------------------
static getSQLAttributes (tableName: string, aliasPrefix = '') {
return buildSQLAttributes({
model: this,
tableName,
aliasPrefix
})
}
// ---------------------------------------------------------------------------
static load (id: number, transaction?: Transaction): Promise<MServer> {
const query = {
where: {
id
},
transaction
}
return ServerModel.findOne(query)
}
static loadByHost (host: string): Promise<MServer> {
const query = {
where: {
host
}
}
return ServerModel.findOne(query)
}
static async loadOrCreateByHost (host: string) {
let server = await ServerModel.loadByHost(host)
if (!server) server = await ServerModel.create({ host })
return server
}
isBlocked () {
return this.BlockedBy && this.BlockedBy.length !== 0
}
toFormattedJSON (this: MServerFormattable) {
return {
host: this.host
}
}
}
+74
ファイルの表示
@@ -0,0 +1,74 @@
import { AllowNull, BelongsToMany, Column, CreatedAt, Table, UpdatedAt } from 'sequelize-typescript'
import { Transaction } from 'sequelize'
import { MTracker } from '@server/types/models/server/tracker.js'
import { VideoModel } from '../video/video.js'
import { VideoTrackerModel } from './video-tracker.js'
import { SequelizeModel } from '../shared/sequelize-type.js'
@Table({
tableName: 'tracker',
indexes: [
{
fields: [ 'url' ],
unique: true
}
]
})
export class TrackerModel extends SequelizeModel<TrackerModel> {
@AllowNull(false)
@Column
url: string
@CreatedAt
createdAt: Date
@UpdatedAt
updatedAt: Date
@BelongsToMany(() => VideoModel, {
foreignKey: 'trackerId',
through: () => VideoTrackerModel,
onDelete: 'CASCADE'
})
Videos: Awaited<VideoModel>[]
static listUrlsByVideoId (videoId: number) {
const query = {
include: [
{
attributes: [ 'id' ],
model: VideoModel.unscoped(),
required: true,
where: { id: videoId }
}
]
}
return TrackerModel.findAll(query)
.then(rows => rows.map(rows => rows.url))
}
static findOrCreateTrackers (trackers: string[], transaction: Transaction): Promise<MTracker[]> {
if (trackers === null) return Promise.resolve([])
const tasks: Promise<MTracker>[] = []
trackers.forEach(tracker => {
const query = {
where: {
url: tracker
},
defaults: {
url: tracker
},
transaction
}
const promise = TrackerModel.findOrCreate<MTracker>(query)
.then(([ trackerInstance ]) => trackerInstance)
tasks.push(promise)
})
return Promise.all(tasks)
}
}
+31
ファイルの表示
@@ -0,0 +1,31 @@
import { Column, CreatedAt, ForeignKey, Table, UpdatedAt } from 'sequelize-typescript'
import { VideoModel } from '../video/video.js'
import { TrackerModel } from './tracker.js'
import { SequelizeModel } from '../shared/index.js'
@Table({
tableName: 'videoTracker',
indexes: [
{
fields: [ 'videoId' ]
},
{
fields: [ 'trackerId' ]
}
]
})
export class VideoTrackerModel extends SequelizeModel<VideoTrackerModel> {
@CreatedAt
createdAt: Date
@UpdatedAt
updatedAt: Date
@ForeignKey(() => VideoModel)
@Column
videoId: number
@ForeignKey(() => TrackerModel)
@Column
trackerId: number
}
+32
ファイルの表示
@@ -0,0 +1,32 @@
import { QueryTypes, Sequelize, Transaction } from 'sequelize'
/**
*
* Abstract builder to run video SQL queries
*
*/
export class AbstractRunQuery {
protected query: string
protected replacements: any = {}
constructor (protected readonly sequelize: Sequelize) {
}
protected runQuery (options: { nest?: boolean, transaction?: Transaction, logging?: boolean } = {}) {
const queryOptions = {
transaction: options.transaction,
logging: options.logging,
replacements: this.replacements,
type: QueryTypes.SELECT as QueryTypes.SELECT,
nest: options.nest ?? false
}
return this.sequelize.query<any>(this.query, queryOptions)
}
protected buildSelect (entities: string[]) {
return `SELECT ${entities.join(', ')} `
}
}
+9
ファイルの表示
@@ -0,0 +1,9 @@
export * from './abstract-run-query.js'
export * from './model-builder.js'
export * from './model-cache.js'
export * from './query.js'
export * from './sequelize-helpers.js'
export * from './sequelize-type.js'
export * from './sort.js'
export * from './sql.js'
export * from './update.js'
+119
ファイルの表示
@@ -0,0 +1,119 @@
import { logger } from '@server/helpers/logger.js'
import isPlainObject from 'lodash-es/isPlainObject.js'
import { ModelStatic, Sequelize, Model as SequelizeModel } from 'sequelize'
/**
*
* Build Sequelize models from sequelize raw query (that must use { nest: true } options)
*
* In order to sequelize to correctly build the JSON this class will ingest,
* the columns selected in the raw query should be in the following form:
* * All tables must be Pascal Cased (for example "VideoChannel")
* * Root table must end with `Model` (for example "VideoCommentModel")
* * Joined tables must contain the origin table name + '->JoinedTable'. For example:
* * "Actor" is joined to "Account": "Actor" table must be renamed "Account->Actor"
* * "Account->Actor" is joined to "Server": "Server" table must be renamed to "Account->Actor->Server"
* * Selected columns must be renamed to contain the JSON path:
* * "videoComment"."id": "VideoCommentModel"."id"
* * "Account"."Actor"."Server"."id": "Account.Actor.Server.id"
* * All tables must contain the row id
*/
export class ModelBuilder <T extends SequelizeModel> {
private readonly modelRegistry = new Map<string, T>()
constructor (private readonly sequelize: Sequelize) {
}
createModels (jsonArray: any[], baseModelName: string): T[] {
const result: T[] = []
for (const json of jsonArray) {
const { created, model } = this.createModel(json, baseModelName, json.id + '.' + baseModelName)
if (created) result.push(model)
}
return result
}
private createModel (json: any, modelName: string, keyPath: string) {
if (!json.id) return { created: false, model: null }
const { created, model } = this.createOrFindModel(json, modelName, keyPath)
for (const key of Object.keys(json)) {
const value = json[key]
if (!value) continue
// Child model
if (isPlainObject(value)) {
const { created, model: subModel } = this.createModel(value, key, `${keyPath}.${json.id}.${key}`)
if (!created || !subModel) continue
const Model = this.findModelBuilder(modelName)
const association = Model.associations[key]
if (!association) {
logger.error('Cannot find association %s of model %s', key, modelName, { associations: Object.keys(Model.associations) })
continue
}
if (association.isMultiAssociation) {
if (!Array.isArray(model[key])) model[key] = []
model[key].push(subModel)
} else {
model[key] = subModel
}
}
}
return { created, model }
}
private createOrFindModel (json: any, modelName: string, keyPath: string) {
const registryKey = this.getModelRegistryKey(json, keyPath)
if (this.modelRegistry.has(registryKey)) {
return {
created: false,
model: this.modelRegistry.get(registryKey)
}
}
const Model = this.findModelBuilder(modelName)
if (!Model) {
logger.error(
'Cannot build model %s that does not exist', this.buildSequelizeModelName(modelName),
{ existing: this.sequelize.modelManager.all.map(m => m.name) }
)
return { created: false, model: null }
}
const model = Model.build(json, { raw: true, isNewRecord: false })
this.modelRegistry.set(registryKey, model)
return { created: true, model }
}
private findModelBuilder (modelName: string) {
return this.sequelize.modelManager.getModel(this.buildSequelizeModelName(modelName)) as ModelStatic<T>
}
private buildSequelizeModelName (modelName: string) {
if (modelName === 'Avatars') return 'ActorImageModel'
if (modelName === 'ActorFollowing') return 'ActorModel'
if (modelName === 'ActorFollower') return 'ActorModel'
if (modelName === 'FlaggedAccount') return 'AccountModel'
if (modelName === 'CommentAutomaticTags') return 'CommentAutomaticTagModel'
return modelName + 'Model'
}
private getModelRegistryKey (json: any, keyPath: string) {
return keyPath + json.id
}
}
+94
ファイルの表示
@@ -0,0 +1,94 @@
import { Model } from 'sequelize-typescript'
import { logger } from '@server/helpers/logger.js'
type ModelCacheType =
'server-account'
| 'local-actor-name'
| 'local-actor-url'
| 'load-video-immutable-id'
| 'load-video-immutable-url'
type DeleteKey =
'video'
class ModelCache {
private static instance: ModelCache
private readonly localCache: { [id in ModelCacheType]: Map<string, any> } = {
'server-account': new Map(),
'local-actor-name': new Map(),
'local-actor-url': new Map(),
'load-video-immutable-id': new Map(),
'load-video-immutable-url': new Map()
}
private readonly deleteIds: {
[deleteKey in DeleteKey]: Map<number, { cacheType: ModelCacheType, key: string }[]>
} = {
video: new Map()
}
private constructor () {
}
static get Instance () {
return this.instance || (this.instance = new this())
}
doCache<T extends Model> (options: {
cacheType: ModelCacheType
key: string
fun: () => Promise<T>
whitelist?: () => boolean
deleteKey?: DeleteKey
}) {
const { cacheType, key, fun, whitelist, deleteKey } = options
if (whitelist && whitelist() !== true) return fun()
const cache = this.localCache[cacheType]
if (cache.has(key)) {
logger.debug('Model cache hit for %s -> %s.', cacheType, key)
return Promise.resolve<T>(cache.get(key))
}
return fun().then(m => {
if (!m) return m
if (!whitelist || whitelist()) cache.set(key, m)
if (deleteKey) {
const map = this.deleteIds[deleteKey]
if (!map.has(m.id)) map.set(m.id, [])
const a = map.get(m.id)
a.push({ cacheType, key })
}
return m
})
}
invalidateCache (deleteKey: DeleteKey, modelId: number) {
const map = this.deleteIds[deleteKey]
if (!map.has(modelId)) return
for (const toDelete of map.get(modelId)) {
logger.debug('Removing %s -> %d of model cache %s -> %s.', deleteKey, modelId, toDelete.cacheType, toDelete.key)
this.localCache[toDelete.cacheType].delete(toDelete.key)
}
map.delete(modelId)
}
clearCache (cacheType: ModelCacheType) {
this.localCache[cacheType] = new Map()
}
}
export {
ModelCache
}
+88
ファイルの表示
@@ -0,0 +1,88 @@
import { forceNumber } from '@peertube/peertube-core-utils'
import { BindOrReplacements, Op, QueryOptionsWithType, QueryTypes, Sequelize, Transaction } from 'sequelize'
import { Fn } from 'sequelize/types/utils'
import validator from 'validator'
async function doesExist (options: {
sequelize: Sequelize
query: string
bind?: BindOrReplacements
transaction?: Transaction
}) {
const { sequelize, query, bind, transaction } = options
const queryOptions: QueryOptionsWithType<QueryTypes.SELECT> = {
type: QueryTypes.SELECT,
bind,
raw: true,
transaction
}
const results = await sequelize.query(query, queryOptions)
return results.length === 1
}
// FIXME: have to specify the result type to not break peertube typings generation
function createSimilarityAttribute (col: string, value: string): Fn {
return Sequelize.fn(
'similarity',
searchTrigramNormalizeCol(col),
searchTrigramNormalizeValue(value)
)
}
function buildWhereIdOrUUID (id: number | string) {
return validator.default.isInt('' + id) ? { id } : { uuid: id }
}
function parseAggregateResult (result: any) {
if (!result) return 0
const total = forceNumber(result)
if (isNaN(total)) return 0
return total
}
function parseRowCountResult (result: any) {
if (result.length !== 0) return result[0].total
return 0
}
function createSafeIn (sequelize: Sequelize, toEscape: (string | number)[], additionalUnescaped: string[] = []) {
return toEscape.map(t => {
return t === null
? null
: sequelize.escape('' + t)
}).concat(additionalUnescaped).join(', ')
}
function searchAttribute (sourceField?: string, targetField?: string) {
if (!sourceField) return {}
return {
[targetField]: {
// FIXME: ts error
[Op.iLike as any]: `%${sourceField}%`
}
}
}
export {
buildWhereIdOrUUID, createSafeIn, createSimilarityAttribute, doesExist, parseAggregateResult,
parseRowCountResult, searchAttribute
}
// ---------------------------------------------------------------------------
function searchTrigramNormalizeValue (value: string) {
return Sequelize.fn('lower', Sequelize.fn('immutable_unaccent', value))
}
function searchTrigramNormalizeCol (col: string) {
return Sequelize.fn('lower', Sequelize.fn('immutable_unaccent', Sequelize.col(col)))
}
+39
ファイルの表示
@@ -0,0 +1,39 @@
import { Sequelize } from 'sequelize'
function isOutdated (model: { createdAt: Date, updatedAt: Date }, refreshInterval: number) {
if (!model.createdAt || !model.updatedAt) {
throw new Error('Miss createdAt & updatedAt attributes to model')
}
const now = Date.now()
const createdAtTime = model.createdAt.getTime()
const updatedAtTime = model.updatedAt.getTime()
return (now - createdAtTime) > refreshInterval && (now - updatedAtTime) > refreshInterval
}
function throwIfNotValid (value: any, validator: (value: any) => boolean, fieldName = 'value', nullable = false) {
if (nullable && (value === null || value === undefined)) return
if (validator(value) === false) {
throw new Error(`"${value}" is not a valid ${fieldName}.`)
}
}
function buildTrigramSearchIndex (indexName: string, attribute: string) {
return {
name: indexName,
// FIXME: gin_trgm_ops is not taken into account in Sequelize 6, so adding it ourselves in the literal function
fields: [ Sequelize.literal('lower(immutable_unaccent(' + attribute + ')) gin_trgm_ops') as any ],
using: 'gin',
operator: 'gin_trgm_ops'
}
}
// ---------------------------------------------------------------------------
export {
throwIfNotValid,
buildTrigramSearchIndex,
isOutdated
}
+6
ファイルの表示
@@ -0,0 +1,6 @@
import { AttributesOnly } from '@peertube/peertube-typescript-utils'
import { Model } from 'sequelize-typescript'
export abstract class SequelizeModel <T> extends Model<Partial<AttributesOnly<T>>> {
id: number
}
+146
ファイルの表示
@@ -0,0 +1,146 @@
import { literal, OrderItem, Sequelize } from 'sequelize'
// Translate for example "-name" to [ [ 'name', 'DESC' ], [ 'id', 'ASC' ] ]
function getSort (value: string, lastSort: OrderItem = [ 'id', 'ASC' ]): OrderItem[] {
const { direction, field } = buildSortDirectionAndField(value)
let finalField: string | ReturnType<typeof Sequelize.col>
if (field.toLowerCase() === 'match') { // Search
finalField = Sequelize.col('similarity')
} else {
finalField = field
}
return [ [ finalField, direction ], lastSort ]
}
function getAdminUsersSort (value: string): OrderItem[] {
const { direction, field } = buildSortDirectionAndField(value)
let finalField: string | ReturnType<typeof Sequelize.col>
if (field === 'videoQuotaUsed') { // Users list
finalField = Sequelize.col('videoQuotaUsed')
} else {
finalField = field
}
const nullPolicy = direction === 'ASC'
? 'NULLS FIRST'
: 'NULLS LAST'
// FIXME: typings
return [ [ finalField as any, direction, nullPolicy ], [ 'id', 'ASC' ] ]
}
function getPlaylistSort (value: string, lastSort: OrderItem = [ 'id', 'ASC' ]): OrderItem[] {
const { direction, field } = buildSortDirectionAndField(value)
if (field.toLowerCase() === 'name') {
return [ [ 'displayName', direction ], lastSort ]
}
return getSort(value, lastSort)
}
function getVideoSort (value: string, lastSort: OrderItem = [ 'id', 'ASC' ]): OrderItem[] {
const { direction, field } = buildSortDirectionAndField(value)
if (field.toLowerCase() === 'trending') { // Sort by aggregation
return [
[ Sequelize.fn('COALESCE', Sequelize.fn('SUM', Sequelize.col('VideoViews.views')), '0'), direction ],
[ Sequelize.col('VideoModel.views'), direction ],
lastSort
]
} else if (field === 'publishedAt') {
return [
[ 'ScheduleVideoUpdate', 'updateAt', direction + ' NULLS LAST' ],
[ Sequelize.col('VideoModel.publishedAt'), direction ],
lastSort
]
}
let finalField: string | ReturnType<typeof Sequelize.col>
// Alias
if (field.toLowerCase() === 'match') { // Search
finalField = Sequelize.col('similarity')
} else {
finalField = field
}
const firstSort: OrderItem = typeof finalField === 'string'
? finalField.split('.').concat([ direction ]) as OrderItem
: [ finalField, direction ]
return [ firstSort, lastSort ]
}
function getBlacklistSort (value: string, lastSort: OrderItem = [ 'id', 'ASC' ]): OrderItem[] {
const { direction, field } = buildSortDirectionAndField(value)
const videoFields = new Set([ 'name', 'duration', 'views', 'likes', 'dislikes', 'uuid' ])
if (videoFields.has(field)) {
return [
[ literal(`"Video.${field}" ${direction}`) ],
lastSort
] as OrderItem[]
}
return getSort(value, lastSort)
}
function getInstanceFollowsSort (value: string, lastSort: OrderItem = [ 'id', 'ASC' ]): OrderItem[] {
const { direction, field } = buildSortDirectionAndField(value)
if (field === 'redundancyAllowed') {
return [
[ 'ActorFollowing.Server.redundancyAllowed', direction ],
lastSort
]
}
return getSort(value, lastSort)
}
function getChannelSyncSort (value: string): OrderItem[] {
const { direction, field } = buildSortDirectionAndField(value)
if (field.toLowerCase() === 'videochannel') {
return [
[ literal('"VideoChannel.name"'), direction ]
]
}
return [ [ field, direction ] ]
}
function buildSortDirectionAndField (value: string) {
let field: string
let direction: 'ASC' | 'DESC'
if (value.substring(0, 1) === '-') {
direction = 'DESC'
field = value.substring(1)
} else {
direction = 'ASC'
field = value
}
return { direction, field }
}
export {
buildSortDirectionAndField,
getPlaylistSort,
getSort,
getAdminUsersSort,
getVideoSort,
getBlacklistSort,
getChannelSyncSort,
getInstanceFollowsSort
}
+72
ファイルの表示
@@ -0,0 +1,72 @@
import { literal, Model, ModelStatic } from 'sequelize'
import { Literal } from 'sequelize/types/utils'
import { forceNumber } from '@peertube/peertube-core-utils'
import { AttributesOnly } from '@peertube/peertube-typescript-utils'
// FIXME: have to specify the result type to not break peertube typings generation
export function buildLocalAccountIdsIn (): Literal {
return literal(
'(SELECT "account"."id" FROM "account" INNER JOIN "actor" ON "actor"."id" = "account"."actorId" AND "actor"."serverId" IS NULL)'
)
}
// FIXME: have to specify the result type to not break peertube typings generation
export function buildLocalActorIdsIn (): Literal {
return literal(
'(SELECT "actor"."id" FROM "actor" WHERE "actor"."serverId" IS NULL)'
)
}
export function buildBlockedAccountSQL (blockerIds: number[]) {
const blockerIdsString = blockerIds.join(', ')
return 'SELECT "targetAccountId" AS "id" FROM "accountBlocklist" WHERE "accountId" IN (' + blockerIdsString + ')' +
' UNION ' +
'SELECT "account"."id" AS "id" FROM account INNER JOIN "actor" ON account."actorId" = actor.id ' +
'INNER JOIN "serverBlocklist" ON "actor"."serverId" = "serverBlocklist"."targetServerId" ' +
'WHERE "serverBlocklist"."accountId" IN (' + blockerIdsString + ')'
}
export function buildServerIdsFollowedBy (actorId: any) {
const actorIdNumber = forceNumber(actorId)
return '(' +
'SELECT "actor"."serverId" FROM "actorFollow" ' +
'INNER JOIN "actor" ON actor.id = "actorFollow"."targetActorId" ' +
'WHERE "actorFollow"."actorId" = ' + actorIdNumber +
')'
}
export function buildSQLAttributes<M extends Model> (options: {
model: ModelStatic<M>
tableName: string
excludeAttributes?: Exclude<keyof AttributesOnly<M>, symbol>[]
aliasPrefix?: string
idBuilder?: string[]
}) {
const { model, tableName, aliasPrefix = '', excludeAttributes, idBuilder } = options
const attributes = Object.keys(model.getAttributes()) as Exclude<keyof AttributesOnly<M>, symbol>[]
const builtAttributes = attributes
.filter(a => {
if (!excludeAttributes) return true
if (excludeAttributes.includes(a)) return false
return true
})
.map(a => {
return `"${tableName}"."${a}" AS "${aliasPrefix}${a}"`
})
if (idBuilder) {
const idSelect = idBuilder.map(a => `"${tableName}"."${a}"`)
.join(` || '-' || `)
builtAttributes.push(`${idSelect} AS "${aliasPrefix}id"`)
}
return builtAttributes
}
+34
ファイルの表示
@@ -0,0 +1,34 @@
import { QueryTypes, Sequelize, Transaction } from 'sequelize'
const updating = new Set<string>()
// Sequelize always skip the update if we only update updatedAt field
async function setAsUpdated (options: {
sequelize: Sequelize
table: string
id: number
transaction?: Transaction
}) {
const { sequelize, table, id, transaction } = options
const key = table + '-' + id
if (updating.has(key)) return
updating.add(key)
try {
await sequelize.query(
`UPDATE "${table}" SET "updatedAt" = :updatedAt WHERE id = :id`,
{
replacements: { table, id, updatedAt: new Date() },
type: QueryTypes.UPDATE,
transaction
}
)
} finally {
updating.delete(key)
}
}
export {
setAsUpdated
}
+284
ファイルの表示
@@ -0,0 +1,284 @@
import { Sequelize } from 'sequelize'
import { AbstractRunQuery, ModelBuilder } from '@server/models/shared/index.js'
import { UserNotificationModelForApi } from '@server/types/models/index.js'
import { ActorImageType } from '@peertube/peertube-models'
import { getSort } from '../../shared/index.js'
export interface ListNotificationsOptions {
userId: number
unread?: boolean
sort: string
offset: number
limit: number
}
export class UserNotificationListQueryBuilder extends AbstractRunQuery {
private innerQuery: string
constructor (
protected readonly sequelize: Sequelize,
private readonly options: ListNotificationsOptions
) {
super(sequelize)
}
async listNotifications () {
this.buildQuery()
const results = await this.runQuery({ nest: true })
const modelBuilder = new ModelBuilder<UserNotificationModelForApi>(this.sequelize)
return modelBuilder.createModels(results, 'UserNotification')
}
private buildInnerQuery () {
this.innerQuery = `SELECT * FROM "userNotification" AS "UserNotificationModel" ` +
`${this.getWhere()} ` +
`${this.getOrder()} ` +
`LIMIT :limit OFFSET :offset `
this.replacements.limit = this.options.limit
this.replacements.offset = this.options.offset
}
private buildQuery () {
this.buildInnerQuery()
this.query = `
${this.getSelect()}
FROM (${this.innerQuery}) "UserNotificationModel"
${this.getJoins()}
${this.getOrder()}`
}
private getWhere () {
let base = '"UserNotificationModel"."userId" = :userId '
this.replacements.userId = this.options.userId
if (this.options.unread === true) {
base += 'AND "UserNotificationModel"."read" IS FALSE '
} else if (this.options.unread === false) {
base += 'AND "UserNotificationModel"."read" IS TRUE '
}
return `WHERE ${base}`
}
private getOrder () {
const orders = getSort(this.options.sort)
return 'ORDER BY ' + orders.map(o => `"UserNotificationModel"."${o[0]}" ${o[1]}`).join(', ')
}
private getSelect () {
return `SELECT
"UserNotificationModel"."id",
"UserNotificationModel"."type",
"UserNotificationModel"."read",
"UserNotificationModel"."createdAt",
"UserNotificationModel"."updatedAt",
"Video"."id" AS "Video.id",
"Video"."uuid" AS "Video.uuid",
"Video"."name" AS "Video.name",
"Video->VideoChannel"."id" AS "Video.VideoChannel.id",
"Video->VideoChannel"."name" AS "Video.VideoChannel.name",
"Video->VideoChannel->Actor"."id" AS "Video.VideoChannel.Actor.id",
"Video->VideoChannel->Actor"."preferredUsername" AS "Video.VideoChannel.Actor.preferredUsername",
"Video->VideoChannel->Actor->Avatars"."id" AS "Video.VideoChannel.Actor.Avatars.id",
"Video->VideoChannel->Actor->Avatars"."width" AS "Video.VideoChannel.Actor.Avatars.width",
"Video->VideoChannel->Actor->Avatars"."type" AS "Video.VideoChannel.Actor.Avatars.type",
"Video->VideoChannel->Actor->Avatars"."filename" AS "Video.VideoChannel.Actor.Avatars.filename",
"Video->VideoChannel->Actor->Server"."id" AS "Video.VideoChannel.Actor.Server.id",
"Video->VideoChannel->Actor->Server"."host" AS "Video.VideoChannel.Actor.Server.host",
"VideoComment"."id" AS "VideoComment.id",
"VideoComment"."originCommentId" AS "VideoComment.originCommentId",
"VideoComment"."heldForReview" AS "VideoComment.heldForReview",
"VideoComment->Account"."id" AS "VideoComment.Account.id",
"VideoComment->Account"."name" AS "VideoComment.Account.name",
"VideoComment->Account->Actor"."id" AS "VideoComment.Account.Actor.id",
"VideoComment->Account->Actor"."preferredUsername" AS "VideoComment.Account.Actor.preferredUsername",
"VideoComment->Account->Actor->Avatars"."id" AS "VideoComment.Account.Actor.Avatars.id",
"VideoComment->Account->Actor->Avatars"."width" AS "VideoComment.Account.Actor.Avatars.width",
"VideoComment->Account->Actor->Avatars"."type" AS "VideoComment.Account.Actor.Avatars.type",
"VideoComment->Account->Actor->Avatars"."filename" AS "VideoComment.Account.Actor.Avatars.filename",
"VideoComment->Account->Actor->Server"."id" AS "VideoComment.Account.Actor.Server.id",
"VideoComment->Account->Actor->Server"."host" AS "VideoComment.Account.Actor.Server.host",
"VideoComment->Video"."id" AS "VideoComment.Video.id",
"VideoComment->Video"."uuid" AS "VideoComment.Video.uuid",
"VideoComment->Video"."name" AS "VideoComment.Video.name",
"Abuse"."id" AS "Abuse.id",
"Abuse"."state" AS "Abuse.state",
"Abuse->VideoAbuse"."id" AS "Abuse.VideoAbuse.id",
"Abuse->VideoAbuse->Video"."id" AS "Abuse.VideoAbuse.Video.id",
"Abuse->VideoAbuse->Video"."uuid" AS "Abuse.VideoAbuse.Video.uuid",
"Abuse->VideoAbuse->Video"."name" AS "Abuse.VideoAbuse.Video.name",
"Abuse->VideoCommentAbuse"."id" AS "Abuse.VideoCommentAbuse.id",
"Abuse->VideoCommentAbuse->VideoComment"."id" AS "Abuse.VideoCommentAbuse.VideoComment.id",
"Abuse->VideoCommentAbuse->VideoComment"."originCommentId" AS "Abuse.VideoCommentAbuse.VideoComment.originCommentId",
"Abuse->VideoCommentAbuse->VideoComment->Video"."id" AS "Abuse.VideoCommentAbuse.VideoComment.Video.id",
"Abuse->VideoCommentAbuse->VideoComment->Video"."name" AS "Abuse.VideoCommentAbuse.VideoComment.Video.name",
"Abuse->VideoCommentAbuse->VideoComment->Video"."uuid" AS "Abuse.VideoCommentAbuse.VideoComment.Video.uuid",
"Abuse->FlaggedAccount"."id" AS "Abuse.FlaggedAccount.id",
"Abuse->FlaggedAccount"."name" AS "Abuse.FlaggedAccount.name",
"Abuse->FlaggedAccount"."description" AS "Abuse.FlaggedAccount.description",
"Abuse->FlaggedAccount"."actorId" AS "Abuse.FlaggedAccount.actorId",
"Abuse->FlaggedAccount"."userId" AS "Abuse.FlaggedAccount.userId",
"Abuse->FlaggedAccount"."applicationId" AS "Abuse.FlaggedAccount.applicationId",
"Abuse->FlaggedAccount"."createdAt" AS "Abuse.FlaggedAccount.createdAt",
"Abuse->FlaggedAccount"."updatedAt" AS "Abuse.FlaggedAccount.updatedAt",
"Abuse->FlaggedAccount->Actor"."id" AS "Abuse.FlaggedAccount.Actor.id",
"Abuse->FlaggedAccount->Actor"."preferredUsername" AS "Abuse.FlaggedAccount.Actor.preferredUsername",
"Abuse->FlaggedAccount->Actor->Avatars"."id" AS "Abuse.FlaggedAccount.Actor.Avatars.id",
"Abuse->FlaggedAccount->Actor->Avatars"."width" AS "Abuse.FlaggedAccount.Actor.Avatars.width",
"Abuse->FlaggedAccount->Actor->Avatars"."type" AS "Abuse.FlaggedAccount.Actor.Avatars.type",
"Abuse->FlaggedAccount->Actor->Avatars"."filename" AS "Abuse.FlaggedAccount.Actor.Avatars.filename",
"Abuse->FlaggedAccount->Actor->Server"."id" AS "Abuse.FlaggedAccount.Actor.Server.id",
"Abuse->FlaggedAccount->Actor->Server"."host" AS "Abuse.FlaggedAccount.Actor.Server.host",
"VideoBlacklist"."id" AS "VideoBlacklist.id",
"VideoBlacklist->Video"."id" AS "VideoBlacklist.Video.id",
"VideoBlacklist->Video"."uuid" AS "VideoBlacklist.Video.uuid",
"VideoBlacklist->Video"."name" AS "VideoBlacklist.Video.name",
"VideoImport"."id" AS "VideoImport.id",
"VideoImport"."magnetUri" AS "VideoImport.magnetUri",
"VideoImport"."targetUrl" AS "VideoImport.targetUrl",
"VideoImport"."torrentName" AS "VideoImport.torrentName",
"VideoImport->Video"."id" AS "VideoImport.Video.id",
"VideoImport->Video"."uuid" AS "VideoImport.Video.uuid",
"VideoImport->Video"."name" AS "VideoImport.Video.name",
"Plugin"."id" AS "Plugin.id",
"Plugin"."name" AS "Plugin.name",
"Plugin"."type" AS "Plugin.type",
"Plugin"."latestVersion" AS "Plugin.latestVersion",
"Application"."id" AS "Application.id",
"Application"."latestPeerTubeVersion" AS "Application.latestPeerTubeVersion",
"ActorFollow"."id" AS "ActorFollow.id",
"ActorFollow"."state" AS "ActorFollow.state",
"ActorFollow->ActorFollower"."id" AS "ActorFollow.ActorFollower.id",
"ActorFollow->ActorFollower"."preferredUsername" AS "ActorFollow.ActorFollower.preferredUsername",
"ActorFollow->ActorFollower->Account"."id" AS "ActorFollow.ActorFollower.Account.id",
"ActorFollow->ActorFollower->Account"."name" AS "ActorFollow.ActorFollower.Account.name",
"ActorFollow->ActorFollower->Avatars"."id" AS "ActorFollow.ActorFollower.Avatars.id",
"ActorFollow->ActorFollower->Avatars"."width" AS "ActorFollow.ActorFollower.Avatars.width",
"ActorFollow->ActorFollower->Avatars"."type" AS "ActorFollow.ActorFollower.Avatars.type",
"ActorFollow->ActorFollower->Avatars"."filename" AS "ActorFollow.ActorFollower.Avatars.filename",
"ActorFollow->ActorFollower->Server"."id" AS "ActorFollow.ActorFollower.Server.id",
"ActorFollow->ActorFollower->Server"."host" AS "ActorFollow.ActorFollower.Server.host",
"ActorFollow->ActorFollowing"."id" AS "ActorFollow.ActorFollowing.id",
"ActorFollow->ActorFollowing"."preferredUsername" AS "ActorFollow.ActorFollowing.preferredUsername",
"ActorFollow->ActorFollowing"."type" AS "ActorFollow.ActorFollowing.type",
"ActorFollow->ActorFollowing->VideoChannel"."id" AS "ActorFollow.ActorFollowing.VideoChannel.id",
"ActorFollow->ActorFollowing->VideoChannel"."name" AS "ActorFollow.ActorFollowing.VideoChannel.name",
"ActorFollow->ActorFollowing->Account"."id" AS "ActorFollow.ActorFollowing.Account.id",
"ActorFollow->ActorFollowing->Account"."name" AS "ActorFollow.ActorFollowing.Account.name",
"ActorFollow->ActorFollowing->Server"."id" AS "ActorFollow.ActorFollowing.Server.id",
"ActorFollow->ActorFollowing->Server"."host" AS "ActorFollow.ActorFollowing.Server.host",
"Account"."id" AS "Account.id",
"Account"."name" AS "Account.name",
"Account->Actor"."id" AS "Account.Actor.id",
"Account->Actor"."preferredUsername" AS "Account.Actor.preferredUsername",
"Account->Actor->Avatars"."id" AS "Account.Actor.Avatars.id",
"Account->Actor->Avatars"."width" AS "Account.Actor.Avatars.width",
"Account->Actor->Avatars"."type" AS "Account.Actor.Avatars.type",
"Account->Actor->Avatars"."filename" AS "Account.Actor.Avatars.filename",
"Account->Actor->Server"."id" AS "Account.Actor.Server.id",
"Account->Actor->Server"."host" AS "Account.Actor.Server.host",
"UserRegistration"."id" AS "UserRegistration.id",
"UserRegistration"."username" AS "UserRegistration.username",
"VideoCaption"."id" AS "VideoCaption.id",
"VideoCaption"."language" AS "VideoCaption.language",
"VideoCaption->Video"."id" AS "VideoCaption.Video.id",
"VideoCaption->Video"."uuid" AS "VideoCaption.Video.uuid",
"VideoCaption->Video"."name" AS "VideoCaption.Video.name"`
}
private getJoins () {
return `
LEFT JOIN (
"video" AS "Video"
INNER JOIN "videoChannel" AS "Video->VideoChannel" ON "Video"."channelId" = "Video->VideoChannel"."id"
INNER JOIN "actor" AS "Video->VideoChannel->Actor" ON "Video->VideoChannel"."actorId" = "Video->VideoChannel->Actor"."id"
LEFT JOIN "actorImage" AS "Video->VideoChannel->Actor->Avatars"
ON "Video->VideoChannel->Actor"."id" = "Video->VideoChannel->Actor->Avatars"."actorId"
AND "Video->VideoChannel->Actor->Avatars"."type" = ${ActorImageType.AVATAR}
LEFT JOIN "server" AS "Video->VideoChannel->Actor->Server"
ON "Video->VideoChannel->Actor"."serverId" = "Video->VideoChannel->Actor->Server"."id"
) ON "UserNotificationModel"."videoId" = "Video"."id"
LEFT JOIN (
"videoComment" AS "VideoComment"
INNER JOIN "account" AS "VideoComment->Account" ON "VideoComment"."accountId" = "VideoComment->Account"."id"
INNER JOIN "actor" AS "VideoComment->Account->Actor" ON "VideoComment->Account"."actorId" = "VideoComment->Account->Actor"."id"
LEFT JOIN "actorImage" AS "VideoComment->Account->Actor->Avatars"
ON "VideoComment->Account->Actor"."id" = "VideoComment->Account->Actor->Avatars"."actorId"
AND "VideoComment->Account->Actor->Avatars"."type" = ${ActorImageType.AVATAR}
LEFT JOIN "server" AS "VideoComment->Account->Actor->Server"
ON "VideoComment->Account->Actor"."serverId" = "VideoComment->Account->Actor->Server"."id"
INNER JOIN "video" AS "VideoComment->Video" ON "VideoComment"."videoId" = "VideoComment->Video"."id"
) ON "UserNotificationModel"."commentId" = "VideoComment"."id"
LEFT JOIN "abuse" AS "Abuse" ON "UserNotificationModel"."abuseId" = "Abuse"."id"
LEFT JOIN "videoAbuse" AS "Abuse->VideoAbuse" ON "Abuse"."id" = "Abuse->VideoAbuse"."abuseId"
LEFT JOIN "video" AS "Abuse->VideoAbuse->Video" ON "Abuse->VideoAbuse"."videoId" = "Abuse->VideoAbuse->Video"."id"
LEFT JOIN "commentAbuse" AS "Abuse->VideoCommentAbuse" ON "Abuse"."id" = "Abuse->VideoCommentAbuse"."abuseId"
LEFT JOIN "videoComment" AS "Abuse->VideoCommentAbuse->VideoComment"
ON "Abuse->VideoCommentAbuse"."videoCommentId" = "Abuse->VideoCommentAbuse->VideoComment"."id"
LEFT JOIN "video" AS "Abuse->VideoCommentAbuse->VideoComment->Video"
ON "Abuse->VideoCommentAbuse->VideoComment"."videoId" = "Abuse->VideoCommentAbuse->VideoComment->Video"."id"
LEFT JOIN (
"account" AS "Abuse->FlaggedAccount"
INNER JOIN "actor" AS "Abuse->FlaggedAccount->Actor" ON "Abuse->FlaggedAccount"."actorId" = "Abuse->FlaggedAccount->Actor"."id"
LEFT JOIN "actorImage" AS "Abuse->FlaggedAccount->Actor->Avatars"
ON "Abuse->FlaggedAccount->Actor"."id" = "Abuse->FlaggedAccount->Actor->Avatars"."actorId"
AND "Abuse->FlaggedAccount->Actor->Avatars"."type" = ${ActorImageType.AVATAR}
LEFT JOIN "server" AS "Abuse->FlaggedAccount->Actor->Server"
ON "Abuse->FlaggedAccount->Actor"."serverId" = "Abuse->FlaggedAccount->Actor->Server"."id"
) ON "Abuse"."flaggedAccountId" = "Abuse->FlaggedAccount"."id"
LEFT JOIN (
"videoBlacklist" AS "VideoBlacklist"
INNER JOIN "video" AS "VideoBlacklist->Video" ON "VideoBlacklist"."videoId" = "VideoBlacklist->Video"."id"
) ON "UserNotificationModel"."videoBlacklistId" = "VideoBlacklist"."id"
LEFT JOIN "videoImport" AS "VideoImport" ON "UserNotificationModel"."videoImportId" = "VideoImport"."id"
LEFT JOIN "video" AS "VideoImport->Video" ON "VideoImport"."videoId" = "VideoImport->Video"."id"
LEFT JOIN "plugin" AS "Plugin" ON "UserNotificationModel"."pluginId" = "Plugin"."id"
LEFT JOIN "application" AS "Application" ON "UserNotificationModel"."applicationId" = "Application"."id"
LEFT JOIN (
"actorFollow" AS "ActorFollow"
INNER JOIN "actor" AS "ActorFollow->ActorFollower" ON "ActorFollow"."actorId" = "ActorFollow->ActorFollower"."id"
INNER JOIN "account" AS "ActorFollow->ActorFollower->Account"
ON "ActorFollow->ActorFollower"."id" = "ActorFollow->ActorFollower->Account"."actorId"
LEFT JOIN "actorImage" AS "ActorFollow->ActorFollower->Avatars"
ON "ActorFollow->ActorFollower"."id" = "ActorFollow->ActorFollower->Avatars"."actorId"
AND "ActorFollow->ActorFollower->Avatars"."type" = ${ActorImageType.AVATAR}
LEFT JOIN "server" AS "ActorFollow->ActorFollower->Server"
ON "ActorFollow->ActorFollower"."serverId" = "ActorFollow->ActorFollower->Server"."id"
INNER JOIN "actor" AS "ActorFollow->ActorFollowing" ON "ActorFollow"."targetActorId" = "ActorFollow->ActorFollowing"."id"
LEFT JOIN "videoChannel" AS "ActorFollow->ActorFollowing->VideoChannel"
ON "ActorFollow->ActorFollowing"."id" = "ActorFollow->ActorFollowing->VideoChannel"."actorId"
LEFT JOIN "account" AS "ActorFollow->ActorFollowing->Account"
ON "ActorFollow->ActorFollowing"."id" = "ActorFollow->ActorFollowing->Account"."actorId"
LEFT JOIN "server" AS "ActorFollow->ActorFollowing->Server"
ON "ActorFollow->ActorFollowing"."serverId" = "ActorFollow->ActorFollowing->Server"."id"
) ON "UserNotificationModel"."actorFollowId" = "ActorFollow"."id"
LEFT JOIN (
"account" AS "Account"
INNER JOIN "actor" AS "Account->Actor" ON "Account"."actorId" = "Account->Actor"."id"
LEFT JOIN "actorImage" AS "Account->Actor->Avatars"
ON "Account->Actor"."id" = "Account->Actor->Avatars"."actorId"
AND "Account->Actor->Avatars"."type" = ${ActorImageType.AVATAR}
LEFT JOIN "server" AS "Account->Actor->Server" ON "Account->Actor"."serverId" = "Account->Actor->Server"."id"
) ON "UserNotificationModel"."accountId" = "Account"."id"
LEFT JOIN "userRegistration" as "UserRegistration" ON "UserNotificationModel"."userRegistrationId" = "UserRegistration"."id"
LEFT JOIN (
"videoCaption" AS "VideoCaption"
INNER JOIN "video" AS "VideoCaption->Video" ON "VideoCaption"."videoId" = "VideoCaption->Video"."id"
) ON "UserNotificationModel"."videoCaptionId" = "VideoCaption"."id"`
}
}
+229
ファイルの表示
@@ -0,0 +1,229 @@
import { FileStorage, UserExportState, type FileStorageType, type UserExport, type UserExportStateType } from '@peertube/peertube-models'
import { logger } from '@server/helpers/logger.js'
import { CONFIG } from '@server/initializers/config.js'
import {
JWT_TOKEN_USER_EXPORT_FILE_LIFETIME,
STATIC_DOWNLOAD_PATHS,
USER_EXPORT_FILE_PREFIX,
USER_EXPORT_STATES,
WEBSERVER
} from '@server/initializers/constants.js'
import { removeUserExportObjectStorage } from '@server/lib/object-storage/user-export.js'
import { getFSUserExportFilePath } from '@server/lib/paths.js'
import { MUserAccountId, MUserExport } from '@server/types/models/index.js'
import { remove } from 'fs-extra/esm'
import jwt from 'jsonwebtoken'
import { join } from 'path'
import { FindOptions, Op } from 'sequelize'
import { AllowNull, BeforeDestroy, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Table, UpdatedAt } from 'sequelize-typescript'
import { doesExist } from '../shared/query.js'
import { SequelizeModel } from '../shared/sequelize-type.js'
import { getSort } from '../shared/sort.js'
import { UserModel } from './user.js'
@Table({
tableName: 'userExport',
indexes: [
{
fields: [ 'userId' ]
},
{
fields: [ 'filename' ],
unique: true
}
]
})
export class UserExportModel extends SequelizeModel<UserExportModel> {
@CreatedAt
createdAt: Date
@UpdatedAt
updatedAt: Date
@AllowNull(true)
@Column
filename: string
@AllowNull(false)
@Column
withVideoFiles: boolean
@AllowNull(false)
@Column
state: UserExportStateType
@AllowNull(true)
@Column(DataType.TEXT)
error: string
@AllowNull(true)
@Column(DataType.BIGINT)
size: number
@AllowNull(false)
@Column
storage: FileStorageType
@AllowNull(true)
@Column
fileUrl: string
@ForeignKey(() => UserModel)
@Column
userId: number
@BelongsTo(() => UserModel, {
foreignKey: {
allowNull: false
},
onDelete: 'CASCADE'
})
User: Awaited<UserModel>
@BeforeDestroy
static removeFile (instance: UserExportModel) {
logger.info('Removing user export file %s.', instance.filename)
if (instance.storage === FileStorage.FILE_SYSTEM) {
remove(getFSUserExportFilePath(instance))
.catch(err => logger.error('Cannot delete user export archive %s from filesystem.', instance.filename, { err }))
} else {
removeUserExportObjectStorage(instance)
.catch(err => logger.error('Cannot delete user export archive %s from object storage.', instance.filename, { err }))
}
return undefined
}
static listByUser (user: MUserAccountId) {
const query: FindOptions = {
where: {
userId: user.id
}
}
return UserExportModel.findAll<MUserExport>(query)
}
static listExpired (expirationTimeMS: number) {
const query: FindOptions = {
where: {
createdAt: {
[Op.lt]: new Date(new Date().getTime() + expirationTimeMS)
}
}
}
return UserExportModel.findAll<MUserExport>(query)
}
static listForApi (options: {
user: MUserAccountId
start: number
count: number
}) {
const { count, start, user } = options
const query: FindOptions = {
offset: start,
limit: count,
order: getSort('createdAt'),
where: {
userId: user.id
}
}
return Promise.all([
UserExportModel.count(query),
UserExportModel.findAll<MUserExport>(query)
]).then(([ total, data ]) => ({ total, data }))
}
static load (id: number | string) {
return UserExportModel.findByPk<MUserExport>(id)
}
static loadByFilename (filename: string) {
return UserExportModel.findOne<MUserExport>({ where: { filename } })
}
// ---------------------------------------------------------------------------
static async doesOwnedFileExist (filename: string, storage: FileStorageType) {
const query = 'SELECT 1 FROM "userExport" ' +
`WHERE "filename" = $filename AND "storage" = $storage LIMIT 1`
return doesExist({ sequelize: this.sequelize, query, bind: { filename, storage } })
}
// ---------------------------------------------------------------------------
generateAndSetFilename () {
if (!this.userId) throw new Error('Cannot generate filename without userId')
if (!this.createdAt) throw new Error('Cannot generate filename without createdAt')
this.filename = `${USER_EXPORT_FILE_PREFIX}${this.userId}-${this.createdAt.toISOString()}.zip`
}
canBeSafelyRemoved () {
const supportedStates = new Set<UserExportStateType>([ UserExportState.COMPLETED, UserExportState.ERRORED, UserExportState.PENDING ])
return supportedStates.has(this.state)
}
generateJWT () {
return jwt.sign(
{
userExportId: this.id
},
CONFIG.SECRETS.PEERTUBE,
{
expiresIn: JWT_TOKEN_USER_EXPORT_FILE_LIFETIME,
audience: this.filename,
issuer: WEBSERVER.URL
}
)
}
isJWTValid (jwtToken: string) {
try {
const payload = jwt.verify(jwtToken, CONFIG.SECRETS.PEERTUBE, {
audience: this.filename,
issuer: WEBSERVER.URL
})
if ((payload as any).userExportId !== this.id) return false
return true
} catch {
return false
}
}
getFileDownloadUrl () {
if (this.state !== UserExportState.COMPLETED) return null
return WEBSERVER.URL + join(STATIC_DOWNLOAD_PATHS.USER_EXPORTS, this.filename) + '?jwt=' + this.generateJWT()
}
// ---------------------------------------------------------------------------
toFormattedJSON (this: MUserExport): UserExport {
return {
id: this.id,
state: {
id: this.state,
label: USER_EXPORT_STATES[this.state]
},
size: this.size,
fileUrl: this.fileUrl,
privateDownloadUrl: this.getFileDownloadUrl(),
createdAt: this.createdAt.toISOString(),
expiresOn: new Date(this.createdAt.getTime() + CONFIG.EXPORT.USERS.EXPORT_EXPIRATION).toISOString()
}
}
}
+88
ファイルの表示
@@ -0,0 +1,88 @@
import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Table, UpdatedAt } from 'sequelize-typescript'
import { MUserImport } from '@server/types/models/index.js'
import { SequelizeModel } from '../shared/index.js'
import { UserModel } from './user.js'
import type { UserImportResultSummary, UserImportStateType } from '@peertube/peertube-models'
import { getSort } from '../shared/sort.js'
import { USER_IMPORT_STATES } from '@server/initializers/constants.js'
@Table({
tableName: 'userImport',
indexes: [
{
fields: [ 'userId' ]
},
{
fields: [ 'filename' ],
unique: true
}
]
})
export class UserImportModel extends SequelizeModel<UserImportModel> {
@CreatedAt
createdAt: Date
@UpdatedAt
updatedAt: Date
@AllowNull(true)
@Column
filename: string
@AllowNull(false)
@Column
state: UserImportStateType
@AllowNull(true)
@Column(DataType.TEXT)
error: string
@AllowNull(true)
@Column(DataType.JSONB)
resultSummary: UserImportResultSummary
@ForeignKey(() => UserModel)
@Column
userId: number
@BelongsTo(() => UserModel, {
foreignKey: {
allowNull: false
},
onDelete: 'CASCADE'
})
User: Awaited<UserModel>
static load (id: number | string) {
return UserImportModel.findByPk<MUserImport>(id)
}
static loadLatestByUserId (userId: number) {
return UserImportModel.findOne<MUserImport>({
where: {
userId
},
order: getSort('-createdAt')
})
}
// ---------------------------------------------------------------------------
generateAndSetFilename () {
if (!this.userId) throw new Error('Cannot generate filename without userId')
if (!this.createdAt) throw new Error('Cannot generate filename without createdAt')
this.filename = `user-import-${this.userId}-${this.createdAt.toISOString()}.zip`
}
toFormattedJSON () {
return {
id: this.id,
state: {
id: this.state,
label: USER_IMPORT_STATES[this.state]
},
createdAt: this.createdAt.toISOString()
}
}
}
+249
ファイルの表示
@@ -0,0 +1,249 @@
import { type UserNotificationSetting, type UserNotificationSettingValueType } from '@peertube/peertube-models'
import { TokensCache } from '@server/lib/auth/tokens-cache.js'
import { MNotificationSettingFormattable } from '@server/types/models/index.js'
import {
AfterDestroy,
AfterUpdate,
AllowNull,
BelongsTo,
Column,
CreatedAt,
Default,
ForeignKey,
Is, Table,
UpdatedAt
} from 'sequelize-typescript'
import { isUserNotificationSettingValid } from '../../helpers/custom-validators/user-notifications.js'
import { SequelizeModel, throwIfNotValid } from '../shared/index.js'
import { UserModel } from './user.js'
@Table({
tableName: 'userNotificationSetting',
indexes: [
{
fields: [ 'userId' ],
unique: true
}
]
})
export class UserNotificationSettingModel extends SequelizeModel<UserNotificationSettingModel> {
@AllowNull(false)
@Default(null)
@Is(
'UserNotificationSettingNewVideoFromSubscription',
value => throwIfNotValid(value, isUserNotificationSettingValid, 'newVideoFromSubscription')
)
@Column
newVideoFromSubscription: UserNotificationSettingValueType
@AllowNull(false)
@Default(null)
@Is(
'UserNotificationSettingNewCommentOnMyVideo',
value => throwIfNotValid(value, isUserNotificationSettingValid, 'newCommentOnMyVideo')
)
@Column
newCommentOnMyVideo: UserNotificationSettingValueType
@AllowNull(false)
@Default(null)
@Is(
'UserNotificationSettingAbuseAsModerator',
value => throwIfNotValid(value, isUserNotificationSettingValid, 'abuseAsModerator')
)
@Column
abuseAsModerator: UserNotificationSettingValueType
@AllowNull(false)
@Default(null)
@Is(
'UserNotificationSettingVideoAutoBlacklistAsModerator',
value => throwIfNotValid(value, isUserNotificationSettingValid, 'videoAutoBlacklistAsModerator')
)
@Column
videoAutoBlacklistAsModerator: UserNotificationSettingValueType
@AllowNull(false)
@Default(null)
@Is(
'UserNotificationSettingBlacklistOnMyVideo',
value => throwIfNotValid(value, isUserNotificationSettingValid, 'blacklistOnMyVideo')
)
@Column
blacklistOnMyVideo: UserNotificationSettingValueType
@AllowNull(false)
@Default(null)
@Is(
'UserNotificationSettingMyVideoPublished',
value => throwIfNotValid(value, isUserNotificationSettingValid, 'myVideoPublished')
)
@Column
myVideoPublished: UserNotificationSettingValueType
@AllowNull(false)
@Default(null)
@Is(
'UserNotificationSettingMyVideoImportFinished',
value => throwIfNotValid(value, isUserNotificationSettingValid, 'myVideoImportFinished')
)
@Column
myVideoImportFinished: UserNotificationSettingValueType
@AllowNull(false)
@Default(null)
@Is(
'UserNotificationSettingNewUserRegistration',
value => throwIfNotValid(value, isUserNotificationSettingValid, 'newUserRegistration')
)
@Column
newUserRegistration: UserNotificationSettingValueType
@AllowNull(false)
@Default(null)
@Is(
'UserNotificationSettingNewInstanceFollower',
value => throwIfNotValid(value, isUserNotificationSettingValid, 'newInstanceFollower')
)
@Column
newInstanceFollower: UserNotificationSettingValueType
@AllowNull(false)
@Default(null)
@Is(
'UserNotificationSettingNewInstanceFollower',
value => throwIfNotValid(value, isUserNotificationSettingValid, 'autoInstanceFollowing')
)
@Column
autoInstanceFollowing: UserNotificationSettingValueType
@AllowNull(false)
@Default(null)
@Is(
'UserNotificationSettingNewFollow',
value => throwIfNotValid(value, isUserNotificationSettingValid, 'newFollow')
)
@Column
newFollow: UserNotificationSettingValueType
@AllowNull(false)
@Default(null)
@Is(
'UserNotificationSettingCommentMention',
value => throwIfNotValid(value, isUserNotificationSettingValid, 'commentMention')
)
@Column
commentMention: UserNotificationSettingValueType
@AllowNull(false)
@Default(null)
@Is(
'UserNotificationSettingAbuseStateChange',
value => throwIfNotValid(value, isUserNotificationSettingValid, 'abuseStateChange')
)
@Column
abuseStateChange: UserNotificationSettingValueType
@AllowNull(false)
@Default(null)
@Is(
'UserNotificationSettingAbuseNewMessage',
value => throwIfNotValid(value, isUserNotificationSettingValid, 'abuseNewMessage')
)
@Column
abuseNewMessage: UserNotificationSettingValueType
@AllowNull(false)
@Default(null)
@Is(
'UserNotificationSettingNewPeerTubeVersion',
value => throwIfNotValid(value, isUserNotificationSettingValid, 'newPeerTubeVersion')
)
@Column
newPeerTubeVersion: UserNotificationSettingValueType
@AllowNull(false)
@Default(null)
@Is(
'UserNotificationSettingNewPeerPluginVersion',
value => throwIfNotValid(value, isUserNotificationSettingValid, 'newPluginVersion')
)
@Column
newPluginVersion: UserNotificationSettingValueType
@AllowNull(false)
@Default(null)
@Is(
'UserNotificationSettingMyVideoStudioEditionFinished',
value => throwIfNotValid(value, isUserNotificationSettingValid, 'myVideoStudioEditionFinished')
)
@Column
myVideoStudioEditionFinished: UserNotificationSettingValueType
@AllowNull(false)
@Default(null)
@Is(
'UserNotificationSettingTranscriptionGeneratedForOwner',
value => throwIfNotValid(value, isUserNotificationSettingValid, 'myVideoTranscriptionGenerated')
)
@Column
myVideoTranscriptionGenerated: UserNotificationSettingValueType
@ForeignKey(() => UserModel)
@Column
userId: number
@BelongsTo(() => UserModel, {
foreignKey: {
allowNull: false
},
onDelete: 'cascade'
})
User: Awaited<UserModel>
@CreatedAt
createdAt: Date
@UpdatedAt
updatedAt: Date
@AfterUpdate
@AfterDestroy
static removeTokenCache (instance: UserNotificationSettingModel) {
return TokensCache.Instance.clearCacheByUserId(instance.userId)
}
static updateUserSettings (settings: UserNotificationSetting, userId: number) {
const query = {
where: {
userId
}
}
return UserNotificationSettingModel.update(settings, query)
}
toFormattedJSON (this: MNotificationSettingFormattable): UserNotificationSetting {
return {
newCommentOnMyVideo: this.newCommentOnMyVideo,
newVideoFromSubscription: this.newVideoFromSubscription,
abuseAsModerator: this.abuseAsModerator,
videoAutoBlacklistAsModerator: this.videoAutoBlacklistAsModerator,
blacklistOnMyVideo: this.blacklistOnMyVideo,
myVideoPublished: this.myVideoPublished,
myVideoImportFinished: this.myVideoImportFinished,
newUserRegistration: this.newUserRegistration,
commentMention: this.commentMention,
newFollow: this.newFollow,
newInstanceFollower: this.newInstanceFollower,
autoInstanceFollowing: this.autoInstanceFollowing,
abuseNewMessage: this.abuseNewMessage,
abuseStateChange: this.abuseStateChange,
newPeerTubeVersion: this.newPeerTubeVersion,
myVideoStudioEditionFinished: this.myVideoStudioEditionFinished,
myVideoTranscriptionGenerated: this.myVideoTranscriptionGenerated,
newPluginVersion: this.newPluginVersion
}
}
}
+566
ファイルの表示
@@ -0,0 +1,566 @@
import { forceNumber, maxBy } from '@peertube/peertube-core-utils'
import { UserNotification, type UserNotificationType_Type } from '@peertube/peertube-models'
import { uuidToShort } from '@peertube/peertube-node-utils'
import { UserNotificationIncludes, UserNotificationModelForApi } from '@server/types/models/user/index.js'
import { ModelIndexesOptions, Op, WhereOptions } from 'sequelize'
import { AllowNull, BelongsTo, Column, CreatedAt, Default, ForeignKey, Is, Table, UpdatedAt } from 'sequelize-typescript'
import { isBooleanValid } from '../../helpers/custom-validators/misc.js'
import { isUserNotificationTypeValid } from '../../helpers/custom-validators/user-notifications.js'
import { AbuseModel } from '../abuse/abuse.js'
import { AccountModel } from '../account/account.js'
import { ActorFollowModel } from '../actor/actor-follow.js'
import { ApplicationModel } from '../application/application.js'
import { PluginModel } from '../server/plugin.js'
import { SequelizeModel, throwIfNotValid } from '../shared/index.js'
import { VideoBlacklistModel } from '../video/video-blacklist.js'
import { VideoCaptionModel } from '../video/video-caption.js'
import { VideoCommentModel } from '../video/video-comment.js'
import { VideoImportModel } from '../video/video-import.js'
import { VideoModel } from '../video/video.js'
import { UserNotificationListQueryBuilder } from './sql/user-notitication-list-query-builder.js'
import { UserRegistrationModel } from './user-registration.js'
import { UserModel } from './user.js'
@Table({
tableName: 'userNotification',
indexes: [
{
fields: [ 'userId' ]
},
{
fields: [ 'videoId' ],
where: {
videoId: {
[Op.ne]: null
}
}
},
{
fields: [ 'commentId' ],
where: {
commentId: {
[Op.ne]: null
}
}
},
{
fields: [ 'abuseId' ],
where: {
abuseId: {
[Op.ne]: null
}
}
},
{
fields: [ 'videoBlacklistId' ],
where: {
videoBlacklistId: {
[Op.ne]: null
}
}
},
{
fields: [ 'videoImportId' ],
where: {
videoImportId: {
[Op.ne]: null
}
}
},
{
fields: [ 'accountId' ],
where: {
accountId: {
[Op.ne]: null
}
}
},
{
fields: [ 'actorFollowId' ],
where: {
actorFollowId: {
[Op.ne]: null
}
}
},
{
fields: [ 'pluginId' ],
where: {
pluginId: {
[Op.ne]: null
}
}
},
{
fields: [ 'applicationId' ],
where: {
applicationId: {
[Op.ne]: null
}
}
},
{
fields: [ 'userRegistrationId' ],
where: {
userRegistrationId: {
[Op.ne]: null
}
}
}
] as (ModelIndexesOptions & { where?: WhereOptions })[]
})
export class UserNotificationModel extends SequelizeModel<UserNotificationModel> {
@AllowNull(false)
@Default(null)
@Is('UserNotificationType', value => throwIfNotValid(value, isUserNotificationTypeValid, 'type'))
@Column
type: UserNotificationType_Type
@AllowNull(false)
@Default(false)
@Is('UserNotificationRead', value => throwIfNotValid(value, isBooleanValid, 'read'))
@Column
read: boolean
@CreatedAt
createdAt: Date
@UpdatedAt
updatedAt: Date
@ForeignKey(() => UserModel)
@Column
userId: number
@BelongsTo(() => UserModel, {
foreignKey: {
allowNull: false
},
onDelete: 'cascade'
})
User: Awaited<UserModel>
@ForeignKey(() => VideoModel)
@Column
videoId: number
@BelongsTo(() => VideoModel, {
foreignKey: {
allowNull: true
},
onDelete: 'cascade'
})
Video: Awaited<VideoModel>
@ForeignKey(() => VideoCommentModel)
@Column
commentId: number
@BelongsTo(() => VideoCommentModel, {
foreignKey: {
allowNull: true
},
onDelete: 'cascade'
})
VideoComment: Awaited<VideoCommentModel>
@ForeignKey(() => AbuseModel)
@Column
abuseId: number
@BelongsTo(() => AbuseModel, {
foreignKey: {
allowNull: true
},
onDelete: 'cascade'
})
Abuse: Awaited<AbuseModel>
@ForeignKey(() => VideoBlacklistModel)
@Column
videoBlacklistId: number
@BelongsTo(() => VideoBlacklistModel, {
foreignKey: {
allowNull: true
},
onDelete: 'cascade'
})
VideoBlacklist: Awaited<VideoBlacklistModel>
@ForeignKey(() => VideoImportModel)
@Column
videoImportId: number
@BelongsTo(() => VideoImportModel, {
foreignKey: {
allowNull: true
},
onDelete: 'cascade'
})
VideoImport: Awaited<VideoImportModel>
@ForeignKey(() => AccountModel)
@Column
accountId: number
@BelongsTo(() => AccountModel, {
foreignKey: {
allowNull: true
},
onDelete: 'cascade'
})
Account: Awaited<AccountModel>
@ForeignKey(() => ActorFollowModel)
@Column
actorFollowId: number
@BelongsTo(() => ActorFollowModel, {
foreignKey: {
allowNull: true
},
onDelete: 'cascade'
})
ActorFollow: Awaited<ActorFollowModel>
@ForeignKey(() => PluginModel)
@Column
pluginId: number
@BelongsTo(() => PluginModel, {
foreignKey: {
allowNull: true
},
onDelete: 'cascade'
})
Plugin: Awaited<PluginModel>
@ForeignKey(() => ApplicationModel)
@Column
applicationId: number
@BelongsTo(() => ApplicationModel, {
foreignKey: {
allowNull: true
},
onDelete: 'cascade'
})
Application: Awaited<ApplicationModel>
@ForeignKey(() => UserRegistrationModel)
@Column
userRegistrationId: number
@BelongsTo(() => UserRegistrationModel, {
foreignKey: {
allowNull: true
},
onDelete: 'cascade'
})
UserRegistration: Awaited<UserRegistrationModel>
@ForeignKey(() => VideoCaptionModel)
@Column
videoCaptionId: number
@BelongsTo(() => VideoCaptionModel, {
foreignKey: {
allowNull: true
},
onDelete: 'cascade'
})
VideoCaption: Awaited<VideoCaptionModel>
static listForApi (userId: number, start: number, count: number, sort: string, unread?: boolean) {
const where = { userId }
const query = {
userId,
unread,
offset: start,
limit: count,
sort,
where
}
if (unread !== undefined) query.where['read'] = !unread
return Promise.all([
UserNotificationModel.count({ where })
.then(count => count || 0),
count === 0
? [] as UserNotificationModelForApi[]
: new UserNotificationListQueryBuilder(this.sequelize, query).listNotifications()
]).then(([ total, data ]) => ({ total, data }))
}
static markAsRead (userId: number, notificationIds: number[]) {
const query = {
where: {
userId,
id: {
[Op.in]: notificationIds
},
read: false
}
}
return UserNotificationModel.update({ read: true }, query)
}
static markAllAsRead (userId: number) {
const query = { where: { userId, read: false } }
return UserNotificationModel.update({ read: true }, query)
}
static removeNotificationsOf (options: { id: number, type: 'account' | 'server', forUserId?: number }) {
const id = forceNumber(options.id)
function buildAccountWhereQuery (base: string) {
const whereSuffix = options.forUserId
? ` AND "userNotification"."userId" = ${options.forUserId}`
: ''
if (options.type === 'account') {
return base +
` WHERE "account"."id" = ${id} ${whereSuffix}`
}
return base +
` WHERE "actor"."serverId" = ${id} ${whereSuffix}`
}
const queries = [
buildAccountWhereQuery(
`SELECT "userNotification"."id" FROM "userNotification" ` +
`INNER JOIN "account" ON "userNotification"."accountId" = "account"."id" ` +
`INNER JOIN actor ON "actor"."id" = "account"."actorId" `
),
// Remove notifications from muted accounts that followed ours
buildAccountWhereQuery(
`SELECT "userNotification"."id" FROM "userNotification" ` +
`INNER JOIN "actorFollow" ON "actorFollow".id = "userNotification"."actorFollowId" ` +
`INNER JOIN actor ON actor.id = "actorFollow"."actorId" ` +
`INNER JOIN account ON account."actorId" = actor.id `
),
// Remove notifications from muted accounts that commented something
buildAccountWhereQuery(
`SELECT "userNotification"."id" FROM "userNotification" ` +
`INNER JOIN "actorFollow" ON "actorFollow".id = "userNotification"."actorFollowId" ` +
`INNER JOIN actor ON actor.id = "actorFollow"."actorId" ` +
`INNER JOIN account ON account."actorId" = actor.id `
),
buildAccountWhereQuery(
`SELECT "userNotification"."id" FROM "userNotification" ` +
`INNER JOIN "videoComment" ON "videoComment".id = "userNotification"."commentId" ` +
`INNER JOIN account ON account.id = "videoComment"."accountId" ` +
`INNER JOIN actor ON "actor"."id" = "account"."actorId" `
)
]
const query = `DELETE FROM "userNotification" WHERE id IN (${queries.join(' UNION ')})`
return UserNotificationModel.sequelize.query(query)
}
toFormattedJSON (this: UserNotificationModelForApi): UserNotification {
const video = this.Video
? {
...this.formatVideo(this.Video),
channel: this.formatActor(this.Video.VideoChannel)
}
: undefined
const videoImport = this.VideoImport
? {
id: this.VideoImport.id,
video: this.VideoImport.Video
? this.formatVideo(this.VideoImport.Video)
: undefined,
torrentName: this.VideoImport.torrentName,
magnetUri: this.VideoImport.magnetUri,
targetUrl: this.VideoImport.targetUrl
}
: undefined
const comment = this.VideoComment
? {
id: this.VideoComment.id,
threadId: this.VideoComment.getThreadId(),
account: this.formatActor(this.VideoComment.Account),
video: this.formatVideo(this.VideoComment.Video),
heldForReview: this.VideoComment.heldForReview
}
: undefined
const abuse = this.Abuse ? this.formatAbuse(this.Abuse) : undefined
const videoBlacklist = this.VideoBlacklist
? {
id: this.VideoBlacklist.id,
video: this.formatVideo(this.VideoBlacklist.Video)
}
: undefined
const account = this.Account ? this.formatActor(this.Account) : undefined
const actorFollowingType = {
Application: 'instance' as 'instance',
Group: 'channel' as 'channel',
Person: 'account' as 'account'
}
const actorFollow = this.ActorFollow
? {
id: this.ActorFollow.id,
state: this.ActorFollow.state,
follower: {
id: this.ActorFollow.ActorFollower.Account.id,
displayName: this.ActorFollow.ActorFollower.Account.getDisplayName(),
name: this.ActorFollow.ActorFollower.preferredUsername,
host: this.ActorFollow.ActorFollower.getHost(),
...this.formatAvatars(this.ActorFollow.ActorFollower.Avatars)
},
following: {
type: actorFollowingType[this.ActorFollow.ActorFollowing.type],
displayName: (this.ActorFollow.ActorFollowing.VideoChannel || this.ActorFollow.ActorFollowing.Account).getDisplayName(),
name: this.ActorFollow.ActorFollowing.preferredUsername,
host: this.ActorFollow.ActorFollowing.getHost()
}
}
: undefined
const plugin = this.Plugin
? {
name: this.Plugin.name,
type: this.Plugin.type,
latestVersion: this.Plugin.latestVersion
}
: undefined
const peertube = this.Application
? { latestVersion: this.Application.latestPeerTubeVersion }
: undefined
const registration = this.UserRegistration
? { id: this.UserRegistration.id, username: this.UserRegistration.username }
: undefined
const videoCaption = this.VideoCaption
? {
id: this.VideoCaption.id,
language: {
id: this.VideoCaption.language,
label: VideoCaptionModel.getLanguageLabel(this.VideoCaption.language)
},
video: this.formatVideo(this.VideoCaption.Video)
}
: undefined
return {
id: this.id,
type: this.type,
read: this.read,
video,
videoImport,
comment,
abuse,
videoBlacklist,
account,
actorFollow,
plugin,
peertube,
registration,
videoCaption,
createdAt: this.createdAt.toISOString(),
updatedAt: this.updatedAt.toISOString()
}
}
formatVideo (video: UserNotificationIncludes.VideoInclude) {
return {
id: video.id,
uuid: video.uuid,
shortUUID: uuidToShort(video.uuid),
name: video.name
}
}
formatAbuse (abuse: UserNotificationIncludes.AbuseInclude) {
const commentAbuse = abuse.VideoCommentAbuse?.VideoComment
? {
threadId: abuse.VideoCommentAbuse.VideoComment.getThreadId(),
video: abuse.VideoCommentAbuse.VideoComment.Video
? {
id: abuse.VideoCommentAbuse.VideoComment.Video.id,
name: abuse.VideoCommentAbuse.VideoComment.Video.name,
shortUUID: uuidToShort(abuse.VideoCommentAbuse.VideoComment.Video.uuid),
uuid: abuse.VideoCommentAbuse.VideoComment.Video.uuid
}
: undefined
}
: undefined
const videoAbuse = abuse.VideoAbuse?.Video
? this.formatVideo(abuse.VideoAbuse.Video)
: undefined
const accountAbuse = (!commentAbuse && !videoAbuse && abuse.FlaggedAccount)
? this.formatActor(abuse.FlaggedAccount)
: undefined
return {
id: abuse.id,
state: abuse.state,
video: videoAbuse,
comment: commentAbuse,
account: accountAbuse
}
}
formatActor (
accountOrChannel: UserNotificationIncludes.AccountIncludeActor | UserNotificationIncludes.VideoChannelIncludeActor
) {
return {
id: accountOrChannel.id,
displayName: accountOrChannel.getDisplayName(),
name: accountOrChannel.Actor.preferredUsername,
host: accountOrChannel.Actor.getHost(),
...this.formatAvatars(accountOrChannel.Actor.Avatars)
}
}
formatAvatars (avatars: UserNotificationIncludes.ActorImageInclude[]) {
if (!avatars || avatars.length === 0) return { avatar: undefined, avatars: [] }
return {
avatar: this.formatAvatar(maxBy(avatars, 'width')),
avatars: avatars.map(a => this.formatAvatar(a))
}
}
formatAvatar (a: UserNotificationIncludes.ActorImageInclude) {
return {
path: a.getStaticPath(),
width: a.width
}
}
formatVideoCaption (a: UserNotificationIncludes.ActorImageInclude) {
return {
path: a.getStaticPath(),
width: a.width
}
}
}
+289
ファイルの表示
@@ -0,0 +1,289 @@
import { UserRegistration, UserRegistrationState, type UserRegistrationStateType } from '@peertube/peertube-models'
import {
isRegistrationModerationResponseValid,
isRegistrationReasonValid,
isRegistrationStateValid
} from '@server/helpers/custom-validators/user-registration.js'
import { isVideoChannelDisplayNameValid } from '@server/helpers/custom-validators/video-channels.js'
import { cryptPassword } from '@server/helpers/peertube-crypto.js'
import { USER_REGISTRATION_STATES } from '@server/initializers/constants.js'
import { MRegistration, MRegistrationFormattable } from '@server/types/models/index.js'
import { FindOptions, Op, QueryTypes, WhereOptions } from 'sequelize'
import {
AllowNull,
BeforeCreate,
BelongsTo,
Column,
CreatedAt,
DataType,
ForeignKey,
Is,
IsEmail, Table,
UpdatedAt
} from 'sequelize-typescript'
import { isUserDisplayNameValid, isUserEmailVerifiedValid, isUserPasswordValid } from '../../helpers/custom-validators/users.js'
import { SequelizeModel, getSort, parseAggregateResult, throwIfNotValid } from '../shared/index.js'
import { UserModel } from './user.js'
import { forceNumber } from '@peertube/peertube-core-utils'
@Table({
tableName: 'userRegistration',
indexes: [
{
fields: [ 'username' ],
unique: true
},
{
fields: [ 'email' ],
unique: true
},
{
fields: [ 'channelHandle' ],
unique: true
},
{
fields: [ 'userId' ],
unique: true
}
]
})
export class UserRegistrationModel extends SequelizeModel<UserRegistrationModel> {
@AllowNull(false)
@Is('RegistrationState', value => throwIfNotValid(value, isRegistrationStateValid, 'state'))
@Column
state: UserRegistrationStateType
@AllowNull(false)
@Is('RegistrationReason', value => throwIfNotValid(value, isRegistrationReasonValid, 'registration reason'))
@Column(DataType.TEXT)
registrationReason: string
@AllowNull(true)
@Is('RegistrationModerationResponse', value => throwIfNotValid(value, isRegistrationModerationResponseValid, 'moderation response', true))
@Column(DataType.TEXT)
moderationResponse: string
@AllowNull(true)
@Is('RegistrationPassword', value => throwIfNotValid(value, isUserPasswordValid, 'registration password', true))
@Column
password: string
@AllowNull(false)
@Column
username: string
@AllowNull(false)
@IsEmail
@Column(DataType.STRING(400))
email: string
@AllowNull(true)
@Is('RegistrationEmailVerified', value => throwIfNotValid(value, isUserEmailVerifiedValid, 'email verified boolean', true))
@Column
emailVerified: boolean
@AllowNull(true)
@Is('RegistrationAccountDisplayName', value => throwIfNotValid(value, isUserDisplayNameValid, 'account display name', true))
@Column
accountDisplayName: string
@AllowNull(true)
@Is('ChannelHandle', value => throwIfNotValid(value, isVideoChannelDisplayNameValid, 'channel handle', true))
@Column
channelHandle: string
@AllowNull(true)
@Is('ChannelDisplayName', value => throwIfNotValid(value, isVideoChannelDisplayNameValid, 'channel display name', true))
@Column
channelDisplayName: string
@AllowNull(true)
@Column
processedAt: Date
@CreatedAt
createdAt: Date
@UpdatedAt
updatedAt: Date
@ForeignKey(() => UserModel)
@Column
userId: number
@BelongsTo(() => UserModel, {
foreignKey: {
allowNull: true
},
onDelete: 'SET NULL'
})
User: Awaited<UserModel>
@BeforeCreate
static async cryptPasswordIfNeeded (instance: UserRegistrationModel) {
instance.password = await cryptPassword(instance.password)
}
static load (id: number): Promise<MRegistration> {
return UserRegistrationModel.findByPk(id)
}
static loadByEmail (email: string): Promise<MRegistration> {
const query = {
where: { email }
}
return UserRegistrationModel.findOne(query)
}
static loadByEmailOrUsername (emailOrUsername: string): Promise<MRegistration> {
const query = {
where: {
[Op.or]: [
{ email: emailOrUsername },
{ username: emailOrUsername }
]
}
}
return UserRegistrationModel.findOne(query)
}
static loadByEmailOrHandle (options: {
email: string
username: string
channelHandle?: string
}): Promise<MRegistration> {
const { email, username, channelHandle } = options
let or: WhereOptions = [
{ email },
{ channelHandle: username },
{ username }
]
if (channelHandle) {
or = or.concat([
{ username: channelHandle },
{ channelHandle }
])
}
const query = {
where: {
[Op.or]: or
}
}
return UserRegistrationModel.findOne(query)
}
// ---------------------------------------------------------------------------
static listForApi (options: {
start: number
count: number
sort: string
search?: string
}) {
const { start, count, sort, search } = options
const where: WhereOptions = {}
if (search) {
Object.assign(where, {
[Op.or]: [
{
email: {
[Op.iLike]: '%' + search + '%'
}
},
{
username: {
[Op.iLike]: '%' + search + '%'
}
}
]
})
}
const query: FindOptions = {
offset: start,
limit: count,
order: getSort(sort),
where,
include: [
{
model: UserModel.unscoped(),
required: false
}
]
}
return Promise.all([
UserRegistrationModel.count(query),
UserRegistrationModel.findAll<MRegistrationFormattable>(query)
]).then(([ total, data ]) => ({ total, data }))
}
// ---------------------------------------------------------------------------
static getStats () {
const query = `SELECT ` +
`AVG(EXTRACT(EPOCH FROM ("processedAt" - "createdAt") * 1000)) ` +
`FILTER (WHERE "processedAt" IS NOT NULL AND "createdAt" > CURRENT_DATE - INTERVAL '3 months')` +
`AS "avgResponseTime", ` +
// "processedAt" has been introduced in PeerTube 6.1 so also check the abuse state to check processed abuses
`COUNT(*) FILTER (WHERE "processedAt" IS NOT NULL OR "state" != ${UserRegistrationState.PENDING}) AS "processedRequests", ` +
`COUNT(*) AS "totalRequests" ` +
`FROM "userRegistration"`
return UserRegistrationModel.sequelize.query<any>(query, {
type: QueryTypes.SELECT,
raw: true
}).then(([ row ]) => {
return {
totalRegistrationRequests: parseAggregateResult(row.totalRequests),
totalRegistrationRequestsProcessed: parseAggregateResult(row.processedRequests),
averageRegistrationRequestResponseTimeMs: row?.avgResponseTime
? forceNumber(row.avgResponseTime)
: null
}
})
}
// ---------------------------------------------------------------------------
toFormattedJSON (this: MRegistrationFormattable): UserRegistration {
return {
id: this.id,
state: {
id: this.state,
label: USER_REGISTRATION_STATES[this.state]
},
registrationReason: this.registrationReason,
moderationResponse: this.moderationResponse,
username: this.username,
email: this.email,
emailVerified: this.emailVerified,
accountDisplayName: this.accountDisplayName,
channelHandle: this.channelHandle,
channelDisplayName: this.channelDisplayName,
createdAt: this.createdAt,
updatedAt: this.updatedAt,
user: this.User
? { id: this.User.id }
: null
}
}
}
+135
ファイルの表示
@@ -0,0 +1,135 @@
import { DestroyOptions, Op, Transaction } from 'sequelize'
import { AllowNull, BelongsTo, Column, CreatedAt, ForeignKey, IsInt, Table, UpdatedAt } from 'sequelize-typescript'
import { ResultList } from '@peertube/peertube-models'
import { MUserAccountId, MUserId } from '@server/types/models/index.js'
import { VideoModel } from '../video/video.js'
import { UserModel } from './user.js'
import { SequelizeModel } from '../shared/sequelize-type.js'
import { USER_EXPORT_MAX_ITEMS } from '@server/initializers/constants.js'
import { getSort } from '../shared/sort.js'
@Table({
tableName: 'userVideoHistory',
indexes: [
{
fields: [ 'userId', 'videoId' ],
unique: true
},
{
fields: [ 'userId' ]
},
{
fields: [ 'videoId' ]
}
]
})
export class UserVideoHistoryModel extends SequelizeModel<UserVideoHistoryModel> {
@CreatedAt
createdAt: Date
@UpdatedAt
updatedAt: Date
@AllowNull(false)
@IsInt
@Column
currentTime: number
@ForeignKey(() => VideoModel)
@Column
videoId: number
@BelongsTo(() => VideoModel, {
foreignKey: {
allowNull: false
},
onDelete: 'CASCADE'
})
Video: Awaited<VideoModel>
@ForeignKey(() => UserModel)
@Column
userId: number
@BelongsTo(() => UserModel, {
foreignKey: {
allowNull: false
},
onDelete: 'CASCADE'
})
User: Awaited<UserModel>
// FIXME: have to specify the result type to not break peertube typings generation
static listForApi (user: MUserAccountId, start: number, count: number, search?: string): Promise<ResultList<VideoModel>> {
return VideoModel.listForApi({
start,
count,
search,
sort: '-"userVideoHistory"."updatedAt"',
nsfw: null, // All
displayOnlyForFollower: null,
user,
historyOfUser: user
})
}
static async listForExport (user: MUserId) {
const rows = await UserVideoHistoryModel.findAll({
attributes: [ 'createdAt', 'updatedAt', 'currentTime' ],
where: {
userId: user.id
},
limit: USER_EXPORT_MAX_ITEMS,
include: [
{
attributes: [ 'url' ],
model: VideoModel.unscoped(),
required: true
}
],
order: getSort('updatedAt')
})
return rows.map(r => ({ createdAt: r.createdAt, updatedAt: r.updatedAt, currentTime: r.currentTime, videoUrl: r.Video.url }))
}
static removeUserHistoryElement (user: MUserId, videoId: number) {
const query: DestroyOptions = {
where: {
userId: user.id,
videoId
}
}
return UserVideoHistoryModel.destroy(query)
}
static removeUserHistoryBefore (user: MUserId, beforeDate: string, t: Transaction) {
const query: DestroyOptions = {
where: {
userId: user.id
},
transaction: t
}
if (beforeDate) {
query.where['updatedAt'] = {
[Op.lt]: beforeDate
}
}
return UserVideoHistoryModel.destroy(query)
}
static removeOldHistory (beforeDate: string) {
const query: DestroyOptions = {
where: {
updatedAt: {
[Op.lt]: beforeDate
}
}
}
return UserVideoHistoryModel.destroy(query)
}
}
ファイル差分が大きすぎるため省略します 差分を読込み
+2
ファイルの表示
@@ -0,0 +1,2 @@
export * from './video-activity-pub-format.js'
export * from './video-api-format.js'
+1
ファイルの表示
@@ -0,0 +1 @@
export * from './video-format-utils.js'
+7
ファイルの表示
@@ -0,0 +1,7 @@
import { MVideoFile } from '@server/types/models/index.js'
export function sortByResolutionDesc (fileA: MVideoFile, fileB: MVideoFile) {
if (fileA.resolution < fileB.resolution) return 1
if (fileA.resolution === fileB.resolution) return 0
return -1
}
+295
ファイルの表示
@@ -0,0 +1,295 @@
import {
ActivityIconObject,
ActivityPlaylistUrlObject,
ActivityPubStoryboard,
ActivityTagObject,
ActivityTrackerUrlObject,
ActivityUrlObject, VideoCommentPolicy, VideoObject
} from '@peertube/peertube-models'
import { getAPPublicValue } from '@server/helpers/activity-pub-utils.js'
import { isArray } from '@server/helpers/custom-validators/misc.js'
import { generateMagnetUri } from '@server/helpers/webtorrent.js'
import { getActivityStreamDuration } from '@server/lib/activitypub/activity.js'
import { getLocalVideoFileMetadataUrl } from '@server/lib/video-urls.js'
import { WEBSERVER } from '../../../initializers/constants.js'
import {
getLocalVideoChaptersActivityPubUrl,
getLocalVideoCommentsActivityPubUrl,
getLocalVideoDislikesActivityPubUrl,
getLocalVideoLikesActivityPubUrl,
getLocalVideoSharesActivityPubUrl
} from '../../../lib/activitypub/url.js'
import { MStreamingPlaylistFiles, MUserId, MVideo, MVideoAP, MVideoFile } from '../../../types/models/index.js'
import { sortByResolutionDesc } from './shared/index.js'
import { getCategoryLabel, getLanguageLabel, getLicenceLabel } from './video-api-format.js'
export function videoModelToActivityPubObject (video: MVideoAP): VideoObject {
const language = video.language
? { identifier: video.language, name: getLanguageLabel(video.language) }
: undefined
const category = video.category
? { identifier: video.category + '', name: getCategoryLabel(video.category) }
: undefined
const licence = video.licence
? { identifier: video.licence + '', name: getLicenceLabel(video.licence) }
: undefined
const url: ActivityUrlObject[] = [
// HTML url should be the first element in the array so Mastodon correctly displays the embed
{
type: 'Link',
mediaType: 'text/html',
href: WEBSERVER.URL + '/videos/watch/' + video.uuid
} as ActivityUrlObject,
...buildVideoFileUrls({ video, files: video.VideoFiles }),
...buildStreamingPlaylistUrls(video),
...buildTrackerUrls(video)
]
return {
type: 'Video' as 'Video',
id: video.url,
name: video.name,
duration: getActivityStreamDuration(video.duration),
uuid: video.uuid,
category,
licence,
language,
views: video.views,
sensitive: video.nsfw,
waitTranscoding: video.waitTranscoding,
state: video.state,
commentsEnabled: video.commentsPolicy !== VideoCommentPolicy.DISABLED,
canReply: video.commentsPolicy === VideoCommentPolicy.ENABLED
? null
: getAPPublicValue(), // Requires approval
commentsPolicy: video.commentsPolicy,
downloadEnabled: video.downloadEnabled,
published: video.publishedAt.toISOString(),
originallyPublishedAt: video.originallyPublishedAt
? video.originallyPublishedAt.toISOString()
: null,
updated: video.updatedAt.toISOString(),
uploadDate: video.inputFileUpdatedAt?.toISOString(),
tag: buildTags(video),
mediaType: 'text/markdown',
content: video.description,
support: video.support,
subtitleLanguage: buildSubtitleLanguage(video),
icon: buildIcon(video),
preview: buildPreviewAPAttribute(video),
aspectRatio: video.aspectRatio,
url,
likes: getLocalVideoLikesActivityPubUrl(video),
dislikes: getLocalVideoDislikesActivityPubUrl(video),
shares: getLocalVideoSharesActivityPubUrl(video),
comments: getLocalVideoCommentsActivityPubUrl(video),
hasParts: getLocalVideoChaptersActivityPubUrl(video),
attributedTo: [
{
type: 'Person',
id: video.VideoChannel.Account.Actor.url
},
{
type: 'Group',
id: video.VideoChannel.Actor.url
}
],
...buildLiveAPAttributes(video)
}
}
// ---------------------------------------------------------------------------
// Private
// ---------------------------------------------------------------------------
function buildLiveAPAttributes (video: MVideoAP) {
if (!video.isLive) {
return {
isLiveBroadcast: false,
liveSaveReplay: null,
permanentLive: null,
latencyMode: null
}
}
return {
isLiveBroadcast: true,
liveSaveReplay: video.VideoLive.saveReplay,
permanentLive: video.VideoLive.permanentLive,
latencyMode: video.VideoLive.latencyMode
}
}
function buildPreviewAPAttribute (video: MVideoAP): ActivityPubStoryboard[] {
if (!video.Storyboard) return undefined
const storyboard = video.Storyboard
return [
{
type: 'Image',
rel: [ 'storyboard' ],
url: [
{
mediaType: 'image/jpeg',
href: storyboard.getOriginFileUrl(video),
width: storyboard.totalWidth,
height: storyboard.totalHeight,
tileWidth: storyboard.spriteWidth,
tileHeight: storyboard.spriteHeight,
tileDuration: getActivityStreamDuration(storyboard.spriteDuration)
}
]
}
]
}
function buildVideoFileUrls (options: {
video: MVideo
files: MVideoFile[]
user?: MUserId
}): ActivityUrlObject[] {
const { video, files } = options
if (!isArray(files)) return []
const urls: ActivityUrlObject[] = []
const trackerUrls = video.getTrackerUrls()
const sortedFiles = files
.filter(f => !f.isLive())
.sort(sortByResolutionDesc)
for (const file of sortedFiles) {
const fileAP = file.toActivityPubObject(video)
urls.push(fileAP)
urls.push({
type: 'Link',
rel: [ 'metadata', fileAP.mediaType ],
mediaType: 'application/json' as 'application/json',
href: getLocalVideoFileMetadataUrl(video, file),
height: file.height || file.resolution,
width: file.width,
fps: file.fps
})
if (file.hasTorrent()) {
urls.push({
type: 'Link',
mediaType: 'application/x-bittorrent' as 'application/x-bittorrent',
href: file.getTorrentUrl(),
height: file.height || file.resolution,
width: file.width,
fps: file.fps
})
urls.push({
type: 'Link',
mediaType: 'application/x-bittorrent;x-scheme-handler/magnet' as 'application/x-bittorrent;x-scheme-handler/magnet',
href: generateMagnetUri(video, file, trackerUrls),
height: file.height || file.resolution,
width: file.width,
fps: file.fps
})
}
}
return urls
}
// ---------------------------------------------------------------------------
function buildStreamingPlaylistUrls (video: MVideoAP): ActivityPlaylistUrlObject[] {
if (!isArray(video.VideoStreamingPlaylists)) return []
return video.VideoStreamingPlaylists
.map(playlist => ({
type: 'Link',
mediaType: 'application/x-mpegURL' as 'application/x-mpegURL',
href: playlist.getMasterPlaylistUrl(video),
tag: buildStreamingPlaylistTags(video, playlist)
}))
}
function buildStreamingPlaylistTags (video: MVideoAP, playlist: MStreamingPlaylistFiles) {
return [
...playlist.p2pMediaLoaderInfohashes.map(i => ({ type: 'Infohash' as 'Infohash', name: i })),
{
type: 'Link',
name: 'sha256',
mediaType: 'application/json' as 'application/json',
href: playlist.getSha256SegmentsUrl(video)
},
...buildVideoFileUrls({ video, files: playlist.VideoFiles })
] as ActivityTagObject[]
}
// ---------------------------------------------------------------------------
function buildTrackerUrls (video: MVideoAP): ActivityTrackerUrlObject[] {
return video.getTrackerUrls()
.map(trackerUrl => {
const rel2 = trackerUrl.startsWith('http')
? 'http'
: 'websocket'
return {
type: 'Link',
name: `tracker-${rel2}`,
rel: [ 'tracker', rel2 ],
href: trackerUrl
}
})
}
// ---------------------------------------------------------------------------
function buildTags (video: MVideoAP) {
if (!isArray(video.Tags)) return []
return video.Tags.map(t => ({
type: 'Hashtag' as 'Hashtag',
name: t.name
}))
}
function buildIcon (video: MVideoAP): ActivityIconObject[] {
return [ video.getMiniature(), video.getPreview() ]
.map(i => i.toActivityPubObject(video))
}
function buildSubtitleLanguage (video: MVideoAP) {
if (!isArray(video.VideoCaptions)) return []
return video.VideoCaptions
.map(caption => caption.toActivityPubObject(video))
}
+341
ファイルの表示
@@ -0,0 +1,341 @@
import {
Video,
VideoAdditionalAttributes,
VideoCommentPolicy,
VideoDetails,
VideoFile,
VideoInclude,
VideosCommonQueryAfterSanitize,
VideoStreamingPlaylist
} from '@peertube/peertube-models'
import { uuidToShort } from '@peertube/peertube-node-utils'
import { generateMagnetUri } from '@server/helpers/webtorrent.js'
import { tracer } from '@server/lib/opentelemetry/tracing.js'
import { getLocalVideoFileMetadataUrl } from '@server/lib/video-urls.js'
import { VideoViewsManager } from '@server/lib/views/video-views-manager.js'
import { isArray } from '../../../helpers/custom-validators/misc.js'
import {
VIDEO_CATEGORIES,
VIDEO_COMMENTS_POLICY,
VIDEO_LANGUAGES,
VIDEO_LICENCES,
VIDEO_PRIVACIES,
VIDEO_STATES
} from '../../../initializers/constants.js'
import { MServer, MStreamingPlaylistRedundanciesOpt, MVideoFormattable, MVideoFormattableDetails } from '../../../types/models/index.js'
import { MVideoFileRedundanciesOpt } from '../../../types/models/video/video-file.js'
import { sortByResolutionDesc } from './shared/index.js'
export type VideoFormattingJSONOptions = {
completeDescription?: boolean
additionalAttributes?: {
state?: boolean
waitTranscoding?: boolean
scheduledUpdate?: boolean
blacklistInfo?: boolean
files?: boolean
source?: boolean
blockedOwner?: boolean
automaticTags?: boolean
}
}
export function guessAdditionalAttributesFromQuery (query: VideosCommonQueryAfterSanitize): VideoFormattingJSONOptions {
if (!query?.include) return {}
return {
additionalAttributes: {
state: !!(query.include & VideoInclude.NOT_PUBLISHED_STATE),
waitTranscoding: !!(query.include & VideoInclude.NOT_PUBLISHED_STATE),
scheduledUpdate: !!(query.include & VideoInclude.NOT_PUBLISHED_STATE),
blacklistInfo: !!(query.include & VideoInclude.BLACKLISTED),
files: !!(query.include & VideoInclude.FILES),
source: !!(query.include & VideoInclude.SOURCE),
blockedOwner: !!(query.include & VideoInclude.BLOCKED_OWNER),
automaticTags: !!(query.include & VideoInclude.AUTOMATIC_TAGS)
}
}
}
// ---------------------------------------------------------------------------
export function videoModelToFormattedJSON (video: MVideoFormattable, options: VideoFormattingJSONOptions = {}): Video {
const span = tracer.startSpan('peertube.VideoModel.toFormattedJSON')
const userHistory = isArray(video.UserVideoHistories)
? video.UserVideoHistories[0]
: undefined
const videoObject: Video = {
id: video.id,
uuid: video.uuid,
shortUUID: uuidToShort(video.uuid),
url: video.url,
name: video.name,
category: {
id: video.category,
label: getCategoryLabel(video.category)
},
licence: {
id: video.licence,
label: getLicenceLabel(video.licence)
},
language: {
id: video.language,
label: getLanguageLabel(video.language)
},
privacy: {
id: video.privacy,
label: getPrivacyLabel(video.privacy)
},
nsfw: video.nsfw,
truncatedDescription: video.getTruncatedDescription(),
description: options && options.completeDescription === true
? video.description
: video.getTruncatedDescription(),
isLocal: video.isOwned(),
duration: video.duration,
aspectRatio: video.aspectRatio,
views: video.views,
viewers: VideoViewsManager.Instance.getTotalViewersOf(video),
likes: video.likes,
dislikes: video.dislikes,
thumbnailPath: video.getMiniatureStaticPath(),
previewPath: video.getPreviewStaticPath(),
embedPath: video.getEmbedStaticPath(),
createdAt: video.createdAt,
updatedAt: video.updatedAt,
publishedAt: video.publishedAt,
originallyPublishedAt: video.originallyPublishedAt,
isLive: video.isLive,
account: video.VideoChannel.Account.toFormattedSummaryJSON(),
channel: video.VideoChannel.toFormattedSummaryJSON(),
userHistory: userHistory
? { currentTime: userHistory.currentTime }
: undefined,
// Can be added by external plugins
pluginData: (video as any).pluginData,
...buildAdditionalAttributes(video, options)
}
span.end()
return videoObject
}
export function videoModelToFormattedDetailsJSON (video: MVideoFormattableDetails): VideoDetails {
const span = tracer.startSpan('peertube.VideoModel.toFormattedDetailsJSON')
const videoJSON = video.toFormattedJSON({
completeDescription: true,
additionalAttributes: {
scheduledUpdate: true,
blacklistInfo: true,
files: true
}
}) as Video & Required<Pick<Video, 'files' | 'streamingPlaylists' | 'scheduledUpdate' | 'blacklisted' | 'blacklistedReason'>>
const tags = video.Tags
? video.Tags.map(t => t.name)
: []
const detailsJSON = {
...videoJSON,
support: video.support,
descriptionPath: video.getDescriptionAPIPath(),
channel: video.VideoChannel.toFormattedJSON(),
account: video.VideoChannel.Account.toFormattedJSON(),
tags,
// TODO: remove, deprecated in PeerTube 6.2
commentsEnabled: video.commentsPolicy !== VideoCommentPolicy.DISABLED,
commentsPolicy: {
id: video.commentsPolicy,
label: VIDEO_COMMENTS_POLICY[video.commentsPolicy]
},
downloadEnabled: video.downloadEnabled,
waitTranscoding: video.waitTranscoding,
inputFileUpdatedAt: video.inputFileUpdatedAt,
state: {
id: video.state,
label: getStateLabel(video.state)
},
trackerUrls: video.getTrackerUrls()
}
span.end()
return detailsJSON
}
export function streamingPlaylistsModelToFormattedJSON (
video: MVideoFormattable,
playlists: MStreamingPlaylistRedundanciesOpt[]
): VideoStreamingPlaylist[] {
if (isArray(playlists) === false) return []
return playlists
.map(playlist => ({
id: playlist.id,
type: playlist.type,
playlistUrl: playlist.getMasterPlaylistUrl(video),
segmentsSha256Url: playlist.getSha256SegmentsUrl(video),
redundancies: isArray(playlist.RedundancyVideos)
? playlist.RedundancyVideos.map(r => ({ baseUrl: r.fileUrl }))
: [],
files: videoFilesModelToFormattedJSON(video, playlist.VideoFiles)
}))
}
export function videoFilesModelToFormattedJSON (
video: MVideoFormattable,
videoFiles: MVideoFileRedundanciesOpt[],
options: {
includeMagnet?: boolean // default true
} = {}
): VideoFile[] {
const { includeMagnet = true } = options
if (isArray(videoFiles) === false) return []
const trackerUrls = includeMagnet
? video.getTrackerUrls()
: []
return videoFiles
.filter(f => !f.isLive())
.sort(sortByResolutionDesc)
.map(videoFile => {
return {
id: videoFile.id,
resolution: {
id: videoFile.resolution,
label: getResolutionLabel(videoFile.resolution)
},
width: videoFile.width,
height: videoFile.height,
magnetUri: includeMagnet && videoFile.hasTorrent()
? generateMagnetUri(video, videoFile, trackerUrls)
: undefined,
size: videoFile.size,
fps: videoFile.fps,
torrentUrl: videoFile.getTorrentUrl(),
torrentDownloadUrl: videoFile.getTorrentDownloadUrl(),
fileUrl: videoFile.getFileUrl(video),
fileDownloadUrl: videoFile.getFileDownloadUrl(video),
metadataUrl: videoFile.metadataUrl ?? getLocalVideoFileMetadataUrl(video, videoFile)
}
})
}
// ---------------------------------------------------------------------------
export function getCategoryLabel (id: number) {
return VIDEO_CATEGORIES[id] || 'Unknown'
}
export function getLicenceLabel (id: number) {
return VIDEO_LICENCES[id] || 'Unknown'
}
export function getLanguageLabel (id: string) {
return VIDEO_LANGUAGES[id] || 'Unknown'
}
export function getPrivacyLabel (id: number) {
return VIDEO_PRIVACIES[id] || 'Unknown'
}
export function getStateLabel (id: number) {
return VIDEO_STATES[id] || 'Unknown'
}
export function getResolutionLabel (resolution: number) {
if (resolution === 0) return 'Audio'
return `${resolution}p`
}
// ---------------------------------------------------------------------------
// Private
// ---------------------------------------------------------------------------
function buildAdditionalAttributes (video: MVideoFormattable, options: VideoFormattingJSONOptions) {
const add = options.additionalAttributes
const result: Partial<VideoAdditionalAttributes> = {}
if (add?.state === true) {
result.state = {
id: video.state,
label: getStateLabel(video.state)
}
}
if (add?.waitTranscoding === true) {
result.waitTranscoding = video.waitTranscoding
}
if (add?.scheduledUpdate === true && video.ScheduleVideoUpdate) {
result.scheduledUpdate = {
updateAt: video.ScheduleVideoUpdate.updateAt,
privacy: video.ScheduleVideoUpdate.privacy || undefined
}
}
if (add?.blacklistInfo === true) {
result.blacklisted = !!video.VideoBlacklist
result.blacklistedReason =
video.VideoBlacklist
? video.VideoBlacklist.reason
: null
}
if (add?.blockedOwner === true) {
result.blockedOwner = video.VideoChannel.Account.isBlocked()
const server = video.VideoChannel.Account.Actor.Server as MServer
result.blockedServer = !!(server?.isBlocked())
}
if (add?.files === true) {
result.streamingPlaylists = streamingPlaylistsModelToFormattedJSON(video, video.VideoStreamingPlaylists)
result.files = videoFilesModelToFormattedJSON(video, video.VideoFiles)
}
if (add?.source === true) {
result.videoSource = video.VideoSource?.toFormattedJSON() || null
}
if (add?.automaticTags === true) {
result.automaticTags = (video.VideoAutomaticTags || []).map(t => t.AutomaticTag.name)
}
return result
}
+95
ファイルの表示
@@ -0,0 +1,95 @@
import { Op, Transaction } from 'sequelize'
import { AllowNull, BelongsTo, Column, CreatedAt, DataType, Default, ForeignKey, Table, UpdatedAt } from 'sequelize-typescript'
import { VideoPrivacy } from '@peertube/peertube-models'
import { MScheduleVideoUpdate, MScheduleVideoUpdateFormattable } from '@server/types/models/index.js'
import { VideoModel } from './video.js'
import { SequelizeModel } from '../shared/index.js'
@Table({
tableName: 'scheduleVideoUpdate',
indexes: [
{
fields: [ 'videoId' ],
unique: true
},
{
fields: [ 'updateAt' ]
}
]
})
export class ScheduleVideoUpdateModel extends SequelizeModel<ScheduleVideoUpdateModel> {
@AllowNull(false)
@Default(null)
@Column
updateAt: Date
@AllowNull(true)
@Default(null)
@Column(DataType.INTEGER)
privacy: typeof VideoPrivacy.PUBLIC | typeof VideoPrivacy.UNLISTED | typeof VideoPrivacy.INTERNAL
@CreatedAt
createdAt: Date
@UpdatedAt
updatedAt: Date
@ForeignKey(() => VideoModel)
@Column
videoId: number
@BelongsTo(() => VideoModel, {
foreignKey: {
allowNull: false
},
onDelete: 'cascade'
})
Video: Awaited<VideoModel>
static areVideosToUpdate () {
const query = {
logging: false,
attributes: [ 'id' ],
where: {
updateAt: {
[Op.lte]: new Date()
}
}
}
return ScheduleVideoUpdateModel.findOne(query)
.then(res => !!res)
}
static listVideosToUpdate (transaction?: Transaction) {
const query = {
where: {
updateAt: {
[Op.lte]: new Date()
}
},
transaction
}
return ScheduleVideoUpdateModel.findAll<MScheduleVideoUpdate>(query)
}
static deleteByVideoId (videoId: number, t: Transaction) {
const query = {
where: {
videoId
},
transaction: t
}
return ScheduleVideoUpdateModel.destroy(query)
}
toFormattedJSON (this: MScheduleVideoUpdateFormattable) {
return {
updateAt: this.updateAt,
privacy: this.privacy || undefined
}
}
}
+451
ファイルの表示
@@ -0,0 +1,451 @@
import { ActorImageType, VideoPrivacy } from '@peertube/peertube-models'
import { AbstractRunQuery, ModelBuilder } from '@server/models/shared/index.js'
import { Model, Sequelize, Transaction } from 'sequelize'
import { createSafeIn, getSort, parseRowCountResult } from '../../../shared/index.js'
import { VideoCommentTableAttributes } from './video-comment-table-attributes.js'
export interface ListVideoCommentsOptions {
selectType: 'api' | 'feed' | 'comment-only'
autoTagOfAccountId?: number
start?: number
count?: number
sort?: string
videoId?: number
threadId?: number
accountId?: number
blockerAccountIds?: number[]
isThread?: boolean
notDeleted?: boolean
isLocal?: boolean
onLocalVideo?: boolean
onPublicVideo?: boolean
videoChannelOwnerId?: number
videoAccountOwnerId?: number
heldForReview: boolean
heldForReviewAccountIdException?: number
autoTagOneOf?: string[]
search?: string
searchAccount?: string
searchVideo?: string
includeReplyCounters?: boolean
transaction?: Transaction
}
export class VideoCommentListQueryBuilder extends AbstractRunQuery {
private readonly tableAttributes = new VideoCommentTableAttributes()
private innerQuery: string
private select = ''
private joins = ''
private innerSelect = ''
private innerJoins = ''
private innerLateralJoins = ''
private innerWhere = ''
private readonly built = {
cte: false,
accountJoin: false,
videoJoin: false,
videoChannelJoin: false,
avatarJoin: false,
automaticTagsJoin: false
}
constructor (
protected readonly sequelize: Sequelize,
private readonly options: ListVideoCommentsOptions
) {
super(sequelize)
if (this.options.includeReplyCounters && !this.options.videoId) {
throw new Error('Cannot include reply counters without videoId')
}
}
async listComments <T extends Model> () {
this.buildListQuery()
const results = await this.runQuery({ nest: true, transaction: this.options.transaction })
const modelBuilder = new ModelBuilder<T>(this.sequelize)
return modelBuilder.createModels(results, 'VideoComment')
}
async countComments () {
this.buildCountQuery()
const result = await this.runQuery({ transaction: this.options.transaction })
return parseRowCountResult(result)
}
// ---------------------------------------------------------------------------
private buildListQuery () {
this.buildInnerListQuery()
this.buildListSelect()
this.query = `${this.select} ` +
`FROM (${this.innerQuery}) AS "VideoCommentModel" ` +
`${this.joins} ` +
`${this.getOrder()}`
}
private buildInnerListQuery () {
this.buildWhere()
this.buildInnerListSelect()
this.innerQuery = `${this.innerSelect} ` +
`FROM "videoComment" AS "VideoCommentModel" ` +
`${this.innerJoins} ` +
`${this.innerLateralJoins} ` +
`${this.innerWhere} ` +
`${this.getOrder()} ` +
`${this.getInnerLimit()}`
}
// ---------------------------------------------------------------------------
private buildCountQuery () {
this.buildWhere()
this.query = `SELECT COUNT(*) AS "total" ` +
`FROM "videoComment" AS "VideoCommentModel" ` +
`${this.innerJoins} ` +
`${this.innerWhere}`
}
// ---------------------------------------------------------------------------
private buildWhere () {
let where: string[] = []
if (this.options.videoId) {
this.replacements.videoId = this.options.videoId
where.push('"VideoCommentModel"."videoId" = :videoId')
}
if (this.options.threadId) {
this.replacements.threadId = this.options.threadId
where.push('("VideoCommentModel"."id" = :threadId OR "VideoCommentModel"."originCommentId" = :threadId)')
}
if (this.options.accountId) {
this.replacements.accountId = this.options.accountId
where.push('"VideoCommentModel"."accountId" = :accountId')
}
if (this.options.blockerAccountIds) {
this.buildVideoChannelJoin()
where = where.concat(this.getBlockWhere('VideoCommentModel', 'Video->VideoChannel'))
}
if (this.options.isThread === true) {
where.push('"VideoCommentModel"."inReplyToCommentId" IS NULL')
}
if (this.options.notDeleted === true) {
where.push('"VideoCommentModel"."deletedAt" IS NULL')
}
if (this.options.heldForReview === true) {
where.push('"VideoCommentModel"."heldForReview" IS TRUE')
} else if (this.options.heldForReview === false) {
const base = '"VideoCommentModel"."heldForReview" IS FALSE'
if (this.options.heldForReviewAccountIdException) {
this.replacements.heldForReviewAccountIdException = this.options.heldForReviewAccountIdException
where.push(`(${base} OR "VideoCommentModel"."accountId" = :heldForReviewAccountIdException)`)
} else {
where.push(base)
}
}
if (this.options.autoTagOneOf) {
const tags = this.options.autoTagOneOf.map(t => t.toLowerCase())
this.buildAutomaticTagsJoin()
where.push('lower("CommentAutomaticTags->AutomaticTag"."name") IN (' + createSafeIn(this.sequelize, tags) + ')')
}
if (this.options.isLocal === true) {
this.buildAccountJoin()
where.push('"Account->Actor"."serverId" IS NULL')
} else if (this.options.isLocal === false) {
this.buildAccountJoin()
where.push('"Account->Actor"."serverId" IS NOT NULL')
}
if (this.options.onLocalVideo === true) {
this.buildVideoJoin()
where.push('"Video"."remote" IS FALSE')
} else if (this.options.onLocalVideo === false) {
this.buildVideoJoin()
where.push('"Video"."remote" IS TRUE')
}
if (this.options.onPublicVideo === true) {
this.buildVideoJoin()
where.push(`"Video"."privacy" = ${VideoPrivacy.PUBLIC}`)
}
if (this.options.videoAccountOwnerId) {
this.buildVideoChannelJoin()
this.replacements.videoAccountOwnerId = this.options.videoAccountOwnerId
where.push(`"Video->VideoChannel"."accountId" = :videoAccountOwnerId`)
}
if (this.options.videoChannelOwnerId) {
this.buildVideoChannelJoin()
this.replacements.videoChannelOwnerId = this.options.videoChannelOwnerId
where.push(`"Video->VideoChannel"."id" = :videoChannelOwnerId`)
}
if (this.options.search) {
this.buildVideoJoin()
this.buildAccountJoin()
const escapedLikeSearch = this.sequelize.escape('%' + this.options.search + '%')
where.push(
`(` +
`"VideoCommentModel"."text" ILIKE ${escapedLikeSearch} OR ` +
`"Account->Actor"."preferredUsername" ILIKE ${escapedLikeSearch} OR ` +
`"Account"."name" ILIKE ${escapedLikeSearch} OR ` +
`"Video"."name" ILIKE ${escapedLikeSearch} ` +
`)`
)
}
if (this.options.searchAccount) {
this.buildAccountJoin()
const escapedLikeSearch = this.sequelize.escape('%' + this.options.searchAccount + '%')
where.push(
`(` +
`"Account->Actor"."preferredUsername" ILIKE ${escapedLikeSearch} OR ` +
`"Account"."name" ILIKE ${escapedLikeSearch} ` +
`)`
)
}
if (this.options.searchVideo) {
this.buildVideoJoin()
const escapedLikeSearch = this.sequelize.escape('%' + this.options.searchVideo + '%')
where.push(`"Video"."name" ILIKE ${escapedLikeSearch}`)
}
if (where.length !== 0) {
this.innerWhere = `WHERE ${where.join(' AND ')}`
}
}
private buildAccountJoin () {
if (this.built.accountJoin) return
this.innerJoins += ' LEFT JOIN "account" "Account" ON "Account"."id" = "VideoCommentModel"."accountId" ' +
'LEFT JOIN "actor" "Account->Actor" ON "Account->Actor"."id" = "Account"."actorId" ' +
'LEFT JOIN "server" "Account->Actor->Server" ON "Account->Actor"."serverId" = "Account->Actor->Server"."id" '
this.built.accountJoin = true
}
private buildVideoJoin () {
if (this.built.videoJoin) return
this.innerJoins += ' LEFT JOIN "video" "Video" ON "Video"."id" = "VideoCommentModel"."videoId" '
this.built.videoJoin = true
}
private buildVideoChannelJoin () {
if (this.built.videoChannelJoin) return
this.buildVideoJoin()
this.innerJoins += ' LEFT JOIN "videoChannel" "Video->VideoChannel" ON "Video"."channelId" = "Video->VideoChannel"."id" '
this.built.videoChannelJoin = true
}
private buildAvatarsJoin () {
if (this.built.avatarJoin) return
this.joins += `LEFT JOIN "actorImage" "Account->Actor->Avatars" ` +
`ON "VideoCommentModel"."Account.Actor.id" = "Account->Actor->Avatars"."actorId" ` +
`AND "Account->Actor->Avatars"."type" = ${ActorImageType.AVATAR}`
this.built.avatarJoin = true
}
private buildAutomaticTagsJoin () {
if (this.built.automaticTagsJoin) return
this.innerJoins += 'LEFT JOIN (' +
'"commentAutomaticTag" AS "CommentAutomaticTags" INNER JOIN "automaticTag" AS "CommentAutomaticTags->AutomaticTag" ' +
'ON "CommentAutomaticTags->AutomaticTag"."id" = "CommentAutomaticTags"."automaticTagId" ' +
') ON "VideoCommentModel"."id" = "CommentAutomaticTags"."commentId" AND "CommentAutomaticTags"."accountId" = :autoTagOfAccountId'
this.replacements.autoTagOfAccountId = this.options.autoTagOfAccountId
this.built.automaticTagsJoin = true
}
// ---------------------------------------------------------------------------
private buildListSelect () {
const toSelect = [ '"VideoCommentModel".*' ]
if (this.options.selectType === 'api' || this.options.selectType === 'feed') {
this.buildAvatarsJoin()
toSelect.push(this.tableAttributes.getAvatarAttributes())
}
this.select = this.buildSelect(toSelect)
}
private buildInnerListSelect () {
let toSelect = [ this.tableAttributes.getVideoCommentAttributes() ]
if (this.options.selectType === 'api' || this.options.selectType === 'feed') {
this.buildAccountJoin()
this.buildVideoJoin()
toSelect = toSelect.concat([
this.tableAttributes.getVideoAttributes(),
this.tableAttributes.getAccountAttributes(),
this.tableAttributes.getActorAttributes(),
this.tableAttributes.getServerAttributes()
])
}
if (this.options.autoTagOfAccountId && this.options.selectType === 'api') {
this.buildAutomaticTagsJoin()
toSelect = toSelect.concat([
this.tableAttributes.getCommentAutomaticTagAttributes(),
this.tableAttributes.getAutomaticTagAttributes()
])
}
if (this.options.includeReplyCounters === true) {
this.buildTotalRepliesSelect()
this.buildAuthorTotalRepliesSelect()
toSelect.push('"totalRepliesFromVideoAuthor"."count" AS "totalRepliesFromVideoAuthor"')
toSelect.push('"totalReplies"."count" AS "totalReplies"')
}
this.innerSelect = this.buildSelect(toSelect)
}
// ---------------------------------------------------------------------------
private getBlockWhere (commentTableName: string, channelTableName: string) {
const where: string[] = []
const blockerIdsString = createSafeIn(
this.sequelize,
this.options.blockerAccountIds,
[ `"${channelTableName}"."accountId"` ]
)
where.push(
`NOT EXISTS (` +
`SELECT 1 FROM "accountBlocklist" ` +
`WHERE "targetAccountId" = "${commentTableName}"."accountId" ` +
`AND "accountId" IN (${blockerIdsString})` +
`)`
)
where.push(
`NOT EXISTS (` +
`SELECT 1 FROM "account" ` +
`INNER JOIN "actor" ON account."actorId" = actor.id ` +
`INNER JOIN "serverBlocklist" ON "actor"."serverId" = "serverBlocklist"."targetServerId" ` +
`WHERE "account"."id" = "${commentTableName}"."accountId" ` +
`AND "serverBlocklist"."accountId" IN (${blockerIdsString})` +
`)`
)
return where
}
// ---------------------------------------------------------------------------
private buildTotalRepliesSelect () {
const blockWhereString = this.getBlockWhere('replies', 'videoChannel').join(' AND ')
// Help the planner by providing videoId that should filter out many comments
this.replacements.videoId = this.options.videoId
this.innerLateralJoins += `LEFT JOIN LATERAL (` +
`SELECT COUNT("replies"."id") AS "count" FROM "videoComment" AS "replies" ` +
`INNER JOIN "video" ON "video"."id" = "replies"."videoId" AND "replies"."videoId" = :videoId ` +
`LEFT JOIN "videoChannel" ON "video"."channelId" = "videoChannel"."id" ` +
`WHERE "replies"."originCommentId" = "VideoCommentModel"."id" ` +
`AND "deletedAt" IS NULL ` +
`AND ${blockWhereString} ` +
`) "totalReplies" ON TRUE `
}
private buildAuthorTotalRepliesSelect () {
// Help the planner by providing videoId that should filter out many comments
this.replacements.videoId = this.options.videoId
this.innerLateralJoins += `LEFT JOIN LATERAL (` +
`SELECT COUNT("replies"."id") AS "count" FROM "videoComment" AS "replies" ` +
`INNER JOIN "video" ON "video"."id" = "replies"."videoId" AND "replies"."videoId" = :videoId ` +
`INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ` +
`WHERE "replies"."originCommentId" = "VideoCommentModel"."id" AND "replies"."accountId" = "videoChannel"."accountId"` +
`) "totalRepliesFromVideoAuthor" ON TRUE `
}
private getOrder () {
if (!this.options.sort) return ''
const orders = getSort(this.options.sort)
return 'ORDER BY ' + orders.map(o => `"${o[0]}" ${o[1]}`).join(', ')
}
private getInnerLimit () {
if (!this.options.count) return ''
this.replacements.limit = this.options.count
this.replacements.offset = this.options.start || 0
return `LIMIT :limit OFFSET :offset `
}
}
+65
ファイルの表示
@@ -0,0 +1,65 @@
import { Memoize } from '@server/helpers/memoize.js'
import { AccountModel } from '@server/models/account/account.js'
import { ActorImageModel } from '@server/models/actor/actor-image.js'
import { ActorModel } from '@server/models/actor/actor.js'
import { ServerModel } from '@server/models/server/server.js'
import { buildSQLAttributes } from '@server/models/shared/sql.js'
import { AutomaticTagModel } from '../../../automatic-tag/automatic-tag.js'
import { VideoCommentModel } from '../../video-comment.js'
import { CommentAutomaticTagModel } from '@server/models/automatic-tag/comment-automatic-tag.js'
export class VideoCommentTableAttributes {
@Memoize()
getVideoCommentAttributes () {
return VideoCommentModel.getSQLAttributes('VideoCommentModel').join(', ')
}
@Memoize()
getAccountAttributes () {
return AccountModel.getSQLAttributes('Account', 'Account.').join(', ')
}
@Memoize()
getVideoAttributes () {
return [
`"Video"."id" AS "Video.id"`,
`"Video"."uuid" AS "Video.uuid"`,
`"Video"."name" AS "Video.name"`
].join(', ')
}
@Memoize()
getActorAttributes () {
return ActorModel.getSQLAPIAttributes('Account->Actor', `Account.Actor.`).join(', ')
}
@Memoize()
getServerAttributes () {
return ServerModel.getSQLAttributes('Account->Actor->Server', `Account.Actor.Server.`).join(', ')
}
@Memoize()
getAvatarAttributes () {
return ActorImageModel.getSQLAttributes('Account->Actor->Avatars', 'Account.Actor.Avatars.').join(', ')
}
@Memoize()
getCommentAutomaticTagAttributes () {
return buildSQLAttributes({
model: CommentAutomaticTagModel,
tableName: 'CommentAutomaticTags',
aliasPrefix: 'CommentAutomaticTags.',
idBuilder: [ 'commentId', 'automaticTagId', 'accountId' ]
}).join(', ')
}
@Memoize()
getAutomaticTagAttributes () {
return buildSQLAttributes({
model: AutomaticTagModel,
tableName: 'CommentAutomaticTags->AutomaticTag',
aliasPrefix: 'CommentAutomaticTags.AutomaticTag.'
}).join(', ')
}
}
+3
ファイルの表示
@@ -0,0 +1,3 @@
export * from './video-model-get-query-builder.js'
export * from './videos-id-list-query-builder.js'
export * from './videos-model-list-query-builder.js'
+370
ファイルの表示
@@ -0,0 +1,370 @@
import { Sequelize } from 'sequelize'
import validator from 'validator'
import { MUserAccountId } from '@server/types/models/index.js'
import { ActorImageType } from '@peertube/peertube-models'
import { AbstractRunQuery } from '../../../../shared/abstract-run-query.js'
import { createSafeIn } from '../../../../shared/index.js'
import { VideoTableAttributes } from './video-table-attributes.js'
/**
*
* Abstract builder to create SQL query and fetch video models
*
*/
export class AbstractVideoQueryBuilder extends AbstractRunQuery {
protected attributes: { [key: string]: string } = {}
protected joins = ''
protected where: string
protected tables: VideoTableAttributes
constructor (
protected readonly sequelize: Sequelize,
protected readonly mode: 'list' | 'get'
) {
super(sequelize)
this.tables = new VideoTableAttributes(this.mode)
}
protected buildSelect () {
return 'SELECT ' + Object.keys(this.attributes).map(key => {
const value = this.attributes[key]
if (value) return `${key} AS ${value}`
return key
}).join(', ')
}
protected includeChannels () {
this.addJoin('INNER JOIN "videoChannel" AS "VideoChannel" ON "video"."channelId" = "VideoChannel"."id"')
this.addJoin('INNER JOIN "actor" AS "VideoChannel->Actor" ON "VideoChannel"."actorId" = "VideoChannel->Actor"."id"')
this.addJoin(
'LEFT OUTER JOIN "server" AS "VideoChannel->Actor->Server" ON "VideoChannel->Actor"."serverId" = "VideoChannel->Actor->Server"."id"'
)
this.addJoin(
'LEFT OUTER JOIN "actorImage" AS "VideoChannel->Actor->Avatars" ' +
'ON "VideoChannel->Actor"."id" = "VideoChannel->Actor->Avatars"."actorId" ' +
`AND "VideoChannel->Actor->Avatars"."type" = ${ActorImageType.AVATAR}`
)
this.attributes = {
...this.attributes,
...this.buildAttributesObject('VideoChannel', this.tables.getChannelAttributes()),
...this.buildActorInclude('VideoChannel->Actor'),
...this.buildAvatarInclude('VideoChannel->Actor->Avatars'),
...this.buildServerInclude('VideoChannel->Actor->Server')
}
}
protected includeAccounts () {
this.addJoin('INNER JOIN "account" AS "VideoChannel->Account" ON "VideoChannel"."accountId" = "VideoChannel->Account"."id"')
this.addJoin(
'INNER JOIN "actor" AS "VideoChannel->Account->Actor" ON "VideoChannel->Account"."actorId" = "VideoChannel->Account->Actor"."id"'
)
this.addJoin(
'LEFT OUTER JOIN "server" AS "VideoChannel->Account->Actor->Server" ' +
'ON "VideoChannel->Account->Actor"."serverId" = "VideoChannel->Account->Actor->Server"."id"'
)
this.addJoin(
'LEFT OUTER JOIN "actorImage" AS "VideoChannel->Account->Actor->Avatars" ' +
'ON "VideoChannel->Account"."actorId"= "VideoChannel->Account->Actor->Avatars"."actorId" ' +
`AND "VideoChannel->Account->Actor->Avatars"."type" = ${ActorImageType.AVATAR}`
)
this.attributes = {
...this.attributes,
...this.buildAttributesObject('VideoChannel->Account', this.tables.getAccountAttributes()),
...this.buildActorInclude('VideoChannel->Account->Actor'),
...this.buildAvatarInclude('VideoChannel->Account->Actor->Avatars'),
...this.buildServerInclude('VideoChannel->Account->Actor->Server')
}
}
protected includeOwnerUser () {
this.addJoin('INNER JOIN "videoChannel" AS "VideoChannel" ON "video"."channelId" = "VideoChannel"."id"')
this.addJoin('INNER JOIN "account" AS "VideoChannel->Account" ON "VideoChannel"."accountId" = "VideoChannel->Account"."id"')
this.attributes = {
...this.attributes,
...this.buildAttributesObject('VideoChannel', this.tables.getChannelAttributes()),
...this.buildAttributesObject('VideoChannel->Account', this.tables.getUserAccountAttributes())
}
}
protected includeThumbnails () {
this.addJoin('LEFT OUTER JOIN "thumbnail" AS "Thumbnails" ON "video"."id" = "Thumbnails"."videoId"')
this.attributes = {
...this.attributes,
...this.buildAttributesObject('Thumbnails', this.tables.getThumbnailAttributes())
}
}
protected includeWebVideoFiles () {
this.addJoin('LEFT JOIN "videoFile" AS "VideoFiles" ON "VideoFiles"."videoId" = "video"."id"')
this.attributes = {
...this.attributes,
...this.buildAttributesObject('VideoFiles', this.tables.getFileAttributes())
}
}
protected includeStreamingPlaylistFiles () {
this.addJoin(
'LEFT JOIN "videoStreamingPlaylist" AS "VideoStreamingPlaylists" ON "VideoStreamingPlaylists"."videoId" = "video"."id"'
)
this.addJoin(
'LEFT JOIN "videoFile" AS "VideoStreamingPlaylists->VideoFiles" ' +
'ON "VideoStreamingPlaylists->VideoFiles"."videoStreamingPlaylistId" = "VideoStreamingPlaylists"."id"'
)
this.attributes = {
...this.attributes,
...this.buildAttributesObject('VideoStreamingPlaylists', this.tables.getStreamingPlaylistAttributes()),
...this.buildAttributesObject('VideoStreamingPlaylists->VideoFiles', this.tables.getFileAttributes())
}
}
protected includeUserHistory (userId: number) {
this.addJoin(
'LEFT OUTER JOIN "userVideoHistory" ' +
'ON "video"."id" = "userVideoHistory"."videoId" AND "userVideoHistory"."userId" = :userVideoHistoryId'
)
this.replacements.userVideoHistoryId = userId
this.attributes = {
...this.attributes,
...this.buildAttributesObject('userVideoHistory', this.tables.getUserHistoryAttributes())
}
}
protected includePlaylist (playlistId: number) {
this.addJoin(
'INNER JOIN "videoPlaylistElement" as "VideoPlaylistElement" ON "videoPlaylistElement"."videoId" = "video"."id" ' +
'AND "VideoPlaylistElement"."videoPlaylistId" = :videoPlaylistId'
)
this.replacements.videoPlaylistId = playlistId
this.attributes = {
...this.attributes,
...this.buildAttributesObject('VideoPlaylistElement', this.tables.getPlaylistAttributes())
}
}
protected includeTags () {
this.addJoin(
'LEFT OUTER JOIN (' +
'"videoTag" AS "Tags->VideoTagModel" INNER JOIN "tag" AS "Tags" ON "Tags"."id" = "Tags->VideoTagModel"."tagId"' +
') ' +
'ON "video"."id" = "Tags->VideoTagModel"."videoId"'
)
this.attributes = {
...this.attributes,
...this.buildAttributesObject('Tags', this.tables.getTagAttributes()),
...this.buildAttributesObject('Tags->VideoTagModel', this.tables.getVideoTagAttributes())
}
}
protected includeBlacklisted () {
this.addJoin(
'LEFT OUTER JOIN "videoBlacklist" AS "VideoBlacklist" ON "video"."id" = "VideoBlacklist"."videoId"'
)
this.attributes = {
...this.attributes,
...this.buildAttributesObject('VideoBlacklist', this.tables.getBlacklistedAttributes())
}
}
protected includeBlockedOwnerAndServer (serverAccountId: number, user?: MUserAccountId) {
const blockerIds = [ serverAccountId ]
if (user) blockerIds.push(user.Account.id)
const inClause = createSafeIn(this.sequelize, blockerIds)
this.addJoin(
'LEFT JOIN "accountBlocklist" AS "VideoChannel->Account->AccountBlocklist" ' +
'ON "VideoChannel->Account"."id" = "VideoChannel->Account->AccountBlocklist"."targetAccountId" ' +
'AND "VideoChannel->Account->AccountBlocklist"."accountId" IN (' + inClause + ')'
)
this.addJoin(
'LEFT JOIN "serverBlocklist" AS "VideoChannel->Account->Actor->Server->ServerBlocklist" ' +
'ON "VideoChannel->Account->Actor->Server->ServerBlocklist"."targetServerId" = "VideoChannel->Account->Actor"."serverId" ' +
'AND "VideoChannel->Account->Actor->Server->ServerBlocklist"."accountId" IN (' + inClause + ') '
)
this.attributes = {
...this.attributes,
...this.buildAttributesObject('VideoChannel->Account->AccountBlocklist', this.tables.getBlocklistAttributes()),
...this.buildAttributesObject('VideoChannel->Account->Actor->Server->ServerBlocklist', this.tables.getBlocklistAttributes())
}
}
protected includeScheduleUpdate () {
this.addJoin(
'LEFT OUTER JOIN "scheduleVideoUpdate" AS "ScheduleVideoUpdate" ON "video"."id" = "ScheduleVideoUpdate"."videoId"'
)
this.attributes = {
...this.attributes,
...this.buildAttributesObject('ScheduleVideoUpdate', this.tables.getScheduleUpdateAttributes())
}
}
protected includeLive () {
this.addJoin(
'LEFT OUTER JOIN "videoLive" AS "VideoLive" ON "video"."id" = "VideoLive"."videoId"'
)
this.attributes = {
...this.attributes,
...this.buildAttributesObject('VideoLive', this.tables.getLiveAttributes())
}
}
protected includeVideoSource () {
this.addJoin(
'LEFT OUTER JOIN "videoSource" AS "VideoSource" ON "video"."id" = "VideoSource"."videoId"'
)
this.attributes = {
...this.attributes,
...this.buildAttributesObject('VideoSource', this.tables.getVideoSourceAttributes())
}
}
protected includeAutomaticTags (autoTagOfAccountId: number) {
this.addJoin(
'LEFT JOIN (' +
'"videoAutomaticTag" AS "VideoAutomaticTags" INNER JOIN "automaticTag" AS "VideoAutomaticTags->AutomaticTag" ' +
'ON "VideoAutomaticTags->AutomaticTag"."id" = "VideoAutomaticTags"."automaticTagId" ' +
') ON "video"."id" = "VideoAutomaticTags"."videoId" AND "VideoAutomaticTags"."accountId" = :autoTagOfAccountId'
)
this.replacements.autoTagOfAccountId = autoTagOfAccountId
this.attributes = {
...this.attributes,
...this.buildAttributesObject('VideoAutomaticTags', this.tables.getVideoAutoTagAttributes()),
...this.buildAttributesObject('VideoAutomaticTags->AutomaticTag', this.tables.getAutoTagAttributes())
}
}
protected includeTrackers () {
this.addJoin(
'LEFT OUTER JOIN (' +
'"videoTracker" AS "Trackers->VideoTrackerModel" ' +
'INNER JOIN "tracker" AS "Trackers" ON "Trackers"."id" = "Trackers->VideoTrackerModel"."trackerId"' +
') ON "video"."id" = "Trackers->VideoTrackerModel"."videoId"'
)
this.attributes = {
...this.attributes,
...this.buildAttributesObject('Trackers', this.tables.getTrackerAttributes()),
...this.buildAttributesObject('Trackers->VideoTrackerModel', this.tables.getVideoTrackerAttributes())
}
}
protected includeWebVideoRedundancies () {
this.addJoin(
'LEFT OUTER JOIN "videoRedundancy" AS "VideoFiles->RedundancyVideos" ON ' +
'"VideoFiles"."id" = "VideoFiles->RedundancyVideos"."videoFileId"'
)
this.attributes = {
...this.attributes,
...this.buildAttributesObject('VideoFiles->RedundancyVideos', this.tables.getRedundancyAttributes())
}
}
protected includeStreamingPlaylistRedundancies () {
this.addJoin(
'LEFT OUTER JOIN "videoRedundancy" AS "VideoStreamingPlaylists->RedundancyVideos" ' +
'ON "VideoStreamingPlaylists"."id" = "VideoStreamingPlaylists->RedundancyVideos"."videoStreamingPlaylistId"'
)
this.attributes = {
...this.attributes,
...this.buildAttributesObject('VideoStreamingPlaylists->RedundancyVideos', this.tables.getRedundancyAttributes())
}
}
protected buildActorInclude (prefixKey: string) {
return this.buildAttributesObject(prefixKey, this.tables.getActorAttributes())
}
protected buildAvatarInclude (prefixKey: string) {
return this.buildAttributesObject(prefixKey, this.tables.getAvatarAttributes())
}
protected buildServerInclude (prefixKey: string) {
return this.buildAttributesObject(prefixKey, this.tables.getServerAttributes())
}
protected buildAttributesObject (prefixKey: string, attributeKeys: string[]) {
const result: { [id: string]: string } = {}
const prefixValue = prefixKey.replace(/->/g, '.')
for (const attribute of attributeKeys) {
result[`"${prefixKey}"."${attribute}"`] = `"${prefixValue}.${attribute}"`
}
return result
}
protected whereId (options: { ids?: number[], id?: string | number, url?: string }) {
if (options.ids) {
this.where = `WHERE "video"."id" IN (${createSafeIn(this.sequelize, options.ids)})`
return
}
if (options.url) {
this.where = 'WHERE "video"."url" = :videoUrl'
this.replacements.videoUrl = options.url
return
}
if (validator.default.isInt('' + options.id)) {
this.where = 'WHERE "video".id = :videoId'
} else {
this.where = 'WHERE uuid = :videoId'
}
this.replacements.videoId = options.id
}
protected addJoin (join: string) {
this.joins += join + ' '
}
}
+75
ファイルの表示
@@ -0,0 +1,75 @@
import { Sequelize, Transaction } from 'sequelize'
import { AbstractVideoQueryBuilder } from './abstract-video-query-builder.js'
export type FileQueryOptions = {
id?: string | number
url?: string
includeRedundancy: boolean
transaction?: Transaction
logging?: boolean
}
/**
*
* Fetch files (web videos and streaming playlist) according to a video
*
*/
export class VideoFileQueryBuilder extends AbstractVideoQueryBuilder {
protected attributes: { [key: string]: string }
constructor (protected readonly sequelize: Sequelize) {
super(sequelize, 'get')
}
queryWebVideos (options: FileQueryOptions) {
this.buildWebVideoFilesQuery(options)
return this.runQuery(options)
}
queryStreamingPlaylistVideos (options: FileQueryOptions) {
this.buildVideoStreamingPlaylistFilesQuery(options)
return this.runQuery(options)
}
private buildWebVideoFilesQuery (options: FileQueryOptions) {
this.attributes = {
'"video"."id"': ''
}
this.includeWebVideoFiles()
if (options.includeRedundancy) {
this.includeWebVideoRedundancies()
}
this.whereId(options)
this.query = this.buildQuery()
}
private buildVideoStreamingPlaylistFilesQuery (options: FileQueryOptions) {
this.attributes = {
'"video"."id"': ''
}
this.includeStreamingPlaylistFiles()
if (options.includeRedundancy) {
this.includeStreamingPlaylistRedundancies()
}
this.whereId(options)
this.query = this.buildQuery()
}
private buildQuery () {
return `${this.buildSelect()} FROM "video" ${this.joins} ${this.where}`
}
}
+450
ファイルの表示
@@ -0,0 +1,450 @@
import { VideoInclude, VideoIncludeType } from '@peertube/peertube-models'
import { AccountBlocklistModel } from '@server/models/account/account-blocklist.js'
import { AccountModel } from '@server/models/account/account.js'
import { ActorImageModel } from '@server/models/actor/actor-image.js'
import { ActorModel } from '@server/models/actor/actor.js'
import { AutomaticTagModel } from '@server/models/automatic-tag/automatic-tag.js'
import { VideoAutomaticTagModel } from '@server/models/automatic-tag/video-automatic-tag.js'
import { VideoRedundancyModel } from '@server/models/redundancy/video-redundancy.js'
import { ServerBlocklistModel } from '@server/models/server/server-blocklist.js'
import { ServerModel } from '@server/models/server/server.js'
import { TrackerModel } from '@server/models/server/tracker.js'
import { UserVideoHistoryModel } from '@server/models/user/user-video-history.js'
import { VideoSourceModel } from '@server/models/video/video-source.js'
import { ScheduleVideoUpdateModel } from '../../../schedule-video-update.js'
import { TagModel } from '../../../tag.js'
import { ThumbnailModel } from '../../../thumbnail.js'
import { VideoBlacklistModel } from '../../../video-blacklist.js'
import { VideoChannelModel } from '../../../video-channel.js'
import { VideoFileModel } from '../../../video-file.js'
import { VideoLiveModel } from '../../../video-live.js'
import { VideoStreamingPlaylistModel } from '../../../video-streaming-playlist.js'
import { VideoModel } from '../../../video.js'
import { VideoTableAttributes } from './video-table-attributes.js'
type SQLRow = { [id: string]: string | number }
/**
*
* Build video models from SQL rows
*
*/
export class VideoModelBuilder {
private videosMemo: { [ id: number ]: VideoModel }
private videoStreamingPlaylistMemo: { [ id: number ]: VideoStreamingPlaylistModel }
private videoFileMemo: { [ id: number ]: VideoFileModel }
private thumbnailsDone: Set<any>
private actorImagesDone: Set<any>
private historyDone: Set<any>
private blacklistDone: Set<any>
private accountBlocklistDone: Set<any>
private serverBlocklistDone: Set<any>
private liveDone: Set<any>
private sourceDone: Set<any>
private redundancyDone: Set<any>
private scheduleVideoUpdateDone: Set<any>
private trackersDone: Set<string>
private tagsDone: Set<string>
private autoTagsDone: Set<string>
private videos: VideoModel[]
private readonly buildOpts = { raw: true, isNewRecord: false }
constructor (
private readonly mode: 'get' | 'list',
private readonly tables: VideoTableAttributes
) {
}
buildVideosFromRows (options: {
rows: SQLRow[]
include?: VideoIncludeType
rowsWebVideoFiles?: SQLRow[]
rowsStreamingPlaylist?: SQLRow[]
}) {
const { rows, rowsWebVideoFiles, rowsStreamingPlaylist, include } = options
this.reinit()
for (const row of rows) {
this.buildVideoAndAccount(row)
const videoModel = this.videosMemo[row.id as number]
this.setUserHistory(row, videoModel)
this.addThumbnail(row, videoModel)
const channelActor = videoModel.VideoChannel?.Actor
if (channelActor) {
this.addActorAvatar(row, 'VideoChannel.Actor', channelActor)
}
const accountActor = videoModel.VideoChannel?.Account?.Actor
if (accountActor) {
this.addActorAvatar(row, 'VideoChannel.Account.Actor', accountActor)
}
if (!rowsWebVideoFiles) {
this.addWebVideoFile(row, videoModel)
}
if (!rowsStreamingPlaylist) {
this.addStreamingPlaylist(row, videoModel)
this.addStreamingPlaylistFile(row)
}
if (this.mode === 'get') {
this.addTag(row, videoModel)
this.addTracker(row, videoModel)
this.setBlacklisted(row, videoModel)
this.setScheduleVideoUpdate(row, videoModel)
this.setLive(row, videoModel)
} else {
if (include & VideoInclude.BLACKLISTED) {
this.setBlacklisted(row, videoModel)
}
if (include & VideoInclude.BLOCKED_OWNER) {
this.setBlockedOwner(row, videoModel)
this.setBlockedServer(row, videoModel)
}
if (include & VideoInclude.SOURCE) {
this.setSource(row, videoModel)
}
if (include & VideoInclude.AUTOMATIC_TAGS) {
this.addAutoTag(row, videoModel)
}
}
}
this.grabSeparateWebVideoFiles(rowsWebVideoFiles)
this.grabSeparateStreamingPlaylistFiles(rowsStreamingPlaylist)
return this.videos
}
private reinit () {
this.videosMemo = {}
this.videoStreamingPlaylistMemo = {}
this.videoFileMemo = {}
this.thumbnailsDone = new Set()
this.actorImagesDone = new Set()
this.historyDone = new Set()
this.blacklistDone = new Set()
this.liveDone = new Set()
this.sourceDone = new Set()
this.redundancyDone = new Set()
this.scheduleVideoUpdateDone = new Set()
this.accountBlocklistDone = new Set()
this.serverBlocklistDone = new Set()
this.trackersDone = new Set()
this.tagsDone = new Set()
this.autoTagsDone = new Set()
this.videos = []
}
private grabSeparateWebVideoFiles (rowsWebVideoFiles?: SQLRow[]) {
if (!rowsWebVideoFiles) return
for (const row of rowsWebVideoFiles) {
const id = row['VideoFiles.id']
if (!id) continue
const videoModel = this.videosMemo[row.id]
this.addWebVideoFile(row, videoModel)
this.addRedundancy(row, 'VideoFiles', this.videoFileMemo[id])
}
}
private grabSeparateStreamingPlaylistFiles (rowsStreamingPlaylist?: SQLRow[]) {
if (!rowsStreamingPlaylist) return
for (const row of rowsStreamingPlaylist) {
const id = row['VideoStreamingPlaylists.id']
if (!id) continue
const videoModel = this.videosMemo[row.id]
this.addStreamingPlaylist(row, videoModel)
this.addStreamingPlaylistFile(row)
this.addRedundancy(row, 'VideoStreamingPlaylists', this.videoStreamingPlaylistMemo[id])
}
}
private buildVideoAndAccount (row: SQLRow) {
if (this.videosMemo[row.id]) return
const videoModel = new VideoModel(this.grab(row, this.tables.getVideoAttributes(), ''), this.buildOpts)
videoModel.UserVideoHistories = []
videoModel.Thumbnails = []
videoModel.VideoFiles = []
videoModel.VideoStreamingPlaylists = []
videoModel.Tags = []
videoModel.VideoAutomaticTags = []
videoModel.Trackers = []
this.buildAccount(row, videoModel)
this.videosMemo[row.id] = videoModel
// Keep rows order
this.videos.push(videoModel)
}
private buildAccount (row: SQLRow, videoModel: VideoModel) {
const id = row['VideoChannel.Account.id']
if (!id) return
const channelModel = new VideoChannelModel(this.grab(row, this.tables.getChannelAttributes(), 'VideoChannel'), this.buildOpts)
channelModel.Actor = this.buildActor(row, 'VideoChannel')
const accountModel = new AccountModel(this.grab(row, this.tables.getAccountAttributes(), 'VideoChannel.Account'), this.buildOpts)
accountModel.Actor = this.buildActor(row, 'VideoChannel.Account')
accountModel.BlockedBy = []
channelModel.Account = accountModel
videoModel.VideoChannel = channelModel
}
private buildActor (row: SQLRow, prefix: string) {
const actorPrefix = `${prefix}.Actor`
const serverPrefix = `${actorPrefix}.Server`
const serverModel = row[`${serverPrefix}.id`] !== null
? new ServerModel(this.grab(row, this.tables.getServerAttributes(), serverPrefix), this.buildOpts)
: null
if (serverModel) serverModel.BlockedBy = []
const actorModel = new ActorModel(this.grab(row, this.tables.getActorAttributes(), actorPrefix), this.buildOpts)
actorModel.Server = serverModel
actorModel.Avatars = []
return actorModel
}
private setUserHistory (row: SQLRow, videoModel: VideoModel) {
const id = row['userVideoHistory.id']
if (!id || this.historyDone.has(id)) return
const attributes = this.grab(row, this.tables.getUserHistoryAttributes(), 'userVideoHistory')
const historyModel = new UserVideoHistoryModel(attributes, this.buildOpts)
videoModel.UserVideoHistories.push(historyModel)
this.historyDone.add(id)
}
private addActorAvatar (row: SQLRow, actorPrefix: string, actor: ActorModel) {
const avatarPrefix = `${actorPrefix}.Avatars`
const id = row[`${avatarPrefix}.id`]
const key = `${row.id}${id}`
if (!id || this.actorImagesDone.has(key)) return
const attributes = this.grab(row, this.tables.getAvatarAttributes(), avatarPrefix)
const avatarModel = new ActorImageModel(attributes, this.buildOpts)
actor.Avatars.push(avatarModel)
this.actorImagesDone.add(key)
}
private addThumbnail (row: SQLRow, videoModel: VideoModel) {
const id = row['Thumbnails.id']
if (!id || this.thumbnailsDone.has(id)) return
const attributes = this.grab(row, this.tables.getThumbnailAttributes(), 'Thumbnails')
const thumbnailModel = new ThumbnailModel(attributes, this.buildOpts)
videoModel.Thumbnails.push(thumbnailModel)
this.thumbnailsDone.add(id)
}
private addWebVideoFile (row: SQLRow, videoModel: VideoModel) {
const id = row['VideoFiles.id']
if (!id || this.videoFileMemo[id]) return
const attributes = this.grab(row, this.tables.getFileAttributes(), 'VideoFiles')
const videoFileModel = new VideoFileModel(attributes, this.buildOpts)
videoModel.VideoFiles.push(videoFileModel)
this.videoFileMemo[id] = videoFileModel
}
private addStreamingPlaylist (row: SQLRow, videoModel: VideoModel) {
const id = row['VideoStreamingPlaylists.id']
if (!id || this.videoStreamingPlaylistMemo[id]) return
const attributes = this.grab(row, this.tables.getStreamingPlaylistAttributes(), 'VideoStreamingPlaylists')
const streamingPlaylist = new VideoStreamingPlaylistModel(attributes, this.buildOpts)
streamingPlaylist.VideoFiles = []
videoModel.VideoStreamingPlaylists.push(streamingPlaylist)
this.videoStreamingPlaylistMemo[id] = streamingPlaylist
}
private addStreamingPlaylistFile (row: SQLRow) {
const id = row['VideoStreamingPlaylists.VideoFiles.id']
if (!id || this.videoFileMemo[id]) return
const streamingPlaylist = this.videoStreamingPlaylistMemo[row['VideoStreamingPlaylists.id']]
const attributes = this.grab(row, this.tables.getFileAttributes(), 'VideoStreamingPlaylists.VideoFiles')
const videoFileModel = new VideoFileModel(attributes, this.buildOpts)
streamingPlaylist.VideoFiles.push(videoFileModel)
this.videoFileMemo[id] = videoFileModel
}
private addRedundancy (row: SQLRow, prefix: string, to: VideoFileModel | VideoStreamingPlaylistModel) {
if (!to.RedundancyVideos) to.RedundancyVideos = []
const redundancyPrefix = `${prefix}.RedundancyVideos`
const id = row[`${redundancyPrefix}.id`]
if (!id || this.redundancyDone.has(id)) return
const attributes = this.grab(row, this.tables.getRedundancyAttributes(), redundancyPrefix)
const redundancyModel = new VideoRedundancyModel(attributes, this.buildOpts)
to.RedundancyVideos.push(redundancyModel)
this.redundancyDone.add(id)
}
private addTag (row: SQLRow, videoModel: VideoModel) {
if (!row['Tags.name']) return
const key = `${row['Tags.VideoTagModel.videoId']}-${row['Tags.VideoTagModel.tagId']}`
if (this.tagsDone.has(key)) return
const attributes = this.grab(row, this.tables.getTagAttributes(), 'Tags')
const tagModel = new TagModel(attributes, this.buildOpts)
videoModel.Tags.push(tagModel)
this.tagsDone.add(key)
}
private addAutoTag (row: SQLRow, videoModel: VideoModel) {
if (!row['VideoAutomaticTags.AutomaticTag.id']) return
const key = `${row['VideoAutomaticTags.videoId']}-${row['VideoAutomaticTags.accountId']}-${row['VideoAutomaticTags.automaticTagId']}`
if (this.autoTagsDone.has(key)) return
const videoAutomaticTagAttributes = this.grab(row, this.tables.getVideoAutoTagAttributes(), 'VideoAutomaticTags')
const automaticTagModel = new VideoAutomaticTagModel(videoAutomaticTagAttributes, this.buildOpts)
const automaticTagAttributes = this.grab(row, this.tables.getAutoTagAttributes(), 'VideoAutomaticTags.AutomaticTag')
automaticTagModel.AutomaticTag = new AutomaticTagModel(automaticTagAttributes, this.buildOpts)
videoModel.VideoAutomaticTags.push(automaticTagModel)
this.autoTagsDone.add(key)
}
private addTracker (row: SQLRow, videoModel: VideoModel) {
if (!row['Trackers.id']) return
const key = `${row['Trackers.VideoTrackerModel.videoId']}-${row['Trackers.VideoTrackerModel.trackerId']}`
if (this.trackersDone.has(key)) return
const attributes = this.grab(row, this.tables.getTrackerAttributes(), 'Trackers')
const trackerModel = new TrackerModel(attributes, this.buildOpts)
videoModel.Trackers.push(trackerModel)
this.trackersDone.add(key)
}
private setBlacklisted (row: SQLRow, videoModel: VideoModel) {
const id = row['VideoBlacklist.id']
if (!id || this.blacklistDone.has(id)) return
const attributes = this.grab(row, this.tables.getBlacklistedAttributes(), 'VideoBlacklist')
videoModel.VideoBlacklist = new VideoBlacklistModel(attributes, this.buildOpts)
this.blacklistDone.add(id)
}
private setBlockedOwner (row: SQLRow, videoModel: VideoModel) {
const id = row['VideoChannel.Account.AccountBlocklist.id']
if (!id) return
const key = `${videoModel.id}-${id}`
if (this.accountBlocklistDone.has(key)) return
const attributes = this.grab(row, this.tables.getBlocklistAttributes(), 'VideoChannel.Account.AccountBlocklist')
videoModel.VideoChannel.Account.BlockedBy.push(new AccountBlocklistModel(attributes, this.buildOpts))
this.accountBlocklistDone.add(key)
}
private setBlockedServer (row: SQLRow, videoModel: VideoModel) {
const id = row['VideoChannel.Account.Actor.Server.ServerBlocklist.id']
if (!id || this.serverBlocklistDone.has(id)) return
const key = `${videoModel.id}-${id}`
if (this.serverBlocklistDone.has(key)) return
const attributes = this.grab(row, this.tables.getBlocklistAttributes(), 'VideoChannel.Account.Actor.Server.ServerBlocklist')
videoModel.VideoChannel.Account.Actor.Server.BlockedBy.push(new ServerBlocklistModel(attributes, this.buildOpts))
this.serverBlocklistDone.add(key)
}
private setScheduleVideoUpdate (row: SQLRow, videoModel: VideoModel) {
const id = row['ScheduleVideoUpdate.id']
if (!id || this.scheduleVideoUpdateDone.has(id)) return
const attributes = this.grab(row, this.tables.getScheduleUpdateAttributes(), 'ScheduleVideoUpdate')
videoModel.ScheduleVideoUpdate = new ScheduleVideoUpdateModel(attributes, this.buildOpts)
this.scheduleVideoUpdateDone.add(id)
}
private setLive (row: SQLRow, videoModel: VideoModel) {
const id = row['VideoLive.id']
if (!id || this.liveDone.has(id)) return
const attributes = this.grab(row, this.tables.getLiveAttributes(), 'VideoLive')
videoModel.VideoLive = new VideoLiveModel(attributes, this.buildOpts)
this.liveDone.add(id)
}
private setSource (row: SQLRow, videoModel: VideoModel) {
const id = row['VideoSource.id']
if (!id || this.sourceDone.has(id)) return
const attributes = this.grab(row, this.tables.getVideoSourceAttributes(), 'VideoSource')
videoModel.VideoSource = new VideoSourceModel(attributes, this.buildOpts)
this.sourceDone.add(id)
}
private grab (row: SQLRow, attributes: string[], prefix: string) {
const result: { [ id: string ]: string | number } = {}
for (const a of attributes) {
const key = prefix
? prefix + '.' + a
: a
result[a] = row[key]
}
return result
}
}
+299
ファイルの表示
@@ -0,0 +1,299 @@
/**
*
* Class to build video attributes/join names we want to fetch from the database
*
*/
export class VideoTableAttributes {
constructor (private readonly mode: 'get' | 'list') {
}
getChannelAttributesForUser () {
return [ 'id', 'accountId' ]
}
getChannelAttributes () {
let attributeKeys = [
'id',
'name',
'description',
'accountId',
'actorId'
]
if (this.mode === 'get') {
attributeKeys = attributeKeys.concat([
'support',
'createdAt',
'updatedAt'
])
}
return attributeKeys
}
getUserAccountAttributes () {
return [ 'id', 'userId' ]
}
getAccountAttributes () {
let attributeKeys = [ 'id', 'name', 'actorId' ]
if (this.mode === 'get') {
attributeKeys = attributeKeys.concat([
'description',
'userId',
'createdAt',
'updatedAt'
])
}
return attributeKeys
}
getThumbnailAttributes () {
let attributeKeys = [ 'id', 'type', 'filename' ]
if (this.mode === 'get') {
attributeKeys = attributeKeys.concat([
'height',
'width',
'fileUrl',
'onDisk',
'automaticallyGenerated',
'videoId',
'videoPlaylistId',
'createdAt',
'updatedAt'
])
}
return attributeKeys
}
getFileAttributes () {
return [
'id',
'createdAt',
'updatedAt',
'resolution',
'size',
'extname',
'filename',
'fileUrl',
'torrentFilename',
'torrentUrl',
'infoHash',
'fps',
'metadataUrl',
'videoStreamingPlaylistId',
'videoId',
'width',
'height',
'storage'
]
}
getStreamingPlaylistAttributes () {
return [
'id',
'playlistUrl',
'playlistFilename',
'type',
'p2pMediaLoaderInfohashes',
'p2pMediaLoaderPeerVersion',
'segmentsSha256Filename',
'segmentsSha256Url',
'videoId',
'createdAt',
'updatedAt',
'storage'
]
}
getUserHistoryAttributes () {
return [ 'id', 'currentTime' ]
}
getPlaylistAttributes () {
return [
'createdAt',
'updatedAt',
'url',
'position',
'startTimestamp',
'stopTimestamp',
'videoPlaylistId'
]
}
getTagAttributes () {
return [ 'id', 'name' ]
}
getVideoTagAttributes () {
return [ 'videoId', 'tagId', 'createdAt', 'updatedAt' ]
}
getBlacklistedAttributes () {
return [ 'id', 'reason', 'unfederated' ]
}
getBlocklistAttributes () {
return [ 'id' ]
}
getScheduleUpdateAttributes () {
return [
'id',
'updateAt',
'privacy',
'videoId',
'createdAt',
'updatedAt'
]
}
getLiveAttributes () {
return [
'id',
'streamKey',
'saveReplay',
'permanentLive',
'latencyMode',
'videoId',
'replaySettingId',
'createdAt',
'updatedAt'
]
}
getVideoSourceAttributes () {
return [
'id',
'inputFilename',
'keptOriginalFilename',
'resolution',
'size',
'width',
'height',
'fps',
'metadata',
'createdAt'
]
}
getTrackerAttributes () {
return [ 'id', 'url' ]
}
getVideoTrackerAttributes () {
return [
'videoId',
'trackerId',
'createdAt',
'updatedAt'
]
}
getVideoAutoTagAttributes () {
return [ 'videoId', 'accountId', 'automaticTagId' ]
}
getAutoTagAttributes () {
return [ 'id', 'name' ]
}
getRedundancyAttributes () {
return [ 'id', 'fileUrl' ]
}
getActorAttributes () {
let attributeKeys = [
'id',
'preferredUsername',
'url',
'serverId'
]
if (this.mode === 'get') {
attributeKeys = attributeKeys.concat([
'type',
'followersCount',
'followingCount',
'inboxUrl',
'outboxUrl',
'sharedInboxUrl',
'followersUrl',
'followingUrl',
'remoteCreatedAt',
'createdAt',
'updatedAt'
])
}
return attributeKeys
}
getAvatarAttributes () {
let attributeKeys = [
'id',
'width',
'filename',
'type',
'fileUrl',
'onDisk',
'createdAt',
'updatedAt'
]
if (this.mode === 'get') {
attributeKeys = attributeKeys.concat([
'height',
'width',
'type'
])
}
return attributeKeys
}
getServerAttributes () {
return [ 'id', 'host' ]
}
getVideoAttributes () {
return [
'id',
'uuid',
'name',
'category',
'licence',
'language',
'privacy',
'nsfw',
'description',
'support',
'duration',
'views',
'likes',
'dislikes',
'remote',
'isLive',
'aspectRatio',
'url',
'commentsPolicy',
'downloadEnabled',
'waitTranscoding',
'state',
'publishedAt',
'originallyPublishedAt',
'inputFileUpdatedAt',
'channelId',
'createdAt',
'updatedAt',
'moveJobsRunning'
]
}
}
+190
ファイルの表示
@@ -0,0 +1,190 @@
import { Sequelize, Transaction } from 'sequelize'
import { pick } from '@peertube/peertube-core-utils'
import { AbstractVideoQueryBuilder } from './shared/abstract-video-query-builder.js'
import { VideoFileQueryBuilder } from './shared/video-file-query-builder.js'
import { VideoModelBuilder } from './shared/video-model-builder.js'
import { VideoTableAttributes } from './shared/video-table-attributes.js'
/**
*
* Build a GET SQL query, fetch rows and create the video model
*
*/
export type GetType =
'api' |
'full' |
'account-blacklist-files' |
'account' |
'all-files' |
'thumbnails' |
'thumbnails-blacklist' |
'id' |
'blacklist-rights'
export type BuildVideoGetQueryOptions = {
id?: number | string
url?: string
type: GetType
userId?: number
transaction?: Transaction
logging?: boolean
}
export class VideoModelGetQueryBuilder {
videoQueryBuilder: VideosModelGetQuerySubBuilder
webVideoFilesQueryBuilder: VideoFileQueryBuilder
streamingPlaylistFilesQueryBuilder: VideoFileQueryBuilder
private readonly videoModelBuilder: VideoModelBuilder
private static readonly videoFilesInclude = new Set<GetType>([ 'api', 'full', 'account-blacklist-files', 'all-files' ])
constructor (protected readonly sequelize: Sequelize) {
this.videoQueryBuilder = new VideosModelGetQuerySubBuilder(sequelize)
this.webVideoFilesQueryBuilder = new VideoFileQueryBuilder(sequelize)
this.streamingPlaylistFilesQueryBuilder = new VideoFileQueryBuilder(sequelize)
this.videoModelBuilder = new VideoModelBuilder('get', new VideoTableAttributes('get'))
}
async queryVideo (options: BuildVideoGetQueryOptions) {
const fileQueryOptions = {
...pick(options, [ 'id', 'url', 'transaction', 'logging' ]),
includeRedundancy: this.shouldIncludeRedundancies(options)
}
const [ videoRows, webVideoFilesRows, streamingPlaylistFilesRows ] = await Promise.all([
this.videoQueryBuilder.queryVideos(options),
VideoModelGetQueryBuilder.videoFilesInclude.has(options.type)
? this.webVideoFilesQueryBuilder.queryWebVideos(fileQueryOptions)
: Promise.resolve(undefined),
VideoModelGetQueryBuilder.videoFilesInclude.has(options.type)
? this.streamingPlaylistFilesQueryBuilder.queryStreamingPlaylistVideos(fileQueryOptions)
: Promise.resolve(undefined)
])
const videos = this.videoModelBuilder.buildVideosFromRows({
rows: videoRows,
rowsWebVideoFiles: webVideoFilesRows,
rowsStreamingPlaylist: streamingPlaylistFilesRows
})
if (videos.length > 1) {
throw new Error('Video results is more than 1')
}
if (videos.length === 0) return null
return videos[0]
}
private shouldIncludeRedundancies (options: BuildVideoGetQueryOptions) {
return options.type === 'api'
}
}
export class VideosModelGetQuerySubBuilder extends AbstractVideoQueryBuilder {
protected attributes: { [key: string]: string }
protected webVideoFilesQuery: string
protected streamingPlaylistFilesQuery: string
private static readonly trackersInclude = new Set<GetType>([ 'api' ])
private static readonly liveInclude = new Set<GetType>([ 'api', 'full' ])
private static readonly scheduleUpdateInclude = new Set<GetType>([ 'api', 'full' ])
private static readonly tagsInclude = new Set<GetType>([ 'api', 'full' ])
private static readonly userHistoryInclude = new Set<GetType>([ 'api', 'full' ])
private static readonly accountInclude = new Set<GetType>([ 'api', 'full', 'account', 'account-blacklist-files' ])
private static readonly ownerUserInclude = new Set<GetType>([ 'blacklist-rights' ])
private static readonly blacklistedInclude = new Set<GetType>([
'api',
'full',
'account-blacklist-files',
'thumbnails-blacklist',
'blacklist-rights'
])
private static readonly thumbnailsInclude = new Set<GetType>([
'api',
'full',
'account-blacklist-files',
'all-files',
'thumbnails',
'thumbnails-blacklist'
])
constructor (protected readonly sequelize: Sequelize) {
super(sequelize, 'get')
}
queryVideos (options: BuildVideoGetQueryOptions) {
this.buildMainGetQuery(options)
return this.runQuery(options)
}
private buildMainGetQuery (options: BuildVideoGetQueryOptions) {
this.attributes = {
'"video".*': ''
}
if (VideosModelGetQuerySubBuilder.thumbnailsInclude.has(options.type)) {
this.includeThumbnails()
}
if (VideosModelGetQuerySubBuilder.blacklistedInclude.has(options.type)) {
this.includeBlacklisted()
}
if (VideosModelGetQuerySubBuilder.accountInclude.has(options.type)) {
this.includeChannels()
this.includeAccounts()
}
if (VideosModelGetQuerySubBuilder.tagsInclude.has(options.type)) {
this.includeTags()
}
if (VideosModelGetQuerySubBuilder.scheduleUpdateInclude.has(options.type)) {
this.includeScheduleUpdate()
}
if (VideosModelGetQuerySubBuilder.liveInclude.has(options.type)) {
this.includeLive()
}
if (options.userId && VideosModelGetQuerySubBuilder.userHistoryInclude.has(options.type)) {
this.includeUserHistory(options.userId)
}
if (VideosModelGetQuerySubBuilder.ownerUserInclude.has(options.type)) {
this.includeOwnerUser()
}
if (VideosModelGetQuerySubBuilder.trackersInclude.has(options.type)) {
this.includeTrackers()
}
this.whereId(options)
this.query = this.buildQuery(options)
}
private buildQuery (options: BuildVideoGetQueryOptions) {
const order = VideosModelGetQuerySubBuilder.tagsInclude.has(options.type)
? 'ORDER BY "Tags"."name" ASC'
: ''
const from = `SELECT * FROM "video" ${this.where} LIMIT 1`
return `${this.buildSelect()} FROM (${from}) AS "video" ${this.joins} ${order}`
}
}
+780
ファイルの表示
@@ -0,0 +1,780 @@
import { Sequelize, Transaction } from 'sequelize'
import validator from 'validator'
import { forceNumber } from '@peertube/peertube-core-utils'
import { VideoInclude, VideoIncludeType, VideoPrivacy, VideoPrivacyType, VideoState } from '@peertube/peertube-models'
import { exists } from '@server/helpers/custom-validators/misc.js'
import { WEBSERVER } from '@server/initializers/constants.js'
import { buildSortDirectionAndField } from '@server/models/shared/index.js'
import { MUserAccountId, MUserId } from '@server/types/models/index.js'
import { AbstractRunQuery } from '../../../shared/abstract-run-query.js'
import { createSafeIn, parseRowCountResult } from '../../../shared/index.js'
/**
*
* Build videos list SQL query to fetch rows
*
*/
export type DisplayOnlyForFollowerOptions = {
actorId: number
orLocalVideos: boolean
}
export type BuildVideosListQueryOptions = {
attributes?: string[]
serverAccountIdForBlock: number
displayOnlyForFollower: DisplayOnlyForFollowerOptions
count: number
start: number
sort: string
nsfw?: boolean
host?: string
isLive?: boolean
isLocal?: boolean
include?: VideoIncludeType
categoryOneOf?: number[]
licenceOneOf?: number[]
languageOneOf?: string[]
tagsOneOf?: string[]
tagsAllOf?: string[]
privacyOneOf?: VideoPrivacyType[]
autoTagOneOf?: string[]
uuids?: string[]
hasFiles?: boolean
hasHLSFiles?: boolean
hasWebVideoFiles?: boolean
hasWebtorrentFiles?: boolean // TODO: Remove in v7
accountId?: number
videoChannelId?: number
videoPlaylistId?: number
trendingAlgorithm?: string // best, hot, or any other algorithm implemented
trendingDays?: number
// Used to include user history information, exclude blocked videos, include internal videos, adapt hot algorithm...
user?: MUserAccountId
// Only list videos watched by this user
historyOfUser?: MUserId
startDate?: string // ISO 8601
endDate?: string // ISO 8601
originallyPublishedStartDate?: string
originallyPublishedEndDate?: string
durationMin?: number // seconds
durationMax?: number // seconds
search?: string
isCount?: boolean
group?: string
having?: string
transaction?: Transaction
logging?: boolean
excludeAlreadyWatched?: boolean
}
export class VideosIdListQueryBuilder extends AbstractRunQuery {
protected replacements: any = {}
private attributes: string[]
private joins: string[] = []
private readonly and: string[] = []
private readonly cte: string[] = []
private group = ''
private having = ''
private sort = ''
private limit = ''
private offset = ''
constructor (protected readonly sequelize: Sequelize) {
super(sequelize)
}
queryVideoIds (options: BuildVideosListQueryOptions) {
this.buildIdsListQuery(options)
return this.runQuery()
}
countVideoIds (countOptions: BuildVideosListQueryOptions): Promise<number> {
this.buildIdsListQuery(countOptions)
return this.runQuery().then(rows => parseRowCountResult(rows))
}
getQuery (options: BuildVideosListQueryOptions) {
this.buildIdsListQuery(options)
return { query: this.query, sort: this.sort, replacements: this.replacements }
}
private buildIdsListQuery (options: BuildVideosListQueryOptions) {
this.attributes = options.attributes || [ '"video"."id"' ]
if (options.group) this.group = options.group
if (options.having) this.having = options.having
this.joins = this.joins.concat([
'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId"',
'INNER JOIN "account" ON "account"."id" = "videoChannel"."accountId"',
'INNER JOIN "actor" "accountActor" ON "account"."actorId" = "accountActor"."id"'
])
if (!(options.include & VideoInclude.BLACKLISTED)) {
this.whereNotBlacklisted()
}
if (options.serverAccountIdForBlock && !(options.include & VideoInclude.BLOCKED_OWNER)) {
this.whereNotBlocked(options.serverAccountIdForBlock, options.user)
}
// Only list published videos
if (!(options.include & VideoInclude.NOT_PUBLISHED_STATE)) {
this.whereStateAvailable()
}
if (options.videoPlaylistId) {
this.joinPlaylist(options.videoPlaylistId)
}
if (exists(options.isLocal)) {
this.whereLocal(options.isLocal)
}
if (options.host) {
this.whereHost(options.host)
}
if (options.accountId) {
this.whereAccountId(options.accountId)
}
if (options.videoChannelId) {
this.whereChannelId(options.videoChannelId)
}
if (options.displayOnlyForFollower) {
this.whereFollowerActorId(options.displayOnlyForFollower)
}
if (options.hasFiles === true) {
this.whereFileExists()
}
if (exists(options.hasWebtorrentFiles)) {
this.whereWebVideoFileExists(options.hasWebtorrentFiles)
} else if (exists(options.hasWebVideoFiles)) {
this.whereWebVideoFileExists(options.hasWebVideoFiles)
}
if (exists(options.hasHLSFiles)) {
this.whereHLSFileExists(options.hasHLSFiles)
}
if (options.tagsOneOf) {
this.whereTagsOneOf(options.tagsOneOf)
}
if (options.tagsAllOf) {
this.whereTagsAllOf(options.tagsAllOf)
}
if (options.autoTagOneOf) {
this.whereAutoTagOneOf(options.autoTagOneOf)
}
if (options.privacyOneOf) {
this.wherePrivacyOneOf(options.privacyOneOf)
} else {
// Only list videos with the appropriate privacy
this.wherePrivacyAvailable(options.user)
}
if (options.uuids) {
this.whereUUIDs(options.uuids)
}
if (options.nsfw === true) {
this.whereNSFW()
} else if (options.nsfw === false) {
this.whereSFW()
}
if (options.isLive === true) {
this.whereLive()
} else if (options.isLive === false) {
this.whereVOD()
}
if (options.categoryOneOf) {
this.whereCategoryOneOf(options.categoryOneOf)
}
if (options.licenceOneOf) {
this.whereLicenceOneOf(options.licenceOneOf)
}
if (options.languageOneOf) {
this.whereLanguageOneOf(options.languageOneOf)
}
// We don't exclude results in this so if we do a count we don't need to add this complex clause
if (options.isCount !== true) {
if (options.trendingDays) {
this.groupForTrending(options.trendingDays)
} else if ([ 'best', 'hot' ].includes(options.trendingAlgorithm)) {
this.groupForHotOrBest(options.trendingAlgorithm, options.user)
}
}
if (options.historyOfUser) {
this.joinHistory(options.historyOfUser.id)
}
if (options.startDate) {
this.whereStartDate(options.startDate)
}
if (options.endDate) {
this.whereEndDate(options.endDate)
}
if (options.originallyPublishedStartDate) {
this.whereOriginallyPublishedStartDate(options.originallyPublishedStartDate)
}
if (options.originallyPublishedEndDate) {
this.whereOriginallyPublishedEndDate(options.originallyPublishedEndDate)
}
if (options.durationMin) {
this.whereDurationMin(options.durationMin)
}
if (options.durationMax) {
this.whereDurationMax(options.durationMax)
}
if (options.excludeAlreadyWatched) {
if (exists(options.user.id)) {
this.whereExcludeAlreadyWatched(options.user.id)
} else {
throw new Error('Cannot use excludeAlreadyWatched parameter when auth token is not provided')
}
}
this.whereSearch(options.search)
if (options.isCount === true) {
this.setCountAttribute()
} else {
if (exists(options.sort)) {
this.setSort(options.sort)
}
if (exists(options.count)) {
this.setLimit(options.count)
}
if (exists(options.start)) {
this.setOffset(options.start)
}
}
const cteString = this.cte.length !== 0
? `WITH ${this.cte.join(', ')} `
: ''
this.query = cteString +
'SELECT ' + this.attributes.join(', ') + ' ' +
'FROM "video" ' + this.joins.join(' ') + ' ' +
'WHERE ' + this.and.join(' AND ') + ' ' +
this.group + ' ' +
this.having + ' ' +
this.sort + ' ' +
this.limit + ' ' +
this.offset
}
private setCountAttribute () {
this.attributes = [ 'COUNT(*) as "total"' ]
}
private joinHistory (userId: number) {
this.joins.push('INNER JOIN "userVideoHistory" ON "video"."id" = "userVideoHistory"."videoId"')
this.and.push('"userVideoHistory"."userId" = :historyOfUser')
this.replacements.historyOfUser = userId
}
private joinPlaylist (playlistId: number) {
this.joins.push(
'INNER JOIN "videoPlaylistElement" "video"."id" = "videoPlaylistElement"."videoId" ' +
'AND "videoPlaylistElement"."videoPlaylistId" = :videoPlaylistId'
)
this.replacements.videoPlaylistId = playlistId
}
private whereStateAvailable () {
this.and.push(
`("video"."state" = ${VideoState.PUBLISHED} OR ` +
`("video"."state" = ${VideoState.TO_TRANSCODE} AND "video"."waitTranscoding" IS false))`
)
}
private wherePrivacyAvailable (user?: MUserAccountId) {
if (user) {
this.and.push(
`("video"."privacy" = ${VideoPrivacy.PUBLIC} OR "video"."privacy" = ${VideoPrivacy.INTERNAL})`
)
} else { // Or only public videos
this.and.push(
`"video"."privacy" = ${VideoPrivacy.PUBLIC}`
)
}
}
private whereLocal (isLocal: boolean) {
const isRemote = isLocal ? 'FALSE' : 'TRUE'
this.and.push('"video"."remote" IS ' + isRemote)
}
private whereHost (host: string) {
// Local instance
if (host === WEBSERVER.HOST) {
this.and.push('"accountActor"."serverId" IS NULL')
return
}
this.joins.push('INNER JOIN "server" ON "server"."id" = "accountActor"."serverId"')
this.and.push('"server"."host" = :host')
this.replacements.host = host
}
private whereAccountId (accountId: number) {
this.and.push('"account"."id" = :accountId')
this.replacements.accountId = accountId
}
private whereChannelId (channelId: number) {
this.and.push('"videoChannel"."id" = :videoChannelId')
this.replacements.videoChannelId = channelId
}
private whereFollowerActorId (options: { actorId: number, orLocalVideos: boolean }) {
let query =
'(' +
' EXISTS (' + // Videos shared by actors we follow
' SELECT 1 FROM "videoShare" ' +
' INNER JOIN "actorFollow" "actorFollowShare" ON "actorFollowShare"."targetActorId" = "videoShare"."actorId" ' +
' AND "actorFollowShare"."actorId" = :followerActorId AND "actorFollowShare"."state" = \'accepted\' ' +
' WHERE "videoShare"."videoId" = "video"."id"' +
' )' +
' OR' +
' EXISTS (' + // Videos published by channels or accounts we follow
' SELECT 1 from "actorFollow" ' +
' WHERE ("actorFollow"."targetActorId" = "account"."actorId" OR "actorFollow"."targetActorId" = "videoChannel"."actorId") ' +
' AND "actorFollow"."actorId" = :followerActorId ' +
' AND "actorFollow"."state" = \'accepted\'' +
' )'
if (options.orLocalVideos) {
query += ' OR "video"."remote" IS FALSE'
}
query += ')'
this.and.push(query)
this.replacements.followerActorId = options.actorId
}
private whereFileExists () {
this.and.push(`(${this.buildWebVideoFileExistsQuery(true)} OR ${this.buildHLSFileExistsQuery(true)})`)
}
private whereWebVideoFileExists (exists: boolean) {
this.and.push(this.buildWebVideoFileExistsQuery(exists))
}
private whereHLSFileExists (exists: boolean) {
this.and.push(this.buildHLSFileExistsQuery(exists))
}
private buildWebVideoFileExistsQuery (exists: boolean) {
const prefix = exists ? '' : 'NOT '
return prefix + 'EXISTS (SELECT 1 FROM "videoFile" WHERE "videoFile"."videoId" = "video"."id")'
}
private buildHLSFileExistsQuery (exists: boolean) {
const prefix = exists ? '' : 'NOT '
return prefix + 'EXISTS (' +
' SELECT 1 FROM "videoStreamingPlaylist" ' +
' INNER JOIN "videoFile" ON "videoFile"."videoStreamingPlaylistId" = "videoStreamingPlaylist"."id" ' +
' WHERE "videoStreamingPlaylist"."videoId" = "video"."id"' +
')'
}
private whereTagsOneOf (tagsOneOf: string[]) {
const tagsOneOfLower = tagsOneOf.map(t => t.toLowerCase())
this.cte.push(
'"tagsOneOf" AS (' +
' SELECT "videoTag"."videoId" AS "videoId" FROM "videoTag" ' +
' INNER JOIN "tag" ON "tag"."id" = "videoTag"."tagId" ' +
' WHERE lower("tag"."name") IN (' + createSafeIn(this.sequelize, tagsOneOfLower) + ') ' +
')'
)
this.joins.push('INNER JOIN "tagsOneOf" ON "video"."id" = "tagsOneOf"."videoId"')
}
private whereAutoTagOneOf (autoTagOneOf: string[]) {
const tags = autoTagOneOf.map(t => t.toLowerCase())
this.cte.push(
'"autoTagsOneOf" AS (' +
' SELECT "videoAutomaticTag"."videoId" AS "videoId" FROM "videoAutomaticTag" ' +
' INNER JOIN "automaticTag" ON "automaticTag"."id" = "videoAutomaticTag"."automaticTagId" ' +
' WHERE lower("automaticTag"."name") IN (' + createSafeIn(this.sequelize, tags) + ') ' +
')'
)
this.joins.push('INNER JOIN "autoTagsOneOf" ON "video"."id" = "autoTagsOneOf"."videoId"')
}
private whereTagsAllOf (tagsAllOf: string[]) {
const tagsAllOfLower = tagsAllOf.map(t => t.toLowerCase())
this.cte.push(
'"tagsAllOf" AS (' +
' SELECT "videoTag"."videoId" AS "videoId" FROM "videoTag" ' +
' INNER JOIN "tag" ON "tag"."id" = "videoTag"."tagId" ' +
' WHERE lower("tag"."name") IN (' + createSafeIn(this.sequelize, tagsAllOfLower) + ') ' +
' GROUP BY "videoTag"."videoId" HAVING COUNT(*) = ' + tagsAllOfLower.length +
')'
)
this.joins.push('INNER JOIN "tagsAllOf" ON "video"."id" = "tagsAllOf"."videoId"')
}
private wherePrivacyOneOf (privacyOneOf: VideoPrivacyType[]) {
this.and.push('"video"."privacy" IN (:privacyOneOf)')
this.replacements.privacyOneOf = privacyOneOf
}
private whereUUIDs (uuids: string[]) {
this.and.push('"video"."uuid" IN (' + createSafeIn(this.sequelize, uuids) + ')')
}
private whereCategoryOneOf (categoryOneOf: number[]) {
this.and.push('"video"."category" IN (:categoryOneOf)')
this.replacements.categoryOneOf = categoryOneOf
}
private whereLicenceOneOf (licenceOneOf: number[]) {
this.and.push('"video"."licence" IN (:licenceOneOf)')
this.replacements.licenceOneOf = licenceOneOf
}
private whereLanguageOneOf (languageOneOf: string[]) {
const languages = languageOneOf.filter(l => l && l !== '_unknown')
const languagesQueryParts: string[] = []
if (languages.length !== 0) {
languagesQueryParts.push('"video"."language" IN (:languageOneOf)')
this.replacements.languageOneOf = languages
languagesQueryParts.push(
'EXISTS (' +
' SELECT 1 FROM "videoCaption" WHERE "videoCaption"."language" ' +
' IN (' + createSafeIn(this.sequelize, languages) + ') AND ' +
' "videoCaption"."videoId" = "video"."id"' +
')'
)
}
if (languageOneOf.includes('_unknown')) {
languagesQueryParts.push('"video"."language" IS NULL')
}
if (languagesQueryParts.length !== 0) {
this.and.push('(' + languagesQueryParts.join(' OR ') + ')')
}
}
private whereNSFW () {
this.and.push('"video"."nsfw" IS TRUE')
}
private whereSFW () {
this.and.push('"video"."nsfw" IS FALSE')
}
private whereLive () {
this.and.push('"video"."isLive" IS TRUE')
}
private whereVOD () {
this.and.push('"video"."isLive" IS FALSE')
}
private whereNotBlocked (serverAccountId: number, user?: MUserAccountId) {
const blockerIds = [ serverAccountId ]
if (user) blockerIds.push(user.Account.id)
const inClause = createSafeIn(this.sequelize, blockerIds)
this.and.push(
'NOT EXISTS (' +
' SELECT 1 FROM "accountBlocklist" ' +
' WHERE "accountBlocklist"."accountId" IN (' + inClause + ') ' +
' AND "accountBlocklist"."targetAccountId" = "account"."id" ' +
')' +
'AND NOT EXISTS (' +
' SELECT 1 FROM "serverBlocklist" WHERE "serverBlocklist"."accountId" IN (' + inClause + ') ' +
' AND "serverBlocklist"."targetServerId" = "accountActor"."serverId"' +
')'
)
}
private whereSearch (search?: string) {
if (!search) {
this.attributes.push('0 as similarity')
return
}
const escapedSearch = this.sequelize.escape(search)
const escapedLikeSearch = this.sequelize.escape('%' + search + '%')
this.cte.push(
'"trigramSearch" AS (' +
' SELECT "video"."id", ' +
` similarity(lower(immutable_unaccent("video"."name")), lower(immutable_unaccent(${escapedSearch}))) as similarity ` +
' FROM "video" ' +
' WHERE lower(immutable_unaccent("video"."name")) % lower(immutable_unaccent(' + escapedSearch + ')) OR ' +
' lower(immutable_unaccent("video"."name")) LIKE lower(immutable_unaccent(' + escapedLikeSearch + '))' +
')'
)
this.joins.push('LEFT JOIN "trigramSearch" ON "video"."id" = "trigramSearch"."id"')
let base = '(' +
' "trigramSearch"."id" IS NOT NULL OR ' +
' EXISTS (' +
' SELECT 1 FROM "videoTag" ' +
' INNER JOIN "tag" ON "tag"."id" = "videoTag"."tagId" ' +
` WHERE lower("tag"."name") = lower(${escapedSearch}) ` +
' AND "video"."id" = "videoTag"."videoId"' +
' )'
if (validator.default.isUUID(search)) {
base += ` OR "video"."uuid" = ${escapedSearch}`
}
base += ')'
this.and.push(base)
this.attributes.push(`COALESCE("trigramSearch"."similarity", 0) as similarity`)
}
private whereNotBlacklisted () {
this.and.push('"video"."id" NOT IN (SELECT "videoBlacklist"."videoId" FROM "videoBlacklist")')
}
private whereStartDate (startDate: string) {
this.and.push('"video"."publishedAt" >= :startDate')
this.replacements.startDate = startDate
}
private whereEndDate (endDate: string) {
this.and.push('"video"."publishedAt" <= :endDate')
this.replacements.endDate = endDate
}
private whereOriginallyPublishedStartDate (startDate: string) {
this.and.push('"video"."originallyPublishedAt" >= :originallyPublishedStartDate')
this.replacements.originallyPublishedStartDate = startDate
}
private whereOriginallyPublishedEndDate (endDate: string) {
this.and.push('"video"."originallyPublishedAt" <= :originallyPublishedEndDate')
this.replacements.originallyPublishedEndDate = endDate
}
private whereDurationMin (durationMin: number) {
this.and.push('"video"."duration" >= :durationMin')
this.replacements.durationMin = durationMin
}
private whereDurationMax (durationMax: number) {
this.and.push('"video"."duration" <= :durationMax')
this.replacements.durationMax = durationMax
}
private whereExcludeAlreadyWatched (userId: number) {
this.and.push(
'NOT EXISTS (' +
' SELECT 1' +
' FROM "userVideoHistory"' +
' WHERE "video"."id" = "userVideoHistory"."videoId"' +
' AND "userVideoHistory"."userId" = :excludeAlreadyWatchedUserId' +
')'
)
this.replacements.excludeAlreadyWatchedUserId = userId
}
private groupForTrending (trendingDays: number) {
const viewsGteDate = new Date(new Date().getTime() - (24 * 3600 * 1000) * trendingDays)
this.joins.push('LEFT JOIN "videoView" ON "video"."id" = "videoView"."videoId" AND "videoView"."startDate" >= :viewsGteDate')
this.replacements.viewsGteDate = viewsGteDate
this.attributes.push('COALESCE(SUM("videoView"."views"), 0) AS "score"')
this.group = 'GROUP BY "video"."id"'
}
private groupForHotOrBest (trendingAlgorithm: string, user?: MUserAccountId) {
/**
* "Hotness" is a measure based on absolute view/comment/like/dislike numbers,
* with fixed weights only applied to their log values.
*
* This algorithm gives little chance for an old video to have a good score,
* for which recent spikes in interactions could be a sign of "hotness" and
* justify a better score. However there are multiple ways to achieve that
* goal, which is left for later. Yes, this is a TODO :)
*
* notes:
* - weights and base score are in number of half-days.
* - all comments are counted, regardless of being written by the video author or not
* see https://github.com/reddit-archive/reddit/blob/master/r2/r2/lib/db/_sorts.pyx#L47-L58
* - we have less interactions than on reddit, so multiply weights by an arbitrary factor
*/
const weights = {
like: 3 * 50,
dislike: -3 * 50,
view: Math.floor((1 / 3) * 50),
comment: 2 * 50, // a comment takes more time than a like to do, but can be done multiple times
history: -2 * 50
}
this.joins.push('LEFT JOIN "videoComment" ON "video"."id" = "videoComment"."videoId"')
let attribute =
`LOG(GREATEST(1, "video"."likes" - 1)) * ${weights.like} ` + // likes (+)
`+ LOG(GREATEST(1, "video"."dislikes" - 1)) * ${weights.dislike} ` + // dislikes (-)
`+ LOG("video"."views" + 1) * ${weights.view} ` + // views (+)
`+ LOG(GREATEST(1, COUNT(DISTINCT "videoComment"."id"))) * ${weights.comment} ` + // comments (+)
'+ (SELECT (EXTRACT(epoch FROM "video"."publishedAt") - 1446156582) / 47000) ' // base score (in number of half-days)
if (trendingAlgorithm === 'best' && user) {
this.joins.push(
'LEFT JOIN "userVideoHistory" ON "video"."id" = "userVideoHistory"."videoId" AND "userVideoHistory"."userId" = :bestUser'
)
this.replacements.bestUser = user.id
attribute += `+ POWER(COUNT(DISTINCT "userVideoHistory"."id"), 2.0) * ${weights.history} `
}
attribute += 'AS "score"'
this.attributes.push(attribute)
this.group = 'GROUP BY "video"."id"'
}
private setSort (sort: string) {
if (sort === '-originallyPublishedAt' || sort === 'originallyPublishedAt') {
this.attributes.push('COALESCE("video"."originallyPublishedAt", "video"."publishedAt") AS "publishedAtForOrder"')
}
if (sort === '-localVideoFilesSize' || sort === 'localVideoFilesSize') {
this.attributes.push(
'(' +
'CASE ' +
'WHEN "video"."remote" IS TRUE THEN 0 ' + // Consider remote videos with size of 0
'ELSE (' +
'(SELECT COALESCE(SUM(size), 0) FROM "videoFile" WHERE "videoFile"."videoId" = "video"."id")' +
' + ' +
'(' +
'SELECT COALESCE(SUM(size), 0) FROM "videoFile" ' +
'INNER JOIN "videoStreamingPlaylist" ON "videoStreamingPlaylist"."id" = "videoFile"."videoStreamingPlaylistId" ' +
'AND "videoStreamingPlaylist"."videoId" = "video"."id"' +
')' +
' + ' +
'(' +
'SELECT COALESCE(SUM(size), 0) FROM "videoSource" ' +
'WHERE "videoSource"."videoId" = "video"."id" AND "videoSource"."storage" IS NOT NULL' +
')' +
') END' +
') AS "localVideoFilesSize"'
)
}
this.sort = this.buildOrder(sort)
}
private buildOrder (value: string) {
const { direction, field } = buildSortDirectionAndField(value)
if (field.match(/^[a-zA-Z."]+$/) === null) throw new Error('Invalid sort column ' + field)
if (field.toLowerCase() === 'random') return 'ORDER BY RANDOM()'
if ([ 'trending', 'hot', 'best' ].includes(field.toLowerCase())) { // Sort by aggregation
return `ORDER BY "score" ${direction}, "video"."views" ${direction}`
}
let firstSort: string
if (field.toLowerCase() === 'match') { // Search
firstSort = '"similarity"'
} else if (field === 'originallyPublishedAt') {
firstSort = '"publishedAtForOrder"'
} else if (field === 'localVideoFilesSize') {
firstSort = '"localVideoFilesSize"'
} else if (field.includes('.')) {
firstSort = field
} else {
firstSort = `"video"."${field}"`
}
return `ORDER BY ${firstSort} ${direction}, "video"."id" ASC`
}
private setLimit (countArg: number) {
const count = forceNumber(countArg)
this.limit = `LIMIT ${count}`
}
private setOffset (startArg: number) {
const start = forceNumber(startArg)
this.offset = `OFFSET ${start}`
}
}
+115
ファイルの表示
@@ -0,0 +1,115 @@
import { Sequelize } from 'sequelize'
import { pick } from '@peertube/peertube-core-utils'
import { VideoInclude } from '@peertube/peertube-models'
import { AbstractVideoQueryBuilder } from './shared/abstract-video-query-builder.js'
import { VideoFileQueryBuilder } from './shared/video-file-query-builder.js'
import { VideoModelBuilder } from './shared/video-model-builder.js'
import { BuildVideosListQueryOptions, VideosIdListQueryBuilder } from './videos-id-list-query-builder.js'
import { getServerActor } from '@server/models/application/application.js'
import { MActorAccount } from '@server/types/models/index.js'
/**
*
* Build videos list SQL query and create video models
*
*/
export class VideosModelListQueryBuilder extends AbstractVideoQueryBuilder {
protected attributes: { [key: string]: string }
private innerQuery: string
private innerSort: string
webVideoFilesQueryBuilder: VideoFileQueryBuilder
streamingPlaylistFilesQueryBuilder: VideoFileQueryBuilder
private readonly videoModelBuilder: VideoModelBuilder
constructor (protected readonly sequelize: Sequelize) {
super(sequelize, 'list')
this.videoModelBuilder = new VideoModelBuilder(this.mode, this.tables)
this.webVideoFilesQueryBuilder = new VideoFileQueryBuilder(sequelize)
this.streamingPlaylistFilesQueryBuilder = new VideoFileQueryBuilder(sequelize)
}
async queryVideos (options: BuildVideosListQueryOptions) {
const serverActor = await getServerActor()
this.buildInnerQuery(options)
this.buildMainQuery(options, serverActor)
const rows = await this.runQuery()
if (options.include & VideoInclude.FILES) {
const videoIds = Array.from(new Set(rows.map(r => r.id)))
if (videoIds.length !== 0) {
const fileQueryOptions = {
...pick(options, [ 'transaction', 'logging' ]),
ids: videoIds,
includeRedundancy: false
}
const [ rowsWebVideoFiles, rowsStreamingPlaylist ] = await Promise.all([
this.webVideoFilesQueryBuilder.queryWebVideos(fileQueryOptions),
this.streamingPlaylistFilesQueryBuilder.queryStreamingPlaylistVideos(fileQueryOptions)
])
return this.videoModelBuilder.buildVideosFromRows({ rows, include: options.include, rowsStreamingPlaylist, rowsWebVideoFiles })
}
}
return this.videoModelBuilder.buildVideosFromRows({ rows, include: options.include })
}
private buildInnerQuery (options: BuildVideosListQueryOptions) {
const idsQueryBuilder = new VideosIdListQueryBuilder(this.sequelize)
const { query, sort, replacements } = idsQueryBuilder.getQuery(options)
this.replacements = replacements
this.innerQuery = query
this.innerSort = sort
}
private buildMainQuery (options: BuildVideosListQueryOptions, serverActor: MActorAccount) {
this.attributes = {
'"video".*': ''
}
this.addJoin('INNER JOIN "video" ON "tmp"."id" = "video"."id"')
this.includeChannels()
this.includeAccounts()
this.includeThumbnails()
if (options.user) {
this.includeUserHistory(options.user.id)
}
if (options.videoPlaylistId) {
this.includePlaylist(options.videoPlaylistId)
}
if (options.include & VideoInclude.BLACKLISTED) {
this.includeBlacklisted()
}
if (options.include & VideoInclude.BLOCKED_OWNER) {
this.includeBlockedOwnerAndServer(options.serverAccountIdForBlock, options.user)
}
if (options.include & VideoInclude.SOURCE) {
this.includeVideoSource()
}
if (options.include & VideoInclude.AUTOMATIC_TAGS) {
this.includeAutomaticTags(serverActor.Account.id)
}
const select = this.buildSelect()
this.query = `${select} FROM (${this.innerQuery}) AS "tmp" ${this.joins} ${this.innerSort}`
}
}
+169
ファイルの表示
@@ -0,0 +1,169 @@
import { remove } from 'fs-extra/esm'
import { join } from 'path'
import { AfterDestroy, AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Table, UpdatedAt } from 'sequelize-typescript'
import { CONFIG } from '@server/initializers/config.js'
import { MStoryboard, MStoryboardVideo, MVideo } from '@server/types/models/index.js'
import { Storyboard } from '@peertube/peertube-models'
import { logger } from '../../helpers/logger.js'
import { CONSTRAINTS_FIELDS, LAZY_STATIC_PATHS, WEBSERVER } from '../../initializers/constants.js'
import { VideoModel } from './video.js'
import { Transaction } from 'sequelize'
import { SequelizeModel } from '../shared/index.js'
@Table({
tableName: 'storyboard',
indexes: [
{
fields: [ 'videoId' ],
unique: true
},
{
fields: [ 'filename' ],
unique: true
}
]
})
export class StoryboardModel extends SequelizeModel<StoryboardModel> {
@AllowNull(false)
@Column
filename: string
@AllowNull(false)
@Column
totalHeight: number
@AllowNull(false)
@Column
totalWidth: number
@AllowNull(false)
@Column
spriteHeight: number
@AllowNull(false)
@Column
spriteWidth: number
@AllowNull(false)
@Column
spriteDuration: number
@AllowNull(true)
@Column(DataType.STRING(CONSTRAINTS_FIELDS.COMMONS.URL.max))
fileUrl: string
@ForeignKey(() => VideoModel)
@Column
videoId: number
@BelongsTo(() => VideoModel, {
foreignKey: {
allowNull: false
},
onDelete: 'CASCADE'
})
Video: Awaited<VideoModel>
@CreatedAt
createdAt: Date
@UpdatedAt
updatedAt: Date
@AfterDestroy
static removeInstanceFile (instance: StoryboardModel) {
logger.info('Removing storyboard file %s.', instance.filename)
// Don't block the transaction
instance.removeFile()
.catch(err => logger.error('Cannot remove storyboard file %s.', instance.filename, { err }))
}
static loadByVideo (videoId: number, transaction?: Transaction): Promise<MStoryboard> {
const query = {
where: {
videoId
},
transaction
}
return StoryboardModel.findOne(query)
}
static loadByFilename (filename: string): Promise<MStoryboard> {
const query = {
where: {
filename
}
}
return StoryboardModel.findOne(query)
}
static loadWithVideoByFilename (filename: string): Promise<MStoryboardVideo> {
const query = {
where: {
filename
},
include: [
{
model: VideoModel.unscoped(),
required: true
}
]
}
return StoryboardModel.findOne(query)
}
// ---------------------------------------------------------------------------
static async listStoryboardsOf (video: MVideo): Promise<MStoryboardVideo[]> {
const query = {
where: {
videoId: video.id
}
}
const storyboards = await StoryboardModel.findAll<MStoryboard>(query)
return storyboards.map(s => Object.assign(s, { Video: video }))
}
// ---------------------------------------------------------------------------
getOriginFileUrl (video: MVideo) {
if (video.isOwned()) {
return WEBSERVER.URL + this.getLocalStaticPath()
}
return this.fileUrl
}
getLocalStaticPath () {
return LAZY_STATIC_PATHS.STORYBOARDS + this.filename
}
getPath () {
return join(CONFIG.STORAGE.STORYBOARDS_DIR, this.filename)
}
removeFile () {
return remove(this.getPath())
}
toFormattedJSON (this: MStoryboardVideo): Storyboard {
return {
storyboardPath: this.getLocalStaticPath(),
totalHeight: this.totalHeight,
totalWidth: this.totalWidth,
spriteWidth: this.spriteWidth,
spriteHeight: this.spriteHeight,
spriteDuration: this.spriteDuration
}
}
}
+84
ファイルの表示
@@ -0,0 +1,84 @@
import { VideoPrivacy, VideoState } from '@peertube/peertube-models'
import { MTag } from '@server/types/models/video/tag.js'
import { QueryTypes, Transaction, col, fn } from 'sequelize'
import { AllowNull, BelongsToMany, Column, Is, Table } from 'sequelize-typescript'
import { isVideoTagValid } from '../../helpers/custom-validators/videos.js'
import { SequelizeModel, throwIfNotValid } from '../shared/index.js'
import { VideoTagModel } from './video-tag.js'
import { VideoModel } from './video.js'
@Table({
tableName: 'tag',
timestamps: false,
indexes: [
{
fields: [ 'name' ],
unique: true
},
{
name: 'tag_lower_name',
fields: [ fn('lower', col('name')) ]
}
]
})
export class TagModel extends SequelizeModel<TagModel> {
@AllowNull(false)
@Is('VideoTag', value => throwIfNotValid(value, isVideoTagValid, 'tag'))
@Column
name: string
@BelongsToMany(() => VideoModel, {
foreignKey: 'tagId',
through: () => VideoTagModel,
onDelete: 'CASCADE'
})
Videos: Awaited<VideoModel>[]
// threshold corresponds to how many video the field should have to be returned
static getRandomSamples (threshold: number, count: number): Promise<string[]> {
const query = 'SELECT tag.name FROM tag ' +
'INNER JOIN "videoTag" ON "videoTag"."tagId" = tag.id ' +
'INNER JOIN video ON video.id = "videoTag"."videoId" ' +
'WHERE video.privacy = $videoPrivacy AND video.state = $videoState ' +
'GROUP BY tag.name HAVING COUNT(tag.name) >= $threshold ' +
'ORDER BY random() ' +
'LIMIT $count'
const options = {
bind: { threshold, count, videoPrivacy: VideoPrivacy.PUBLIC, videoState: VideoState.PUBLISHED },
type: QueryTypes.SELECT as QueryTypes.SELECT
}
return TagModel.sequelize.query<{ name: string }>(query, options)
.then(data => data.map(d => d.name))
}
static findOrCreateMultiple (options: {
tags: string[]
transaction?: Transaction
}): Promise<MTag[]> {
const { tags, transaction } = options
if (tags === null) return Promise.resolve([])
const uniqueTags = new Set(tags)
const tasks = Array.from(uniqueTags).map(tag => {
const query = {
where: {
name: tag
},
defaults: {
name: tag
},
transaction
}
return this.findOrCreate(query)
.then(([ tagInstance ]) => tagInstance)
})
return Promise.all(tasks)
}
}
+240
ファイルの表示
@@ -0,0 +1,240 @@
import { ActivityIconObject, ThumbnailType, type ThumbnailType_Type } from '@peertube/peertube-models'
import { afterCommitIfTransaction } from '@server/helpers/database-utils.js'
import { MThumbnail, MThumbnailVideo, MVideo, MVideoPlaylist } from '@server/types/models/index.js'
import { remove } from 'fs-extra/esm'
import { join } from 'path'
import {
AfterDestroy,
AllowNull,
BeforeCreate,
BeforeUpdate,
BelongsTo,
Column,
CreatedAt,
DataType,
Default,
ForeignKey, Table,
UpdatedAt
} from 'sequelize-typescript'
import { logger } from '../../helpers/logger.js'
import { CONFIG } from '../../initializers/config.js'
import { CONSTRAINTS_FIELDS, LAZY_STATIC_PATHS, WEBSERVER } from '../../initializers/constants.js'
import { VideoPlaylistModel } from './video-playlist.js'
import { VideoModel } from './video.js'
import { SequelizeModel } from '../shared/sequelize-type.js'
@Table({
tableName: 'thumbnail',
indexes: [
{
fields: [ 'videoId' ]
},
{
fields: [ 'videoPlaylistId' ],
unique: true
},
{
fields: [ 'filename', 'type' ],
unique: true
}
]
})
export class ThumbnailModel extends SequelizeModel<ThumbnailModel> {
@AllowNull(false)
@Column
filename: string
@AllowNull(true)
@Default(null)
@Column
height: number
@AllowNull(true)
@Default(null)
@Column
width: number
@AllowNull(false)
@Column
type: ThumbnailType_Type
@AllowNull(true)
@Column(DataType.STRING(CONSTRAINTS_FIELDS.COMMONS.URL.max))
fileUrl: string
@AllowNull(true)
@Column
automaticallyGenerated: boolean
@AllowNull(false)
@Column
onDisk: boolean
@ForeignKey(() => VideoModel)
@Column
videoId: number
@BelongsTo(() => VideoModel, {
foreignKey: {
allowNull: true
},
onDelete: 'CASCADE'
})
Video: Awaited<VideoModel>
@ForeignKey(() => VideoPlaylistModel)
@Column
videoPlaylistId: number
@BelongsTo(() => VideoPlaylistModel, {
foreignKey: {
allowNull: true
},
onDelete: 'CASCADE'
})
VideoPlaylist: Awaited<VideoPlaylistModel>
@CreatedAt
createdAt: Date
@UpdatedAt
updatedAt: Date
// If this thumbnail replaced existing one, track the old name
previousThumbnailFilename: string
private static readonly types: { [ id in ThumbnailType_Type ]: { label: string, directory: string, staticPath: string } } = {
[ThumbnailType.MINIATURE]: {
label: 'miniature',
directory: CONFIG.STORAGE.THUMBNAILS_DIR,
staticPath: LAZY_STATIC_PATHS.THUMBNAILS
},
[ThumbnailType.PREVIEW]: {
label: 'preview',
directory: CONFIG.STORAGE.PREVIEWS_DIR,
staticPath: LAZY_STATIC_PATHS.PREVIEWS
}
}
@BeforeCreate
@BeforeUpdate
static removeOldFile (instance: ThumbnailModel, options) {
return afterCommitIfTransaction(options.transaction, () => instance.removePreviousFilenameIfNeeded())
}
@AfterDestroy
static removeFiles (instance: ThumbnailModel) {
logger.info('Removing %s file %s.', ThumbnailModel.types[instance.type].label, instance.filename)
// Don't block the transaction
instance.removeThumbnail()
.catch(err => logger.error('Cannot remove thumbnail file %s.', instance.filename, { err }))
}
static loadByFilename (filename: string, thumbnailType: ThumbnailType_Type): Promise<MThumbnail> {
const query = {
where: {
filename,
type: thumbnailType
}
}
return ThumbnailModel.findOne(query)
}
static loadWithVideoByFilename (filename: string, thumbnailType: ThumbnailType_Type): Promise<MThumbnailVideo> {
const query = {
where: {
filename,
type: thumbnailType
},
include: [
{
model: VideoModel.unscoped(),
required: true
}
]
}
return ThumbnailModel.findOne(query)
}
static listRemoteOnDisk () {
return this.findAll<MThumbnail>({
where: {
onDisk: true
},
include: [
{
attributes: [ 'id' ],
model: VideoModel.unscoped(),
required: true,
where: {
remote: true
}
}
]
})
}
// ---------------------------------------------------------------------------
static buildPath (type: ThumbnailType_Type, filename: string) {
const directory = ThumbnailModel.types[type].directory
return join(directory, filename)
}
// ---------------------------------------------------------------------------
getOriginFileUrl (videoOrPlaylist: MVideo | MVideoPlaylist) {
const staticPath = ThumbnailModel.types[this.type].staticPath + this.filename
if (videoOrPlaylist.isOwned()) return WEBSERVER.URL + staticPath
return this.fileUrl
}
getLocalStaticPath () {
return ThumbnailModel.types[this.type].staticPath + this.filename
}
getPath () {
return ThumbnailModel.buildPath(this.type, this.filename)
}
getPreviousPath () {
return ThumbnailModel.buildPath(this.type, this.previousThumbnailFilename)
}
removeThumbnail () {
return remove(this.getPath())
}
removePreviousFilenameIfNeeded () {
if (!this.previousThumbnailFilename) return
const previousPath = this.getPreviousPath()
remove(previousPath)
.catch(err => logger.error('Cannot remove previous thumbnail file %s.', previousPath, { err }))
this.previousThumbnailFilename = undefined
}
isOwned () {
return !this.fileUrl
}
// ---------------------------------------------------------------------------
toActivityPubObject (this: MThumbnail, video: MVideo): ActivityIconObject {
return {
type: 'Image',
url: this.getOriginFileUrl(video),
mediaType: 'image/jpeg',
width: this.width,
height: this.height
}
}
}
+133
ファイルの表示
@@ -0,0 +1,133 @@
import { VideoBlacklist, type VideoBlacklistType_Type } from '@peertube/peertube-models'
import { MVideoBlacklist, MVideoBlacklistFormattable } from '@server/types/models/index.js'
import { FindOptions } from 'sequelize'
import { AllowNull, BelongsTo, Column, CreatedAt, DataType, Default, ForeignKey, Is, Table, UpdatedAt } from 'sequelize-typescript'
import { isVideoBlacklistReasonValid, isVideoBlacklistTypeValid } from '../../helpers/custom-validators/video-blacklist.js'
import { CONSTRAINTS_FIELDS } from '../../initializers/constants.js'
import { SequelizeModel, getBlacklistSort, searchAttribute, throwIfNotValid } from '../shared/index.js'
import { ThumbnailModel } from './thumbnail.js'
import { SummaryOptions, VideoChannelModel, ScopeNames as VideoChannelScopeNames } from './video-channel.js'
import { VideoModel } from './video.js'
@Table({
tableName: 'videoBlacklist',
indexes: [
{
fields: [ 'videoId' ],
unique: true
}
]
})
export class VideoBlacklistModel extends SequelizeModel<VideoBlacklistModel> {
@AllowNull(true)
@Is('VideoBlacklistReason', value => throwIfNotValid(value, isVideoBlacklistReasonValid, 'reason', true))
@Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_BLACKLIST.REASON.max))
reason: string
@AllowNull(false)
@Column
unfederated: boolean
@AllowNull(false)
@Default(null)
@Is('VideoBlacklistType', value => throwIfNotValid(value, isVideoBlacklistTypeValid, 'type'))
@Column
type: VideoBlacklistType_Type
@CreatedAt
createdAt: Date
@UpdatedAt
updatedAt: Date
@ForeignKey(() => VideoModel)
@Column
videoId: number
@BelongsTo(() => VideoModel, {
foreignKey: {
allowNull: false
},
onDelete: 'cascade'
})
Video: Awaited<VideoModel>
static listForApi (parameters: {
start: number
count: number
sort: string
search?: string
type?: VideoBlacklistType_Type
}) {
const { start, count, sort, search, type } = parameters
function buildBaseQuery (): FindOptions {
return {
offset: start,
limit: count,
order: getBlacklistSort(sort)
}
}
const countQuery = buildBaseQuery()
const findQuery = buildBaseQuery()
findQuery.include = [
{
model: VideoModel,
required: true,
where: searchAttribute(search, 'name'),
include: [
{
model: VideoChannelModel.scope({ method: [ VideoChannelScopeNames.SUMMARY, { withAccount: true } as SummaryOptions ] }),
required: true
},
{
model: ThumbnailModel,
attributes: [ 'type', 'filename' ],
required: false
}
]
}
]
if (type) {
countQuery.where = { type }
findQuery.where = { type }
}
return Promise.all([
VideoBlacklistModel.count(countQuery),
VideoBlacklistModel.findAll(findQuery)
]).then(([ count, rows ]) => {
return {
data: rows,
total: count
}
})
}
static loadByVideoId (id: number): Promise<MVideoBlacklist> {
const query = {
where: {
videoId: id
}
}
return VideoBlacklistModel.findOne(query)
}
toFormattedJSON (this: MVideoBlacklistFormattable): VideoBlacklist {
return {
id: this.id,
createdAt: this.createdAt,
updatedAt: this.updatedAt,
reason: this.reason,
unfederated: this.unfederated,
type: this.type,
video: this.Video.toFormattedJSON()
}
}
}
+279
ファイルの表示
@@ -0,0 +1,279 @@
import { VideoCaption, VideoCaptionObject } from '@peertube/peertube-models'
import { buildUUID } from '@peertube/peertube-node-utils'
import {
MVideo,
MVideoCaption,
MVideoCaptionFormattable,
MVideoCaptionLanguageUrl,
MVideoCaptionVideo
} from '@server/types/models/index.js'
import { remove } from 'fs-extra/esm'
import { join } from 'path'
import { Op, OrderItem, Transaction } from 'sequelize'
import {
AllowNull,
BeforeDestroy,
BelongsTo,
Column,
CreatedAt,
DataType,
ForeignKey,
Is, Scopes,
Table,
UpdatedAt
} from 'sequelize-typescript'
import { isVideoCaptionLanguageValid } from '../../helpers/custom-validators/video-captions.js'
import { logger } from '../../helpers/logger.js'
import { CONFIG } from '../../initializers/config.js'
import { CONSTRAINTS_FIELDS, LAZY_STATIC_PATHS, VIDEO_LANGUAGES, WEBSERVER } from '../../initializers/constants.js'
import { SequelizeModel, buildWhereIdOrUUID, throwIfNotValid } from '../shared/index.js'
import { VideoModel } from './video.js'
export enum ScopeNames {
WITH_VIDEO_UUID_AND_REMOTE = 'WITH_VIDEO_UUID_AND_REMOTE'
}
@Scopes(() => ({
[ScopeNames.WITH_VIDEO_UUID_AND_REMOTE]: {
include: [
{
attributes: [ 'id', 'uuid', 'remote' ],
model: VideoModel.unscoped(),
required: true
}
]
}
}))
@Table({
tableName: 'videoCaption',
indexes: [
{
fields: [ 'filename' ],
unique: true
},
{
fields: [ 'videoId' ]
},
{
fields: [ 'videoId', 'language' ],
unique: true
}
]
})
export class VideoCaptionModel extends SequelizeModel<VideoCaptionModel> {
@CreatedAt
createdAt: Date
@UpdatedAt
updatedAt: Date
@AllowNull(false)
@Is('VideoCaptionLanguage', value => throwIfNotValid(value, isVideoCaptionLanguageValid, 'language'))
@Column
language: string
@AllowNull(false)
@Column
filename: string
@AllowNull(true)
@Column(DataType.STRING(CONSTRAINTS_FIELDS.COMMONS.URL.max))
fileUrl: string
@AllowNull(false)
@Column
automaticallyGenerated: boolean
@ForeignKey(() => VideoModel)
@Column
videoId: number
@BelongsTo(() => VideoModel, {
foreignKey: {
allowNull: false
},
onDelete: 'CASCADE'
})
Video: Awaited<VideoModel>
@BeforeDestroy
static async removeFiles (instance: VideoCaptionModel, options) {
if (!instance.Video) {
instance.Video = await instance.$get('Video', { transaction: options.transaction })
}
if (instance.isOwned()) {
logger.info('Removing caption %s.', instance.filename)
try {
await instance.removeCaptionFile()
} catch (err) {
logger.error('Cannot remove caption file %s.', instance.filename)
}
}
return undefined
}
static async insertOrReplaceLanguage (caption: MVideoCaption, transaction: Transaction) {
const existing = await VideoCaptionModel.loadByVideoIdAndLanguage(caption.videoId, caption.language, transaction)
// Delete existing file
if (existing) await existing.destroy({ transaction })
return caption.save({ transaction })
}
// ---------------------------------------------------------------------------
static loadByVideoIdAndLanguage (videoId: string | number, language: string, transaction?: Transaction): Promise<MVideoCaptionVideo> {
const videoInclude = {
model: VideoModel.unscoped(),
attributes: [ 'id', 'name', 'remote', 'uuid', 'url' ],
where: buildWhereIdOrUUID(videoId)
}
const query = {
where: {
language
},
include: [
videoInclude
],
transaction
}
return VideoCaptionModel.findOne(query)
}
static loadWithVideoByFilename (filename: string): Promise<MVideoCaptionVideo> {
const query = {
where: {
filename
},
include: [
{
model: VideoModel.unscoped(),
attributes: [ 'id', 'remote', 'uuid' ]
}
]
}
return VideoCaptionModel.findOne(query)
}
// ---------------------------------------------------------------------------
static async hasVideoCaption (videoId: number) {
const query = {
where: {
videoId
}
}
const result = await VideoCaptionModel.unscoped().findOne(query)
return !!result
}
static listVideoCaptions (videoId: number, transaction?: Transaction): Promise<MVideoCaptionVideo[]> {
const query = {
order: [ [ 'language', 'ASC' ] ] as OrderItem[],
where: {
videoId
},
transaction
}
return VideoCaptionModel.scope(ScopeNames.WITH_VIDEO_UUID_AND_REMOTE).findAll(query)
}
static async listCaptionsOfMultipleVideos (videoIds: number[], transaction?: Transaction) {
const query = {
order: [ [ 'language', 'ASC' ] ] as OrderItem[],
where: {
videoId: {
[Op.in]: videoIds
}
},
transaction
}
const captions = await VideoCaptionModel.scope(ScopeNames.WITH_VIDEO_UUID_AND_REMOTE).findAll<MVideoCaptionVideo>(query)
const result: { [ id: number ]: MVideoCaptionVideo[] } = {}
for (const id of videoIds) {
result[id] = []
}
for (const caption of captions) {
result[caption.videoId].push(caption)
}
return result
}
// ---------------------------------------------------------------------------
static getLanguageLabel (language: string) {
return VIDEO_LANGUAGES[language] || 'Unknown'
}
static generateCaptionName (language: string) {
return `${buildUUID()}-${language}.vtt`
}
// ---------------------------------------------------------------------------
toFormattedJSON (this: MVideoCaptionFormattable): VideoCaption {
return {
language: {
id: this.language,
label: VideoCaptionModel.getLanguageLabel(this.language)
},
automaticallyGenerated: this.automaticallyGenerated,
captionPath: this.getCaptionStaticPath(),
updatedAt: this.updatedAt.toISOString()
}
}
toActivityPubObject (this: MVideoCaptionLanguageUrl, video: MVideo): VideoCaptionObject {
return {
identifier: this.language,
name: VideoCaptionModel.getLanguageLabel(this.language),
automaticallyGenerated: this.automaticallyGenerated,
url: this.getFileUrl(video)
}
}
// ---------------------------------------------------------------------------
isOwned () {
return this.Video.remote === false
}
getCaptionStaticPath (this: MVideoCaptionLanguageUrl) {
return join(LAZY_STATIC_PATHS.VIDEO_CAPTIONS, this.filename)
}
getFSPath () {
return join(CONFIG.STORAGE.CAPTIONS_DIR, this.filename)
}
removeCaptionFile (this: MVideoCaption) {
return remove(this.getFSPath())
}
getFileUrl (this: MVideoCaptionLanguageUrl, video: MVideo) {
if (video.isOwned()) return WEBSERVER.URL + this.getCaptionStaticPath()
return this.fileUrl
}
isEqual (this: MVideoCaption, other: MVideoCaption) {
if (this.fileUrl) return this.fileUrl === other.fileUrl
return this.filename === other.filename
}
}
+136
ファイルの表示
@@ -0,0 +1,136 @@
import { VideoChangeOwnership, type VideoChangeOwnershipStatusType } from '@peertube/peertube-models'
import { MVideoChangeOwnershipFormattable, MVideoChangeOwnershipFull } from '@server/types/models/video/video-change-ownership.js'
import { AllowNull, BelongsTo, Column, CreatedAt, ForeignKey, Scopes, Table, UpdatedAt } from 'sequelize-typescript'
import { AccountModel } from '../account/account.js'
import { SequelizeModel, getSort } from '../shared/index.js'
import { VideoModel, ScopeNames as VideoScopeNames } from './video.js'
enum ScopeNames {
WITH_ACCOUNTS = 'WITH_ACCOUNTS',
WITH_VIDEO = 'WITH_VIDEO'
}
@Table({
tableName: 'videoChangeOwnership',
indexes: [
{
fields: [ 'videoId' ]
},
{
fields: [ 'initiatorAccountId' ]
},
{
fields: [ 'nextOwnerAccountId' ]
}
]
})
@Scopes(() => ({
[ScopeNames.WITH_ACCOUNTS]: {
include: [
{
model: AccountModel,
as: 'Initiator',
required: true
},
{
model: AccountModel,
as: 'NextOwner',
required: true
}
]
},
[ScopeNames.WITH_VIDEO]: {
include: [
{
model: VideoModel.scope([
VideoScopeNames.WITH_THUMBNAILS,
VideoScopeNames.WITH_WEB_VIDEO_FILES,
VideoScopeNames.WITH_STREAMING_PLAYLISTS,
VideoScopeNames.WITH_ACCOUNT_DETAILS
]),
required: true
}
]
}
}))
export class VideoChangeOwnershipModel extends SequelizeModel<VideoChangeOwnershipModel> {
@CreatedAt
createdAt: Date
@UpdatedAt
updatedAt: Date
@AllowNull(false)
@Column
status: VideoChangeOwnershipStatusType
@ForeignKey(() => AccountModel)
@Column
initiatorAccountId: number
@BelongsTo(() => AccountModel, {
foreignKey: {
name: 'initiatorAccountId',
allowNull: false
},
onDelete: 'cascade'
})
Initiator: Awaited<AccountModel>
@ForeignKey(() => AccountModel)
@Column
nextOwnerAccountId: number
@BelongsTo(() => AccountModel, {
foreignKey: {
name: 'nextOwnerAccountId',
allowNull: false
},
onDelete: 'cascade'
})
NextOwner: Awaited<AccountModel>
@ForeignKey(() => VideoModel)
@Column
videoId: number
@BelongsTo(() => VideoModel, {
foreignKey: {
allowNull: false
},
onDelete: 'cascade'
})
Video: Awaited<VideoModel>
static listForApi (nextOwnerId: number, start: number, count: number, sort: string) {
const query = {
offset: start,
limit: count,
order: getSort(sort),
where: {
nextOwnerAccountId: nextOwnerId
}
}
return Promise.all([
VideoChangeOwnershipModel.scope(ScopeNames.WITH_ACCOUNTS).count(query),
VideoChangeOwnershipModel.scope([ ScopeNames.WITH_ACCOUNTS, ScopeNames.WITH_VIDEO ]).findAll<MVideoChangeOwnershipFull>(query)
]).then(([ count, rows ]) => ({ total: count, data: rows }))
}
static load (id: number): Promise<MVideoChangeOwnershipFull> {
return VideoChangeOwnershipModel.scope([ ScopeNames.WITH_ACCOUNTS, ScopeNames.WITH_VIDEO ])
.findByPk(id)
}
toFormattedJSON (this: MVideoChangeOwnershipFormattable): VideoChangeOwnership {
return {
id: this.id,
status: this.status,
initiatorAccount: this.Initiator.toFormattedJSON(),
nextOwnerAccount: this.NextOwner.toFormattedJSON(),
video: this.Video.toFormattedJSON(),
createdAt: this.createdAt
}
}
}
+173
ファイルの表示
@@ -0,0 +1,173 @@
import { VideoChannelSync, VideoChannelSyncState, type VideoChannelSyncStateType } from '@peertube/peertube-models'
import { isUrlValid } from '@server/helpers/custom-validators/activitypub/misc.js'
import { isVideoChannelSyncStateValid } from '@server/helpers/custom-validators/video-channel-syncs.js'
import { CONSTRAINTS_FIELDS, VIDEO_CHANNEL_SYNC_STATE } from '@server/initializers/constants.js'
import { MChannelSync, MChannelSyncChannel, MChannelSyncFormattable } from '@server/types/models/index.js'
import { Op } from 'sequelize'
import {
AllowNull,
BelongsTo,
Column,
CreatedAt,
DataType,
Default,
DefaultScope,
ForeignKey,
Is, Table,
UpdatedAt
} from 'sequelize-typescript'
import { AccountModel } from '../account/account.js'
import { SequelizeModel, getChannelSyncSort, throwIfNotValid } from '../shared/index.js'
import { UserModel } from '../user/user.js'
import { VideoChannelModel } from './video-channel.js'
@DefaultScope(() => ({
include: [
{
model: VideoChannelModel, // Default scope includes avatar and server
required: true
}
]
}))
@Table({
tableName: 'videoChannelSync',
indexes: [
{
fields: [ 'videoChannelId' ]
}
]
})
export class VideoChannelSyncModel extends SequelizeModel<VideoChannelSyncModel> {
@AllowNull(false)
@Default(null)
@Is('VideoChannelExternalChannelUrl', value => throwIfNotValid(value, isUrlValid, 'externalChannelUrl', true))
@Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_CHANNEL_SYNCS.EXTERNAL_CHANNEL_URL.max))
externalChannelUrl: string
@AllowNull(false)
@Default(VideoChannelSyncState.WAITING_FIRST_RUN)
@Is('VideoChannelSyncState', value => throwIfNotValid(value, isVideoChannelSyncStateValid, 'state'))
@Column
state: VideoChannelSyncStateType
@AllowNull(true)
@Column(DataType.DATE)
lastSyncAt: Date
@CreatedAt
createdAt: Date
@UpdatedAt
updatedAt: Date
@ForeignKey(() => VideoChannelModel)
@Column
videoChannelId: number
@BelongsTo(() => VideoChannelModel, {
foreignKey: {
allowNull: false
},
onDelete: 'cascade'
})
VideoChannel: Awaited<VideoChannelModel>
static listByAccountForAPI (options: {
accountId: number
start: number
count: number
sort: string
}) {
const getQuery = (forCount: boolean) => {
const videoChannelModel = forCount
? VideoChannelModel.unscoped()
: VideoChannelModel
return {
offset: options.start,
limit: options.count,
order: getChannelSyncSort(options.sort),
include: [
{
model: videoChannelModel,
required: true,
where: {
accountId: options.accountId
}
}
]
}
}
return Promise.all([
VideoChannelSyncModel.unscoped().count(getQuery(true)),
VideoChannelSyncModel.unscoped().findAll(getQuery(false))
]).then(([ total, data ]) => ({ total, data }))
}
static countByAccount (accountId: number) {
const query = {
include: [
{
model: VideoChannelModel.unscoped(),
required: true,
where: {
accountId
}
}
]
}
return VideoChannelSyncModel.unscoped().count(query)
}
static loadWithChannel (id: number): Promise<MChannelSyncChannel> {
return VideoChannelSyncModel.findByPk(id)
}
static async listSyncs (): Promise<MChannelSync[]> {
const query = {
include: [
{
model: VideoChannelModel.unscoped(),
required: true,
include: [
{
model: AccountModel.unscoped(),
required: true,
include: [ {
attributes: [],
model: UserModel.unscoped(),
required: true,
where: {
videoQuota: {
[Op.ne]: 0
},
videoQuotaDaily: {
[Op.ne]: 0
}
}
} ]
}
]
}
]
}
return VideoChannelSyncModel.unscoped().findAll(query)
}
toFormattedJSON (this: MChannelSyncFormattable): VideoChannelSync {
return {
id: this.id,
state: {
id: this.state,
label: VIDEO_CHANNEL_SYNC_STATE[this.state]
},
externalChannelUrl: this.externalChannelUrl,
createdAt: this.createdAt.toISOString(),
channel: this.VideoChannel.toFormattedSummaryJSON(),
lastSyncAt: this.lastSyncAt?.toISOString()
}
}
}
+859
ファイルの表示
@@ -0,0 +1,859 @@
import { forceNumber, pick } from '@peertube/peertube-core-utils'
import { ActivityPubActor, VideoChannel, VideoChannelSummary } from '@peertube/peertube-models'
import { CONFIG } from '@server/initializers/config.js'
import { InternalEventEmitter } from '@server/lib/internal-event-emitter.js'
import { MAccountHost } from '@server/types/models/index.js'
import { FindOptions, Includeable, literal, Op, QueryTypes, ScopeOptions, Transaction, WhereOptions } from 'sequelize'
import {
AfterCreate,
AfterDestroy,
AfterUpdate,
AllowNull,
BeforeDestroy,
BelongsTo,
Column,
CreatedAt,
DataType,
Default,
DefaultScope,
ForeignKey,
HasMany,
Is, Scopes,
Sequelize,
Table,
UpdatedAt
} from 'sequelize-typescript'
import {
isVideoChannelDescriptionValid,
isVideoChannelDisplayNameValid,
isVideoChannelSupportValid
} from '../../helpers/custom-validators/video-channels.js'
import { CONSTRAINTS_FIELDS, WEBSERVER } from '../../initializers/constants.js'
import { sendDeleteActor } from '../../lib/activitypub/send/index.js'
import {
MChannelAP,
MChannelBannerAccountDefault,
MChannelFormattable,
MChannelHost,
MChannelSummaryFormattable,
type MChannel, MChannelDefault
} from '../../types/models/video/index.js'
import { AccountModel, ScopeNames as AccountModelScopeNames, SummaryOptions as AccountSummaryOptions } from '../account/account.js'
import { ActorFollowModel } from '../actor/actor-follow.js'
import { ActorImageModel } from '../actor/actor-image.js'
import { ActorModel, unusedActorAttributesForAPI } from '../actor/actor.js'
import { ServerModel } from '../server/server.js'
import {
SequelizeModel,
buildServerIdsFollowedBy,
buildTrigramSearchIndex,
createSimilarityAttribute,
getSort,
setAsUpdated,
throwIfNotValid
} from '../shared/index.js'
import { VideoPlaylistModel } from './video-playlist.js'
import { VideoModel } from './video.js'
export enum ScopeNames {
FOR_API = 'FOR_API',
SUMMARY = 'SUMMARY',
WITH_ACCOUNT = 'WITH_ACCOUNT',
WITH_ACTOR = 'WITH_ACTOR',
WITH_ACTOR_BANNER = 'WITH_ACTOR_BANNER',
WITH_VIDEOS = 'WITH_VIDEOS',
WITH_STATS = 'WITH_STATS'
}
type AvailableForListOptions = {
actorId: number
search?: string
host?: string
handles?: string[]
forCount?: boolean
}
type AvailableWithStatsOptions = {
daysPrior: number
}
export type SummaryOptions = {
actorRequired?: boolean // Default: true
withAccount?: boolean // Default: false
withAccountBlockerIds?: number[]
}
@DefaultScope(() => ({
include: [
{
model: ActorModel,
required: true
}
]
}))
@Scopes(() => ({
[ScopeNames.FOR_API]: (options: AvailableForListOptions) => {
// Only list local channels OR channels that are on an instance followed by actorId
const inQueryInstanceFollow = buildServerIdsFollowedBy(options.actorId)
const whereActorAnd: WhereOptions[] = [
{
[Op.or]: [
{
serverId: null
},
{
serverId: {
[Op.in]: Sequelize.literal(inQueryInstanceFollow)
}
}
]
}
]
let serverRequired = false
let whereServer: WhereOptions
if (options.host && options.host !== WEBSERVER.HOST) {
serverRequired = true
whereServer = { host: options.host }
}
if (options.host === WEBSERVER.HOST) {
whereActorAnd.push({
serverId: null
})
}
if (Array.isArray(options.handles) && options.handles.length !== 0) {
const or: string[] = []
for (const handle of options.handles || []) {
const [ preferredUsername, host ] = handle.split('@')
const sanitizedPreferredUsername = VideoChannelModel.sequelize.escape(preferredUsername.toLowerCase())
const sanitizedHost = VideoChannelModel.sequelize.escape(host)
if (!host || host === WEBSERVER.HOST) {
or.push(`(LOWER("preferredUsername") = ${sanitizedPreferredUsername} AND "serverId" IS NULL)`)
} else {
or.push(
`(` +
`LOWER("preferredUsername") = ${sanitizedPreferredUsername} ` +
`AND "host" = ${sanitizedHost}` +
`)`
)
}
}
whereActorAnd.push({
id: {
[Op.in]: literal(`(SELECT "actor".id FROM actor LEFT JOIN server on server.id = actor."serverId" WHERE ${or.join(' OR ')})`)
}
})
}
const channelActorInclude: Includeable[] = []
const accountActorInclude: Includeable[] = []
if (options.forCount !== true) {
accountActorInclude.push({
model: ServerModel,
required: false
})
accountActorInclude.push({
model: ActorImageModel,
as: 'Avatars',
required: false
})
channelActorInclude.push({
model: ActorImageModel,
as: 'Avatars',
required: false
})
channelActorInclude.push({
model: ActorImageModel,
as: 'Banners',
required: false
})
}
if (options.forCount !== true || serverRequired) {
channelActorInclude.push({
model: ServerModel,
duplicating: false,
required: serverRequired,
where: whereServer
})
}
return {
include: [
{
attributes: {
exclude: unusedActorAttributesForAPI
},
model: ActorModel.unscoped(),
where: {
[Op.and]: whereActorAnd
},
include: channelActorInclude
},
{
model: AccountModel.unscoped(),
required: true,
include: [
{
attributes: {
exclude: unusedActorAttributesForAPI
},
model: ActorModel.unscoped(),
required: true,
include: accountActorInclude
}
]
}
]
}
},
[ScopeNames.SUMMARY]: (options: SummaryOptions = {}) => {
const include: Includeable[] = [
{
attributes: [ 'id', 'preferredUsername', 'url', 'serverId' ],
model: ActorModel.unscoped(),
required: options.actorRequired ?? true,
include: [
{
attributes: [ 'host' ],
model: ServerModel.unscoped(),
required: false
},
{
model: ActorImageModel,
as: 'Avatars',
required: false
}
]
}
]
const base: FindOptions = {
attributes: [ 'id', 'name', 'description', 'actorId' ]
}
if (options.withAccount === true) {
include.push({
model: AccountModel.scope({
method: [ AccountModelScopeNames.SUMMARY, { withAccountBlockerIds: options.withAccountBlockerIds } as AccountSummaryOptions ]
}),
required: true
})
}
base.include = include
return base
},
[ScopeNames.WITH_ACCOUNT]: {
include: [
{
model: AccountModel,
required: true
}
]
},
[ScopeNames.WITH_ACTOR]: {
include: [
ActorModel
]
},
[ScopeNames.WITH_ACTOR_BANNER]: {
include: [
{
model: ActorModel,
include: [
{
model: ActorImageModel,
required: false,
as: 'Banners'
}
]
}
]
},
[ScopeNames.WITH_VIDEOS]: {
include: [
VideoModel
]
},
[ScopeNames.WITH_STATS]: (options: AvailableWithStatsOptions = { daysPrior: 30 }) => {
const daysPrior = forceNumber(options.daysPrior)
return {
attributes: {
include: [
[
literal('(SELECT COUNT(*) FROM "video" WHERE "channelId" = "VideoChannelModel"."id")'),
'videosCount'
],
[
literal(
'(' +
`SELECT string_agg(concat_ws('|', t.day, t.views), ',') ` +
'FROM ( ' +
'WITH ' +
'days AS ( ' +
`SELECT generate_series(date_trunc('day', now()) - '${daysPrior} day'::interval, ` +
`date_trunc('day', now()), '1 day'::interval) AS day ` +
') ' +
'SELECT days.day AS day, COALESCE(SUM("videoView".views), 0) AS views ' +
'FROM days ' +
'LEFT JOIN (' +
'"videoView" INNER JOIN "video" ON "videoView"."videoId" = "video"."id" ' +
'AND "video"."channelId" = "VideoChannelModel"."id"' +
`) ON date_trunc('day', "videoView"."startDate") = date_trunc('day', days.day) ` +
'GROUP BY day ' +
'ORDER BY day ' +
') t' +
')'
),
'viewsPerDay'
],
[
literal(
'(' +
'SELECT COALESCE(SUM("video".views), 0) AS totalViews ' +
'FROM "video" ' +
'WHERE "video"."channelId" = "VideoChannelModel"."id"' +
')'
),
'totalViews'
]
]
}
}
}
}))
@Table({
tableName: 'videoChannel',
indexes: [
buildTrigramSearchIndex('video_channel_name_trigram', 'name'),
{
fields: [ 'accountId' ]
},
{
fields: [ 'actorId' ]
}
]
})
export class VideoChannelModel extends SequelizeModel<VideoChannelModel> {
@AllowNull(false)
@Is('VideoChannelName', value => throwIfNotValid(value, isVideoChannelDisplayNameValid, 'name'))
@Column
name: string
@AllowNull(true)
@Default(null)
@Is('VideoChannelDescription', value => throwIfNotValid(value, isVideoChannelDescriptionValid, 'description', true))
@Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_CHANNELS.DESCRIPTION.max))
description: string
@AllowNull(true)
@Default(null)
@Is('VideoChannelSupport', value => throwIfNotValid(value, isVideoChannelSupportValid, 'support', true))
@Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_CHANNELS.SUPPORT.max))
support: string
@CreatedAt
createdAt: Date
@UpdatedAt
updatedAt: Date
@ForeignKey(() => ActorModel)
@Column
actorId: number
@BelongsTo(() => ActorModel, {
foreignKey: {
allowNull: false
},
onDelete: 'cascade'
})
Actor: Awaited<ActorModel>
@ForeignKey(() => AccountModel)
@Column
accountId: number
@BelongsTo(() => AccountModel, {
foreignKey: {
allowNull: false
}
})
Account: Awaited<AccountModel>
@HasMany(() => VideoModel, {
foreignKey: {
name: 'channelId',
allowNull: false
},
onDelete: 'CASCADE',
hooks: true
})
Videos: Awaited<VideoModel>[]
@HasMany(() => VideoPlaylistModel, {
foreignKey: {
allowNull: true
},
onDelete: 'CASCADE',
hooks: true
})
VideoPlaylists: Awaited<VideoPlaylistModel>[]
@AfterCreate
static notifyCreate (channel: MChannel) {
InternalEventEmitter.Instance.emit('channel-created', { channel })
}
@AfterUpdate
static notifyUpdate (channel: MChannel) {
InternalEventEmitter.Instance.emit('channel-updated', { channel })
}
@AfterDestroy
static notifyDestroy (channel: MChannel) {
InternalEventEmitter.Instance.emit('channel-deleted', { channel })
}
@BeforeDestroy
static async sendDeleteIfOwned (instance: VideoChannelModel, options) {
if (!instance.Actor) {
instance.Actor = await instance.$get('Actor', { transaction: options.transaction })
}
await ActorFollowModel.removeFollowsOf(instance.Actor.id, options.transaction)
if (instance.Actor.isOwned()) {
return sendDeleteActor(instance.Actor, options.transaction)
}
return undefined
}
static countByAccount (accountId: number) {
const query = {
where: {
accountId
}
}
return VideoChannelModel.unscoped().count(query)
}
static async getStats () {
function getLocalVideoChannelStats (days?: number) {
const options = {
type: QueryTypes.SELECT as QueryTypes.SELECT,
raw: true
}
const videoJoin = days
? `INNER JOIN "video" AS "Videos" ON "VideoChannelModel"."id" = "Videos"."channelId" ` +
`AND ("Videos"."publishedAt" > Now() - interval '${days}d')`
: ''
const query = `
SELECT COUNT(DISTINCT("VideoChannelModel"."id")) AS "count"
FROM "videoChannel" AS "VideoChannelModel"
${videoJoin}
INNER JOIN "account" AS "Account" ON "VideoChannelModel"."accountId" = "Account"."id"
INNER JOIN "actor" AS "Account->Actor" ON "Account"."actorId" = "Account->Actor"."id"
AND "Account->Actor"."serverId" IS NULL`
return VideoChannelModel.sequelize.query<{ count: string }>(query, options)
.then(r => parseInt(r[0].count, 10))
}
const totalLocalVideoChannels = await getLocalVideoChannelStats()
const totalLocalDailyActiveVideoChannels = await getLocalVideoChannelStats(1)
const totalLocalWeeklyActiveVideoChannels = await getLocalVideoChannelStats(7)
const totalLocalMonthlyActiveVideoChannels = await getLocalVideoChannelStats(30)
const totalLocalHalfYearActiveVideoChannels = await getLocalVideoChannelStats(180)
return {
totalLocalVideoChannels,
totalLocalDailyActiveVideoChannels,
totalLocalWeeklyActiveVideoChannels,
totalLocalMonthlyActiveVideoChannels,
totalLocalHalfYearActiveVideoChannels
}
}
static listLocalsForSitemap (sort: string): Promise<MChannelHost[]> {
const query = {
attributes: [ ],
offset: 0,
order: getSort(sort),
include: [
{
attributes: [ 'preferredUsername', 'serverId' ],
model: ActorModel.unscoped(),
where: {
serverId: null
}
}
]
}
return VideoChannelModel
.unscoped()
.findAll(query)
}
static listForApi (parameters: Pick<AvailableForListOptions, 'actorId'> & {
start: number
count: number
sort: string
}) {
const { actorId } = parameters
const query = {
offset: parameters.start,
limit: parameters.count,
order: getSort(parameters.sort)
}
const getScope = (forCount: boolean) => {
return { method: [ ScopeNames.FOR_API, { actorId, forCount } as AvailableForListOptions ] }
}
return Promise.all([
VideoChannelModel.scope(getScope(true)).count(),
VideoChannelModel.scope(getScope(false)).findAll(query)
]).then(([ total, data ]) => ({ total, data }))
}
static searchForApi (options: Pick<AvailableForListOptions, 'actorId' | 'search' | 'host' | 'handles'> & {
start: number
count: number
sort: string
}) {
let attributesInclude: any[] = [ literal('0 as similarity') ]
let where: WhereOptions
if (options.search) {
const escapedSearch = VideoChannelModel.sequelize.escape(options.search)
const escapedLikeSearch = VideoChannelModel.sequelize.escape('%' + options.search + '%')
attributesInclude = [ createSimilarityAttribute('VideoChannelModel.name', options.search) ]
where = {
[Op.or]: [
Sequelize.literal(
'lower(immutable_unaccent("VideoChannelModel"."name")) % lower(immutable_unaccent(' + escapedSearch + '))'
),
Sequelize.literal(
'lower(immutable_unaccent("VideoChannelModel"."name")) LIKE lower(immutable_unaccent(' + escapedLikeSearch + '))'
)
]
}
}
const query = {
attributes: {
include: attributesInclude
},
offset: options.start,
limit: options.count,
order: getSort(options.sort),
where
}
const getScope = (forCount: boolean) => {
return {
method: [
ScopeNames.FOR_API, {
...pick(options, [ 'actorId', 'host', 'handles' ]),
forCount
} as AvailableForListOptions
]
}
}
return Promise.all([
VideoChannelModel.scope(getScope(true)).count(query),
VideoChannelModel.scope(getScope(false)).findAll(query)
]).then(([ total, data ]) => ({ total, data }))
}
static listByAccountForAPI (options: {
accountId: number
start: number
count: number
sort: string
withStats?: boolean
search?: string
}) {
const escapedSearch = VideoModel.sequelize.escape(options.search)
const escapedLikeSearch = VideoModel.sequelize.escape('%' + options.search + '%')
const where = options.search
? {
[Op.or]: [
Sequelize.literal(
'lower(immutable_unaccent("VideoChannelModel"."name")) % lower(immutable_unaccent(' + escapedSearch + '))'
),
Sequelize.literal(
'lower(immutable_unaccent("VideoChannelModel"."name")) LIKE lower(immutable_unaccent(' + escapedLikeSearch + '))'
)
]
}
: null
const getQuery = (forCount: boolean) => {
const accountModel = forCount
? AccountModel.unscoped()
: AccountModel
return {
offset: options.start,
limit: options.count,
order: getSort(options.sort),
include: [
{
model: accountModel,
where: {
id: options.accountId
},
required: true
}
],
where
}
}
const findScopes: string | ScopeOptions | (string | ScopeOptions)[] = [ ScopeNames.WITH_ACTOR_BANNER ]
if (options.withStats === true) {
findScopes.push({
method: [ ScopeNames.WITH_STATS, { daysPrior: 30 } as AvailableWithStatsOptions ]
})
}
return Promise.all([
VideoChannelModel.unscoped().count(getQuery(true)),
VideoChannelModel.scope(findScopes).findAll(getQuery(false))
]).then(([ total, data ]) => ({ total, data }))
}
static listAllByAccount (accountId: number): Promise<MChannelDefault[]> {
const query = {
limit: CONFIG.VIDEO_CHANNELS.MAX_PER_USER,
include: [
{
attributes: [],
model: AccountModel.unscoped(),
where: {
id: accountId
},
required: true
}
]
}
return VideoChannelModel.findAll(query)
}
static loadAndPopulateAccount (id: number, transaction?: Transaction): Promise<MChannelBannerAccountDefault> {
return VideoChannelModel.unscoped()
.scope([ ScopeNames.WITH_ACTOR_BANNER, ScopeNames.WITH_ACCOUNT ])
.findByPk(id, { transaction })
}
static loadByUrlAndPopulateAccount (url: string): Promise<MChannelBannerAccountDefault> {
const query = {
include: [
{
model: ActorModel,
required: true,
where: {
url
},
include: [
{
model: ActorImageModel,
required: false,
as: 'Banners'
}
]
}
]
}
return VideoChannelModel
.scope([ ScopeNames.WITH_ACCOUNT ])
.findOne(query)
}
static loadByNameWithHostAndPopulateAccount (nameWithHost: string) {
const [ name, host ] = nameWithHost.split('@')
if (!host || host === WEBSERVER.HOST) return VideoChannelModel.loadLocalByNameAndPopulateAccount(name)
return VideoChannelModel.loadByNameAndHostAndPopulateAccount(name, host)
}
static loadLocalByNameAndPopulateAccount (name: string): Promise<MChannelBannerAccountDefault> {
const query = {
include: [
{
model: ActorModel,
required: true,
where: {
[Op.and]: [
ActorModel.wherePreferredUsername(name, 'Actor.preferredUsername'),
{ serverId: null }
]
},
include: [
{
model: ActorImageModel,
required: false,
as: 'Banners'
}
]
}
]
}
return VideoChannelModel.unscoped()
.scope([ ScopeNames.WITH_ACCOUNT ])
.findOne(query)
}
static loadByNameAndHostAndPopulateAccount (name: string, host: string): Promise<MChannelBannerAccountDefault> {
const query = {
include: [
{
model: ActorModel,
required: true,
where: ActorModel.wherePreferredUsername(name, 'Actor.preferredUsername'),
include: [
{
model: ServerModel,
required: true,
where: { host }
},
{
model: ActorImageModel,
required: false,
as: 'Banners'
}
]
}
]
}
return VideoChannelModel.unscoped()
.scope([ ScopeNames.WITH_ACCOUNT ])
.findOne(query)
}
toFormattedSummaryJSON (this: MChannelSummaryFormattable): VideoChannelSummary {
const actor = this.Actor.toFormattedSummaryJSON()
return {
id: this.id,
name: actor.name,
displayName: this.getDisplayName(),
url: actor.url,
host: actor.host,
avatars: actor.avatars
}
}
toFormattedJSON (this: MChannelFormattable): VideoChannel {
const viewsPerDayString = this.get('viewsPerDay') as string
const videosCount = this.get('videosCount') as number
let viewsPerDay: { date: Date, views: number }[]
if (viewsPerDayString) {
viewsPerDay = viewsPerDayString.split(',')
.map(v => {
const [ dateString, amount ] = v.split('|')
return {
date: new Date(dateString),
views: +amount
}
})
}
const totalViews = this.get('totalViews') as number
const actor = this.Actor.toFormattedJSON()
const videoChannel = {
id: this.id,
displayName: this.getDisplayName(),
description: this.description,
support: this.support,
isLocal: this.Actor.isOwned(),
updatedAt: this.updatedAt,
ownerAccount: undefined,
videosCount,
viewsPerDay,
totalViews,
avatars: actor.avatars
}
if (this.Account) videoChannel.ownerAccount = this.Account.toFormattedJSON()
return Object.assign(actor, videoChannel)
}
async toActivityPubObject (this: MChannelAP): Promise<ActivityPubActor> {
const obj = await this.Actor.toActivityPubObject(this.name)
return {
...obj,
summary: this.description,
support: this.support,
postingRestrictedToMods: true,
attributedTo: [
{
type: 'Person' as 'Person',
id: this.Account.Actor.url
}
]
}
}
// Avoid error when running this method on MAccount... | MChannel...
getClientUrl (this: MAccountHost | MChannelHost) {
return WEBSERVER.URL + '/c/' + this.Actor.getIdentifier() + '/videos'
}
getDisplayName () {
return this.name
}
isOutdated () {
return this.Actor.isOutdated()
}
setAsUpdated (transaction?: Transaction) {
return setAsUpdated({ sequelize: this.sequelize, table: 'videoChannel', id: this.id, transaction })
}
}
+95
ファイルの表示
@@ -0,0 +1,95 @@
import { AllowNull, BelongsTo, Column, CreatedAt, ForeignKey, Table, UpdatedAt } from 'sequelize-typescript'
import { MVideo, MVideoChapter } from '@server/types/models/index.js'
import { VideoChapter, VideoChapterObject } from '@peertube/peertube-models'
import { VideoModel } from './video.js'
import { Transaction } from 'sequelize'
import { getSort } from '../shared/sort.js'
import { SequelizeModel } from '../shared/sequelize-type.js'
@Table({
tableName: 'videoChapter',
indexes: [
{
fields: [ 'videoId', 'timecode' ],
unique: true
}
]
})
export class VideoChapterModel extends SequelizeModel<VideoChapterModel> {
@AllowNull(false)
@Column
timecode: number
@AllowNull(false)
@Column
title: string
@ForeignKey(() => VideoModel)
@Column
videoId: number
@BelongsTo(() => VideoModel, {
foreignKey: {
allowNull: false
},
onDelete: 'CASCADE'
})
Video: Awaited<VideoModel>
@CreatedAt
createdAt: Date
@UpdatedAt
updatedAt: Date
static deleteChapters (videoId: number, transaction: Transaction) {
const query = {
where: {
videoId
},
transaction
}
return VideoChapterModel.destroy(query)
}
static listChaptersOfVideo (videoId: number, transaction?: Transaction) {
const query = {
where: {
videoId
},
order: getSort('timecode'),
transaction
}
return VideoChapterModel.findAll<MVideoChapter>(query)
}
static hasVideoChapters (videoId: number, transaction: Transaction) {
return VideoChapterModel.findOne({
where: { videoId },
transaction
}).then(c => !!c)
}
toActivityPubJSON (this: MVideoChapter, options: {
video: MVideo
nextChapter: MVideoChapter
}): VideoChapterObject {
return {
name: this.title,
startOffset: this.timecode,
endOffset: options.nextChapter
? options.nextChapter.timecode
: options.video.duration
}
}
toFormattedJSON (this: MVideoChapter): VideoChapter {
return {
timecode: this.timecode,
title: this.title
}
}
}
+802
ファイルの表示
@@ -0,0 +1,802 @@
import { pick } from '@peertube/peertube-core-utils'
import {
ActivityTagObject,
ActivityTombstoneObject,
UserRight,
VideoComment,
VideoCommentForAdminOrUser,
VideoCommentObject
} from '@peertube/peertube-models'
import { extractMentions } from '@server/helpers/mentions.js'
import { getLocalApproveReplyActivityPubUrl } from '@server/lib/activitypub/url.js'
import { getServerActor } from '@server/models/application/application.js'
import { MAccount, MAccountId, MUserAccountId } from '@server/types/models/index.js'
import { Op, Order, QueryTypes, Sequelize, Transaction } from 'sequelize'
import {
AllowNull,
BelongsTo, Column,
CreatedAt,
DataType,
ForeignKey,
HasMany,
Is, Scopes,
Table,
UpdatedAt
} from 'sequelize-typescript'
import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc.js'
import { CONSTRAINTS_FIELDS, USER_EXPORT_MAX_ITEMS } from '../../initializers/constants.js'
import {
MComment,
MCommentAP,
MCommentAdminOrUserFormattable,
MCommentExport,
MCommentFormattable,
MCommentId,
MCommentOwner,
MCommentOwnerReplyVideoImmutable, MCommentOwnerVideoFeed,
MCommentOwnerVideoReply,
MVideo,
MVideoImmutable
} from '../../types/models/video/index.js'
import { VideoCommentAbuseModel } from '../abuse/video-comment-abuse.js'
import { AccountModel } from '../account/account.js'
import { ActorModel } from '../actor/actor.js'
import { CommentAutomaticTagModel } from '../automatic-tag/comment-automatic-tag.js'
import { SequelizeModel, buildLocalAccountIdsIn, buildSQLAttributes, throwIfNotValid } from '../shared/index.js'
import { ListVideoCommentsOptions, VideoCommentListQueryBuilder } from './sql/comment/video-comment-list-query-builder.js'
import { VideoChannelModel } from './video-channel.js'
import { VideoModel } from './video.js'
export enum ScopeNames {
WITH_ACCOUNT = 'WITH_ACCOUNT',
WITH_IN_REPLY_TO = 'WITH_IN_REPLY_TO',
WITH_VIDEO = 'WITH_VIDEO'
}
@Scopes(() => ({
[ScopeNames.WITH_ACCOUNT]: {
include: [
{
model: AccountModel
}
]
},
[ScopeNames.WITH_IN_REPLY_TO]: {
include: [
{
model: VideoCommentModel,
as: 'InReplyToVideoComment'
}
]
},
[ScopeNames.WITH_VIDEO]: {
include: [
{
model: VideoModel,
required: true,
include: [
{
model: VideoChannelModel.unscoped(),
attributes: [ 'id', 'accountId' ],
required: true,
include: [
{
attributes: [ 'id', 'url' ],
model: ActorModel.unscoped(),
required: true
},
{
attributes: [ 'id' ],
model: AccountModel.unscoped(),
required: true,
include: [
{
attributes: [ 'id', 'url' ],
model: ActorModel.unscoped(),
required: true
}
]
}
]
}
]
}
]
}
}))
@Table({
tableName: 'videoComment',
indexes: [
{
fields: [ 'videoId' ]
},
{
fields: [ 'videoId', 'originCommentId' ]
},
{
fields: [ 'url' ],
unique: true
},
{
fields: [ 'accountId' ]
},
{
fields: [
{ name: 'createdAt', order: 'DESC' }
]
}
]
})
export class VideoCommentModel extends SequelizeModel<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
@AllowNull(false)
@Column
heldForReview: boolean
@AllowNull(true)
@Column
replyApproval: string
@ForeignKey(() => VideoCommentModel)
@Column
originCommentId: number
@BelongsTo(() => VideoCommentModel, {
foreignKey: {
name: 'originCommentId',
allowNull: true
},
as: 'OriginVideoComment',
onDelete: 'CASCADE'
})
OriginVideoComment: Awaited<VideoCommentModel>
@ForeignKey(() => VideoCommentModel)
@Column
inReplyToCommentId: number
@BelongsTo(() => VideoCommentModel, {
foreignKey: {
name: 'inReplyToCommentId',
allowNull: true
},
as: 'InReplyToVideoComment',
onDelete: 'CASCADE'
})
InReplyToVideoComment: Awaited<VideoCommentModel> | null
@ForeignKey(() => VideoModel)
@Column
videoId: number
@BelongsTo(() => VideoModel, {
foreignKey: {
allowNull: false
},
onDelete: 'CASCADE'
})
Video: Awaited<VideoModel>
@ForeignKey(() => AccountModel)
@Column
accountId: number
@BelongsTo(() => AccountModel, {
foreignKey: {
allowNull: true
},
onDelete: 'CASCADE'
})
Account: Awaited<AccountModel>
@HasMany(() => VideoCommentAbuseModel, {
foreignKey: {
name: 'videoCommentId',
allowNull: true
},
onDelete: 'set null'
})
CommentAbuses: Awaited<VideoCommentAbuseModel>[]
@HasMany(() => CommentAutomaticTagModel, {
foreignKey: 'commentId',
onDelete: 'CASCADE'
})
CommentAutomaticTags: Awaited<CommentAutomaticTagModel>[]
// ---------------------------------------------------------------------------
static getSQLAttributes (tableName: string, aliasPrefix = '') {
return buildSQLAttributes({
model: this,
tableName,
aliasPrefix
})
}
// ---------------------------------------------------------------------------
static loadById (id: number, transaction?: Transaction): Promise<MComment> {
const query = {
where: {
id
},
transaction
}
return VideoCommentModel.findOne(query)
}
static loadByIdAndPopulateVideoAndAccountAndReply (id: number, transaction?: Transaction): Promise<MCommentOwnerVideoReply> {
const query = {
where: {
id
},
transaction
}
return VideoCommentModel
.scope([ ScopeNames.WITH_VIDEO, ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_IN_REPLY_TO ])
.findOne(query)
}
// ---------------------------------------------------------------------------
static loadByUrlAndPopulateAccountAndVideoAndReply (url: string, transaction?: Transaction): Promise<MCommentOwnerVideoReply> {
const query = {
where: {
url
},
transaction
}
return VideoCommentModel.scope([ ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_VIDEO, ScopeNames.WITH_IN_REPLY_TO ]).findOne(query)
}
static loadByUrlAndPopulateReplyAndVideoImmutableAndAccount (
url: string,
transaction?: Transaction
): Promise<MCommentOwnerReplyVideoImmutable> {
const query = {
where: {
url
},
include: [
{
attributes: [ 'id', 'uuid', 'url', 'remote' ],
model: VideoModel.unscoped()
}
],
transaction
}
return VideoCommentModel.scope([ ScopeNames.WITH_IN_REPLY_TO, ScopeNames.WITH_ACCOUNT ]).findOne(query)
}
// ---------------------------------------------------------------------------
static listCommentsForApi (parameters: {
start: number
count: number
sort: string
autoTagOfAccountId: number
videoAccountOwnerId?: number
videoChannelOwnerId?: number
onLocalVideo?: boolean
isLocal?: boolean
search?: string
searchAccount?: string
searchVideo?: string
heldForReview: boolean
videoId?: number
videoChannelId?: number
autoTagOneOf?: string[]
}) {
const queryOptions: ListVideoCommentsOptions = {
...pick(parameters, [
'start',
'count',
'sort',
'isLocal',
'search',
'searchVideo',
'searchAccount',
'onLocalVideo',
'videoId',
'videoChannelId',
'autoTagOneOf',
'autoTagOfAccountId',
'videoAccountOwnerId',
'videoChannelOwnerId',
'heldForReview'
]),
selectType: 'api',
notDeleted: true
}
return Promise.all([
new VideoCommentListQueryBuilder(VideoCommentModel.sequelize, queryOptions).listComments<MCommentAdminOrUserFormattable>(),
new VideoCommentListQueryBuilder(VideoCommentModel.sequelize, queryOptions).countComments()
]).then(([ rows, count ]) => {
return { total: count, data: rows }
})
}
static async listThreadsForApi (parameters: {
video: MVideo
start: number
count: number
sort: string
user?: MUserAccountId
}) {
const { video, user } = parameters
const { blockerAccountIds, canSeeHeldForReview } = await VideoCommentModel.buildBlockerAccountIdsAndCanSeeHeldForReview({ user, video })
const commonOptions: ListVideoCommentsOptions = {
selectType: 'api',
videoId: video.id,
blockerAccountIds,
heldForReview: canSeeHeldForReview
? undefined // Display all comments for video owner or moderator
: false,
heldForReviewAccountIdException: user?.Account?.id
}
const listOptions: ListVideoCommentsOptions = {
...commonOptions,
...pick(parameters, [ 'sort', 'start', 'count' ]),
isThread: true,
includeReplyCounters: true
}
const countOptions: ListVideoCommentsOptions = {
...commonOptions,
isThread: true
}
const notDeletedCountOptions: ListVideoCommentsOptions = {
...commonOptions,
notDeleted: true
}
return Promise.all([
new VideoCommentListQueryBuilder(VideoCommentModel.sequelize, listOptions).listComments<MCommentAdminOrUserFormattable>(),
new VideoCommentListQueryBuilder(VideoCommentModel.sequelize, countOptions).countComments(),
new VideoCommentListQueryBuilder(VideoCommentModel.sequelize, notDeletedCountOptions).countComments()
]).then(([ rows, count, totalNotDeletedComments ]) => {
return { total: count, data: rows, totalNotDeletedComments }
})
}
static async listThreadCommentsForApi (parameters: {
video: MVideo
threadId: number
user?: MUserAccountId
}) {
const { user, video, threadId } = parameters
const { blockerAccountIds, canSeeHeldForReview } = await VideoCommentModel.buildBlockerAccountIdsAndCanSeeHeldForReview({ user, video })
const queryOptions: ListVideoCommentsOptions = {
threadId,
videoId: video.id,
selectType: 'api',
sort: 'createdAt',
blockerAccountIds,
includeReplyCounters: true,
heldForReview: canSeeHeldForReview
? undefined // Display all comments for video owner or moderator
: false,
heldForReviewAccountIdException: user?.Account?.id
}
return Promise.all([
new VideoCommentListQueryBuilder(VideoCommentModel.sequelize, queryOptions).listComments<MCommentAdminOrUserFormattable>(),
new VideoCommentListQueryBuilder(VideoCommentModel.sequelize, queryOptions).countComments()
]).then(([ rows, count ]) => {
return { total: count, data: rows }
})
}
static listThreadParentComments (options: {
comment: MCommentId
transaction?: Transaction
order?: 'ASC' | 'DESC'
}): Promise<MCommentOwner[]> {
const { comment, transaction, order = 'ASC' } = options
const query = {
order: [ [ 'createdAt', order ] ] as Order,
where: {
id: {
[Op.in]: Sequelize.literal('(' +
'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"' +
') ' +
'SELECT id FROM children' +
')'),
[Op.ne]: comment.id
}
},
transaction
}
return VideoCommentModel
.scope([ ScopeNames.WITH_ACCOUNT ])
.findAll(query)
}
static async listAndCountByVideoForAP (parameters: {
video: MVideoImmutable
start: number
count: number
}) {
const { video } = parameters
const blockerAccountIds = await VideoCommentModel.buildBlockerAccountIds({ user: null })
const queryOptions: ListVideoCommentsOptions = {
...pick(parameters, [ 'start', 'count' ]),
selectType: 'comment-only',
videoId: video.id,
sort: 'createdAt',
heldForReview: false,
blockerAccountIds
}
return Promise.all([
new VideoCommentListQueryBuilder(VideoCommentModel.sequelize, queryOptions).listComments<MComment>(),
new VideoCommentListQueryBuilder(VideoCommentModel.sequelize, queryOptions).countComments()
]).then(([ rows, count ]) => {
return { total: count, data: rows }
})
}
static async listForFeed (parameters: {
start: number
count: number
videoId?: number
videoAccountOwnerId?: number
videoChannelOwnerId?: number
}) {
const blockerAccountIds = await VideoCommentModel.buildBlockerAccountIds({ user: null })
const queryOptions: ListVideoCommentsOptions = {
...pick(parameters, [ 'start', 'count', 'videoAccountOwnerId', 'videoId', 'videoChannelOwnerId' ]),
selectType: 'feed',
sort: '-createdAt',
onPublicVideo: true,
notDeleted: true,
heldForReview: false,
blockerAccountIds
}
return new VideoCommentListQueryBuilder(VideoCommentModel.sequelize, queryOptions).listComments<MCommentOwnerVideoFeed>()
}
static listForBulkDelete (ofAccount: MAccount, filter: { onVideosOfAccount?: MAccountId } = {}) {
const queryOptions: ListVideoCommentsOptions = {
selectType: 'comment-only',
accountId: ofAccount.id,
videoAccountOwnerId: filter.onVideosOfAccount?.id,
heldForReview: undefined,
notDeleted: true,
count: 5000
}
return new VideoCommentListQueryBuilder(VideoCommentModel.sequelize, queryOptions).listComments<MComment>()
}
static listForExport (ofAccountId: number): Promise<MCommentExport[]> {
return VideoCommentModel.findAll({
attributes: [ 'id', 'url', 'text', 'createdAt' ],
where: {
accountId: ofAccountId,
deletedAt: null
},
include: [
{
attributes: [ 'id', 'uuid', 'url' ],
required: true,
model: VideoModel.unscoped()
},
{
attributes: [ 'url' ],
required: false,
model: VideoCommentModel,
as: 'InReplyToVideoComment'
}
],
limit: USER_EXPORT_MAX_ITEMS
})
}
// ---------------------------------------------------------------------------
static async getStats () {
const where = {
deletedAt: null,
heldForReview: false
}
const totalLocalVideoComments = await VideoCommentModel.count({
where,
include: [
{
model: AccountModel.unscoped(),
required: true,
include: [
{
model: ActorModel.unscoped(),
required: true,
where: {
serverId: null
}
}
]
}
]
})
const totalVideoComments = await VideoCommentModel.count({ where })
return {
totalLocalVideoComments,
totalVideoComments
}
}
// ---------------------------------------------------------------------------
static listRemoteCommentUrlsOfLocalVideos () {
const query = `SELECT "videoComment".url FROM "videoComment" ` +
`INNER JOIN account ON account.id = "videoComment"."accountId" ` +
`INNER JOIN actor ON actor.id = "account"."actorId" AND actor."serverId" IS NOT NULL ` +
`INNER JOIN video ON video.id = "videoComment"."videoId" AND video.remote IS FALSE`
return VideoCommentModel.sequelize.query<{ url: string }>(query, {
type: QueryTypes.SELECT,
raw: true
}).then(rows => rows.map(r => r.url))
}
static cleanOldCommentsOf (videoId: number, beforeUpdatedAt: Date) {
const query = {
where: {
updatedAt: {
[Op.lt]: beforeUpdatedAt
},
videoId,
accountId: {
[Op.notIn]: buildLocalAccountIdsIn()
},
// Do not delete Tombstones
deletedAt: null
}
}
return VideoCommentModel.destroy(query)
}
// ---------------------------------------------------------------------------
getCommentStaticPath () {
return this.Video.getWatchStaticPath() + ';threadId=' + this.getThreadId()
}
getCommentUserReviewPath () {
return '/my-account/videos/comments?search=heldForReview:true'
}
getThreadId (): number {
return this.originCommentId || this.id
}
isOwned () {
if (!this.Account) return false
return this.Account.isOwned()
}
markAsDeleted () {
this.text = ''
this.deletedAt = new Date()
this.accountId = null
}
isDeleted () {
return this.deletedAt !== null
}
extractMentions () {
return extractMentions(this.text, this.isOwned())
}
toFormattedJSON (this: MCommentFormattable) {
return {
id: this.id,
url: this.url,
text: this.text,
threadId: this.getThreadId(),
inReplyToCommentId: this.inReplyToCommentId || null,
videoId: this.videoId,
createdAt: this.createdAt,
updatedAt: this.updatedAt,
deletedAt: this.deletedAt,
heldForReview: this.heldForReview,
isDeleted: this.isDeleted(),
totalRepliesFromVideoAuthor: this.get('totalRepliesFromVideoAuthor') || 0,
totalReplies: this.get('totalReplies') || 0,
account: this.Account
? this.Account.toFormattedJSON()
: null
} as VideoComment
}
toFormattedForAdminOrUserJSON (this: MCommentAdminOrUserFormattable) {
return {
id: this.id,
url: this.url,
text: this.text,
threadId: this.getThreadId(),
inReplyToCommentId: this.inReplyToCommentId || null,
videoId: this.videoId,
createdAt: this.createdAt,
updatedAt: this.updatedAt,
heldForReview: this.heldForReview,
automaticTags: (this.CommentAutomaticTags || []).map(m => m.AutomaticTag.name),
video: {
id: this.Video.id,
uuid: this.Video.uuid,
name: this.Video.name
},
account: this.Account
? this.Account.toFormattedJSON()
: null
} as VideoCommentForAdminOrUser
}
toActivityPubObject (this: MCommentAP, threadParentComments: MCommentOwner[]): VideoCommentObject | ActivityTombstoneObject {
const inReplyTo = this.inReplyToCommentId === null
? this.Video.url // New thread, so we reply to the video
: 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()
}
}
const tag: ActivityTagObject[] = []
for (const parentComment of threadParentComments) {
if (!parentComment.Account) continue
const actor = parentComment.Account.Actor
tag.push({
type: 'Mention',
href: actor.url,
name: `@${actor.preferredUsername}@${actor.getHost()}`
})
}
let replyApproval = this.replyApproval
if (this.Video.isOwned() && !this.heldForReview) {
replyApproval = getLocalApproveReplyActivityPubUrl(this.Video, this)
}
return {
type: 'Note' as 'Note',
id: this.url,
content: this.text,
mediaType: 'text/markdown',
inReplyTo,
updated: this.updatedAt.toISOString(),
published: this.createdAt.toISOString(),
url: this.url,
attributedTo: this.Account.Actor.url,
replyApproval,
tag
}
}
private static async buildBlockerAccountIds (options: {
user: MUserAccountId
}): Promise<number[]> {
const { user } = options
const serverActor = await getServerActor()
const blockerAccountIds = [ serverActor.Account.id ]
if (user) blockerAccountIds.push(user.Account.id)
return blockerAccountIds
}
private static buildBlockerAccountIdsAndCanSeeHeldForReview (options: {
video: MVideo
user: MUserAccountId
}) {
const { video, user } = options
const blockerAccountIdsPromise = this.buildBlockerAccountIds(options)
let canSeeHeldForReviewPromise: Promise<boolean>
if (user) {
if (user.hasRight(UserRight.SEE_ALL_COMMENTS)) {
canSeeHeldForReviewPromise = Promise.resolve(true)
} else {
canSeeHeldForReviewPromise = VideoChannelModel.loadAndPopulateAccount(video.channelId)
.then(c => c.accountId === user.Account.id)
}
} else {
canSeeHeldForReviewPromise = Promise.resolve(false)
}
return Promise.all([ blockerAccountIdsPromise, canSeeHeldForReviewPromise ])
.then(([ blockerAccountIds, canSeeHeldForReview ]) => ({ blockerAccountIds, canSeeHeldForReview }))
}
}
+656
ファイルの表示
@@ -0,0 +1,656 @@
import { ActivityVideoUrlObject, FileStorage, VideoResolution, type FileStorageType } from '@peertube/peertube-models'
import { logger } from '@server/helpers/logger.js'
import { extractVideo } from '@server/helpers/video.js'
import { CONFIG } from '@server/initializers/config.js'
import { buildRemoteUrl } from '@server/lib/activitypub/url.js'
import {
getHLSPrivateFileUrl,
getObjectStoragePublicFileUrl,
getWebVideoPrivateFileUrl
} from '@server/lib/object-storage/index.js'
import { getFSTorrentFilePath } from '@server/lib/paths.js'
import { getVideoFileMimeType } from '@server/lib/video-file.js'
import { isVideoInPrivateDirectory } from '@server/lib/video-privacy.js'
import { MStreamingPlaylistVideo, MVideo, MVideoWithHost, isStreamingPlaylist } from '@server/types/models/index.js'
import { remove } from 'fs-extra/esm'
import memoizee from 'memoizee'
import { join } from 'path'
import { FindOptions, Op, Transaction, WhereOptions } from 'sequelize'
import {
AllowNull,
BelongsTo,
Column,
CreatedAt,
DataType,
Default,
DefaultScope,
ForeignKey,
HasMany,
Is, Scopes,
Table,
UpdatedAt
} from 'sequelize-typescript'
import validator from 'validator'
import {
isVideoFPSResolutionValid,
isVideoFileExtnameValid,
isVideoFileInfoHashValid,
isVideoFileResolutionValid,
isVideoFileSizeValid
} from '../../helpers/custom-validators/videos.js'
import {
LAZY_STATIC_PATHS,
MEMOIZE_LENGTH,
MEMOIZE_TTL,
STATIC_DOWNLOAD_PATHS,
STATIC_PATHS,
WEBSERVER
} from '../../initializers/constants.js'
import { MVideoFile, MVideoFileStreamingPlaylistVideo, MVideoFileVideo } from '../../types/models/video/video-file.js'
import { VideoRedundancyModel } from '../redundancy/video-redundancy.js'
import { SequelizeModel, doesExist, parseAggregateResult, throwIfNotValid } from '../shared/index.js'
import { VideoStreamingPlaylistModel } from './video-streaming-playlist.js'
import { VideoModel } from './video.js'
export enum ScopeNames {
WITH_VIDEO = 'WITH_VIDEO',
WITH_METADATA = 'WITH_METADATA',
WITH_VIDEO_OR_PLAYLIST = 'WITH_VIDEO_OR_PLAYLIST'
}
@DefaultScope(() => ({
attributes: {
exclude: [ 'metadata' ]
}
}))
@Scopes(() => ({
[ScopeNames.WITH_VIDEO]: {
include: [
{
model: VideoModel.unscoped(),
required: true
}
]
},
[ScopeNames.WITH_VIDEO_OR_PLAYLIST]: (options: { whereVideo?: WhereOptions } = {}) => {
return {
include: [
{
model: VideoModel.unscoped(),
required: false,
where: options.whereVideo
},
{
model: VideoStreamingPlaylistModel.unscoped(),
required: false,
include: [
{
model: VideoModel.unscoped(),
required: true,
where: options.whereVideo
}
]
}
]
}
},
[ScopeNames.WITH_METADATA]: {
attributes: {
include: [ 'metadata' ]
}
}
}))
@Table({
tableName: 'videoFile',
indexes: [
{
fields: [ 'videoId' ],
where: {
videoId: {
[Op.ne]: null
}
}
},
{
fields: [ 'videoStreamingPlaylistId' ],
where: {
videoStreamingPlaylistId: {
[Op.ne]: null
}
}
},
{
fields: [ 'infoHash' ]
},
{
fields: [ 'torrentFilename' ],
unique: true
},
{
fields: [ 'filename' ],
unique: true
},
{
fields: [ 'videoId', 'resolution', 'fps' ],
unique: true,
where: {
videoId: {
[Op.ne]: null
}
}
},
{
fields: [ 'videoStreamingPlaylistId', 'resolution', 'fps' ],
unique: true,
where: {
videoStreamingPlaylistId: {
[Op.ne]: null
}
}
}
]
})
export class VideoFileModel extends SequelizeModel<VideoFileModel> {
@CreatedAt
createdAt: Date
@UpdatedAt
updatedAt: Date
@AllowNull(false)
@Is('VideoFileResolution', value => throwIfNotValid(value, isVideoFileResolutionValid, 'resolution'))
@Column
resolution: number
@AllowNull(true)
@Column
width: number
@AllowNull(true)
@Column
height: number
@AllowNull(false)
@Is('VideoFileSize', value => throwIfNotValid(value, isVideoFileSizeValid, 'size'))
@Column(DataType.BIGINT)
size: number
@AllowNull(false)
@Is('VideoFileExtname', value => throwIfNotValid(value, isVideoFileExtnameValid, 'extname'))
@Column
extname: string
@AllowNull(true)
@Is('VideoFileInfohash', value => throwIfNotValid(value, isVideoFileInfoHashValid, 'info hash', true))
@Column
infoHash: string
@AllowNull(false)
@Default(-1)
@Is('VideoFileFPS', value => throwIfNotValid(value, isVideoFPSResolutionValid, 'fps'))
@Column
fps: number
@AllowNull(true)
@Column(DataType.JSONB)
metadata: any
@AllowNull(true)
@Column
metadataUrl: string
// Could be null for remote files
@AllowNull(true)
@Column
fileUrl: string
// Could be null for live files
@AllowNull(true)
@Column
filename: string
// Could be null for remote files
@AllowNull(true)
@Column
torrentUrl: string
// Could be null for live files
@AllowNull(true)
@Column
torrentFilename: string
@ForeignKey(() => VideoModel)
@Column
videoId: number
@AllowNull(false)
@Default(FileStorage.FILE_SYSTEM)
@Column
storage: FileStorageType
@BelongsTo(() => VideoModel, {
foreignKey: {
allowNull: true
},
onDelete: 'CASCADE'
})
Video: Awaited<VideoModel>
@ForeignKey(() => VideoStreamingPlaylistModel)
@Column
videoStreamingPlaylistId: number
@BelongsTo(() => VideoStreamingPlaylistModel, {
foreignKey: {
allowNull: true
},
onDelete: 'CASCADE'
})
VideoStreamingPlaylist: Awaited<VideoStreamingPlaylistModel>
@HasMany(() => VideoRedundancyModel, {
foreignKey: {
allowNull: true
},
onDelete: 'CASCADE',
hooks: true
})
RedundancyVideos: Awaited<VideoRedundancyModel>[]
static doesInfohashExistCached = memoizee(VideoFileModel.doesInfohashExist.bind(VideoFileModel), {
promise: true,
max: MEMOIZE_LENGTH.INFO_HASH_EXISTS,
maxAge: MEMOIZE_TTL.INFO_HASH_EXISTS
})
static doesInfohashExist (infoHash: string) {
const query = 'SELECT 1 FROM "videoFile" WHERE "infoHash" = $infoHash LIMIT 1'
return doesExist({ sequelize: this.sequelize, query, bind: { infoHash } })
}
static async doesVideoExistForVideoFile (id: number, videoIdOrUUID: number | string) {
const videoFile = await VideoFileModel.loadWithVideoOrPlaylist(id, videoIdOrUUID)
return !!videoFile
}
static async doesOwnedTorrentFileExist (filename: string) {
const query = 'SELECT 1 FROM "videoFile" ' +
'LEFT JOIN "video" "webvideo" ON "webvideo"."id" = "videoFile"."videoId" AND "webvideo"."remote" IS FALSE ' +
'LEFT JOIN "videoStreamingPlaylist" ON "videoStreamingPlaylist"."id" = "videoFile"."videoStreamingPlaylistId" ' +
'LEFT JOIN "video" "hlsVideo" ON "hlsVideo"."id" = "videoStreamingPlaylist"."videoId" AND "hlsVideo"."remote" IS FALSE ' +
'WHERE "torrentFilename" = $filename AND ("hlsVideo"."id" IS NOT NULL OR "webvideo"."id" IS NOT NULL) LIMIT 1'
return doesExist({ sequelize: this.sequelize, query, bind: { filename } })
}
static async doesOwnedFileExist (filename: string, storage: FileStorageType) {
const query = 'SELECT 1 FROM "videoFile" INNER JOIN "video" ON "video"."id" = "videoFile"."videoId" AND "video"."remote" IS FALSE ' +
`WHERE "filename" = $filename AND "storage" = $storage LIMIT 1`
return doesExist({ sequelize: this.sequelize, query, bind: { filename, storage } })
}
static loadByFilename (filename: string) {
const query = {
where: {
filename
}
}
return VideoFileModel.findOne(query)
}
static loadWithVideoByFilename (filename: string): Promise<MVideoFileVideo | MVideoFileStreamingPlaylistVideo> {
const query = {
where: {
filename
}
}
return VideoFileModel.scope(ScopeNames.WITH_VIDEO_OR_PLAYLIST).findOne(query)
}
static loadWithVideoOrPlaylistByTorrentFilename (filename: string) {
const query = {
where: {
torrentFilename: filename
}
}
return VideoFileModel.scope(ScopeNames.WITH_VIDEO_OR_PLAYLIST).findOne(query)
}
static load (id: number): Promise<MVideoFile> {
return VideoFileModel.findByPk(id)
}
static loadWithMetadata (id: number) {
return VideoFileModel.scope(ScopeNames.WITH_METADATA).findByPk(id)
}
static loadWithVideo (id: number) {
return VideoFileModel.scope(ScopeNames.WITH_VIDEO).findByPk(id)
}
static loadWithVideoOrPlaylist (id: number, videoIdOrUUID: number | string) {
const whereVideo = validator.default.isUUID(videoIdOrUUID + '')
? { uuid: videoIdOrUUID }
: { id: videoIdOrUUID }
const options = {
where: {
id
}
}
return VideoFileModel.scope({ method: [ ScopeNames.WITH_VIDEO_OR_PLAYLIST, whereVideo ] })
.findOne(options)
.then(file => {
// We used `required: false` so check we have at least a video or a streaming playlist
if (!file.Video && !file.VideoStreamingPlaylist) return null
return file
})
}
static listByStreamingPlaylist (streamingPlaylistId: number, transaction: Transaction) {
const query = {
include: [
{
model: VideoModel.unscoped(),
required: true,
include: [
{
model: VideoStreamingPlaylistModel.unscoped(),
required: true,
where: {
id: streamingPlaylistId
}
}
]
}
],
transaction
}
return VideoFileModel.findAll(query)
}
static getStats () {
const webVideoFilesQuery: FindOptions = {
include: [
{
attributes: [],
required: true,
model: VideoModel.unscoped(),
where: {
remote: false
}
}
]
}
const hlsFilesQuery: FindOptions = {
include: [
{
attributes: [],
required: true,
model: VideoStreamingPlaylistModel.unscoped(),
include: [
{
attributes: [],
model: VideoModel.unscoped(),
required: true,
where: {
remote: false
}
}
]
}
]
}
return Promise.all([
VideoFileModel.aggregate('size', 'SUM', webVideoFilesQuery),
VideoFileModel.aggregate('size', 'SUM', hlsFilesQuery)
]).then(([ webVideoResult, hlsResult ]) => ({
totalLocalVideoFilesSize: parseAggregateResult(webVideoResult) + parseAggregateResult(hlsResult)
}))
}
// Redefine upsert because sequelize does not use an appropriate where clause in the update query with 2 unique indexes
static async customUpsert (
videoFile: MVideoFile,
mode: 'streaming-playlist' | 'video',
transaction: Transaction
) {
const baseFind = {
fps: videoFile.fps,
resolution: videoFile.resolution,
transaction
}
const element = mode === 'streaming-playlist'
? await VideoFileModel.loadHLSFile({ ...baseFind, playlistId: videoFile.videoStreamingPlaylistId })
: await VideoFileModel.loadWebVideoFile({ ...baseFind, videoId: videoFile.videoId })
if (!element) return videoFile.save({ transaction })
for (const k of Object.keys(videoFile.toJSON())) {
element.set(k, videoFile[k])
}
return element.save({ transaction })
}
static async loadWebVideoFile (options: {
videoId: number
fps: number
resolution: number
transaction?: Transaction
}) {
const where = {
fps: options.fps,
resolution: options.resolution,
videoId: options.videoId
}
return VideoFileModel.findOne({ where, transaction: options.transaction })
}
static async loadHLSFile (options: {
playlistId: number
fps: number
resolution: number
transaction?: Transaction
}) {
const where = {
fps: options.fps,
resolution: options.resolution,
videoStreamingPlaylistId: options.playlistId
}
return VideoFileModel.findOne({ where, transaction: options.transaction })
}
static removeHLSFilesOfStreamingPlaylistId (videoStreamingPlaylistId: number) {
const options = {
where: { videoStreamingPlaylistId }
}
return VideoFileModel.destroy(options)
}
hasTorrent () {
return this.infoHash && this.torrentFilename
}
getVideoOrStreamingPlaylist (this: MVideoFileVideo | MVideoFileStreamingPlaylistVideo): MVideo | MStreamingPlaylistVideo {
if (this.videoId || (this as MVideoFileVideo).Video) return (this as MVideoFileVideo).Video
return (this as MVideoFileStreamingPlaylistVideo).VideoStreamingPlaylist
}
getVideo (this: MVideoFileVideo | MVideoFileStreamingPlaylistVideo): MVideo {
return extractVideo(this.getVideoOrStreamingPlaylist())
}
isAudio () {
return this.resolution === VideoResolution.H_NOVIDEO
}
isLive () {
return this.size === -1
}
isHLS () {
return !!this.videoStreamingPlaylistId
}
// ---------------------------------------------------------------------------
getObjectStorageUrl (video: MVideo) {
if (video.hasPrivateStaticPath() && CONFIG.OBJECT_STORAGE.PROXY.PROXIFY_PRIVATE_FILES === true) {
return this.getPrivateObjectStorageUrl(video)
}
return this.getPublicObjectStorageUrl()
}
private getPrivateObjectStorageUrl (video: MVideo) {
if (this.isHLS()) {
return getHLSPrivateFileUrl(video, this.filename)
}
return getWebVideoPrivateFileUrl(this.filename)
}
private getPublicObjectStorageUrl () {
if (this.isHLS()) {
return getObjectStoragePublicFileUrl(this.fileUrl, CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS)
}
return getObjectStoragePublicFileUrl(this.fileUrl, CONFIG.OBJECT_STORAGE.WEB_VIDEOS)
}
// ---------------------------------------------------------------------------
getFileUrl (video: MVideo) {
if (video.isOwned()) {
if (this.storage === FileStorage.OBJECT_STORAGE) {
return this.getObjectStorageUrl(video)
}
return WEBSERVER.URL + this.getFileStaticPath(video)
}
return this.fileUrl
}
// ---------------------------------------------------------------------------
getFileStaticPath (video: MVideo) {
if (this.isHLS()) return this.getHLSFileStaticPath(video)
return this.getWebVideoFileStaticPath(video)
}
private getWebVideoFileStaticPath (video: MVideo) {
if (isVideoInPrivateDirectory(video.privacy)) {
return join(STATIC_PATHS.PRIVATE_WEB_VIDEOS, this.filename)
}
return join(STATIC_PATHS.WEB_VIDEOS, this.filename)
}
private getHLSFileStaticPath (video: MVideo) {
if (isVideoInPrivateDirectory(video.privacy)) {
return join(STATIC_PATHS.STREAMING_PLAYLISTS.PRIVATE_HLS, video.uuid, this.filename)
}
return join(STATIC_PATHS.STREAMING_PLAYLISTS.HLS, video.uuid, this.filename)
}
// ---------------------------------------------------------------------------
getFileDownloadUrl (video: MVideoWithHost) {
const path = this.isHLS()
? join(STATIC_DOWNLOAD_PATHS.HLS_VIDEOS, `${video.uuid}-${this.resolution}-fragmented${this.extname}`)
: join(STATIC_DOWNLOAD_PATHS.VIDEOS, `${video.uuid}-${this.resolution}${this.extname}`)
if (video.isOwned()) return WEBSERVER.URL + path
// FIXME: don't guess remote URL
return buildRemoteUrl(video, path)
}
getRemoteTorrentUrl (video: MVideo) {
if (video.isOwned()) throw new Error(`Video ${video.url} is not a remote video`)
return this.torrentUrl
}
// We proxify torrent requests so use a local URL
getTorrentUrl () {
if (!this.torrentFilename) return null
return WEBSERVER.URL + this.getTorrentStaticPath()
}
getTorrentStaticPath () {
if (!this.torrentFilename) return null
return join(LAZY_STATIC_PATHS.TORRENTS, this.torrentFilename)
}
getTorrentDownloadUrl () {
if (!this.torrentFilename) return null
return WEBSERVER.URL + join(STATIC_DOWNLOAD_PATHS.TORRENTS, this.torrentFilename)
}
removeTorrent () {
if (!this.torrentFilename) return null
const torrentPath = getFSTorrentFilePath(this)
return remove(torrentPath)
.catch(err => logger.warn('Cannot delete torrent %s.', torrentPath, { err }))
}
hasSameUniqueKeysThan (other: MVideoFile) {
return this.fps === other.fps &&
this.resolution === other.resolution &&
(
(this.videoId !== null && this.videoId === other.videoId) ||
(this.videoStreamingPlaylistId !== null && this.videoStreamingPlaylistId === other.videoStreamingPlaylistId)
)
}
withVideoOrPlaylist (videoOrPlaylist: MVideo | MStreamingPlaylistVideo) {
if (isStreamingPlaylist(videoOrPlaylist)) return Object.assign(this, { VideoStreamingPlaylist: videoOrPlaylist })
return Object.assign(this, { Video: videoOrPlaylist })
}
// ---------------------------------------------------------------------------
toActivityPubObject (this: MVideoFile, video: MVideo): ActivityVideoUrlObject {
const mimeType = getVideoFileMimeType(this.extname, false)
return {
type: 'Link',
mediaType: mimeType as ActivityVideoUrlObject['mediaType'],
href: this.getFileUrl(video),
height: this.height || this.resolution,
width: this.width,
size: this.size,
fps: this.fps
}
}
}
+272
ファイルの表示
@@ -0,0 +1,272 @@
import { VideoImport, VideoImportState, type VideoImportStateType } from '@peertube/peertube-models'
import { afterCommitIfTransaction } from '@server/helpers/database-utils.js'
import { MVideoImportDefault, MVideoImportFormattable } from '@server/types/models/video/video-import.js'
import { IncludeOptions, Op, WhereOptions } from 'sequelize'
import {
AfterUpdate,
AllowNull,
BelongsTo,
Column,
CreatedAt,
DataType,
Default,
DefaultScope,
ForeignKey,
Is, Table,
UpdatedAt
} from 'sequelize-typescript'
import { isVideoImportStateValid, isVideoImportTargetUrlValid } from '../../helpers/custom-validators/video-imports.js'
import { isVideoMagnetUriValid } from '../../helpers/custom-validators/videos.js'
import { CONSTRAINTS_FIELDS, VIDEO_IMPORT_STATES } from '../../initializers/constants.js'
import { SequelizeModel, getSort, searchAttribute, throwIfNotValid } from '../shared/index.js'
import { UserModel } from '../user/user.js'
import { VideoChannelSyncModel } from './video-channel-sync.js'
import { VideoModel, ScopeNames as VideoModelScopeNames } from './video.js'
const defaultVideoScope = () => {
return VideoModel.scope([
VideoModelScopeNames.WITH_ACCOUNT_DETAILS,
VideoModelScopeNames.WITH_TAGS,
VideoModelScopeNames.WITH_THUMBNAILS
])
}
@DefaultScope(() => ({
include: [
{
model: UserModel.unscoped(),
required: true
},
{
model: defaultVideoScope(),
required: false
},
{
model: VideoChannelSyncModel.unscoped(),
required: false
}
]
}))
@Table({
tableName: 'videoImport',
indexes: [
{
fields: [ 'videoId' ],
unique: true
},
{
fields: [ 'userId' ]
}
]
})
export class VideoImportModel extends SequelizeModel<VideoImportModel> {
@CreatedAt
createdAt: Date
@UpdatedAt
updatedAt: Date
@AllowNull(true)
@Default(null)
@Is('VideoImportTargetUrl', value => throwIfNotValid(value, isVideoImportTargetUrlValid, 'targetUrl', true))
@Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_IMPORTS.URL.max))
targetUrl: string
@AllowNull(true)
@Default(null)
@Is('VideoImportMagnetUri', value => throwIfNotValid(value, isVideoMagnetUriValid, 'magnetUri', true))
@Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_IMPORTS.URL.max)) // Use the same constraints than URLs
magnetUri: string
@AllowNull(true)
@Default(null)
@Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_IMPORTS.TORRENT_NAME.max))
torrentName: string
@AllowNull(false)
@Default(null)
@Is('VideoImportState', value => throwIfNotValid(value, isVideoImportStateValid, 'state'))
@Column
state: VideoImportStateType
@AllowNull(true)
@Default(null)
@Column(DataType.TEXT)
error: string
@ForeignKey(() => UserModel)
@Column
userId: number
@BelongsTo(() => UserModel, {
foreignKey: {
allowNull: false
},
onDelete: 'cascade'
})
User: Awaited<UserModel>
@ForeignKey(() => VideoModel)
@Column
videoId: number
@BelongsTo(() => VideoModel, {
foreignKey: {
allowNull: true
},
onDelete: 'set null'
})
Video: Awaited<VideoModel>
@ForeignKey(() => VideoChannelSyncModel)
@Column
videoChannelSyncId: number
@BelongsTo(() => VideoChannelSyncModel, {
foreignKey: {
allowNull: true
},
onDelete: 'set null'
})
VideoChannelSync: Awaited<VideoChannelSyncModel>
@AfterUpdate
static deleteVideoIfFailed (instance: VideoImportModel, options) {
if (instance.state === VideoImportState.FAILED) {
return afterCommitIfTransaction(options.transaction, () => instance.Video.destroy())
}
return undefined
}
static loadAndPopulateVideo (id: number): Promise<MVideoImportDefault> {
return VideoImportModel.findByPk(id)
}
static listUserVideoImportsForApi (options: {
userId: number
start: number
count: number
sort: string
search?: string
targetUrl?: string
videoChannelSyncId?: number
}) {
const { userId, start, count, sort, targetUrl, videoChannelSyncId, search } = options
const where: WhereOptions = [ { userId } ]
const include: IncludeOptions[] = [
{
attributes: [ 'id' ],
model: UserModel.unscoped(), // FIXME: Without this, sequelize try to COUNT(DISTINCT(*)) which is an invalid SQL query
required: true
},
{
model: VideoChannelSyncModel.unscoped(),
required: false
}
]
if (targetUrl) where.push({ targetUrl })
if (videoChannelSyncId) where.push({ videoChannelSyncId })
if (search) {
include.push({
model: defaultVideoScope(),
required: false
})
where.push({
[Op.or]: [
searchAttribute(search, '$Video.name$'),
searchAttribute(search, 'targetUrl'),
searchAttribute(search, 'torrentName'),
searchAttribute(search, 'magnetUri')
]
})
} else {
include.push({
model: defaultVideoScope(),
required: false
})
}
const query = {
distinct: true,
include,
offset: start,
limit: count,
order: getSort(sort),
where
}
return Promise.all([
VideoImportModel.unscoped().count(query),
VideoImportModel.findAll<MVideoImportDefault>(query)
]).then(([ total, data ]) => ({ total, data }))
}
static async urlAlreadyImported (channelId: number, targetUrl: string): Promise<boolean> {
const element = await VideoImportModel.unscoped().findOne({
where: {
targetUrl,
state: {
[Op.in]: [ VideoImportState.PENDING, VideoImportState.PROCESSING, VideoImportState.SUCCESS ]
}
},
include: [
{
model: VideoModel,
required: true,
where: {
channelId
}
}
]
})
return !!element
}
getTargetIdentifier () {
return this.targetUrl || this.magnetUri || this.torrentName
}
toFormattedJSON (this: MVideoImportFormattable): VideoImport {
const videoFormatOptions = {
completeDescription: true,
additionalAttributes: { state: true, waitTranscoding: true, scheduledUpdate: true }
}
const video = this.Video
? Object.assign(this.Video.toFormattedJSON(videoFormatOptions), { tags: this.Video.Tags.map(t => t.name) })
: undefined
const videoChannelSync = this.VideoChannelSync
? { id: this.VideoChannelSync.id, externalChannelUrl: this.VideoChannelSync.externalChannelUrl }
: undefined
return {
id: this.id,
targetUrl: this.targetUrl,
magnetUri: this.magnetUri,
torrentName: this.torrentName,
state: {
id: this.state,
label: VideoImportModel.getStateLabel(this.state)
},
error: this.error,
updatedAt: this.updatedAt.toISOString(),
createdAt: this.createdAt.toISOString(),
video,
videoChannelSync
}
}
private static getStateLabel (id: number) {
return VIDEO_IMPORT_STATES[id] || 'Unknown'
}
}
+127
ファイルの表示
@@ -0,0 +1,127 @@
import { forceNumber } from '@peertube/peertube-core-utils'
import { Op, QueryTypes, Transaction } from 'sequelize'
import { AllowNull, BelongsTo, Column, CreatedAt, Default, ForeignKey, IsInt, Table, Unique, UpdatedAt } from 'sequelize-typescript'
import { SequelizeModel } from '../shared/sequelize-type.js'
import { VideoModel } from './video.js'
export type VideoJobInfoColumnType = 'pendingMove' | 'pendingTranscode' | 'pendingTranscription'
@Table({
tableName: 'videoJobInfo',
indexes: [
{
fields: [ 'videoId' ],
where: {
videoId: {
[Op.ne]: null
}
}
}
]
})
export class VideoJobInfoModel extends SequelizeModel<VideoJobInfoModel> {
@CreatedAt
createdAt: Date
@UpdatedAt
updatedAt: Date
@AllowNull(false)
@Default(0)
@IsInt
@Column
pendingMove: number
@AllowNull(false)
@Default(0)
@IsInt
@Column
pendingTranscode: number
@AllowNull(false)
@Default(0)
@IsInt
@Column
pendingTranscription: number
@ForeignKey(() => VideoModel)
@Unique
@Column
videoId: number
@BelongsTo(() => VideoModel, {
foreignKey: {
allowNull: false
},
onDelete: 'cascade'
})
Video: Awaited<VideoModel>
static load (videoId: number, transaction?: Transaction) {
const where = {
videoId
}
return VideoJobInfoModel.findOne({ where, transaction })
}
static async increaseOrCreate (videoUUID: string, column: VideoJobInfoColumnType, amountArg = 1): Promise<number> {
const options = { type: QueryTypes.SELECT as QueryTypes.SELECT, bind: { videoUUID } }
const amount = forceNumber(amountArg)
const [ result ] = await VideoJobInfoModel.sequelize.query<{ pendingMove: number }>(`
INSERT INTO "videoJobInfo" ("videoId", "${column}", "createdAt", "updatedAt")
SELECT
"video"."id" AS "videoId", ${amount}, NOW(), NOW()
FROM
"video"
WHERE
"video"."uuid" = $videoUUID
ON CONFLICT ("videoId") DO UPDATE
SET
"${column}" = "videoJobInfo"."${column}" + ${amount},
"updatedAt" = NOW()
RETURNING
"${column}"
`, options)
return result[column]
}
static async decrease (videoUUID: string, column: VideoJobInfoColumnType): Promise<number> {
const options = { type: QueryTypes.SELECT as QueryTypes.SELECT, bind: { videoUUID } }
const result = await VideoJobInfoModel.sequelize.query(`
UPDATE
"videoJobInfo"
SET
"${column}" = "videoJobInfo"."${column}" - 1,
"updatedAt" = NOW()
FROM "video"
WHERE
"video"."id" = "videoJobInfo"."videoId" AND "video"."uuid" = $videoUUID
RETURNING
"${column}";
`, options)
if (result.length === 0) return undefined
return result[0][column]
}
static async abortAllTasks (videoUUID: string, column: VideoJobInfoColumnType): Promise<void> {
const options = { type: QueryTypes.UPDATE as QueryTypes.UPDATE, bind: { videoUUID } }
await VideoJobInfoModel.sequelize.query(`
UPDATE
"videoJobInfo"
SET
"${column}" = 0,
"updatedAt" = NOW()
FROM "video"
WHERE
"video"."id" = "videoJobInfo"."videoId" AND "video"."uuid" = $videoUUID
`, options)
}
}
+43
ファイルの表示
@@ -0,0 +1,43 @@
import { type VideoPrivacyType } from '@peertube/peertube-models'
import { isVideoPrivacyValid } from '@server/helpers/custom-validators/videos.js'
import { MLiveReplaySetting } from '@server/types/models/video/video-live-replay-setting.js'
import { Transaction } from 'sequelize'
import { AllowNull, Column, CreatedAt, Is, Table, UpdatedAt } from 'sequelize-typescript'
import { throwIfNotValid } from '../shared/sequelize-helpers.js'
import { SequelizeModel } from '../shared/index.js'
@Table({
tableName: 'videoLiveReplaySetting'
})
export class VideoLiveReplaySettingModel extends SequelizeModel<VideoLiveReplaySettingModel> {
@CreatedAt
createdAt: Date
@UpdatedAt
updatedAt: Date
@AllowNull(false)
@Is('VideoPrivacy', value => throwIfNotValid(value, isVideoPrivacyValid, 'privacy'))
@Column
privacy: VideoPrivacyType
static load (id: number, transaction?: Transaction): Promise<MLiveReplaySetting> {
return VideoLiveReplaySettingModel.findOne({
where: { id },
transaction
})
}
static removeSettings (id: number) {
return VideoLiveReplaySettingModel.destroy({
where: { id }
})
}
toFormattedJSON () {
return {
privacy: this.privacy
}
}
}
+216
ファイルの表示
@@ -0,0 +1,216 @@
import { LiveVideoSession, type LiveVideoErrorType } from '@peertube/peertube-models'
import { uuidToShort } from '@peertube/peertube-node-utils'
import { AttributesOnly } from '@peertube/peertube-typescript-utils'
import { MVideoLiveSession, MVideoLiveSessionReplay } from '@server/types/models/index.js'
import { FindOptions } from 'sequelize'
import {
AllowNull,
BeforeDestroy,
BelongsTo,
Column,
CreatedAt,
DataType,
ForeignKey, Scopes,
Table,
UpdatedAt
} from 'sequelize-typescript'
import { VideoLiveReplaySettingModel } from './video-live-replay-setting.js'
import { VideoModel } from './video.js'
import { SequelizeModel } from '../shared/index.js'
export enum ScopeNames {
WITH_REPLAY = 'WITH_REPLAY'
}
@Scopes(() => ({
[ScopeNames.WITH_REPLAY]: {
include: [
{
model: VideoModel.unscoped(),
as: 'ReplayVideo',
required: false
},
{
model: VideoLiveReplaySettingModel,
required: false
}
]
}
}))
@Table({
tableName: 'videoLiveSession',
indexes: [
{
fields: [ 'replayVideoId' ],
unique: true
},
{
fields: [ 'liveVideoId' ]
},
{
fields: [ 'replaySettingId' ],
unique: true
}
]
})
export class VideoLiveSessionModel extends SequelizeModel<VideoLiveSessionModel> {
@CreatedAt
createdAt: Date
@UpdatedAt
updatedAt: Date
@AllowNull(false)
@Column(DataType.DATE)
startDate: Date
@AllowNull(true)
@Column(DataType.DATE)
endDate: Date
@AllowNull(true)
@Column
error: LiveVideoErrorType
@AllowNull(false)
@Column
saveReplay: boolean
@AllowNull(false)
@Column
endingProcessed: boolean
@ForeignKey(() => VideoModel)
@Column
replayVideoId: number
@BelongsTo(() => VideoModel, {
foreignKey: {
allowNull: true,
name: 'replayVideoId'
},
as: 'ReplayVideo',
onDelete: 'set null'
})
ReplayVideo: Awaited<VideoModel>
@ForeignKey(() => VideoModel)
@Column
liveVideoId: number
@BelongsTo(() => VideoModel, {
foreignKey: {
allowNull: true,
name: 'liveVideoId'
},
as: 'LiveVideo',
onDelete: 'set null'
})
LiveVideo: Awaited<VideoModel>
@ForeignKey(() => VideoLiveReplaySettingModel)
@Column
replaySettingId: number
@BelongsTo(() => VideoLiveReplaySettingModel, {
foreignKey: {
allowNull: true
},
onDelete: 'set null'
})
ReplaySetting: Awaited<VideoLiveReplaySettingModel>
@BeforeDestroy
static deleteReplaySetting (instance: VideoLiveSessionModel) {
return VideoLiveReplaySettingModel.destroy({
where: {
id: instance.replaySettingId
}
})
}
static load (id: number): Promise<MVideoLiveSession> {
return VideoLiveSessionModel.findOne({
where: { id }
})
}
static findSessionOfReplay (replayVideoId: number) {
const query = {
where: {
replayVideoId
}
}
return VideoLiveSessionModel.scope(ScopeNames.WITH_REPLAY).findOne(query)
}
static findCurrentSessionOf (videoUUID: string) {
return VideoLiveSessionModel.findOne({
where: {
endDate: null
},
include: [
{
model: VideoModel.unscoped(),
as: 'LiveVideo',
required: true,
where: {
uuid: videoUUID
}
}
],
order: [ [ 'startDate', 'DESC' ] ]
})
}
static findLatestSessionOf (videoId: number) {
return VideoLiveSessionModel.findOne({
where: {
liveVideoId: videoId
},
order: [ [ 'startDate', 'DESC' ] ]
})
}
static listSessionsOfLiveForAPI (options: { videoId: number }) {
const { videoId } = options
const query: FindOptions<AttributesOnly<VideoLiveSessionModel>> = {
where: {
liveVideoId: videoId
},
order: [ [ 'startDate', 'ASC' ] ]
}
return VideoLiveSessionModel.scope(ScopeNames.WITH_REPLAY).findAll(query)
}
toFormattedJSON (this: MVideoLiveSessionReplay): LiveVideoSession {
const replayVideo = this.ReplayVideo
? {
id: this.ReplayVideo.id,
uuid: this.ReplayVideo.uuid,
shortUUID: uuidToShort(this.ReplayVideo.uuid)
}
: undefined
const replaySettings = this.replaySettingId
? this.ReplaySetting.toFormattedJSON()
: undefined
return {
id: this.id,
startDate: this.startDate.toISOString(),
endDate: this.endDate
? this.endDate.toISOString()
: null,
endingProcessed: this.endingProcessed,
saveReplay: this.saveReplay,
replaySettings,
replayVideo,
error: this.error
}
}
}
+198
ファイルの表示
@@ -0,0 +1,198 @@
import { LiveVideo, VideoState, type LiveVideoLatencyModeType } from '@peertube/peertube-models'
import { CONFIG } from '@server/initializers/config.js'
import { WEBSERVER } from '@server/initializers/constants.js'
import { MVideoLive, MVideoLiveVideoWithSetting, MVideoLiveWithSetting } from '@server/types/models/index.js'
import { Transaction } from 'sequelize'
import {
AllowNull,
BeforeDestroy,
BelongsTo,
Column,
CreatedAt,
DataType,
DefaultScope,
ForeignKey, Table,
UpdatedAt
} from 'sequelize-typescript'
import { VideoBlacklistModel } from './video-blacklist.js'
import { VideoLiveReplaySettingModel } from './video-live-replay-setting.js'
import { VideoModel } from './video.js'
import { SequelizeModel } from '../shared/index.js'
@DefaultScope(() => ({
include: [
{
model: VideoModel,
required: true,
include: [
{
model: VideoBlacklistModel,
required: false
}
]
},
{
model: VideoLiveReplaySettingModel,
required: false
}
]
}))
@Table({
tableName: 'videoLive',
indexes: [
{
fields: [ 'videoId' ],
unique: true
},
{
fields: [ 'replaySettingId' ],
unique: true
}
]
})
export class VideoLiveModel extends SequelizeModel<VideoLiveModel> {
@AllowNull(true)
@Column(DataType.STRING)
streamKey: string
@AllowNull(false)
@Column
saveReplay: boolean
@AllowNull(false)
@Column
permanentLive: boolean
@AllowNull(false)
@Column
latencyMode: LiveVideoLatencyModeType
@CreatedAt
createdAt: Date
@UpdatedAt
updatedAt: Date
@ForeignKey(() => VideoModel)
@Column
videoId: number
@BelongsTo(() => VideoModel, {
foreignKey: {
allowNull: false
},
onDelete: 'cascade'
})
Video: Awaited<VideoModel>
@ForeignKey(() => VideoLiveReplaySettingModel)
@Column
replaySettingId: number
@BelongsTo(() => VideoLiveReplaySettingModel, {
foreignKey: {
allowNull: true
},
onDelete: 'set null'
})
ReplaySetting: Awaited<VideoLiveReplaySettingModel>
@BeforeDestroy
static deleteReplaySetting (instance: VideoLiveModel, options: { transaction: Transaction }) {
return VideoLiveReplaySettingModel.destroy({
where: {
id: instance.replaySettingId
},
transaction: options.transaction
})
}
static loadByStreamKey (streamKey: string) {
const query = {
where: {
streamKey
},
include: [
{
model: VideoModel.unscoped(),
required: true,
where: {
state: VideoState.WAITING_FOR_LIVE
},
include: [
{
model: VideoBlacklistModel.unscoped(),
required: false
}
]
},
{
model: VideoLiveReplaySettingModel.unscoped(),
required: false
}
]
}
return VideoLiveModel.findOne<MVideoLiveVideoWithSetting>(query)
}
static loadByVideoId (videoId: number) {
const query = {
where: {
videoId
}
}
return VideoLiveModel.findOne<MVideoLive>(query)
}
static loadByVideoIdWithSettings (videoId: number) {
const query = {
where: {
videoId
},
include: [
{
model: VideoLiveReplaySettingModel.unscoped(),
required: false
}
]
}
return VideoLiveModel.findOne<MVideoLiveWithSetting>(query)
}
toFormattedJSON (canSeePrivateInformation: boolean): LiveVideo {
let privateInformation: Pick<LiveVideo, 'rtmpUrl' | 'rtmpsUrl' | 'streamKey'> | {} = {}
// If we don't have a stream key, it means this is a remote live so we don't specify the rtmp URL
// We also display these private information only to the live owne/moderators
if (this.streamKey && canSeePrivateInformation === true) {
privateInformation = {
streamKey: this.streamKey,
rtmpUrl: CONFIG.LIVE.RTMP.ENABLED
? WEBSERVER.RTMP_BASE_LIVE_URL
: null,
rtmpsUrl: CONFIG.LIVE.RTMPS.ENABLED
? WEBSERVER.RTMPS_BASE_LIVE_URL
: null
}
}
const replaySettings = this.replaySettingId
? this.ReplaySetting.toFormattedJSON()
: undefined
return {
...privateInformation,
permanentLive: this.permanentLive,
saveReplay: this.saveReplay,
replaySettings,
latencyMode: this.latencyMode
}
}
}
+136
ファイルの表示
@@ -0,0 +1,136 @@
import { AllowNull, BelongsTo, Column, CreatedAt, DefaultScope, ForeignKey, Is, Table, UpdatedAt } from 'sequelize-typescript'
import { VideoModel } from './video.js'
import { ResultList, VideoPassword } from '@peertube/peertube-models'
import { SequelizeModel, getSort, throwIfNotValid } from '../shared/index.js'
import { Transaction } from 'sequelize'
import { MVideoPassword } from '@server/types/models/index.js'
import { isPasswordValid } from '@server/helpers/custom-validators/videos.js'
import { pick } from '@peertube/peertube-core-utils'
@DefaultScope(() => ({
include: [
{
model: VideoModel.unscoped(),
required: true
}
]
}))
@Table({
tableName: 'videoPassword',
indexes: [
{
fields: [ 'videoId', 'password' ],
unique: true
}
]
})
export class VideoPasswordModel extends SequelizeModel<VideoPasswordModel> {
@AllowNull(false)
@Is('VideoPassword', value => throwIfNotValid(value, isPasswordValid, 'videoPassword'))
@Column
password: string
@CreatedAt
createdAt: Date
@UpdatedAt
updatedAt: Date
@ForeignKey(() => VideoModel)
@Column
videoId: number
@BelongsTo(() => VideoModel, {
foreignKey: {
allowNull: false
},
onDelete: 'cascade'
})
Video: Awaited<VideoModel>
static async countByVideoId (videoId: number, t?: Transaction) {
const query = {
where: {
videoId
},
transaction: t
}
return VideoPasswordModel.count(query)
}
static async loadByIdAndVideo (options: { id: number, videoId: number, t?: Transaction }): Promise<MVideoPassword> {
const { id, videoId, t } = options
const query = {
where: {
id,
videoId
},
transaction: t
}
return VideoPasswordModel.findOne(query)
}
static async listPasswords (options: {
start: number
count: number
sort: string
videoId: number
}): Promise<ResultList<MVideoPassword>> {
const { start, count, sort, videoId } = options
const { count: total, rows: data } = await VideoPasswordModel.findAndCountAll({
where: { videoId },
order: getSort(sort),
offset: start,
limit: count
})
return { total, data }
}
static async addPasswords (passwords: string[], videoId: number, transaction?: Transaction): Promise<void> {
for (const password of passwords) {
await VideoPasswordModel.create({
password,
videoId
}, { transaction })
}
}
static async deleteAllPasswords (videoId: number, transaction?: Transaction) {
await VideoPasswordModel.destroy({
where: { videoId },
transaction
})
}
static async deletePassword (passwordId: number, transaction?: Transaction) {
await VideoPasswordModel.destroy({
where: { id: passwordId },
transaction
})
}
static async isACorrectPassword (options: {
videoId: number
password: string
}) {
const query = {
where: pick(options, [ 'videoId', 'password' ])
}
return VideoPasswordModel.findOne(query)
}
toFormattedJSON (): VideoPassword {
return {
id: this.id,
password: this.password,
videoId: this.videoId,
createdAt: this.createdAt,
updatedAt: this.updatedAt
}
}
}
+394
ファイルの表示
@@ -0,0 +1,394 @@
import { AggregateOptions, Op, ScopeOptions, Sequelize, Transaction } from 'sequelize'
import {
AllowNull,
BelongsTo,
Column,
CreatedAt,
DataType,
Default,
ForeignKey,
Is,
IsInt,
Min, Table,
UpdatedAt
} from 'sequelize-typescript'
import validator from 'validator'
import { forceNumber } from '@peertube/peertube-core-utils'
import {
PlaylistElementObject,
VideoPlaylistElement,
VideoPlaylistElementType,
VideoPrivacy,
VideoPrivacyType
} from '@peertube/peertube-models'
import { MUserAccountId } from '@server/types/models/index.js'
import {
MVideoPlaylistElement,
MVideoPlaylistElementAP,
MVideoPlaylistElementFormattable,
MVideoPlaylistElementVideoUrlPlaylistPrivacy,
MVideoPlaylistElementVideoThumbnail,
MVideoPlaylistElementVideoUrl
} from '@server/types/models/video/video-playlist-element.js'
import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc.js'
import { CONSTRAINTS_FIELDS, USER_EXPORT_MAX_ITEMS } from '../../initializers/constants.js'
import { AccountModel } from '../account/account.js'
import { SequelizeModel, getSort, throwIfNotValid } from '../shared/index.js'
import { VideoPlaylistModel } from './video-playlist.js'
import { ForAPIOptions, ScopeNames as VideoScopeNames, VideoModel } from './video.js'
@Table({
tableName: 'videoPlaylistElement',
indexes: [
{
fields: [ 'videoPlaylistId' ]
},
{
fields: [ 'videoId' ]
},
{
fields: [ 'url' ],
unique: true
}
]
})
export class VideoPlaylistElementModel extends SequelizeModel<VideoPlaylistElementModel> {
@CreatedAt
createdAt: Date
@UpdatedAt
updatedAt: Date
@AllowNull(true)
@Is('VideoPlaylistUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url', true))
@Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_PLAYLISTS.URL.max))
url: string
@AllowNull(false)
@Default(1)
@IsInt
@Min(1)
@Column
position: number
@AllowNull(true)
@IsInt
@Min(0)
@Column
startTimestamp: number
@AllowNull(true)
@IsInt
@Min(0)
@Column
stopTimestamp: number
@ForeignKey(() => VideoPlaylistModel)
@Column
videoPlaylistId: number
@BelongsTo(() => VideoPlaylistModel, {
foreignKey: {
allowNull: false
},
onDelete: 'CASCADE'
})
VideoPlaylist: Awaited<VideoPlaylistModel>
@ForeignKey(() => VideoModel)
@Column
videoId: number
@BelongsTo(() => VideoModel, {
foreignKey: {
allowNull: true
},
onDelete: 'set null'
})
Video: Awaited<VideoModel>
static deleteAllOf (videoPlaylistId: number, transaction?: Transaction) {
const query = {
where: {
videoPlaylistId
},
transaction
}
return VideoPlaylistElementModel.destroy(query)
}
static listForApi (options: {
start: number
count: number
videoPlaylistId: number
serverAccount: AccountModel
user?: MUserAccountId
}) {
const accountIds = [ options.serverAccount.id ]
const videoScope: (ScopeOptions | string)[] = [
VideoScopeNames.WITH_BLACKLISTED
]
if (options.user) {
accountIds.push(options.user.Account.id)
videoScope.push({ method: [ VideoScopeNames.WITH_USER_HISTORY, options.user.id ] })
}
const forApiOptions: ForAPIOptions = { withAccountBlockerIds: accountIds }
videoScope.push({
method: [
VideoScopeNames.FOR_API, forApiOptions
]
})
const findQuery = {
offset: options.start,
limit: options.count,
order: getSort('position'),
where: {
videoPlaylistId: options.videoPlaylistId
},
include: [
{
model: VideoModel.scope(videoScope),
required: false
}
]
}
const countQuery = {
where: {
videoPlaylistId: options.videoPlaylistId
}
}
return Promise.all([
VideoPlaylistElementModel.count(countQuery),
VideoPlaylistElementModel.findAll(findQuery)
]).then(([ total, data ]) => ({ total, data }))
}
static loadByPlaylistAndVideo (videoPlaylistId: number, videoId: number): Promise<MVideoPlaylistElement> {
const query = {
where: {
videoPlaylistId,
videoId
}
}
return VideoPlaylistElementModel.findOne(query)
}
static loadById (playlistElementId: number | string): Promise<MVideoPlaylistElement> {
return VideoPlaylistElementModel.findByPk(playlistElementId)
}
static loadByPlaylistAndElementIdForAP (
playlistId: number | string,
playlistElementId: number
): Promise<MVideoPlaylistElementVideoUrlPlaylistPrivacy> {
const playlistWhere = validator.default.isUUID('' + playlistId)
? { uuid: playlistId }
: { id: playlistId }
const query = {
include: [
{
attributes: [ 'privacy' ],
model: VideoPlaylistModel.unscoped(),
where: playlistWhere
},
{
attributes: [ 'url' ],
model: VideoModel.unscoped()
}
],
where: {
id: playlistElementId
}
}
return VideoPlaylistElementModel.findOne(query)
}
static loadFirstElementWithVideoThumbnail (videoPlaylistId: number): Promise<MVideoPlaylistElementVideoThumbnail> {
const query = {
order: getSort('position'),
where: {
videoPlaylistId
},
include: [
{
model: VideoModel.scope(VideoScopeNames.WITH_THUMBNAILS),
required: true
}
]
}
return VideoPlaylistElementModel
.findOne(query)
}
// ---------------------------------------------------------------------------
static listUrlsOfForAP (videoPlaylistId: number, start: number, count: number, t?: Transaction) {
const getQuery = (forCount: boolean) => {
return {
attributes: forCount
? []
: [ 'url' ],
offset: start,
limit: count,
order: getSort('position'),
where: {
videoPlaylistId
},
transaction: t
}
}
return Promise.all([
VideoPlaylistElementModel.count(getQuery(true)),
VideoPlaylistElementModel.findAll(getQuery(false))
]).then(([ total, rows ]) => ({
total,
data: rows.map(e => e.url)
}))
}
static listElementsForExport (videoPlaylistId: number): Promise<MVideoPlaylistElementVideoUrl[]> {
return VideoPlaylistElementModel.findAll({
where: {
videoPlaylistId
},
include: [
{
attributes: [ 'url' ],
model: VideoModel.unscoped(),
required: true
}
],
order: getSort('position'),
limit: USER_EXPORT_MAX_ITEMS
})
}
// ---------------------------------------------------------------------------
static getNextPositionOf (videoPlaylistId: number, transaction?: Transaction) {
const query: AggregateOptions<number> = {
where: {
videoPlaylistId
},
transaction
}
return VideoPlaylistElementModel.max('position', query)
.then(position => position ? position + 1 : 1)
}
static reassignPositionOf (options: {
videoPlaylistId: number
firstPosition: number
endPosition: number
newPosition: number
transaction?: Transaction
}) {
const { videoPlaylistId, firstPosition, endPosition, newPosition, transaction } = options
const query = {
where: {
videoPlaylistId,
position: {
[Op.gte]: firstPosition,
[Op.lte]: endPosition
}
},
transaction,
validate: false // We use a literal to update the position
}
const positionQuery = Sequelize.literal(`${forceNumber(newPosition)} + "position" - ${forceNumber(firstPosition)}`)
return VideoPlaylistElementModel.update({ position: positionQuery }, query)
}
static increasePositionOf (
videoPlaylistId: number,
fromPosition: number,
by = 1,
transaction?: Transaction
) {
const query = {
where: {
videoPlaylistId,
position: {
[Op.gte]: fromPosition
}
},
transaction
}
return VideoPlaylistElementModel.increment({ position: by }, query)
}
toFormattedJSON (
this: MVideoPlaylistElementFormattable,
options: { accountId?: number } = {}
): VideoPlaylistElement {
return {
id: this.id,
position: this.position,
startTimestamp: this.startTimestamp,
stopTimestamp: this.stopTimestamp,
type: this.getType(options.accountId),
video: this.getVideoElement(options.accountId)
}
}
getType (this: MVideoPlaylistElementFormattable, accountId?: number) {
const video = this.Video
if (!video) return VideoPlaylistElementType.DELETED
// Owned video, don't filter it
if (accountId && video.VideoChannel.Account.id === accountId) return VideoPlaylistElementType.REGULAR
// Internal video?
if (video.privacy === VideoPrivacy.INTERNAL && accountId) return VideoPlaylistElementType.REGULAR
// Private, internal and password protected videos cannot be read without appropriate access (ownership, internal)
const protectedPrivacy = new Set<VideoPrivacyType>([ VideoPrivacy.PRIVATE, VideoPrivacy.INTERNAL, VideoPrivacy.PASSWORD_PROTECTED ])
if (protectedPrivacy.has(video.privacy)) {
return VideoPlaylistElementType.PRIVATE
}
if (video.isBlacklisted() || video.isBlocked()) return VideoPlaylistElementType.UNAVAILABLE
return VideoPlaylistElementType.REGULAR
}
getVideoElement (this: MVideoPlaylistElementFormattable, accountId?: number) {
if (!this.Video) return null
if (this.getType(accountId) !== VideoPlaylistElementType.REGULAR) return null
return this.Video.toFormattedJSON()
}
toActivityPubObject (this: MVideoPlaylistElementAP): PlaylistElementObject {
const base: PlaylistElementObject = {
id: this.url,
type: 'PlaylistElement',
url: this.Video?.url || null,
position: this.position
}
if (this.startTimestamp) base.startTimestamp = this.startTimestamp
if (this.stopTimestamp) base.stopTimestamp = this.stopTimestamp
return base
}
}
+779
ファイルの表示
@@ -0,0 +1,779 @@
import { buildPlaylistEmbedPath, buildPlaylistWatchPath, pick } from '@peertube/peertube-core-utils'
import {
ActivityIconObject,
PlaylistObject,
VideoPlaylist,
VideoPlaylistPrivacy,
VideoPlaylistType,
type VideoPlaylistPrivacyType,
type VideoPlaylistType_Type
} from '@peertube/peertube-models'
import { buildUUID, uuidToShort } from '@peertube/peertube-node-utils'
import { activityPubCollectionPagination } from '@server/lib/activitypub/collection.js'
import { MAccountId, MChannelId, MVideoPlaylistElement } from '@server/types/models/index.js'
import { join } from 'path'
import { FindOptions, Includeable, Op, ScopeOptions, Sequelize, Transaction, WhereOptions, literal } from 'sequelize'
import {
AllowNull,
BelongsTo,
Column,
CreatedAt,
DataType,
Default,
ForeignKey,
HasMany,
HasOne,
Is,
IsUUID, Scopes,
Table,
UpdatedAt
} from 'sequelize-typescript'
import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc.js'
import {
isVideoPlaylistDescriptionValid,
isVideoPlaylistNameValid,
isVideoPlaylistPrivacyValid
} from '../../helpers/custom-validators/video-playlists.js'
import {
ACTIVITY_PUB,
CONSTRAINTS_FIELDS,
LAZY_STATIC_PATHS,
THUMBNAILS_SIZE,
USER_EXPORT_MAX_ITEMS,
VIDEO_PLAYLIST_PRIVACIES,
VIDEO_PLAYLIST_TYPES,
WEBSERVER
} from '../../initializers/constants.js'
import { MThumbnail } from '../../types/models/video/thumbnail.js'
import {
MVideoPlaylist,
MVideoPlaylistAP,
MVideoPlaylistAccountThumbnail,
MVideoPlaylistFormattable,
MVideoPlaylistFull,
MVideoPlaylistFullSummary,
MVideoPlaylistSummaryWithElements
} from '../../types/models/video/video-playlist.js'
import { AccountModel, ScopeNames as AccountScopeNames, SummaryOptions } from '../account/account.js'
import { ActorModel } from '../actor/actor.js'
import {
SequelizeModel,
buildServerIdsFollowedBy,
buildTrigramSearchIndex,
buildWhereIdOrUUID,
createSimilarityAttribute,
getPlaylistSort,
isOutdated,
setAsUpdated,
throwIfNotValid
} from '../shared/index.js'
import { ThumbnailModel } from './thumbnail.js'
import { VideoChannelModel, ScopeNames as VideoChannelScopeNames } from './video-channel.js'
import { VideoPlaylistElementModel } from './video-playlist-element.js'
enum ScopeNames {
AVAILABLE_FOR_LIST = 'AVAILABLE_FOR_LIST',
WITH_VIDEOS_LENGTH = 'WITH_VIDEOS_LENGTH',
WITH_ACCOUNT_AND_CHANNEL_SUMMARY = 'WITH_ACCOUNT_AND_CHANNEL_SUMMARY',
WITH_ACCOUNT = 'WITH_ACCOUNT',
WITH_THUMBNAIL = 'WITH_THUMBNAIL',
WITH_ACCOUNT_AND_CHANNEL = 'WITH_ACCOUNT_AND_CHANNEL'
}
type AvailableForListOptions = {
followerActorId?: number
type?: VideoPlaylistType_Type
accountId?: number
videoChannelId?: number
listMyPlaylists?: boolean
search?: string
host?: string
uuids?: string[]
withVideos?: boolean
forCount?: boolean
}
function getVideoLengthSelect () {
return 'SELECT COUNT("id") FROM "videoPlaylistElement" WHERE "videoPlaylistId" = "VideoPlaylistModel"."id"'
}
@Scopes(() => ({
[ScopeNames.WITH_THUMBNAIL]: {
include: [
{
model: ThumbnailModel,
required: false
}
]
},
[ScopeNames.WITH_VIDEOS_LENGTH]: {
attributes: {
include: [
[
literal(`(${getVideoLengthSelect()})`),
'videosLength'
]
]
}
} as FindOptions,
[ScopeNames.WITH_ACCOUNT]: {
include: [
{
model: AccountModel,
required: true
}
]
},
[ScopeNames.WITH_ACCOUNT_AND_CHANNEL_SUMMARY]: {
include: [
{
model: AccountModel.scope(AccountScopeNames.SUMMARY),
required: true
},
{
model: VideoChannelModel.scope(VideoChannelScopeNames.SUMMARY),
required: false
}
]
},
[ScopeNames.WITH_ACCOUNT_AND_CHANNEL]: {
include: [
{
model: AccountModel,
required: true
},
{
model: VideoChannelModel,
required: false
}
]
},
[ScopeNames.AVAILABLE_FOR_LIST]: (options: AvailableForListOptions) => {
const whereAnd: WhereOptions[] = []
const whereServer = options.host && options.host !== WEBSERVER.HOST
? { host: options.host }
: undefined
let whereActor: WhereOptions = {}
if (options.host === WEBSERVER.HOST) {
whereActor = {
[Op.and]: [ { serverId: null } ]
}
}
if (options.listMyPlaylists !== true) {
whereAnd.push({
privacy: VideoPlaylistPrivacy.PUBLIC
})
// … OR playlists that are on an instance followed by actorId
if (options.followerActorId) {
// Only list local playlists
const whereActorOr: WhereOptions[] = [
{
serverId: null
}
]
const inQueryInstanceFollow = buildServerIdsFollowedBy(options.followerActorId)
whereActorOr.push({
serverId: {
[Op.in]: literal(inQueryInstanceFollow)
}
})
Object.assign(whereActor, { [Op.or]: whereActorOr })
}
}
if (options.accountId) {
whereAnd.push({
ownerAccountId: options.accountId
})
}
if (options.videoChannelId) {
whereAnd.push({
videoChannelId: options.videoChannelId
})
}
if (options.type) {
whereAnd.push({
type: options.type
})
}
if (options.uuids) {
whereAnd.push({
uuid: {
[Op.in]: options.uuids
}
})
}
if (options.withVideos === true) {
whereAnd.push(
literal(`(${getVideoLengthSelect()}) != 0`)
)
}
let attributesInclude: any[] = [ literal('0 as similarity') ]
if (options.search) {
const escapedSearch = VideoPlaylistModel.sequelize.escape(options.search)
const escapedLikeSearch = VideoPlaylistModel.sequelize.escape('%' + options.search + '%')
attributesInclude = [ createSimilarityAttribute('VideoPlaylistModel.name', options.search) ]
whereAnd.push({
[Op.or]: [
Sequelize.literal(
'lower(immutable_unaccent("VideoPlaylistModel"."name")) % lower(immutable_unaccent(' + escapedSearch + '))'
),
Sequelize.literal(
'lower(immutable_unaccent("VideoPlaylistModel"."name")) LIKE lower(immutable_unaccent(' + escapedLikeSearch + '))'
)
]
})
}
const where = {
[Op.and]: whereAnd
}
const include: Includeable[] = [
{
model: AccountModel.scope({
method: [ AccountScopeNames.SUMMARY, { whereActor, whereServer, forCount: options.forCount } as SummaryOptions ]
}),
required: true
}
]
if (options.forCount !== true) {
include.push({
model: VideoChannelModel.scope(VideoChannelScopeNames.SUMMARY),
required: false
})
}
return {
attributes: {
include: attributesInclude
},
where,
include
} as FindOptions
}
}))
@Table({
tableName: 'videoPlaylist',
indexes: [
buildTrigramSearchIndex('video_playlist_name_trigram', 'name'),
{
fields: [ 'ownerAccountId' ]
},
{
fields: [ 'videoChannelId' ]
},
{
fields: [ 'url' ],
unique: true
}
]
})
export class VideoPlaylistModel extends SequelizeModel<VideoPlaylistModel> {
@CreatedAt
createdAt: Date
@UpdatedAt
updatedAt: Date
@AllowNull(false)
@Is('VideoPlaylistName', value => throwIfNotValid(value, isVideoPlaylistNameValid, 'name'))
@Column
name: string
@AllowNull(true)
@Is('VideoPlaylistDescription', value => throwIfNotValid(value, isVideoPlaylistDescriptionValid, 'description', true))
@Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_PLAYLISTS.DESCRIPTION.max))
description: string
@AllowNull(false)
@Is('VideoPlaylistPrivacy', value => throwIfNotValid(value, isVideoPlaylistPrivacyValid, 'privacy'))
@Column
privacy: VideoPlaylistPrivacyType
@AllowNull(false)
@Is('VideoPlaylistUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
@Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_PLAYLISTS.URL.max))
url: string
@AllowNull(false)
@Default(DataType.UUIDV4)
@IsUUID(4)
@Column(DataType.UUID)
uuid: string
@AllowNull(false)
@Default(VideoPlaylistType.REGULAR)
@Column
type: VideoPlaylistType_Type
@ForeignKey(() => AccountModel)
@Column
ownerAccountId: number
@BelongsTo(() => AccountModel, {
foreignKey: {
allowNull: false
},
onDelete: 'CASCADE'
})
OwnerAccount: Awaited<AccountModel>
@ForeignKey(() => VideoChannelModel)
@Column
videoChannelId: number
@BelongsTo(() => VideoChannelModel, {
foreignKey: {
allowNull: true
},
onDelete: 'CASCADE'
})
VideoChannel: Awaited<VideoChannelModel>
@HasMany(() => VideoPlaylistElementModel, {
foreignKey: {
name: 'videoPlaylistId',
allowNull: false
},
onDelete: 'CASCADE'
})
VideoPlaylistElements: Awaited<VideoPlaylistElementModel>[]
@HasOne(() => ThumbnailModel, {
foreignKey: {
name: 'videoPlaylistId',
allowNull: true
},
onDelete: 'CASCADE',
hooks: true
})
Thumbnail: Awaited<ThumbnailModel>
static listForApi (options: AvailableForListOptions & {
start: number
count: number
sort: string
}) {
const query = {
offset: options.start,
limit: options.count,
order: getPlaylistSort(options.sort)
}
const commonAvailableForListOptions = pick(options, [
'type',
'followerActorId',
'accountId',
'videoChannelId',
'listMyPlaylists',
'search',
'host',
'uuids'
])
const scopesFind: (string | ScopeOptions)[] = [
{
method: [
ScopeNames.AVAILABLE_FOR_LIST,
{
...commonAvailableForListOptions,
withVideos: options.withVideos || false
} as AvailableForListOptions
]
},
ScopeNames.WITH_VIDEOS_LENGTH,
ScopeNames.WITH_THUMBNAIL
]
const scopesCount: (string | ScopeOptions)[] = [
{
method: [
ScopeNames.AVAILABLE_FOR_LIST,
{
...commonAvailableForListOptions,
withVideos: options.withVideos || false,
forCount: true
} as AvailableForListOptions
]
},
ScopeNames.WITH_VIDEOS_LENGTH
]
return Promise.all([
VideoPlaylistModel.scope(scopesCount).count(),
VideoPlaylistModel.scope(scopesFind).findAll(query)
]).then(([ count, rows ]) => ({ total: count, data: rows }))
}
static searchForApi (options: Pick<AvailableForListOptions, 'followerActorId' | 'search' | 'host' | 'uuids'> & {
start: number
count: number
sort: string
}) {
return VideoPlaylistModel.listForApi({
...options,
type: VideoPlaylistType.REGULAR,
listMyPlaylists: false,
withVideos: true
})
}
static listPublicUrlsOfForAP (options: { account?: MAccountId, channel?: MChannelId }, start: number, count: number) {
const where = {
privacy: VideoPlaylistPrivacy.PUBLIC
}
if (options.account) {
Object.assign(where, { ownerAccountId: options.account.id })
}
if (options.channel) {
Object.assign(where, { videoChannelId: options.channel.id })
}
const getQuery = (forCount: boolean) => {
return {
attributes: forCount === true
? []
: [ 'url' ],
offset: start,
limit: count,
where
}
}
return Promise.all([
VideoPlaylistModel.count(getQuery(true)),
VideoPlaylistModel.findAll(getQuery(false))
]).then(([ total, rows ]) => ({
total,
data: rows.map(p => p.url)
}))
}
static listPlaylistSummariesOf (accountId: number, videoIds: number[]): Promise<MVideoPlaylistSummaryWithElements[]> {
const query = {
attributes: [ 'id', 'name', 'uuid' ],
where: {
ownerAccountId: accountId
},
include: [
{
attributes: [ 'id', 'videoId', 'startTimestamp', 'stopTimestamp' ],
model: VideoPlaylistElementModel.unscoped(),
where: {
videoId: {
[Op.in]: videoIds
}
},
required: true
}
]
}
return VideoPlaylistModel.findAll(query)
}
static listPlaylistForExport (accountId: number): Promise<MVideoPlaylistFull[]> {
return VideoPlaylistModel
.scope([ ScopeNames.WITH_ACCOUNT_AND_CHANNEL, ScopeNames.WITH_VIDEOS_LENGTH, ScopeNames.WITH_THUMBNAIL ])
.findAll({
where: {
ownerAccountId: accountId
},
limit: USER_EXPORT_MAX_ITEMS
})
}
// ---------------------------------------------------------------------------
static doesPlaylistExist (url: string) {
const query = {
attributes: [ 'id' ],
where: {
url
}
}
return VideoPlaylistModel
.findOne(query)
.then(e => !!e)
}
static loadWithAccountAndChannelSummary (id: number | string, transaction: Transaction): Promise<MVideoPlaylistFullSummary> {
const where = buildWhereIdOrUUID(id)
const query = {
where,
transaction
}
return VideoPlaylistModel
.scope([ ScopeNames.WITH_ACCOUNT_AND_CHANNEL_SUMMARY, ScopeNames.WITH_VIDEOS_LENGTH, ScopeNames.WITH_THUMBNAIL ])
.findOne(query)
}
static loadWithAccountAndChannel (id: number | string, transaction: Transaction): Promise<MVideoPlaylistFull> {
const where = buildWhereIdOrUUID(id)
const query = {
where,
transaction
}
return VideoPlaylistModel
.scope([ ScopeNames.WITH_ACCOUNT_AND_CHANNEL, ScopeNames.WITH_VIDEOS_LENGTH, ScopeNames.WITH_THUMBNAIL ])
.findOne(query)
}
static loadByUrlAndPopulateAccount (url: string): Promise<MVideoPlaylistAccountThumbnail> {
const query = {
where: {
url
}
}
return VideoPlaylistModel.scope([ ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_THUMBNAIL ]).findOne(query)
}
static loadByUrlWithAccountAndChannelSummary (url: string): Promise<MVideoPlaylistFullSummary> {
const query = {
where: {
url
}
}
return VideoPlaylistModel
.scope([ ScopeNames.WITH_ACCOUNT_AND_CHANNEL_SUMMARY, ScopeNames.WITH_VIDEOS_LENGTH, ScopeNames.WITH_THUMBNAIL ])
.findOne(query)
}
static loadWatchLaterOf (account: MAccountId): Promise<MVideoPlaylistFull> {
const query = {
where: {
type: VideoPlaylistType.WATCH_LATER,
ownerAccountId: account.id
}
}
return VideoPlaylistModel
.scope([ ScopeNames.WITH_ACCOUNT_AND_CHANNEL, ScopeNames.WITH_VIDEOS_LENGTH, ScopeNames.WITH_THUMBNAIL ])
.findOne(query)
}
static loadRegularByAccountAndName (account: MAccountId, name: string): Promise<MVideoPlaylist> {
const query = {
where: {
type: VideoPlaylistType.REGULAR,
name,
ownerAccountId: account.id
}
}
return VideoPlaylistModel
.findOne(query)
}
static getPrivacyLabel (privacy: VideoPlaylistPrivacyType) {
return VIDEO_PLAYLIST_PRIVACIES[privacy] || 'Unknown'
}
static getTypeLabel (type: VideoPlaylistType_Type) {
return VIDEO_PLAYLIST_TYPES[type] || 'Unknown'
}
static resetPlaylistsOfChannel (videoChannelId: number, transaction: Transaction) {
const query = {
where: {
videoChannelId
},
transaction
}
return VideoPlaylistModel.update({ privacy: VideoPlaylistPrivacy.PRIVATE, videoChannelId: null }, query)
}
async setAndSaveThumbnail (thumbnail: MThumbnail, t: Transaction) {
thumbnail.videoPlaylistId = this.id
this.Thumbnail = await thumbnail.save({ transaction: t })
}
hasThumbnail () {
return !!this.Thumbnail
}
hasGeneratedThumbnail () {
return this.hasThumbnail() && this.Thumbnail.automaticallyGenerated === true
}
shouldGenerateThumbnailWithNewElement (newElement: MVideoPlaylistElement) {
if (this.hasThumbnail() === false) return true
if (newElement.position === 1 && this.hasGeneratedThumbnail()) return true
return false
}
generateThumbnailName () {
const extension = '.jpg'
return 'playlist-' + buildUUID() + extension
}
getThumbnailUrl () {
if (!this.hasThumbnail()) return null
return WEBSERVER.URL + LAZY_STATIC_PATHS.THUMBNAILS + this.Thumbnail.filename
}
getThumbnailStaticPath () {
if (!this.hasThumbnail()) return null
return join(LAZY_STATIC_PATHS.THUMBNAILS, this.Thumbnail.filename)
}
getWatchStaticPath () {
return buildPlaylistWatchPath({ shortUUID: uuidToShort(this.uuid) })
}
getEmbedStaticPath () {
return buildPlaylistEmbedPath(this)
}
static async getStats () {
const totalLocalPlaylists = await VideoPlaylistModel.count({
include: [
{
model: AccountModel.unscoped(),
required: true,
include: [
{
model: ActorModel.unscoped(),
required: true,
where: {
serverId: null
}
}
]
}
],
where: {
privacy: VideoPlaylistPrivacy.PUBLIC
}
})
return {
totalLocalPlaylists
}
}
setAsRefreshed () {
return setAsUpdated({ sequelize: this.sequelize, table: 'videoPlaylist', id: this.id })
}
setVideosLength (videosLength: number) {
this.set('videosLength' as any, videosLength, { raw: true })
}
isOwned () {
return this.OwnerAccount.isOwned()
}
isOutdated () {
if (this.isOwned()) return false
return isOutdated(this, ACTIVITY_PUB.VIDEO_PLAYLIST_REFRESH_INTERVAL)
}
toFormattedJSON (this: MVideoPlaylistFormattable): VideoPlaylist {
return {
id: this.id,
uuid: this.uuid,
shortUUID: uuidToShort(this.uuid),
isLocal: this.isOwned(),
url: this.url,
displayName: this.name,
description: this.description,
privacy: {
id: this.privacy,
label: VideoPlaylistModel.getPrivacyLabel(this.privacy)
},
thumbnailPath: this.getThumbnailStaticPath(),
embedPath: this.getEmbedStaticPath(),
type: {
id: this.type,
label: VideoPlaylistModel.getTypeLabel(this.type)
},
videosLength: this.get('videosLength') as number,
createdAt: this.createdAt,
updatedAt: this.updatedAt,
ownerAccount: this.OwnerAccount.toFormattedSummaryJSON(),
videoChannel: this.VideoChannel
? this.VideoChannel.toFormattedSummaryJSON()
: null
}
}
toActivityPubObject (this: MVideoPlaylistAP, page: number, t: Transaction): Promise<PlaylistObject> {
const handler = (start: number, count: number) => {
return VideoPlaylistElementModel.listUrlsOfForAP(this.id, start, count, t)
}
let icon: ActivityIconObject
if (this.hasThumbnail()) {
icon = {
type: 'Image' as 'Image',
url: this.getThumbnailUrl(),
mediaType: 'image/jpeg' as 'image/jpeg',
width: THUMBNAILS_SIZE.width,
height: THUMBNAILS_SIZE.height
}
}
return activityPubCollectionPagination(this.url, handler, page)
.then(o => {
return Object.assign(o, {
type: 'Playlist' as 'Playlist',
name: this.name,
content: this.description,
mediaType: 'text/markdown' as 'text/markdown',
uuid: this.uuid,
published: this.createdAt.toISOString(),
updated: this.updatedAt.toISOString(),
attributedTo: this.VideoChannel ? [ this.VideoChannel.Actor.url ] : [],
icon
})
})
}
}
+215
ファイルの表示
@@ -0,0 +1,215 @@
import { literal, Op, QueryTypes, Transaction } from 'sequelize'
import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Is, Scopes, Table, UpdatedAt } from 'sequelize-typescript'
import { forceNumber } from '@peertube/peertube-core-utils'
import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc.js'
import { CONSTRAINTS_FIELDS } from '../../initializers/constants.js'
import { MActorDefault, MActorFollowersUrl, MActorId } from '../../types/models/index.js'
import { MVideoShareActor, MVideoShareFull } from '../../types/models/video/index.js'
import { ActorModel } from '../actor/actor.js'
import { buildLocalActorIdsIn, SequelizeModel, throwIfNotValid } from '../shared/index.js'
import { VideoModel } from './video.js'
enum ScopeNames {
FULL = 'FULL',
WITH_ACTOR = 'WITH_ACTOR'
}
@Scopes(() => ({
[ScopeNames.FULL]: {
include: [
{
model: ActorModel,
required: true
},
{
model: VideoModel,
required: true
}
]
},
[ScopeNames.WITH_ACTOR]: {
include: [
{
model: ActorModel,
required: true
}
]
}
}))
@Table({
tableName: 'videoShare',
indexes: [
{
fields: [ 'actorId' ]
},
{
fields: [ 'videoId' ]
},
{
fields: [ 'url' ],
unique: true
}
]
})
export class VideoShareModel extends SequelizeModel<VideoShareModel> {
@AllowNull(false)
@Is('VideoShareUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
@Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_SHARE.URL.max))
url: string
@CreatedAt
createdAt: Date
@UpdatedAt
updatedAt: Date
@ForeignKey(() => ActorModel)
@Column
actorId: number
@BelongsTo(() => ActorModel, {
foreignKey: {
allowNull: false
},
onDelete: 'cascade'
})
Actor: Awaited<ActorModel>
@ForeignKey(() => VideoModel)
@Column
videoId: number
@BelongsTo(() => VideoModel, {
foreignKey: {
allowNull: false
},
onDelete: 'cascade'
})
Video: Awaited<VideoModel>
static load (actorId: number | string, videoId: number | string, t?: Transaction): Promise<MVideoShareActor> {
return VideoShareModel.scope(ScopeNames.WITH_ACTOR).findOne({
where: {
actorId,
videoId
},
transaction: t
})
}
static loadByUrl (url: string, t: Transaction): Promise<MVideoShareFull> {
return VideoShareModel.scope(ScopeNames.FULL).findOne({
where: {
url
},
transaction: t
})
}
static listActorIdsAndFollowerUrlsByShare (videoId: number, t: Transaction) {
const query = `SELECT "actor"."id" AS "id", "actor"."followersUrl" AS "followersUrl" ` +
`FROM "videoShare" ` +
`INNER JOIN "actor" ON "actor"."id" = "videoShare"."actorId" ` +
`WHERE "videoShare"."videoId" = :videoId`
const options = {
type: QueryTypes.SELECT as QueryTypes.SELECT,
replacements: { videoId },
transaction: t
}
return VideoShareModel.sequelize.query<MActorId & MActorFollowersUrl>(query, options)
}
static loadActorsWhoSharedVideosOf (actorOwnerId: number, t: Transaction): Promise<MActorDefault[]> {
const safeOwnerId = forceNumber(actorOwnerId)
// /!\ On actor model
const query = {
where: {
[Op.and]: [
literal(
`EXISTS (` +
` SELECT 1 FROM "videoShare" ` +
` INNER JOIN "video" ON "videoShare"."videoId" = "video"."id" ` +
` INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ` +
` INNER JOIN "account" ON "account"."id" = "videoChannel"."accountId" ` +
` WHERE "videoShare"."actorId" = "ActorModel"."id" AND "account"."actorId" = ${safeOwnerId} ` +
` LIMIT 1` +
`)`
)
]
},
transaction: t
}
return ActorModel.findAll(query)
}
static loadActorsByVideoChannel (videoChannelId: number, t: Transaction): Promise<MActorDefault[]> {
const safeChannelId = forceNumber(videoChannelId)
// /!\ On actor model
const query = {
where: {
[Op.and]: [
literal(
`EXISTS (` +
` SELECT 1 FROM "videoShare" ` +
` INNER JOIN "video" ON "videoShare"."videoId" = "video"."id" ` +
` WHERE "videoShare"."actorId" = "ActorModel"."id" AND "video"."channelId" = ${safeChannelId} ` +
` LIMIT 1` +
`)`
)
]
},
transaction: t
}
return ActorModel.findAll(query)
}
static listAndCountByVideoId (videoId: number, start: number, count: number, t?: Transaction) {
const query = {
offset: start,
limit: count,
where: {
videoId
},
transaction: t
}
return Promise.all([
VideoShareModel.count(query),
VideoShareModel.findAll(query)
]).then(([ total, data ]) => ({ total, data }))
}
static listRemoteShareUrlsOfLocalVideos () {
const query = `SELECT "videoShare".url FROM "videoShare" ` +
`INNER JOIN actor ON actor.id = "videoShare"."actorId" AND actor."serverId" IS NOT NULL ` +
`INNER JOIN video ON video.id = "videoShare"."videoId" AND video.remote IS FALSE`
return VideoShareModel.sequelize.query<{ url: string }>(query, {
type: QueryTypes.SELECT,
raw: true
}).then(rows => rows.map(r => r.url))
}
static cleanOldSharesOf (videoId: number, beforeUpdatedAt: Date) {
const query = {
where: {
updatedAt: {
[Op.lt]: beforeUpdatedAt
},
videoId,
actorId: {
[Op.notIn]: buildLocalActorIdsIn()
}
}
}
return VideoShareModel.destroy(query)
}
}
+150
ファイルの表示
@@ -0,0 +1,150 @@
import { type FileStorageType, type VideoSource } from '@peertube/peertube-models'
import { STATIC_DOWNLOAD_PATHS, WEBSERVER } from '@server/initializers/constants.js'
import { MVideoSource } from '@server/types/models/video/video-source.js'
import { join } from 'path'
import { Transaction } from 'sequelize'
import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Table, UpdatedAt } from 'sequelize-typescript'
import { SequelizeModel, doesExist, getSort } from '../shared/index.js'
import { getResolutionLabel } from './formatter/video-api-format.js'
import { VideoModel } from './video.js'
@Table({
tableName: 'videoSource',
indexes: [
{
fields: [ 'videoId' ]
},
{
fields: [ { name: 'createdAt', order: 'DESC' } ]
},
{
fields: [ 'keptOriginalFilename' ],
unique: true
}
]
})
export class VideoSourceModel extends SequelizeModel<VideoSourceModel> {
@CreatedAt
createdAt: Date
@UpdatedAt
updatedAt: Date
@AllowNull(false)
@Column
inputFilename: string
@AllowNull(true)
@Column
keptOriginalFilename: string
@AllowNull(true)
@Column
resolution: number
@AllowNull(true)
@Column
width: number
@AllowNull(true)
@Column
height: number
@AllowNull(true)
@Column
fps: number
@AllowNull(true)
@Column(DataType.BIGINT)
size: number
@AllowNull(true)
@Column(DataType.JSONB)
metadata: any
@AllowNull(true)
@Column
storage: FileStorageType
@AllowNull(true)
@Column
fileUrl: string
@ForeignKey(() => VideoModel)
@Column
videoId: number
@BelongsTo(() => VideoModel, {
foreignKey: {
allowNull: false
},
onDelete: 'cascade'
})
Video: Awaited<VideoModel>
static loadLatest (videoId: number, transaction?: Transaction) {
return VideoSourceModel.findOne<MVideoSource>({
where: { videoId },
order: getSort('-createdAt'),
transaction
})
}
static loadByKeptOriginalFilename (keptOriginalFilename: string) {
return VideoSourceModel.findOne<MVideoSource>({
where: { keptOriginalFilename }
})
}
static listAll (videoId: number, transaction?: Transaction) {
return VideoSourceModel.findAll<MVideoSource>({
where: { videoId },
transaction
})
}
// ---------------------------------------------------------------------------
static async doesOwnedFileExist (filename: string, storage: FileStorageType) {
const query = 'SELECT 1 FROM "videoSource" ' +
'INNER JOIN "video" ON "video"."id" = "videoSource"."videoId" AND "video"."remote" IS FALSE ' +
`WHERE "keptOriginalFilename" = $filename AND "storage" = $storage LIMIT 1`
return doesExist({ sequelize: this.sequelize, query, bind: { filename, storage } })
}
// ---------------------------------------------------------------------------
getFileDownloadUrl () {
if (!this.keptOriginalFilename) return null
return WEBSERVER.URL + join(STATIC_DOWNLOAD_PATHS.ORIGINAL_VIDEO_FILE, this.keptOriginalFilename)
}
toFormattedJSON (): VideoSource {
return {
filename: this.inputFilename,
inputFilename: this.inputFilename,
fileUrl: this.fileUrl,
fileDownloadUrl: this.getFileDownloadUrl(),
resolution: {
id: this.resolution,
label: this.resolution !== null
? getResolutionLabel(this.resolution)
: null
},
size: this.size,
width: this.width,
height: this.height,
fps: this.fps,
metadata: this.metadata,
createdAt: this.createdAt.toISOString()
}
}
}
+329
ファイルの表示
@@ -0,0 +1,329 @@
import {
FileStorage,
VideoStreamingPlaylistType,
type FileStorageType,
type VideoStreamingPlaylistType_Type
} from '@peertube/peertube-models'
import { sha1 } from '@peertube/peertube-node-utils'
import { CONFIG } from '@server/initializers/config.js'
import { getHLSPrivateFileUrl, getObjectStoragePublicFileUrl } from '@server/lib/object-storage/index.js'
import { generateHLSMasterPlaylistFilename, generateHlsSha256SegmentsFilename } from '@server/lib/paths.js'
import { isVideoInPrivateDirectory } from '@server/lib/video-privacy.js'
import { VideoFileModel } from '@server/models/video/video-file.js'
import { MStreamingPlaylist, MStreamingPlaylistFilesVideo, MVideo } from '@server/types/models/index.js'
import memoizee from 'memoizee'
import { join } from 'path'
import { Op, Transaction } from 'sequelize'
import {
AllowNull,
BelongsTo,
Column,
CreatedAt,
DataType,
Default,
ForeignKey,
HasMany,
Is, Table,
UpdatedAt
} from 'sequelize-typescript'
import { isArrayOf } from '../../helpers/custom-validators/misc.js'
import { isVideoFileInfoHashValid } from '../../helpers/custom-validators/videos.js'
import {
CONSTRAINTS_FIELDS,
MEMOIZE_LENGTH,
MEMOIZE_TTL,
P2P_MEDIA_LOADER_PEER_VERSION,
STATIC_PATHS,
WEBSERVER
} from '../../initializers/constants.js'
import { VideoRedundancyModel } from '../redundancy/video-redundancy.js'
import { SequelizeModel, doesExist, throwIfNotValid } from '../shared/index.js'
import { VideoModel } from './video.js'
@Table({
tableName: 'videoStreamingPlaylist',
indexes: [
{
fields: [ 'videoId' ]
},
{
fields: [ 'videoId', 'type' ],
unique: true
},
{
fields: [ 'p2pMediaLoaderInfohashes' ],
using: 'gin'
}
]
})
export class VideoStreamingPlaylistModel extends SequelizeModel<VideoStreamingPlaylistModel> {
@CreatedAt
createdAt: Date
@UpdatedAt
updatedAt: Date
@AllowNull(false)
@Column
type: VideoStreamingPlaylistType_Type
@AllowNull(false)
@Column
playlistFilename: string
@AllowNull(true)
@Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS.URL.max))
playlistUrl: string
@AllowNull(false)
@Is('VideoStreamingPlaylistInfoHashes', value => throwIfNotValid(value, v => isArrayOf(v, isVideoFileInfoHashValid), 'info hashes'))
@Column(DataType.ARRAY(DataType.STRING))
p2pMediaLoaderInfohashes: string[]
@AllowNull(false)
@Column
p2pMediaLoaderPeerVersion: number
@AllowNull(true)
@Column
segmentsSha256Filename: string
@AllowNull(true)
@Column
segmentsSha256Url: string
@ForeignKey(() => VideoModel)
@Column
videoId: number
@AllowNull(false)
@Default(FileStorage.FILE_SYSTEM)
@Column
storage: FileStorageType
@BelongsTo(() => VideoModel, {
foreignKey: {
allowNull: false
},
onDelete: 'CASCADE'
})
Video: Awaited<VideoModel>
@HasMany(() => VideoFileModel, {
foreignKey: {
allowNull: true
},
onDelete: 'CASCADE'
})
VideoFiles: Awaited<VideoFileModel>[]
@HasMany(() => VideoRedundancyModel, {
foreignKey: {
allowNull: false
},
onDelete: 'CASCADE',
hooks: true
})
RedundancyVideos: Awaited<VideoRedundancyModel>[]
static doesInfohashExistCached = memoizee(VideoStreamingPlaylistModel.doesInfohashExist.bind(VideoStreamingPlaylistModel), {
promise: true,
max: MEMOIZE_LENGTH.INFO_HASH_EXISTS,
maxAge: MEMOIZE_TTL.INFO_HASH_EXISTS
})
static doesInfohashExist (infoHash: string) {
// Don't add a LIMIT 1 here to prevent seq scan by PostgreSQL (not sure why id doesn't use the index when we add a LIMIT)
const query = 'SELECT 1 FROM "videoStreamingPlaylist" WHERE "p2pMediaLoaderInfohashes" @> $infoHash'
return doesExist({ sequelize: this.sequelize, query, bind: { infoHash: `{${infoHash}}` } }) // Transform infoHash in a PG array
}
static buildP2PMediaLoaderInfoHashes (playlistUrl: string, files: unknown[]) {
const hashes: string[] = []
// https://github.com/Novage/p2p-media-loader/blob/master/p2p-media-loader-core/lib/p2p-media-manager.ts#L115
for (let i = 0; i < files.length; i++) {
hashes.push(sha1(`${P2P_MEDIA_LOADER_PEER_VERSION}${playlistUrl}+V${i}`))
}
return hashes
}
static listByIncorrectPeerVersion () {
const query = {
where: {
p2pMediaLoaderPeerVersion: {
[Op.ne]: P2P_MEDIA_LOADER_PEER_VERSION
}
},
include: [
{
model: VideoModel.unscoped(),
required: true
}
]
}
return VideoStreamingPlaylistModel.findAll(query)
}
static loadWithVideoAndFiles (id: number) {
const options = {
include: [
{
model: VideoModel.unscoped(),
required: true
},
{
model: VideoFileModel.unscoped()
}
]
}
return VideoStreamingPlaylistModel.findByPk<MStreamingPlaylistFilesVideo>(id, options)
}
static loadWithVideo (id: number) {
const options = {
include: [
{
model: VideoModel.unscoped(),
required: true
}
]
}
return VideoStreamingPlaylistModel.findByPk(id, options)
}
static loadHLSPlaylistByVideo (videoId: number, transaction?: Transaction): Promise<MStreamingPlaylist> {
const options = {
where: {
type: VideoStreamingPlaylistType.HLS,
videoId
},
transaction
}
return VideoStreamingPlaylistModel.findOne(options)
}
static async loadOrGenerate (video: MVideo, transaction?: Transaction) {
let playlist = await VideoStreamingPlaylistModel.loadHLSPlaylistByVideo(video.id, transaction)
if (!playlist) {
playlist = new VideoStreamingPlaylistModel({
p2pMediaLoaderPeerVersion: P2P_MEDIA_LOADER_PEER_VERSION,
type: VideoStreamingPlaylistType.HLS,
storage: FileStorage.FILE_SYSTEM,
p2pMediaLoaderInfohashes: [],
playlistFilename: generateHLSMasterPlaylistFilename(video.isLive),
segmentsSha256Filename: generateHlsSha256SegmentsFilename(video.isLive),
videoId: video.id
})
await playlist.save({ transaction })
}
return Object.assign(playlist, { Video: video })
}
static doesOwnedVideoUUIDExist (videoUUID: string, storage: FileStorageType) {
const query = `SELECT 1 FROM "videoStreamingPlaylist" ` +
`INNER JOIN "video" ON "video"."id" = "videoStreamingPlaylist"."videoId" ` +
`AND "video"."remote" IS FALSE AND "video"."uuid" = $videoUUID ` +
`AND "storage" = $storage LIMIT 1`
return doesExist({ sequelize: this.sequelize, query, bind: { videoUUID, storage } })
}
assignP2PMediaLoaderInfoHashes (video: MVideo, files: unknown[]) {
const masterPlaylistUrl = this.getMasterPlaylistUrl(video)
this.p2pMediaLoaderInfohashes = VideoStreamingPlaylistModel.buildP2PMediaLoaderInfoHashes(masterPlaylistUrl, files)
}
// ---------------------------------------------------------------------------
getMasterPlaylistUrl (video: MVideo) {
if (video.isOwned()) {
if (this.storage === FileStorage.OBJECT_STORAGE) {
return this.getMasterPlaylistObjectStorageUrl(video)
}
return WEBSERVER.URL + this.getMasterPlaylistStaticPath(video)
}
return this.playlistUrl
}
private getMasterPlaylistObjectStorageUrl (video: MVideo) {
if (video.hasPrivateStaticPath() && CONFIG.OBJECT_STORAGE.PROXY.PROXIFY_PRIVATE_FILES === true) {
return getHLSPrivateFileUrl(video, this.playlistFilename)
}
return getObjectStoragePublicFileUrl(this.playlistUrl, CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS)
}
// ---------------------------------------------------------------------------
getSha256SegmentsUrl (video: MVideo) {
if (video.isOwned()) {
if (!this.segmentsSha256Filename) return null
if (this.storage === FileStorage.OBJECT_STORAGE) {
return this.getSha256SegmentsObjectStorageUrl(video)
}
return WEBSERVER.URL + this.getSha256SegmentsStaticPath(video)
}
return this.segmentsSha256Url
}
private getSha256SegmentsObjectStorageUrl (video: MVideo) {
if (video.hasPrivateStaticPath() && CONFIG.OBJECT_STORAGE.PROXY.PROXIFY_PRIVATE_FILES === true) {
return getHLSPrivateFileUrl(video, this.segmentsSha256Filename)
}
return getObjectStoragePublicFileUrl(this.segmentsSha256Url, CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS)
}
// ---------------------------------------------------------------------------
getStringType () {
if (this.type === VideoStreamingPlaylistType.HLS) return 'hls'
return 'unknown'
}
getTrackerUrls (baseUrlHttp: string, baseUrlWs: string) {
return [ baseUrlWs + '/tracker/socket', baseUrlHttp + '/tracker/announce' ]
}
hasSameUniqueKeysThan (other: MStreamingPlaylist) {
return this.type === other.type &&
this.videoId === other.videoId
}
withVideo (video: MVideo) {
return Object.assign(this, { Video: video })
}
private getMasterPlaylistStaticPath (video: MVideo) {
if (isVideoInPrivateDirectory(video.privacy)) {
return join(STATIC_PATHS.STREAMING_PLAYLISTS.PRIVATE_HLS, video.uuid, this.playlistFilename)
}
return join(STATIC_PATHS.STREAMING_PLAYLISTS.HLS, video.uuid, this.playlistFilename)
}
private getSha256SegmentsStaticPath (video: MVideo) {
if (isVideoInPrivateDirectory(video.privacy)) {
return join(STATIC_PATHS.STREAMING_PLAYLISTS.PRIVATE_HLS, video.uuid, this.segmentsSha256Filename)
}
return join(STATIC_PATHS.STREAMING_PLAYLISTS.HLS, video.uuid, this.segmentsSha256Filename)
}
}
+31
ファイルの表示
@@ -0,0 +1,31 @@
import { Column, CreatedAt, ForeignKey, Table, UpdatedAt } from 'sequelize-typescript'
import { TagModel } from './tag.js'
import { VideoModel } from './video.js'
import { SequelizeModel } from '../shared/index.js'
@Table({
tableName: 'videoTag',
indexes: [
{
fields: [ 'videoId' ]
},
{
fields: [ 'tagId' ]
}
]
})
export class VideoTagModel extends SequelizeModel<VideoTagModel> {
@CreatedAt
createdAt: Date
@UpdatedAt
updatedAt: Date
@ForeignKey(() => VideoModel)
@Column
videoId: number
@ForeignKey(() => TagModel)
@Column
tagId: number
}
ファイル差分が大きすぎるため省略します 差分を読込み
+68
ファイルの表示
@@ -0,0 +1,68 @@
import { MLocalVideoViewerWatchSection } from '@server/types/models/index.js'
import { Transaction } from 'sequelize'
import { AllowNull, BelongsTo, Column, CreatedAt, ForeignKey, Table } from 'sequelize-typescript'
import { SequelizeModel } from '../shared/index.js'
import { LocalVideoViewerModel } from './local-video-viewer.js'
@Table({
tableName: 'localVideoViewerWatchSection',
updatedAt: false,
indexes: [
{
fields: [ 'localVideoViewerId' ]
}
]
})
export class LocalVideoViewerWatchSectionModel extends SequelizeModel<LocalVideoViewerWatchSectionModel> {
@CreatedAt
createdAt: Date
@AllowNull(false)
@Column
watchStart: number
@AllowNull(false)
@Column
watchEnd: number
@ForeignKey(() => LocalVideoViewerModel)
@Column
localVideoViewerId: number
@BelongsTo(() => LocalVideoViewerModel, {
foreignKey: {
allowNull: false
},
onDelete: 'CASCADE'
})
LocalVideoViewer: Awaited<LocalVideoViewerModel>
static async bulkCreateSections (options: {
localVideoViewerId: number
watchSections: {
start: number
end: number
}[]
transaction?: Transaction
}) {
const { localVideoViewerId, watchSections, transaction } = options
const models: MLocalVideoViewerWatchSection[] = []
for (const section of watchSections) {
const watchStart = section.start || 0
const watchEnd = section.end || 0
if (watchStart === watchEnd) continue
const model = await this.create({
watchStart,
watchEnd,
localVideoViewerId
}, { transaction })
models.push(model)
}
return models
}
}
+385
ファイルの表示
@@ -0,0 +1,385 @@
import { QueryTypes } from 'sequelize'
import { AllowNull, BelongsTo, Column, CreatedAt, DataType, Default, ForeignKey, HasMany, IsUUID, Table } from 'sequelize-typescript'
import { getActivityStreamDuration } from '@server/lib/activitypub/activity.js'
import { buildGroupByAndBoundaries } from '@server/lib/timeserie.js'
import { MLocalVideoViewer, MLocalVideoViewerWithWatchSections, MVideo } from '@server/types/models/index.js'
import {
VideoStatsOverall,
VideoStatsRetention,
VideoStatsTimeserie,
VideoStatsTimeserieMetric,
WatchActionObject
} from '@peertube/peertube-models'
import { VideoModel } from '../video/video.js'
import { LocalVideoViewerWatchSectionModel } from './local-video-viewer-watch-section.js'
import { SequelizeModel } from '../shared/index.js'
/**
*
* Aggregate viewers of local videos only to display statistics to video owners
* A viewer is a user that watched one or multiple sections of a specific video inside a time window
*
*/
@Table({
tableName: 'localVideoViewer',
updatedAt: false,
indexes: [
{
fields: [ 'videoId' ]
},
{
fields: [ 'url' ],
unique: true
}
]
})
export class LocalVideoViewerModel extends SequelizeModel<LocalVideoViewerModel> {
@CreatedAt
createdAt: Date
@AllowNull(false)
@Column(DataType.DATE)
startDate: Date
@AllowNull(false)
@Column(DataType.DATE)
endDate: Date
@AllowNull(false)
@Column
watchTime: number
@AllowNull(true)
@Column
country: string
@AllowNull(true)
@Column
subdivisionName: string
@AllowNull(false)
@Default(DataType.UUIDV4)
@IsUUID(4)
@Column(DataType.UUID)
uuid: string
@AllowNull(false)
@Column
url: string
@ForeignKey(() => VideoModel)
@Column
videoId: number
@BelongsTo(() => VideoModel, {
foreignKey: {
allowNull: false
},
onDelete: 'CASCADE'
})
Video: Awaited<VideoModel>
@HasMany(() => LocalVideoViewerWatchSectionModel, {
foreignKey: {
allowNull: false
},
onDelete: 'cascade'
})
WatchSections: Awaited<LocalVideoViewerWatchSectionModel>[]
static loadByUrl (url: string): Promise<MLocalVideoViewer> {
return this.findOne({
where: {
url
}
})
}
static loadFullById (id: number): Promise<MLocalVideoViewerWithWatchSections> {
return this.findOne({
include: [
{
model: VideoModel.unscoped(),
required: true
},
{
model: LocalVideoViewerWatchSectionModel.unscoped(),
required: true
}
],
where: {
id
}
})
}
static async getOverallStats (options: {
video: MVideo
startDate?: string
endDate?: string
}): Promise<VideoStatsOverall> {
const { video, startDate, endDate } = options
const queryOptions = {
type: QueryTypes.SELECT as QueryTypes.SELECT,
replacements: { videoId: video.id } as any
}
if (startDate) queryOptions.replacements.startDate = startDate
if (endDate) queryOptions.replacements.endDate = endDate
const buildTotalViewersPromise = () => {
let totalViewersDateWhere = ''
if (startDate) totalViewersDateWhere += ' AND "localVideoViewer"."endDate" >= :startDate'
if (endDate) totalViewersDateWhere += ' AND "localVideoViewer"."startDate" <= :endDate'
const totalViewersQuery = `SELECT ` +
`COUNT("localVideoViewer"."id") AS "totalViewers" ` +
`FROM "localVideoViewer" ` +
`WHERE "videoId" = :videoId ${totalViewersDateWhere}`
return LocalVideoViewerModel.sequelize.query<any>(totalViewersQuery, queryOptions)
}
const buildWatchTimePromise = () => {
let watchTimeDateWhere = ''
// We know this where is not exact
// But we prefer to take into account only watch section that started and ended **in** the interval
if (startDate) watchTimeDateWhere += ' AND "localVideoViewer"."startDate" >= :startDate'
if (endDate) watchTimeDateWhere += ' AND "localVideoViewer"."endDate" <= :endDate'
const watchTimeQuery = `SELECT ` +
`SUM("localVideoViewer"."watchTime") AS "totalWatchTime", ` +
`AVG("localVideoViewer"."watchTime") AS "averageWatchTime" ` +
`FROM "localVideoViewer" ` +
`WHERE "videoId" = :videoId ${watchTimeDateWhere}`
return LocalVideoViewerModel.sequelize.query<any>(watchTimeQuery, queryOptions)
}
const buildWatchPeakPromise = () => {
let watchPeakDateWhereStart = ''
let watchPeakDateWhereEnd = ''
if (startDate) {
watchPeakDateWhereStart += ' AND "localVideoViewer"."startDate" >= :startDate'
watchPeakDateWhereEnd += ' AND "localVideoViewer"."endDate" >= :startDate'
}
if (endDate) {
watchPeakDateWhereStart += ' AND "localVideoViewer"."startDate" <= :endDate'
watchPeakDateWhereEnd += ' AND "localVideoViewer"."endDate" <= :endDate'
}
// Add viewers that were already here, before our start date
const beforeWatchersQuery = startDate
// eslint-disable-next-line max-len
? `SELECT COUNT(*) AS "total" FROM "localVideoViewer" WHERE "localVideoViewer"."startDate" < :startDate AND "localVideoViewer"."endDate" >= :startDate`
: `SELECT 0 AS "total"`
const watchPeakQuery = `WITH
"beforeWatchers" AS (${beforeWatchersQuery}),
"watchPeakValues" AS (
SELECT "startDate" AS "dateBreakpoint", 1 AS "inc"
FROM "localVideoViewer"
WHERE "videoId" = :videoId ${watchPeakDateWhereStart}
UNION ALL
SELECT "endDate" AS "dateBreakpoint", -1 AS "inc"
FROM "localVideoViewer"
WHERE "videoId" = :videoId ${watchPeakDateWhereEnd}
)
SELECT "dateBreakpoint", "concurrent"
FROM (
SELECT "dateBreakpoint", SUM(SUM("inc")) OVER (ORDER BY "dateBreakpoint") + (SELECT "total" FROM "beforeWatchers") AS "concurrent"
FROM "watchPeakValues"
GROUP BY "dateBreakpoint"
) tmp
ORDER BY "concurrent" DESC
FETCH FIRST 1 ROW ONLY`
return LocalVideoViewerModel.sequelize.query<any>(watchPeakQuery, queryOptions)
}
const buildGeoPromise = (type: 'country' | 'subdivisionName') => {
let dateWhere = ''
if (startDate) dateWhere += ' AND "localVideoViewer"."endDate" >= :startDate'
if (endDate) dateWhere += ' AND "localVideoViewer"."startDate" <= :endDate'
const query = `SELECT "${type}", COUNT("${type}") as viewers ` +
`FROM "localVideoViewer" ` +
`WHERE "videoId" = :videoId AND "${type}" IS NOT NULL ${dateWhere} ` +
`GROUP BY "${type}" ` +
`ORDER BY "viewers" DESC`
return LocalVideoViewerModel.sequelize.query<any>(query, queryOptions)
}
const [ rowsTotalViewers, rowsWatchTime, rowsWatchPeak, rowsCountries, rowsSubdivisions ] = await Promise.all([
buildTotalViewersPromise(),
buildWatchTimePromise(),
buildWatchPeakPromise(),
buildGeoPromise('country'),
buildGeoPromise('subdivisionName')
])
const viewersPeak = rowsWatchPeak.length !== 0
? parseInt(rowsWatchPeak[0].concurrent) || 0
: 0
return {
totalWatchTime: rowsWatchTime.length !== 0
? Math.round(rowsWatchTime[0].totalWatchTime) || 0
: 0,
averageWatchTime: rowsWatchTime.length !== 0
? Math.round(rowsWatchTime[0].averageWatchTime) || 0
: 0,
totalViewers: rowsTotalViewers.length !== 0
? Math.round(rowsTotalViewers[0].totalViewers) || 0
: 0,
viewersPeak,
viewersPeakDate: rowsWatchPeak.length !== 0 && viewersPeak !== 0
? rowsWatchPeak[0].dateBreakpoint || null
: null,
countries: rowsCountries.map(r => ({
isoCode: r.country,
viewers: r.viewers
})),
subdivisions: rowsSubdivisions.map(r => ({
name: r.subdivisionName,
viewers: r.viewers
}))
}
}
static async getRetentionStats (video: MVideo): Promise<VideoStatsRetention> {
const step = Math.max(Math.round(video.duration / 100), 1)
const query = `WITH "total" AS (SELECT COUNT(*) AS viewers FROM "localVideoViewer" WHERE "videoId" = :videoId) ` +
`SELECT serie AS "second", ` +
`(COUNT("localVideoViewer".id)::float / (SELECT GREATEST("total"."viewers", 1) FROM "total")) AS "retention" ` +
`FROM generate_series(0, ${video.duration}, ${step}) serie ` +
`LEFT JOIN "localVideoViewer" ON "localVideoViewer"."videoId" = :videoId ` +
`AND EXISTS (` +
`SELECT 1 FROM "localVideoViewerWatchSection" ` +
`WHERE "localVideoViewer"."id" = "localVideoViewerWatchSection"."localVideoViewerId" ` +
`AND serie >= "localVideoViewerWatchSection"."watchStart" ` +
`AND serie <= "localVideoViewerWatchSection"."watchEnd"` +
`)` +
`GROUP BY serie ` +
`ORDER BY serie ASC`
const queryOptions = {
type: QueryTypes.SELECT as QueryTypes.SELECT,
replacements: { videoId: video.id }
}
const rows = await LocalVideoViewerModel.sequelize.query<any>(query, queryOptions)
return {
data: rows.map(r => ({
second: r.second,
retentionPercent: parseFloat(r.retention) * 100
}))
}
}
static async getTimeserieStats (options: {
video: MVideo
metric: VideoStatsTimeserieMetric
startDate: string
endDate: string
}): Promise<VideoStatsTimeserie> {
const { video, metric } = options
const { groupInterval, startDate, endDate } = buildGroupByAndBoundaries(options.startDate, options.endDate)
const selectMetrics: { [ id in VideoStatsTimeserieMetric ]: string } = {
viewers: 'COUNT("localVideoViewer"."id")',
aggregateWatchTime: 'SUM("localVideoViewer"."watchTime")'
}
const intervalWhere: { [ id in VideoStatsTimeserieMetric ]: string } = {
// Viewer is still in the interval. Overlap algorithm
viewers: '"localVideoViewer"."startDate" <= "intervals"."endDate" ' +
'AND "localVideoViewer"."endDate" >= "intervals"."startDate"',
// We do an aggregation, so only sum things once. Arbitrary we use the end date for that purpose
aggregateWatchTime: '"localVideoViewer"."endDate" >= "intervals"."startDate" ' +
'AND "localVideoViewer"."endDate" <= "intervals"."endDate"'
}
const query = `WITH "intervals" AS (
SELECT
"time" AS "startDate", "time" + :groupInterval::interval as "endDate"
FROM
generate_series(:startDate::timestamptz, :endDate::timestamptz, :groupInterval::interval) serie("time")
)
SELECT "intervals"."startDate" as "date", COALESCE(${selectMetrics[metric]}, 0) AS value
FROM
intervals
LEFT JOIN "localVideoViewer" ON "localVideoViewer"."videoId" = :videoId
AND ${intervalWhere[metric]}
GROUP BY
"intervals"."startDate"
ORDER BY
"intervals"."startDate"`
const queryOptions = {
type: QueryTypes.SELECT as QueryTypes.SELECT,
replacements: {
startDate,
endDate,
groupInterval,
videoId: video.id
}
}
const rows = await LocalVideoViewerModel.sequelize.query<any>(query, queryOptions)
return {
groupInterval,
data: rows.map(r => ({
date: r.date,
value: parseInt(r.value)
}))
}
}
toActivityPubObject (this: MLocalVideoViewerWithWatchSections): WatchActionObject {
const location = this.country
? {
location: {
addressCountry: this.country,
addressRegion: this.subdivisionName
}
}
: {}
return {
id: this.url,
type: 'WatchAction',
duration: getActivityStreamDuration(this.watchTime),
startTime: this.startDate.toISOString(),
endTime: this.endDate.toISOString(),
object: this.Video.url,
uuid: this.uuid,
actionStatus: 'CompletedActionStatus',
watchSections: this.WatchSections.map(w => ({
startTimestamp: w.watchStart,
endTimestamp: w.watchEnd
})),
...location
}
}
}
+67
ファイルの表示
@@ -0,0 +1,67 @@
import { literal, Op } from 'sequelize'
import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Table } from 'sequelize-typescript'
import { VideoModel } from '../video/video.js'
import { SequelizeModel } from '../shared/index.js'
/**
*
* Aggregate views of all videos federated with our instance
* Mainly used by the trending/hot algorithms
*
*/
@Table({
tableName: 'videoView',
updatedAt: false,
indexes: [
{
fields: [ 'videoId' ]
},
{
fields: [ 'startDate' ]
}
]
})
export class VideoViewModel extends SequelizeModel<VideoViewModel> {
@CreatedAt
createdAt: Date
@AllowNull(false)
@Column(DataType.DATE)
startDate: Date
@AllowNull(false)
@Column(DataType.DATE)
endDate: Date
@AllowNull(false)
@Column
views: number
@ForeignKey(() => VideoModel)
@Column
videoId: number
@BelongsTo(() => VideoModel, {
foreignKey: {
allowNull: false
},
onDelete: 'CASCADE'
})
Video: Awaited<VideoModel>
static removeOldRemoteViewsHistory (beforeDate: string) {
const query = {
where: {
startDate: {
[Op.lt]: beforeDate
},
videoId: {
[Op.in]: literal('(SELECT "id" FROM "video" WHERE "remote" IS TRUE)')
}
}
}
return VideoViewModel.destroy(query)
}
}
+206
ファイルの表示
@@ -0,0 +1,206 @@
import { WatchedWordsList } from '@peertube/peertube-models'
import { logger } from '@server/helpers/logger.js'
import { wordsToRegExp } from '@server/helpers/regexp.js'
import { MAccountId, MWatchedWordsList } from '@server/types/models/index.js'
import { LRUCache } from 'lru-cache'
import { Transaction } from 'sequelize'
import {
AllowNull,
BelongsTo,
Column,
CreatedAt,
DataType, ForeignKey, Table,
UpdatedAt
} from 'sequelize-typescript'
import { LRU_CACHE, USER_EXPORT_MAX_ITEMS } from '../../initializers/constants.js'
import { AccountModel } from '../account/account.js'
import { SequelizeModel, getSort } from '../shared/index.js'
@Table({
tableName: 'watchedWordsList',
indexes: [
{
fields: [ 'listName', 'accountId' ],
unique: true
},
{
fields: [ 'accountId' ]
}
]
})
export class WatchedWordsListModel extends SequelizeModel<WatchedWordsListModel> {
@CreatedAt
createdAt: Date
@UpdatedAt
updatedAt: Date
@AllowNull(false)
@Column
listName: string
@AllowNull(false)
@Column(DataType.ARRAY(DataType.STRING))
words: string[]
@ForeignKey(() => AccountModel)
@Column
accountId: number
@BelongsTo(() => AccountModel, {
foreignKey: {
allowNull: false
},
onDelete: 'CASCADE'
})
Account: Awaited<AccountModel>
// accountId => reg expressions
private static readonly regexCache = new LRUCache<number, { listName: string, regex: RegExp }[]>({
max: LRU_CACHE.WATCHED_WORDS_REGEX.MAX_SIZE,
ttl: LRU_CACHE.WATCHED_WORDS_REGEX.TTL
})
static load (options: {
id: number
accountId: number
}) {
const { id, accountId } = options
const query = {
where: { id, accountId }
}
return this.findOne(query)
}
static loadByListName (options: {
listName: string
accountId: number
}) {
const { listName, accountId } = options
const query = {
where: { listName, accountId }
}
return this.findOne(query)
}
// ---------------------------------------------------------------------------
static listNamesOf (account: MAccountId) {
const query = {
raw: true,
attributes: [ 'listName' ],
where: { accountId: account.id }
}
return WatchedWordsListModel.findAll(query)
.then(rows => rows.map(r => r.listName))
}
static listForAPI (options: {
accountId: number
start: number
count: number
sort: string
}) {
const { accountId, start, count, sort } = options
const query = {
offset: start,
limit: count,
order: getSort(sort),
where: { accountId }
}
return Promise.all([
WatchedWordsListModel.count(query),
WatchedWordsListModel.findAll(query)
]).then(([ total, data ]) => ({ total, data }))
}
static listForExport (options: {
accountId: number
}) {
const { accountId } = options
return WatchedWordsListModel.findAll({
limit: USER_EXPORT_MAX_ITEMS,
order: getSort('createdAt'),
where: { accountId }
})
}
// ---------------------------------------------------------------------------
static async buildWatchedWordsRegexp (options: {
accountId: number
transaction: Transaction
}) {
const { accountId, transaction } = options
if (WatchedWordsListModel.regexCache.has(accountId)) {
return WatchedWordsListModel.regexCache.get(accountId)
}
const models = await WatchedWordsListModel.findAll<MWatchedWordsList>({
where: { accountId },
transaction
})
const result = models.map(m => ({ listName: m.listName, regex: wordsToRegExp(m.words) }))
this.regexCache.set(accountId, result)
logger.debug('Will cache watched words regex', { accountId, result, tags: [ 'watched-words' ] })
return result
}
static createList (options: {
accountId: number
listName: string
words: string[]
}) {
WatchedWordsListModel.regexCache.delete(options.accountId)
return super.create(options)
}
updateList (options: {
listName: string
words?: string[]
}) {
const { listName, words } = options
if (words && words.length === 0) {
throw new Error('Cannot update watched words with an empty list')
}
if (words) this.words = words
if (listName) this.listName = listName
WatchedWordsListModel.regexCache.delete(this.accountId)
return this.save()
}
destroy () {
WatchedWordsListModel.regexCache.delete(this.accountId)
return super.destroy()
}
toFormattedJSON (): WatchedWordsList {
return {
id: this.id,
listName: this.listName,
words: this.words,
updatedAt: this.updatedAt,
createdAt: this.createdAt
}
}
}