Init commit
This commit is contained in:
112
server/core/models/abuse/abuse-message.ts
Normal file
112
server/core/models/abuse/abuse-message.ts
Normal file
@@ -0,0 +1,112 @@
|
||||
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)
|
||||
declare message: string
|
||||
|
||||
@AllowNull(false)
|
||||
@Column
|
||||
declare byModerator: boolean
|
||||
|
||||
@CreatedAt
|
||||
declare createdAt: Date
|
||||
|
||||
@UpdatedAt
|
||||
declare updatedAt: Date
|
||||
|
||||
@ForeignKey(() => AccountModel)
|
||||
@Column
|
||||
declare accountId: number
|
||||
|
||||
@BelongsTo(() => AccountModel, {
|
||||
foreignKey: {
|
||||
name: 'accountId',
|
||||
allowNull: true
|
||||
},
|
||||
onDelete: 'set null'
|
||||
})
|
||||
declare Account: Awaited<AccountModel>
|
||||
|
||||
@ForeignKey(() => AbuseModel)
|
||||
@Column
|
||||
declare abuseId: number
|
||||
|
||||
@BelongsTo(() => AbuseModel, {
|
||||
foreignKey: {
|
||||
name: 'abuseId',
|
||||
allowNull: false
|
||||
},
|
||||
onDelete: 'cascade'
|
||||
})
|
||||
declare 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
|
||||
}
|
||||
}
|
||||
}
|
||||
666
server/core/models/abuse/abuse.ts
Normal file
666
server/core/models/abuse/abuse.ts
Normal file
@@ -0,0 +1,666 @@
|
||||
import { abusePredefinedReasonsMap, forceNumber } from '@peertube/peertube-core-utils'
|
||||
import {
|
||||
AbuseFilter,
|
||||
AbuseObject,
|
||||
AbusePredefinedReasonsString,
|
||||
AbusePredefinedReasonsType,
|
||||
AbuseState,
|
||||
AbuseVideoIs,
|
||||
AdminAbuse,
|
||||
AdminVideoAbuse,
|
||||
AdminVideoCommentAbuse,
|
||||
UserAbuse,
|
||||
UserVideoAbuse,
|
||||
type AbuseStateType
|
||||
} from '@peertube/peertube-models'
|
||||
import { uuidToShort } from '@peertube/peertube-node-utils'
|
||||
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))
|
||||
declare reason: string
|
||||
|
||||
@AllowNull(false)
|
||||
@Default(null)
|
||||
@Is('AbuseState', value => throwIfNotValid(value, isAbuseStateValid, 'state'))
|
||||
@Column
|
||||
declare state: AbuseStateType
|
||||
|
||||
@AllowNull(true)
|
||||
@Default(null)
|
||||
@Is('AbuseModerationComment', value => throwIfNotValid(value, isAbuseModerationCommentValid, 'moderationComment', true))
|
||||
@Column(DataType.STRING(CONSTRAINTS_FIELDS.ABUSES.MODERATION_COMMENT.max))
|
||||
declare moderationComment: string
|
||||
|
||||
@AllowNull(true)
|
||||
@Default(null)
|
||||
@Column(DataType.ARRAY(DataType.INTEGER))
|
||||
declare predefinedReasons: AbusePredefinedReasonsType[]
|
||||
|
||||
@AllowNull(true)
|
||||
@Column
|
||||
declare processedAt: Date
|
||||
|
||||
@CreatedAt
|
||||
declare createdAt: Date
|
||||
|
||||
@UpdatedAt
|
||||
declare updatedAt: Date
|
||||
|
||||
@ForeignKey(() => AccountModel)
|
||||
@Column
|
||||
declare reporterAccountId: number
|
||||
|
||||
@BelongsTo(() => AccountModel, {
|
||||
foreignKey: {
|
||||
name: 'reporterAccountId',
|
||||
allowNull: true
|
||||
},
|
||||
as: 'ReporterAccount',
|
||||
onDelete: 'set null'
|
||||
})
|
||||
declare ReporterAccount: Awaited<AccountModel>
|
||||
|
||||
@ForeignKey(() => AccountModel)
|
||||
@Column
|
||||
declare flaggedAccountId: number
|
||||
|
||||
@BelongsTo(() => AccountModel, {
|
||||
foreignKey: {
|
||||
name: 'flaggedAccountId',
|
||||
allowNull: true
|
||||
},
|
||||
as: 'FlaggedAccount',
|
||||
onDelete: 'set null'
|
||||
})
|
||||
declare FlaggedAccount: Awaited<AccountModel>
|
||||
|
||||
@HasOne(() => VideoCommentAbuseModel, {
|
||||
foreignKey: {
|
||||
name: 'abuseId',
|
||||
allowNull: false
|
||||
},
|
||||
onDelete: 'cascade'
|
||||
})
|
||||
declare VideoCommentAbuse: Awaited<VideoCommentAbuseModel>
|
||||
|
||||
@HasOne(() => VideoAbuseModel, {
|
||||
foreignKey: {
|
||||
name: 'abuseId',
|
||||
allowNull: false
|
||||
},
|
||||
onDelete: 'cascade'
|
||||
})
|
||||
declare 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,
|
||||
shortUUID: uuidToShort(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
server/core/models/abuse/sql/abuse-query-builder.ts
Normal file
166
server/core/models/abuse/sql/abuse-query-builder.ts
Normal file
@@ -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
|
||||
}
|
||||
63
server/core/models/abuse/video-abuse.ts
Normal file
63
server/core/models/abuse/video-abuse.ts
Normal file
@@ -0,0 +1,63 @@
|
||||
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
|
||||
declare createdAt: Date
|
||||
|
||||
@UpdatedAt
|
||||
declare updatedAt: Date
|
||||
|
||||
@AllowNull(true)
|
||||
@Default(null)
|
||||
@Column
|
||||
declare startAt: number
|
||||
|
||||
@AllowNull(true)
|
||||
@Default(null)
|
||||
@Column
|
||||
declare endAt: number
|
||||
|
||||
@AllowNull(true)
|
||||
@Default(null)
|
||||
@Column(DataType.JSONB)
|
||||
declare deletedVideo: VideoDetails
|
||||
|
||||
@ForeignKey(() => AbuseModel)
|
||||
@Column
|
||||
declare abuseId: number
|
||||
|
||||
@BelongsTo(() => AbuseModel, {
|
||||
foreignKey: {
|
||||
allowNull: false
|
||||
},
|
||||
onDelete: 'cascade'
|
||||
})
|
||||
declare Abuse: Awaited<AbuseModel>
|
||||
|
||||
@ForeignKey(() => VideoModel)
|
||||
@Column
|
||||
declare videoId: number
|
||||
|
||||
@BelongsTo(() => VideoModel, {
|
||||
foreignKey: {
|
||||
allowNull: true
|
||||
},
|
||||
onDelete: 'set null'
|
||||
})
|
||||
declare Video: Awaited<VideoModel>
|
||||
}
|
||||
47
server/core/models/abuse/video-comment-abuse.ts
Normal file
47
server/core/models/abuse/video-comment-abuse.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
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
|
||||
declare createdAt: Date
|
||||
|
||||
@UpdatedAt
|
||||
declare updatedAt: Date
|
||||
|
||||
@ForeignKey(() => AbuseModel)
|
||||
@Column
|
||||
declare abuseId: number
|
||||
|
||||
@BelongsTo(() => AbuseModel, {
|
||||
foreignKey: {
|
||||
allowNull: false
|
||||
},
|
||||
onDelete: 'cascade'
|
||||
})
|
||||
declare Abuse: Awaited<AbuseModel>
|
||||
|
||||
@ForeignKey(() => VideoCommentModel)
|
||||
@Column
|
||||
declare videoCommentId: number
|
||||
|
||||
@BelongsTo(() => VideoCommentModel, {
|
||||
foreignKey: {
|
||||
allowNull: true
|
||||
},
|
||||
onDelete: 'set null'
|
||||
})
|
||||
declare VideoComment: Awaited<VideoCommentModel>
|
||||
}
|
||||
241
server/core/models/account/account-blocklist.ts
Normal file
241
server/core/models/account/account-blocklist.ts
Normal file
@@ -0,0 +1,241 @@
|
||||
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
|
||||
declare createdAt: Date
|
||||
|
||||
@UpdatedAt
|
||||
declare updatedAt: Date
|
||||
|
||||
@ForeignKey(() => AccountModel)
|
||||
@Column
|
||||
declare accountId: number
|
||||
|
||||
@BelongsTo(() => AccountModel, {
|
||||
foreignKey: {
|
||||
name: 'accountId',
|
||||
allowNull: false
|
||||
},
|
||||
as: 'ByAccount',
|
||||
onDelete: 'CASCADE'
|
||||
})
|
||||
declare ByAccount: Awaited<AccountModel>
|
||||
|
||||
@ForeignKey(() => AccountModel)
|
||||
@Column
|
||||
declare targetAccountId: number
|
||||
|
||||
@BelongsTo(() => AccountModel, {
|
||||
foreignKey: {
|
||||
name: 'targetAccountId',
|
||||
allowNull: false
|
||||
},
|
||||
as: 'BlockedAccount',
|
||||
onDelete: 'CASCADE'
|
||||
})
|
||||
declare 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"."accountId" = "account"."id" ` +
|
||||
`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
|
||||
}
|
||||
}
|
||||
}
|
||||
279
server/core/models/account/account-video-rate.ts
Normal file
279
server/core/models/account/account-video-rate.ts
Normal file
@@ -0,0 +1,279 @@
|
||||
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)))
|
||||
declare type: VideoRateType
|
||||
|
||||
@AllowNull(false)
|
||||
@Is('AccountVideoRateUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
|
||||
@Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_RATES.URL.max))
|
||||
declare url: string
|
||||
|
||||
@CreatedAt
|
||||
declare createdAt: Date
|
||||
|
||||
@UpdatedAt
|
||||
declare updatedAt: Date
|
||||
|
||||
@ForeignKey(() => VideoModel)
|
||||
@Column
|
||||
declare videoId: number
|
||||
|
||||
@BelongsTo(() => VideoModel, {
|
||||
foreignKey: {
|
||||
allowNull: false
|
||||
},
|
||||
onDelete: 'CASCADE'
|
||||
})
|
||||
declare Video: Awaited<VideoModel>
|
||||
|
||||
@ForeignKey(() => AccountModel)
|
||||
@Column
|
||||
declare accountId: number
|
||||
|
||||
@BelongsTo(() => AccountModel, {
|
||||
foreignKey: {
|
||||
allowNull: false
|
||||
},
|
||||
onDelete: 'CASCADE'
|
||||
})
|
||||
declare 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"."accountId" = account."id" 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: [ 'id' ],
|
||||
model: AccountModel.unscoped(),
|
||||
required: true,
|
||||
include: [
|
||||
{
|
||||
attributes: [ 'url', 'accountId' ],
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
533
server/core/models/account/account.ts
Normal file
533
server/core/models/account/account.ts
Normal file
@@ -0,0 +1,533 @@
|
||||
import { Account, AccountSummary, ActivityPubActor, ActivityUrlObject, VideoPrivacy } from '@peertube/peertube-models'
|
||||
import { AttributesOnly } from '@peertube/peertube-typescript-utils'
|
||||
import { ModelCache } from '@server/models/shared/model-cache.js'
|
||||
import { FindOptions, IncludeOptions, Includeable, Op, Transaction, WhereOptions, literal } from 'sequelize'
|
||||
import {
|
||||
AllowNull,
|
||||
BeforeDestroy,
|
||||
BelongsTo,
|
||||
Column,
|
||||
CreatedAt,
|
||||
DataType,
|
||||
Default,
|
||||
DefaultScope,
|
||||
ForeignKey,
|
||||
HasMany,
|
||||
HasOne,
|
||||
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,
|
||||
MAccountIdHost,
|
||||
MAccountSummaryFormattable,
|
||||
MChannelIdHost
|
||||
} from '../../types/models/index.js'
|
||||
import { ActorImageModel } from '../actor/actor-image.js'
|
||||
import { ActorModel, actorSummaryAttributes } 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, serverSummaryAttributes } from '../server/server.js'
|
||||
import { SequelizeModel, buildSQLAttributes, getSort, throwIfNotValid } from '../shared/index.js'
|
||||
import { UserModel } from '../user/user.js'
|
||||
import { VideoChannelCollaboratorModel } from '../video/video-channel-collaborator.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'
|
||||
}
|
||||
|
||||
const accountSummaryAttributes = [ 'id', 'name' ] as const satisfies (keyof AttributesOnly<AccountModel>)[]
|
||||
|
||||
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: serverSummaryAttributes,
|
||||
model: ServerModel.unscoped(),
|
||||
required: !!options.whereServer,
|
||||
where: options.whereServer
|
||||
}
|
||||
|
||||
const actorInclude: Includeable = {
|
||||
attributes: actorSummaryAttributes,
|
||||
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: accountSummaryAttributes
|
||||
}
|
||||
|
||||
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: [ 'applicationId' ]
|
||||
},
|
||||
{
|
||||
fields: [ 'userId' ]
|
||||
}
|
||||
]
|
||||
})
|
||||
export class AccountModel extends SequelizeModel<AccountModel> {
|
||||
@AllowNull(false)
|
||||
@Column
|
||||
declare name: string
|
||||
|
||||
@AllowNull(true)
|
||||
@Default(null)
|
||||
@Is('AccountDescription', value => throwIfNotValid(value, isAccountDescriptionValid, 'description', true))
|
||||
@Column(DataType.STRING(CONSTRAINTS_FIELDS.USERS.DESCRIPTION.max))
|
||||
declare description: string
|
||||
|
||||
@CreatedAt
|
||||
declare createdAt: Date
|
||||
|
||||
@UpdatedAt
|
||||
declare updatedAt: Date
|
||||
|
||||
@ForeignKey(() => UserModel)
|
||||
@Column
|
||||
declare userId: number
|
||||
|
||||
@BelongsTo(() => UserModel, {
|
||||
foreignKey: {
|
||||
allowNull: true
|
||||
},
|
||||
onDelete: 'cascade'
|
||||
})
|
||||
declare User: Awaited<UserModel>
|
||||
|
||||
@ForeignKey(() => ApplicationModel)
|
||||
@Column
|
||||
declare applicationId: number
|
||||
|
||||
@BelongsTo(() => ApplicationModel, {
|
||||
foreignKey: {
|
||||
allowNull: true
|
||||
},
|
||||
onDelete: 'cascade'
|
||||
})
|
||||
declare Application: Awaited<ApplicationModel>
|
||||
|
||||
@HasMany(() => VideoChannelModel, {
|
||||
foreignKey: {
|
||||
allowNull: false
|
||||
},
|
||||
onDelete: 'cascade',
|
||||
hooks: true
|
||||
})
|
||||
declare VideoChannels: Awaited<VideoChannelModel>[]
|
||||
|
||||
@HasMany(() => VideoPlaylistModel, {
|
||||
foreignKey: {
|
||||
allowNull: false
|
||||
},
|
||||
onDelete: 'cascade',
|
||||
hooks: true
|
||||
})
|
||||
declare VideoPlaylists: Awaited<VideoPlaylistModel>[]
|
||||
|
||||
@HasMany(() => VideoCommentModel, {
|
||||
foreignKey: {
|
||||
allowNull: true
|
||||
},
|
||||
onDelete: 'cascade',
|
||||
hooks: true
|
||||
})
|
||||
declare VideoComments: Awaited<VideoCommentModel>[]
|
||||
|
||||
@HasMany(() => AccountBlocklistModel, {
|
||||
foreignKey: {
|
||||
name: 'targetAccountId',
|
||||
allowNull: false
|
||||
},
|
||||
as: 'BlockedBy',
|
||||
onDelete: 'CASCADE'
|
||||
})
|
||||
declare BlockedBy: Awaited<AccountBlocklistModel>[]
|
||||
|
||||
@HasMany(() => AccountAutomaticTagPolicyModel, {
|
||||
foreignKey: {
|
||||
name: 'accountId',
|
||||
allowNull: false
|
||||
},
|
||||
onDelete: 'cascade'
|
||||
})
|
||||
declare AccountAutomaticTagPolicies: Awaited<AccountAutomaticTagPolicyModel>[]
|
||||
|
||||
@HasMany(() => CommentAutomaticTagModel, {
|
||||
foreignKey: 'accountId',
|
||||
onDelete: 'CASCADE'
|
||||
})
|
||||
declare CommentAutomaticTags: Awaited<CommentAutomaticTagModel>[]
|
||||
|
||||
@HasMany(() => VideoAutomaticTagModel, {
|
||||
foreignKey: 'accountId',
|
||||
onDelete: 'CASCADE'
|
||||
})
|
||||
declare VideoAutomaticTags: Awaited<VideoAutomaticTagModel>[]
|
||||
|
||||
@HasMany(() => VideoChannelCollaboratorModel, {
|
||||
foreignKey: 'accountId',
|
||||
onDelete: 'CASCADE'
|
||||
})
|
||||
declare VideoChannelCollaborators: Awaited<VideoChannelCollaboratorModel>[]
|
||||
|
||||
@HasOne(() => ActorModel, {
|
||||
foreignKey: {
|
||||
allowNull: true
|
||||
},
|
||||
hooks: true,
|
||||
onDelete: 'cascade'
|
||||
})
|
||||
declare Actor: Awaited<ActorModel>
|
||||
|
||||
@BeforeDestroy
|
||||
static async sendDeleteIfOwned (instance: AccountModel, options) {
|
||||
if (!instance.Actor) {
|
||||
instance.Actor = await instance.$get('Actor', { transaction: options.transaction })
|
||||
}
|
||||
|
||||
if (instance.isLocal()) {
|
||||
return sendDeleteActor(instance.Actor, options.transaction)
|
||||
}
|
||||
|
||||
return undefined
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
static getSQLAttributes (tableName: string, aliasPrefix = '') {
|
||||
return buildSQLAttributes({
|
||||
model: this,
|
||||
tableName,
|
||||
aliasPrefix
|
||||
})
|
||||
}
|
||||
|
||||
static getSQLSummaryAttributes (tableName: string, aliasPrefix = '') {
|
||||
return buildSQLAttributes({
|
||||
model: this,
|
||||
tableName,
|
||||
aliasPrefix,
|
||||
includeAttributes: accountSummaryAttributes
|
||||
})
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
static load (id: number, transaction?: Transaction): Promise<MAccountDefault> {
|
||||
return AccountModel.findByPk(id, { transaction })
|
||||
}
|
||||
|
||||
static loadByHandle (handle: string): Promise<MAccountDefault> {
|
||||
const [ accountName, host ] = handle.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 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 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 listLocalsForSitemap (sort: string): Promise<MAccountHost[]> {
|
||||
return AccountModel.unscoped().findAll({
|
||||
attributes: [],
|
||||
offset: 0,
|
||||
order: getSort(sort),
|
||||
include: [
|
||||
{
|
||||
attributes: [ 'preferredUsername', 'serverId' ],
|
||||
model: ActorModel.unscoped(),
|
||||
where: {
|
||||
serverId: null
|
||||
}
|
||||
},
|
||||
{
|
||||
attributes: [ 'id' ],
|
||||
model: VideoChannelModel.unscoped(),
|
||||
required: true,
|
||||
where: {
|
||||
[Op.and]: [
|
||||
literal(`EXISTS (SELECT 1 FROM "video" WHERE "privacy" = ${VideoPrivacy.PUBLIC} AND "channelId" = "VideoChannels"."id")`)
|
||||
]
|
||||
}
|
||||
}
|
||||
]
|
||||
})
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
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): Promise<ActivityPubActor> {
|
||||
const obj = await this.Actor.toActivityPubObject(this.name)
|
||||
|
||||
return Object.assign(obj, {
|
||||
url: [
|
||||
{
|
||||
type: 'Link',
|
||||
mediaType: 'text/html',
|
||||
href: this.getClientUrl(true)
|
||||
},
|
||||
{
|
||||
type: 'Link',
|
||||
mediaType: 'text/html',
|
||||
href: this.getClientUrl(false)
|
||||
},
|
||||
{
|
||||
type: 'Link',
|
||||
mediaType: 'text/html',
|
||||
href: this.Actor.url
|
||||
}
|
||||
] as ActivityUrlObject[],
|
||||
|
||||
summary: this.description
|
||||
})
|
||||
}
|
||||
|
||||
isLocal () {
|
||||
return this.Actor.isLocal()
|
||||
}
|
||||
|
||||
isOutdated () {
|
||||
return this.Actor.isOutdated()
|
||||
}
|
||||
|
||||
getDisplayName () {
|
||||
return this.name
|
||||
}
|
||||
|
||||
// Avoid error when running this method on MAccount... | MChannel...
|
||||
getClientUrl (this: MAccountIdHost | MChannelIdHost, channelsSuffix = true) {
|
||||
const suffix = channelsSuffix
|
||||
? '/video-channels'
|
||||
: ''
|
||||
|
||||
return WEBSERVER.URL + '/a/' + this.Actor.getIdentifier() + suffix
|
||||
}
|
||||
|
||||
isBlocked () {
|
||||
return this.BlockedBy && this.BlockedBy.length !== 0
|
||||
}
|
||||
}
|
||||
69
server/core/models/account/actor-custom-page.ts
Normal file
69
server/core/models/account/actor-custom-page.ts
Normal file
@@ -0,0 +1,69 @@
|
||||
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)
|
||||
declare content: string
|
||||
|
||||
@AllowNull(false)
|
||||
@Column
|
||||
declare type: 'homepage'
|
||||
|
||||
@CreatedAt
|
||||
declare createdAt: Date
|
||||
|
||||
@UpdatedAt
|
||||
declare updatedAt: Date
|
||||
|
||||
@ForeignKey(() => ActorModel)
|
||||
@Column
|
||||
declare actorId: number
|
||||
|
||||
@BelongsTo(() => ActorModel, {
|
||||
foreignKey: {
|
||||
name: 'actorId',
|
||||
allowNull: false
|
||||
},
|
||||
onDelete: 'cascade'
|
||||
})
|
||||
declare 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
|
||||
}
|
||||
}
|
||||
}
|
||||
749
server/core/models/actor/actor-follow.ts
Normal file
749
server/core/models/actor/actor-follow.ts
Normal file
@@ -0,0 +1,749 @@
|
||||
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, getSubscriptionSort, 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)))
|
||||
declare state: FollowState
|
||||
|
||||
@AllowNull(false)
|
||||
@Default(ACTOR_FOLLOW_SCORE.BASE)
|
||||
@IsInt
|
||||
@Max(ACTOR_FOLLOW_SCORE.MAX)
|
||||
@Column
|
||||
declare 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))
|
||||
declare url: string
|
||||
|
||||
@CreatedAt
|
||||
declare createdAt: Date
|
||||
|
||||
@UpdatedAt
|
||||
declare updatedAt: Date
|
||||
|
||||
@ForeignKey(() => ActorModel)
|
||||
@Column
|
||||
declare actorId: number
|
||||
|
||||
@BelongsTo(() => ActorModel, {
|
||||
foreignKey: {
|
||||
name: 'actorId',
|
||||
allowNull: false
|
||||
},
|
||||
as: 'ActorFollower',
|
||||
onDelete: 'CASCADE'
|
||||
})
|
||||
declare ActorFollower: Awaited<ActorModel>
|
||||
|
||||
@ForeignKey(() => ActorModel)
|
||||
@Column
|
||||
declare targetActorId: number
|
||||
|
||||
@BelongsTo(() => ActorModel, {
|
||||
foreignKey: {
|
||||
name: 'targetActorId',
|
||||
allowNull: false
|
||||
},
|
||||
as: 'ActorFollowing',
|
||||
onDelete: 'CASCADE'
|
||||
})
|
||||
declare ActorFollowing: Awaited<ActorModel>
|
||||
|
||||
@AfterCreate
|
||||
static incrementFollowerAndFollowingCount (instance: ActorFollowModel, options: any) {
|
||||
let by = 0
|
||||
|
||||
if (instance.state === 'accepted') by = 1
|
||||
|
||||
return afterCommitIfTransaction(options.transaction, () => {
|
||||
return Promise.all([
|
||||
ActorModel.recalculateFollowsCount({ ofId: instance.actorId, type: 'following', by }),
|
||||
ActorModel.recalculateFollowsCount({ ofId: instance.targetActorId, type: 'followers', by })
|
||||
])
|
||||
})
|
||||
}
|
||||
|
||||
@AfterUpdate
|
||||
static incrementOrDecrementFollowerAndFollowingCount (instance: ActorFollowModel, options: any) {
|
||||
let by = 0
|
||||
|
||||
if (instance.previous('state') !== 'accepted' && instance.state === 'accepted') by = 1
|
||||
else if (instance.previous('state') === 'accepted' && instance.state !== 'accepted') by = -1
|
||||
|
||||
return afterCommitIfTransaction(options.transaction, () => {
|
||||
return Promise.all([
|
||||
ActorModel.recalculateFollowsCount({ ofId: instance.actorId, type: 'following', by }),
|
||||
ActorModel.recalculateFollowsCount({ ofId: instance.targetActorId, type: 'followers', by })
|
||||
])
|
||||
})
|
||||
}
|
||||
|
||||
@AfterDestroy
|
||||
static decrementFollowerAndFollowingCount (instance: ActorFollowModel, options: any) {
|
||||
let by = 0
|
||||
|
||||
if (instance.state === 'accepted') by = -1
|
||||
|
||||
return afterCommitIfTransaction(options.transaction, () => {
|
||||
return Promise.all([
|
||||
ActorModel.recalculateFollowsCount({ ofId: instance.actorId, type: 'following', by }),
|
||||
ActorModel.recalculateFollowsCount({ ofId: instance.targetActorId, type: 'followers', by })
|
||||
])
|
||||
})
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
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 ]
|
||||
}
|
||||
|
||||
// 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).count(),
|
||||
new InstanceListFollowingQueryBuilder(this.sequelize, options).list<MActorFollowFormattable>()
|
||||
]).then(([ total, data ]) => ({ total, data }))
|
||||
}
|
||||
|
||||
static listFollowersForApi (options: ListFollowersOptions) {
|
||||
return Promise.all([
|
||||
new InstanceListFollowersQueryBuilder(this.sequelize, options).count(),
|
||||
new InstanceListFollowersQueryBuilder(this.sequelize, options).list<MActorFollowFormattable>()
|
||||
]).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.filter(s => s !== 'channelUpdatedAt'),
|
||||
distinct: true,
|
||||
offset: start,
|
||||
limit: count,
|
||||
order: getSubscriptionSort(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.Actor.id} ` + // 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
|
||||
}
|
||||
}
|
||||
}
|
||||
201
server/core/models/actor/actor-image.ts
Normal file
201
server/core/models/actor/actor-image.ts
Normal file
@@ -0,0 +1,201 @@
|
||||
import { ActivityIconObject, ActorImage, ActorImageType, type ActorImageType_Type } from '@peertube/peertube-models'
|
||||
import { getLowercaseExtension } from '@peertube/peertube-node-utils'
|
||||
import { MActorId, MActorImage, MActorImageFormattable, MActorImagePath } from '@server/types/models/index.js'
|
||||
import { remove } from 'fs-extra/esm'
|
||||
import { join } from 'path'
|
||||
import { Op, Transaction } 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
|
||||
declare filename: string
|
||||
|
||||
@AllowNull(true)
|
||||
@Default(null)
|
||||
@Column
|
||||
declare height: number
|
||||
|
||||
@AllowNull(true)
|
||||
@Default(null)
|
||||
@Column
|
||||
declare width: number
|
||||
|
||||
@AllowNull(true)
|
||||
@Column
|
||||
declare fileUrl: string
|
||||
|
||||
@AllowNull(false)
|
||||
@Column
|
||||
declare onDisk: boolean
|
||||
|
||||
@AllowNull(false)
|
||||
@Column
|
||||
declare type: ActorImageType_Type
|
||||
|
||||
@CreatedAt
|
||||
declare createdAt: Date
|
||||
|
||||
@UpdatedAt
|
||||
declare updatedAt: Date
|
||||
|
||||
@ForeignKey(() => ActorModel)
|
||||
@Column
|
||||
declare actorId: number
|
||||
|
||||
@BelongsTo(() => ActorModel, {
|
||||
foreignKey: {
|
||||
allowNull: false
|
||||
},
|
||||
onDelete: 'CASCADE'
|
||||
})
|
||||
declare 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, transaction?: Transaction) {
|
||||
const query = {
|
||||
where: {
|
||||
actorId: actor.id,
|
||||
type
|
||||
},
|
||||
transaction
|
||||
}
|
||||
|
||||
return ActorImageModel.findAll(query)
|
||||
}
|
||||
|
||||
static async listActorImages (actor: MActorId, transaction?: Transaction) {
|
||||
const promises = [ ActorImageType.AVATAR, ActorImageType.BANNER ].map(type => ActorImageModel.listByActor(actor, type, transaction))
|
||||
|
||||
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: MActorImagePath) {
|
||||
if (!image) return undefined
|
||||
|
||||
return WEBSERVER.URL + image.getStaticPath()
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
toFormattedJSON (this: MActorImageFormattable): ActorImage {
|
||||
return {
|
||||
height: this.height,
|
||||
width: this.width,
|
||||
path: this.getStaticPath(),
|
||||
fileUrl: ActorImageModel.getImageUrl(this),
|
||||
createdAt: this.createdAt,
|
||||
updatedAt: this.updatedAt
|
||||
}
|
||||
}
|
||||
|
||||
toActivityPubObject (): ActivityIconObject {
|
||||
return {
|
||||
type: 'Image',
|
||||
mediaType: this.getMimeType(),
|
||||
height: this.height,
|
||||
width: this.width,
|
||||
url: ActorImageModel.getImageUrl(this)
|
||||
}
|
||||
}
|
||||
|
||||
getStaticPath (this: MActorImagePath) {
|
||||
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)
|
||||
}
|
||||
|
||||
isLocal () {
|
||||
return !this.fileUrl
|
||||
}
|
||||
|
||||
getMimeType () {
|
||||
return MIMETYPES.IMAGE.EXT_MIMETYPE[getLowercaseExtension(this.filename)]
|
||||
}
|
||||
}
|
||||
67
server/core/models/actor/actor-reserved.ts
Normal file
67
server/core/models/actor/actor-reserved.ts
Normal file
@@ -0,0 +1,67 @@
|
||||
import { CONSTRAINTS_FIELDS } from '@server/initializers/constants.js'
|
||||
import { col, fn, Transaction, where } from 'sequelize'
|
||||
import { AllowNull, Column, DataType, Table, UpdatedAt } from 'sequelize-typescript'
|
||||
import { SequelizeModel } from '../shared/index.js'
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// For example deleted actors
|
||||
// Usernames we want to reserve to prevent reuse that would break federation
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
@Table({
|
||||
tableName: 'actorReserved',
|
||||
indexes: [
|
||||
{
|
||||
fields: [ fn('lower', col('preferredUsername')) ],
|
||||
name: 'actor_reserved_preferred_username_lower',
|
||||
unique: true
|
||||
}
|
||||
]
|
||||
})
|
||||
export class ActorReservedModel extends SequelizeModel<ActorReservedModel> {
|
||||
@AllowNull(false)
|
||||
@Column
|
||||
declare preferredUsername: string
|
||||
|
||||
@AllowNull(false)
|
||||
@Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.PUBLIC_KEY.max))
|
||||
declare publicKey: string
|
||||
|
||||
@AllowNull(false)
|
||||
@Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.PRIVATE_KEY.max))
|
||||
declare privateKey: string
|
||||
|
||||
@AllowNull(false)
|
||||
@Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
|
||||
declare url: string
|
||||
|
||||
@AllowNull(false)
|
||||
@Column
|
||||
actorId: number
|
||||
|
||||
@AllowNull(true)
|
||||
@Column
|
||||
accountId: number
|
||||
|
||||
@AllowNull(true)
|
||||
@Column
|
||||
videoChannelId: number
|
||||
|
||||
@UpdatedAt
|
||||
declare updatedAt: Date
|
||||
|
||||
static loadByActorId (actorId: number) {
|
||||
return this.findOne({
|
||||
where: { actorId }
|
||||
})
|
||||
}
|
||||
|
||||
static loadByName (preferredUsername: string, transaction?: Transaction) {
|
||||
return this.findOne({
|
||||
where: [
|
||||
where(fn('lower', col('preferredUsername')), preferredUsername.toLowerCase())
|
||||
],
|
||||
transaction
|
||||
})
|
||||
}
|
||||
}
|
||||
824
server/core/models/actor/actor.ts
Normal file
824
server/core/models/actor/actor.ts
Normal file
@@ -0,0 +1,824 @@
|
||||
import { findAppropriateImage, 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,
|
||||
BeforeDestroy,
|
||||
BelongsTo,
|
||||
Column,
|
||||
CreatedAt,
|
||||
DataType,
|
||||
DefaultScope,
|
||||
ForeignKey,
|
||||
HasMany,
|
||||
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 { UploadImageModel } from '../application/upload-image.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'
|
||||
import { ActorReservedModel } from './actor-reserved.js'
|
||||
|
||||
export const actorSummaryAttributes = [
|
||||
'id',
|
||||
'preferredUsername',
|
||||
'url',
|
||||
'serverId',
|
||||
'accountId',
|
||||
'videoChannelId'
|
||||
] as const satisfies (keyof AttributesOnly<ActorModel>)[]
|
||||
|
||||
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' ]
|
||||
},
|
||||
{
|
||||
fields: [ 'accountId' ],
|
||||
unique: true
|
||||
},
|
||||
{
|
||||
fields: [ 'videoChannelId' ],
|
||||
unique: true
|
||||
}
|
||||
]
|
||||
})
|
||||
export class ActorModel extends SequelizeModel<ActorModel> {
|
||||
@AllowNull(false)
|
||||
@Column(DataType.ENUM(...Object.values(ACTIVITY_PUB_ACTOR_TYPES)))
|
||||
declare type: ActivityPubActorType
|
||||
|
||||
@AllowNull(false)
|
||||
@Is('ActorPreferredUsername', value => throwIfNotValid(value, isActorPreferredUsernameValid, 'actor preferred username'))
|
||||
@Column
|
||||
declare preferredUsername: string
|
||||
|
||||
@AllowNull(false)
|
||||
@Is('ActorUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
|
||||
@Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
|
||||
declare url: string
|
||||
|
||||
@AllowNull(true)
|
||||
@Is('ActorPublicKey', value => throwIfNotValid(value, isActorPublicKeyValid, 'public key', true))
|
||||
@Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.PUBLIC_KEY.max))
|
||||
declare publicKey: string
|
||||
|
||||
@AllowNull(true)
|
||||
@Is('ActorPublicKey', value => throwIfNotValid(value, isActorPrivateKeyValid, 'private key', true))
|
||||
@Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.PRIVATE_KEY.max))
|
||||
declare privateKey: string
|
||||
|
||||
@AllowNull(false)
|
||||
@Is('ActorFollowersCount', value => throwIfNotValid(value, isActorFollowersCountValid, 'followers count'))
|
||||
@Column
|
||||
declare followersCount: number
|
||||
|
||||
@AllowNull(false)
|
||||
@Is('ActorFollowersCount', value => throwIfNotValid(value, isActorFollowingCountValid, 'following count'))
|
||||
@Column
|
||||
declare followingCount: number
|
||||
|
||||
@AllowNull(false)
|
||||
@Is('ActorInboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'inbox url'))
|
||||
@Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
|
||||
declare inboxUrl: string
|
||||
|
||||
@AllowNull(true)
|
||||
@Is('ActorOutboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'outbox url', true))
|
||||
@Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
|
||||
declare outboxUrl: string
|
||||
|
||||
@AllowNull(true)
|
||||
@Is('ActorSharedInboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'shared inbox url', true))
|
||||
@Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
|
||||
declare sharedInboxUrl: string
|
||||
|
||||
@AllowNull(true)
|
||||
@Is('ActorFollowersUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'followers url', true))
|
||||
@Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
|
||||
declare followersUrl: string
|
||||
|
||||
@AllowNull(true)
|
||||
@Is('ActorFollowingUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'following url', true))
|
||||
@Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
|
||||
declare followingUrl: string
|
||||
|
||||
@AllowNull(true)
|
||||
@Column
|
||||
declare remoteCreatedAt: Date
|
||||
|
||||
@CreatedAt
|
||||
declare createdAt: Date
|
||||
|
||||
@UpdatedAt
|
||||
declare updatedAt: Date
|
||||
|
||||
@HasMany(() => ActorImageModel, {
|
||||
as: 'Avatars',
|
||||
onDelete: 'cascade',
|
||||
hooks: true,
|
||||
foreignKey: {
|
||||
allowNull: false
|
||||
},
|
||||
scope: {
|
||||
type: ActorImageType.AVATAR
|
||||
}
|
||||
})
|
||||
declare Avatars: Awaited<ActorImageModel>[]
|
||||
|
||||
@HasMany(() => ActorImageModel, {
|
||||
as: 'Banners',
|
||||
onDelete: 'cascade',
|
||||
hooks: true,
|
||||
foreignKey: {
|
||||
allowNull: false
|
||||
},
|
||||
scope: {
|
||||
type: ActorImageType.BANNER
|
||||
}
|
||||
})
|
||||
declare Banners: Awaited<ActorImageModel>[]
|
||||
|
||||
@HasMany(() => UploadImageModel, {
|
||||
as: 'UploadImages',
|
||||
onDelete: 'cascade',
|
||||
hooks: true,
|
||||
foreignKey: {
|
||||
allowNull: false
|
||||
}
|
||||
})
|
||||
declare UploadImages: Awaited<UploadImageModel>[]
|
||||
|
||||
@HasMany(() => ActorFollowModel, {
|
||||
foreignKey: {
|
||||
name: 'actorId',
|
||||
allowNull: false
|
||||
},
|
||||
as: 'ActorFollowings',
|
||||
onDelete: 'cascade'
|
||||
})
|
||||
declare ActorFollowing: Awaited<ActorFollowModel>[]
|
||||
|
||||
@HasMany(() => ActorFollowModel, {
|
||||
foreignKey: {
|
||||
name: 'targetActorId',
|
||||
allowNull: false
|
||||
},
|
||||
as: 'ActorFollowers',
|
||||
onDelete: 'cascade'
|
||||
})
|
||||
declare ActorFollowers: Awaited<ActorFollowModel>[]
|
||||
|
||||
@ForeignKey(() => ServerModel)
|
||||
@Column
|
||||
declare serverId: number
|
||||
|
||||
@BelongsTo(() => ServerModel, {
|
||||
foreignKey: {
|
||||
allowNull: true
|
||||
},
|
||||
onDelete: 'cascade'
|
||||
})
|
||||
declare Server: Awaited<ServerModel>
|
||||
|
||||
@ForeignKey(() => VideoChannelModel)
|
||||
@Column
|
||||
declare videoChannelId: number
|
||||
|
||||
@BelongsTo(() => VideoChannelModel, {
|
||||
foreignKey: {
|
||||
allowNull: true
|
||||
},
|
||||
onDelete: 'cascade'
|
||||
})
|
||||
declare VideoChannel: Awaited<VideoChannelModel>
|
||||
|
||||
@ForeignKey(() => AccountModel)
|
||||
@Column
|
||||
declare accountId: number
|
||||
|
||||
@BelongsTo(() => AccountModel, {
|
||||
foreignKey: {
|
||||
allowNull: true
|
||||
},
|
||||
onDelete: 'cascade'
|
||||
})
|
||||
declare Account: Awaited<AccountModel>
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
@BeforeDestroy
|
||||
static async reserveActor (instance: ActorModel, options) {
|
||||
if (instance.isLocal()) {
|
||||
await ActorReservedModel.create({
|
||||
preferredUsername: instance.preferredUsername,
|
||||
url: instance.url,
|
||||
publicKey: instance.publicKey,
|
||||
privateKey: instance.privateKey,
|
||||
actorId: instance.id,
|
||||
accountId: instance.accountId,
|
||||
videoChannelId: instance.videoChannelId
|
||||
}, { transaction: options.transaction })
|
||||
}
|
||||
|
||||
return undefined
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
static getSQLAttributes (tableName: string, aliasPrefix = '') {
|
||||
return buildSQLAttributes({
|
||||
model: this,
|
||||
tableName,
|
||||
aliasPrefix
|
||||
})
|
||||
}
|
||||
|
||||
static getSQLAPIAttributes (tableName: string, aliasPrefix = '') {
|
||||
return buildSQLAttributes({
|
||||
model: this,
|
||||
tableName,
|
||||
aliasPrefix,
|
||||
excludeAttributes: unusedActorAttributesForAPI
|
||||
})
|
||||
}
|
||||
|
||||
static getSQLSummaryAttributes (tableName: string, aliasPrefix = '') {
|
||||
return buildSQLAttributes({
|
||||
model: ActorModel,
|
||||
tableName,
|
||||
aliasPrefix,
|
||||
includeAttributes: actorSummaryAttributes
|
||||
})
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
// 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, transaction?: Transaction): Promise<MActor> {
|
||||
const actorServer = await getServerActor()
|
||||
if (id === actorServer.id) return actorServer
|
||||
|
||||
return ActorModel.unscoped().findByPk(id, { transaction })
|
||||
}
|
||||
|
||||
static loadFull (id: number, transaction?: Transaction): Promise<MActorFull> {
|
||||
return ActorModel.scope(ScopeNames.FULL).findByPk(id, { transaction })
|
||||
}
|
||||
|
||||
static loadAccountActorFollowerUrlByVideoId (videoId: number, transaction: Transaction) {
|
||||
const query = `SELECT "actor"."id" AS "id", "actor"."followersUrl" AS "followersUrl" ` +
|
||||
`FROM "actor" ` +
|
||||
`INNER JOIN "account" ON "actor"."accountId" = "account"."id" ` +
|
||||
`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 loadByUniqueKeys (options: {
|
||||
preferredUsername: string
|
||||
serverId: number
|
||||
url: string
|
||||
transaction: Transaction
|
||||
}) {
|
||||
const { preferredUsername, serverId, url, transaction } = options
|
||||
|
||||
return ActorModel.findOne({
|
||||
where: {
|
||||
[Op.or]: [
|
||||
{
|
||||
url
|
||||
},
|
||||
{
|
||||
serverId,
|
||||
preferredUsername
|
||||
}
|
||||
]
|
||||
},
|
||||
transaction
|
||||
})
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
static async recalculateFollowsCount (options: {
|
||||
ofId: number
|
||||
type: 'followers' | 'following'
|
||||
by: number
|
||||
transaction?: Transaction
|
||||
}) {
|
||||
const { transaction, ofId, type, by } = options
|
||||
|
||||
const sanitizedOfId = forceNumber(ofId)
|
||||
const where = { id: sanitizedOfId }
|
||||
|
||||
let columnToUpdate: 'followersCount' | 'followingCount'
|
||||
let columnOfCount: string
|
||||
|
||||
if (type === 'followers') {
|
||||
columnToUpdate = 'followersCount'
|
||||
columnOfCount = 'targetActorId'
|
||||
} else {
|
||||
columnToUpdate = 'followingCount'
|
||||
columnOfCount = 'actorId'
|
||||
}
|
||||
|
||||
const actor = await this.load(ofId, transaction)
|
||||
|
||||
// Remote actor where we don't store all the actor follows
|
||||
// So we just increment the counter
|
||||
if (actor.serverId) {
|
||||
if (!by) return
|
||||
|
||||
return ActorModel.increment(columnToUpdate, {
|
||||
by,
|
||||
where,
|
||||
transaction
|
||||
})
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
static getPublicKeyUrl (url: string) {
|
||||
return url + '#main-key'
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
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)) {
|
||||
let avatars = this.Avatars
|
||||
|
||||
// Use 120px avatar as first position if possible, so that remote servers use it in priority (instead of using 48x48px)
|
||||
const avatar120Px = avatars.find(a => a.width === 120)
|
||||
if (avatar120Px) avatars = [ avatar120Px, ...avatars.filter(a => a.width !== 120) ]
|
||||
|
||||
icon = 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,
|
||||
name,
|
||||
endpoints: {
|
||||
sharedInbox: this.sharedInboxUrl
|
||||
},
|
||||
publicKey: {
|
||||
id: ActorModel.getPublicKeyUrl(this.url),
|
||||
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'
|
||||
}
|
||||
|
||||
isLocal () {
|
||||
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) {
|
||||
if (this.serverId && !this.Server) throw new Error('Server is not loaded in the object')
|
||||
|
||||
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')
|
||||
}
|
||||
|
||||
getAppropriateQualityImage (type: ActorImageType_Type, width: number) {
|
||||
if (!this.hasImage(type)) return undefined
|
||||
|
||||
const images = type === ActorImageType.AVATAR
|
||||
? this.Avatars
|
||||
: this.Banners
|
||||
|
||||
return findAppropriateImage(images, width)
|
||||
}
|
||||
|
||||
isOutdated () {
|
||||
if (this.isLocal()) return false
|
||||
|
||||
return isOutdated(this, ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL)
|
||||
}
|
||||
|
||||
getCreatedAt (this: MActorAPChannel | MActorAPAccount | MActorFormattable) {
|
||||
return this.remoteCreatedAt || this.createdAt
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
import { ActivityPubActorType, FollowState } from '@peertube/peertube-models'
|
||||
import { AbstractListQueryOptions } from '@server/models/shared/abstract-list-query.js'
|
||||
import { Sequelize } from 'sequelize'
|
||||
import { InstanceListFollowsQueryBuilder } from './shared/instance-list-follows-query-builder.js'
|
||||
|
||||
export interface ListFollowersOptions extends AbstractListQueryOptions {
|
||||
actorIds: number[]
|
||||
state?: FollowState
|
||||
actorType?: ActivityPubActorType
|
||||
search?: string
|
||||
}
|
||||
|
||||
export class InstanceListFollowersQueryBuilder extends InstanceListFollowsQueryBuilder<ListFollowersOptions> {
|
||||
constructor (
|
||||
protected readonly sequelize: Sequelize,
|
||||
protected readonly options: ListFollowersOptions
|
||||
) {
|
||||
super(sequelize, options)
|
||||
}
|
||||
|
||||
protected buildSubQueryWhere () {
|
||||
this.buildActorFollowingJoin()
|
||||
|
||||
this.subQueryWhere = 'WHERE "ActorFollowing"."id" IN (:actorIds) '
|
||||
this.replacements.actorIds = this.options.actorIds
|
||||
|
||||
if (this.options.state) {
|
||||
this.subQueryWhere += 'AND "ActorFollowModel"."state" = :state '
|
||||
this.replacements.state = this.options.state
|
||||
}
|
||||
|
||||
if (this.options.search) {
|
||||
const escapedLikeSearch = this.sequelize.escape('%' + this.options.search + '%')
|
||||
|
||||
this.subQueryWhere += `AND (` +
|
||||
`"ActorFollower->Server"."host" ILIKE ${escapedLikeSearch} ` +
|
||||
`OR "ActorFollower"."preferredUsername" ILIKE ${escapedLikeSearch} ` +
|
||||
`)`
|
||||
}
|
||||
|
||||
if (this.options.actorType) {
|
||||
this.subQueryWhere += `AND "ActorFollower"."type" = :actorType `
|
||||
this.replacements.actorType = this.options.actorType
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
import { ActivityPubActorType, FollowState } from '@peertube/peertube-models'
|
||||
import { AbstractListQueryOptions } from '@server/models/shared/abstract-list-query.js'
|
||||
import { Sequelize } from 'sequelize'
|
||||
import { InstanceListFollowsQueryBuilder } from './shared/instance-list-follows-query-builder.js'
|
||||
|
||||
export interface ListFollowingOptions extends AbstractListQueryOptions {
|
||||
followerId: number
|
||||
state?: FollowState
|
||||
actorType?: ActivityPubActorType
|
||||
search?: string
|
||||
}
|
||||
|
||||
export class InstanceListFollowingQueryBuilder extends InstanceListFollowsQueryBuilder<ListFollowingOptions> {
|
||||
constructor (
|
||||
protected readonly sequelize: Sequelize,
|
||||
protected readonly options: ListFollowingOptions
|
||||
) {
|
||||
super(sequelize, options)
|
||||
}
|
||||
|
||||
protected buildSubQueryWhere () {
|
||||
this.buildActorFollowingJoin()
|
||||
|
||||
this.subQueryWhere = 'WHERE "ActorFollowModel"."actorId" = :followerId '
|
||||
this.replacements.followerId = this.options.followerId
|
||||
|
||||
if (this.options.state) {
|
||||
this.subQueryWhere += 'AND "ActorFollowModel"."state" = :state '
|
||||
this.replacements.state = this.options.state
|
||||
}
|
||||
|
||||
if (this.options.search) {
|
||||
const escapedLikeSearch = this.sequelize.escape('%' + this.options.search + '%')
|
||||
|
||||
this.subQueryWhere += `AND (` +
|
||||
`"ActorFollowing->Server"."host" ILIKE ${escapedLikeSearch} ` +
|
||||
`OR "ActorFollowing"."preferredUsername" ILIKE ${escapedLikeSearch} ` +
|
||||
`)`
|
||||
}
|
||||
|
||||
if (this.options.actorType) {
|
||||
this.subQueryWhere += `AND "ActorFollowing"."type" = :actorType `
|
||||
this.replacements.actorType = this.options.actorType
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
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.getSQLAPIAttributes(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(', ')
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
import { ActorImageType } from '@peertube/peertube-models'
|
||||
import { AbstractListQuery, AbstractListQueryOptions } from '@server/models/shared/abstract-list-query.js'
|
||||
import { Sequelize } from 'sequelize'
|
||||
import { getInstanceFollowsSort } from '../../../shared/index.js'
|
||||
import { ActorFollowTableAttributes } from './actor-follow-table-attributes.js'
|
||||
|
||||
export abstract class InstanceListFollowsQueryBuilder<T extends AbstractListQueryOptions> extends AbstractListQuery {
|
||||
protected readonly tableAttributes = new ActorFollowTableAttributes()
|
||||
|
||||
private builtActorFollowingJoin = false
|
||||
|
||||
constructor (
|
||||
protected readonly sequelize: Sequelize,
|
||||
protected readonly options: T
|
||||
) {
|
||||
super(sequelize, { modelName: 'ActorFollowModel', tableName: 'actorFollow' }, options)
|
||||
}
|
||||
|
||||
protected buildQueryJoin () {
|
||||
this.join += this.getAvatarsJoin('ActorFollower')
|
||||
this.join += this.getAvatarsJoin('ActorFollowing')
|
||||
}
|
||||
|
||||
protected buildQueryAttributes () {
|
||||
this.attributes = [
|
||||
...this.attributes,
|
||||
|
||||
this.tableAttributes.getAvatarAttributes('ActorFollower'),
|
||||
this.tableAttributes.getAvatarAttributes('ActorFollowing')
|
||||
]
|
||||
}
|
||||
|
||||
protected buildSubQueryJoin () {
|
||||
this.buildActorFollowingJoin()
|
||||
}
|
||||
|
||||
protected buildSubQueryAttributes () {
|
||||
this.subQueryAttributes = [
|
||||
...this.subQueryAttributes,
|
||||
|
||||
this.tableAttributes.getFollowAttributes(),
|
||||
this.tableAttributes.getActorAttributes('ActorFollower'),
|
||||
this.tableAttributes.getActorAttributes('ActorFollowing'),
|
||||
this.tableAttributes.getServerAttributes('ActorFollower'),
|
||||
this.tableAttributes.getServerAttributes('ActorFollowing')
|
||||
]
|
||||
}
|
||||
|
||||
protected getSort (sort: string) {
|
||||
return getInstanceFollowsSort(sort)
|
||||
}
|
||||
|
||||
protected buildActorFollowingJoin () {
|
||||
if (this.builtActorFollowingJoin) return
|
||||
|
||||
this.subQueryJoin += 'INNER JOIN "actor" "ActorFollower" ON "ActorFollower"."id" = "ActorFollowModel"."actorId" ' +
|
||||
'INNER JOIN "actor" "ActorFollowing" ON "ActorFollowing"."id" = "ActorFollowModel"."targetActorId" '
|
||||
|
||||
this.subQueryJoin += this.getServerJoin('ActorFollowing')
|
||||
this.subQueryJoin += this.getServerJoin('ActorFollower')
|
||||
|
||||
this.builtActorFollowingJoin = true
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
private getServerJoin (actorName: string) {
|
||||
return `LEFT JOIN "server" "${actorName}->Server" ON "${actorName}"."serverId" = "${actorName}->Server"."id" `
|
||||
}
|
||||
|
||||
private getAvatarsJoin (actorName: string) {
|
||||
return `LEFT JOIN "actorImage" "${actorName}->Avatars" ON "${actorName}.id" = "${actorName}->Avatars"."actorId" ` +
|
||||
`AND "${actorName}->Avatars"."type" = ${ActorImageType.AVATAR} `
|
||||
}
|
||||
}
|
||||
116
server/core/models/application/application.ts
Normal file
116
server/core/models/application/application.ts
Normal file
@@ -0,0 +1,116 @@
|
||||
import { getNodeABIVersion } from '@server/helpers/version.js'
|
||||
import { CONFIG } from '@server/initializers/config.js'
|
||||
import memoizee from 'memoizee'
|
||||
import { AllowNull, Column, DataType, Default, DefaultScope, HasOne, IsInt, Table } from 'sequelize-typescript'
|
||||
import type { PickDeep } from 'type-fest'
|
||||
import { AccountModel } from '../account/account.js'
|
||||
import { ActorImageModel } from '../actor/actor-image.js'
|
||||
import { SequelizeModel } from '../shared/index.js'
|
||||
import { UploadImageModel } from './upload-image.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
|
||||
|
||||
const uploadImages = await UploadImageModel.listByActor(actor, undefined)
|
||||
actor.UploadImages = uploadImages
|
||||
|
||||
return actor
|
||||
}, { promise: true })
|
||||
|
||||
type ConfigPart = PickDeep<typeof CONFIG, 'OBJECT_STORAGE.STREAMING_PLAYLISTS'>
|
||||
|
||||
@DefaultScope(() => ({
|
||||
include: [
|
||||
{
|
||||
model: AccountModel,
|
||||
required: true
|
||||
}
|
||||
]
|
||||
}))
|
||||
@Table({
|
||||
tableName: 'application',
|
||||
timestamps: false
|
||||
})
|
||||
export class ApplicationModel extends SequelizeModel<ApplicationModel> {
|
||||
@AllowNull(false)
|
||||
@Default(0)
|
||||
@IsInt
|
||||
@Column
|
||||
declare migrationVersion: number
|
||||
|
||||
@AllowNull(true)
|
||||
@Column
|
||||
declare latestPeerTubeVersion: string
|
||||
|
||||
@AllowNull(false)
|
||||
@Column
|
||||
declare nodeVersion: string
|
||||
|
||||
@AllowNull(false)
|
||||
@Column
|
||||
declare nodeABIVersion: string
|
||||
|
||||
@AllowNull(true)
|
||||
@Column(DataType.JSONB)
|
||||
declare configPart: ConfigPart
|
||||
|
||||
@HasOne(() => AccountModel, {
|
||||
foreignKey: {
|
||||
allowNull: true
|
||||
},
|
||||
onDelete: 'cascade'
|
||||
})
|
||||
declare Account: Awaited<AccountModel>
|
||||
|
||||
private static lastRunConfigPart: ConfigPart
|
||||
private static lastRunNodeABIVersion: string
|
||||
|
||||
static countTotal () {
|
||||
return ApplicationModel.count()
|
||||
}
|
||||
|
||||
static load () {
|
||||
return ApplicationModel.findOne()
|
||||
}
|
||||
|
||||
static async nodeABIChanged () {
|
||||
const application = await this.load()
|
||||
|
||||
const nodeABIVersion = this.lastRunNodeABIVersion || application.nodeABIVersion
|
||||
|
||||
return nodeABIVersion !== getNodeABIVersion()
|
||||
}
|
||||
|
||||
static async streamingPlaylistBaseUrlChanged () {
|
||||
const application = await this.load()
|
||||
const configPart = this.lastRunConfigPart || application.configPart
|
||||
|
||||
return configPart?.OBJECT_STORAGE.STREAMING_PLAYLISTS.BASE_URL !== CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS.BASE_URL
|
||||
}
|
||||
|
||||
static async updateNodeVersionsOrConfig () {
|
||||
const application = await this.load()
|
||||
|
||||
this.lastRunNodeABIVersion = application.nodeABIVersion
|
||||
this.lastRunConfigPart = application.configPart
|
||||
|
||||
application.nodeABIVersion = getNodeABIVersion()
|
||||
application.nodeVersion = process.version
|
||||
|
||||
application.configPart = {
|
||||
OBJECT_STORAGE: {
|
||||
STREAMING_PLAYLISTS: CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS
|
||||
}
|
||||
}
|
||||
|
||||
await application.save()
|
||||
}
|
||||
}
|
||||
128
server/core/models/application/upload-image.ts
Normal file
128
server/core/models/application/upload-image.ts
Normal file
@@ -0,0 +1,128 @@
|
||||
import { type UploadImageType_Type } from '@peertube/peertube-models'
|
||||
import { MActorId, MUploadImage } from '@server/types/models/index.js'
|
||||
import { remove } from 'fs-extra/esm'
|
||||
import { join } from 'path'
|
||||
import { Transaction } from 'sequelize'
|
||||
import { AfterDestroy, AllowNull, BelongsTo, Column, CreatedAt, Default, ForeignKey, Table, UpdatedAt } from 'sequelize-typescript'
|
||||
import { logger } from '../../helpers/logger.js'
|
||||
import { DIRECTORIES, STATIC_PATHS, WEBSERVER } from '../../initializers/constants.js'
|
||||
import { ActorModel } from '../actor/actor.js'
|
||||
import { SequelizeModel } from '../shared/index.js'
|
||||
|
||||
// Image uploads that are not suitable for other tables actor images (avatars/banners)
|
||||
// Can be used to store instance images like logos, favicons, etc.
|
||||
|
||||
@Table({
|
||||
tableName: 'uploadImage',
|
||||
indexes: [
|
||||
{
|
||||
fields: [ 'filename' ],
|
||||
unique: true
|
||||
},
|
||||
{
|
||||
fields: [ 'actorId', 'type', 'width' ],
|
||||
unique: true
|
||||
}
|
||||
]
|
||||
})
|
||||
export class UploadImageModel extends SequelizeModel<UploadImageModel> {
|
||||
@AllowNull(false)
|
||||
@Column
|
||||
declare filename: string
|
||||
|
||||
@AllowNull(true)
|
||||
@Default(null)
|
||||
@Column
|
||||
declare height: number
|
||||
|
||||
@AllowNull(true)
|
||||
@Default(null)
|
||||
@Column
|
||||
declare width: number
|
||||
|
||||
@AllowNull(true)
|
||||
@Column
|
||||
declare fileUrl: string
|
||||
|
||||
@AllowNull(false)
|
||||
@Column
|
||||
declare type: UploadImageType_Type
|
||||
|
||||
@CreatedAt
|
||||
declare createdAt: Date
|
||||
|
||||
@UpdatedAt
|
||||
declare updatedAt: Date
|
||||
|
||||
@ForeignKey(() => ActorModel)
|
||||
@Column
|
||||
declare actorId: number
|
||||
|
||||
@BelongsTo(() => ActorModel, {
|
||||
foreignKey: {
|
||||
allowNull: false
|
||||
},
|
||||
onDelete: 'CASCADE'
|
||||
})
|
||||
declare Actor: Awaited<ActorModel>
|
||||
|
||||
@AfterDestroy
|
||||
static removeFile (instance: UploadImageModel) {
|
||||
logger.info('Removing upload image file %s.', instance.filename)
|
||||
|
||||
// Don't block the transaction
|
||||
instance.removeImage()
|
||||
.catch(err => logger.error('Cannot remove upload image file %s.', instance.filename, { err }))
|
||||
}
|
||||
|
||||
static listByActor (actor: MActorId, transaction: Transaction) {
|
||||
const query = {
|
||||
where: {
|
||||
actorId: actor.id
|
||||
},
|
||||
transaction
|
||||
}
|
||||
|
||||
return UploadImageModel.findAll(query)
|
||||
}
|
||||
|
||||
static listByActorAndType (actor: MActorId, type: UploadImageType_Type, transaction: Transaction) {
|
||||
const query = {
|
||||
where: {
|
||||
actorId: actor.id,
|
||||
type
|
||||
},
|
||||
transaction
|
||||
}
|
||||
|
||||
return UploadImageModel.findAll(query)
|
||||
}
|
||||
|
||||
static getImageUrl (image: MUploadImage) {
|
||||
if (!image) return undefined
|
||||
|
||||
return WEBSERVER.URL + image.getStaticPath()
|
||||
}
|
||||
|
||||
static getPathOf (filename: string) {
|
||||
return join(DIRECTORIES.UPLOAD_IMAGES, filename)
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
getStaticPath (this: MUploadImage) {
|
||||
return join(STATIC_PATHS.UPLOAD_IMAGES, this.filename)
|
||||
}
|
||||
|
||||
getPath () {
|
||||
return UploadImageModel.getPathOf(this.filename)
|
||||
}
|
||||
|
||||
removeImage () {
|
||||
return remove(this.getPath())
|
||||
}
|
||||
|
||||
isLocal () {
|
||||
return !this.fileUrl
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
declare createdAt: Date
|
||||
|
||||
@UpdatedAt
|
||||
declare updatedAt: Date
|
||||
|
||||
@AllowNull(true)
|
||||
@Column(DataType.INTEGER)
|
||||
declare policy: AutomaticTagPolicyType
|
||||
|
||||
@ForeignKey(() => AccountModel)
|
||||
@Column
|
||||
declare accountId: number
|
||||
|
||||
@BelongsTo(() => AccountModel, {
|
||||
foreignKey: {
|
||||
allowNull: false
|
||||
},
|
||||
onDelete: 'cascade'
|
||||
})
|
||||
declare Account: Awaited<AccountModel>
|
||||
|
||||
@ForeignKey(() => AutomaticTagModel)
|
||||
@Column
|
||||
declare automaticTagId: number
|
||||
|
||||
@BelongsTo(() => AutomaticTagModel, {
|
||||
foreignKey: {
|
||||
allowNull: false
|
||||
},
|
||||
onDelete: 'cascade'
|
||||
})
|
||||
declare 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 })
|
||||
}
|
||||
}
|
||||
68
server/core/models/automatic-tag/automatic-tag.ts
Normal file
68
server/core/models/automatic-tag/automatic-tag.ts
Normal file
@@ -0,0 +1,68 @@
|
||||
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
|
||||
declare name: string
|
||||
|
||||
@HasMany(() => CommentAutomaticTagModel, {
|
||||
foreignKey: 'automaticTagId',
|
||||
onDelete: 'CASCADE'
|
||||
})
|
||||
declare CommentAutomaticTags: Awaited<CommentAutomaticTagModel>[]
|
||||
|
||||
@HasMany(() => VideoAutomaticTagModel, {
|
||||
foreignKey: 'automaticTagId',
|
||||
onDelete: 'CASCADE'
|
||||
})
|
||||
declare VideoAutomaticTags: Awaited<VideoAutomaticTagModel>[]
|
||||
|
||||
@HasMany(() => AccountAutomaticTagPolicyModel, {
|
||||
foreignKey: {
|
||||
name: 'automaticTagId',
|
||||
allowNull: false
|
||||
},
|
||||
onDelete: 'cascade'
|
||||
})
|
||||
declare 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)
|
||||
}
|
||||
}
|
||||
74
server/core/models/automatic-tag/comment-automatic-tag.ts
Normal file
74
server/core/models/automatic-tag/comment-automatic-tag.ts
Normal file
@@ -0,0 +1,74 @@
|
||||
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
|
||||
declare createdAt: Date
|
||||
|
||||
@UpdatedAt
|
||||
declare updatedAt: Date
|
||||
|
||||
@ForeignKey(() => VideoCommentModel)
|
||||
@PrimaryKey
|
||||
@Column
|
||||
declare commentId: number
|
||||
|
||||
@ForeignKey(() => AutomaticTagModel)
|
||||
@PrimaryKey
|
||||
@Column
|
||||
declare automaticTagId: number
|
||||
|
||||
@ForeignKey(() => AccountModel)
|
||||
@PrimaryKey
|
||||
@Column
|
||||
declare accountId: number
|
||||
|
||||
@BelongsTo(() => AccountModel, {
|
||||
foreignKey: {
|
||||
allowNull: false
|
||||
},
|
||||
onDelete: 'CASCADE'
|
||||
})
|
||||
declare Account: Awaited<AccountModel>
|
||||
|
||||
@BelongsTo(() => AutomaticTagModel, {
|
||||
foreignKey: {
|
||||
allowNull: false
|
||||
},
|
||||
onDelete: 'CASCADE'
|
||||
})
|
||||
declare AutomaticTag: Awaited<AutomaticTagModel>
|
||||
|
||||
@BelongsTo(() => VideoCommentModel, {
|
||||
foreignKey: {
|
||||
allowNull: false
|
||||
},
|
||||
onDelete: 'CASCADE'
|
||||
})
|
||||
declare VideoComment: Awaited<VideoCommentModel>
|
||||
|
||||
static deleteAllOfAccountAndComment (options: {
|
||||
accountId: number
|
||||
commentId: number
|
||||
transaction: Transaction
|
||||
}) {
|
||||
const { accountId, commentId, transaction } = options
|
||||
|
||||
return this.destroy({
|
||||
where: { accountId, commentId },
|
||||
transaction
|
||||
})
|
||||
}
|
||||
}
|
||||
74
server/core/models/automatic-tag/video-automatic-tag.ts
Normal file
74
server/core/models/automatic-tag/video-automatic-tag.ts
Normal file
@@ -0,0 +1,74 @@
|
||||
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
|
||||
declare createdAt: Date
|
||||
|
||||
@UpdatedAt
|
||||
declare updatedAt: Date
|
||||
|
||||
@ForeignKey(() => VideoModel)
|
||||
@PrimaryKey
|
||||
@Column
|
||||
declare videoId: number
|
||||
|
||||
@ForeignKey(() => AutomaticTagModel)
|
||||
@PrimaryKey
|
||||
@Column
|
||||
declare automaticTagId: number
|
||||
|
||||
@ForeignKey(() => AccountModel)
|
||||
@PrimaryKey
|
||||
@Column
|
||||
declare accountId: number
|
||||
|
||||
@BelongsTo(() => AccountModel, {
|
||||
foreignKey: {
|
||||
allowNull: false
|
||||
},
|
||||
onDelete: 'CASCADE'
|
||||
})
|
||||
declare Account: Awaited<AccountModel>
|
||||
|
||||
@BelongsTo(() => AutomaticTagModel, {
|
||||
foreignKey: {
|
||||
allowNull: false
|
||||
},
|
||||
onDelete: 'CASCADE'
|
||||
})
|
||||
declare AutomaticTag: Awaited<AutomaticTagModel>
|
||||
|
||||
@BelongsTo(() => VideoModel, {
|
||||
foreignKey: {
|
||||
allowNull: false
|
||||
},
|
||||
onDelete: 'CASCADE'
|
||||
})
|
||||
declare Video: Awaited<VideoModel>
|
||||
|
||||
static deleteAllOfAccountAndVideo (options: {
|
||||
accountId: number
|
||||
videoId: number
|
||||
transaction: Transaction
|
||||
}) {
|
||||
const { accountId, videoId, transaction } = options
|
||||
|
||||
return this.destroy({
|
||||
where: { accountId, videoId },
|
||||
transaction
|
||||
})
|
||||
}
|
||||
}
|
||||
62
server/core/models/oauth/oauth-client.ts
Normal file
62
server/core/models/oauth/oauth-client.ts
Normal file
@@ -0,0 +1,62 @@
|
||||
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
|
||||
declare clientId: string
|
||||
|
||||
@AllowNull(false)
|
||||
@Column
|
||||
declare clientSecret: string
|
||||
|
||||
@Column(DataType.ARRAY(DataType.STRING))
|
||||
declare grants: string[]
|
||||
|
||||
@Column(DataType.ARRAY(DataType.STRING))
|
||||
declare redirectUris: string[]
|
||||
|
||||
@CreatedAt
|
||||
declare createdAt: Date
|
||||
|
||||
@UpdatedAt
|
||||
declare updatedAt: Date
|
||||
|
||||
@HasMany(() => OAuthTokenModel, {
|
||||
onDelete: 'cascade'
|
||||
})
|
||||
declare 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)
|
||||
}
|
||||
}
|
||||
312
server/core/models/oauth/oauth-token.ts
Normal file
312
server/core/models/oauth/oauth-token.ts
Normal file
@@ -0,0 +1,312 @@
|
||||
import { TokenSession } from '@peertube/peertube-models'
|
||||
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 { Op, Transaction } from 'sequelize'
|
||||
import {
|
||||
AfterDestroy,
|
||||
AfterUpdate,
|
||||
AllowNull,
|
||||
BelongsTo,
|
||||
Column,
|
||||
CreatedAt,
|
||||
ForeignKey,
|
||||
Scopes,
|
||||
Table,
|
||||
UpdatedAt
|
||||
} from 'sequelize-typescript'
|
||||
import { logger } from '../../helpers/logger.js'
|
||||
import { AccountModel } from '../account/account.js'
|
||||
import { ActorModel } from '../actor/actor.js'
|
||||
import { getSort, SequelizeModel } from '../shared/index.js'
|
||||
import { UserModel } from '../user/user.js'
|
||||
import { OAuthClientModel } from './oauth-client.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
|
||||
declare accessToken: string
|
||||
|
||||
@AllowNull(false)
|
||||
@Column
|
||||
declare accessTokenExpiresAt: Date
|
||||
|
||||
@AllowNull(false)
|
||||
@Column
|
||||
declare refreshToken: string
|
||||
|
||||
@AllowNull(false)
|
||||
@Column
|
||||
declare refreshTokenExpiresAt: Date
|
||||
|
||||
@Column
|
||||
declare authName: string
|
||||
|
||||
@Column
|
||||
declare loginDevice: string
|
||||
|
||||
@Column
|
||||
declare loginIP: string
|
||||
|
||||
@Column
|
||||
declare loginDate: Date
|
||||
|
||||
@Column
|
||||
declare lastActivityDevice: string
|
||||
|
||||
@Column
|
||||
declare lastActivityIP: string
|
||||
|
||||
@Column
|
||||
declare lastActivityDate: Date
|
||||
|
||||
@CreatedAt
|
||||
declare createdAt: Date
|
||||
|
||||
@UpdatedAt
|
||||
declare updatedAt: Date
|
||||
|
||||
@ForeignKey(() => UserModel)
|
||||
@Column
|
||||
declare userId: number
|
||||
|
||||
@BelongsTo(() => UserModel, {
|
||||
foreignKey: {
|
||||
allowNull: false
|
||||
},
|
||||
onDelete: 'cascade'
|
||||
})
|
||||
declare User: Awaited<UserModel>
|
||||
|
||||
@ForeignKey(() => OAuthClientModel)
|
||||
@Column
|
||||
declare oAuthClientId: number
|
||||
|
||||
@BelongsTo(() => OAuthClientModel, {
|
||||
foreignKey: {
|
||||
allowNull: false
|
||||
},
|
||||
onDelete: 'cascade'
|
||||
})
|
||||
declare 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 loadSessionOf (options: {
|
||||
id: number
|
||||
userId: number
|
||||
}) {
|
||||
const now = new Date()
|
||||
|
||||
return OAuthTokenModel.findOne({
|
||||
where: {
|
||||
id: options.id,
|
||||
userId: options.userId,
|
||||
accessTokenExpiresAt: {
|
||||
[Op.gt]: now
|
||||
},
|
||||
refreshTokenExpiresAt: {
|
||||
[Op.gt]: now
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
static async listSessionsOf (options: {
|
||||
start: number
|
||||
count: number
|
||||
sort: string
|
||||
userId: number
|
||||
}) {
|
||||
const now = new Date()
|
||||
|
||||
const { count, rows } = await OAuthTokenModel.findAndCountAll({
|
||||
offset: options.start,
|
||||
limit: options.count,
|
||||
order: getSort(options.sort),
|
||||
where: {
|
||||
userId: options.userId,
|
||||
accessTokenExpiresAt: {
|
||||
[Op.gt]: now
|
||||
},
|
||||
refreshTokenExpiresAt: {
|
||||
[Op.gt]: now
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
return {
|
||||
total: count,
|
||||
data: rows
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
static deleteUserToken (userId: number, t?: Transaction) {
|
||||
TokensCache.Instance.deleteUserToken(userId)
|
||||
|
||||
const query = {
|
||||
where: {
|
||||
userId
|
||||
},
|
||||
transaction: t
|
||||
}
|
||||
|
||||
return OAuthTokenModel.destroy(query)
|
||||
}
|
||||
|
||||
toSessionFormattedJSON (activeToken: string): TokenSession {
|
||||
return {
|
||||
id: this.id,
|
||||
|
||||
loginIP: this.loginIP,
|
||||
loginDevice: this.loginDevice,
|
||||
loginDate: this.loginDate,
|
||||
|
||||
lastActivityIP: this.lastActivityIP,
|
||||
lastActivityDevice: this.lastActivityDevice,
|
||||
lastActivityDate: this.lastActivityDate,
|
||||
|
||||
currentSession: this.accessToken === activeToken,
|
||||
|
||||
createdAt: this.createdAt
|
||||
}
|
||||
}
|
||||
}
|
||||
614
server/core/models/redundancy/video-redundancy.ts
Normal file
614
server/core/models/redundancy/video-redundancy.ts
Normal file
@@ -0,0 +1,614 @@
|
||||
import {
|
||||
CacheFileObject,
|
||||
RedundancyInformation,
|
||||
VideoPrivacy,
|
||||
VideoRedundanciesTarget,
|
||||
VideoRedundancy,
|
||||
VideoRedundancyStrategy,
|
||||
VideoRedundancyStrategyWithManual
|
||||
} from '@peertube/peertube-models'
|
||||
import { isTestInstance } from '@peertube/peertube-node-utils'
|
||||
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: VideoStreamingPlaylistModel,
|
||||
required: false,
|
||||
include: [
|
||||
{
|
||||
model: VideoModel,
|
||||
required: true
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
}))
|
||||
@Table({
|
||||
tableName: 'videoRedundancy',
|
||||
indexes: [
|
||||
{
|
||||
fields: [ 'videoStreamingPlaylistId' ]
|
||||
},
|
||||
{
|
||||
fields: [ 'actorId' ]
|
||||
},
|
||||
{
|
||||
fields: [ 'expiresOn' ]
|
||||
},
|
||||
{
|
||||
fields: [ 'url' ],
|
||||
unique: true
|
||||
}
|
||||
]
|
||||
})
|
||||
export class VideoRedundancyModel extends SequelizeModel<VideoRedundancyModel> {
|
||||
@CreatedAt
|
||||
declare createdAt: Date
|
||||
|
||||
@UpdatedAt
|
||||
declare updatedAt: Date
|
||||
|
||||
@AllowNull(true)
|
||||
@Column
|
||||
declare expiresOn: Date
|
||||
|
||||
@AllowNull(false)
|
||||
@Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS_REDUNDANCY.URL.max))
|
||||
declare fileUrl: string
|
||||
|
||||
@AllowNull(false)
|
||||
@Is('VideoRedundancyUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
|
||||
@Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS_REDUNDANCY.URL.max))
|
||||
declare url: string
|
||||
|
||||
@AllowNull(true)
|
||||
@Column
|
||||
declare strategy: string // Only used by us
|
||||
|
||||
@ForeignKey(() => VideoStreamingPlaylistModel)
|
||||
@Column
|
||||
declare videoStreamingPlaylistId: number
|
||||
|
||||
@BelongsTo(() => VideoStreamingPlaylistModel, {
|
||||
foreignKey: {
|
||||
allowNull: false
|
||||
},
|
||||
onDelete: 'cascade'
|
||||
})
|
||||
declare VideoStreamingPlaylist: Awaited<VideoStreamingPlaylistModel>
|
||||
|
||||
@ForeignKey(() => ActorModel)
|
||||
@Column
|
||||
declare actorId: number
|
||||
|
||||
@BelongsTo(() => ActorModel, {
|
||||
foreignKey: {
|
||||
allowNull: false
|
||||
},
|
||||
onDelete: 'cascade'
|
||||
})
|
||||
declare Actor: Awaited<ActorModel>
|
||||
|
||||
@BeforeDestroy
|
||||
static async removeFile (instance: VideoRedundancyModel) {
|
||||
if (!instance.isLocal()) return
|
||||
|
||||
const videoStreamingPlaylist = await VideoStreamingPlaylistModel.loadWithVideo(instance.videoStreamingPlaylistId)
|
||||
|
||||
const videoUUID = videoStreamingPlaylist.Video.uuid
|
||||
logger.info('Removing duplicated video streaming playlist %s.', videoUUID)
|
||||
|
||||
videoStreamingPlaylist.Video.removeAllStreamingPlaylistFiles({ playlist: videoStreamingPlaylist, isRedundancy: true })
|
||||
.catch(err => logger.error('Cannot delete video streaming playlist files of %s.', videoUUID, { err }))
|
||||
|
||||
return undefined
|
||||
}
|
||||
|
||||
static async listLocalByStreamingPlaylistId (videoStreamingPlaylistId: number): Promise<MVideoRedundancyVideo[]> {
|
||||
const actor = await getServerActor()
|
||||
|
||||
const query = {
|
||||
where: {
|
||||
actorId: actor.id,
|
||||
videoStreamingPlaylistId
|
||||
}
|
||||
}
|
||||
|
||||
return VideoRedundancyModel.scope(ScopeNames.WITH_VIDEO).findAll(query)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Select redundancy candidates
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
static async findMostViewToDuplicate (randomizedFactor: number) {
|
||||
const peertubeActor = await getServerActor()
|
||||
|
||||
// On VideoModel!
|
||||
const query = {
|
||||
attributes: [ 'id', 'views' ],
|
||||
limit: randomizedFactor,
|
||||
order: getVideoSort('-views'),
|
||||
where: {
|
||||
...this.buildVideoCandidateWhere(),
|
||||
...this.buildVideoIdsForDuplication(peertubeActor)
|
||||
},
|
||||
include: [
|
||||
VideoRedundancyModel.buildRedundancyAllowedInclude(),
|
||||
VideoRedundancyModel.buildStreamingPlaylistRequiredInclude()
|
||||
]
|
||||
}
|
||||
|
||||
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: {
|
||||
...this.buildVideoCandidateWhere(),
|
||||
...this.buildVideoIdsForDuplication(peertubeActor)
|
||||
},
|
||||
include: [
|
||||
VideoRedundancyModel.buildRedundancyAllowedInclude(),
|
||||
VideoRedundancyModel.buildStreamingPlaylistRequiredInclude(),
|
||||
|
||||
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: {
|
||||
...this.buildVideoCandidateWhere(),
|
||||
...this.buildVideoIdsForDuplication(peertubeActor),
|
||||
|
||||
views: {
|
||||
[Op.gte]: minViews
|
||||
}
|
||||
},
|
||||
include: [
|
||||
VideoRedundancyModel.buildRedundancyAllowedInclude(),
|
||||
VideoRedundancyModel.buildStreamingPlaylistRequiredInclude(),
|
||||
|
||||
// Required by publishedAt sort
|
||||
{
|
||||
model: ScheduleVideoUpdateModel.unscoped(),
|
||||
required: false
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
return VideoRedundancyModel.getVideoSample(VideoModel.unscoped().findAll(query))
|
||||
}
|
||||
|
||||
static async isLocalByVideoUUIDExists (uuid: string) {
|
||||
const actor = await getServerActor()
|
||||
|
||||
const query = {
|
||||
raw: true,
|
||||
attributes: [ 'id' ],
|
||||
where: {
|
||||
actorId: actor.id
|
||||
},
|
||||
include: [
|
||||
{
|
||||
model: VideoStreamingPlaylistModel.unscoped(),
|
||||
required: true,
|
||||
include: [
|
||||
{
|
||||
attributes: [],
|
||||
model: VideoModel.unscoped(),
|
||||
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())
|
||||
}
|
||||
|
||||
private static buildVideoCandidateWhere () {
|
||||
return {
|
||||
privacy: VideoPrivacy.PUBLIC,
|
||||
remote: true,
|
||||
isLive: false
|
||||
}
|
||||
}
|
||||
|
||||
private static buildRedundancyAllowedInclude () {
|
||||
return {
|
||||
attributes: [],
|
||||
model: VideoChannelModel.unscoped(),
|
||||
required: true,
|
||||
include: [
|
||||
{
|
||||
attributes: [],
|
||||
model: ActorModel.unscoped(),
|
||||
required: true,
|
||||
include: [
|
||||
{
|
||||
attributes: [],
|
||||
model: ServerModel.unscoped(),
|
||||
required: true,
|
||||
where: {
|
||||
redundancyAllowed: true
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
private static buildStreamingPlaylistRequiredInclude () {
|
||||
return {
|
||||
attributes: [],
|
||||
required: true,
|
||||
model: VideoStreamingPlaylistModel.unscoped()
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
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 query = {
|
||||
where: {
|
||||
actorId: actor.id
|
||||
},
|
||||
include: [
|
||||
{
|
||||
model: VideoStreamingPlaylistModel.unscoped(),
|
||||
required: true,
|
||||
include: [
|
||||
{
|
||||
model: VideoModel,
|
||||
required: true,
|
||||
include: [
|
||||
{
|
||||
attributes: [],
|
||||
model: VideoChannelModel.unscoped(),
|
||||
required: true,
|
||||
include: [
|
||||
{
|
||||
attributes: [],
|
||||
model: ActorModel.unscoped(),
|
||||
required: true,
|
||||
where: {
|
||||
serverId
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
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 = {}
|
||||
|
||||
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 } })
|
||||
}
|
||||
|
||||
if (strategy) {
|
||||
Object.assign(redundancyWhere, { strategy })
|
||||
}
|
||||
|
||||
// /!\ On video model /!\
|
||||
const findOptions = {
|
||||
offset: start,
|
||||
limit: count,
|
||||
order: getSort(sort),
|
||||
where: videosWhere,
|
||||
include: [
|
||||
{
|
||||
required: true,
|
||||
model: VideoStreamingPlaylistModel.unscoped(),
|
||||
include: [
|
||||
{
|
||||
model: VideoRedundancyModel.unscoped(),
|
||||
required: true,
|
||||
where: redundancyWhere
|
||||
},
|
||||
{
|
||||
model: VideoFileModel,
|
||||
required: true
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
return Promise.all([
|
||||
VideoModel.findAll(findOptions),
|
||||
|
||||
VideoModel.count({
|
||||
where: {
|
||||
...videosWhere,
|
||||
|
||||
id: {
|
||||
[Op.in]: literal(
|
||||
'(' +
|
||||
'SELECT "videoId" FROM "videoStreamingPlaylist" ' +
|
||||
'INNER JOIN "videoRedundancy" ON "videoRedundancy"."videoStreamingPlaylistId" = "videoStreamingPlaylist".id' +
|
||||
')'
|
||||
)
|
||||
}
|
||||
}
|
||||
})
|
||||
]).then(([ data, total ]) => ({ total, data }))
|
||||
}
|
||||
|
||||
static async getStats (strategy: VideoRedundancyStrategyWithManual) {
|
||||
const actor = await getServerActor()
|
||||
|
||||
const sql = `WITH "tmp" AS ` +
|
||||
`(` +
|
||||
`SELECT "videoStreamingFile"."size" AS "videoStreamingFileSize", "videoStreamingPlaylist"."videoId" AS "videoStreamingVideoId"` +
|
||||
`FROM "videoRedundancy" AS "videoRedundancy" ` +
|
||||
`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` +
|
||||
`) ` +
|
||||
`SELECT ` +
|
||||
`COALESCE(SUM("videoStreamingFileSize"), '0') AS "totalUsed", ` +
|
||||
`COUNT(DISTINCT "videoStreamingVideoId") 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 streamingPlaylistsRedundancies: RedundancyInformation[] = []
|
||||
|
||||
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: {
|
||||
streamingPlaylists: streamingPlaylistsRedundancies
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
getVideo () {
|
||||
return this.VideoStreamingPlaylist.Video
|
||||
}
|
||||
|
||||
getVideoUUID () {
|
||||
return this.getVideo()?.uuid
|
||||
}
|
||||
|
||||
isLocal () {
|
||||
return !!this.strategy
|
||||
}
|
||||
|
||||
toActivityPubObject (this: MVideoRedundancyAP): CacheFileObject {
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Don't include video files we already duplicated
|
||||
private static buildVideoIdsForDuplication (peertubeActor: MActor) {
|
||||
const notIn = literal(
|
||||
'(' +
|
||||
`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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
378
server/core/models/runner/runner-job.ts
Normal file
378
server/core/models/runner/runner-job.ts
Normal file
@@ -0,0 +1,378 @@
|
||||
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)
|
||||
declare uuid: string
|
||||
|
||||
@AllowNull(false)
|
||||
@Column
|
||||
declare type: RunnerJobType
|
||||
|
||||
@AllowNull(false)
|
||||
@Column(DataType.JSONB)
|
||||
declare payload: RunnerJobPayload
|
||||
|
||||
@AllowNull(false)
|
||||
@Column(DataType.JSONB)
|
||||
declare privatePayload: RunnerJobPrivatePayload
|
||||
|
||||
@AllowNull(false)
|
||||
@Column
|
||||
declare state: RunnerJobStateType
|
||||
|
||||
@AllowNull(false)
|
||||
@Default(0)
|
||||
@Column
|
||||
declare failures: number
|
||||
|
||||
@AllowNull(true)
|
||||
@Column(DataType.STRING(CONSTRAINTS_FIELDS.RUNNER_JOBS.ERROR_MESSAGE.max))
|
||||
declare error: string
|
||||
|
||||
// Less has priority
|
||||
@AllowNull(false)
|
||||
@Column
|
||||
declare priority: number
|
||||
|
||||
// Used to fetch the appropriate job when the runner wants to post the result
|
||||
@AllowNull(true)
|
||||
@Column
|
||||
declare processingJobToken: string
|
||||
|
||||
@AllowNull(true)
|
||||
@Column
|
||||
declare progress: number
|
||||
|
||||
@AllowNull(true)
|
||||
@Column
|
||||
declare startedAt: Date
|
||||
|
||||
@AllowNull(true)
|
||||
@Column
|
||||
declare finishedAt: Date
|
||||
|
||||
@CreatedAt
|
||||
declare createdAt: Date
|
||||
|
||||
@UpdatedAt
|
||||
declare updatedAt: Date
|
||||
|
||||
@ForeignKey(() => RunnerJobModel)
|
||||
@Column
|
||||
declare dependsOnRunnerJobId: number
|
||||
|
||||
@BelongsTo(() => RunnerJobModel, {
|
||||
foreignKey: {
|
||||
name: 'dependsOnRunnerJobId',
|
||||
allowNull: true
|
||||
},
|
||||
onDelete: 'cascade'
|
||||
})
|
||||
declare DependsOnRunnerJob: Awaited<RunnerJobModel>
|
||||
|
||||
@ForeignKey(() => RunnerModel)
|
||||
@Column
|
||||
declare runnerId: number
|
||||
|
||||
@BelongsTo(() => RunnerModel, {
|
||||
foreignKey: {
|
||||
name: 'runnerId',
|
||||
allowNull: true
|
||||
},
|
||||
onDelete: 'SET NULL'
|
||||
})
|
||||
declare 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 (jobTypes?: string[]) {
|
||||
const jobTypesWhere = jobTypes
|
||||
? {
|
||||
type: {
|
||||
[Op.in]: jobTypes
|
||||
}
|
||||
}
|
||||
: {}
|
||||
|
||||
return RunnerJobModel.findAll<MRunnerJob>({
|
||||
limit: 10,
|
||||
order: getSort('priority'),
|
||||
where: {
|
||||
state: RunnerJobState.PENDING,
|
||||
|
||||
...jobTypesWhere
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
99
server/core/models/runner/runner-registration-token.ts
Normal file
99
server/core/models/runner/runner-registration-token.ts
Normal file
@@ -0,0 +1,99 @@
|
||||
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
|
||||
declare registrationToken: string
|
||||
|
||||
@CreatedAt
|
||||
declare createdAt: Date
|
||||
|
||||
@UpdatedAt
|
||||
declare updatedAt: Date
|
||||
|
||||
@HasMany(() => RunnerModel, {
|
||||
foreignKey: {
|
||||
allowNull: true
|
||||
},
|
||||
onDelete: 'cascade'
|
||||
})
|
||||
declare 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
|
||||
}
|
||||
}
|
||||
}
|
||||
127
server/core/models/runner/runner.ts
Normal file
127
server/core/models/runner/runner.ts
Normal file
@@ -0,0 +1,127 @@
|
||||
import { Runner } from '@peertube/peertube-models'
|
||||
import { CONSTRAINTS_FIELDS } from '@server/initializers/constants.js'
|
||||
import { MRunner } from '@server/types/models/runners/index.js'
|
||||
import { FindOptions } from 'sequelize'
|
||||
import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Table, UpdatedAt } from 'sequelize-typescript'
|
||||
import { SequelizeModel, getSort } from '../shared/index.js'
|
||||
import { RunnerRegistrationTokenModel } from './runner-registration-token.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
|
||||
declare runnerToken: string
|
||||
|
||||
@AllowNull(false)
|
||||
@Column
|
||||
declare name: string
|
||||
|
||||
@AllowNull(true)
|
||||
@Column(DataType.STRING(CONSTRAINTS_FIELDS.RUNNERS.DESCRIPTION.max))
|
||||
declare description: string
|
||||
|
||||
@AllowNull(false)
|
||||
@Column
|
||||
declare lastContact: Date
|
||||
|
||||
@AllowNull(false)
|
||||
@Column
|
||||
declare ip: string
|
||||
|
||||
@AllowNull(true)
|
||||
@Column
|
||||
declare version: string
|
||||
|
||||
@CreatedAt
|
||||
declare createdAt: Date
|
||||
|
||||
@UpdatedAt
|
||||
declare updatedAt: Date
|
||||
|
||||
@ForeignKey(() => RunnerRegistrationTokenModel)
|
||||
@Column
|
||||
declare runnerRegistrationTokenId: number
|
||||
|
||||
@BelongsTo(() => RunnerRegistrationTokenModel, {
|
||||
foreignKey: {
|
||||
allowNull: false
|
||||
},
|
||||
onDelete: 'cascade'
|
||||
})
|
||||
declare 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,
|
||||
version: this.version,
|
||||
|
||||
createdAt: this.createdAt,
|
||||
updatedAt: this.updatedAt
|
||||
}
|
||||
}
|
||||
}
|
||||
312
server/core/models/server/plugin.ts
Normal file
312
server/core/models/server/plugin.ts
Normal file
@@ -0,0 +1,312 @@
|
||||
import {
|
||||
PeerTubePlugin,
|
||||
PluginType,
|
||||
RegisterServerSettingOptions,
|
||||
SettingEntries,
|
||||
SettingValue,
|
||||
type PluginType_Type
|
||||
} from '@peertube/peertube-models'
|
||||
import { isStableOrUnstableVersionValid, isStableVersionValid } from '@server/helpers/custom-validators/misc.js'
|
||||
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,
|
||||
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
|
||||
declare name: string
|
||||
|
||||
@AllowNull(false)
|
||||
@Is('PluginType', value => throwIfNotValid(value, isPluginTypeValid, 'type'))
|
||||
@Column
|
||||
declare type: PluginType_Type
|
||||
|
||||
@AllowNull(false)
|
||||
@Is('PluginVersion', value => throwIfNotValid(value, isStableOrUnstableVersionValid, 'version'))
|
||||
@Column
|
||||
declare version: string
|
||||
|
||||
@AllowNull(true)
|
||||
@Is('PluginLatestVersion', value => throwIfNotValid(value, isStableVersionValid, 'latestVersion'))
|
||||
@Column
|
||||
declare latestVersion: string
|
||||
|
||||
@AllowNull(false)
|
||||
@Column
|
||||
declare enabled: boolean
|
||||
|
||||
@AllowNull(false)
|
||||
@Column
|
||||
declare uninstalled: boolean
|
||||
|
||||
@AllowNull(false)
|
||||
@Column
|
||||
declare peertubeEngine: string
|
||||
|
||||
@AllowNull(true)
|
||||
@Is('PluginDescription', value => throwIfNotValid(value, isPluginDescriptionValid, 'description'))
|
||||
@Column
|
||||
declare description: string
|
||||
|
||||
@AllowNull(false)
|
||||
@Is('PluginHomepage', value => throwIfNotValid(value, isPluginHomepage, 'homepage'))
|
||||
@Column
|
||||
declare homepage: string
|
||||
|
||||
@AllowNull(true)
|
||||
@Column(DataType.JSONB)
|
||||
declare settings: any
|
||||
|
||||
@AllowNull(true)
|
||||
@Column(DataType.JSONB)
|
||||
declare storage: any
|
||||
|
||||
@CreatedAt
|
||||
declare createdAt: Date
|
||||
|
||||
@UpdatedAt
|
||||
declare 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?.[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
|
||||
}
|
||||
}
|
||||
}
|
||||
187
server/core/models/server/server-blocklist.ts
Normal file
187
server/core/models/server/server-blocklist.ts
Normal file
@@ -0,0 +1,187 @@
|
||||
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
|
||||
declare createdAt: Date
|
||||
|
||||
@UpdatedAt
|
||||
declare updatedAt: Date
|
||||
|
||||
@ForeignKey(() => AccountModel)
|
||||
@Column
|
||||
declare accountId: number
|
||||
|
||||
@BelongsTo(() => AccountModel, {
|
||||
foreignKey: {
|
||||
name: 'accountId',
|
||||
allowNull: false
|
||||
},
|
||||
onDelete: 'CASCADE'
|
||||
})
|
||||
declare ByAccount: Awaited<AccountModel>
|
||||
|
||||
@ForeignKey(() => ServerModel)
|
||||
@Column
|
||||
declare targetServerId: number
|
||||
|
||||
@BelongsTo(() => ServerModel, {
|
||||
foreignKey: {
|
||||
allowNull: false
|
||||
},
|
||||
onDelete: 'CASCADE'
|
||||
})
|
||||
declare 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
|
||||
}
|
||||
}
|
||||
}
|
||||
114
server/core/models/server/server.ts
Normal file
114
server/core/models/server/server.ts
Normal file
@@ -0,0 +1,114 @@
|
||||
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'
|
||||
import { AttributesOnly } from '@peertube/peertube-typescript-utils'
|
||||
|
||||
export const serverSummaryAttributes = [ 'id', 'host' ] as const satisfies (keyof AttributesOnly<ServerModel>)[]
|
||||
|
||||
@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
|
||||
declare host: string
|
||||
|
||||
@AllowNull(false)
|
||||
@Default(false)
|
||||
@Column
|
||||
declare redundancyAllowed: boolean
|
||||
|
||||
@CreatedAt
|
||||
declare createdAt: Date
|
||||
|
||||
@UpdatedAt
|
||||
declare updatedAt: Date
|
||||
|
||||
@HasMany(() => ActorModel, {
|
||||
foreignKey: {
|
||||
name: 'serverId',
|
||||
allowNull: true
|
||||
},
|
||||
onDelete: 'CASCADE',
|
||||
hooks: true
|
||||
})
|
||||
declare Actors: Awaited<ActorModel>[]
|
||||
|
||||
@HasMany(() => ServerBlocklistModel, {
|
||||
foreignKey: {
|
||||
allowNull: false
|
||||
},
|
||||
onDelete: 'CASCADE'
|
||||
})
|
||||
declare BlockedBy: Awaited<ServerBlocklistModel>[]
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
static getSQLAttributes (tableName: string, aliasPrefix = '') {
|
||||
return buildSQLAttributes({
|
||||
model: this,
|
||||
tableName,
|
||||
aliasPrefix
|
||||
})
|
||||
}
|
||||
|
||||
static getSQLSummaryAttributes (tableName: string, aliasPrefix = '') {
|
||||
return buildSQLAttributes({
|
||||
model: this,
|
||||
tableName,
|
||||
aliasPrefix,
|
||||
includeAttributes: serverSummaryAttributes
|
||||
})
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
73
server/core/models/server/tracker.ts
Normal file
73
server/core/models/server/tracker.ts
Normal file
@@ -0,0 +1,73 @@
|
||||
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
|
||||
declare url: string
|
||||
|
||||
@CreatedAt
|
||||
declare createdAt: Date
|
||||
|
||||
@UpdatedAt
|
||||
declare updatedAt: Date
|
||||
|
||||
@BelongsToMany(() => VideoModel, {
|
||||
foreignKey: 'trackerId',
|
||||
through: () => VideoTrackerModel,
|
||||
onDelete: 'CASCADE'
|
||||
})
|
||||
declare 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
server/core/models/server/video-tracker.ts
Normal file
31
server/core/models/server/video-tracker.ts
Normal file
@@ -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
|
||||
declare createdAt: Date
|
||||
|
||||
@UpdatedAt
|
||||
declare updatedAt: Date
|
||||
|
||||
@ForeignKey(() => VideoModel)
|
||||
@Column
|
||||
declare videoId: number
|
||||
|
||||
@ForeignKey(() => TrackerModel)
|
||||
@Column
|
||||
declare trackerId: number
|
||||
}
|
||||
124
server/core/models/shared/abstract-list-query.ts
Normal file
124
server/core/models/shared/abstract-list-query.ts
Normal file
@@ -0,0 +1,124 @@
|
||||
import { AbstractRunQuery, ModelBuilder, parseRowCountResult } from '@server/models/shared/index.js'
|
||||
import { Model, Sequelize, Transaction } from 'sequelize'
|
||||
|
||||
export interface AbstractListQueryOptions {
|
||||
start?: number
|
||||
count?: number
|
||||
sort?: string
|
||||
transaction?: Transaction
|
||||
}
|
||||
|
||||
export interface ModelInfo {
|
||||
tableName: string
|
||||
modelName: string
|
||||
}
|
||||
|
||||
export abstract class AbstractListQuery extends AbstractRunQuery {
|
||||
protected attributes: string[] = []
|
||||
protected join = ''
|
||||
|
||||
private subQuery: string
|
||||
|
||||
protected subQueryAttributes: string[] = []
|
||||
protected subQueryLateralJoin = ''
|
||||
protected subQueryJoin = ''
|
||||
protected subQueryWhere = ''
|
||||
|
||||
protected abstract buildQueryAttributes (): void
|
||||
protected abstract buildQueryJoin (): void
|
||||
|
||||
protected abstract buildSubQueryWhere (): void
|
||||
protected abstract buildSubQueryAttributes (): void
|
||||
protected abstract buildSubQueryJoin (): void
|
||||
|
||||
protected buildSubQueryLateralJoin () {
|
||||
// Optional
|
||||
}
|
||||
|
||||
protected getCalculatedAttributes () {
|
||||
return []
|
||||
}
|
||||
|
||||
constructor (
|
||||
protected readonly sequelize: Sequelize,
|
||||
protected readonly modelInfo: ModelInfo,
|
||||
protected readonly options: AbstractListQueryOptions
|
||||
) {
|
||||
super(sequelize)
|
||||
}
|
||||
|
||||
async get<T extends Model> () {
|
||||
this.options.start = 0
|
||||
this.options.count = 1
|
||||
|
||||
const rows = await this.list<T>()
|
||||
|
||||
return rows.length === 1
|
||||
? rows[0]
|
||||
: null
|
||||
}
|
||||
|
||||
async list<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, this.modelInfo.modelName.replace(/Model$/, ''))
|
||||
}
|
||||
|
||||
async count () {
|
||||
this.buildCountQuery()
|
||||
|
||||
const result = await this.runQuery({ transaction: this.options.transaction })
|
||||
|
||||
return parseRowCountResult(result)
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
private buildListQuery () {
|
||||
this.buildSubQuery()
|
||||
|
||||
this.attributes = [ `"${this.modelInfo.modelName}".*` ]
|
||||
this.buildQueryAttributes()
|
||||
this.buildQueryJoin()
|
||||
|
||||
this.query = `${this.buildSelect(this.attributes)} ` +
|
||||
`FROM (${this.subQuery}) AS "${this.modelInfo.modelName}" ` +
|
||||
`${this.join} ` +
|
||||
`${this.getOrder(this.modelInfo.modelName, this.options.sort)}`
|
||||
}
|
||||
|
||||
private buildSubQuery () {
|
||||
this.buildSubQueryWhere()
|
||||
this.buildSubQueryJoin()
|
||||
this.buildSubQueryLateralJoin()
|
||||
this.buildSubQueryAttributes()
|
||||
|
||||
this.subQuery = `${this.buildSelect(this.subQueryAttributes)} ` +
|
||||
`FROM "${this.modelInfo.tableName}" AS "${this.modelInfo.modelName}" ` +
|
||||
`${this.subQueryJoin} ` +
|
||||
`${this.subQueryLateralJoin} ` +
|
||||
`${this.subQueryWhere} `
|
||||
|
||||
if (this.options.sort) {
|
||||
this.subQuery += `${this.getOrder(this.modelInfo.modelName, this.options.sort, this.getCalculatedAttributes())} `
|
||||
}
|
||||
|
||||
if (this.options.start !== undefined && this.options.count !== undefined) {
|
||||
this.subQuery += this.getLimit(this.options.start, this.options.count)
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
private buildCountQuery () {
|
||||
this.buildSubQueryWhere()
|
||||
|
||||
this.query = `SELECT COUNT(*) AS "total" ` +
|
||||
`FROM "${this.modelInfo.tableName}" AS "${this.modelInfo.modelName}" ` +
|
||||
`${this.subQueryJoin} ` +
|
||||
`${this.subQueryWhere}`
|
||||
}
|
||||
}
|
||||
78
server/core/models/shared/abstract-run-query.ts
Normal file
78
server/core/models/shared/abstract-run-query.ts
Normal file
@@ -0,0 +1,78 @@
|
||||
import { QueryTypes, Sequelize, Transaction } from 'sequelize'
|
||||
import { getSort } from './sort.js'
|
||||
import { Col } from 'sequelize/lib/utils'
|
||||
|
||||
/**
|
||||
* Abstract builder to run video SQL queries
|
||||
*/
|
||||
|
||||
export class AbstractRunQuery {
|
||||
protected query: string
|
||||
protected replacements: any = {}
|
||||
|
||||
protected queryConfig = ''
|
||||
|
||||
constructor (protected readonly sequelize: Sequelize) {
|
||||
}
|
||||
|
||||
protected async 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
|
||||
}
|
||||
|
||||
if (this.queryConfig) {
|
||||
await this.sequelize.query(this.queryConfig, queryOptions)
|
||||
}
|
||||
|
||||
return this.sequelize.query<any>(this.query, queryOptions)
|
||||
}
|
||||
|
||||
protected buildSelect (attributes: string[]) {
|
||||
return `SELECT ${attributes.join(', ')} `
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
protected getOrder (tableName: string, sort: string, calculatedAttributes: string[] = []) {
|
||||
if (!sort) return ''
|
||||
|
||||
const orders = this.getSort(sort)
|
||||
|
||||
const orderStr = orders.map(o => {
|
||||
const columnName = o[0] instanceof Col
|
||||
? o[0].col
|
||||
: o[0]
|
||||
|
||||
const direction = o[1]
|
||||
|
||||
// Prefix with the table name if the column name isn't a full path
|
||||
// ("id", "displayName", etc. VS "ActorModel.id", "Server.redundancyAllowed", etc.)
|
||||
const prefix = calculatedAttributes.includes(columnName) || columnName.includes('.')
|
||||
? ''
|
||||
: `"${tableName}".`
|
||||
|
||||
return `${prefix}"${columnName}" ${direction}`
|
||||
}).join(', ')
|
||||
|
||||
return 'ORDER BY ' + orderStr
|
||||
}
|
||||
|
||||
protected getSort (sort: string) {
|
||||
return getSort(sort)
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
protected getLimit (start: number, count: number) {
|
||||
if (!count) return ''
|
||||
|
||||
this.replacements.limit = count
|
||||
this.replacements.offset = start || 0
|
||||
|
||||
return `LIMIT :limit OFFSET :offset `
|
||||
}
|
||||
}
|
||||
9
server/core/models/shared/delete.ts
Normal file
9
server/core/models/shared/delete.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import { MAX_SQL_DELETE_ITEMS } from '@server/initializers/constants.js'
|
||||
|
||||
export async function safeBulkDestroy (destroyFn: () => Promise<number>) {
|
||||
const destroyedRows = await destroyFn()
|
||||
|
||||
if (destroyedRows === MAX_SQL_DELETE_ITEMS) {
|
||||
return safeBulkDestroy(destroyFn)
|
||||
}
|
||||
}
|
||||
11
server/core/models/shared/index.ts
Normal file
11
server/core/models/shared/index.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
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/common-helpers.js'
|
||||
export * from './update.js'
|
||||
export * from './delete.js'
|
||||
export * from './table.js'
|
||||
154
server/core/models/shared/model-builder.ts
Normal file
154
server/core/models/shared/model-builder.ts
Normal file
@@ -0,0 +1,154 @@
|
||||
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>()
|
||||
|
||||
private readonly aliasToTableName = {
|
||||
ChannelCollab: 'VideoChannelCollaborator',
|
||||
Collabs: 'VideoChannelCollaborators'
|
||||
}
|
||||
|
||||
private readonly tableNameToModelName = {
|
||||
Avatars: 'ActorImageModel',
|
||||
Banners: 'ActorImageModel',
|
||||
ActorFollowing: 'ActorModel',
|
||||
ActorFollower: 'ActorModel',
|
||||
FlaggedAccount: 'AccountModel',
|
||||
CommentAutomaticTags: 'CommentAutomaticTagModel',
|
||||
OwnerAccount: 'AccountModel',
|
||||
Thumbnails: 'ThumbnailModel',
|
||||
Channel: 'VideoChannelModel',
|
||||
VideoChannels: 'VideoChannelModel',
|
||||
VideoPlaylists: 'VideoPlaylistModel',
|
||||
NotificationSetting: 'UserNotificationSettingModel',
|
||||
VideoChannelCollaborators: 'VideoChannelCollaboratorModel',
|
||||
Tags: 'TagModel'
|
||||
}
|
||||
|
||||
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, rootTableName: string, keyPath: string) {
|
||||
if (!json.id) return { created: false, model: null }
|
||||
|
||||
const { created, model } = this.createOrFindModel(json, rootTableName, keyPath)
|
||||
|
||||
for (const key of Object.keys(json)) {
|
||||
const value = json[key]
|
||||
if (!value) continue
|
||||
|
||||
const tableName = this.buildTableName(key)
|
||||
|
||||
// Child model
|
||||
if (isPlainObject(value)) {
|
||||
const Model = this.findModelBuilder(rootTableName)
|
||||
const association = Model.associations[tableName]
|
||||
|
||||
if (!association) {
|
||||
if (!Model.getAttributes()[tableName]) {
|
||||
logger.error(`Cannot find association ${tableName} from key ${key} of model ${rootTableName}`, {
|
||||
associations: Object.keys(Model.associations),
|
||||
model: Model.getAttributes()
|
||||
})
|
||||
}
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
const { created, model: subModel } = this.createModel(value, tableName, `${keyPath}.${json.id}.${key}`)
|
||||
|
||||
if (association.isMultiAssociation && !Array.isArray(model[tableName])) {
|
||||
model[tableName] = []
|
||||
}
|
||||
|
||||
if (!created || !subModel) continue
|
||||
|
||||
if (Array.isArray(model[tableName])) {
|
||||
model[tableName].push(subModel)
|
||||
} else {
|
||||
model[tableName] = subModel
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return { created, model }
|
||||
}
|
||||
|
||||
private createOrFindModel (json: any, tableName: 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(tableName)
|
||||
if (!Model) {
|
||||
throw new Error(`Cannot find model builder for ${tableName}. You may have to add an alias in ModelBuilder class`)
|
||||
}
|
||||
|
||||
if (!Model) {
|
||||
logger.error(
|
||||
'Cannot build model %s that does not exist',
|
||||
this.buildSequelizeModelName(tableName),
|
||||
{ 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 (tableName: string) {
|
||||
return this.sequelize.modelManager.getModel(this.buildSequelizeModelName(tableName)) as ModelStatic<T>
|
||||
}
|
||||
|
||||
private buildSequelizeModelName (tableNameArg: string) {
|
||||
const tableName = this.buildTableName(tableNameArg)
|
||||
|
||||
return this.tableNameToModelName[tableName] ?? tableName + 'Model'
|
||||
}
|
||||
|
||||
private buildTableName (tableName: string) {
|
||||
return this.aliasToTableName[tableName] ?? tableName
|
||||
}
|
||||
|
||||
private getModelRegistryKey (json: any, keyPath: string) {
|
||||
return keyPath + json.id
|
||||
}
|
||||
}
|
||||
94
server/core/models/shared/model-cache.ts
Normal file
94
server/core/models/shared/model-cache.ts
Normal file
@@ -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
|
||||
}
|
||||
78
server/core/models/shared/position.ts
Normal file
78
server/core/models/shared/position.ts
Normal file
@@ -0,0 +1,78 @@
|
||||
import { forceNumber } from '@peertube/peertube-core-utils'
|
||||
import { AggregateOptions, Attributes, Model, ModelStatic, Op, Sequelize, Transaction, WhereOptions } from 'sequelize'
|
||||
|
||||
export async function getNextPositionOf<T extends Model> (options: {
|
||||
model: ModelStatic<T>
|
||||
columnName: keyof Attributes<T>
|
||||
where: WhereOptions<Attributes<T>>
|
||||
transaction: Transaction
|
||||
}) {
|
||||
const { columnName, model, where, transaction } = options
|
||||
|
||||
const query: AggregateOptions<number> = {
|
||||
where,
|
||||
transaction
|
||||
}
|
||||
|
||||
const position = await model.max(columnName, query)
|
||||
|
||||
return position
|
||||
? position + 1
|
||||
: 1
|
||||
}
|
||||
|
||||
export function reassignPositionOf<T extends Model> (options: {
|
||||
model: ModelStatic<T>
|
||||
columnName: keyof Attributes<T>
|
||||
where: WhereOptions<Attributes<T>>
|
||||
transaction: Transaction
|
||||
|
||||
firstPosition: number
|
||||
endPosition: number
|
||||
newPosition: number
|
||||
}) {
|
||||
const { firstPosition, endPosition, newPosition, model, where, columnName, transaction } = options
|
||||
|
||||
const query = {
|
||||
where: {
|
||||
...where,
|
||||
|
||||
[columnName]: {
|
||||
[Op.gte]: firstPosition,
|
||||
[Op.lte]: endPosition
|
||||
}
|
||||
},
|
||||
transaction,
|
||||
validate: false // We use a literal to update the position
|
||||
}
|
||||
|
||||
const escapedColumnName = model.sequelize.escape(columnName as string).replace(/'/g, '')
|
||||
const positionQuery = Sequelize.literal(`${forceNumber(newPosition)} + "${escapedColumnName}" - ${forceNumber(firstPosition)}`)
|
||||
|
||||
return model.update({ [columnName]: positionQuery } as Record<typeof columnName, typeof positionQuery>, query)
|
||||
}
|
||||
|
||||
export function increasePositionOf<T extends Model> (options: {
|
||||
model: ModelStatic<T>
|
||||
columnName: keyof Attributes<T>
|
||||
where: WhereOptions<Attributes<T>>
|
||||
transaction: Transaction
|
||||
|
||||
fromPosition: number
|
||||
by: number
|
||||
}) {
|
||||
const { model, where, transaction, fromPosition, by, columnName } = options
|
||||
|
||||
const query = {
|
||||
where: {
|
||||
...where,
|
||||
|
||||
[columnName]: {
|
||||
[Op.gte]: fromPosition
|
||||
}
|
||||
},
|
||||
transaction
|
||||
}
|
||||
|
||||
return model.increment({ [columnName]: by } as Record<typeof columnName, number>, query)
|
||||
}
|
||||
91
server/core/models/shared/query.ts
Normal file
91
server/core/models/shared/query.ts
Normal file
@@ -0,0 +1,91 @@
|
||||
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): number {
|
||||
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
server/core/models/shared/sequelize-helpers.ts
Normal file
39
server/core/models/shared/sequelize-helpers.ts
Normal file
@@ -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
server/core/models/shared/sequelize-type.ts
Normal file
6
server/core/models/shared/sequelize-type.ts
Normal file
@@ -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>>> {
|
||||
declare id: number
|
||||
}
|
||||
146
server/core/models/shared/sort.ts
Normal file
146
server/core/models/shared/sort.ts
Normal file
@@ -0,0 +1,146 @@
|
||||
import { literal, OrderItem, Sequelize } from 'sequelize'
|
||||
|
||||
// Translate for example "-name" to [ [ 'name', 'DESC' ], [ 'id', 'ASC' ] ]
|
||||
export 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 ]
|
||||
}
|
||||
|
||||
export 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' ] ]
|
||||
}
|
||||
|
||||
export function getPlaylistSort (value: string, lastSort: OrderItem = [ 'id', 'ASC' ]): OrderItem[] {
|
||||
const { direction, field } = buildSortDirectionAndField(value)
|
||||
|
||||
if (field.toLowerCase() === 'name') {
|
||||
return [ [ 'name', direction ], lastSort ]
|
||||
}
|
||||
|
||||
return getSort(value, lastSort)
|
||||
}
|
||||
|
||||
export 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 ]
|
||||
}
|
||||
|
||||
export 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)
|
||||
}
|
||||
|
||||
export 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)
|
||||
}
|
||||
|
||||
export function getChannelSyncSort (value: string): OrderItem[] {
|
||||
const { direction, field } = buildSortDirectionAndField(value)
|
||||
if (field.toLowerCase() === 'videochannel') {
|
||||
return [
|
||||
[ literal('"VideoChannel.name"'), direction ]
|
||||
]
|
||||
}
|
||||
return [ [ field, direction ] ]
|
||||
}
|
||||
|
||||
export function getSubscriptionSort (value: string): OrderItem[] {
|
||||
const { direction, field } = buildSortDirectionAndField(value)
|
||||
|
||||
if (field === 'channelUpdatedAt') {
|
||||
return [
|
||||
[ literal('"ActorFollowing.VideoChannel.updatedAt"'), direction ]
|
||||
]
|
||||
}
|
||||
return [ [ field, direction ] ]
|
||||
}
|
||||
|
||||
export function buildSortDirectionAndField (value: string) {
|
||||
let field: string
|
||||
let direction: 'ASC' | 'DESC'
|
||||
|
||||
if (value.startsWith('-')) {
|
||||
direction = 'DESC'
|
||||
field = value.substring(1)
|
||||
} else {
|
||||
direction = 'ASC'
|
||||
field = value
|
||||
}
|
||||
|
||||
return { direction, field }
|
||||
}
|
||||
101
server/core/models/shared/sql/actor-helpers.ts
Normal file
101
server/core/models/shared/sql/actor-helpers.ts
Normal file
@@ -0,0 +1,101 @@
|
||||
import { ActorImageType } from '@peertube/peertube-models'
|
||||
|
||||
export function getActorJoin (options: {
|
||||
base?: string
|
||||
on: string
|
||||
required: boolean
|
||||
type: 'account' | 'channel'
|
||||
includeAvatars?: boolean // default false
|
||||
}) {
|
||||
const { base = '', on, includeAvatars = false, type, required } = options
|
||||
|
||||
const avatarsJoin = includeAvatars
|
||||
? getAvatarsJoin({ base: `${base}Actor`, on: `"${base}Actor"."id"` })
|
||||
: ''
|
||||
|
||||
const join = required
|
||||
? 'INNER JOIN'
|
||||
: 'LEFT JOIN'
|
||||
|
||||
const column = type === 'account'
|
||||
? 'accountId'
|
||||
: 'videoChannelId'
|
||||
|
||||
return ` ${join} "actor" "${base}Actor" ON "${base}Actor"."${column}" = ${on} ` +
|
||||
`LEFT JOIN "server" "${base}Actor->Server" ` +
|
||||
` ON "${base}Actor->Server"."id" = "${base}Actor"."serverId" ` +
|
||||
avatarsJoin
|
||||
}
|
||||
|
||||
export function getChannelJoin (options: {
|
||||
base?: string
|
||||
on: string
|
||||
includeAccount: boolean
|
||||
includeAvatars: boolean
|
||||
includeActors: boolean
|
||||
required: boolean
|
||||
}) {
|
||||
const { base = '', on, includeAccount, includeAvatars, includeActors, required } = options
|
||||
|
||||
const accountJoin = includeAccount
|
||||
? getAccountJoin({
|
||||
base: `${base}VideoChannel->`,
|
||||
on: `"${base}VideoChannel"."accountId"`,
|
||||
includeAvatars,
|
||||
includeActor: includeActors,
|
||||
required
|
||||
})
|
||||
: ''
|
||||
|
||||
const actorJoin = includeActors
|
||||
? getActorJoin({
|
||||
base: `${base}VideoChannel->`,
|
||||
on: `"${base}VideoChannel"."id"`,
|
||||
type: 'channel',
|
||||
includeAvatars,
|
||||
required
|
||||
})
|
||||
: ''
|
||||
|
||||
const join = required
|
||||
? 'INNER JOIN'
|
||||
: 'LEFT JOIN'
|
||||
|
||||
return ` ${join} "videoChannel" "${base}VideoChannel" ON "${base}VideoChannel"."id" = ${on} ` +
|
||||
actorJoin +
|
||||
accountJoin
|
||||
}
|
||||
|
||||
export function getAccountJoin (options: {
|
||||
base?: string
|
||||
on: string
|
||||
includeAvatars: boolean
|
||||
includeActor: boolean
|
||||
required: boolean
|
||||
}) {
|
||||
const { base = '', on, includeAvatars, includeActor, required } = options
|
||||
|
||||
const actorJoin = includeActor
|
||||
? getActorJoin({
|
||||
base: `${base}Account->`,
|
||||
on: `"${base}Account"."id"`,
|
||||
type: 'account',
|
||||
includeAvatars,
|
||||
required
|
||||
})
|
||||
: ''
|
||||
|
||||
return ` LEFT JOIN "account" "${base}Account" ON "${base}Account"."id" = ${on} ` +
|
||||
actorJoin
|
||||
}
|
||||
|
||||
export function getAvatarsJoin (options: {
|
||||
base?: string
|
||||
on: string
|
||||
}) {
|
||||
const { base = '', on } = options
|
||||
|
||||
return ` LEFT JOIN "actorImage" "${base}Avatars" ` +
|
||||
`ON "${base}Avatars"."actorId" = ${on} ` +
|
||||
`AND "${base}Avatars"."type" = ${ActorImageType.AVATAR} `
|
||||
}
|
||||
39
server/core/models/shared/sql/common-helpers.ts
Normal file
39
server/core/models/shared/sql/common-helpers.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import { forceNumber } from '@peertube/peertube-core-utils'
|
||||
import { FollowState } from '@peertube/peertube-models'
|
||||
import { literal } from 'sequelize'
|
||||
import { Literal } from 'sequelize/types/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"."accountId" = "account"."id" 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."id" = actor."accountId" ' +
|
||||
'INNER JOIN "serverBlocklist" ON "actor"."serverId" = "serverBlocklist"."targetServerId" ' +
|
||||
'WHERE "serverBlocklist"."accountId" IN (' + blockerIdsString + ')'
|
||||
}
|
||||
|
||||
export function buildServerIdsFollowedBy (actorId: any) {
|
||||
const actorIdNumber = forceNumber(actorId)
|
||||
const followState: FollowState = 'accepted'
|
||||
|
||||
return '(' +
|
||||
'SELECT "actor"."serverId" FROM "actorFollow" ' +
|
||||
'INNER JOIN "actor" ON actor.id = "actorFollow"."targetActorId" ' +
|
||||
`WHERE "actorFollow"."actorId" = ${actorIdNumber} AND "actorFollow"."state" = '${followState}'` +
|
||||
')'
|
||||
}
|
||||
44
server/core/models/shared/table.ts
Normal file
44
server/core/models/shared/table.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import { AttributesOnly } from '@peertube/peertube-typescript-utils'
|
||||
import { Model, ModelStatic } from 'sequelize'
|
||||
|
||||
export function buildSQLAttributes<M extends Model> (options: {
|
||||
model: ModelStatic<M>
|
||||
tableName: string
|
||||
|
||||
excludeAttributes?: readonly Exclude<keyof AttributesOnly<M>, symbol>[]
|
||||
includeAttributes?: readonly Exclude<keyof AttributesOnly<M>, symbol>[]
|
||||
|
||||
aliasPrefix?: string
|
||||
|
||||
idBuilder?: string[]
|
||||
}) {
|
||||
const { model, tableName, aliasPrefix = '', excludeAttributes, includeAttributes, 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
|
||||
})
|
||||
.filter(a => {
|
||||
if (!includeAttributes) return true
|
||||
if (includeAttributes.includes(a)) return true
|
||||
|
||||
return false
|
||||
})
|
||||
.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
|
||||
}
|
||||
30
server/core/models/shared/update.ts
Normal file
30
server/core/models/shared/update.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import { QueryTypes, Sequelize, Transaction } from 'sequelize'
|
||||
|
||||
const updating = new Set<string>()
|
||||
|
||||
// Sequelize always skip the update if we only update updatedAt field
|
||||
export 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)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,271 @@
|
||||
import { ActorImageType, UserNotificationType_Type } from '@peertube/peertube-models'
|
||||
import { AbstractListQuery, AbstractListQueryOptions } from '@server/models/shared/abstract-list-query.js'
|
||||
import { Sequelize } from 'sequelize'
|
||||
|
||||
export interface ListNotificationsOptions extends AbstractListQueryOptions {
|
||||
userId: number
|
||||
unread?: boolean
|
||||
typeOneOf?: UserNotificationType_Type[]
|
||||
}
|
||||
|
||||
export class UserNotificationListQueryBuilder extends AbstractListQuery {
|
||||
constructor (
|
||||
protected readonly sequelize: Sequelize,
|
||||
protected readonly options: ListNotificationsOptions
|
||||
) {
|
||||
super(sequelize, { modelName: 'UserNotificationModel', tableName: 'userNotification' }, options)
|
||||
}
|
||||
|
||||
protected buildSubQueryWhere () {
|
||||
this.subQueryWhere = 'WHERE "UserNotificationModel"."userId" = :userId '
|
||||
this.replacements.userId = this.options.userId
|
||||
|
||||
if (this.options.unread === true) {
|
||||
this.subQueryWhere += 'AND "UserNotificationModel"."read" IS FALSE '
|
||||
} else if (this.options.unread === false) {
|
||||
this.subQueryWhere += 'AND "UserNotificationModel"."read" IS TRUE '
|
||||
}
|
||||
|
||||
if (this.options.typeOneOf) {
|
||||
this.subQueryWhere += 'AND "UserNotificationModel"."type" IN (:typeOneOf) '
|
||||
this.replacements.typeOneOf = this.options.typeOneOf
|
||||
}
|
||||
}
|
||||
|
||||
protected buildSubQueryJoin () {
|
||||
}
|
||||
|
||||
protected buildSubQueryAttributes () {
|
||||
this.subQueryAttributes = [
|
||||
...this.subQueryAttributes,
|
||||
|
||||
`*`
|
||||
]
|
||||
}
|
||||
|
||||
protected buildQueryAttributes () {
|
||||
this.attributes = [
|
||||
...this.attributes,
|
||||
|
||||
`"UserNotificationModel"."id"`,
|
||||
`"UserNotificationModel"."type"`,
|
||||
`"UserNotificationModel"."read"`,
|
||||
`"UserNotificationModel"."data"`,
|
||||
`"UserNotificationModel"."createdAt"`,
|
||||
`"UserNotificationModel"."updatedAt"`,
|
||||
|
||||
`"Video"."id" AS "Video.id"`,
|
||||
`"Video"."uuid" AS "Video.uuid"`,
|
||||
`"Video"."name" AS "Video.name"`,
|
||||
`"Video"."state" AS "Video.state"`,
|
||||
...this.getAccountOrChannelAttributes('Video->VideoChannel', 'Video.VideoChannel'),
|
||||
|
||||
`"VideoComment"."id" AS "VideoComment.id"`,
|
||||
`"VideoComment"."originCommentId" AS "VideoComment.originCommentId"`,
|
||||
`"VideoComment"."heldForReview" AS "VideoComment.heldForReview"`,
|
||||
`"VideoComment->Video"."id" AS "VideoComment.Video.id"`,
|
||||
`"VideoComment->Video"."uuid" AS "VideoComment.Video.uuid"`,
|
||||
`"VideoComment->Video"."name" AS "VideoComment.Video.name"`,
|
||||
`"VideoComment->Video"."state" AS "VideoComment.Video.state"`,
|
||||
...this.getAccountOrChannelAttributes('VideoComment->Account', 'VideoComment.Account'),
|
||||
|
||||
`"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->VideoAbuse->Video"."state" AS "Abuse.VideoAbuse.Video.state"`,
|
||||
`"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->VideoCommentAbuse->VideoComment->Video"."state" AS "Abuse.VideoCommentAbuse.VideoComment.Video.state"`,
|
||||
...this.getAccountOrChannelAttributes('Abuse->FlaggedAccount', 'Abuse.FlaggedAccount'),
|
||||
|
||||
`"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"`,
|
||||
`"VideoBlacklist->Video"."state" AS "VideoBlacklist.Video.state"`,
|
||||
|
||||
`"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"`,
|
||||
`"VideoImport->Video"."state" AS "VideoImport.Video.state"`,
|
||||
|
||||
`"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"`,
|
||||
|
||||
...this.getAccountOrChannelAttributes('Account', 'Account'),
|
||||
|
||||
`"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"`,
|
||||
`"VideoCaption->Video"."state" AS "VideoCaption.Video.state"`,
|
||||
|
||||
`"ChannelCollab"."id" AS "ChannelCollab.id"`,
|
||||
`"ChannelCollab"."state" AS "ChannelCollab.state"`,
|
||||
...this.getAccountOrChannelAttributes('ChannelCollab->Account', 'ChannelCollab.Account'),
|
||||
...this.getAccountOrChannelAttributes('ChannelCollab->Channel->Account', 'ChannelCollab.Channel.Account'),
|
||||
...this.getAccountOrChannelAttributes('ChannelCollab->Channel', 'ChannelCollab.Channel')
|
||||
]
|
||||
}
|
||||
|
||||
protected buildQueryJoin () {
|
||||
this.join = `
|
||||
LEFT JOIN (
|
||||
"video" AS "Video"
|
||||
${this.getChannelJoin('Video', 'channelId')}
|
||||
) ON "UserNotificationModel"."videoId" = "Video"."id"
|
||||
|
||||
LEFT JOIN (
|
||||
"videoComment" AS "VideoComment"
|
||||
${this.getAccountJoin('VideoComment', 'accountId')}
|
||||
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"
|
||||
${this.getActorJoin('Abuse->FlaggedAccount', 'accountId')}
|
||||
) 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"."accountId" = "ActorFollow->ActorFollower->Account"."id"
|
||||
${this.getActorImageJoin('ActorFollow->ActorFollower')}
|
||||
${this.getActorServerJoin('ActorFollow->ActorFollower')}
|
||||
|
||||
INNER JOIN "actor" AS "ActorFollow->ActorFollowing" ON "ActorFollow"."targetActorId" = "ActorFollow->ActorFollowing"."id"
|
||||
LEFT JOIN "videoChannel" AS "ActorFollow->ActorFollowing->VideoChannel"
|
||||
ON "ActorFollow->ActorFollowing"."videoChannelId" = "ActorFollow->ActorFollowing->VideoChannel"."id"
|
||||
LEFT JOIN "account" AS "ActorFollow->ActorFollowing->Account"
|
||||
ON "ActorFollow->ActorFollowing"."accountId" = "ActorFollow->ActorFollowing->Account"."id"
|
||||
${this.getActorServerJoin('ActorFollow->ActorFollowing')}
|
||||
) ON "UserNotificationModel"."actorFollowId" = "ActorFollow"."id"
|
||||
|
||||
LEFT JOIN (
|
||||
"account" AS "Account"
|
||||
${this.getActorJoin('Account', 'accountId')}
|
||||
) 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"
|
||||
|
||||
LEFT JOIN (
|
||||
"videoChannelCollaborator" AS "ChannelCollab"
|
||||
${this.getAccountJoin('ChannelCollab', 'accountId')}
|
||||
${this.getChannelJoin('ChannelCollab', 'channelId', 'Channel')}
|
||||
${this.getAccountJoin('ChannelCollab->Channel', 'accountId')}
|
||||
) ON "UserNotificationModel"."channelCollaboratorId" = "ChannelCollab"."id"`
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
private getAccountOrChannelAttributes (tableName: string, alias: string) {
|
||||
return [
|
||||
`"${tableName}"."id" AS "${alias}.id"`,
|
||||
`"${tableName}"."name" AS "${alias}.name"`,
|
||||
`"${tableName}->Actor"."id" AS "${alias}.Actor.id"`,
|
||||
`"${tableName}->Actor"."preferredUsername" AS "${alias}.Actor.preferredUsername"`,
|
||||
`"${tableName}->Actor->Avatars"."id" AS "${alias}.Actor.Avatars.id"`,
|
||||
`"${tableName}->Actor->Avatars"."width" AS "${alias}.Actor.Avatars.width"`,
|
||||
`"${tableName}->Actor->Avatars"."type" AS "${alias}.Actor.Avatars.type"`,
|
||||
`"${tableName}->Actor->Avatars"."filename" AS "${alias}.Actor.Avatars.filename"`,
|
||||
`"${tableName}->Actor->Server"."id" AS "${alias}.Actor.Server.id"`,
|
||||
`"${tableName}->Actor->Server"."host" AS "${alias}.Actor.Server.host"`
|
||||
]
|
||||
}
|
||||
|
||||
private getAccountJoin (tableName: string, columnJoin: string) {
|
||||
return `INNER JOIN "account" AS "${tableName}->Account" ON "${tableName}"."${columnJoin}" = "${tableName}->Account"."id" ` +
|
||||
this.getActorJoin(`${tableName}->Account`, 'accountId')
|
||||
}
|
||||
|
||||
private getChannelJoin (tableName: string, columnJoin: string, aliasTableName = 'VideoChannel') {
|
||||
// eslint-disable-next-line max-len
|
||||
return `INNER JOIN "videoChannel" AS "${tableName}->${aliasTableName}" ON "${tableName}"."${columnJoin}" = "${tableName}->${aliasTableName}".id ` +
|
||||
this.getActorJoin(`${tableName}->${aliasTableName}`, 'videoChannelId')
|
||||
}
|
||||
|
||||
private getActorJoin (tableName: string, column: string) {
|
||||
return `INNER JOIN "actor" AS "${tableName}->Actor" ON "${tableName}"."id" = "${tableName}->Actor"."${column}" ` +
|
||||
this.getActorImageJoin(`${tableName}->Actor`) +
|
||||
this.getActorServerJoin(`${tableName}->Actor`)
|
||||
}
|
||||
|
||||
private getActorImageJoin (tableName: string) {
|
||||
return `LEFT JOIN "actorImage" AS "${tableName}->Avatars"
|
||||
ON "${tableName}"."id" = "${tableName}->Avatars"."actorId"
|
||||
AND "${tableName}->Avatars"."type" = ${ActorImageType.AVATAR} `
|
||||
}
|
||||
|
||||
private getActorServerJoin (tableName: string) {
|
||||
return `LEFT JOIN "server" AS "${tableName}->Server"
|
||||
ON "${tableName}"."serverId" = "${tableName}->Server"."id" `
|
||||
}
|
||||
}
|
||||
155
server/core/models/user/sql/user/user-list-query-builder.ts
Normal file
155
server/core/models/user/sql/user/user-list-query-builder.ts
Normal file
@@ -0,0 +1,155 @@
|
||||
import { ActorImageType, VideoChannelCollaboratorState, VideoPlaylistType } from '@peertube/peertube-models'
|
||||
import { AbstractListQuery, AbstractListQueryOptions } from '@server/models/shared/abstract-list-query.js'
|
||||
import { Sequelize } from 'sequelize'
|
||||
import { UserTableAttributes } from './user-table-attributes.js'
|
||||
|
||||
export interface ListUserOptions extends AbstractListQueryOptions {
|
||||
userId: number
|
||||
}
|
||||
|
||||
export class UserListQueryBuilder extends AbstractListQuery {
|
||||
private readonly tableAttributes = new UserTableAttributes()
|
||||
|
||||
private builtAccountJoin = false
|
||||
private builtChannelsJoin = false
|
||||
private builtChannelCollabsJoin = false
|
||||
private builtPlaylistsJoin = false
|
||||
private builtNotificationSettingsJoin = false
|
||||
|
||||
constructor (
|
||||
protected readonly sequelize: Sequelize,
|
||||
protected readonly options: ListUserOptions
|
||||
) {
|
||||
super(sequelize, { modelName: 'UserModel', tableName: 'user' }, options)
|
||||
}
|
||||
|
||||
protected buildSubQueryWhere () {
|
||||
const where: string[] = []
|
||||
|
||||
if (this.options.userId) {
|
||||
where.push('"UserModel"."id" = :userId')
|
||||
|
||||
this.replacements.userId = this.options.userId
|
||||
}
|
||||
|
||||
if (where.length !== 0) {
|
||||
this.subQueryWhere = `WHERE ${where.join(' AND ')}`
|
||||
}
|
||||
}
|
||||
|
||||
private buildAccountJoin () {
|
||||
if (this.builtAccountJoin) return
|
||||
|
||||
this.join += ' INNER JOIN "account" "Account" ON "Account"."userId" = "UserModel"."id" ' +
|
||||
'INNER JOIN "actor" "Account->Actor" ON "Account->Actor"."accountId" = "Account"."id" ' +
|
||||
'LEFT JOIN "server" "Account->Actor->Server" ON "Account->Actor"."serverId" = "Account->Actor->Server"."id" ' +
|
||||
'LEFT JOIN "actorImage" "Account->Actor->Avatars" ON "Account->Actor->Avatars"."actorId" = "Account->Actor"."id" ' +
|
||||
` AND "Account->Actor->Avatars"."type" = ${ActorImageType.AVATAR} `
|
||||
|
||||
this.builtAccountJoin = true
|
||||
}
|
||||
|
||||
private buildChannelsJoin () {
|
||||
if (this.builtChannelsJoin) return
|
||||
|
||||
this.join += ' LEFT JOIN "videoChannel" "Account->VideoChannels" ON "Account"."id" = "Account->VideoChannels"."accountId" ' +
|
||||
'LEFT JOIN "actor" "Account->VideoChannels->Actor" ' +
|
||||
' ON "Account->VideoChannels->Actor"."videoChannelId" = "Account->VideoChannels"."id" ' +
|
||||
'LEFT JOIN "server" "Account->VideoChannels->Actor->Server" ' +
|
||||
' ON "Account->VideoChannels->Actor->Server"."id" = "Account->VideoChannels->Actor"."serverId" ' +
|
||||
'LEFT JOIN "actorImage" "Account->VideoChannels->Actor->Avatars" ' +
|
||||
' ON "Account->VideoChannels->Actor->Avatars"."actorId" = "Account->VideoChannels->Actor"."id" ' +
|
||||
` AND "Account->VideoChannels->Actor->Avatars"."type" = ${ActorImageType.AVATAR} ` +
|
||||
'LEFT JOIN "actorImage" "Account->VideoChannels->Actor->Banners" ' +
|
||||
' ON "Account->VideoChannels->Actor->Banners"."actorId" = "Account->VideoChannels->Actor"."id" ' +
|
||||
` AND "Account->VideoChannels->Actor->Banners"."type" = ${ActorImageType.BANNER} `
|
||||
|
||||
this.builtChannelsJoin = true
|
||||
}
|
||||
|
||||
private buildChannelCollabs () {
|
||||
if (this.builtChannelCollabsJoin) return
|
||||
|
||||
this.join += 'LEFT JOIN "videoChannelCollaborator" AS "Account->Collabs" ' +
|
||||
`ON "Account"."id" = "Account->Collabs"."accountId" AND "Account->Collabs"."state" = ${VideoChannelCollaboratorState.ACCEPTED} ` +
|
||||
'LEFT JOIN (' +
|
||||
' "videoChannel" "Account->Collabs->Channel" ' +
|
||||
' INNER JOIN "actor" AS "Account->Collabs->Channel->Actor" ' +
|
||||
' ON "Account->Collabs->Channel"."id" = "Account->Collabs->Channel->Actor"."videoChannelId" ' +
|
||||
' LEFT JOIN "server" AS "Account->Collabs->Channel->Actor->Server" ' +
|
||||
' ON "Account->Collabs->Channel->Actor"."serverId" = "Account->Collabs->Channel->Actor->Server"."id"' +
|
||||
' LEFT JOIN "actorImage" AS "Account->Collabs->Channel->Actor->Avatars" ' +
|
||||
' ON "Account->Collabs->Channel->Actor"."id" = "Account->Collabs->Channel->Actor->Avatars"."actorId"' +
|
||||
` AND "Account->Collabs->Channel->Actor->Avatars"."type" = ${ActorImageType.AVATAR}` +
|
||||
' LEFT JOIN "actorImage" AS "Account->Collabs->Channel->Actor->Banners" ' +
|
||||
' ON "Account->Collabs->Channel->Actor"."id" = "Account->Collabs->Channel->Actor->Banners"."actorId" ' +
|
||||
` AND "Account->Collabs->Channel->Actor->Banners"."type" = ${ActorImageType.BANNER} ` +
|
||||
') ON "Account->Collabs"."channelId" = "Account->Collabs->Channel"."id"'
|
||||
|
||||
this.builtChannelCollabsJoin = true
|
||||
}
|
||||
|
||||
private buildPlaylistsJoin () {
|
||||
if (this.builtPlaylistsJoin) return
|
||||
|
||||
this.join += 'INNER JOIN "videoPlaylist" AS "Account->VideoPlaylists" ' +
|
||||
'ON "Account"."id" = "Account->VideoPlaylists"."ownerAccountId" ' +
|
||||
`AND "Account->VideoPlaylists"."type" = ${VideoPlaylistType.WATCH_LATER} `
|
||||
|
||||
this.builtPlaylistsJoin = true
|
||||
}
|
||||
|
||||
private buildNotificationSettingsJoin () {
|
||||
if (this.builtNotificationSettingsJoin) return
|
||||
|
||||
this.join += 'INNER JOIN "userNotificationSetting" AS "NotificationSetting" ON "UserModel"."id" = "NotificationSetting"."userId"'
|
||||
|
||||
this.builtNotificationSettingsJoin = true
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
protected buildQueryJoin () {
|
||||
this.buildAccountJoin()
|
||||
this.buildChannelsJoin()
|
||||
this.buildChannelCollabs()
|
||||
this.buildPlaylistsJoin()
|
||||
this.buildNotificationSettingsJoin()
|
||||
}
|
||||
|
||||
protected buildQueryAttributes () {
|
||||
this.attributes = [
|
||||
...this.attributes,
|
||||
|
||||
this.tableAttributes.getAccountAttributes(),
|
||||
this.tableAttributes.getAccountActorAttributes(),
|
||||
this.tableAttributes.getAccountServerAttributes(),
|
||||
this.tableAttributes.getAccountAvatarAttributes(),
|
||||
this.tableAttributes.getChannelAttributes(),
|
||||
this.tableAttributes.getChannelActorAttributes(),
|
||||
this.tableAttributes.getChannelServerAttributes(),
|
||||
this.tableAttributes.getChannelAvatarAttributes(),
|
||||
this.tableAttributes.getChannelBannerAttributes(),
|
||||
this.tableAttributes.getPlaylistAttributes(),
|
||||
this.tableAttributes.getNotificationSettingAttributes(),
|
||||
this.tableAttributes.getCollabAttributes(),
|
||||
this.tableAttributes.getCollabChannelAttributes(),
|
||||
this.tableAttributes.getCollabChannelActorAttributes(),
|
||||
this.tableAttributes.getCollabChannelActorServerAttributes(),
|
||||
this.tableAttributes.getCollabChannelActorAvatarAttributes(),
|
||||
this.tableAttributes.getCollabChannelActorBannerAttributes()
|
||||
]
|
||||
}
|
||||
|
||||
protected buildSubQueryJoin () {
|
||||
// empty
|
||||
}
|
||||
|
||||
protected buildSubQueryAttributes () {
|
||||
this.subQueryAttributes = [
|
||||
...this.subQueryAttributes,
|
||||
|
||||
this.tableAttributes.getUserAttributes()
|
||||
]
|
||||
}
|
||||
}
|
||||
120
server/core/models/user/sql/user/user-table-attributes.ts
Normal file
120
server/core/models/user/sql/user/user-table-attributes.ts
Normal file
@@ -0,0 +1,120 @@
|
||||
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 { UserModel } from '../../user.js'
|
||||
import { VideoChannelModel } from '@server/models/video/video-channel.js'
|
||||
import { VideoPlaylistModel } from '@server/models/video/video-playlist.js'
|
||||
import { UserNotificationSettingModel } from '../../user-notification-setting.js'
|
||||
import { VideoChannelCollaboratorModel } from '@server/models/video/video-channel-collaborator.js'
|
||||
|
||||
export class UserTableAttributes {
|
||||
@Memoize()
|
||||
getUserAttributes () {
|
||||
return UserModel.getSQLAttributes('UserModel').join(', ')
|
||||
}
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
@Memoize()
|
||||
getAccountAttributes () {
|
||||
return AccountModel.getSQLAttributes('Account', 'Account.').join(', ')
|
||||
}
|
||||
|
||||
@Memoize()
|
||||
getAccountActorAttributes () {
|
||||
return ActorModel.getSQLAPIAttributes('Account->Actor', `Account.Actor.`).join(', ')
|
||||
}
|
||||
|
||||
@Memoize()
|
||||
getAccountServerAttributes () {
|
||||
return ServerModel.getSQLAttributes('Account->Actor->Server', `Account.Actor.Server.`).join(', ')
|
||||
}
|
||||
|
||||
@Memoize()
|
||||
getAccountAvatarAttributes () {
|
||||
return ActorImageModel.getSQLAttributes('Account->Actor->Avatars', 'Account.Actor.Avatars.').join(', ')
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
@Memoize()
|
||||
getChannelAttributes () {
|
||||
return VideoChannelModel.getSQLAttributes('Account->VideoChannels', 'Account.VideoChannels.').join(', ')
|
||||
}
|
||||
|
||||
@Memoize()
|
||||
getChannelActorAttributes () {
|
||||
return ActorModel.getSQLAPIAttributes('Account->VideoChannels->Actor', `Account.VideoChannels.Actor.`).join(', ')
|
||||
}
|
||||
|
||||
@Memoize()
|
||||
getChannelServerAttributes () {
|
||||
return ServerModel.getSQLSummaryAttributes('Account->VideoChannels->Actor->Server', `Account.VideoChannels.Actor.Server.`).join(', ')
|
||||
}
|
||||
|
||||
@Memoize()
|
||||
getChannelAvatarAttributes () {
|
||||
return ActorImageModel.getSQLAttributes('Account->VideoChannels->Actor->Avatars', 'Account.VideoChannels.Actor.Avatars.').join(', ')
|
||||
}
|
||||
|
||||
@Memoize()
|
||||
getChannelBannerAttributes () {
|
||||
return ActorImageModel.getSQLAttributes('Account->VideoChannels->Actor->Banners', 'Account.VideoChannels.Actor.Banners.').join(', ')
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
@Memoize()
|
||||
getPlaylistAttributes () {
|
||||
return VideoPlaylistModel.getSQLSummaryAttributes('Account->VideoPlaylists', 'Account.VideoPlaylists.').join(', ')
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
@Memoize()
|
||||
getNotificationSettingAttributes () {
|
||||
return UserNotificationSettingModel.getSQLAttributes('NotificationSetting', 'NotificationSetting.').join(', ')
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
@Memoize()
|
||||
getCollabAttributes () {
|
||||
return VideoChannelCollaboratorModel.getSQLAttributes('Account->Collabs', 'Account.Collabs.').join(', ')
|
||||
}
|
||||
|
||||
@Memoize()
|
||||
getCollabChannelAttributes () {
|
||||
return VideoChannelModel.getSQLAttributes('Account->Collabs->Channel', 'Account.Collabs.Channel.').join(', ')
|
||||
}
|
||||
|
||||
@Memoize()
|
||||
getCollabChannelActorAttributes () {
|
||||
return ActorModel.getSQLAPIAttributes('Account->Collabs->Channel->Actor', 'Account.Collabs.Channel.Actor.').join(', ')
|
||||
}
|
||||
|
||||
@Memoize()
|
||||
getCollabChannelActorServerAttributes () {
|
||||
return ServerModel.getSQLAttributes(
|
||||
'Account->Collabs->Channel->Actor->Server',
|
||||
'Account.Collabs.Channel.Actor.Server.'
|
||||
).join(', ')
|
||||
}
|
||||
|
||||
@Memoize()
|
||||
getCollabChannelActorAvatarAttributes () {
|
||||
return ActorImageModel.getSQLAttributes(
|
||||
'Account->Collabs->Channel->Actor->Avatars',
|
||||
'Account.Collabs.Channel.Actor.Avatars.'
|
||||
).join(', ')
|
||||
}
|
||||
|
||||
@Memoize()
|
||||
getCollabChannelActorBannerAttributes () {
|
||||
return ActorImageModel.getSQLAttributes(
|
||||
'Account->Collabs->Channel->Actor->Banners',
|
||||
'Account.Collabs.Channel.Actor.Banners.'
|
||||
).join(', ')
|
||||
}
|
||||
}
|
||||
228
server/core/models/user/user-export.ts
Normal file
228
server/core/models/user/user-export.ts
Normal file
@@ -0,0 +1,228 @@
|
||||
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,
|
||||
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
|
||||
declare createdAt: Date
|
||||
|
||||
@UpdatedAt
|
||||
declare updatedAt: Date
|
||||
|
||||
@AllowNull(true)
|
||||
@Column
|
||||
declare filename: string
|
||||
|
||||
@AllowNull(false)
|
||||
@Column
|
||||
declare withVideoFiles: boolean
|
||||
|
||||
@AllowNull(false)
|
||||
@Column
|
||||
declare state: UserExportStateType
|
||||
|
||||
@AllowNull(true)
|
||||
@Column(DataType.TEXT)
|
||||
declare error: string
|
||||
|
||||
@AllowNull(true)
|
||||
@Column(DataType.BIGINT)
|
||||
declare size: number
|
||||
|
||||
@AllowNull(false)
|
||||
@Column
|
||||
declare storage: FileStorageType
|
||||
|
||||
@AllowNull(true)
|
||||
@Column
|
||||
declare fileUrl: string
|
||||
|
||||
@ForeignKey(() => UserModel)
|
||||
@Column
|
||||
declare userId: number
|
||||
|
||||
@BelongsTo(() => UserModel, {
|
||||
foreignKey: {
|
||||
allowNull: false
|
||||
},
|
||||
onDelete: 'CASCADE'
|
||||
})
|
||||
declare 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(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
server/core/models/user/user-import.ts
Normal file
88
server/core/models/user/user-import.ts
Normal file
@@ -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
|
||||
declare createdAt: Date
|
||||
|
||||
@UpdatedAt
|
||||
declare updatedAt: Date
|
||||
|
||||
@AllowNull(true)
|
||||
@Column
|
||||
declare filename: string
|
||||
|
||||
@AllowNull(false)
|
||||
@Column
|
||||
declare state: UserImportStateType
|
||||
|
||||
@AllowNull(true)
|
||||
@Column(DataType.TEXT)
|
||||
declare error: string
|
||||
|
||||
@AllowNull(true)
|
||||
@Column(DataType.JSONB)
|
||||
declare resultSummary: UserImportResultSummary
|
||||
|
||||
@ForeignKey(() => UserModel)
|
||||
@Column
|
||||
declare userId: number
|
||||
|
||||
@BelongsTo(() => UserModel, {
|
||||
foreignKey: {
|
||||
allowNull: false
|
||||
},
|
||||
onDelete: 'CASCADE'
|
||||
})
|
||||
declare 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()
|
||||
}
|
||||
}
|
||||
}
|
||||
261
server/core/models/user/user-notification-setting.ts
Normal file
261
server/core/models/user/user-notification-setting.ts
Normal file
@@ -0,0 +1,261 @@
|
||||
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 { buildSQLAttributes, 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
|
||||
declare newVideoFromSubscription: UserNotificationSettingValueType
|
||||
|
||||
@AllowNull(false)
|
||||
@Default(null)
|
||||
@Is(
|
||||
'UserNotificationSettingNewCommentOnMyVideo',
|
||||
value => throwIfNotValid(value, isUserNotificationSettingValid, 'newCommentOnMyVideo')
|
||||
)
|
||||
@Column
|
||||
declare newCommentOnMyVideo: UserNotificationSettingValueType
|
||||
|
||||
@AllowNull(false)
|
||||
@Default(null)
|
||||
@Is(
|
||||
'UserNotificationSettingAbuseAsModerator',
|
||||
value => throwIfNotValid(value, isUserNotificationSettingValid, 'abuseAsModerator')
|
||||
)
|
||||
@Column
|
||||
declare abuseAsModerator: UserNotificationSettingValueType
|
||||
|
||||
@AllowNull(false)
|
||||
@Default(null)
|
||||
@Is(
|
||||
'UserNotificationSettingVideoAutoBlacklistAsModerator',
|
||||
value => throwIfNotValid(value, isUserNotificationSettingValid, 'videoAutoBlacklistAsModerator')
|
||||
)
|
||||
@Column
|
||||
declare videoAutoBlacklistAsModerator: UserNotificationSettingValueType
|
||||
|
||||
@AllowNull(false)
|
||||
@Default(null)
|
||||
@Is(
|
||||
'UserNotificationSettingBlacklistOnMyVideo',
|
||||
value => throwIfNotValid(value, isUserNotificationSettingValid, 'blacklistOnMyVideo')
|
||||
)
|
||||
@Column
|
||||
declare blacklistOnMyVideo: UserNotificationSettingValueType
|
||||
|
||||
@AllowNull(false)
|
||||
@Default(null)
|
||||
@Is(
|
||||
'UserNotificationSettingMyVideoPublished',
|
||||
value => throwIfNotValid(value, isUserNotificationSettingValid, 'myVideoPublished')
|
||||
)
|
||||
@Column
|
||||
declare myVideoPublished: UserNotificationSettingValueType
|
||||
|
||||
@AllowNull(false)
|
||||
@Default(null)
|
||||
@Is(
|
||||
'UserNotificationSettingMyVideoImportFinished',
|
||||
value => throwIfNotValid(value, isUserNotificationSettingValid, 'myVideoImportFinished')
|
||||
)
|
||||
@Column
|
||||
declare myVideoImportFinished: UserNotificationSettingValueType
|
||||
|
||||
@AllowNull(false)
|
||||
@Default(null)
|
||||
@Is(
|
||||
'UserNotificationSettingNewUserRegistration',
|
||||
value => throwIfNotValid(value, isUserNotificationSettingValid, 'newUserRegistration')
|
||||
)
|
||||
@Column
|
||||
declare newUserRegistration: UserNotificationSettingValueType
|
||||
|
||||
@AllowNull(false)
|
||||
@Default(null)
|
||||
@Is(
|
||||
'UserNotificationSettingNewInstanceFollower',
|
||||
value => throwIfNotValid(value, isUserNotificationSettingValid, 'newInstanceFollower')
|
||||
)
|
||||
@Column
|
||||
declare newInstanceFollower: UserNotificationSettingValueType
|
||||
|
||||
@AllowNull(false)
|
||||
@Default(null)
|
||||
@Is(
|
||||
'UserNotificationSettingNewInstanceFollower',
|
||||
value => throwIfNotValid(value, isUserNotificationSettingValid, 'autoInstanceFollowing')
|
||||
)
|
||||
@Column
|
||||
declare autoInstanceFollowing: UserNotificationSettingValueType
|
||||
|
||||
@AllowNull(false)
|
||||
@Default(null)
|
||||
@Is(
|
||||
'UserNotificationSettingNewFollow',
|
||||
value => throwIfNotValid(value, isUserNotificationSettingValid, 'newFollow')
|
||||
)
|
||||
@Column
|
||||
declare newFollow: UserNotificationSettingValueType
|
||||
|
||||
@AllowNull(false)
|
||||
@Default(null)
|
||||
@Is(
|
||||
'UserNotificationSettingCommentMention',
|
||||
value => throwIfNotValid(value, isUserNotificationSettingValid, 'commentMention')
|
||||
)
|
||||
@Column
|
||||
declare commentMention: UserNotificationSettingValueType
|
||||
|
||||
@AllowNull(false)
|
||||
@Default(null)
|
||||
@Is(
|
||||
'UserNotificationSettingAbuseStateChange',
|
||||
value => throwIfNotValid(value, isUserNotificationSettingValid, 'abuseStateChange')
|
||||
)
|
||||
@Column
|
||||
declare abuseStateChange: UserNotificationSettingValueType
|
||||
|
||||
@AllowNull(false)
|
||||
@Default(null)
|
||||
@Is(
|
||||
'UserNotificationSettingAbuseNewMessage',
|
||||
value => throwIfNotValid(value, isUserNotificationSettingValid, 'abuseNewMessage')
|
||||
)
|
||||
@Column
|
||||
declare abuseNewMessage: UserNotificationSettingValueType
|
||||
|
||||
@AllowNull(false)
|
||||
@Default(null)
|
||||
@Is(
|
||||
'UserNotificationSettingNewPeerTubeVersion',
|
||||
value => throwIfNotValid(value, isUserNotificationSettingValid, 'newPeerTubeVersion')
|
||||
)
|
||||
@Column
|
||||
declare newPeerTubeVersion: UserNotificationSettingValueType
|
||||
|
||||
@AllowNull(false)
|
||||
@Default(null)
|
||||
@Is(
|
||||
'UserNotificationSettingNewPeerPluginVersion',
|
||||
value => throwIfNotValid(value, isUserNotificationSettingValid, 'newPluginVersion')
|
||||
)
|
||||
@Column
|
||||
declare newPluginVersion: UserNotificationSettingValueType
|
||||
|
||||
@AllowNull(false)
|
||||
@Default(null)
|
||||
@Is(
|
||||
'UserNotificationSettingMyVideoStudioEditionFinished',
|
||||
value => throwIfNotValid(value, isUserNotificationSettingValid, 'myVideoStudioEditionFinished')
|
||||
)
|
||||
@Column
|
||||
declare myVideoStudioEditionFinished: UserNotificationSettingValueType
|
||||
|
||||
@AllowNull(false)
|
||||
@Default(null)
|
||||
@Is(
|
||||
'UserNotificationSettingTranscriptionGeneratedForOwner',
|
||||
value => throwIfNotValid(value, isUserNotificationSettingValid, 'myVideoTranscriptionGenerated')
|
||||
)
|
||||
@Column
|
||||
declare myVideoTranscriptionGenerated: UserNotificationSettingValueType
|
||||
|
||||
@ForeignKey(() => UserModel)
|
||||
@Column
|
||||
declare userId: number
|
||||
|
||||
@BelongsTo(() => UserModel, {
|
||||
foreignKey: {
|
||||
allowNull: false
|
||||
},
|
||||
onDelete: 'cascade'
|
||||
})
|
||||
declare User: Awaited<UserModel>
|
||||
|
||||
@CreatedAt
|
||||
declare createdAt: Date
|
||||
|
||||
@UpdatedAt
|
||||
declare updatedAt: Date
|
||||
|
||||
@AfterUpdate
|
||||
@AfterDestroy
|
||||
static removeTokenCache (instance: UserNotificationSettingModel) {
|
||||
return TokensCache.Instance.clearCacheByUserId(instance.userId)
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
static getSQLAttributes (tableName: string, aliasPrefix = '') {
|
||||
return buildSQLAttributes({
|
||||
model: this,
|
||||
tableName,
|
||||
aliasPrefix
|
||||
})
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
626
server/core/models/user/user-notification.ts
Normal file
626
server/core/models/user/user-notification.ts
Normal file
@@ -0,0 +1,626 @@
|
||||
import { forceNumber, maxBy } from '@peertube/peertube-core-utils'
|
||||
import type { UserNotification, UserNotificationData, 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, DataType, 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 { ActorImageModel } from '../actor/actor-image.js'
|
||||
import { ApplicationModel } from '../application/application.js'
|
||||
import { PluginModel } from '../server/plugin.js'
|
||||
import { SequelizeModel, throwIfNotValid } from '../shared/index.js'
|
||||
import { getStateLabel } from '../video/formatter/video-api-format.js'
|
||||
import { VideoBlacklistModel } from '../video/video-blacklist.js'
|
||||
import { VideoCaptionModel } from '../video/video-caption.js'
|
||||
import { VideoChannelCollaboratorModel } from '../video/video-channel-collaborator.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-notification/user-notification-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
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
fields: [ 'channelCollaboratorId' ],
|
||||
where: {
|
||||
channelCollaboratorId: {
|
||||
[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
|
||||
declare type: UserNotificationType_Type
|
||||
|
||||
@AllowNull(false)
|
||||
@Default(false)
|
||||
@Is('UserNotificationRead', value => throwIfNotValid(value, isBooleanValid, 'read'))
|
||||
@Column
|
||||
declare read: boolean
|
||||
|
||||
@AllowNull(true)
|
||||
@Column(DataType.JSONB)
|
||||
declare data: UserNotificationData
|
||||
|
||||
@CreatedAt
|
||||
declare createdAt: Date
|
||||
|
||||
@UpdatedAt
|
||||
declare updatedAt: Date
|
||||
|
||||
@ForeignKey(() => UserModel)
|
||||
@Column
|
||||
declare userId: number
|
||||
|
||||
@BelongsTo(() => UserModel, {
|
||||
foreignKey: {
|
||||
allowNull: false
|
||||
},
|
||||
onDelete: 'cascade'
|
||||
})
|
||||
declare User: Awaited<UserModel>
|
||||
|
||||
@ForeignKey(() => VideoModel)
|
||||
@Column
|
||||
declare videoId: number
|
||||
|
||||
@BelongsTo(() => VideoModel, {
|
||||
foreignKey: {
|
||||
allowNull: true
|
||||
},
|
||||
onDelete: 'cascade'
|
||||
})
|
||||
declare Video: Awaited<VideoModel>
|
||||
|
||||
@ForeignKey(() => VideoCommentModel)
|
||||
@Column
|
||||
declare commentId: number
|
||||
|
||||
@BelongsTo(() => VideoCommentModel, {
|
||||
foreignKey: {
|
||||
allowNull: true
|
||||
},
|
||||
onDelete: 'cascade'
|
||||
})
|
||||
declare VideoComment: Awaited<VideoCommentModel>
|
||||
|
||||
@ForeignKey(() => AbuseModel)
|
||||
@Column
|
||||
declare abuseId: number
|
||||
|
||||
@BelongsTo(() => AbuseModel, {
|
||||
foreignKey: {
|
||||
allowNull: true
|
||||
},
|
||||
onDelete: 'cascade'
|
||||
})
|
||||
declare Abuse: Awaited<AbuseModel>
|
||||
|
||||
@ForeignKey(() => VideoBlacklistModel)
|
||||
@Column
|
||||
declare videoBlacklistId: number
|
||||
|
||||
@BelongsTo(() => VideoBlacklistModel, {
|
||||
foreignKey: {
|
||||
allowNull: true
|
||||
},
|
||||
onDelete: 'cascade'
|
||||
})
|
||||
declare VideoBlacklist: Awaited<VideoBlacklistModel>
|
||||
|
||||
@ForeignKey(() => VideoImportModel)
|
||||
@Column
|
||||
declare videoImportId: number
|
||||
|
||||
@BelongsTo(() => VideoImportModel, {
|
||||
foreignKey: {
|
||||
allowNull: true
|
||||
},
|
||||
onDelete: 'cascade'
|
||||
})
|
||||
declare VideoImport: Awaited<VideoImportModel>
|
||||
|
||||
@ForeignKey(() => AccountModel)
|
||||
@Column
|
||||
declare accountId: number
|
||||
|
||||
@BelongsTo(() => AccountModel, {
|
||||
foreignKey: {
|
||||
allowNull: true
|
||||
},
|
||||
onDelete: 'cascade'
|
||||
})
|
||||
declare Account: Awaited<AccountModel>
|
||||
|
||||
@ForeignKey(() => ActorFollowModel)
|
||||
@Column
|
||||
declare actorFollowId: number
|
||||
|
||||
@BelongsTo(() => ActorFollowModel, {
|
||||
foreignKey: {
|
||||
allowNull: true
|
||||
},
|
||||
onDelete: 'cascade'
|
||||
})
|
||||
declare ActorFollow: Awaited<ActorFollowModel>
|
||||
|
||||
@ForeignKey(() => PluginModel)
|
||||
@Column
|
||||
declare pluginId: number
|
||||
|
||||
@BelongsTo(() => PluginModel, {
|
||||
foreignKey: {
|
||||
allowNull: true
|
||||
},
|
||||
onDelete: 'cascade'
|
||||
})
|
||||
declare Plugin: Awaited<PluginModel>
|
||||
|
||||
@ForeignKey(() => ApplicationModel)
|
||||
@Column
|
||||
declare applicationId: number
|
||||
|
||||
@BelongsTo(() => ApplicationModel, {
|
||||
foreignKey: {
|
||||
allowNull: true
|
||||
},
|
||||
onDelete: 'cascade'
|
||||
})
|
||||
declare Application: Awaited<ApplicationModel>
|
||||
|
||||
@ForeignKey(() => UserRegistrationModel)
|
||||
@Column
|
||||
declare userRegistrationId: number
|
||||
|
||||
@BelongsTo(() => UserRegistrationModel, {
|
||||
foreignKey: {
|
||||
allowNull: true
|
||||
},
|
||||
onDelete: 'cascade'
|
||||
})
|
||||
declare UserRegistration: Awaited<UserRegistrationModel>
|
||||
|
||||
@ForeignKey(() => VideoCaptionModel)
|
||||
@Column
|
||||
declare videoCaptionId: number
|
||||
|
||||
@BelongsTo(() => VideoCaptionModel, {
|
||||
foreignKey: {
|
||||
allowNull: true
|
||||
},
|
||||
onDelete: 'cascade'
|
||||
})
|
||||
declare VideoCaption: Awaited<VideoCaptionModel>
|
||||
|
||||
@ForeignKey(() => VideoChannelCollaboratorModel)
|
||||
@Column
|
||||
declare channelCollaboratorId: number
|
||||
|
||||
@BelongsTo(() => VideoChannelCollaboratorModel, {
|
||||
foreignKey: {
|
||||
allowNull: true
|
||||
},
|
||||
onDelete: 'cascade'
|
||||
})
|
||||
declare VideoChannelCollaborator: Awaited<VideoChannelCollaboratorModel>
|
||||
|
||||
static listForApi (options: {
|
||||
userId: number
|
||||
start: number
|
||||
count: number
|
||||
sort: string
|
||||
unread?: boolean
|
||||
typeOneOf?: UserNotificationType_Type[]
|
||||
}) {
|
||||
const { userId, start, count, sort, unread, typeOneOf } = options
|
||||
|
||||
const countWhere = { userId }
|
||||
|
||||
const query = {
|
||||
start,
|
||||
count,
|
||||
sort,
|
||||
|
||||
userId,
|
||||
unread,
|
||||
typeOneOf
|
||||
}
|
||||
|
||||
if (unread !== undefined) countWhere['read'] = !unread
|
||||
if (typeOneOf !== undefined) countWhere['type'] = { [Op.in]: typeOneOf }
|
||||
|
||||
return Promise.all([
|
||||
UserNotificationModel.count({ where: countWhere })
|
||||
.then(count => count || 0),
|
||||
|
||||
count === 0
|
||||
? [] as UserNotificationModelForApi[]
|
||||
: new UserNotificationListQueryBuilder(this.sequelize, query).list<UserNotificationModelForApi>()
|
||||
]).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 = [
|
||||
// Remove notifications from muted accounts
|
||||
buildAccountWhereQuery(
|
||||
`SELECT "userNotification"."id" FROM "userNotification" ` +
|
||||
`INNER JOIN "account" ON "userNotification"."accountId" = "account"."id" ` +
|
||||
`INNER JOIN actor ON "actor"."accountId" = "account"."id" `
|
||||
),
|
||||
|
||||
// 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."id" = actor."accountId" `
|
||||
),
|
||||
|
||||
// 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."id" = actor."accountId" `
|
||||
),
|
||||
|
||||
// Remove notifications of comments from muted accounts
|
||||
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"."accountId" = "account"."id" `
|
||||
),
|
||||
|
||||
// Remove notifications from muted accounts that invited us to collaborate to a channel
|
||||
buildAccountWhereQuery(
|
||||
`SELECT "userNotification"."id" FROM "userNotification" ` +
|
||||
`INNER JOIN "videoChannelCollaborator" ON "videoChannelCollaborator".id = "userNotification"."channelCollaboratorId" ` +
|
||||
`INNER JOIN "videoChannel" ON "videoChannel".id = "videoChannelCollaborator"."channelId" ` +
|
||||
`INNER JOIN "account" ON "videoChannel"."accountId" = "account"."id" ` +
|
||||
`INNER JOIN actor ON "actor"."accountId" = "account"."id" `
|
||||
)
|
||||
]
|
||||
|
||||
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
|
||||
|
||||
const videoChannelCollaborator = this.VideoChannelCollaborator
|
||||
? {
|
||||
id: this.VideoChannelCollaborator.id,
|
||||
channelOwner: this.formatActor(this.VideoChannelCollaborator.Channel.Account),
|
||||
channel: this.formatActor(this.VideoChannelCollaborator.Channel),
|
||||
account: this.formatActor(this.VideoChannelCollaborator.Account),
|
||||
state: {
|
||||
id: this.VideoChannelCollaborator.state,
|
||||
label: VideoChannelCollaboratorModel.getStateLabel(this.VideoChannelCollaborator.state)
|
||||
}
|
||||
}
|
||||
: undefined
|
||||
|
||||
return {
|
||||
id: this.id,
|
||||
type: this.type,
|
||||
read: this.read,
|
||||
data: this.data,
|
||||
video,
|
||||
videoImport,
|
||||
comment,
|
||||
abuse,
|
||||
videoBlacklist,
|
||||
account,
|
||||
actorFollow,
|
||||
plugin,
|
||||
peertube,
|
||||
registration,
|
||||
videoCaption,
|
||||
videoChannelCollaborator,
|
||||
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,
|
||||
state: {
|
||||
id: video.state,
|
||||
label: getStateLabel(video.state)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
formatAbuse (abuse: UserNotificationIncludes.AbuseInclude) {
|
||||
const commentAbuse = abuse.VideoCommentAbuse?.VideoComment
|
||||
? {
|
||||
threadId: abuse.VideoCommentAbuse.VideoComment.getThreadId(),
|
||||
|
||||
video: abuse.VideoCommentAbuse.VideoComment.Video
|
||||
? this.formatVideo(abuse.VideoCommentAbuse.VideoComment.Video)
|
||||
: 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 {
|
||||
fileUrl: ActorImageModel.getImageUrl(a),
|
||||
path: a.getStaticPath(),
|
||||
width: a.width
|
||||
}
|
||||
}
|
||||
}
|
||||
301
server/core/models/user/user-registration.ts
Normal file
301
server/core/models/user/user-registration.ts
Normal file
@@ -0,0 +1,301 @@
|
||||
import { forceNumber } from '@peertube/peertube-core-utils'
|
||||
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 { col, FindOptions, fn, Op, QueryTypes, where, WhereOptions } from 'sequelize'
|
||||
import {
|
||||
AllowNull,
|
||||
BeforeCreate,
|
||||
BelongsTo,
|
||||
Column,
|
||||
CreatedAt,
|
||||
DataType,
|
||||
ForeignKey,
|
||||
Is,
|
||||
IsEmail,
|
||||
Table,
|
||||
UpdatedAt
|
||||
} from 'sequelize-typescript'
|
||||
import { isUserDisplayNameValid, isUserEmailVerifiedValid } from '../../helpers/custom-validators/users.js'
|
||||
import { getSort, parseAggregateResult, SequelizeModel, throwIfNotValid } from '../shared/index.js'
|
||||
import { UserModel } from './user.js'
|
||||
|
||||
@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
|
||||
declare state: UserRegistrationStateType
|
||||
|
||||
@AllowNull(false)
|
||||
@Is('RegistrationReason', value => throwIfNotValid(value, isRegistrationReasonValid, 'registration reason'))
|
||||
@Column(DataType.TEXT)
|
||||
declare registrationReason: string
|
||||
|
||||
@AllowNull(true)
|
||||
@Is('RegistrationModerationResponse', value => throwIfNotValid(value, isRegistrationModerationResponseValid, 'moderation response', true))
|
||||
@Column(DataType.TEXT)
|
||||
declare moderationResponse: string
|
||||
|
||||
@AllowNull(true)
|
||||
@Column
|
||||
declare password: string
|
||||
|
||||
@AllowNull(false)
|
||||
@Column
|
||||
declare username: string
|
||||
|
||||
@AllowNull(false)
|
||||
@IsEmail
|
||||
@Column(DataType.STRING(400))
|
||||
declare email: string
|
||||
|
||||
@AllowNull(true)
|
||||
@Is('RegistrationEmailVerified', value => throwIfNotValid(value, isUserEmailVerifiedValid, 'email verified boolean', true))
|
||||
@Column
|
||||
declare emailVerified: boolean
|
||||
|
||||
@AllowNull(true)
|
||||
@Is('RegistrationAccountDisplayName', value => throwIfNotValid(value, isUserDisplayNameValid, 'account display name', true))
|
||||
@Column
|
||||
declare accountDisplayName: string
|
||||
|
||||
@AllowNull(true)
|
||||
@Is('ChannelHandle', value => throwIfNotValid(value, isVideoChannelDisplayNameValid, 'channel handle', true))
|
||||
@Column
|
||||
declare channelHandle: string
|
||||
|
||||
@AllowNull(true)
|
||||
@Is('ChannelDisplayName', value => throwIfNotValid(value, isVideoChannelDisplayNameValid, 'channel display name', true))
|
||||
@Column
|
||||
declare channelDisplayName: string
|
||||
|
||||
@AllowNull(true)
|
||||
@Column
|
||||
declare processedAt: Date
|
||||
|
||||
@CreatedAt
|
||||
declare createdAt: Date
|
||||
|
||||
@UpdatedAt
|
||||
declare updatedAt: Date
|
||||
|
||||
@ForeignKey(() => UserModel)
|
||||
@Column
|
||||
declare userId: number
|
||||
|
||||
@BelongsTo(() => UserModel, {
|
||||
foreignKey: {
|
||||
allowNull: true
|
||||
},
|
||||
onDelete: 'SET NULL'
|
||||
})
|
||||
declare 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 listByEmailCaseInsensitive (email: string): Promise<MRegistration[]> {
|
||||
const query = {
|
||||
where: where(
|
||||
fn('LOWER', col('email')),
|
||||
'=',
|
||||
email.toLowerCase()
|
||||
)
|
||||
}
|
||||
|
||||
return UserRegistrationModel.findAll(query)
|
||||
}
|
||||
|
||||
static listByEmailCaseInsensitiveOrUsername (emailOrUsername: string): Promise<MRegistration[]> {
|
||||
const query = {
|
||||
where: {
|
||||
[Op.or]: [
|
||||
where(
|
||||
fn('LOWER', col('email')),
|
||||
'=',
|
||||
emailOrUsername.toLowerCase()
|
||||
),
|
||||
|
||||
{ username: emailOrUsername }
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
return UserRegistrationModel.findAll(query)
|
||||
}
|
||||
|
||||
static listByEmailCaseInsensitiveOrHandle (options: {
|
||||
email: string
|
||||
username: string
|
||||
channelHandle?: string
|
||||
}): Promise<MRegistration[]> {
|
||||
const { email, username, channelHandle } = options
|
||||
|
||||
let or: WhereOptions = [
|
||||
where(
|
||||
fn('LOWER', col('email')),
|
||||
'=',
|
||||
email.toLowerCase()
|
||||
),
|
||||
{ channelHandle: username },
|
||||
{ username }
|
||||
]
|
||||
|
||||
if (channelHandle) {
|
||||
or = or.concat([
|
||||
{ username: channelHandle },
|
||||
{ channelHandle }
|
||||
])
|
||||
}
|
||||
|
||||
const query = {
|
||||
where: {
|
||||
[Op.or]: or
|
||||
}
|
||||
}
|
||||
|
||||
return UserRegistrationModel.findAll(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
server/core/models/user/user-video-history.ts
Normal file
135
server/core/models/user/user-video-history.ts
Normal file
@@ -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
|
||||
declare createdAt: Date
|
||||
|
||||
@UpdatedAt
|
||||
declare updatedAt: Date
|
||||
|
||||
@AllowNull(false)
|
||||
@IsInt
|
||||
@Column
|
||||
declare currentTime: number
|
||||
|
||||
@ForeignKey(() => VideoModel)
|
||||
@Column
|
||||
declare videoId: number
|
||||
|
||||
@BelongsTo(() => VideoModel, {
|
||||
foreignKey: {
|
||||
allowNull: false
|
||||
},
|
||||
onDelete: 'CASCADE'
|
||||
})
|
||||
declare Video: Awaited<VideoModel>
|
||||
|
||||
@ForeignKey(() => UserModel)
|
||||
@Column
|
||||
declare userId: number
|
||||
|
||||
@BelongsTo(() => UserModel, {
|
||||
foreignKey: {
|
||||
allowNull: false
|
||||
},
|
||||
onDelete: 'CASCADE'
|
||||
})
|
||||
declare 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)
|
||||
}
|
||||
}
|
||||
1127
server/core/models/user/user.ts
Normal file
1127
server/core/models/user/user.ts
Normal file
File diff suppressed because it is too large
Load Diff
114
server/core/models/video/event-registration.ts
Normal file
114
server/core/models/video/event-registration.ts
Normal file
@@ -0,0 +1,114 @@
|
||||
import { FindOptions, Transaction } from 'sequelize'
|
||||
import {
|
||||
AllowNull,
|
||||
BelongsTo,
|
||||
Column,
|
||||
CreatedAt,
|
||||
DataType,
|
||||
ForeignKey,
|
||||
Table,
|
||||
UpdatedAt
|
||||
} from 'sequelize-typescript'
|
||||
import { SequelizeModel } from '../shared/index.js'
|
||||
import { EventModel } from './event.js'
|
||||
|
||||
@Table({
|
||||
tableName: 'eventRegistration',
|
||||
indexes: [
|
||||
{
|
||||
fields: [ 'eventId' ]
|
||||
}
|
||||
]
|
||||
})
|
||||
export class EventRegistrationModel extends SequelizeModel<EventRegistrationModel> {
|
||||
@AllowNull(false)
|
||||
@Column(DataType.STRING(255))
|
||||
declare firstName: string
|
||||
|
||||
@AllowNull(false)
|
||||
@Column(DataType.STRING(255))
|
||||
declare lastName: string
|
||||
|
||||
@AllowNull(false)
|
||||
@Column(DataType.STRING(320))
|
||||
declare email: string
|
||||
|
||||
@AllowNull(false)
|
||||
@Column(DataType.STRING(50))
|
||||
declare phone: string
|
||||
|
||||
@AllowNull(false)
|
||||
@Column(DataType.STRING(255))
|
||||
declare companyName: string
|
||||
|
||||
@AllowNull(false)
|
||||
@Column(DataType.STRING(255))
|
||||
declare jobTitle: string
|
||||
|
||||
@AllowNull(false)
|
||||
@Column(DataType.STRING(100))
|
||||
declare country: string
|
||||
|
||||
@CreatedAt
|
||||
declare createdAt: Date
|
||||
|
||||
@UpdatedAt
|
||||
declare updatedAt: Date
|
||||
|
||||
@ForeignKey(() => EventModel)
|
||||
@Column
|
||||
declare eventId: number
|
||||
|
||||
@BelongsTo(() => EventModel, {
|
||||
foreignKey: {
|
||||
allowNull: false
|
||||
},
|
||||
onDelete: 'CASCADE'
|
||||
})
|
||||
declare Event: Awaited<EventModel>
|
||||
|
||||
static listForApi (options: {
|
||||
eventId: number
|
||||
start: number
|
||||
count: number
|
||||
}) {
|
||||
const { eventId, start, count } = options
|
||||
|
||||
const query: FindOptions = {
|
||||
where: {
|
||||
eventId
|
||||
},
|
||||
order: [ [ 'createdAt', 'DESC' ] ],
|
||||
offset: start,
|
||||
limit: count
|
||||
}
|
||||
|
||||
return EventRegistrationModel.findAndCountAll(query)
|
||||
}
|
||||
|
||||
static loadByIdAndEventId (registrationId: number, eventId: number, transaction?: Transaction) {
|
||||
return EventRegistrationModel.findOne({
|
||||
where: {
|
||||
id: registrationId,
|
||||
eventId
|
||||
},
|
||||
transaction
|
||||
})
|
||||
}
|
||||
|
||||
toFormattedJSON (this: EventRegistrationModel) {
|
||||
return {
|
||||
id: this.id,
|
||||
firstName: this.firstName,
|
||||
lastName: this.lastName,
|
||||
email: this.email,
|
||||
phone: this.phone,
|
||||
companyName: this.companyName,
|
||||
jobTitle: this.jobTitle,
|
||||
country: this.country,
|
||||
eventId: this.eventId,
|
||||
createdAt: this.createdAt.toISOString(),
|
||||
updatedAt: this.updatedAt.toISOString()
|
||||
}
|
||||
}
|
||||
}
|
||||
182
server/core/models/video/event.ts
Normal file
182
server/core/models/video/event.ts
Normal file
@@ -0,0 +1,182 @@
|
||||
import { Transaction } from 'sequelize'
|
||||
import { AllowNull, BelongsTo, Column, CreatedAt, DataType, Default, ForeignKey, Table, UpdatedAt } from 'sequelize-typescript'
|
||||
import { MEvent } from '@server/types/models/index.js'
|
||||
import { VideoChannelModel } from './video-channel.js'
|
||||
import { SequelizeModel } from '../shared/index.js'
|
||||
|
||||
export enum EventState {
|
||||
ACTIVE = 'active',
|
||||
DISABLED = 'disabled'
|
||||
}
|
||||
|
||||
@Table({
|
||||
tableName: 'event',
|
||||
indexes: [
|
||||
{
|
||||
fields: [ 'channelId' ]
|
||||
},
|
||||
{
|
||||
fields: [ 'state' ]
|
||||
},
|
||||
{
|
||||
fields: [ 'startTime' ]
|
||||
}
|
||||
]
|
||||
})
|
||||
export class EventModel extends SequelizeModel<EventModel> {
|
||||
@AllowNull(false)
|
||||
@Column
|
||||
declare title: string
|
||||
|
||||
@AllowNull(true)
|
||||
@Column(DataType.TEXT)
|
||||
declare description: string | null
|
||||
|
||||
@AllowNull(false)
|
||||
@Column
|
||||
declare startTime: Date
|
||||
|
||||
@AllowNull(false)
|
||||
@Column
|
||||
declare endTime: Date
|
||||
|
||||
@AllowNull(true)
|
||||
@Column
|
||||
declare thumbnailFilename: string | null
|
||||
|
||||
@AllowNull(true)
|
||||
@Column(DataType.BLOB)
|
||||
declare thumbnailData: Buffer | null
|
||||
|
||||
@AllowNull(true)
|
||||
@Column
|
||||
declare thumbnailMimeType: string | null
|
||||
|
||||
@AllowNull(true)
|
||||
@Column(DataType.BIGINT)
|
||||
declare thumbnailSize: number | null
|
||||
|
||||
@AllowNull(true)
|
||||
@Column
|
||||
declare thumbnailEtag: string | null
|
||||
|
||||
@AllowNull(true)
|
||||
@Column(DataType.TEXT)
|
||||
declare liveUrl: string | null
|
||||
|
||||
@AllowNull(false)
|
||||
@Default(EventState.ACTIVE)
|
||||
@Column
|
||||
declare state: EventState
|
||||
|
||||
@CreatedAt
|
||||
declare createdAt: Date
|
||||
|
||||
@UpdatedAt
|
||||
declare updatedAt: Date
|
||||
|
||||
@ForeignKey(() => VideoChannelModel)
|
||||
@Column
|
||||
declare channelId: number
|
||||
|
||||
@BelongsTo(() => VideoChannelModel, {
|
||||
foreignKey: {
|
||||
allowNull: false
|
||||
},
|
||||
onDelete: 'cascade'
|
||||
})
|
||||
declare Channel: Awaited<VideoChannelModel>
|
||||
|
||||
static listByChannelId (channelId: number, transaction?: Transaction) {
|
||||
const query = {
|
||||
where: {
|
||||
channelId
|
||||
},
|
||||
order: [ [ 'startTime', 'DESC' ] ] as any,
|
||||
transaction
|
||||
}
|
||||
|
||||
return EventModel.findAll<MEvent>(query)
|
||||
}
|
||||
|
||||
static findByIdAndChannelId (eventId: number, channelId: number, transaction?: Transaction) {
|
||||
const query = {
|
||||
where: {
|
||||
id: eventId,
|
||||
channelId
|
||||
},
|
||||
transaction
|
||||
}
|
||||
|
||||
return EventModel.findOne<MEvent>(query)
|
||||
}
|
||||
|
||||
static loadByThumbnailFilename (thumbnailFilename: string, transaction?: Transaction) {
|
||||
return EventModel.findOne<MEvent>({
|
||||
where: {
|
||||
thumbnailFilename
|
||||
},
|
||||
transaction
|
||||
})
|
||||
}
|
||||
|
||||
static listPublicEvents (limit?: number, states?: string[]) {
|
||||
const query: any = {
|
||||
order: [ [ 'startTime', 'DESC' ] ] as any,
|
||||
limit
|
||||
}
|
||||
|
||||
// Only filter by state if explicitly provided; otherwise return all events
|
||||
if (states && states.length > 0) {
|
||||
query.where = { state: states }
|
||||
}
|
||||
|
||||
return EventModel.findAll<MEvent>(query)
|
||||
}
|
||||
|
||||
static async deleteByIdAndChannelId (eventId: number, channelId: number, transaction?: Transaction) {
|
||||
const event = await EventModel.findByIdAndChannelId(eventId, channelId, transaction)
|
||||
if (!event) return false
|
||||
|
||||
await event.destroy({ transaction })
|
||||
return true
|
||||
}
|
||||
|
||||
static async updateEventState (eventId: number, newState: EventState, transaction?: Transaction) {
|
||||
const query = {
|
||||
where: {
|
||||
id: eventId
|
||||
},
|
||||
transaction
|
||||
}
|
||||
|
||||
return EventModel.update({ state: newState }, query)
|
||||
}
|
||||
|
||||
toFormattedJSON (this: EventModel) {
|
||||
return {
|
||||
id: this.id,
|
||||
title: this.title,
|
||||
description: this.description,
|
||||
startTime: this.startTime.toISOString(),
|
||||
endTime: this.endTime.toISOString(),
|
||||
thumbnailFilename: this.thumbnailFilename,
|
||||
liveUrl: this.liveUrl,
|
||||
state: this.state,
|
||||
channelId: this.channelId,
|
||||
createdAt: this.createdAt.toISOString(),
|
||||
updatedAt: this.updatedAt.toISOString()
|
||||
}
|
||||
}
|
||||
|
||||
toLogKeys (this: EventModel) {
|
||||
return {
|
||||
id: this.id,
|
||||
title: this.title,
|
||||
state: this.state,
|
||||
startTime: this.startTime,
|
||||
endTime: this.endTime,
|
||||
channelId: this.channelId
|
||||
}
|
||||
}
|
||||
}
|
||||
2
server/core/models/video/formatter/index.ts
Normal file
2
server/core/models/video/formatter/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export * from './video-activity-pub-format.js'
|
||||
export * from './video-api-format.js'
|
||||
1
server/core/models/video/formatter/shared/index.ts
Normal file
1
server/core/models/video/formatter/shared/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from './video-format-utils.js'
|
||||
@@ -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
|
||||
}
|
||||
322
server/core/models/video/formatter/video-activity-pub-format.ts
Normal file
322
server/core/models/video/formatter/video-activity-pub-format.ts
Normal file
@@ -0,0 +1,322 @@
|
||||
import {
|
||||
ActivityHashTagObject,
|
||||
ActivityIconObject,
|
||||
ActivityPlaylistUrlObject,
|
||||
ActivityPubStoryboard,
|
||||
ActivitySensitiveTagObject,
|
||||
ActivityTagObject,
|
||||
ActivityTrackerUrlObject,
|
||||
ActivityUrlObject,
|
||||
nsfwFlagsToString,
|
||||
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/lib/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,
|
||||
getLocalVideoPlayerSettingsActivityPubUrl,
|
||||
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 + video.getWatchStaticPath()
|
||||
} as ActivityUrlObject,
|
||||
|
||||
{
|
||||
type: 'Link',
|
||||
mediaType: 'text/html',
|
||||
href: video.url
|
||||
} 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,
|
||||
summary: video.nsfwSummary,
|
||||
|
||||
waitTranscoding: video.waitTranscoding,
|
||||
|
||||
state: video.state,
|
||||
|
||||
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,
|
||||
|
||||
schedules: (video.VideoLive?.LiveSchedules || []).map(s => ({
|
||||
startDate: s.startAt
|
||||
})),
|
||||
|
||||
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),
|
||||
playerSettings: getLocalVideoPlayerSettingsActivityPubUrl(video),
|
||||
|
||||
attributedTo: [
|
||||
video.VideoChannel.Account.Actor.url,
|
||||
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): (ActivitySensitiveTagObject | ActivityHashTagObject)[] {
|
||||
const tags = isArray(video.Tags)
|
||||
? video.Tags
|
||||
: []
|
||||
|
||||
return [
|
||||
...tags.map(t =>
|
||||
({
|
||||
type: 'Hashtag' as 'Hashtag',
|
||||
name: t.name
|
||||
}) as ActivityHashTagObject
|
||||
),
|
||||
|
||||
...nsfwFlagsToString(video.nsfwFlags).map(f =>
|
||||
({
|
||||
type: 'SensitiveTag' as 'SensitiveTag',
|
||||
name: f
|
||||
}) as ActivitySensitiveTagObject
|
||||
)
|
||||
]
|
||||
}
|
||||
|
||||
function buildIcon (video: MVideoAP): ActivityIconObject[] {
|
||||
return [ video.getMiniature(), video.getPreview() ]
|
||||
.filter(i => !!i)
|
||||
.map(i => i.toActivityPubObject(video))
|
||||
}
|
||||
|
||||
function buildSubtitleLanguage (video: MVideoAP) {
|
||||
if (!isArray(video.VideoCaptions)) return []
|
||||
|
||||
return video.VideoCaptions
|
||||
.map(caption => caption.toActivityPubObject(video))
|
||||
}
|
||||
374
server/core/models/video/formatter/video-api-format.ts
Normal file
374
server/core/models/video/formatter/video-api-format.ts
Normal file
@@ -0,0 +1,374 @@
|
||||
import { getResolutionLabel } from '@peertube/peertube-core-utils'
|
||||
import {
|
||||
Video,
|
||||
VideoAdditionalAttributes,
|
||||
VideoDetails,
|
||||
VideoFile,
|
||||
VideoInclude,
|
||||
VideosCommonQueryAfterSanitize,
|
||||
VideoStreamingPlaylist
|
||||
} from '@peertube/peertube-models'
|
||||
import { uuidToShort } from '@peertube/peertube-node-utils'
|
||||
import { generateMagnetUri } from '@server/lib/webtorrent.js'
|
||||
import { tracer } from '@server/lib/opentelemetry/tracing.js'
|
||||
import { getHLSResolutionPlaylistFilename } from '@server/lib/paths.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 { MVideoFile } 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
|
||||
liveSchedules?: boolean
|
||||
}
|
||||
}
|
||||
|
||||
export function guessAdditionalAttributesFromQuery (
|
||||
query: Pick<VideosCommonQueryAfterSanitize, 'include' | 'includeScheduledLive'>
|
||||
): VideoFormattingJSONOptions {
|
||||
return {
|
||||
additionalAttributes: {
|
||||
state: query.includeScheduledLive || !!(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),
|
||||
liveSchedules: query.includeScheduledLive
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
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,
|
||||
nsfwFlags: video.nsfwFlags,
|
||||
nsfwSummary: video.nsfwSummary,
|
||||
|
||||
truncatedDescription: video.getTruncatedDescription(),
|
||||
description: options?.completeDescription === true
|
||||
? video.description
|
||||
: video.getTruncatedDescription(),
|
||||
|
||||
isLocal: video.isLocal(),
|
||||
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,
|
||||
|
||||
comments: video.comments,
|
||||
|
||||
// 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: {
|
||||
liveSchedules: true,
|
||||
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,
|
||||
channel: video.VideoChannel.toFormattedJSON(),
|
||||
account: video.VideoChannel.Account.toFormattedJSON(),
|
||||
tags,
|
||||
|
||||
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, { includePlaylistUrl: true })
|
||||
}))
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export function videoFilesModelToFormattedJSON (
|
||||
video: MVideoFormattable,
|
||||
videoFiles: MVideoFile[],
|
||||
options?: {
|
||||
includePlaylistUrl?: true
|
||||
includeMagnet?: boolean
|
||||
}
|
||||
): (VideoFile & { playlistUrl: string })[]
|
||||
|
||||
export function videoFilesModelToFormattedJSON (
|
||||
video: MVideoFormattable,
|
||||
videoFiles: MVideoFile[],
|
||||
options: {
|
||||
includePlaylistUrl?: boolean // default false
|
||||
includeMagnet?: boolean // default true
|
||||
} = {}
|
||||
): VideoFile[] {
|
||||
const { includePlaylistUrl = false, includeMagnet = true } = options
|
||||
|
||||
if (isArray(videoFiles) === false) return []
|
||||
|
||||
const trackerUrls = includeMagnet
|
||||
? video.getTrackerUrls()
|
||||
: []
|
||||
|
||||
return videoFiles
|
||||
.filter(f => !f.isLive())
|
||||
.sort(sortByResolutionDesc)
|
||||
.map(videoFile => {
|
||||
const fileUrl = videoFile.getFileUrl(video)
|
||||
|
||||
return {
|
||||
id: videoFile.id,
|
||||
|
||||
resolution: {
|
||||
id: videoFile.resolution,
|
||||
|
||||
label: getResolutionLabel({
|
||||
resolution: videoFile.resolution,
|
||||
height: videoFile.height,
|
||||
width: videoFile.width
|
||||
})
|
||||
},
|
||||
|
||||
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,
|
||||
fileDownloadUrl: videoFile.getFileDownloadUrl(video),
|
||||
|
||||
metadataUrl: videoFile.metadataUrl ?? getLocalVideoFileMetadataUrl(video, videoFile),
|
||||
|
||||
hasAudio: videoFile.hasAudio(),
|
||||
hasVideo: videoFile.hasVideo(),
|
||||
|
||||
playlistUrl: includePlaylistUrl === true
|
||||
? getHLSResolutionPlaylistFilename(fileUrl)
|
||||
: undefined,
|
||||
|
||||
storage: video.remote
|
||||
? null
|
||||
: videoFile.storage
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
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'
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// 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)
|
||||
}
|
||||
|
||||
if (add?.liveSchedules === true) {
|
||||
result.liveSchedules = (video.VideoLive?.LiveSchedules || []).map(s => s.toFormattedJSON())
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
214
server/core/models/video/live-video.ts
Normal file
214
server/core/models/video/live-video.ts
Normal file
@@ -0,0 +1,214 @@
|
||||
import { FindAndCountOptions, Op, Transaction } from 'sequelize'
|
||||
import {
|
||||
AllowNull,
|
||||
BelongsTo,
|
||||
Column,
|
||||
CreatedAt,
|
||||
DataType,
|
||||
Default,
|
||||
ForeignKey,
|
||||
Table,
|
||||
UpdatedAt
|
||||
} from 'sequelize-typescript'
|
||||
import { MLiveVideo } from '@server/types/models/index.js'
|
||||
import { UserModel } from '../user/user.js'
|
||||
import { SequelizeModel } from '../shared/index.js'
|
||||
|
||||
export enum LiveVideoStatus {
|
||||
SCHEDULED = 'scheduled',
|
||||
CANCELLED = 'cancelled',
|
||||
COMPLETED = 'completed'
|
||||
}
|
||||
|
||||
@Table({
|
||||
tableName: 'liveVideo',
|
||||
indexes: [
|
||||
{
|
||||
fields: [ 'targetCamera' ]
|
||||
},
|
||||
{
|
||||
fields: [ 'startTime' ]
|
||||
},
|
||||
{
|
||||
fields: [ 'status' ]
|
||||
}
|
||||
]
|
||||
})
|
||||
export class LiveVideoModel extends SequelizeModel<LiveVideoModel> {
|
||||
@AllowNull(false)
|
||||
@Column
|
||||
declare title: string
|
||||
|
||||
@AllowNull(true)
|
||||
@Column
|
||||
declare thumbnailFilename: string | null
|
||||
|
||||
@AllowNull(true)
|
||||
@Column(DataType.BLOB)
|
||||
declare thumbnailData: Buffer | null
|
||||
|
||||
@AllowNull(true)
|
||||
@Column
|
||||
declare thumbnailMimeType: string | null
|
||||
|
||||
@AllowNull(true)
|
||||
@Column(DataType.BIGINT)
|
||||
declare thumbnailSize: number | null
|
||||
|
||||
@AllowNull(true)
|
||||
@Column
|
||||
declare thumbnailEtag: string | null
|
||||
|
||||
@AllowNull(false)
|
||||
@Column
|
||||
declare videoOriginalName: string
|
||||
|
||||
@AllowNull(false)
|
||||
@Column(DataType.TEXT)
|
||||
declare videoStoragePath: string
|
||||
|
||||
@AllowNull(false)
|
||||
@Column
|
||||
declare videoMimeType: string
|
||||
|
||||
@AllowNull(false)
|
||||
@Column(DataType.BIGINT)
|
||||
declare videoSize: number
|
||||
|
||||
@AllowNull(false)
|
||||
@Column
|
||||
declare startTime: Date
|
||||
|
||||
@AllowNull(false)
|
||||
@Column
|
||||
declare endTime: Date
|
||||
|
||||
@AllowNull(false)
|
||||
@Column
|
||||
declare targetCamera: string
|
||||
|
||||
@AllowNull(false)
|
||||
@Default(LiveVideoStatus.SCHEDULED)
|
||||
@Column
|
||||
declare status: LiveVideoStatus
|
||||
|
||||
@ForeignKey(() => UserModel)
|
||||
@Column
|
||||
declare createdByUserId: number
|
||||
|
||||
@BelongsTo(() => UserModel, {
|
||||
foreignKey: {
|
||||
allowNull: false
|
||||
},
|
||||
onDelete: 'restrict'
|
||||
})
|
||||
declare CreatedBy: Awaited<UserModel>
|
||||
|
||||
@CreatedAt
|
||||
declare createdAt: Date
|
||||
|
||||
@UpdatedAt
|
||||
declare updatedAt: Date
|
||||
|
||||
static listForApi (options: {
|
||||
start?: number
|
||||
count?: number
|
||||
targetCamera?: string
|
||||
status?: string
|
||||
transaction?: Transaction
|
||||
}) {
|
||||
const query: FindAndCountOptions = {
|
||||
offset: options.start,
|
||||
limit: options.count,
|
||||
order: [ [ 'startTime', 'DESC' ] ],
|
||||
transaction: options.transaction
|
||||
}
|
||||
|
||||
const where: Record<string, any> = {}
|
||||
|
||||
if (options.targetCamera) where.targetCamera = options.targetCamera
|
||||
if (options.status) where.status = options.status
|
||||
|
||||
if (Object.keys(where).length !== 0) query.where = where
|
||||
|
||||
return LiveVideoModel.findAndCountAll<MLiveVideo>(query)
|
||||
}
|
||||
|
||||
static loadById (id: number, transaction?: Transaction) {
|
||||
return LiveVideoModel.findByPk<MLiveVideo>(id, { transaction })
|
||||
}
|
||||
|
||||
static loadByThumbnailFilename (thumbnailFilename: string, transaction?: Transaction) {
|
||||
return LiveVideoModel.findOne<MLiveVideo>({
|
||||
where: {
|
||||
thumbnailFilename
|
||||
},
|
||||
transaction
|
||||
})
|
||||
}
|
||||
|
||||
static listConflicts (options: {
|
||||
targetCamera: string
|
||||
startTime: Date
|
||||
endTime: Date
|
||||
excludeId?: number
|
||||
transaction?: Transaction
|
||||
}) {
|
||||
const where: Record<string, any> = {
|
||||
targetCamera: options.targetCamera,
|
||||
status: {
|
||||
[Op.not]: LiveVideoStatus.CANCELLED
|
||||
}
|
||||
}
|
||||
|
||||
if (options.excludeId) {
|
||||
where.id = {
|
||||
[Op.ne]: options.excludeId
|
||||
}
|
||||
}
|
||||
|
||||
return LiveVideoModel.findAll<MLiveVideo>({
|
||||
where,
|
||||
transaction: options.transaction
|
||||
})
|
||||
}
|
||||
|
||||
static listForStarting (options: {
|
||||
startBefore: Date
|
||||
endAfter: Date
|
||||
transaction?: Transaction
|
||||
}) {
|
||||
return LiveVideoModel.findAll<MLiveVideo>({
|
||||
where: {
|
||||
status: LiveVideoStatus.SCHEDULED,
|
||||
startTime: {
|
||||
[Op.lte]: options.startBefore
|
||||
},
|
||||
endTime: {
|
||||
[Op.gt]: options.endAfter
|
||||
}
|
||||
},
|
||||
order: [ [ 'startTime', 'ASC' ] ],
|
||||
transaction: options.transaction
|
||||
})
|
||||
}
|
||||
|
||||
toFormattedJSON (this: LiveVideoModel) {
|
||||
return {
|
||||
id: this.id,
|
||||
title: this.title,
|
||||
thumbnailFilename: this.thumbnailFilename,
|
||||
videoOriginalName: this.videoOriginalName,
|
||||
videoStoragePath: this.videoStoragePath,
|
||||
videoMimeType: this.videoMimeType,
|
||||
videoSize: this.videoSize,
|
||||
startTime: this.startTime.toISOString(),
|
||||
endTime: this.endTime.toISOString(),
|
||||
targetCamera: this.targetCamera,
|
||||
status: this.status,
|
||||
createdByUserId: this.createdByUserId,
|
||||
createdAt: this.createdAt.toISOString(),
|
||||
updatedAt: this.updatedAt.toISOString()
|
||||
}
|
||||
}
|
||||
}
|
||||
184
server/core/models/video/player-setting.ts
Normal file
184
server/core/models/video/player-setting.ts
Normal file
@@ -0,0 +1,184 @@
|
||||
import type {
|
||||
PlayerChannelSettings,
|
||||
PlayerSettingsObject,
|
||||
PlayerThemeChannelSetting,
|
||||
PlayerThemeVideoSetting,
|
||||
PlayerVideoSettings
|
||||
} from '@peertube/peertube-models'
|
||||
import { CONFIG } from '@server/initializers/config.js'
|
||||
import { DEFAULT_CHANNEL_PLAYER_SETTING_VALUE, DEFAULT_INSTANCE_PLAYER_SETTING_VALUE } from '@server/initializers/constants.js'
|
||||
import { getLocalChannelPlayerSettingsActivityPubUrl, getLocalVideoPlayerSettingsActivityPubUrl } from '@server/lib/activitypub/url.js'
|
||||
import { MChannel, MChannelActorLight, MVideoUrl } from '@server/types/models/index.js'
|
||||
import { MPlayerSetting } from '@server/types/models/video/player-setting.js'
|
||||
import { Op, Transaction } from 'sequelize'
|
||||
import { AllowNull, BelongsTo, Column, CreatedAt, Default, ForeignKey, Table, UpdatedAt } from 'sequelize-typescript'
|
||||
import { SequelizeModel } from '../shared/index.js'
|
||||
import { VideoChannelModel } from './video-channel.js'
|
||||
import { VideoModel } from './video.js'
|
||||
|
||||
@Table({
|
||||
tableName: 'playerSetting',
|
||||
indexes: [
|
||||
{
|
||||
fields: [ 'videoId' ],
|
||||
unique: true
|
||||
},
|
||||
{
|
||||
fields: [ 'channelId' ],
|
||||
unique: true
|
||||
}
|
||||
]
|
||||
})
|
||||
export class PlayerSettingModel extends SequelizeModel<PlayerSettingModel> {
|
||||
@CreatedAt
|
||||
declare createdAt: Date
|
||||
|
||||
@UpdatedAt
|
||||
declare updatedAt: Date
|
||||
|
||||
@AllowNull(false)
|
||||
@Default(DEFAULT_INSTANCE_PLAYER_SETTING_VALUE)
|
||||
@Column
|
||||
declare theme: PlayerThemeVideoSetting | PlayerThemeChannelSetting
|
||||
|
||||
@ForeignKey(() => VideoModel)
|
||||
@Column
|
||||
declare videoId: number
|
||||
|
||||
@BelongsTo(() => VideoModel, {
|
||||
foreignKey: {
|
||||
allowNull: true
|
||||
},
|
||||
onDelete: 'cascade'
|
||||
})
|
||||
declare Video: Awaited<VideoModel>
|
||||
|
||||
@ForeignKey(() => VideoChannelModel)
|
||||
@Column
|
||||
declare channelId: number
|
||||
|
||||
@BelongsTo(() => VideoChannelModel, {
|
||||
foreignKey: {
|
||||
allowNull: true
|
||||
},
|
||||
onDelete: 'cascade'
|
||||
})
|
||||
declare VideoChannel: Awaited<VideoChannelModel>
|
||||
|
||||
static loadByVideoId (videoId: number, transaction?: Transaction) {
|
||||
return PlayerSettingModel.findOne<MPlayerSetting>({
|
||||
where: { videoId },
|
||||
transaction
|
||||
})
|
||||
}
|
||||
|
||||
static loadByChannelId (channelId: number, transaction?: Transaction) {
|
||||
return PlayerSettingModel.findOne<MPlayerSetting>({
|
||||
where: { channelId },
|
||||
transaction
|
||||
})
|
||||
}
|
||||
|
||||
static async loadByVideoIdOrChannelId (options: {
|
||||
videoId: number
|
||||
channelId: number
|
||||
transaction?: Transaction
|
||||
}) {
|
||||
const { videoId, channelId, transaction } = options
|
||||
|
||||
const results = await PlayerSettingModel.findAll<MPlayerSetting>({
|
||||
where: {
|
||||
[Op.or]: [
|
||||
{ videoId },
|
||||
{ channelId }
|
||||
]
|
||||
},
|
||||
transaction
|
||||
})
|
||||
|
||||
const videoSetting = results.find(s => s.videoId === videoId)
|
||||
const channelSetting = results.find(s => s.channelId === channelId)
|
||||
|
||||
return { videoSetting, channelSetting }
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
static formatVideoPlayerSetting (options: {
|
||||
videoSetting: MPlayerSetting
|
||||
channelSetting: MPlayerSetting
|
||||
}): PlayerVideoSettings {
|
||||
const { videoSetting, channelSetting } = options
|
||||
|
||||
const channelFormattedSetting = this.formatChannelPlayerSetting({ channelSetting })
|
||||
if (!videoSetting) return channelFormattedSetting
|
||||
|
||||
let theme: PlayerThemeVideoSetting
|
||||
if (videoSetting.theme === DEFAULT_CHANNEL_PLAYER_SETTING_VALUE) {
|
||||
theme = channelFormattedSetting.theme
|
||||
} else if (videoSetting.theme === DEFAULT_INSTANCE_PLAYER_SETTING_VALUE) {
|
||||
theme = CONFIG.DEFAULTS.PLAYER.THEME
|
||||
} else {
|
||||
theme = videoSetting.theme
|
||||
}
|
||||
|
||||
return {
|
||||
theme
|
||||
}
|
||||
}
|
||||
|
||||
static formatVideoPlayerRawSetting (videoSetting: MPlayerSetting) {
|
||||
return {
|
||||
theme: videoSetting?.theme ?? DEFAULT_CHANNEL_PLAYER_SETTING_VALUE
|
||||
}
|
||||
}
|
||||
|
||||
static formatChannelPlayerSetting (options: {
|
||||
channelSetting: MPlayerSetting
|
||||
}): PlayerChannelSettings {
|
||||
const { channelSetting } = options
|
||||
|
||||
const instanceSetting = {
|
||||
theme: CONFIG.DEFAULTS.PLAYER.THEME
|
||||
}
|
||||
|
||||
if (!channelSetting) return instanceSetting
|
||||
|
||||
return {
|
||||
theme: channelSetting.theme === DEFAULT_INSTANCE_PLAYER_SETTING_VALUE
|
||||
? instanceSetting.theme
|
||||
: channelSetting.theme as PlayerThemeChannelSetting
|
||||
}
|
||||
}
|
||||
|
||||
static formatChannelPlayerRawSetting (channelSetting: MPlayerSetting) {
|
||||
return {
|
||||
theme: channelSetting?.theme ?? DEFAULT_INSTANCE_PLAYER_SETTING_VALUE
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
static formatAPPlayerSetting (options: {
|
||||
settings: MPlayerSetting
|
||||
channel: MChannel & MChannelActorLight
|
||||
video: MVideoUrl
|
||||
}): PlayerSettingsObject {
|
||||
const { channel, settings, video } = options
|
||||
|
||||
const json = video
|
||||
? this.formatVideoPlayerRawSetting(settings)
|
||||
: this.formatChannelPlayerRawSetting(settings)
|
||||
|
||||
return {
|
||||
id: video
|
||||
? getLocalVideoPlayerSettingsActivityPubUrl(video)
|
||||
: getLocalChannelPlayerSettingsActivityPubUrl(channel.Actor.preferredUsername),
|
||||
|
||||
object: video?.url || channel?.Actor.url,
|
||||
type: 'PlayerSettings',
|
||||
|
||||
theme: json.theme
|
||||
}
|
||||
}
|
||||
}
|
||||
94
server/core/models/video/schedule-video-update.ts
Normal file
94
server/core/models/video/schedule-video-update.ts
Normal file
@@ -0,0 +1,94 @@
|
||||
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
|
||||
declare updateAt: Date
|
||||
|
||||
@AllowNull(true)
|
||||
@Default(null)
|
||||
@Column(DataType.INTEGER)
|
||||
declare privacy: typeof VideoPrivacy.PUBLIC | typeof VideoPrivacy.UNLISTED | typeof VideoPrivacy.INTERNAL
|
||||
|
||||
@CreatedAt
|
||||
declare createdAt: Date
|
||||
|
||||
@UpdatedAt
|
||||
declare updatedAt: Date
|
||||
|
||||
@ForeignKey(() => VideoModel)
|
||||
@Column
|
||||
declare videoId: number
|
||||
|
||||
@BelongsTo(() => VideoModel, {
|
||||
foreignKey: {
|
||||
allowNull: false
|
||||
},
|
||||
onDelete: 'cascade'
|
||||
})
|
||||
declare 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
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,278 @@
|
||||
import { ActorImageType, VideoChannelCollaboratorState } from '@peertube/peertube-models'
|
||||
import { WEBSERVER } from '@server/initializers/constants.js'
|
||||
import { AbstractListQuery, AbstractListQueryOptions } from '@server/models/shared/abstract-list-query.js'
|
||||
import { buildServerIdsFollowedBy } from '@server/models/shared/index.js'
|
||||
import { Sequelize } from 'sequelize'
|
||||
import { VideoChannelTableAttributes } from './video-channel-table-attributes.js'
|
||||
|
||||
export interface ListVideoChannelsOptions extends AbstractListQueryOptions {
|
||||
actorId?: number
|
||||
search?: string
|
||||
host?: string
|
||||
handles?: string[]
|
||||
forCount?: boolean
|
||||
|
||||
accountId?: number
|
||||
|
||||
// If accountId is provided, include channels where the account is a collaborator
|
||||
// default: false
|
||||
includeCollaborations?: boolean
|
||||
|
||||
statsDaysPrior?: number
|
||||
}
|
||||
|
||||
export class VideoChannelListQueryBuilder extends AbstractListQuery {
|
||||
private readonly tableAttributes = new VideoChannelTableAttributes()
|
||||
|
||||
private builtActorJoin = false
|
||||
private builtAccountJoin = false
|
||||
private builtChannelCollaboratorsJoin = false
|
||||
private builtAccountAvatarJoin = false
|
||||
private builtChannelAvatarJoin = false
|
||||
private builtChannelBannerJoin = false
|
||||
|
||||
constructor (
|
||||
protected readonly sequelize: Sequelize,
|
||||
protected readonly options: ListVideoChannelsOptions
|
||||
) {
|
||||
super(sequelize, { modelName: 'VideoChannelModel', tableName: 'videoChannel' }, options)
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
protected buildSubQueryWhere () {
|
||||
const where: string[] = []
|
||||
|
||||
if (this.options.host) {
|
||||
this.buildActorJoin()
|
||||
|
||||
if (this.options.host === WEBSERVER.HOST) {
|
||||
where.push('"Actor"."serverId" IS NULL')
|
||||
} else {
|
||||
where.push('"Actor->Server"."host" = :host')
|
||||
|
||||
this.replacements.host = this.options.host
|
||||
}
|
||||
}
|
||||
|
||||
// Only list local channels OR channels that are on an instance followed by actorId
|
||||
if (this.options.actorId) {
|
||||
this.buildActorJoin()
|
||||
|
||||
where.push(
|
||||
`(` +
|
||||
`"Actor"."serverId" IS NULL OR ` +
|
||||
`"Actor"."serverId" IN ${buildServerIdsFollowedBy(this.options.actorId)}` +
|
||||
`)`
|
||||
)
|
||||
}
|
||||
|
||||
if (this.options.accountId) {
|
||||
this.buildAccountJoin()
|
||||
|
||||
if (this.options.includeCollaborations !== true) {
|
||||
where.push('"VideoChannelModel"."accountId" = :accountId')
|
||||
|
||||
this.replacements.accountId = this.options.accountId
|
||||
} else {
|
||||
this.buildChannelCollaboratorsJoin()
|
||||
|
||||
where.push(
|
||||
`("VideoChannelModel"."accountId" = :accountId OR "VideoChannelCollaborators"."accountId" = :accountId)`
|
||||
)
|
||||
|
||||
this.replacements.accountId = this.options.accountId
|
||||
}
|
||||
}
|
||||
|
||||
if (Array.isArray(this.options.handles) && this.options.handles.length !== 0) {
|
||||
this.buildActorJoin()
|
||||
|
||||
const or: string[] = []
|
||||
|
||||
for (const handle of this.options.handles || []) {
|
||||
const [ preferredUsername, host ] = handle.split('@')
|
||||
|
||||
const sanitizedPreferredUsername = this.sequelize.escape(preferredUsername.toLowerCase())
|
||||
const sanitizedHost = this.sequelize.escape(host)
|
||||
|
||||
if (!host || host === WEBSERVER.HOST) {
|
||||
or.push(`(LOWER("Actor"."preferredUsername") = ${sanitizedPreferredUsername} AND "Actor"."serverId" IS NULL)`)
|
||||
} else {
|
||||
or.push(`(LOWER("Actor"."preferredUsername") = ${sanitizedPreferredUsername} AND "Actor->Server"."host" = ${sanitizedHost})`)
|
||||
}
|
||||
}
|
||||
|
||||
where.push(`(${or.join(' OR ')})`)
|
||||
}
|
||||
|
||||
if (this.options.search) {
|
||||
this.buildAccountJoin()
|
||||
|
||||
const escapedSearch = this.sequelize.escape(this.options.search)
|
||||
const escapedLikeSearch = this.sequelize.escape('%' + this.options.search + '%')
|
||||
|
||||
this.subQueryAttributes.push(
|
||||
`word_similarity(lower(immutable_unaccent(${escapedSearch})), lower(immutable_unaccent("VideoChannelModel"."name"))) as similarity`
|
||||
)
|
||||
|
||||
where.push(
|
||||
`(` +
|
||||
`lower(immutable_unaccent(${escapedSearch})) <% lower(immutable_unaccent("VideoChannelModel"."name")) OR ` +
|
||||
`lower(immutable_unaccent("VideoChannelModel"."name")) LIKE lower(immutable_unaccent(${escapedLikeSearch})) OR ` +
|
||||
`lower(immutable_unaccent("Account"."name")) LIKE lower(immutable_unaccent(${escapedLikeSearch}))` +
|
||||
`)`
|
||||
)
|
||||
} else {
|
||||
this.subQueryAttributes.push('0 as similarity')
|
||||
}
|
||||
|
||||
if (where.length !== 0) {
|
||||
this.subQueryWhere = `WHERE ${where.join(' AND ')}`
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
private buildActorJoin () {
|
||||
if (this.builtActorJoin) return
|
||||
|
||||
this.subQueryJoin += ' INNER JOIN "actor" "Actor" ON "Actor"."videoChannelId" = "VideoChannelModel"."id" ' +
|
||||
'LEFT JOIN "server" "Actor->Server" ON "Actor"."serverId" = "Actor->Server"."id" '
|
||||
|
||||
this.builtActorJoin = true
|
||||
}
|
||||
|
||||
private buildAccountJoin () {
|
||||
if (this.builtAccountJoin) return
|
||||
|
||||
this.subQueryJoin += ' INNER JOIN "account" "Account" ON "Account"."id" = "VideoChannelModel"."accountId" ' +
|
||||
'INNER JOIN "actor" "Account->Actor" ON "Account->Actor"."accountId" = "Account"."id" ' +
|
||||
'LEFT JOIN "server" "Account->Actor->Server" ON "Account->Actor"."serverId" = "Account->Actor->Server"."id" '
|
||||
|
||||
this.builtAccountJoin = true
|
||||
}
|
||||
|
||||
private buildChannelCollaboratorsJoin () {
|
||||
if (this.builtChannelCollaboratorsJoin) return
|
||||
|
||||
this.subQueryJoin += ' LEFT JOIN "videoChannelCollaborator" "VideoChannelCollaborators" ' +
|
||||
'ON "VideoChannelCollaborators"."channelId" = "VideoChannelModel"."id" ' +
|
||||
'AND "VideoChannelCollaborators"."state" = :channelCollaboratorState ' +
|
||||
// Ensure we join with max 1 collaborator to not duplicate rows
|
||||
'AND "VideoChannelCollaborators"."accountId" = :accountId '
|
||||
|
||||
this.replacements.channelCollaboratorState = VideoChannelCollaboratorState.ACCEPTED
|
||||
this.replacements.accountId = this.options.accountId
|
||||
|
||||
this.builtChannelCollaboratorsJoin = true
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
private buildAccountAvatarsJoin () {
|
||||
if (this.builtAccountAvatarJoin) return
|
||||
|
||||
this.join += `LEFT JOIN "actorImage" "Account->Actor->Avatars" ` +
|
||||
`ON "Account->Actor->Avatars"."actorId" = "VideoChannelModel"."Account.Actor.id" ` +
|
||||
`AND "Account->Actor->Avatars"."type" = ${ActorImageType.AVATAR} `
|
||||
|
||||
this.builtAccountAvatarJoin = true
|
||||
}
|
||||
|
||||
private buildChannelAvatarsJoin () {
|
||||
if (this.builtChannelAvatarJoin) return
|
||||
|
||||
this.join += `LEFT JOIN "actorImage" "Actor->Avatars" ` +
|
||||
`ON "Actor->Avatars"."actorId" = "VideoChannelModel"."Actor.id" ` +
|
||||
`AND "Actor->Avatars"."type" = ${ActorImageType.AVATAR} `
|
||||
|
||||
this.builtChannelAvatarJoin = true
|
||||
}
|
||||
|
||||
private buildChannelBannersJoin () {
|
||||
if (this.builtChannelBannerJoin) return
|
||||
|
||||
this.join += `LEFT JOIN "actorImage" "Actor->Banners" ` +
|
||||
`ON "Actor->Banners"."actorId" = "VideoChannelModel"."Actor.id" ` +
|
||||
`AND "Actor->Banners"."type" = ${ActorImageType.BANNER} `
|
||||
|
||||
this.builtChannelBannerJoin = true
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
protected buildQueryJoin () {
|
||||
this.buildChannelAvatarsJoin()
|
||||
this.buildAccountAvatarsJoin()
|
||||
this.buildChannelBannersJoin()
|
||||
}
|
||||
|
||||
protected buildQueryAttributes () {
|
||||
this.attributes = [
|
||||
...this.attributes,
|
||||
|
||||
this.tableAttributes.getAccountAvatarAttributes(),
|
||||
this.tableAttributes.getChannelAvatarAttributes(),
|
||||
this.tableAttributes.getChannelBannerAttributes()
|
||||
]
|
||||
|
||||
if (this.options.statsDaysPrior) {
|
||||
this.attributes.push(
|
||||
`(SELECT COUNT(*) FROM "video" WHERE "channelId" = "VideoChannelModel"."id") AS "videosCount"`
|
||||
)
|
||||
|
||||
this.attributes.push(
|
||||
// dprint-ignore
|
||||
'(' +
|
||||
`SELECT string_agg(concat_ws('|', t.day, t.views), ',') ` +
|
||||
'FROM ( ' +
|
||||
'WITH days AS ( ' +
|
||||
`SELECT generate_series(date_trunc('day', now()) - '${this.options.statsDaysPrior} 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' +
|
||||
') AS "viewsPerDay"'
|
||||
)
|
||||
|
||||
this.attributes.push(
|
||||
'(' +
|
||||
'SELECT COALESCE(SUM("video".views), 0) AS totalViews ' +
|
||||
'FROM "video" ' +
|
||||
'WHERE "video"."channelId" = "VideoChannelModel"."id"' +
|
||||
') AS "totalViews"'
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
protected buildSubQueryJoin () {
|
||||
this.buildActorJoin()
|
||||
this.buildAccountJoin()
|
||||
}
|
||||
|
||||
protected buildSubQueryAttributes () {
|
||||
this.subQueryAttributes = [
|
||||
...this.subQueryAttributes,
|
||||
|
||||
this.tableAttributes.getVideoChannelAttributes(),
|
||||
|
||||
this.tableAttributes.getChannelActorAttributes(),
|
||||
this.tableAttributes.getChannelServerAttributes(),
|
||||
|
||||
this.tableAttributes.getAccountAttributes(),
|
||||
this.tableAttributes.getAccountActorAttributes(),
|
||||
this.tableAttributes.getAccountServerAttributes()
|
||||
]
|
||||
}
|
||||
|
||||
protected getCalculatedAttributes () {
|
||||
return [ 'similarity' ]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
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 { VideoChannelModel } from '../../video-channel.js'
|
||||
|
||||
export class VideoChannelTableAttributes {
|
||||
@Memoize()
|
||||
getVideoChannelAttributes () {
|
||||
return VideoChannelModel.getSQLAttributes('VideoChannelModel').join(', ')
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
@Memoize()
|
||||
getChannelActorAttributes () {
|
||||
return ActorModel.getSQLAPIAttributes('Actor', `Actor.`).join(', ')
|
||||
}
|
||||
|
||||
@Memoize()
|
||||
getChannelServerAttributes () {
|
||||
return ServerModel.getSQLSummaryAttributes('Actor->Server', `Actor.Server.`).join(', ')
|
||||
}
|
||||
|
||||
@Memoize()
|
||||
getChannelAvatarAttributes () {
|
||||
return ActorImageModel.getSQLAttributes('Actor->Avatars', 'Actor.Avatars.').join(', ')
|
||||
}
|
||||
|
||||
@Memoize()
|
||||
getChannelBannerAttributes () {
|
||||
return ActorImageModel.getSQLAttributes('Actor->Banners', 'Actor.Banners.').join(', ')
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
@Memoize()
|
||||
getAccountAttributes () {
|
||||
return AccountModel.getSQLSummaryAttributes('Account', 'Account.').join(', ')
|
||||
}
|
||||
|
||||
@Memoize()
|
||||
getAccountActorAttributes () {
|
||||
return ActorModel.getSQLSummaryAttributes('Account->Actor', `Account.Actor.`).join(', ')
|
||||
}
|
||||
|
||||
@Memoize()
|
||||
getAccountServerAttributes () {
|
||||
return ServerModel.getSQLSummaryAttributes('Account->Actor->Server', `Account.Actor.Server.`).join(', ')
|
||||
}
|
||||
|
||||
@Memoize()
|
||||
getAccountAvatarAttributes () {
|
||||
return ActorImageModel.getSQLAttributes('Account->Actor->Avatars', 'Account.Actor.Avatars.').join(', ')
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,510 @@
|
||||
import { VideoChannelCollaboratorState, VideoPrivacy } from '@peertube/peertube-models'
|
||||
import { AbstractListQuery, AbstractListQueryOptions } from '@server/models/shared/abstract-list-query.js'
|
||||
import { getAccountJoin, getActorJoin, getAvatarsJoin, getChannelJoin } from '@server/models/shared/sql/actor-helpers.js'
|
||||
import { Sequelize } from 'sequelize'
|
||||
import { createSafeIn } from '../../../shared/index.js'
|
||||
import { VideoCommentTableAttributes } from './video-comment-table-attributes.js'
|
||||
|
||||
export interface ListVideoCommentsOptions extends AbstractListQueryOptions {
|
||||
selectType: 'api-list' | 'api-video' | 'feed' | 'comment-only'
|
||||
|
||||
autoTagOfAccountId?: number
|
||||
|
||||
videoId?: number
|
||||
threadId?: number
|
||||
accountId?: number
|
||||
|
||||
blockerAccountIds?: number[]
|
||||
|
||||
isThread?: boolean
|
||||
notDeleted?: boolean
|
||||
|
||||
isLocal?: boolean
|
||||
onLocalVideo?: boolean
|
||||
|
||||
onPublicVideo?: boolean
|
||||
videoChannelOwnerId?: number
|
||||
videoAccountOwnerId?: number
|
||||
videoAccountOwnerIncludeCollaborations?: boolean
|
||||
|
||||
heldForReview: boolean
|
||||
heldForReviewAccountIdException?: number
|
||||
|
||||
autoTagOneOf?: string[]
|
||||
|
||||
search?: string
|
||||
searchAccount?: string
|
||||
searchVideo?: string
|
||||
|
||||
includeReplyCounters?: boolean
|
||||
}
|
||||
|
||||
export class VideoCommentListQueryBuilder extends AbstractListQuery {
|
||||
private readonly tableAttributes = new VideoCommentTableAttributes()
|
||||
|
||||
private builtAccountJoin = false
|
||||
private builtAccountActorJoin = false
|
||||
private builtVideoJoin = false
|
||||
private builtVideoChannelJoin = false
|
||||
private builtVideoChannelActorJoin = false
|
||||
private builtVideoChannelCollaboratorsJoin = false
|
||||
private builtAccountAvatarJoin = false
|
||||
private builtChannelAvatarJoin = false
|
||||
private builtAutomaticTagsJoin = false
|
||||
|
||||
constructor (
|
||||
protected readonly sequelize: Sequelize,
|
||||
protected readonly options: ListVideoCommentsOptions
|
||||
) {
|
||||
super(sequelize, { modelName: 'VideoCommentModel', tableName: 'videoComment' }, options)
|
||||
|
||||
if (this.options.includeReplyCounters && !this.options.videoId) {
|
||||
throw new Error('Cannot include reply counters without videoId')
|
||||
}
|
||||
}
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
protected buildSubQueryWhere () {
|
||||
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()
|
||||
this.buildAccountActorJoin()
|
||||
|
||||
where.push('"Account->Actor"."serverId" IS NULL')
|
||||
} else if (this.options.isLocal === false) {
|
||||
this.buildAccountJoin()
|
||||
this.buildAccountActorJoin()
|
||||
|
||||
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
|
||||
|
||||
if (this.options.videoAccountOwnerIncludeCollaborations !== true) {
|
||||
where.push(`"Video->VideoChannel"."accountId" = :videoAccountOwnerId`)
|
||||
} else {
|
||||
this.buildVideoChannelCollaboratorsJoin()
|
||||
|
||||
where.push(
|
||||
`(` +
|
||||
`"Video->VideoChannel"."accountId" = :videoAccountOwnerId OR ` +
|
||||
`"Video->VideoChannel->VideoChannelCollaborators"."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()
|
||||
this.buildAccountActorJoin()
|
||||
|
||||
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()
|
||||
this.buildAccountActorJoin()
|
||||
|
||||
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.subQueryWhere = `WHERE ${where.join(' AND ')}`
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
private buildAccountJoin () {
|
||||
if (this.builtAccountJoin) return
|
||||
|
||||
this.subQueryJoin += getAccountJoin({
|
||||
on: `"VideoCommentModel"."accountId"`,
|
||||
includeAvatars: false,
|
||||
includeActor: false,
|
||||
required: false
|
||||
})
|
||||
|
||||
this.builtAccountJoin = true
|
||||
}
|
||||
|
||||
private buildAccountActorJoin () {
|
||||
if (this.builtAccountActorJoin) return
|
||||
|
||||
this.subQueryJoin += getActorJoin({
|
||||
base: 'Account->',
|
||||
on: `"Account"."id"`,
|
||||
type: 'account',
|
||||
includeAvatars: false,
|
||||
required: false
|
||||
})
|
||||
|
||||
this.builtAccountActorJoin = true
|
||||
}
|
||||
|
||||
private buildVideoJoin () {
|
||||
if (this.builtVideoJoin) return
|
||||
|
||||
this.subQueryJoin += ' INNER JOIN "video" "Video" ON "Video"."id" = "VideoCommentModel"."videoId" '
|
||||
|
||||
this.builtVideoJoin = true
|
||||
}
|
||||
|
||||
private buildVideoChannelJoin () {
|
||||
if (this.builtVideoChannelJoin) return
|
||||
|
||||
this.buildVideoJoin()
|
||||
|
||||
this.subQueryJoin += getChannelJoin({
|
||||
base: 'Video->',
|
||||
on: '"Video"."channelId"',
|
||||
includeAccount: false,
|
||||
includeAvatars: false,
|
||||
includeActors: false,
|
||||
required: true
|
||||
})
|
||||
|
||||
this.builtVideoChannelJoin = true
|
||||
}
|
||||
|
||||
private buildVideoChannelActorJoin () {
|
||||
if (this.builtVideoChannelActorJoin) return
|
||||
|
||||
this.subQueryJoin += getActorJoin({
|
||||
base: 'Video->VideoChannel->',
|
||||
on: '"Video->VideoChannel"."id"',
|
||||
type: 'channel',
|
||||
includeAvatars: false,
|
||||
required: true
|
||||
})
|
||||
|
||||
this.builtVideoChannelActorJoin = true
|
||||
}
|
||||
|
||||
private buildVideoChannelCollaboratorsJoin () {
|
||||
if (this.builtVideoChannelCollaboratorsJoin) return
|
||||
|
||||
this.buildVideoChannelJoin()
|
||||
|
||||
this.subQueryJoin += ' LEFT JOIN "videoChannelCollaborator" "Video->VideoChannel->VideoChannelCollaborators" ' +
|
||||
'ON "Video->VideoChannel->VideoChannelCollaborators"."channelId" = "Video->VideoChannel"."id" ' +
|
||||
'AND "Video->VideoChannel->VideoChannelCollaborators"."state" = :channelCollaboratorState ' +
|
||||
// Ensure we join with max 1 collaborator to not duplicate rows
|
||||
'AND "Video->VideoChannel->VideoChannelCollaborators"."accountId" = :videoAccountOwnerId '
|
||||
|
||||
this.replacements.videoAccountOwnerId = this.options.videoAccountOwnerId
|
||||
this.replacements.channelCollaboratorState = VideoChannelCollaboratorState.ACCEPTED
|
||||
|
||||
this.builtVideoChannelCollaboratorsJoin = true
|
||||
}
|
||||
|
||||
private buildAutomaticTagsJoin () {
|
||||
if (this.builtAutomaticTagsJoin) return
|
||||
|
||||
this.subQueryJoin += ' 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.builtAutomaticTagsJoin = true
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
private buildAccountAvatarsJoin () {
|
||||
if (this.builtAccountAvatarJoin) return
|
||||
|
||||
this.join += getAvatarsJoin({
|
||||
base: 'Account->Actor->',
|
||||
on: '"VideoCommentModel"."Account.Actor.id"'
|
||||
})
|
||||
|
||||
this.builtAccountAvatarJoin = true
|
||||
}
|
||||
|
||||
private buildChannelAvatarsJoin () {
|
||||
if (this.builtChannelAvatarJoin) return
|
||||
|
||||
this.join += getAvatarsJoin({
|
||||
base: 'Video->VideoChannel->Actor->',
|
||||
on: '"VideoCommentModel"."Video.VideoChannel.Actor.id"'
|
||||
})
|
||||
|
||||
this.builtChannelAvatarJoin = true
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
protected buildQueryJoin () {
|
||||
const selectType = this.options.selectType
|
||||
|
||||
if (selectType === 'api-list' || selectType === 'api-video' || selectType === 'feed') {
|
||||
this.buildAccountAvatarsJoin()
|
||||
}
|
||||
|
||||
if (selectType === 'api-list') {
|
||||
this.buildChannelAvatarsJoin()
|
||||
}
|
||||
}
|
||||
|
||||
protected buildQueryAttributes () {
|
||||
const selectType = this.options.selectType
|
||||
|
||||
if (selectType === 'api-list' || selectType === 'api-video' || selectType === 'feed') {
|
||||
this.attributes.push(this.tableAttributes.getAccountAvatarAttributes())
|
||||
}
|
||||
|
||||
if (selectType === 'api-list') {
|
||||
this.attributes.push(this.tableAttributes.getChannelAvatarAttributes())
|
||||
}
|
||||
}
|
||||
|
||||
protected buildSubQueryJoin () {
|
||||
const selectType = this.options.selectType
|
||||
|
||||
if (selectType === 'api-list' || selectType === 'api-video' || selectType === 'feed') {
|
||||
this.buildAccountJoin()
|
||||
this.buildAccountActorJoin()
|
||||
}
|
||||
|
||||
if (selectType === 'api-list') {
|
||||
this.buildVideoJoin()
|
||||
this.buildVideoChannelJoin()
|
||||
this.buildVideoChannelActorJoin()
|
||||
}
|
||||
|
||||
if (this.options.autoTagOfAccountId && selectType === 'api-list') {
|
||||
this.buildAutomaticTagsJoin()
|
||||
}
|
||||
}
|
||||
|
||||
protected buildSubQueryAttributes () {
|
||||
const selectType = this.options.selectType
|
||||
|
||||
this.subQueryAttributes = [
|
||||
...this.subQueryAttributes,
|
||||
|
||||
this.tableAttributes.getVideoCommentAttributes()
|
||||
]
|
||||
|
||||
if (selectType === 'api-list' || selectType === 'api-video' || selectType === 'feed') {
|
||||
this.subQueryAttributes = [
|
||||
...this.subQueryAttributes,
|
||||
|
||||
this.tableAttributes.getVideoAttributes(),
|
||||
|
||||
this.tableAttributes.getAccountAttributes(),
|
||||
this.tableAttributes.getAccountActorAttributes(),
|
||||
this.tableAttributes.getAccountServerAttributes()
|
||||
]
|
||||
}
|
||||
|
||||
if (selectType === 'api-list') {
|
||||
this.subQueryAttributes = [
|
||||
...this.subQueryAttributes,
|
||||
|
||||
this.tableAttributes.getChannelAttributes(),
|
||||
this.tableAttributes.getChannelActorAttributes(),
|
||||
this.tableAttributes.getChannelServerAttributes()
|
||||
]
|
||||
}
|
||||
|
||||
if (this.options.autoTagOfAccountId && this.options.selectType === 'api-list') {
|
||||
this.subQueryAttributes = [
|
||||
...this.subQueryAttributes,
|
||||
|
||||
this.tableAttributes.getCommentAutomaticTagAttributes(),
|
||||
this.tableAttributes.getAutomaticTagAttributes()
|
||||
]
|
||||
}
|
||||
|
||||
if (this.options.includeReplyCounters === true) {
|
||||
this.subQueryAttributes.push('"totalRepliesFromVideoAuthor"."count" AS "totalRepliesFromVideoAuthor"')
|
||||
this.subQueryAttributes.push('"totalReplies"."count" AS "totalReplies"')
|
||||
}
|
||||
}
|
||||
|
||||
protected getCalculatedAttributes () {
|
||||
return [
|
||||
'totalRepliesFromVideoAuthor',
|
||||
'totalReplies'
|
||||
]
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
protected buildSubQueryLateralJoin () {
|
||||
if (this.options.includeReplyCounters === true) {
|
||||
this.buildTotalRepliesLateralJoin()
|
||||
this.buildAuthorTotalRepliesLateralJoin()
|
||||
}
|
||||
}
|
||||
|
||||
private buildTotalRepliesLateralJoin () {
|
||||
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.subQueryLateralJoin += `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"."inReplyToCommentId" = "VideoCommentModel"."id" OR "replies"."originCommentId" = "VideoCommentModel"."id") ` +
|
||||
`AND "deletedAt" IS NULL ` +
|
||||
`AND ${blockWhereString} ` +
|
||||
`) "totalReplies" ON TRUE `
|
||||
}
|
||||
|
||||
private buildAuthorTotalRepliesLateralJoin () {
|
||||
// Help the planner by providing videoId that should filter out many comments
|
||||
this.replacements.videoId = this.options.videoId
|
||||
|
||||
this.subQueryLateralJoin += `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"."inReplyToCommentId" = "VideoCommentModel"."id" OR "replies"."originCommentId" = "VideoCommentModel"."id") ` +
|
||||
`AND "replies"."accountId" = "videoChannel"."accountId"` +
|
||||
`) "totalRepliesFromVideoAuthor" ON TRUE `
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
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."id" = actor."accountId" ` +
|
||||
`INNER JOIN "serverBlocklist" ON "actor"."serverId" = "serverBlocklist"."targetServerId" ` +
|
||||
`WHERE "account"."id" = "${commentTableName}"."accountId" ` +
|
||||
`AND "serverBlocklist"."accountId" IN (${blockerIdsString})` +
|
||||
`)`
|
||||
)
|
||||
|
||||
return where
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
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 { CommentAutomaticTagModel } from '@server/models/automatic-tag/comment-automatic-tag.js'
|
||||
import { ServerModel } from '@server/models/server/server.js'
|
||||
import { buildSQLAttributes } from '@server/models/shared/table.js'
|
||||
import { AutomaticTagModel } from '../../../automatic-tag/automatic-tag.js'
|
||||
import { VideoCommentModel } from '../../video-comment.js'
|
||||
import { VideoChannelModel } from '../../video-channel.js'
|
||||
|
||||
export class VideoCommentTableAttributes {
|
||||
@Memoize()
|
||||
getVideoCommentAttributes () {
|
||||
return VideoCommentModel.getSQLAttributes('VideoCommentModel').join(', ')
|
||||
}
|
||||
|
||||
@Memoize()
|
||||
getVideoAttributes () {
|
||||
return [
|
||||
`"Video"."id" AS "Video.id"`,
|
||||
`"Video"."uuid" AS "Video.uuid"`,
|
||||
`"Video"."name" AS "Video.name"`
|
||||
].join(', ')
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
@Memoize()
|
||||
getAccountAttributes () {
|
||||
return AccountModel.getSQLAttributes('Account', 'Account.').join(', ')
|
||||
}
|
||||
|
||||
@Memoize()
|
||||
getAccountActorAttributes () {
|
||||
return ActorModel.getSQLAPIAttributes('Account->Actor', `Account.Actor.`).join(', ')
|
||||
}
|
||||
|
||||
@Memoize()
|
||||
getAccountServerAttributes () {
|
||||
return ServerModel.getSQLAttributes('Account->Actor->Server', `Account.Actor.Server.`).join(', ')
|
||||
}
|
||||
|
||||
@Memoize()
|
||||
getAccountAvatarAttributes () {
|
||||
return ActorImageModel.getSQLAttributes('Account->Actor->Avatars', 'Account.Actor.Avatars.').join(', ')
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
@Memoize()
|
||||
getChannelAttributes () {
|
||||
return VideoChannelModel.getSQLSummaryAttributes('Video->VideoChannel', 'Video.VideoChannel.').join(', ')
|
||||
}
|
||||
|
||||
@Memoize()
|
||||
getChannelActorAttributes () {
|
||||
return ActorModel.getSQLSummaryAttributes('Video->VideoChannel->Actor', `Video.VideoChannel.Actor.`).join(', ')
|
||||
}
|
||||
|
||||
@Memoize()
|
||||
getChannelServerAttributes () {
|
||||
return ServerModel.getSQLAttributes('Video->VideoChannel->Actor->Server', `Video.VideoChannel.Actor.Server.`).join(', ')
|
||||
}
|
||||
|
||||
@Memoize()
|
||||
getChannelAvatarAttributes () {
|
||||
return ActorImageModel.getSQLAttributes('Video->VideoChannel->Actor->Avatars', 'Video.VideoChannel.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(', ')
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,217 @@
|
||||
import { VideoChannelCollaboratorState } from '@peertube/peertube-models'
|
||||
import { AbstractListQuery, AbstractListQueryOptions } from '@server/models/shared/abstract-list-query.js'
|
||||
import { getAvatarsJoin, getChannelJoin } from '@server/models/shared/sql/actor-helpers.js'
|
||||
import { Sequelize } from 'sequelize'
|
||||
import { VideoImportTableAttributes } from './video-import-table-attributes.js'
|
||||
|
||||
export interface ListVideoImportsOptions extends AbstractListQueryOptions {
|
||||
userId: number
|
||||
|
||||
id?: number
|
||||
videoId?: number
|
||||
search?: string
|
||||
targetUrl?: string
|
||||
videoChannelSyncId?: number
|
||||
|
||||
collaborationAccountId?: number
|
||||
}
|
||||
|
||||
export class VideoImportListQueryBuilder extends AbstractListQuery {
|
||||
private readonly tableAttributes = new VideoImportTableAttributes()
|
||||
|
||||
private builtVideoJoin = false
|
||||
private builtTagJoin = false
|
||||
private builtChannelCollaboratorsJoin = false
|
||||
private builtThumbnailJoin = false
|
||||
private builtAccountAvatarJoin = false
|
||||
private builtChannelAvatarJoin = false
|
||||
|
||||
constructor (
|
||||
protected readonly sequelize: Sequelize,
|
||||
protected readonly options: ListVideoImportsOptions
|
||||
) {
|
||||
super(sequelize, { modelName: 'VideoImportModel', tableName: 'videoImport' }, options)
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
protected buildSubQueryWhere () {
|
||||
const where: string[] = []
|
||||
|
||||
this.buildVideoJoin()
|
||||
|
||||
if (this.options.collaborationAccountId) {
|
||||
this.buildChannelCollaboratorsJoin()
|
||||
|
||||
where.push(
|
||||
'(' +
|
||||
'"VideoImportModel"."userId" = :userId OR ' +
|
||||
'"Video->VideoChannel->VideoChannelCollaborators"."accountId" = :collaborationAccountId OR ' +
|
||||
'"Video->VideoChannel->Account"."userId" = :userId' +
|
||||
')'
|
||||
)
|
||||
|
||||
this.replacements.collaborationAccountId = this.options.collaborationAccountId
|
||||
this.replacements.userId = this.options.userId
|
||||
} else {
|
||||
where.push(
|
||||
'(' +
|
||||
'"VideoImportModel"."userId" = :userId OR ' +
|
||||
'"Video->VideoChannel->Account"."userId" = :userId' +
|
||||
')'
|
||||
)
|
||||
|
||||
this.replacements.userId = this.options.userId
|
||||
}
|
||||
|
||||
if (this.options.id) {
|
||||
where.push('"VideoImportModel"."id" = :id')
|
||||
|
||||
this.replacements.id = this.options.id
|
||||
}
|
||||
|
||||
if (this.options.videoId) {
|
||||
where.push('"VideoImportModel"."videoId" = :videoId')
|
||||
|
||||
this.replacements.videoId = this.options.videoId
|
||||
}
|
||||
|
||||
if (this.options.targetUrl) {
|
||||
where.push('"VideoImportModel"."targetUrl" = :targetUrl')
|
||||
|
||||
this.replacements.targetUrl = this.options.targetUrl
|
||||
}
|
||||
|
||||
if (this.options.videoChannelSyncId) {
|
||||
where.push('"VideoImportModel"."videoChannelSyncId" = :videoChannelSyncId')
|
||||
|
||||
this.replacements.videoChannelSyncId = this.options.videoChannelSyncId
|
||||
}
|
||||
|
||||
if (this.options.search) {
|
||||
const escapedLikeSearch = this.sequelize.escape('%' + this.options.search + '%')
|
||||
|
||||
where.push(
|
||||
`(` +
|
||||
`lower(immutable_unaccent("Video"."name")) LIKE lower(immutable_unaccent(${escapedLikeSearch})) OR ` +
|
||||
`lower(immutable_unaccent("VideoImportModel"."targetUrl")) LIKE lower(immutable_unaccent(${escapedLikeSearch})) OR ` +
|
||||
`lower(immutable_unaccent("VideoImportModel"."torrentName")) LIKE lower(immutable_unaccent(${escapedLikeSearch})) OR ` +
|
||||
`lower(immutable_unaccent("VideoImportModel"."magnetUri")) LIKE lower(immutable_unaccent(${escapedLikeSearch}))` +
|
||||
`)`
|
||||
)
|
||||
}
|
||||
|
||||
if (where.length !== 0) {
|
||||
this.subQueryWhere = `WHERE ${where.join(' AND ')}`
|
||||
}
|
||||
}
|
||||
|
||||
private buildVideoJoin () {
|
||||
if (this.builtVideoJoin) return
|
||||
|
||||
this.subQueryJoin += ` LEFT JOIN "video" "Video" ON "Video"."id" = "VideoImportModel"."videoId" ` +
|
||||
getChannelJoin({
|
||||
base: 'Video->',
|
||||
on: '"Video"."channelId"',
|
||||
includeAccount: true,
|
||||
includeActors: true,
|
||||
includeAvatars: false,
|
||||
required: false
|
||||
})
|
||||
|
||||
this.builtVideoJoin = true
|
||||
}
|
||||
|
||||
private buildChannelCollaboratorsJoin () {
|
||||
if (this.builtChannelCollaboratorsJoin) return
|
||||
|
||||
this.buildVideoJoin()
|
||||
|
||||
this.subQueryJoin += ' LEFT JOIN "videoChannelCollaborator" "Video->VideoChannel->VideoChannelCollaborators" ' +
|
||||
'ON "Video->VideoChannel->VideoChannelCollaborators"."channelId" = "Video->VideoChannel"."id" ' +
|
||||
'AND "Video->VideoChannel->VideoChannelCollaborators"."state" = :channelCollaboratorState ' +
|
||||
// Ensure we join with max 1 collaborator to not duplicate rows
|
||||
'AND "Video->VideoChannel->VideoChannelCollaborators"."accountId" = :collaborationAccountId '
|
||||
|
||||
this.replacements.channelCollaboratorState = VideoChannelCollaboratorState.ACCEPTED
|
||||
this.replacements.collaborationAccountId = this.options.collaborationAccountId
|
||||
|
||||
this.builtChannelCollaboratorsJoin = true
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
private buildThumbnailJoin () {
|
||||
if (this.builtThumbnailJoin) return
|
||||
|
||||
this.join += ' LEFT JOIN "thumbnail" "Video->Thumbnails" ON "Video->Thumbnails"."videoId" = "Video.id" '
|
||||
|
||||
this.builtThumbnailJoin = true
|
||||
}
|
||||
|
||||
private buildTagJoin () {
|
||||
if (this.builtTagJoin) return
|
||||
|
||||
this.join += ' LEFT JOIN "videoTag" ON "videoTag"."videoId" = "Video.id" ' +
|
||||
'LEFT JOIN "tag" "Video->Tags" ON "Video->Tags"."id" = "videoTag"."tagId" '
|
||||
|
||||
this.builtTagJoin = true
|
||||
}
|
||||
|
||||
private buildAccountAvatarsJoin () {
|
||||
if (this.builtAccountAvatarJoin) return
|
||||
|
||||
this.join += getAvatarsJoin({ base: 'Video->VideoChannel->Account->Actor->', on: '"Video.VideoChannel.Account.Actor.id"' })
|
||||
|
||||
this.builtAccountAvatarJoin = true
|
||||
}
|
||||
|
||||
private buildChannelAvatarsJoin () {
|
||||
if (this.builtChannelAvatarJoin) return
|
||||
|
||||
this.join += getAvatarsJoin({ base: 'Video->VideoChannel->Actor->', on: '"Video.VideoChannel.Actor.id"' })
|
||||
|
||||
this.builtChannelAvatarJoin = true
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
protected buildQueryJoin () {
|
||||
this.buildChannelAvatarsJoin()
|
||||
this.buildAccountAvatarsJoin()
|
||||
this.buildTagJoin()
|
||||
this.buildThumbnailJoin()
|
||||
}
|
||||
|
||||
protected buildQueryAttributes () {
|
||||
this.attributes = [
|
||||
...this.attributes,
|
||||
|
||||
this.tableAttributes.getVideoTagAttributes(),
|
||||
this.tableAttributes.getAccountAvatarAttributes(),
|
||||
this.tableAttributes.getChannelAvatarAttributes(),
|
||||
this.tableAttributes.getThumbnailAttributes()
|
||||
]
|
||||
}
|
||||
|
||||
protected buildSubQueryJoin () {
|
||||
this.buildVideoJoin()
|
||||
}
|
||||
|
||||
protected buildSubQueryAttributes () {
|
||||
this.subQueryAttributes = [
|
||||
...this.subQueryAttributes,
|
||||
|
||||
this.tableAttributes.getVideoImportAttributes(),
|
||||
this.tableAttributes.getVideoAttributes(),
|
||||
|
||||
this.tableAttributes.getChannelAttributes(),
|
||||
this.tableAttributes.getChannelActorAttributes(),
|
||||
this.tableAttributes.getChannelServerAttributes(),
|
||||
|
||||
this.tableAttributes.getAccountAttributes(),
|
||||
this.tableAttributes.getAccountActorAttributes(),
|
||||
this.tableAttributes.getAccountServerAttributes()
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
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 { TagModel } from '../../tag.js'
|
||||
import { ThumbnailModel } from '../../thumbnail.js'
|
||||
import { VideoChannelModel } from '../../video-channel.js'
|
||||
import { VideoImportModel } from '../../video-import.js'
|
||||
import { VideoModel } from '../../video.js'
|
||||
|
||||
export class VideoImportTableAttributes {
|
||||
@Memoize()
|
||||
getVideoImportAttributes () {
|
||||
return VideoImportModel.getSQLAttributes('VideoImportModel').join(', ')
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
@Memoize()
|
||||
getVideoAttributes () {
|
||||
return VideoModel.getSQLAttributes('Video', 'Video.').join(', ')
|
||||
}
|
||||
|
||||
getVideoTagAttributes () {
|
||||
return TagModel.getSQLAttributes('Video->Tags', 'Video.Tags.').join(', ')
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
@Memoize()
|
||||
getChannelAttributes () {
|
||||
return VideoChannelModel.getSQLAttributes('Video->VideoChannel', 'Video.VideoChannel.').join(', ')
|
||||
}
|
||||
|
||||
@Memoize()
|
||||
getChannelActorAttributes () {
|
||||
return ActorModel.getSQLSummaryAttributes('Video->VideoChannel->Actor', `Video.VideoChannel.Actor.`).join(', ')
|
||||
}
|
||||
|
||||
@Memoize()
|
||||
getChannelServerAttributes () {
|
||||
return ServerModel.getSQLSummaryAttributes('Video->VideoChannel->Actor->Server', `Video.VideoChannel.Actor.Server.`).join(', ')
|
||||
}
|
||||
|
||||
@Memoize()
|
||||
getChannelAvatarAttributes () {
|
||||
return ActorImageModel.getSQLAttributes('Video->VideoChannel->Actor->Avatars', 'Video.VideoChannel.Actor.Avatars.').join(', ')
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
@Memoize()
|
||||
getAccountAttributes () {
|
||||
return AccountModel.getSQLSummaryAttributes('Video->VideoChannel->Account', 'Video.VideoChannel.Account.').join(', ')
|
||||
}
|
||||
|
||||
@Memoize()
|
||||
getAccountActorAttributes () {
|
||||
return ActorModel.getSQLSummaryAttributes('Video->VideoChannel->Account->Actor', `Video.VideoChannel.Account.Actor.`).join(', ')
|
||||
}
|
||||
|
||||
@Memoize()
|
||||
getAccountServerAttributes () {
|
||||
return ServerModel.getSQLSummaryAttributes('Video->VideoChannel->Account->Actor->Server', `Video.VideoChannel.Account.Actor.Server.`)
|
||||
.join(', ')
|
||||
}
|
||||
|
||||
@Memoize()
|
||||
getAccountAvatarAttributes () {
|
||||
return ActorImageModel.getSQLAttributes('Video->VideoChannel->Account->Actor->Avatars', 'Video.VideoChannel.Account.Actor.Avatars.')
|
||||
.join(', ')
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
@Memoize()
|
||||
getThumbnailAttributes () {
|
||||
return ThumbnailModel.getSQLAttributes('Video->Thumbnails', 'Video.Thumbnails.').join(', ')
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,282 @@
|
||||
import { ActorImageType, VideoChannelCollaboratorState, VideoPlaylistPrivacy, VideoPlaylistType_Type } from '@peertube/peertube-models'
|
||||
import { WEBSERVER } from '@server/initializers/constants.js'
|
||||
import { AbstractListQuery, AbstractListQueryOptions } from '@server/models/shared/abstract-list-query.js'
|
||||
import { buildServerIdsFollowedBy, getPlaylistSort } from '@server/models/shared/index.js'
|
||||
import { Sequelize } from 'sequelize'
|
||||
import { VideoPlaylistTableAttributes } from './video-playlist-table-attributes.js'
|
||||
|
||||
export interface ListVideoPlaylistsOptions extends AbstractListQueryOptions {
|
||||
followerActorId?: number
|
||||
type?: VideoPlaylistType_Type
|
||||
|
||||
accountId?: number
|
||||
includeCollaborationsForAccount?: number
|
||||
|
||||
videoChannelId?: number
|
||||
listMyPlaylists?: boolean
|
||||
search?: string
|
||||
host?: string
|
||||
uuids?: string[]
|
||||
channelNameOneOf?: string[]
|
||||
withVideos?: boolean
|
||||
}
|
||||
|
||||
export class VideoPlaylistListQueryBuilder extends AbstractListQuery {
|
||||
private readonly tableAttributes = new VideoPlaylistTableAttributes()
|
||||
|
||||
private builtAccountJoin = false
|
||||
private builtVideoChannelJoin = false
|
||||
private builtChannelCollaboratorsJoin = false
|
||||
private builtAccountAvatarJoin = false
|
||||
private builtChannelAvatarJoin = false
|
||||
private builtThumbnailJoin = false
|
||||
|
||||
constructor (
|
||||
protected readonly sequelize: Sequelize,
|
||||
protected readonly options: ListVideoPlaylistsOptions
|
||||
) {
|
||||
super(sequelize, { modelName: 'VideoPlaylistModel', tableName: 'videoPlaylist' }, options)
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
protected buildSubQueryWhere () {
|
||||
const where: string[] = []
|
||||
|
||||
if (this.options.host) {
|
||||
this.buildAccountJoin()
|
||||
|
||||
if (this.options.host === WEBSERVER.HOST) {
|
||||
where.push('"OwnerAccount->Actor"."serverId" IS NULL')
|
||||
} else {
|
||||
where.push('"OwnerAccount->Actor->Server"."host" = :host')
|
||||
|
||||
this.replacements.host = this.options.host
|
||||
}
|
||||
}
|
||||
|
||||
if (this.options.listMyPlaylists !== true) {
|
||||
where.push('"VideoPlaylistModel"."privacy" = :privacy')
|
||||
|
||||
this.replacements.privacy = VideoPlaylistPrivacy.PUBLIC
|
||||
}
|
||||
|
||||
if (this.options.followerActorId) {
|
||||
this.buildAccountJoin()
|
||||
|
||||
where.push(
|
||||
`(` +
|
||||
`"OwnerAccount->Actor"."serverId" IS NULL OR ` +
|
||||
`"OwnerAccount->Actor"."serverId" IN ${buildServerIdsFollowedBy(this.options.followerActorId)}` +
|
||||
`)`
|
||||
)
|
||||
}
|
||||
|
||||
if (this.options.accountId) {
|
||||
if (this.options.includeCollaborationsForAccount) {
|
||||
this.buildChannelCollaboratorsJoin()
|
||||
|
||||
where.push(
|
||||
`(` +
|
||||
` "VideoPlaylistModel"."ownerAccountId" = :accountId OR ` +
|
||||
` "VideoChannel->VideoChannelCollaborators"."accountId" = :collaborationAccountId` +
|
||||
`)`
|
||||
)
|
||||
|
||||
this.replacements.accountId = this.options.accountId
|
||||
this.replacements.collaborationAccountId = this.options.includeCollaborationsForAccount
|
||||
} else {
|
||||
where.push('"VideoPlaylistModel"."ownerAccountId" = :accountId')
|
||||
|
||||
this.replacements.accountId = this.options.accountId
|
||||
}
|
||||
}
|
||||
|
||||
if (this.options.videoChannelId) {
|
||||
if (this.options.includeCollaborationsForAccount) {
|
||||
this.buildChannelCollaboratorsJoin()
|
||||
|
||||
where.push(
|
||||
`(` +
|
||||
` "VideoPlaylistModel"."videoChannelId" = :videoChannelId OR ` +
|
||||
` "VideoChannel->VideoChannelCollaborators"."accountId" = :collaborationAccountId` +
|
||||
`)`
|
||||
)
|
||||
|
||||
this.replacements.videoChannelId = this.options.videoChannelId
|
||||
this.replacements.collaborationAccountId = this.options.includeCollaborationsForAccount
|
||||
} else {
|
||||
where.push('"VideoPlaylistModel"."videoChannelId" = :videoChannelId')
|
||||
|
||||
this.replacements.videoChannelId = this.options.videoChannelId
|
||||
}
|
||||
}
|
||||
|
||||
if (this.options.channelNameOneOf && this.options.channelNameOneOf.length !== 0) {
|
||||
this.buildChannelJoin()
|
||||
|
||||
where.push('"VideoChannel->Actor"."preferredUsername" IN (:channelNameOneOf)')
|
||||
this.replacements.channelNameOneOf = this.options.channelNameOneOf
|
||||
}
|
||||
|
||||
if (this.options.type) {
|
||||
where.push('"VideoPlaylistModel"."type" = :type')
|
||||
|
||||
this.replacements.type = this.options.type
|
||||
}
|
||||
|
||||
if (this.options.uuids) {
|
||||
where.push('"VideoPlaylistModel"."uuid" IN (:uuids)')
|
||||
|
||||
this.replacements.uuids = this.options.uuids
|
||||
}
|
||||
|
||||
if (this.options.withVideos === true) {
|
||||
where.push(`(${this.getTotalVideosQuery()}) != 0`)
|
||||
}
|
||||
|
||||
if (this.options.search) {
|
||||
const escapedSearch = this.sequelize.escape(this.options.search)
|
||||
const escapedLikeSearch = this.sequelize.escape('%' + this.options.search + '%')
|
||||
|
||||
this.subQueryAttributes.push(
|
||||
`word_similarity(lower(immutable_unaccent(${escapedSearch})), lower(immutable_unaccent("VideoPlaylistModel"."name"))) as similarity`
|
||||
)
|
||||
|
||||
where.push(
|
||||
`(` +
|
||||
`lower(immutable_unaccent(${escapedSearch})) <% lower(immutable_unaccent("VideoPlaylistModel"."name")) OR ` +
|
||||
`lower(immutable_unaccent("VideoPlaylistModel"."name")) LIKE lower(immutable_unaccent(${escapedLikeSearch}))` +
|
||||
`)`
|
||||
)
|
||||
} else {
|
||||
this.subQueryAttributes.push('0 as similarity')
|
||||
}
|
||||
|
||||
if (where.length !== 0) {
|
||||
this.subQueryWhere = `WHERE ${where.join(' AND ')}`
|
||||
}
|
||||
}
|
||||
|
||||
private buildAccountJoin () {
|
||||
if (this.builtAccountJoin) return
|
||||
|
||||
this.subQueryJoin += ' INNER JOIN "account" "OwnerAccount" ON "OwnerAccount"."id" = "VideoPlaylistModel"."ownerAccountId" ' +
|
||||
'INNER JOIN "actor" "OwnerAccount->Actor" ON "OwnerAccount->Actor"."accountId" = "OwnerAccount"."id" ' +
|
||||
'LEFT JOIN "server" "OwnerAccount->Actor->Server" ON "OwnerAccount->Actor"."serverId" = "OwnerAccount->Actor->Server"."id" '
|
||||
|
||||
this.builtAccountJoin = true
|
||||
}
|
||||
|
||||
private buildChannelJoin () {
|
||||
if (this.builtVideoChannelJoin) return
|
||||
|
||||
this.subQueryJoin += ' LEFT JOIN "videoChannel" "VideoChannel" ON "VideoPlaylistModel"."videoChannelId" = "VideoChannel"."id" ' +
|
||||
'LEFT JOIN "actor" "VideoChannel->Actor" ON "VideoChannel->Actor"."videoChannelId" = "VideoChannel"."id" ' +
|
||||
'LEFT JOIN "server" "VideoChannel->Actor->Server" ON "VideoChannel->Actor"."serverId" = "VideoChannel->Actor->Server"."id" '
|
||||
|
||||
this.builtVideoChannelJoin = true
|
||||
}
|
||||
|
||||
private buildChannelCollaboratorsJoin () {
|
||||
if (this.builtChannelCollaboratorsJoin) return
|
||||
|
||||
this.buildChannelJoin()
|
||||
|
||||
this.subQueryJoin += ' LEFT JOIN "videoChannelCollaborator" "VideoChannel->VideoChannelCollaborators" ' +
|
||||
'ON "VideoChannel->VideoChannelCollaborators"."channelId" = "VideoChannel"."id" ' +
|
||||
'AND "VideoChannel->VideoChannelCollaborators"."state" = :channelCollaboratorState ' +
|
||||
// Ensure we join with max 1 collaborator to not duplicate rows
|
||||
'AND "VideoChannel->VideoChannelCollaborators"."accountId" = :accountId '
|
||||
|
||||
this.replacements.channelCollaboratorState = VideoChannelCollaboratorState.ACCEPTED
|
||||
this.replacements.accountId = this.options.accountId
|
||||
|
||||
this.builtChannelCollaboratorsJoin = true
|
||||
}
|
||||
|
||||
private buildAccountAvatarsJoin () {
|
||||
if (this.builtAccountAvatarJoin) return
|
||||
|
||||
this.join += `LEFT JOIN "actorImage" "OwnerAccount->Actor->Avatars" ` +
|
||||
`ON "OwnerAccount->Actor->Avatars"."actorId" = "VideoPlaylistModel"."OwnerAccount.Actor.id" ` +
|
||||
`AND "OwnerAccount->Actor->Avatars"."type" = ${ActorImageType.AVATAR} `
|
||||
|
||||
this.builtAccountAvatarJoin = true
|
||||
}
|
||||
|
||||
private buildChannelAvatarsJoin () {
|
||||
if (this.builtChannelAvatarJoin) return
|
||||
|
||||
this.join += `LEFT JOIN "actorImage" "VideoChannel->Actor->Avatars" ` +
|
||||
`ON "VideoChannel->Actor->Avatars"."actorId" = "VideoPlaylistModel"."VideoChannel.Actor.id" ` +
|
||||
`AND "VideoChannel->Actor->Avatars"."type" = ${ActorImageType.AVATAR} `
|
||||
|
||||
this.builtChannelAvatarJoin = true
|
||||
}
|
||||
|
||||
private buildThumbnailJoin () {
|
||||
if (this.builtThumbnailJoin) return
|
||||
|
||||
this.join += ' LEFT JOIN "thumbnail" "Thumbnail" ON "Thumbnail"."videoPlaylistId" = "VideoPlaylistModel"."id" '
|
||||
|
||||
this.builtThumbnailJoin = true
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
protected buildQueryJoin () {
|
||||
this.buildChannelAvatarsJoin()
|
||||
this.buildAccountAvatarsJoin()
|
||||
this.buildThumbnailJoin()
|
||||
}
|
||||
|
||||
protected buildQueryAttributes () {
|
||||
this.attributes = [
|
||||
...this.attributes,
|
||||
|
||||
this.tableAttributes.getAccountAvatarAttributes(),
|
||||
this.tableAttributes.getChannelAvatarAttributes(),
|
||||
this.tableAttributes.getThumbnailAttributes()
|
||||
]
|
||||
}
|
||||
|
||||
protected buildSubQueryJoin () {
|
||||
this.buildAccountJoin()
|
||||
this.buildChannelJoin()
|
||||
}
|
||||
|
||||
protected buildSubQueryAttributes () {
|
||||
this.subQueryAttributes = [
|
||||
...this.subQueryAttributes,
|
||||
|
||||
this.tableAttributes.getVideoPlaylistAttributes(),
|
||||
|
||||
this.tableAttributes.getChannelAttributes(),
|
||||
this.tableAttributes.getChannelActorAttributes(),
|
||||
this.tableAttributes.getChannelServerAttributes(),
|
||||
|
||||
this.tableAttributes.getAccountAttributes(),
|
||||
this.tableAttributes.getAccountActorAttributes(),
|
||||
this.tableAttributes.getAccountServerAttributes(),
|
||||
|
||||
this.getTotalVideosAttribute()
|
||||
]
|
||||
}
|
||||
|
||||
private getTotalVideosQuery () {
|
||||
return `SELECT COUNT("id") FROM "videoPlaylistElement" WHERE "videoPlaylistId" = "VideoPlaylistModel"."id"`
|
||||
}
|
||||
|
||||
private getTotalVideosAttribute () {
|
||||
return `(${this.getTotalVideosQuery()}) AS "videosLength"`
|
||||
}
|
||||
|
||||
protected getSort (sort: string) {
|
||||
return getPlaylistSort(sort)
|
||||
}
|
||||
|
||||
protected getCalculatedAttributes () {
|
||||
return [ 'similarity', 'videosLength' ]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
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 { VideoChannelModel } from '../../video-channel.js'
|
||||
import { VideoPlaylistModel } from '../../video-playlist.js'
|
||||
import { ThumbnailModel } from '../../thumbnail.js'
|
||||
|
||||
export class VideoPlaylistTableAttributes {
|
||||
@Memoize()
|
||||
getVideoPlaylistAttributes () {
|
||||
return VideoPlaylistModel.getSQLAttributes('VideoPlaylistModel').join(', ')
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
@Memoize()
|
||||
getChannelAttributes () {
|
||||
return VideoChannelModel.getSQLSummaryAttributes('VideoChannel', 'VideoChannel.').join(', ')
|
||||
}
|
||||
|
||||
@Memoize()
|
||||
getChannelActorAttributes () {
|
||||
return ActorModel.getSQLSummaryAttributes('VideoChannel->Actor', `VideoChannel.Actor.`).join(', ')
|
||||
}
|
||||
|
||||
@Memoize()
|
||||
getChannelServerAttributes () {
|
||||
return ServerModel.getSQLSummaryAttributes('VideoChannel->Actor->Server', `VideoChannel.Actor.Server.`).join(', ')
|
||||
}
|
||||
|
||||
@Memoize()
|
||||
getChannelAvatarAttributes () {
|
||||
return ActorImageModel.getSQLAttributes('VideoChannel->Actor->Avatars', 'VideoChannel.Actor.Avatars.').join(', ')
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
@Memoize()
|
||||
getAccountAttributes () {
|
||||
return AccountModel.getSQLSummaryAttributes('OwnerAccount', 'OwnerAccount.').join(', ')
|
||||
}
|
||||
|
||||
@Memoize()
|
||||
getAccountActorAttributes () {
|
||||
return ActorModel.getSQLSummaryAttributes('OwnerAccount->Actor', `OwnerAccount.Actor.`).join(', ')
|
||||
}
|
||||
|
||||
@Memoize()
|
||||
getAccountServerAttributes () {
|
||||
return ServerModel.getSQLSummaryAttributes('OwnerAccount->Actor->Server', `OwnerAccount.Actor.Server.`).join(', ')
|
||||
}
|
||||
|
||||
@Memoize()
|
||||
getAccountAvatarAttributes () {
|
||||
return ActorImageModel.getSQLAttributes('OwnerAccount->Actor->Avatars', 'OwnerAccount.Actor.Avatars.').join(', ')
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
@Memoize()
|
||||
getThumbnailAttributes () {
|
||||
return ThumbnailModel.getSQLAttributes('Thumbnail', 'Thumbnail.').join(', ')
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,125 @@
|
||||
import { VideoChannelCollaboratorState } from '@peertube/peertube-models'
|
||||
import { AbstractListQuery, AbstractListQueryOptions } from '@server/models/shared/abstract-list-query.js'
|
||||
import { getChannelSyncSort } from '@server/models/shared/index.js'
|
||||
import { OrderItem, Sequelize } from 'sequelize'
|
||||
import { VideoChannelSyncTableAttributes } from './video-channel-sync-table-attributes.js'
|
||||
import { getAvatarsJoin, getChannelJoin } from '@server/models/shared/sql/actor-helpers.js'
|
||||
|
||||
export interface ListVideoChannelSyncsOptions extends AbstractListQueryOptions {
|
||||
accountId: number
|
||||
|
||||
includeCollaborations?: boolean
|
||||
}
|
||||
|
||||
export class VideoChannelSyncListQueryBuilder extends AbstractListQuery {
|
||||
private readonly tableAttributes = new VideoChannelSyncTableAttributes()
|
||||
|
||||
private builtChannelJoin = false
|
||||
private builtChannelAvatarsJoin = false
|
||||
private builtChannelCollaboratorsJoin = false
|
||||
|
||||
constructor (
|
||||
protected readonly sequelize: Sequelize,
|
||||
protected readonly options: ListVideoChannelSyncsOptions
|
||||
) {
|
||||
super(sequelize, { modelName: 'VideoChannelSyncModel', tableName: 'videoChannelSync' }, options)
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
protected buildSubQueryWhere () {
|
||||
const where: string[] = []
|
||||
|
||||
this.buildChannelJoin()
|
||||
|
||||
if (this.options.includeCollaborations) {
|
||||
this.buildChannelCollaboratorsJoin()
|
||||
|
||||
where.push(
|
||||
`("VideoChannel"."accountId" = :accountId OR "VideoChannel->VideoChannelCollaborators"."accountId" = :accountId)`
|
||||
)
|
||||
|
||||
this.replacements.accountId = this.options.accountId
|
||||
} else {
|
||||
where.push(`"VideoChannel"."accountId" = :accountId`)
|
||||
|
||||
this.replacements.accountId = this.options.accountId
|
||||
}
|
||||
|
||||
if (where.length !== 0) {
|
||||
this.subQueryWhere = `WHERE ${where.join(' AND ')}`
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
private buildChannelJoin () {
|
||||
if (this.builtChannelJoin) return
|
||||
|
||||
this.subQueryJoin += getChannelJoin({
|
||||
on: `"VideoChannelSyncModel"."videoChannelId"`,
|
||||
includeAccount: false,
|
||||
includeAvatars: false,
|
||||
includeActors: true,
|
||||
required: true
|
||||
})
|
||||
this.builtChannelJoin = true
|
||||
}
|
||||
|
||||
private buildChannelCollaboratorsJoin () {
|
||||
if (this.builtChannelCollaboratorsJoin) return
|
||||
|
||||
this.subQueryJoin += ' LEFT JOIN "videoChannelCollaborator" "VideoChannel->VideoChannelCollaborators" ' +
|
||||
'ON "VideoChannel->VideoChannelCollaborators"."channelId" = "VideoChannel"."id" ' +
|
||||
'AND "VideoChannel->VideoChannelCollaborators"."state" = :channelCollaboratorState ' +
|
||||
// Ensure we join with max 1 collaborator to not duplicate rows
|
||||
'AND "VideoChannel->VideoChannelCollaborators"."accountId" = :accountId '
|
||||
|
||||
this.replacements.channelCollaboratorState = VideoChannelCollaboratorState.ACCEPTED
|
||||
this.replacements.accountId = this.options.accountId
|
||||
|
||||
this.builtChannelCollaboratorsJoin = true
|
||||
}
|
||||
|
||||
private buildChannelAvatarsJoin () {
|
||||
if (this.builtChannelAvatarsJoin) return
|
||||
|
||||
this.join += getAvatarsJoin({ base: 'VideoChannel->Actor->', on: `"VideoChannelSyncModel"."VideoChannel.Actor.id"` })
|
||||
|
||||
this.builtChannelAvatarsJoin = true
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
protected buildQueryJoin () {
|
||||
this.buildChannelAvatarsJoin()
|
||||
}
|
||||
|
||||
protected buildQueryAttributes () {
|
||||
this.attributes = [
|
||||
...this.attributes,
|
||||
|
||||
this.tableAttributes.getChannelAvatarAttributes()
|
||||
]
|
||||
}
|
||||
|
||||
protected buildSubQueryJoin () {
|
||||
this.buildChannelJoin()
|
||||
}
|
||||
|
||||
protected buildSubQueryAttributes () {
|
||||
this.subQueryAttributes = [
|
||||
...this.subQueryAttributes,
|
||||
|
||||
this.tableAttributes.getVideoChannelSyncAttributes(),
|
||||
|
||||
this.tableAttributes.getChannelAttributes(),
|
||||
this.tableAttributes.getChannelActorAttributes(),
|
||||
this.tableAttributes.getChannelServerAttributes()
|
||||
]
|
||||
}
|
||||
|
||||
protected getSort (sort: string): OrderItem[] {
|
||||
return getChannelSyncSort(sort)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
import { Memoize } from '@server/helpers/memoize.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 { VideoChannelSyncModel } from '../../video-channel-sync.js'
|
||||
import { VideoChannelModel } from '../../video-channel.js'
|
||||
|
||||
export class VideoChannelSyncTableAttributes {
|
||||
@Memoize()
|
||||
getVideoChannelSyncAttributes () {
|
||||
return VideoChannelSyncModel.getSQLAttributes('VideoChannelSyncModel').join(', ')
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
@Memoize()
|
||||
getChannelAttributes () {
|
||||
return VideoChannelModel.getSQLSummaryAttributes('VideoChannel', `VideoChannel.`).join(', ')
|
||||
}
|
||||
|
||||
@Memoize()
|
||||
getChannelActorAttributes () {
|
||||
return ActorModel.getSQLAPIAttributes('VideoChannel->Actor', `VideoChannel.Actor.`).join(', ')
|
||||
}
|
||||
|
||||
@Memoize()
|
||||
getChannelServerAttributes () {
|
||||
return ServerModel.getSQLSummaryAttributes('VideoChannel->Actor->Server', `VideoChannel.Actor.Server.`).join(', ')
|
||||
}
|
||||
|
||||
@Memoize()
|
||||
getChannelAvatarAttributes () {
|
||||
return ActorImageModel.getSQLAttributes('VideoChannel->Actor->Avatars', 'VideoChannel.Actor.Avatars.').join(', ')
|
||||
}
|
||||
}
|
||||
3
server/core/models/video/sql/video/index.ts
Normal file
3
server/core/models/video/sql/video/index.ts
Normal file
@@ -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'
|
||||
@@ -0,0 +1,380 @@
|
||||
import { ActorImageType } from '@peertube/peertube-models'
|
||||
import { MUserAccountId } from '@server/types/models/index.js'
|
||||
import { Sequelize } from 'sequelize'
|
||||
import validator from 'validator'
|
||||
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"."id" = "VideoChannel->Actor"."videoChannelId"')
|
||||
|
||||
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"."id" = "VideoChannel->Account->Actor"."accountId"'
|
||||
)
|
||||
|
||||
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->Actor"."id" = "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 includeLiveSchedules () {
|
||||
this.addJoin(
|
||||
'LEFT OUTER JOIN "videoLiveSchedule" AS "VideoLive->VideoLiveSchedules" ' +
|
||||
'ON "VideoLive->VideoLiveSchedules"."liveVideoId" = "VideoLive"."id"'
|
||||
)
|
||||
|
||||
this.attributes = {
|
||||
...this.attributes,
|
||||
|
||||
...this.buildAttributesObject('VideoLive->VideoLiveSchedules', this.tables.getLiveScheduleAttributes())
|
||||
}
|
||||
}
|
||||
|
||||
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 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 includeCaptions () {
|
||||
this.addJoin(
|
||||
'LEFT JOIN "videoCaption" AS "VideoCaptions" ON "video"."id" = "VideoCaptions"."videoId"'
|
||||
)
|
||||
|
||||
this.attributes = {
|
||||
...this.attributes,
|
||||
|
||||
...this.buildAttributesObject('VideoCaptions', this.tables.getCaptionAttributes())
|
||||
}
|
||||
}
|
||||
|
||||
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 + ' '
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
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 {
|
||||
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()
|
||||
|
||||
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}`
|
||||
}
|
||||
}
|
||||
500
server/core/models/video/sql/video/shared/video-model-builder.ts
Normal file
500
server/core/models/video/sql/video/shared/video-model-builder.ts
Normal file
@@ -0,0 +1,500 @@
|
||||
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 { VideoCaptionModel } from '@server/models/video/video-caption.js'
|
||||
import { VideoLiveScheduleModel } from '@server/models/video/video-live-schedule.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 liveScheduleDone: Set<any>
|
||||
private redundancyDone: Set<any>
|
||||
private scheduleVideoUpdateDone: Set<any>
|
||||
|
||||
private trackersDone: Set<string>
|
||||
private tagsDone: Set<string>
|
||||
private autoTagsDone: Set<string>
|
||||
private captionsDone: Set<any>
|
||||
|
||||
private videos: VideoModel[]
|
||||
|
||||
private readonly buildOpts = { raw: true, isNewRecord: false }
|
||||
|
||||
constructor (
|
||||
private readonly mode: 'get' | 'list',
|
||||
private readonly tables: VideoTableAttributes
|
||||
) {
|
||||
}
|
||||
|
||||
buildVideosFromRows (options: {
|
||||
rows: SQLRow[]
|
||||
addCaptions: boolean
|
||||
include?: VideoIncludeType
|
||||
rowsWebVideoFiles?: SQLRow[]
|
||||
rowsStreamingPlaylist?: SQLRow[]
|
||||
}) {
|
||||
const { rows, rowsWebVideoFiles, rowsStreamingPlaylist, include, addCaptions } = options
|
||||
|
||||
this.reinit()
|
||||
|
||||
for (const row of rows) {
|
||||
this.buildVideoAndAccount(row, { addCaptions })
|
||||
|
||||
const videoModel = this.videosMemo[row.id as number]
|
||||
|
||||
this.setUserHistory(row, videoModel)
|
||||
this.addThumbnail(row, videoModel)
|
||||
this.setLive(row, videoModel)
|
||||
this.addLiveSchedule(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.addCaption(row, videoModel)
|
||||
this.setBlacklisted(row, videoModel)
|
||||
this.setScheduleVideoUpdate(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.NOT_PUBLISHED_STATE) {
|
||||
this.setScheduleVideoUpdate(row, videoModel)
|
||||
}
|
||||
|
||||
if (include & VideoInclude.SOURCE) {
|
||||
this.setSource(row, videoModel)
|
||||
}
|
||||
|
||||
if (include & VideoInclude.AUTOMATIC_TAGS) {
|
||||
this.addAutoTag(row, videoModel)
|
||||
}
|
||||
|
||||
if (include & VideoInclude.TAGS) {
|
||||
this.addTag(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.liveScheduleDone = new Set()
|
||||
|
||||
this.accountBlocklistDone = new Set()
|
||||
this.serverBlocklistDone = new Set()
|
||||
|
||||
this.trackersDone = new Set()
|
||||
this.tagsDone = new Set()
|
||||
this.autoTagsDone = new Set()
|
||||
this.captionsDone = 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)
|
||||
}
|
||||
}
|
||||
|
||||
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, options: {
|
||||
addCaptions: boolean
|
||||
}) {
|
||||
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 = []
|
||||
|
||||
if (options.addCaptions === true) {
|
||||
videoModel.VideoCaptions = []
|
||||
}
|
||||
|
||||
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: 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 addCaption (row: SQLRow, videoModel: VideoModel) {
|
||||
const id = row['VideoCaptions.id']
|
||||
|
||||
if (!id || this.captionsDone.has(id)) return
|
||||
|
||||
const attributes = this.grab(row, this.tables.getCaptionAttributes(), 'VideoCaptions')
|
||||
const captionModel = new VideoCaptionModel(attributes, this.buildOpts)
|
||||
|
||||
videoModel.VideoCaptions.push(captionModel)
|
||||
|
||||
this.captionsDone.add(id)
|
||||
}
|
||||
|
||||
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 addLiveSchedule (row: SQLRow, videoModel: VideoModel) {
|
||||
const id = row['VideoLive.VideoLiveSchedules.id']
|
||||
if (!id) return
|
||||
if (this.liveScheduleDone.has(id)) return
|
||||
|
||||
const videoLiveScheduleAttributes = this.grab(row, this.tables.getLiveScheduleAttributes(), 'VideoLive.VideoLiveSchedules')
|
||||
|
||||
const liveScheduleModel = new VideoLiveScheduleModel(videoLiveScheduleAttributes, this.buildOpts)
|
||||
|
||||
if (!videoModel.VideoLive.LiveSchedules) {
|
||||
videoModel.VideoLive.LiveSchedules = []
|
||||
}
|
||||
|
||||
videoModel.VideoLive.LiveSchedules.push(liveScheduleModel)
|
||||
|
||||
this.liveScheduleDone.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
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,314 @@
|
||||
/**
|
||||
* 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'
|
||||
]
|
||||
|
||||
if (this.mode === 'get') {
|
||||
attributeKeys = attributeKeys.concat([
|
||||
'support',
|
||||
'createdAt',
|
||||
'updatedAt'
|
||||
])
|
||||
}
|
||||
|
||||
return attributeKeys
|
||||
}
|
||||
|
||||
getUserAccountAttributes () {
|
||||
return [ 'id', 'userId' ]
|
||||
}
|
||||
|
||||
getAccountAttributes () {
|
||||
let attributeKeys = [ 'id', 'name' ]
|
||||
|
||||
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',
|
||||
'formatFlags',
|
||||
'streams',
|
||||
'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'
|
||||
]
|
||||
}
|
||||
|
||||
getLiveScheduleAttributes () {
|
||||
return [
|
||||
'id',
|
||||
'startAt',
|
||||
'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' ]
|
||||
}
|
||||
|
||||
getCaptionAttributes () {
|
||||
return [ 'id', 'language', 'fileUrl', 'storage', 'filename', 'automaticallyGenerated', 'm3u8Filename', 'm3u8Url' ]
|
||||
}
|
||||
|
||||
getActorAttributes () {
|
||||
let attributeKeys = [
|
||||
'id',
|
||||
'preferredUsername',
|
||||
'url',
|
||||
'serverId',
|
||||
'accountId',
|
||||
'videoChannelId'
|
||||
]
|
||||
|
||||
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',
|
||||
'nsfwSummary',
|
||||
'nsfwFlags',
|
||||
'description',
|
||||
'support',
|
||||
'duration',
|
||||
'views',
|
||||
'likes',
|
||||
'dislikes',
|
||||
'remote',
|
||||
'isLive',
|
||||
'aspectRatio',
|
||||
'url',
|
||||
'commentsPolicy',
|
||||
'downloadEnabled',
|
||||
'waitTranscoding',
|
||||
'state',
|
||||
'publishedAt',
|
||||
'originallyPublishedAt',
|
||||
'inputFileUpdatedAt',
|
||||
'channelId',
|
||||
'createdAt',
|
||||
'updatedAt',
|
||||
'moveJobsRunning',
|
||||
'comments'
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,196 @@
|
||||
import { pick } from '@peertube/peertube-core-utils'
|
||||
import { Sequelize, Transaction } from 'sequelize'
|
||||
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'
|
||||
| 'seo'
|
||||
|
||||
const videoFilesInclude = new Set<GetType>([ 'api', 'full', 'account-blacklist-files', 'all-files' ])
|
||||
const captionsInclude = new Set<GetType>([ 'seo' ])
|
||||
|
||||
const trackersInclude = new Set<GetType>([ 'api' ])
|
||||
const liveInclude = new Set<GetType>([ 'api', 'full' ])
|
||||
const scheduleUpdateInclude = new Set<GetType>([ 'api', 'full' ])
|
||||
const tagsInclude = new Set<GetType>([ 'api', 'full', 'seo' ])
|
||||
const userHistoryInclude = new Set<GetType>([ 'api', 'full' ])
|
||||
const accountInclude = new Set<GetType>([ 'api', 'full', 'account', 'account-blacklist-files', 'seo' ])
|
||||
const ownerUserInclude = new Set<GetType>([ 'blacklist-rights' ])
|
||||
|
||||
const blacklistedInclude = new Set<GetType>([
|
||||
'api',
|
||||
'full',
|
||||
'account-blacklist-files',
|
||||
'thumbnails-blacklist',
|
||||
'blacklist-rights',
|
||||
'seo'
|
||||
])
|
||||
|
||||
const thumbnailsInclude = new Set<GetType>([
|
||||
'api',
|
||||
'full',
|
||||
'account-blacklist-files',
|
||||
'all-files',
|
||||
'thumbnails',
|
||||
'thumbnails-blacklist',
|
||||
'seo'
|
||||
])
|
||||
|
||||
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
|
||||
|
||||
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),
|
||||
|
||||
videoFilesInclude.has(options.type)
|
||||
? this.webVideoFilesQueryBuilder.queryWebVideos(fileQueryOptions)
|
||||
: Promise.resolve(undefined),
|
||||
|
||||
videoFilesInclude.has(options.type)
|
||||
? this.streamingPlaylistFilesQueryBuilder.queryStreamingPlaylistVideos(fileQueryOptions)
|
||||
: Promise.resolve(undefined)
|
||||
])
|
||||
|
||||
const videos = this.videoModelBuilder.buildVideosFromRows({
|
||||
rows: videoRows,
|
||||
addCaptions: captionsInclude.has(options.type),
|
||||
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 webVideoFilesQuery: string
|
||||
protected streamingPlaylistFilesQuery: string
|
||||
|
||||
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 (thumbnailsInclude.has(options.type)) {
|
||||
this.includeThumbnails()
|
||||
}
|
||||
|
||||
if (blacklistedInclude.has(options.type)) {
|
||||
this.includeBlacklisted()
|
||||
}
|
||||
|
||||
if (accountInclude.has(options.type)) {
|
||||
this.includeChannels()
|
||||
this.includeAccounts()
|
||||
}
|
||||
|
||||
if (tagsInclude.has(options.type)) {
|
||||
this.includeTags()
|
||||
}
|
||||
|
||||
if (scheduleUpdateInclude.has(options.type)) {
|
||||
this.includeScheduleUpdate()
|
||||
}
|
||||
|
||||
if (liveInclude.has(options.type)) {
|
||||
this.includeLive()
|
||||
this.includeLiveSchedules()
|
||||
}
|
||||
|
||||
if (options.userId && userHistoryInclude.has(options.type)) {
|
||||
this.includeUserHistory(options.userId)
|
||||
}
|
||||
|
||||
if (ownerUserInclude.has(options.type)) {
|
||||
this.includeOwnerUser()
|
||||
}
|
||||
|
||||
if (trackersInclude.has(options.type)) {
|
||||
this.includeTrackers()
|
||||
}
|
||||
|
||||
if (captionsInclude.has(options.type)) {
|
||||
this.includeCaptions()
|
||||
}
|
||||
|
||||
this.whereId(options)
|
||||
|
||||
this.query = this.buildQuery(options)
|
||||
}
|
||||
|
||||
private buildQuery (options: BuildVideoGetQueryOptions) {
|
||||
const order = 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}`
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,907 @@
|
||||
import { forceNumber } from '@peertube/peertube-core-utils'
|
||||
import {
|
||||
VideoChannelCollaboratorState,
|
||||
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 { Sequelize, Transaction } from 'sequelize'
|
||||
import validator from 'validator'
|
||||
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
|
||||
nsfwFlagsIncluded?: number
|
||||
nsfwFlagsExcluded?: number
|
||||
|
||||
host?: string
|
||||
isLive?: boolean
|
||||
isLocal?: boolean
|
||||
include?: VideoIncludeType
|
||||
includeScheduledLive?: boolean
|
||||
|
||||
categoryOneOf?: number[]
|
||||
licenceOneOf?: number[]
|
||||
languageOneOf?: string[]
|
||||
|
||||
tagsOneOf?: string[]
|
||||
tagsAllOf?: string[]
|
||||
|
||||
privacyOneOf?: VideoPrivacyType[]
|
||||
|
||||
autoTagOneOf?: string[]
|
||||
|
||||
uuids?: string[]
|
||||
|
||||
hasFiles?: boolean
|
||||
hasHLSFiles?: boolean
|
||||
|
||||
hasWebVideoFiles?: boolean
|
||||
|
||||
accountId?: number
|
||||
includeCollaborations?: boolean
|
||||
|
||||
videoChannelId?: number
|
||||
channelNameOneOf?: string[]
|
||||
|
||||
videoPlaylistId?: number
|
||||
|
||||
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 = ''
|
||||
|
||||
private builtChannelJoin = false
|
||||
|
||||
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,
|
||||
queryConfig: this.queryConfig
|
||||
}
|
||||
}
|
||||
|
||||
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"."id" = "accountActor"."accountId"'
|
||||
])
|
||||
|
||||
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)) {
|
||||
if (options.includeScheduledLive) this.joinLiveSchedules()
|
||||
|
||||
this.whereStateAvailable({ includeScheduledLive: options.includeScheduledLive ?? false })
|
||||
}
|
||||
|
||||
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({ accountId: options.accountId, includeCollaborations: options.includeCollaborations })
|
||||
}
|
||||
|
||||
if (options.videoChannelId) {
|
||||
this.whereChannelId(options.videoChannelId)
|
||||
}
|
||||
|
||||
if (options.channelNameOneOf && options.channelNameOneOf.length !== 0) {
|
||||
this.whereChannelOneOf(options.channelNameOneOf)
|
||||
}
|
||||
|
||||
if (options.displayOnlyForFollower) {
|
||||
this.whereFollowerActorId({ ...options.displayOnlyForFollower, isCount: options.isCount === true })
|
||||
}
|
||||
|
||||
if (options.hasFiles === true) {
|
||||
this.whereFileExists()
|
||||
}
|
||||
|
||||
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(options.nsfwFlagsExcluded)
|
||||
} else if (options.nsfw === false) {
|
||||
this.whereSFW(options.nsfwFlagsIncluded)
|
||||
} else if (options.nsfwFlagsExcluded) {
|
||||
this.whereNSFWFlagsExcluded(options.nsfwFlagsExcluded)
|
||||
}
|
||||
|
||||
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.sort.endsWith('trending')) {
|
||||
this.groupForTrending(options.trendingDays)
|
||||
} else if (options.sort.endsWith('hot') || options.sort.endsWith('best')) {
|
||||
this.addAttributeForHotOrBest(options.sort, 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 joinLiveSchedules () {
|
||||
this.joins.push(
|
||||
'LEFT JOIN "videoLive" ON "video"."id" = "videoLive"."videoId"',
|
||||
'LEFT JOIN "videoLiveSchedule" ON "videoLiveSchedule"."liveVideoId" = "videoLive"."id"'
|
||||
)
|
||||
}
|
||||
|
||||
private joinChannel () {
|
||||
if (this.builtChannelJoin) return
|
||||
this.builtChannelJoin = true
|
||||
|
||||
this.joins.push('INNER JOIN "actor" "channelActor" ON "videoChannel"."id" = "channelActor"."videoChannelId"')
|
||||
}
|
||||
|
||||
private whereStateAvailable (options: {
|
||||
includeScheduledLive: boolean
|
||||
}) {
|
||||
const or: string[] = []
|
||||
|
||||
or.push(`"video"."state" = ${VideoState.PUBLISHED}`)
|
||||
or.push(`("video"."state" = ${VideoState.TO_TRANSCODE} AND "video"."waitTranscoding" IS false)`)
|
||||
|
||||
if (options.includeScheduledLive) {
|
||||
or.push(`("video"."state" = ${VideoState.WAITING_FOR_LIVE} AND "videoLiveSchedule"."startAt" > NOW())`)
|
||||
}
|
||||
|
||||
this.and.push(`(${or.join(' OR ')})`)
|
||||
}
|
||||
|
||||
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 (options: {
|
||||
accountId: number
|
||||
includeCollaborations: boolean
|
||||
}) {
|
||||
if (options.includeCollaborations !== true) {
|
||||
this.and.push('"account"."id" = :accountId')
|
||||
this.replacements.accountId = options.accountId
|
||||
return
|
||||
}
|
||||
|
||||
this.joins.push(
|
||||
'LEFT JOIN "videoChannelCollaborator" ON "videoChannelCollaborator"."channelId" = "videoChannel".id ' +
|
||||
'AND "videoChannelCollaborator"."state" = :channelCollaboratorState ' +
|
||||
// Ensure we join with max 1 collaborator to not duplicate rows
|
||||
'AND "videoChannelCollaborator"."accountId" = :accountId'
|
||||
)
|
||||
|
||||
this.and.push('("account"."id" = :accountId OR "videoChannelCollaborator"."accountId" = :accountId)')
|
||||
|
||||
this.replacements.accountId = options.accountId
|
||||
this.replacements.channelCollaboratorState = VideoChannelCollaboratorState.ACCEPTED
|
||||
}
|
||||
|
||||
private whereChannelId (channelId: number) {
|
||||
this.and.push('"videoChannel"."id" = :videoChannelId')
|
||||
this.replacements.videoChannelId = channelId
|
||||
}
|
||||
|
||||
private whereChannelOneOf (channelOneOf: string[]) {
|
||||
this.joinChannel()
|
||||
|
||||
this.and.push('"channelActor"."preferredUsername" IN (:channelOneOf)')
|
||||
this.replacements.channelOneOf = channelOneOf
|
||||
}
|
||||
|
||||
private whereFollowerActorId (options: { actorId: number, orLocalVideos: boolean, isCount: boolean }) {
|
||||
// EXISTS doesn't work very well with COUNT queries, so we use a CTE instead
|
||||
if (options.isCount) {
|
||||
// Don't use CTE on purpose, that seems to be slower in this case
|
||||
const targetActorIdQuery =
|
||||
`SELECT "targetActorId" FROM "actorFollow" WHERE "actorFollow"."actorId" = :followerActorId AND "actorFollow"."state" = 'accepted'`
|
||||
|
||||
let cteQuery = '"videoCandidates" AS (' +
|
||||
`SELECT "videoShare"."videoId" FROM "videoShare" WHERE "videoShare"."actorId" IN (${targetActorIdQuery}) ` +
|
||||
`UNION ` +
|
||||
`SELECT "video"."id" FROM "video" INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ` +
|
||||
` INNER JOIN "actor" "channelActor" ON "videoChannel"."id" = "channelActor"."videoChannelId" ` +
|
||||
` WHERE "channelActor"."id" IN (${targetActorIdQuery}) ` +
|
||||
`UNION ` +
|
||||
`SELECT "video"."id" FROM "video" INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ` +
|
||||
` INNER JOIN "account" ON "account"."id" = "videoChannel"."accountId" ` +
|
||||
` INNER JOIN "actor" "accountActor" ON "account"."id" = "accountActor"."accountId" ` +
|
||||
` WHERE "accountActor"."id" IN (${targetActorIdQuery}) `
|
||||
|
||||
if (options.orLocalVideos) {
|
||||
cteQuery += 'UNION SELECT "video"."id" FROM "video" WHERE "remote" IS FALSE'
|
||||
}
|
||||
|
||||
cteQuery += ')'
|
||||
|
||||
this.cte.push(cteQuery)
|
||||
|
||||
this.joins.push('INNER JOIN "videoCandidates" ON "video"."id" = "videoCandidates"."videoId"')
|
||||
} else {
|
||||
this.joinChannel()
|
||||
|
||||
let query = ''
|
||||
|
||||
query = '(' +
|
||||
' EXISTS (' + // Videos shared by actors (instances, channels) 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"' +
|
||||
' UNION ALL ' +
|
||||
' SELECT 1 from "actorFollow" ' + // Videos published by accounts we follow
|
||||
' WHERE "actorFollow"."targetActorId" = "accountActor"."id" ' +
|
||||
' AND "actorFollow"."actorId" = :followerActorId ' +
|
||||
' AND "actorFollow"."state" = \'accepted\'' +
|
||||
' UNION ALL ' +
|
||||
' SELECT 1 from "actorFollow" ' + // Videos published by channels we follow
|
||||
' WHERE "actorFollow"."targetActorId" = "channelActor"."id" ' +
|
||||
' AND "actorFollow"."actorId" = :followerActorId ' +
|
||||
' AND "actorFollow"."state" = \'accepted\'' +
|
||||
' LIMIT 1' +
|
||||
' )'
|
||||
|
||||
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 (nsfwFlagsExcluded?: number) {
|
||||
let filter = '"video"."nsfw" IS TRUE'
|
||||
|
||||
if (nsfwFlagsExcluded) {
|
||||
filter += ' AND "video"."nsfwFlags" & :nsfwFlagsExcluded = 0'
|
||||
this.replacements.nsfwFlagsExcluded = nsfwFlagsExcluded
|
||||
}
|
||||
|
||||
this.and.push(filter)
|
||||
}
|
||||
|
||||
private whereSFW (nsfwFlagsIncluded?: number) {
|
||||
let filter = '"video"."nsfw" IS FALSE'
|
||||
|
||||
if (nsfwFlagsIncluded) {
|
||||
filter = `(${filter} OR "video"."nsfwFlags" & :nsfwFlagsIncluded != 0)`
|
||||
this.replacements.nsfwFlagsIncluded = nsfwFlagsIncluded
|
||||
}
|
||||
|
||||
this.and.push(filter)
|
||||
}
|
||||
|
||||
private whereNSFWFlagsExcluded (nsfwFlagsExcluded: number) {
|
||||
this.and.push('"video"."nsfwFlags" & :nsfwFlagsExcluded = 0')
|
||||
this.replacements.nsfwFlagsExcluded = nsfwFlagsExcluded
|
||||
}
|
||||
|
||||
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.queryConfig = 'SET pg_trgm.word_similarity_threshold = 0.40;'
|
||||
|
||||
this.cte.push(
|
||||
'"trigramSearch" AS (' +
|
||||
' SELECT "video"."id", ' +
|
||||
` word_similarity(lower(immutable_unaccent(${escapedSearch})), lower(immutable_unaccent("video"."name"))) as similarity ` +
|
||||
' FROM "video" ' +
|
||||
' WHERE lower(immutable_unaccent(' + escapedSearch + ')) <% lower(immutable_unaccent("video"."name")) 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)
|
||||
|
||||
let attribute = `COALESCE("trigramSearch"."similarity", 0)`
|
||||
if (this.group) attribute = `AVG(${attribute})`
|
||||
|
||||
this.attributes.push(`${attribute} 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 addAttributeForHotOrBest (sort: 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
|
||||
}
|
||||
|
||||
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, "video"."comments")) * ${weights.comment} ` + // comments (+)
|
||||
'+ (SELECT (EXTRACT(epoch FROM "video"."publishedAt") - 1446156582) / 47000) ' // base score (in number of half-days)
|
||||
|
||||
if (sort.endsWith('best') && user) {
|
||||
this.joins.push(
|
||||
'LEFT JOIN "userVideoHistory" ON "video"."id" = "userVideoHistory"."videoId" AND "userVideoHistory"."userId" = :bestUser'
|
||||
)
|
||||
this.replacements.bestUser = user.id
|
||||
|
||||
attribute += `+ POWER(CASE WHEN "userVideoHistory"."id" IS NULL THEN 0 ELSE 1 END, 2.0) * ${weights.history} `
|
||||
}
|
||||
|
||||
attribute += 'AS "score"'
|
||||
this.attributes.push(attribute)
|
||||
}
|
||||
|
||||
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 (field.toLowerCase() === 'total') return `ORDER BY "total" ${direction}`
|
||||
|
||||
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}`
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,134 @@
|
||||
import { pick } from '@peertube/peertube-core-utils'
|
||||
import { VideoInclude } from '@peertube/peertube-models'
|
||||
import { getServerActor } from '@server/models/application/application.js'
|
||||
import { MActorAccount } from '@server/types/models/index.js'
|
||||
import { Sequelize } from 'sequelize'
|
||||
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'
|
||||
|
||||
/**
|
||||
* Build videos list SQL query and create video models
|
||||
*/
|
||||
|
||||
export class VideosModelListQueryBuilder extends AbstractVideoQueryBuilder {
|
||||
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,
|
||||
addCaptions: false,
|
||||
rowsStreamingPlaylist,
|
||||
rowsWebVideoFiles
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return this.videoModelBuilder.buildVideosFromRows({ rows, include: options.include, addCaptions: false })
|
||||
}
|
||||
|
||||
private buildInnerQuery (options: BuildVideosListQueryOptions) {
|
||||
const idsQueryBuilder = new VideosIdListQueryBuilder(this.sequelize)
|
||||
const { query, sort, replacements, queryConfig } = idsQueryBuilder.getQuery(options)
|
||||
|
||||
this.replacements = replacements
|
||||
this.innerQuery = query
|
||||
this.innerSort = sort
|
||||
this.queryConfig = queryConfig
|
||||
}
|
||||
|
||||
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.isLive || options.includeScheduledLive) {
|
||||
this.includeLive()
|
||||
}
|
||||
|
||||
if (options.includeScheduledLive) {
|
||||
this.includeLiveSchedules()
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
if (options.include & VideoInclude.TAGS) {
|
||||
this.includeTags()
|
||||
}
|
||||
|
||||
if (options.include & VideoInclude.NOT_PUBLISHED_STATE) {
|
||||
this.includeScheduleUpdate()
|
||||
}
|
||||
|
||||
const select = this.buildSelect()
|
||||
|
||||
this.query = `${select} FROM (${this.innerQuery}) AS "tmp" ${this.joins} ${this.innerSort}`
|
||||
}
|
||||
}
|
||||
173
server/core/models/video/storyboard.ts
Normal file
173
server/core/models/video/storyboard.ts
Normal file
@@ -0,0 +1,173 @@
|
||||
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
|
||||
declare filename: string
|
||||
|
||||
@AllowNull(false)
|
||||
@Column
|
||||
declare totalHeight: number
|
||||
|
||||
@AllowNull(false)
|
||||
@Column
|
||||
declare totalWidth: number
|
||||
|
||||
@AllowNull(false)
|
||||
@Column
|
||||
declare spriteHeight: number
|
||||
|
||||
@AllowNull(false)
|
||||
@Column
|
||||
declare spriteWidth: number
|
||||
|
||||
@AllowNull(false)
|
||||
@Column
|
||||
declare spriteDuration: number
|
||||
|
||||
@AllowNull(true)
|
||||
@Column(DataType.STRING(CONSTRAINTS_FIELDS.COMMONS.URL.max))
|
||||
declare fileUrl: string
|
||||
|
||||
@ForeignKey(() => VideoModel)
|
||||
@Column
|
||||
declare videoId: number
|
||||
|
||||
@BelongsTo(() => VideoModel, {
|
||||
foreignKey: {
|
||||
allowNull: false
|
||||
},
|
||||
onDelete: 'CASCADE'
|
||||
})
|
||||
declare Video: Awaited<VideoModel>
|
||||
|
||||
@CreatedAt
|
||||
declare createdAt: Date
|
||||
|
||||
@UpdatedAt
|
||||
declare 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.isLocal()) {
|
||||
return WEBSERVER.URL + this.getLocalStaticPath()
|
||||
}
|
||||
|
||||
return this.fileUrl
|
||||
}
|
||||
|
||||
getFileUrl () {
|
||||
return WEBSERVER.URL + this.getLocalStaticPath()
|
||||
}
|
||||
|
||||
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 {
|
||||
fileUrl: this.getFileUrl(),
|
||||
storyboardPath: this.getLocalStaticPath(),
|
||||
|
||||
totalHeight: this.totalHeight,
|
||||
totalWidth: this.totalWidth,
|
||||
|
||||
spriteWidth: this.spriteWidth,
|
||||
spriteHeight: this.spriteHeight,
|
||||
|
||||
spriteDuration: this.spriteDuration
|
||||
}
|
||||
}
|
||||
}
|
||||
95
server/core/models/video/tag.ts
Normal file
95
server/core/models/video/tag.ts
Normal file
@@ -0,0 +1,95 @@
|
||||
import { VideoPrivacy, VideoState } from '@peertube/peertube-models'
|
||||
import { MTag } from '@server/types/models/video/tag.js'
|
||||
import { col, fn, QueryTypes, Transaction } from 'sequelize'
|
||||
import { AllowNull, BelongsToMany, Column, Is, Table } from 'sequelize-typescript'
|
||||
import { isVideoTagValid } from '../../helpers/custom-validators/videos.js'
|
||||
import { buildSQLAttributes, 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
|
||||
declare name: string
|
||||
|
||||
@BelongsToMany(() => VideoModel, {
|
||||
foreignKey: 'tagId',
|
||||
through: () => VideoTagModel,
|
||||
onDelete: 'CASCADE'
|
||||
})
|
||||
declare Videos: Awaited<VideoModel>[]
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
static getSQLAttributes (tableName: string, aliasPrefix = '') {
|
||||
return buildSQLAttributes({
|
||||
model: this,
|
||||
tableName,
|
||||
aliasPrefix
|
||||
})
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
// 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)
|
||||
}
|
||||
}
|
||||
254
server/core/models/video/thumbnail.ts
Normal file
254
server/core/models/video/thumbnail.ts
Normal file
@@ -0,0 +1,254 @@
|
||||
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 { extname, 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, MIMETYPES, WEBSERVER } from '../../initializers/constants.js'
|
||||
import { SequelizeModel } from '../shared/sequelize-type.js'
|
||||
import { buildSQLAttributes } from '../shared/table.js'
|
||||
import { VideoPlaylistModel } from './video-playlist.js'
|
||||
import { VideoModel } from './video.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
|
||||
declare filename: string
|
||||
|
||||
@AllowNull(true)
|
||||
@Default(null)
|
||||
@Column
|
||||
declare height: number
|
||||
|
||||
@AllowNull(true)
|
||||
@Default(null)
|
||||
@Column
|
||||
declare width: number
|
||||
|
||||
@AllowNull(false)
|
||||
@Column
|
||||
declare type: ThumbnailType_Type
|
||||
|
||||
@AllowNull(true)
|
||||
@Column(DataType.STRING(CONSTRAINTS_FIELDS.COMMONS.URL.max))
|
||||
declare fileUrl: string
|
||||
|
||||
@AllowNull(true)
|
||||
@Column
|
||||
declare automaticallyGenerated: boolean
|
||||
|
||||
@AllowNull(false)
|
||||
@Column
|
||||
declare onDisk: boolean
|
||||
|
||||
@ForeignKey(() => VideoModel)
|
||||
@Column
|
||||
declare videoId: number
|
||||
|
||||
@BelongsTo(() => VideoModel, {
|
||||
foreignKey: {
|
||||
allowNull: true
|
||||
},
|
||||
onDelete: 'CASCADE'
|
||||
})
|
||||
declare Video: Awaited<VideoModel>
|
||||
|
||||
@ForeignKey(() => VideoPlaylistModel)
|
||||
@Column
|
||||
declare videoPlaylistId: number
|
||||
|
||||
@BelongsTo(() => VideoPlaylistModel, {
|
||||
foreignKey: {
|
||||
allowNull: true
|
||||
},
|
||||
onDelete: 'CASCADE'
|
||||
})
|
||||
declare VideoPlaylist: Awaited<VideoPlaylistModel>
|
||||
|
||||
@CreatedAt
|
||||
declare createdAt: Date
|
||||
|
||||
@UpdatedAt
|
||||
declare 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 getSQLAttributes (tableName: string, aliasPrefix = '') {
|
||||
return buildSQLAttributes({
|
||||
model: this,
|
||||
tableName,
|
||||
aliasPrefix
|
||||
})
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
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
|
||||
|
||||
// FIXME: typings
|
||||
if ((videoOrPlaylist as MVideo).isLocal()) 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
|
||||
}
|
||||
|
||||
isLocal () {
|
||||
return !this.fileUrl
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
toActivityPubObject (this: MThumbnail, video: MVideo): ActivityIconObject {
|
||||
return {
|
||||
type: 'Image',
|
||||
url: this.getOriginFileUrl(video),
|
||||
mediaType: MIMETYPES.IMAGE.EXT_MIMETYPE[extname(this.filename)],
|
||||
width: this.width,
|
||||
height: this.height
|
||||
}
|
||||
}
|
||||
}
|
||||
132
server/core/models/video/video-blacklist.ts
Normal file
132
server/core/models/video/video-blacklist.ts
Normal file
@@ -0,0 +1,132 @@
|
||||
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))
|
||||
declare reason: string
|
||||
|
||||
@AllowNull(false)
|
||||
@Column
|
||||
declare unfederated: boolean
|
||||
|
||||
@AllowNull(false)
|
||||
@Default(null)
|
||||
@Is('VideoBlacklistType', value => throwIfNotValid(value, isVideoBlacklistTypeValid, 'type'))
|
||||
@Column
|
||||
declare type: VideoBlacklistType_Type
|
||||
|
||||
@CreatedAt
|
||||
declare createdAt: Date
|
||||
|
||||
@UpdatedAt
|
||||
declare updatedAt: Date
|
||||
|
||||
@ForeignKey(() => VideoModel)
|
||||
@Column
|
||||
declare videoId: number
|
||||
|
||||
@BelongsTo(() => VideoModel, {
|
||||
foreignKey: {
|
||||
allowNull: false
|
||||
},
|
||||
onDelete: 'cascade'
|
||||
})
|
||||
declare 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()
|
||||
}
|
||||
}
|
||||
}
|
||||
419
server/core/models/video/video-caption.ts
Normal file
419
server/core/models/video/video-caption.ts
Normal file
@@ -0,0 +1,419 @@
|
||||
import { removeVTTExt } from '@peertube/peertube-core-utils'
|
||||
import { FileStorage, type FileStorageType, VideoCaption, VideoCaptionObject } from '@peertube/peertube-models'
|
||||
import { buildUUID } from '@peertube/peertube-node-utils'
|
||||
import { getObjectStoragePublicFileUrl } from '@server/lib/object-storage/urls.js'
|
||||
import { removeCaptionObjectStorage, removeHLSFileObjectStorageByFilename } from '@server/lib/object-storage/videos.js'
|
||||
import { VideoPathManager } from '@server/lib/video-path-manager.js'
|
||||
import {
|
||||
MVideo,
|
||||
MVideoCaption,
|
||||
MVideoCaptionFilename,
|
||||
MVideoCaptionFormattable,
|
||||
MVideoCaptionLanguageUrl,
|
||||
MVideoCaptionUrl,
|
||||
MVideoCaptionVideo,
|
||||
MVideoOwned,
|
||||
MVideoPrivacy
|
||||
} 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,
|
||||
Default,
|
||||
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, doesExist, throwIfNotValid } from '../shared/index.js'
|
||||
import { VideoStreamingPlaylistModel } from './video-streaming-playlist.js'
|
||||
import { VideoModel } from './video.js'
|
||||
|
||||
export enum ScopeNames {
|
||||
CAPTION_WITH_VIDEO = 'CAPTION_WITH_VIDEO'
|
||||
}
|
||||
|
||||
const videoAttributes = [ 'id', 'name', 'remote', 'uuid', 'url', 'state', 'privacy' ]
|
||||
|
||||
@Scopes(() => ({
|
||||
[ScopeNames.CAPTION_WITH_VIDEO]: {
|
||||
include: [
|
||||
{
|
||||
attributes: videoAttributes,
|
||||
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
|
||||
declare createdAt: Date
|
||||
|
||||
@UpdatedAt
|
||||
declare updatedAt: Date
|
||||
|
||||
@AllowNull(false)
|
||||
@Is('VideoCaptionLanguage', value => throwIfNotValid(value, isVideoCaptionLanguageValid, 'language'))
|
||||
@Column
|
||||
declare language: string
|
||||
|
||||
@AllowNull(false)
|
||||
@Column
|
||||
declare filename: string
|
||||
|
||||
@AllowNull(true)
|
||||
@Column
|
||||
declare m3u8Filename: string
|
||||
|
||||
@AllowNull(false)
|
||||
@Default(FileStorage.FILE_SYSTEM)
|
||||
@Column
|
||||
declare storage: FileStorageType
|
||||
|
||||
@AllowNull(true)
|
||||
@Column(DataType.STRING(CONSTRAINTS_FIELDS.COMMONS.URL.max))
|
||||
declare fileUrl: string
|
||||
|
||||
@AllowNull(true)
|
||||
@Column
|
||||
declare m3u8Url: string
|
||||
|
||||
@AllowNull(false)
|
||||
@Column
|
||||
declare automaticallyGenerated: boolean
|
||||
|
||||
@ForeignKey(() => VideoModel)
|
||||
@Column
|
||||
declare videoId: number
|
||||
|
||||
@BelongsTo(() => VideoModel, {
|
||||
foreignKey: {
|
||||
allowNull: false
|
||||
},
|
||||
onDelete: 'CASCADE'
|
||||
})
|
||||
declare Video: Awaited<VideoModel>
|
||||
|
||||
@BeforeDestroy
|
||||
static async removeFiles (instance: VideoCaptionModel, options) {
|
||||
if (!instance.Video) {
|
||||
instance.Video = await instance.$get('Video', { transaction: options.transaction })
|
||||
}
|
||||
|
||||
if (instance.isLocal()) {
|
||||
logger.info('Removing caption %s.', instance.filename)
|
||||
|
||||
instance.removeAllCaptionFiles()
|
||||
.catch(err => logger.error('Cannot remove caption file ' + instance.filename, { err }))
|
||||
}
|
||||
|
||||
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 async doesOwnedFileExist (filename: string, storage: FileStorageType) {
|
||||
const query = 'SELECT 1 FROM "videoCaption" ' +
|
||||
`WHERE "filename" = $filename AND "storage" = $storage LIMIT 1`
|
||||
|
||||
return doesExist({ sequelize: this.sequelize, query, bind: { filename, storage } })
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
static loadWithVideo (captionId: number, transaction?: Transaction): Promise<MVideoCaptionVideo> {
|
||||
const query = {
|
||||
where: { id: captionId },
|
||||
include: [
|
||||
{
|
||||
model: VideoModel.unscoped(),
|
||||
attributes: videoAttributes
|
||||
}
|
||||
],
|
||||
transaction
|
||||
}
|
||||
|
||||
return VideoCaptionModel.findOne(query)
|
||||
}
|
||||
|
||||
static loadByVideoIdAndLanguage (videoId: string | number, language: string, transaction?: Transaction): Promise<MVideoCaptionVideo> {
|
||||
const videoInclude = {
|
||||
model: VideoModel.unscoped(),
|
||||
attributes: videoAttributes,
|
||||
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: videoAttributes
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
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.CAPTION_WITH_VIDEO).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.CAPTION_WITH_VIDEO).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`
|
||||
}
|
||||
|
||||
static generateM3U8Filename (vttFilename: string) {
|
||||
return removeVTTExt(vttFilename) + '.m3u8'
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
toFormattedJSON (this: MVideoCaptionFormattable): VideoCaption {
|
||||
return {
|
||||
language: {
|
||||
id: this.language,
|
||||
label: VideoCaptionModel.getLanguageLabel(this.language)
|
||||
},
|
||||
automaticallyGenerated: this.automaticallyGenerated,
|
||||
|
||||
// TODO: remove, deprecated in 8.0
|
||||
captionPath: this.Video.isLocal() && this.fileUrl
|
||||
? null // On object storage
|
||||
: this.getFileStaticPath(),
|
||||
|
||||
fileUrl: this.getFileUrl(this.Video),
|
||||
m3u8Url: this.getM3U8Url(this.Video),
|
||||
|
||||
updatedAt: this.updatedAt.toISOString()
|
||||
}
|
||||
}
|
||||
|
||||
toActivityPubObject (this: MVideoCaptionLanguageUrl, video: MVideo): VideoCaptionObject {
|
||||
return {
|
||||
identifier: this.language,
|
||||
name: VideoCaptionModel.getLanguageLabel(this.language),
|
||||
automaticallyGenerated: this.automaticallyGenerated,
|
||||
|
||||
url: [
|
||||
{
|
||||
type: 'Link',
|
||||
mediaType: 'text/vtt',
|
||||
href: this.getOriginFileUrl(video)
|
||||
},
|
||||
{
|
||||
type: 'Link',
|
||||
mediaType: 'application/x-mpegURL',
|
||||
href: this.getOriginFileUrl(video)
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
isLocal () {
|
||||
return this.Video.remote === false
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
getFileStaticPath (this: MVideoCaptionFilename) {
|
||||
return join(LAZY_STATIC_PATHS.VIDEO_CAPTIONS, this.filename)
|
||||
}
|
||||
|
||||
getM3U8StaticPath (this: MVideoCaptionFilename, video: MVideoPrivacy) {
|
||||
if (!this.m3u8Filename) return null
|
||||
|
||||
return VideoStreamingPlaylistModel.getPlaylistFileStaticPath(video, this.m3u8Filename)
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
getFSFilePath () {
|
||||
return join(CONFIG.STORAGE.CAPTIONS_DIR, this.filename)
|
||||
}
|
||||
|
||||
getFSM3U8Path (video: MVideoPrivacy) {
|
||||
if (!this.m3u8Filename) return null
|
||||
|
||||
return VideoPathManager.Instance.getFSHLSOutputPath(video, this.m3u8Filename)
|
||||
}
|
||||
|
||||
async removeAllCaptionFiles (this: MVideoCaptionVideo) {
|
||||
await this.removeCaptionFile()
|
||||
await this.removeCaptionPlaylist()
|
||||
}
|
||||
|
||||
async removeCaptionFile (this: MVideoCaptionVideo) {
|
||||
if (this.storage === FileStorage.OBJECT_STORAGE) {
|
||||
if (this.fileUrl) {
|
||||
await removeCaptionObjectStorage(this)
|
||||
}
|
||||
} else {
|
||||
await remove(this.getFSFilePath())
|
||||
}
|
||||
|
||||
this.filename = null
|
||||
this.fileUrl = null
|
||||
}
|
||||
|
||||
async removeCaptionPlaylist (this: MVideoCaptionVideo) {
|
||||
if (!this.m3u8Filename) return
|
||||
|
||||
const hls = await VideoStreamingPlaylistModel.loadHLSByVideoWithVideo(this.videoId)
|
||||
if (!hls) return
|
||||
|
||||
if (this.storage === FileStorage.OBJECT_STORAGE) {
|
||||
if (this.m3u8Url) {
|
||||
await removeHLSFileObjectStorageByFilename(hls, this.m3u8Filename)
|
||||
}
|
||||
} else {
|
||||
await remove(this.getFSM3U8Path(this.Video))
|
||||
}
|
||||
|
||||
this.m3u8Filename = null
|
||||
this.m3u8Url = null
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
getFileUrl (this: MVideoCaptionUrl, video: MVideoOwned) {
|
||||
if (video.isLocal() && this.storage === FileStorage.OBJECT_STORAGE) {
|
||||
return getObjectStoragePublicFileUrl(this.fileUrl, CONFIG.OBJECT_STORAGE.CAPTIONS)
|
||||
}
|
||||
|
||||
return WEBSERVER.URL + this.getFileStaticPath()
|
||||
}
|
||||
|
||||
getOriginFileUrl (this: MVideoCaptionUrl, video: MVideoOwned) {
|
||||
if (video.isLocal()) return this.getFileUrl(video)
|
||||
|
||||
return this.fileUrl
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
getM3U8Url (this: MVideoCaptionUrl, video: MVideoOwned & MVideoPrivacy) {
|
||||
if (!this.m3u8Filename) return null
|
||||
|
||||
if (video.isLocal()) {
|
||||
if (this.storage === FileStorage.OBJECT_STORAGE) {
|
||||
return getObjectStoragePublicFileUrl(this.m3u8Url, CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS)
|
||||
}
|
||||
|
||||
return WEBSERVER.URL + this.getM3U8StaticPath(video)
|
||||
}
|
||||
|
||||
return this.m3u8Url
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
isEqual (this: MVideoCaption, other: MVideoCaption) {
|
||||
if (this.fileUrl) return this.fileUrl === other.fileUrl
|
||||
|
||||
return this.filename === other.filename
|
||||
}
|
||||
}
|
||||
136
server/core/models/video/video-change-ownership.ts
Normal file
136
server/core/models/video/video-change-ownership.ts
Normal file
@@ -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
|
||||
declare createdAt: Date
|
||||
|
||||
@UpdatedAt
|
||||
declare updatedAt: Date
|
||||
|
||||
@AllowNull(false)
|
||||
@Column
|
||||
declare status: VideoChangeOwnershipStatusType
|
||||
|
||||
@ForeignKey(() => AccountModel)
|
||||
@Column
|
||||
declare initiatorAccountId: number
|
||||
|
||||
@BelongsTo(() => AccountModel, {
|
||||
foreignKey: {
|
||||
name: 'initiatorAccountId',
|
||||
allowNull: false
|
||||
},
|
||||
onDelete: 'cascade'
|
||||
})
|
||||
declare Initiator: Awaited<AccountModel>
|
||||
|
||||
@ForeignKey(() => AccountModel)
|
||||
@Column
|
||||
declare nextOwnerAccountId: number
|
||||
|
||||
@BelongsTo(() => AccountModel, {
|
||||
foreignKey: {
|
||||
name: 'nextOwnerAccountId',
|
||||
allowNull: false
|
||||
},
|
||||
onDelete: 'cascade'
|
||||
})
|
||||
declare NextOwner: Awaited<AccountModel>
|
||||
|
||||
@ForeignKey(() => VideoModel)
|
||||
@Column
|
||||
declare videoId: number
|
||||
|
||||
@BelongsTo(() => VideoModel, {
|
||||
foreignKey: {
|
||||
allowNull: false
|
||||
},
|
||||
onDelete: 'cascade'
|
||||
})
|
||||
declare 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.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
|
||||
}
|
||||
}
|
||||
}
|
||||
508
server/core/models/video/video-channel-activity.ts
Normal file
508
server/core/models/video/video-channel-activity.ts
Normal file
@@ -0,0 +1,508 @@
|
||||
import {
|
||||
ResultList,
|
||||
VideoChannelActivityAction,
|
||||
VideoChannelActivityTarget,
|
||||
type VideoChannelActivity,
|
||||
type VideoChannelActivityActionType,
|
||||
type VideoChannelActivityDetails,
|
||||
type VideoChannelActivityTargetType
|
||||
} from '@peertube/peertube-models'
|
||||
import { uuidToShort } from '@peertube/peertube-node-utils'
|
||||
import { VIDEO_CHANNEL_ACTIVITY_ACTIONS, VIDEO_CHANNEL_ACTIVITY_TARGETS } from '@server/initializers/constants.js'
|
||||
import { MChannelId, MChannelSync, MUserAccountId, MVideo, MVideoImport, MVideoPlaylist } from '@server/types/models/index.js'
|
||||
import { MChannelActivityFormattable } from '@server/types/models/video/video-channel-activity.js'
|
||||
import { Op, Transaction } from 'sequelize'
|
||||
import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Table, UpdatedAt } from 'sequelize-typescript'
|
||||
import { AccountModel } from '../account/account.js'
|
||||
import { ActorModel } from '../actor/actor.js'
|
||||
import { getSort, SequelizeModel } from '../shared/index.js'
|
||||
import { VideoChannelSyncModel } from './video-channel-sync.js'
|
||||
import { VideoChannelModel } from './video-channel.js'
|
||||
import { VideoImportModel } from './video-import.js'
|
||||
import { VideoPlaylistModel } from './video-playlist.js'
|
||||
import { VideoModel } from './video.js'
|
||||
|
||||
interface VideoChannelActivityData {
|
||||
video?: {
|
||||
id: number
|
||||
name: string
|
||||
uuid: string
|
||||
url: string
|
||||
isLive: boolean
|
||||
}
|
||||
|
||||
videoImport?: {
|
||||
id: number
|
||||
name: string
|
||||
uuid: string
|
||||
url: string
|
||||
targetUrl: string
|
||||
}
|
||||
|
||||
playlist?: {
|
||||
id: number
|
||||
name: string
|
||||
uuid: string
|
||||
url: string
|
||||
}
|
||||
|
||||
channel?: {
|
||||
id: number
|
||||
name: string
|
||||
displayName: string
|
||||
url: string
|
||||
}
|
||||
|
||||
channelSync?: {
|
||||
id: number
|
||||
externalChannelUrl: string
|
||||
}
|
||||
}
|
||||
|
||||
@Table({
|
||||
tableName: 'videoChannelActivity',
|
||||
indexes: [
|
||||
{
|
||||
fields: [ 'videoChannelId' ]
|
||||
},
|
||||
{
|
||||
fields: [ 'accountId' ]
|
||||
},
|
||||
{
|
||||
fields: [ 'videoId' ],
|
||||
where: {
|
||||
videoId: {
|
||||
[Op.ne]: null
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
fields: [ 'videoPlaylistId' ],
|
||||
where: {
|
||||
videoPlaylistId: {
|
||||
[Op.ne]: null
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
fields: [ 'videoChannelSyncId' ],
|
||||
where: {
|
||||
videoChannelSyncId: {
|
||||
[Op.ne]: null
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
fields: [ 'videoImportId' ],
|
||||
where: {
|
||||
videoImportId: {
|
||||
[Op.ne]: null
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
})
|
||||
export class VideoChannelActivityModel extends SequelizeModel<VideoChannelActivityModel> {
|
||||
@AllowNull(false)
|
||||
@Column
|
||||
declare action: VideoChannelActivityActionType
|
||||
|
||||
@AllowNull(false)
|
||||
@Column
|
||||
declare targetType: VideoChannelActivityTargetType
|
||||
|
||||
// Store association data in case they are deleted
|
||||
@AllowNull(false)
|
||||
@Column(DataType.JSONB)
|
||||
declare data: VideoChannelActivityData
|
||||
|
||||
// More information about the action
|
||||
@AllowNull(true)
|
||||
@Column(DataType.JSONB)
|
||||
declare details: VideoChannelActivityDetails
|
||||
|
||||
@CreatedAt
|
||||
declare createdAt: Date
|
||||
|
||||
@UpdatedAt
|
||||
declare updatedAt: Date
|
||||
|
||||
@ForeignKey(() => VideoChannelModel)
|
||||
@Column
|
||||
declare videoChannelId: number
|
||||
|
||||
@BelongsTo(() => VideoChannelModel, {
|
||||
foreignKey: {
|
||||
allowNull: false
|
||||
},
|
||||
onDelete: 'cascade'
|
||||
})
|
||||
declare VideoChannel: Awaited<VideoChannelModel>
|
||||
|
||||
@ForeignKey(() => AccountModel)
|
||||
@Column
|
||||
declare accountId: number
|
||||
|
||||
@BelongsTo(() => AccountModel, {
|
||||
foreignKey: {
|
||||
allowNull: true
|
||||
},
|
||||
onDelete: 'set null'
|
||||
})
|
||||
declare Account: Awaited<AccountModel>
|
||||
|
||||
@ForeignKey(() => VideoModel)
|
||||
@Column
|
||||
declare videoId: number
|
||||
|
||||
@BelongsTo(() => VideoModel, {
|
||||
foreignKey: {
|
||||
allowNull: true
|
||||
},
|
||||
onDelete: 'set null'
|
||||
})
|
||||
declare Video: Awaited<VideoModel>
|
||||
|
||||
@ForeignKey(() => VideoPlaylistModel)
|
||||
@Column
|
||||
declare videoPlaylistId: number
|
||||
|
||||
@BelongsTo(() => VideoPlaylistModel, {
|
||||
foreignKey: {
|
||||
allowNull: true
|
||||
},
|
||||
onDelete: 'set null'
|
||||
})
|
||||
declare VideoPlaylist: Awaited<VideoPlaylistModel>
|
||||
|
||||
@ForeignKey(() => VideoChannelSyncModel)
|
||||
@Column
|
||||
declare videoChannelSyncId: number
|
||||
|
||||
@BelongsTo(() => VideoChannelSyncModel, {
|
||||
foreignKey: {
|
||||
allowNull: true
|
||||
},
|
||||
onDelete: 'set null'
|
||||
})
|
||||
declare VideoChannelSync: Awaited<VideoChannelSyncModel>
|
||||
|
||||
@ForeignKey(() => VideoImportModel)
|
||||
@Column
|
||||
declare videoImportId: number
|
||||
|
||||
@BelongsTo(() => VideoImportModel, {
|
||||
foreignKey: {
|
||||
allowNull: true
|
||||
},
|
||||
onDelete: 'set null'
|
||||
})
|
||||
declare VideoImport: Awaited<VideoImportModel>
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
static async listForAPI (options: {
|
||||
channelId: number
|
||||
start: number
|
||||
count: number
|
||||
sort: string
|
||||
}): Promise<ResultList<MChannelActivityFormattable>> {
|
||||
const where = { videoChannelId: options.channelId }
|
||||
|
||||
const countQuery = VideoChannelActivityModel.count({ where })
|
||||
const dataQuery = VideoChannelActivityModel.findAll({
|
||||
where,
|
||||
offset: options.start,
|
||||
limit: options.count,
|
||||
order: getSort(options.sort),
|
||||
include: [
|
||||
{
|
||||
model: VideoChannelModel.unscoped(),
|
||||
required: true,
|
||||
include: [
|
||||
{
|
||||
attributes: [ 'preferredUsername', 'url' ],
|
||||
model: ActorModel.unscoped()
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
model: AccountModel,
|
||||
required: false
|
||||
},
|
||||
{
|
||||
model: VideoModel.unscoped(),
|
||||
required: false
|
||||
},
|
||||
{
|
||||
model: VideoPlaylistModel.unscoped(),
|
||||
required: false
|
||||
},
|
||||
{
|
||||
model: VideoChannelSyncModel.unscoped(),
|
||||
required: false
|
||||
},
|
||||
{
|
||||
model: VideoImportModel.unscoped(),
|
||||
required: false,
|
||||
include: [
|
||||
{
|
||||
model: VideoModel.unscoped(),
|
||||
required: false
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
})
|
||||
|
||||
const [ total, data ] = await Promise.all([ countQuery, dataQuery ])
|
||||
|
||||
return { total, data }
|
||||
}
|
||||
|
||||
static async addVideoActivity (options: {
|
||||
action: VideoChannelActivityActionType
|
||||
user: MUserAccountId
|
||||
channel: MChannelId
|
||||
video: MVideo
|
||||
transaction: Transaction
|
||||
}) {
|
||||
const { action, user, channel, video, transaction } = options
|
||||
|
||||
return this.create({
|
||||
action,
|
||||
targetType: VideoChannelActivityTarget.VIDEO,
|
||||
data: {
|
||||
video: {
|
||||
id: video.id,
|
||||
name: video.name,
|
||||
uuid: video.uuid,
|
||||
url: video.url,
|
||||
isLive: video.isLive
|
||||
}
|
||||
},
|
||||
details: null,
|
||||
accountId: user.Account.id,
|
||||
videoChannelId: channel.id,
|
||||
videoId: action !== VideoChannelActivityAction.DELETE
|
||||
? video.id
|
||||
: null
|
||||
}, { transaction })
|
||||
}
|
||||
|
||||
static async addVideoImportActivity (options: {
|
||||
action: VideoChannelActivityActionType
|
||||
user: MUserAccountId
|
||||
channel: MChannelId
|
||||
videoImport: MVideoImport
|
||||
video: MVideo
|
||||
transaction: Transaction
|
||||
}) {
|
||||
const { action, user, channel, video, videoImport, transaction } = options
|
||||
|
||||
return this.create({
|
||||
action,
|
||||
targetType: VideoChannelActivityTarget.VIDEO_IMPORT,
|
||||
data: {
|
||||
videoImport: {
|
||||
id: video.id,
|
||||
name: video.name,
|
||||
uuid: video.uuid,
|
||||
url: video.url,
|
||||
targetUrl: videoImport.targetUrl
|
||||
}
|
||||
},
|
||||
details: null,
|
||||
accountId: user.Account.id,
|
||||
videoChannelId: channel.id,
|
||||
videoImportId: action !== VideoChannelActivityAction.DELETE
|
||||
? videoImport.id
|
||||
: null
|
||||
}, { transaction })
|
||||
}
|
||||
|
||||
static async addChannelActivity (options: {
|
||||
action: VideoChannelActivityActionType
|
||||
user: MUserAccountId
|
||||
channel: MChannelActivityFormattable['VideoChannel']
|
||||
transaction: Transaction
|
||||
}) {
|
||||
const { action, user, channel, transaction } = options
|
||||
|
||||
return this.create({
|
||||
action,
|
||||
targetType: VideoChannelActivityTarget.CHANNEL,
|
||||
data: {
|
||||
channel: {
|
||||
id: channel.id,
|
||||
name: channel.Actor.preferredUsername,
|
||||
displayName: channel.name,
|
||||
url: channel.Actor.url
|
||||
}
|
||||
},
|
||||
details: null,
|
||||
accountId: user.Account.id,
|
||||
videoChannelId: channel.id
|
||||
}, { transaction })
|
||||
}
|
||||
|
||||
static async addPlaylistActivity (options: {
|
||||
action: VideoChannelActivityActionType
|
||||
user: MUserAccountId
|
||||
channel: MChannelId
|
||||
playlist: MVideoPlaylist
|
||||
transaction: Transaction
|
||||
}) {
|
||||
const { action, user, channel, playlist, transaction } = options
|
||||
|
||||
return this.create({
|
||||
action,
|
||||
targetType: VideoChannelActivityTarget.PLAYLIST,
|
||||
data: {
|
||||
playlist: {
|
||||
id: playlist.id,
|
||||
name: playlist.name,
|
||||
uuid: playlist.uuid,
|
||||
url: playlist.url
|
||||
}
|
||||
},
|
||||
details: null,
|
||||
accountId: user.Account.id,
|
||||
videoChannelId: channel.id,
|
||||
videoPlaylistId: action !== VideoChannelActivityAction.DELETE
|
||||
? playlist.id
|
||||
: null
|
||||
}, { transaction })
|
||||
}
|
||||
|
||||
static async addChannelSyncActivity (options: {
|
||||
action: VideoChannelActivityActionType
|
||||
user: MUserAccountId
|
||||
channel: MChannelId
|
||||
sync: MChannelSync
|
||||
transaction: Transaction
|
||||
}) {
|
||||
const { action, user, channel, sync, transaction } = options
|
||||
|
||||
return this.create({
|
||||
action,
|
||||
targetType: VideoChannelActivityTarget.CHANNEL_SYNC,
|
||||
data: {
|
||||
channelSync: {
|
||||
id: sync.id,
|
||||
externalChannelUrl: sync.externalChannelUrl
|
||||
}
|
||||
},
|
||||
details: null,
|
||||
accountId: user.Account.id,
|
||||
videoChannelId: channel.id,
|
||||
videoChannelSyncId: action !== VideoChannelActivityAction.DELETE
|
||||
? sync.id
|
||||
: null
|
||||
}, { transaction })
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
toFormattedJSON (this: MChannelActivityFormattable): VideoChannelActivity {
|
||||
return {
|
||||
id: this.id,
|
||||
|
||||
account: this.Account
|
||||
? this.Account.toFormattedSummaryJSON()
|
||||
: null,
|
||||
|
||||
action: {
|
||||
id: this.action,
|
||||
label: VIDEO_CHANNEL_ACTIVITY_ACTIONS[this.action]
|
||||
},
|
||||
|
||||
targetType: {
|
||||
id: this.targetType,
|
||||
label: VIDEO_CHANNEL_ACTIVITY_TARGETS[this.targetType]
|
||||
},
|
||||
|
||||
video: this.formatVideo(),
|
||||
playlist: this.formatPlaylist(),
|
||||
channel: this.formatChannel(),
|
||||
channelSync: this.formatSync(),
|
||||
videoImport: this.formatVideoImport(),
|
||||
|
||||
details: this.details,
|
||||
createdAt: this.createdAt
|
||||
}
|
||||
}
|
||||
|
||||
formatChannel (this: MChannelActivityFormattable): VideoChannelActivity['channel'] {
|
||||
return {
|
||||
id: this.VideoChannel.id,
|
||||
name: this.VideoChannel.Actor.preferredUsername,
|
||||
displayName: this.VideoChannel.name,
|
||||
url: this.VideoChannel.Actor.url
|
||||
}
|
||||
}
|
||||
|
||||
formatVideo (this: MChannelActivityFormattable): VideoChannelActivity['video'] {
|
||||
if (this.targetType !== VideoChannelActivityTarget.VIDEO) return null
|
||||
|
||||
const target = this.Video || this.data.video
|
||||
|
||||
return {
|
||||
id: target.id,
|
||||
name: target.name,
|
||||
uuid: target.uuid,
|
||||
shortUUID: uuidToShort(target.uuid),
|
||||
isLive: target.isLive,
|
||||
url: target.url
|
||||
}
|
||||
}
|
||||
|
||||
formatVideoImport (this: MChannelActivityFormattable): VideoChannelActivity['videoImport'] {
|
||||
if (this.targetType !== VideoChannelActivityTarget.VIDEO_IMPORT) return null
|
||||
|
||||
if (this.VideoImport && this.VideoImport.Video) {
|
||||
return {
|
||||
id: this.VideoImport.id,
|
||||
name: this.VideoImport.Video.name,
|
||||
uuid: this.VideoImport.Video.uuid,
|
||||
shortUUID: uuidToShort(this.VideoImport.Video.uuid),
|
||||
url: this.VideoImport.Video.url,
|
||||
targetUrl: this.VideoImport.targetUrl
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
id: this.data.videoImport.id,
|
||||
name: this.data.videoImport.name,
|
||||
uuid: this.data.videoImport.uuid,
|
||||
shortUUID: uuidToShort(this.data.videoImport.uuid),
|
||||
url: this.data.videoImport.url,
|
||||
targetUrl: this.data.videoImport.targetUrl
|
||||
}
|
||||
}
|
||||
|
||||
formatPlaylist (this: MChannelActivityFormattable): VideoChannelActivity['playlist'] {
|
||||
if (this.targetType !== VideoChannelActivityTarget.PLAYLIST) return null
|
||||
|
||||
const target = this.VideoPlaylist || this.data.playlist
|
||||
|
||||
return {
|
||||
id: target.id,
|
||||
name: target.name,
|
||||
uuid: target.uuid,
|
||||
shortUUID: uuidToShort(target.uuid),
|
||||
url: target.url
|
||||
}
|
||||
}
|
||||
|
||||
formatSync (): VideoChannelActivity['channelSync'] {
|
||||
if (this.targetType !== VideoChannelActivityTarget.CHANNEL_SYNC) return null
|
||||
|
||||
const target = this.VideoChannelSync || this.data.channelSync
|
||||
|
||||
return {
|
||||
id: target.id,
|
||||
externalChannelUrl: target.externalChannelUrl
|
||||
}
|
||||
}
|
||||
}
|
||||
213
server/core/models/video/video-channel-collaborator.ts
Normal file
213
server/core/models/video/video-channel-collaborator.ts
Normal file
@@ -0,0 +1,213 @@
|
||||
import {
|
||||
VideoChannelCollaboratorState,
|
||||
type VideoChannelCollaborator,
|
||||
type VideoChannelCollaboratorStateType
|
||||
} from '@peertube/peertube-models'
|
||||
import { CHANNEL_COLLABORATOR_STATE } from '@server/initializers/constants.js'
|
||||
import { MChannelCollaboratorAccount, MChannelId, MUserId } from '@server/types/models/index.js'
|
||||
import { AllowNull, BelongsTo, Column, CreatedAt, ForeignKey, Scopes, Table, UpdatedAt } from 'sequelize-typescript'
|
||||
import { AccountModel } from '../account/account.js'
|
||||
import { ActorModel } from '../actor/actor.js'
|
||||
import { SequelizeModel, buildSQLAttributes, doesExist, getSort } from '../shared/index.js'
|
||||
import { VideoChannelModel } from './video-channel.js'
|
||||
import { Op } from 'sequelize'
|
||||
|
||||
enum ScopeNames {
|
||||
WITH_ACCOUNT = 'WITH_ACCOUNT'
|
||||
}
|
||||
|
||||
@Table({
|
||||
tableName: 'videoChannelCollaborator',
|
||||
indexes: [
|
||||
{
|
||||
fields: [ 'channelId', 'accountId' ],
|
||||
unique: true
|
||||
}
|
||||
]
|
||||
})
|
||||
@Scopes(() => ({
|
||||
[ScopeNames.WITH_ACCOUNT]: {
|
||||
include: [
|
||||
{
|
||||
model: AccountModel,
|
||||
required: true
|
||||
}
|
||||
]
|
||||
}
|
||||
}))
|
||||
export class VideoChannelCollaboratorModel extends SequelizeModel<VideoChannelCollaboratorModel> {
|
||||
@CreatedAt
|
||||
declare createdAt: Date
|
||||
|
||||
@UpdatedAt
|
||||
declare updatedAt: Date
|
||||
|
||||
@AllowNull(false)
|
||||
@Column
|
||||
declare state: VideoChannelCollaboratorStateType
|
||||
|
||||
@ForeignKey(() => AccountModel)
|
||||
@Column
|
||||
declare accountId: number
|
||||
|
||||
@BelongsTo(() => AccountModel, {
|
||||
foreignKey: {
|
||||
name: 'accountId',
|
||||
allowNull: false
|
||||
},
|
||||
onDelete: 'cascade'
|
||||
})
|
||||
declare Account: Awaited<AccountModel>
|
||||
|
||||
@ForeignKey(() => AccountModel)
|
||||
@Column
|
||||
declare channelId: number
|
||||
|
||||
@BelongsTo(() => VideoChannelModel, {
|
||||
foreignKey: {
|
||||
name: 'channelId',
|
||||
allowNull: false
|
||||
},
|
||||
onDelete: 'cascade'
|
||||
})
|
||||
declare Channel: Awaited<VideoChannelModel>
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
static getSQLAttributes (tableName: string, aliasPrefix = '') {
|
||||
return buildSQLAttributes({
|
||||
model: this,
|
||||
tableName,
|
||||
aliasPrefix
|
||||
})
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
static listForApi (options: {
|
||||
channelId: number
|
||||
start: number
|
||||
count: number
|
||||
sort: string
|
||||
}) {
|
||||
const { channelId, start, count, sort } = options
|
||||
|
||||
const query = {
|
||||
offset: start,
|
||||
limit: count,
|
||||
order: getSort(sort),
|
||||
where: {
|
||||
channelId,
|
||||
state: {
|
||||
[Op.ne]: VideoChannelCollaboratorState.REJECTED
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return Promise.all([
|
||||
VideoChannelCollaboratorModel.count(query),
|
||||
VideoChannelCollaboratorModel.scope([ ScopeNames.WITH_ACCOUNT ]).findAll<MChannelCollaboratorAccount>(query)
|
||||
]).then(([ count, rows ]) => ({ total: count, data: rows }))
|
||||
}
|
||||
|
||||
static countByChannel (channelId: number): Promise<number> {
|
||||
const query = {
|
||||
where: {
|
||||
channelId
|
||||
}
|
||||
}
|
||||
|
||||
return VideoChannelCollaboratorModel.count(query)
|
||||
}
|
||||
|
||||
static loadByChannelHandle (id: number, channelHandle: string): Promise<MChannelCollaboratorAccount> {
|
||||
return VideoChannelCollaboratorModel
|
||||
.scope([ ScopeNames.WITH_ACCOUNT ])
|
||||
.findOne({
|
||||
where: { id },
|
||||
include: [
|
||||
{
|
||||
model: VideoChannelModel.unscoped(),
|
||||
attributes: [ 'id' ],
|
||||
required: true,
|
||||
include: [
|
||||
{
|
||||
model: ActorModel.unscoped(),
|
||||
required: true,
|
||||
where: {
|
||||
preferredUsername: channelHandle
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
})
|
||||
}
|
||||
|
||||
static loadByCollaboratorAccountName (options: {
|
||||
channelId: number
|
||||
accountName: string
|
||||
}): Promise<MChannelCollaboratorAccount | null> {
|
||||
const { channelId, accountName } = options
|
||||
|
||||
return VideoChannelCollaboratorModel.findOne({
|
||||
where: {
|
||||
channelId
|
||||
},
|
||||
include: [
|
||||
{
|
||||
model: AccountModel.unscoped(),
|
||||
required: true,
|
||||
include: [
|
||||
{
|
||||
model: ActorModel.unscoped(),
|
||||
required: true,
|
||||
where: {
|
||||
preferredUsername: accountName
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
})
|
||||
}
|
||||
|
||||
static async isCollaborator (options: {
|
||||
user: MUserId
|
||||
channel: MChannelId
|
||||
}): Promise<boolean> {
|
||||
const { user, channel } = options
|
||||
|
||||
const query = `SELECT 1 FROM "videoChannelCollaborator" ` +
|
||||
`INNER JOIN "account" ON "account"."id" = "videoChannelCollaborator"."accountId" AND account."userId" = $userId ` +
|
||||
`WHERE "videoChannelCollaborator"."channelId" = $channelId AND "state" = $state ` +
|
||||
`LIMIT 1`
|
||||
|
||||
return doesExist({
|
||||
sequelize: this.sequelize,
|
||||
query,
|
||||
bind: { userId: user.id, channelId: channel.id, state: VideoChannelCollaboratorState.ACCEPTED }
|
||||
})
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
static getStateLabel (state: VideoChannelCollaboratorStateType) {
|
||||
return CHANNEL_COLLABORATOR_STATE[state]
|
||||
}
|
||||
|
||||
toFormattedJSON (this: MChannelCollaboratorAccount): VideoChannelCollaborator {
|
||||
return {
|
||||
id: this.id,
|
||||
|
||||
state: {
|
||||
id: this.state,
|
||||
label: VideoChannelCollaboratorModel.getStateLabel(this.state)
|
||||
},
|
||||
|
||||
account: this.Account.toFormattedJSON(),
|
||||
updatedAt: this.updatedAt.toISOString(),
|
||||
createdAt: this.createdAt.toISOString()
|
||||
}
|
||||
}
|
||||
}
|
||||
158
server/core/models/video/video-channel-sync.ts
Normal file
158
server/core/models/video/video-channel-sync.ts
Normal file
@@ -0,0 +1,158 @@
|
||||
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, buildSQLAttributes, throwIfNotValid } from '../shared/index.js'
|
||||
import { UserModel } from '../user/user.js'
|
||||
import { VideoChannelSyncListQueryBuilder } from './sql/video-channel-sync/video-channel-sync-list-query-builder.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))
|
||||
declare externalChannelUrl: string
|
||||
|
||||
@AllowNull(false)
|
||||
@Default(VideoChannelSyncState.WAITING_FIRST_RUN)
|
||||
@Is('VideoChannelSyncState', value => throwIfNotValid(value, isVideoChannelSyncStateValid, 'state'))
|
||||
@Column
|
||||
declare state: VideoChannelSyncStateType
|
||||
|
||||
@AllowNull(true)
|
||||
@Column(DataType.DATE)
|
||||
declare lastSyncAt: Date
|
||||
|
||||
@CreatedAt
|
||||
declare createdAt: Date
|
||||
|
||||
@UpdatedAt
|
||||
declare updatedAt: Date
|
||||
|
||||
@ForeignKey(() => VideoChannelModel)
|
||||
@Column
|
||||
declare videoChannelId: number
|
||||
|
||||
@BelongsTo(() => VideoChannelModel, {
|
||||
foreignKey: {
|
||||
allowNull: false
|
||||
},
|
||||
onDelete: 'cascade'
|
||||
})
|
||||
declare VideoChannel: Awaited<VideoChannelModel>
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
static getSQLAttributes (tableName: string, aliasPrefix = '') {
|
||||
return buildSQLAttributes({
|
||||
model: this,
|
||||
tableName,
|
||||
aliasPrefix
|
||||
})
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
static listByAccountForAPI (options: {
|
||||
accountId: number
|
||||
start: number
|
||||
count: number
|
||||
sort: string
|
||||
includeCollaborations: boolean
|
||||
}) {
|
||||
return Promise.all([
|
||||
new VideoChannelSyncListQueryBuilder(VideoChannelSyncModel.sequelize, options).list<MChannelSyncFormattable>(),
|
||||
new VideoChannelSyncListQueryBuilder(VideoChannelSyncModel.sequelize, options).count()
|
||||
]).then(([ rows, count ]) => {
|
||||
return { total: count, data: rows }
|
||||
})
|
||||
}
|
||||
|
||||
static countByAccount (accountId: number) {
|
||||
return new VideoChannelSyncListQueryBuilder(VideoChannelSyncModel.sequelize, { accountId }).count()
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
static loadWithChannel (id: number): Promise<MChannelSyncChannel> {
|
||||
return VideoChannelSyncModel.findByPk(id)
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
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()
|
||||
}
|
||||
}
|
||||
}
|
||||
607
server/core/models/video/video-channel.ts
Normal file
607
server/core/models/video/video-channel.ts
Normal file
@@ -0,0 +1,607 @@
|
||||
import { ActivityPubActor, ActivityUrlObject, VideoChannel, VideoChannelSummary, VideoPrivacy } from '@peertube/peertube-models'
|
||||
import { AttributesOnly } from '@peertube/peertube-typescript-utils'
|
||||
import { CONFIG } from '@server/initializers/config.js'
|
||||
import { getLocalActorPlayerSettingsActivityPubUrl } from '@server/lib/activitypub/url.js'
|
||||
import { InternalEventEmitter } from '@server/lib/internal-event-emitter.js'
|
||||
import { MAccountIdHost } from '@server/types/models/index.js'
|
||||
import { FindOptions, Includeable, literal, Op, QueryTypes, Transaction } from 'sequelize'
|
||||
import {
|
||||
AfterCreate,
|
||||
AfterDestroy,
|
||||
AfterUpdate,
|
||||
AllowNull,
|
||||
BeforeDestroy,
|
||||
BelongsTo,
|
||||
Column,
|
||||
CreatedAt,
|
||||
DataType,
|
||||
Default,
|
||||
DefaultScope,
|
||||
ForeignKey,
|
||||
HasMany,
|
||||
HasOne,
|
||||
Is,
|
||||
Scopes,
|
||||
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,
|
||||
MChannelDefault,
|
||||
MChannelFormattable,
|
||||
MChannelHost,
|
||||
MChannelIdHost,
|
||||
MChannelSummaryFormattable,
|
||||
type MChannel
|
||||
} from '../../types/models/video/index.js'
|
||||
import { AccountModel, ScopeNames as AccountModelScopeNames, SummaryOptions as AccountSummaryOptions } from '../account/account.js'
|
||||
import { ActorImageModel } from '../actor/actor-image.js'
|
||||
import { ActorModel, actorSummaryAttributes } from '../actor/actor.js'
|
||||
import { ServerModel, serverSummaryAttributes } from '../server/server.js'
|
||||
import { buildSQLAttributes, buildTrigramSearchIndex, getSort, SequelizeModel, setAsUpdated, throwIfNotValid } from '../shared/index.js'
|
||||
import { ListVideoChannelsOptions, VideoChannelListQueryBuilder } from './sql/channel/video-channel-list-query-builder.js'
|
||||
import { VideoChannelCollaboratorModel } from './video-channel-collaborator.js'
|
||||
import { VideoPlaylistModel } from './video-playlist.js'
|
||||
import { VideoModel } from './video.js'
|
||||
|
||||
const channelSummaryAttributes = [ 'id', 'name', 'description' ] as const satisfies (keyof AttributesOnly<AccountModel>)[]
|
||||
|
||||
export enum ScopeNames {
|
||||
SUMMARY = 'SUMMARY',
|
||||
WITH_ACCOUNT = 'WITH_ACCOUNT',
|
||||
WITH_ACTOR = 'WITH_ACTOR',
|
||||
WITH_ACTOR_BANNER = 'WITH_ACTOR_BANNER',
|
||||
WITH_VIDEOS = 'WITH_VIDEOS'
|
||||
}
|
||||
|
||||
export type SummaryOptions = {
|
||||
actorRequired?: boolean // Default: true
|
||||
withAccount?: boolean // Default: false
|
||||
withAccountBlockerIds?: number[]
|
||||
}
|
||||
|
||||
@DefaultScope(() => ({
|
||||
include: [
|
||||
{
|
||||
model: ActorModel,
|
||||
required: true
|
||||
}
|
||||
]
|
||||
}))
|
||||
@Scopes(() => ({
|
||||
[ScopeNames.SUMMARY]: (options: SummaryOptions = {}) => {
|
||||
const include: Includeable[] = [
|
||||
{
|
||||
attributes: actorSummaryAttributes,
|
||||
model: ActorModel.unscoped(),
|
||||
required: options.actorRequired ?? true,
|
||||
include: [
|
||||
{
|
||||
attributes: serverSummaryAttributes,
|
||||
model: ServerModel.unscoped(),
|
||||
required: false
|
||||
},
|
||||
{
|
||||
model: ActorImageModel,
|
||||
as: 'Avatars',
|
||||
required: false
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
|
||||
const base: FindOptions = {
|
||||
attributes: channelSummaryAttributes
|
||||
}
|
||||
|
||||
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
|
||||
]
|
||||
}
|
||||
}))
|
||||
@Table({
|
||||
tableName: 'videoChannel',
|
||||
indexes: [
|
||||
buildTrigramSearchIndex('video_channel_name_trigram', 'name'),
|
||||
|
||||
{
|
||||
fields: [ 'accountId' ]
|
||||
}
|
||||
]
|
||||
})
|
||||
export class VideoChannelModel extends SequelizeModel<VideoChannelModel> {
|
||||
@AllowNull(false)
|
||||
@Is('VideoChannelName', value => throwIfNotValid(value, isVideoChannelDisplayNameValid, 'name'))
|
||||
@Column
|
||||
declare name: string
|
||||
|
||||
@AllowNull(true)
|
||||
@Default(null)
|
||||
@Is('VideoChannelDescription', value => throwIfNotValid(value, isVideoChannelDescriptionValid, 'description', true))
|
||||
@Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_CHANNELS.DESCRIPTION.max))
|
||||
declare description: string
|
||||
|
||||
@AllowNull(true)
|
||||
@Default(null)
|
||||
@Is('VideoChannelSupport', value => throwIfNotValid(value, isVideoChannelSupportValid, 'support', true))
|
||||
@Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_CHANNELS.SUPPORT.max))
|
||||
declare support: string
|
||||
|
||||
@CreatedAt
|
||||
declare createdAt: Date
|
||||
|
||||
@UpdatedAt
|
||||
declare updatedAt: Date
|
||||
|
||||
@ForeignKey(() => AccountModel)
|
||||
@Column
|
||||
declare accountId: number
|
||||
|
||||
@BelongsTo(() => AccountModel, {
|
||||
foreignKey: {
|
||||
allowNull: false
|
||||
}
|
||||
})
|
||||
declare Account: Awaited<AccountModel>
|
||||
|
||||
@HasMany(() => VideoModel, {
|
||||
foreignKey: {
|
||||
name: 'channelId',
|
||||
allowNull: false
|
||||
},
|
||||
onDelete: 'CASCADE',
|
||||
hooks: true
|
||||
})
|
||||
declare Videos: Awaited<VideoModel>[]
|
||||
|
||||
@HasMany(() => VideoPlaylistModel, {
|
||||
foreignKey: {
|
||||
allowNull: true
|
||||
},
|
||||
onDelete: 'CASCADE',
|
||||
hooks: true
|
||||
})
|
||||
declare VideoPlaylists: Awaited<VideoPlaylistModel>[]
|
||||
|
||||
@HasMany(() => VideoChannelCollaboratorModel, {
|
||||
foreignKey: 'accountId',
|
||||
onDelete: 'CASCADE'
|
||||
})
|
||||
declare VideoChannelCollaborators: Awaited<VideoChannelCollaboratorModel>[]
|
||||
|
||||
@HasOne(() => ActorModel, {
|
||||
foreignKey: {
|
||||
allowNull: true
|
||||
},
|
||||
onDelete: 'cascade',
|
||||
hooks: true
|
||||
})
|
||||
declare Actor: Awaited<ActorModel>
|
||||
|
||||
@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 })
|
||||
}
|
||||
|
||||
if (instance.Actor.isLocal()) {
|
||||
return sendDeleteActor(instance.Actor, options.transaction)
|
||||
}
|
||||
|
||||
return undefined
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
static getSQLAttributes (tableName: string, aliasPrefix = '') {
|
||||
return buildSQLAttributes({
|
||||
model: this,
|
||||
tableName,
|
||||
aliasPrefix
|
||||
})
|
||||
}
|
||||
|
||||
static getSQLSummaryAttributes (tableName: string, aliasPrefix = '') {
|
||||
return buildSQLAttributes({
|
||||
model: this,
|
||||
tableName,
|
||||
aliasPrefix,
|
||||
includeAttributes: channelSummaryAttributes
|
||||
})
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
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"."id" = "Account->Actor"."accountId"
|
||||
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
|
||||
}
|
||||
}
|
||||
],
|
||||
where: {
|
||||
[Op.and]: [
|
||||
literal(`EXISTS (SELECT 1 FROM "video" WHERE "privacy" = ${VideoPrivacy.PUBLIC} AND "channelId" = "VideoChannelModel"."id")`)
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
return VideoChannelModel
|
||||
.unscoped()
|
||||
.findAll(query)
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
static listForApi (options: ListVideoChannelsOptions) {
|
||||
return Promise.all([
|
||||
new VideoChannelListQueryBuilder(VideoChannelModel.sequelize, options).list<VideoChannelModel>() as Promise<MChannelFormattable[]>,
|
||||
new VideoChannelListQueryBuilder(VideoChannelModel.sequelize, options).count()
|
||||
]).then(([ rows, count ]) => {
|
||||
return { total: count, data: rows }
|
||||
})
|
||||
}
|
||||
|
||||
static listByAccountForAPI (
|
||||
options: Pick<ListVideoChannelsOptions, 'accountId' | 'includeCollaborations' | 'search' | 'start' | 'count' | 'sort'> & {
|
||||
withStats?: boolean
|
||||
}
|
||||
) {
|
||||
const listOptions = options.withStats
|
||||
? { ...options, statsDaysPrior: 30 }
|
||||
: options
|
||||
|
||||
return this.listForApi(listOptions)
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
static listAllOwnedByAccount (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 loadByHandleAndPopulateAccount (handle: string) {
|
||||
const [ name, host ] = handle.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.isLocal(),
|
||||
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,
|
||||
|
||||
url: [
|
||||
{
|
||||
type: 'Link',
|
||||
mediaType: 'text/html',
|
||||
href: this.getClientUrl(true)
|
||||
},
|
||||
{
|
||||
type: 'Link',
|
||||
mediaType: 'text/html',
|
||||
href: this.getClientUrl(false)
|
||||
},
|
||||
{
|
||||
type: 'Link',
|
||||
mediaType: 'text/html',
|
||||
href: this.Actor.url
|
||||
}
|
||||
] as ActivityUrlObject[],
|
||||
|
||||
playerSettings: getLocalActorPlayerSettingsActivityPubUrl(this.Actor),
|
||||
|
||||
summary: this.description,
|
||||
support: this.support,
|
||||
postingRestrictedToMods: true,
|
||||
attributedTo: [
|
||||
this.Account.Actor.url
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
// Avoid error when running this method on MAccount... | MChannel...
|
||||
getClientUrl (this: MAccountIdHost | MChannelIdHost, videosSuffix = true) {
|
||||
const suffix = videosSuffix
|
||||
? '/videos'
|
||||
: ''
|
||||
|
||||
return WEBSERVER.URL + '/c/' + this.Actor.getIdentifier() + suffix
|
||||
}
|
||||
|
||||
getClientManageUrl (this: MAccountIdHost | MChannelIdHost) {
|
||||
return WEBSERVER.URL + '/my-library/video-channels/manage/' + this.Actor.getIdentifier()
|
||||
}
|
||||
|
||||
getDisplayName () {
|
||||
return this.name
|
||||
}
|
||||
|
||||
isOutdated () {
|
||||
return this.Actor.isOutdated()
|
||||
}
|
||||
|
||||
setAsUpdated (transaction?: Transaction) {
|
||||
return setAsUpdated({ sequelize: this.sequelize, table: 'videoChannel', id: this.id, transaction })
|
||||
}
|
||||
}
|
||||
94
server/core/models/video/video-chapter.ts
Normal file
94
server/core/models/video/video-chapter.ts
Normal file
@@ -0,0 +1,94 @@
|
||||
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
|
||||
declare timecode: number
|
||||
|
||||
@AllowNull(false)
|
||||
@Column
|
||||
declare title: string
|
||||
|
||||
@ForeignKey(() => VideoModel)
|
||||
@Column
|
||||
declare videoId: number
|
||||
|
||||
@BelongsTo(() => VideoModel, {
|
||||
foreignKey: {
|
||||
allowNull: false
|
||||
},
|
||||
onDelete: 'CASCADE'
|
||||
})
|
||||
declare Video: Awaited<VideoModel>
|
||||
|
||||
@CreatedAt
|
||||
declare createdAt: Date
|
||||
|
||||
@UpdatedAt
|
||||
declare 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
|
||||
}
|
||||
}
|
||||
}
|
||||
863
server/core/models/video/video-comment.ts
Normal file
863
server/core/models/video/video-comment.ts
Normal file
@@ -0,0 +1,863 @@
|
||||
import { pick } from '@peertube/peertube-core-utils'
|
||||
import {
|
||||
ActivityTagObject,
|
||||
ActivityTombstoneObject,
|
||||
UserRight,
|
||||
VideoComment,
|
||||
VideoCommentForAdminOrUser,
|
||||
VideoCommentObject
|
||||
} from '@peertube/peertube-models'
|
||||
import { afterCommitIfTransaction, retryTransactionWrapper } from '@server/helpers/database-utils.js'
|
||||
import { logger } from '@server/helpers/logger.js'
|
||||
import { extractMentions } from '@server/helpers/mentions.js'
|
||||
import { sequelizeTypescript } from '@server/initializers/database.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 {
|
||||
AfterCreate,
|
||||
AfterDestroy,
|
||||
AfterUpdate,
|
||||
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,
|
||||
MCommentAdminOrUserFormattable,
|
||||
MCommentAP,
|
||||
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 { buildLocalAccountIdsIn, buildSQLAttributes, SequelizeModel, 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
|
||||
declare createdAt: Date
|
||||
|
||||
@UpdatedAt
|
||||
declare updatedAt: Date
|
||||
|
||||
@AllowNull(true)
|
||||
@Column(DataType.DATE)
|
||||
declare deletedAt: Date
|
||||
|
||||
@AllowNull(false)
|
||||
@Is('VideoCommentUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
|
||||
@Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEOS.URL.max))
|
||||
declare url: string
|
||||
|
||||
@AllowNull(false)
|
||||
@Column(DataType.TEXT)
|
||||
declare text: string
|
||||
|
||||
@AllowNull(false)
|
||||
@Column
|
||||
declare heldForReview: boolean
|
||||
|
||||
@AllowNull(true)
|
||||
@Column
|
||||
declare replyApproval: string
|
||||
|
||||
@ForeignKey(() => VideoCommentModel)
|
||||
@Column
|
||||
declare originCommentId: number
|
||||
|
||||
@BelongsTo(() => VideoCommentModel, {
|
||||
foreignKey: {
|
||||
name: 'originCommentId',
|
||||
allowNull: true
|
||||
},
|
||||
as: 'OriginVideoComment',
|
||||
onDelete: 'CASCADE'
|
||||
})
|
||||
declare OriginVideoComment: Awaited<VideoCommentModel>
|
||||
|
||||
@ForeignKey(() => VideoCommentModel)
|
||||
@Column
|
||||
declare inReplyToCommentId: number
|
||||
|
||||
@BelongsTo(() => VideoCommentModel, {
|
||||
foreignKey: {
|
||||
name: 'inReplyToCommentId',
|
||||
allowNull: true
|
||||
},
|
||||
as: 'InReplyToVideoComment',
|
||||
onDelete: 'CASCADE'
|
||||
})
|
||||
declare InReplyToVideoComment: Awaited<VideoCommentModel> | null
|
||||
|
||||
@ForeignKey(() => VideoModel)
|
||||
@Column
|
||||
declare videoId: number
|
||||
|
||||
@BelongsTo(() => VideoModel, {
|
||||
foreignKey: {
|
||||
allowNull: false
|
||||
},
|
||||
onDelete: 'CASCADE'
|
||||
})
|
||||
declare Video: Awaited<VideoModel>
|
||||
|
||||
@ForeignKey(() => AccountModel)
|
||||
@Column
|
||||
declare accountId: number
|
||||
|
||||
@BelongsTo(() => AccountModel, {
|
||||
foreignKey: {
|
||||
allowNull: true
|
||||
},
|
||||
onDelete: 'CASCADE'
|
||||
})
|
||||
declare Account: Awaited<AccountModel>
|
||||
|
||||
@HasMany(() => VideoCommentAbuseModel, {
|
||||
foreignKey: {
|
||||
name: 'videoCommentId',
|
||||
allowNull: true
|
||||
},
|
||||
onDelete: 'set null'
|
||||
})
|
||||
declare CommentAbuses: Awaited<VideoCommentAbuseModel>[]
|
||||
|
||||
@HasMany(() => CommentAutomaticTagModel, {
|
||||
foreignKey: 'commentId',
|
||||
onDelete: 'CASCADE'
|
||||
})
|
||||
declare CommentAutomaticTags: Awaited<CommentAutomaticTagModel>[]
|
||||
|
||||
@AfterCreate
|
||||
@AfterDestroy
|
||||
static incrementCommentCount (instance: VideoCommentModel, options: any) {
|
||||
if (instance.heldForReview) return // Don't count held comments
|
||||
|
||||
return afterCommitIfTransaction(options.transaction, () => this.rebuildCommentsCount(instance.videoId))
|
||||
}
|
||||
|
||||
@AfterUpdate
|
||||
static updateCommentCountOnHeldStatusChange (instance: VideoCommentModel, options: any) {
|
||||
return afterCommitIfTransaction(options.transaction, () => this.rebuildCommentsCount(instance.videoId))
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
static async rebuildCommentsCount (videoId: number) {
|
||||
try {
|
||||
await retryTransactionWrapper(() => {
|
||||
return sequelizeTypescript.transaction(async transaction => {
|
||||
const video = await VideoModel.load(videoId, transaction)
|
||||
if (!video) return
|
||||
|
||||
video.comments = await new VideoCommentListQueryBuilder(VideoCommentModel.sequelize, {
|
||||
selectType: 'comment-only',
|
||||
videoId: video.id,
|
||||
heldForReview: false,
|
||||
notDeleted: true,
|
||||
transaction
|
||||
}).count()
|
||||
|
||||
await video.save({ transaction })
|
||||
})
|
||||
})
|
||||
} catch (err) {
|
||||
logger.error('Cannot rebuild comments count for video ' + videoId, { err })
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
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 listForApi (parameters: {
|
||||
start: number
|
||||
count: number
|
||||
sort: string
|
||||
|
||||
autoTagOfAccountId: number
|
||||
|
||||
videoAccountOwnerId?: number
|
||||
videoAccountOwnerIncludeCollaborations?: boolean
|
||||
|
||||
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',
|
||||
'videoAccountOwnerIncludeCollaborations',
|
||||
'videoChannelOwnerId',
|
||||
'heldForReview'
|
||||
]),
|
||||
|
||||
selectType: 'api-list',
|
||||
notDeleted: true
|
||||
}
|
||||
|
||||
return Promise.all([
|
||||
new VideoCommentListQueryBuilder(VideoCommentModel.sequelize, queryOptions).list<MCommentAdminOrUserFormattable>(),
|
||||
new VideoCommentListQueryBuilder(VideoCommentModel.sequelize, queryOptions).count()
|
||||
]).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-video',
|
||||
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).list<MCommentAdminOrUserFormattable>(),
|
||||
new VideoCommentListQueryBuilder(VideoCommentModel.sequelize, countOptions).count(),
|
||||
new VideoCommentListQueryBuilder(VideoCommentModel.sequelize, notDeletedCountOptions).count()
|
||||
]).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-video',
|
||||
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).list<MCommentAdminOrUserFormattable>(),
|
||||
new VideoCommentListQueryBuilder(VideoCommentModel.sequelize, queryOptions).count()
|
||||
]).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).list<MComment>(),
|
||||
new VideoCommentListQueryBuilder(VideoCommentModel.sequelize, queryOptions).count()
|
||||
]).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).list<MCommentOwnerVideoFeed>()
|
||||
}
|
||||
|
||||
static listForBulkDelete (
|
||||
ofAccount: MAccount,
|
||||
filter: {
|
||||
onVideosOfAccount?: MAccountId
|
||||
includeCollaborations?: boolean
|
||||
} = {}
|
||||
) {
|
||||
const queryOptions: ListVideoCommentsOptions = {
|
||||
selectType: 'comment-only',
|
||||
|
||||
accountId: ofAccount.id,
|
||||
videoAccountOwnerId: filter.onVideosOfAccount?.id,
|
||||
videoAccountOwnerIncludeCollaborations: filter.includeCollaborations,
|
||||
|
||||
heldForReview: undefined,
|
||||
|
||||
notDeleted: true,
|
||||
count: 5000
|
||||
}
|
||||
|
||||
return new VideoCommentListQueryBuilder(VideoCommentModel.sequelize, queryOptions).list<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."accountId" = "account"."id" 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
|
||||
}
|
||||
|
||||
isLocal () {
|
||||
if (!this.Account) return false
|
||||
|
||||
return this.Account.isLocal()
|
||||
}
|
||||
|
||||
markAsDeleted () {
|
||||
this.text = ''
|
||||
this.deletedAt = new Date()
|
||||
this.accountId = null
|
||||
}
|
||||
|
||||
isDeleted () {
|
||||
return this.deletedAt !== null
|
||||
}
|
||||
|
||||
extractMentions () {
|
||||
return extractMentions(this.text, this.isLocal())
|
||||
}
|
||||
|
||||
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,
|
||||
|
||||
channel: this.Video.VideoChannel.toFormattedSummaryJSON()
|
||||
},
|
||||
|
||||
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.isLocal() && !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 }))
|
||||
}
|
||||
}
|
||||
676
server/core/models/video/video-file.ts
Normal file
676
server/core/models/video/video-file.ts
Normal file
@@ -0,0 +1,676 @@
|
||||
import {
|
||||
ActivityVideoUrlObject,
|
||||
FileStorage,
|
||||
type FileStorageType,
|
||||
VideoFileFormatFlag,
|
||||
type VideoFileFormatFlagType,
|
||||
VideoFileStream,
|
||||
type VideoFileStreamType,
|
||||
VideoResolution
|
||||
} 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,
|
||||
Is,
|
||||
Scopes,
|
||||
Table,
|
||||
UpdatedAt
|
||||
} from 'sequelize-typescript'
|
||||
import validator from 'validator'
|
||||
import {
|
||||
isVideoFPSResolutionValid,
|
||||
isVideoFileExtnameValid,
|
||||
isVideoFileInfoHashValid,
|
||||
isVideoFileResolutionValid,
|
||||
isVideoFileSizeValid
|
||||
} from '../../helpers/custom-validators/videos.js'
|
||||
import { DOWNLOAD_PATHS, LAZY_STATIC_PATHS, MEMOIZE_LENGTH, MEMOIZE_TTL, STATIC_PATHS, WEBSERVER } from '../../initializers/constants.js'
|
||||
import { MVideoFile, MVideoFileStreamingPlaylistVideo, MVideoFileVideo } from '../../types/models/video/video-file.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: [ '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
|
||||
declare createdAt: Date
|
||||
|
||||
@UpdatedAt
|
||||
declare updatedAt: Date
|
||||
|
||||
@AllowNull(false)
|
||||
@Is('VideoFileResolution', value => throwIfNotValid(value, isVideoFileResolutionValid, 'resolution'))
|
||||
@Column
|
||||
declare resolution: number
|
||||
|
||||
@AllowNull(true)
|
||||
@Column
|
||||
declare width: number
|
||||
|
||||
@AllowNull(true)
|
||||
@Column
|
||||
declare height: number
|
||||
|
||||
@AllowNull(false)
|
||||
@Is('VideoFileSize', value => throwIfNotValid(value, isVideoFileSizeValid, 'size'))
|
||||
@Column(DataType.BIGINT)
|
||||
declare size: number
|
||||
|
||||
@AllowNull(false)
|
||||
@Is('VideoFileExtname', value => throwIfNotValid(value, isVideoFileExtnameValid, 'extname'))
|
||||
@Column
|
||||
declare extname: string
|
||||
|
||||
@AllowNull(true)
|
||||
@Is('VideoFileInfohash', value => throwIfNotValid(value, isVideoFileInfoHashValid, 'info hash', true))
|
||||
@Column
|
||||
declare infoHash: string
|
||||
|
||||
@AllowNull(false)
|
||||
@Default(-1)
|
||||
@Is('VideoFileFPS', value => throwIfNotValid(value, isVideoFPSResolutionValid, 'fps'))
|
||||
@Column
|
||||
declare fps: number
|
||||
|
||||
@AllowNull(false)
|
||||
@Column
|
||||
declare formatFlags: VideoFileFormatFlagType
|
||||
|
||||
@AllowNull(false)
|
||||
@Column
|
||||
declare streams: VideoFileStreamType
|
||||
|
||||
@AllowNull(true)
|
||||
@Column(DataType.JSONB)
|
||||
declare metadata: any
|
||||
|
||||
@AllowNull(true)
|
||||
@Column
|
||||
declare metadataUrl: string
|
||||
|
||||
// Could be null for remote files
|
||||
@AllowNull(true)
|
||||
@Column
|
||||
declare fileUrl: string
|
||||
|
||||
// Could be null for live files
|
||||
@AllowNull(true)
|
||||
@Column
|
||||
declare filename: string
|
||||
|
||||
// Could be null for remote files
|
||||
@AllowNull(true)
|
||||
@Column
|
||||
declare torrentUrl: string
|
||||
|
||||
// Could be null for live files
|
||||
@AllowNull(true)
|
||||
@Column
|
||||
declare torrentFilename: string
|
||||
|
||||
@ForeignKey(() => VideoModel)
|
||||
@Column
|
||||
declare videoId: number
|
||||
|
||||
@AllowNull(false)
|
||||
@Default(FileStorage.FILE_SYSTEM)
|
||||
@Column
|
||||
declare storage: FileStorageType
|
||||
|
||||
@BelongsTo(() => VideoModel, {
|
||||
foreignKey: {
|
||||
allowNull: true
|
||||
},
|
||||
onDelete: 'CASCADE'
|
||||
})
|
||||
declare Video: Awaited<VideoModel>
|
||||
|
||||
@ForeignKey(() => VideoStreamingPlaylistModel)
|
||||
@Column
|
||||
declare videoStreamingPlaylistId: number
|
||||
|
||||
@BelongsTo(() => VideoStreamingPlaylistModel, {
|
||||
foreignKey: {
|
||||
allowNull: true
|
||||
},
|
||||
onDelete: 'CASCADE'
|
||||
})
|
||||
declare VideoStreamingPlaylist: Awaited<VideoStreamingPlaylistModel>
|
||||
|
||||
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 doesOwnedWebVideoFileExist (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<MVideoFile>(id)
|
||||
}
|
||||
|
||||
static loadWithVideo (id: number, transaction?: Transaction) {
|
||||
return VideoFileModel.scope(ScopeNames.WITH_VIDEO).findByPk<MVideoFileVideo>(id, { transaction })
|
||||
}
|
||||
|
||||
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 => {
|
||||
if (!file) return null
|
||||
|
||||
// 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: VideoStreamingPlaylistModel.unscoped(),
|
||||
required: true,
|
||||
where: {
|
||||
id: streamingPlaylistId
|
||||
}
|
||||
}
|
||||
],
|
||||
transaction
|
||||
}
|
||||
|
||||
return VideoFileModel.findAll<MVideoFile>(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
|
||||
}
|
||||
|
||||
hasAudio () {
|
||||
return (this.streams & VideoFileStream.AUDIO) === VideoFileStream.AUDIO
|
||||
}
|
||||
|
||||
hasVideo () {
|
||||
return (this.streams & VideoFileStream.VIDEO) === VideoFileStream.VIDEO
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
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.isLocal()) {
|
||||
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(DOWNLOAD_PATHS.HLS_VIDEOS, `${video.uuid}-${this.resolution}-fragmented${this.extname}`)
|
||||
: join(DOWNLOAD_PATHS.WEB_VIDEOS, `${video.uuid}-${this.resolution}${this.extname}`)
|
||||
|
||||
if (video.isLocal()) return WEBSERVER.URL + path
|
||||
|
||||
// FIXME: don't guess remote URL
|
||||
return buildRemoteUrl(video, path)
|
||||
}
|
||||
|
||||
getRemoteTorrentUrl (video: MVideo) {
|
||||
if (video.isLocal()) 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(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)
|
||||
|
||||
const attachment: ActivityVideoUrlObject['attachment'] = []
|
||||
|
||||
if (this.hasAudio()) {
|
||||
attachment.push({
|
||||
type: 'PropertyValue',
|
||||
name: 'ffprobe_codec_type',
|
||||
value: 'audio'
|
||||
})
|
||||
}
|
||||
|
||||
if (this.hasVideo()) {
|
||||
attachment.push({
|
||||
type: 'PropertyValue',
|
||||
name: 'ffprobe_codec_type',
|
||||
value: 'video'
|
||||
})
|
||||
}
|
||||
|
||||
if (this.formatFlags & VideoFileFormatFlag.FRAGMENTED) {
|
||||
attachment.push({
|
||||
type: 'PropertyValue',
|
||||
name: 'peertube_format_flag',
|
||||
value: 'fragmented'
|
||||
})
|
||||
}
|
||||
|
||||
if (this.formatFlags & VideoFileFormatFlag.WEB_VIDEO) {
|
||||
attachment.push({
|
||||
type: 'PropertyValue',
|
||||
name: 'peertube_format_flag',
|
||||
value: 'web-video'
|
||||
})
|
||||
}
|
||||
|
||||
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,
|
||||
attachment
|
||||
}
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user