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

538 lines
13 KiB
TypeScript
Raw Normal View History

2017-12-12 11:53:50 -05:00
import {
AllowNull,
BeforeDestroy,
BelongsTo,
Column,
CreatedAt,
DataType,
Default,
DefaultScope,
ForeignKey,
2019-02-26 04:55:40 -05:00
HasMany, IFindOptions,
Is,
Model,
Scopes,
2018-08-23 11:58:39 -04:00
Sequelize,
Table,
UpdatedAt
2017-12-12 11:53:50 -05:00
} from 'sequelize-typescript'
2017-12-14 11:38:41 -05:00
import { ActivityPubActor } from '../../../shared/models/activitypub'
2019-02-26 04:55:40 -05:00
import { VideoChannel, VideoChannelSummary } from '../../../shared/models/videos'
import {
isVideoChannelDescriptionValid,
isVideoChannelNameValid,
isVideoChannelSupportValid
} from '../../helpers/custom-validators/video-channels'
import { sendDeleteActor } from '../../lib/activitypub/send'
2019-02-26 04:55:40 -05:00
import { AccountModel, ScopeNames as AccountModelScopeNames } from '../account/account'
2018-08-23 11:58:39 -04:00
import { ActorModel, unusedActorAttributesForAPI } from '../activitypub/actor'
2019-02-26 04:55:40 -05:00
import { buildServerIdsFollowedBy, buildTrigramSearchIndex, createSimilarityAttribute, getSort, throwIfNotValid } from '../utils'
2017-12-12 11:53:50 -05:00
import { VideoModel } from './video'
2019-02-21 08:06:10 -05:00
import { CONFIG, CONSTRAINTS_FIELDS } from '../../initializers'
2018-08-17 09:45:42 -04:00
import { ServerModel } from '../server/server'
2018-08-23 11:58:39 -04:00
import { DefineIndexesOptions } from 'sequelize'
2019-02-26 04:55:40 -05:00
import { AvatarModel } from '../avatar/avatar'
import { VideoPlaylistModel } from './video-playlist'
2018-08-23 11:58:39 -04:00
// FIXME: Define indexes here because there is an issue with TS and Sequelize.literal when called directly in the annotation
const indexes: DefineIndexesOptions[] = [
buildTrigramSearchIndex('video_channel_name_trigram', 'name'),
{
fields: [ 'accountId' ]
},
{
fields: [ 'actorId' ]
}
]
2017-12-12 11:53:50 -05:00
2019-02-26 04:55:40 -05:00
export enum ScopeNames {
2018-08-23 11:58:39 -04:00
AVAILABLE_FOR_LIST = 'AVAILABLE_FOR_LIST',
2017-12-14 04:07:57 -05:00
WITH_ACCOUNT = 'WITH_ACCOUNT',
2017-12-14 11:38:41 -05:00
WITH_ACTOR = 'WITH_ACTOR',
2019-02-26 04:55:40 -05:00
WITH_VIDEOS = 'WITH_VIDEOS',
SUMMARY = 'SUMMARY'
2017-12-14 04:07:57 -05:00
}
2018-08-23 11:58:39 -04:00
type AvailableForListOptions = {
actorId: number
}
2017-12-14 11:38:41 -05:00
@DefaultScope({
include: [
{
model: () => ActorModel,
required: true
}
]
})
2017-12-14 04:07:57 -05:00
@Scopes({
2019-02-26 04:55:40 -05:00
[ScopeNames.SUMMARY]: (required: boolean, withAccount: boolean) => {
const base: IFindOptions<VideoChannelModel> = {
attributes: [ 'name', 'description', 'id' ],
include: [
{
attributes: [ 'uuid', 'preferredUsername', 'url', 'serverId', 'avatarId' ],
model: ActorModel.unscoped(),
required: true,
include: [
{
attributes: [ 'host' ],
model: ServerModel.unscoped(),
required: false
},
{
model: AvatarModel.unscoped(),
required: false
}
]
}
]
}
if (withAccount === true) {
base.include.push({
model: AccountModel.scope(AccountModelScopeNames.SUMMARY),
required: true
})
}
2018-08-23 11:58:39 -04:00
2019-02-26 04:55:40 -05:00
return base
},
[ScopeNames.AVAILABLE_FOR_LIST]: (options: AvailableForListOptions) => {
2018-08-23 11:58:39 -04:00
// Only list local channels OR channels that are on an instance followed by actorId
2019-02-26 04:55:40 -05:00
const inQueryInstanceFollow = buildServerIdsFollowedBy(options.actorId)
2018-08-23 11:58:39 -04:00
return {
include: [
{
attributes: {
exclude: unusedActorAttributesForAPI
},
model: ActorModel,
where: {
[Sequelize.Op.or]: [
2018-06-18 05:34:14 -04:00
{
2018-08-23 11:58:39 -04:00
serverId: null
},
{
serverId: {
[ Sequelize.Op.in ]: Sequelize.literal(inQueryInstanceFollow)
}
2018-06-18 05:34:14 -04:00
}
]
2017-12-14 11:38:41 -05:00
}
2018-08-23 11:58:39 -04:00
},
{
model: AccountModel,
required: true,
include: [
{
attributes: {
exclude: unusedActorAttributesForAPI
},
model: ActorModel, // Default scope includes avatar and server
required: true
}
]
}
]
}
},
[ScopeNames.WITH_ACCOUNT]: {
include: [
{
model: () => AccountModel,
required: true
2017-12-14 04:07:57 -05:00
}
]
},
[ScopeNames.WITH_VIDEOS]: {
include: [
() => VideoModel
]
2017-12-14 11:38:41 -05:00
},
[ScopeNames.WITH_ACTOR]: {
include: [
() => ActorModel
]
2017-12-14 04:07:57 -05:00
}
})
2017-12-12 11:53:50 -05:00
@Table({
tableName: 'videoChannel',
2018-08-23 11:58:39 -04:00
indexes
2017-12-12 11:53:50 -05:00
})
export class VideoChannelModel extends Model<VideoChannelModel> {
2017-10-24 13:41:09 -04:00
2017-12-12 11:53:50 -05:00
@AllowNull(false)
@Is('VideoChannelName', value => throwIfNotValid(value, isVideoChannelNameValid, 'name'))
@Column
name: string
2017-10-24 13:41:09 -04:00
2017-12-12 11:53:50 -05:00
@AllowNull(true)
@Default(null)
2017-12-12 11:53:50 -05:00
@Is('VideoChannelDescription', value => throwIfNotValid(value, isVideoChannelDescriptionValid, 'description'))
@Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_CHANNELS.DESCRIPTION.max))
2017-12-12 11:53:50 -05:00
description: string
2017-10-24 13:41:09 -04:00
@AllowNull(true)
@Default(null)
@Is('VideoChannelSupport', value => throwIfNotValid(value, isVideoChannelSupportValid, 'support'))
@Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_CHANNELS.SUPPORT.max))
support: string
2017-12-12 11:53:50 -05:00
@CreatedAt
createdAt: Date
2017-10-24 13:41:09 -04:00
2017-12-12 11:53:50 -05:00
@UpdatedAt
updatedAt: Date
2017-12-14 05:18:49 -05:00
@ForeignKey(() => ActorModel)
@Column
actorId: number
@BelongsTo(() => ActorModel, {
foreignKey: {
allowNull: false
},
onDelete: 'cascade'
})
Actor: ActorModel
2017-12-12 11:53:50 -05:00
@ForeignKey(() => AccountModel)
@Column
accountId: number
2017-12-12 11:53:50 -05:00
@BelongsTo(() => AccountModel, {
foreignKey: {
allowNull: false
},
2018-04-25 04:21:38 -04:00
hooks: true
2017-12-12 11:53:50 -05:00
})
Account: AccountModel
2017-10-24 13:41:09 -04:00
2017-12-12 11:53:50 -05:00
@HasMany(() => VideoModel, {
2017-10-24 13:41:09 -04:00
foreignKey: {
2017-12-12 11:53:50 -05:00
name: 'channelId',
2017-10-24 13:41:09 -04:00
allowNull: false
},
onDelete: 'CASCADE',
hooks: true
2017-10-24 13:41:09 -04:00
})
2017-12-12 11:53:50 -05:00
Videos: VideoModel[]
2017-10-24 13:41:09 -04:00
2019-02-26 04:55:40 -05:00
@HasMany(() => VideoPlaylistModel, {
foreignKey: {
2019-02-28 05:14:26 -05:00
allowNull: true
2019-02-26 04:55:40 -05:00
},
onDelete: 'cascade',
hooks: true
})
VideoPlaylists: VideoPlaylistModel[]
@BeforeDestroy
static async sendDeleteIfOwned (instance: VideoChannelModel, options) {
if (!instance.Actor) {
instance.Actor = await instance.$get('Actor', { transaction: options.transaction }) as ActorModel
}
if (instance.Actor.isOwned()) {
return sendDeleteActor(instance.Actor, options.transaction)
}
return undefined
2017-12-12 11:53:50 -05:00
}
2017-10-24 13:41:09 -04:00
2017-12-12 11:53:50 -05:00
static countByAccount (accountId: number) {
const query = {
where: {
accountId
}
2017-10-24 13:41:09 -04:00
}
2017-12-12 11:53:50 -05:00
return VideoChannelModel.count(query)
2017-10-24 13:41:09 -04:00
}
2018-08-23 11:58:39 -04:00
static listForApi (actorId: number, start: number, count: number, sort: string) {
2017-12-12 11:53:50 -05:00
const query = {
offset: start,
limit: count,
2018-02-19 03:41:03 -05:00
order: getSort(sort)
2017-12-12 11:53:50 -05:00
}
2017-10-24 13:41:09 -04:00
2018-08-23 11:58:39 -04:00
const scopes = {
method: [ ScopeNames.AVAILABLE_FOR_LIST, { actorId } as AvailableForListOptions ]
}
2017-12-14 11:38:41 -05:00
return VideoChannelModel
2018-08-23 11:58:39 -04:00
.scope(scopes)
.findAndCountAll(query)
.then(({ rows, count }) => {
return { total: count, data: rows }
})
}
2018-12-05 11:27:24 -05:00
static listLocalsForSitemap (sort: string) {
const query = {
attributes: [ ],
offset: 0,
order: getSort(sort),
include: [
{
attributes: [ 'preferredUsername', 'serverId' ],
model: ActorModel.unscoped(),
where: {
serverId: null
}
}
]
}
return VideoChannelModel
.unscoped()
.findAll(query)
}
2018-08-23 11:58:39 -04:00
static searchForApi (options: {
actorId: number
search: string
start: number
count: number
sort: string
}) {
const attributesInclude = []
const escapedSearch = VideoModel.sequelize.escape(options.search)
const escapedLikeSearch = VideoModel.sequelize.escape('%' + options.search + '%')
attributesInclude.push(createSimilarityAttribute('VideoChannelModel.name', options.search))
const query = {
attributes: {
include: attributesInclude
},
offset: options.start,
limit: options.count,
order: getSort(options.sort),
where: {
2018-08-28 09:16:04 -04:00
[Sequelize.Op.or]: [
Sequelize.literal(
'lower(immutable_unaccent("VideoChannelModel"."name")) % lower(immutable_unaccent(' + escapedSearch + '))'
),
Sequelize.literal(
'lower(immutable_unaccent("VideoChannelModel"."name")) LIKE lower(immutable_unaccent(' + escapedLikeSearch + '))'
2018-08-23 11:58:39 -04:00
)
2018-08-28 09:16:04 -04:00
]
2018-08-23 11:58:39 -04:00
}
}
const scopes = {
method: [ ScopeNames.AVAILABLE_FOR_LIST, { actorId: options.actorId } as AvailableForListOptions ]
}
return VideoChannelModel
.scope(scopes)
2017-12-14 11:38:41 -05:00
.findAndCountAll(query)
2017-12-12 11:53:50 -05:00
.then(({ rows, count }) => {
return { total: count, data: rows }
})
2017-10-24 13:41:09 -04:00
}
2017-12-12 11:53:50 -05:00
static listByAccount (accountId: number) {
const query = {
2018-02-19 03:41:03 -05:00
order: getSort('createdAt'),
2017-12-12 11:53:50 -05:00
include: [
{
model: AccountModel,
where: {
id: accountId
},
2017-12-14 11:38:41 -05:00
required: true
2017-12-12 11:53:50 -05:00
}
]
}
2017-10-24 13:41:09 -04:00
2017-12-14 11:38:41 -05:00
return VideoChannelModel
.findAndCountAll(query)
2017-12-12 11:53:50 -05:00
.then(({ rows, count }) => {
return { total: count, data: rows }
})
2017-10-24 13:41:09 -04:00
}
2018-09-04 04:22:10 -04:00
static loadByIdAndPopulateAccount (id: number) {
return VideoChannelModel.unscoped()
.scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
2019-02-21 08:28:06 -05:00
.findByPk(id)
2018-09-04 04:22:10 -04:00
}
2017-12-12 11:53:50 -05:00
static loadByIdAndAccount (id: number, accountId: number) {
2018-08-17 09:45:42 -04:00
const query = {
2017-12-12 11:53:50 -05:00
where: {
id,
accountId
2017-12-14 04:07:57 -05:00
}
2017-11-10 11:27:49 -05:00
}
2017-12-12 11:53:50 -05:00
2018-09-04 04:22:10 -04:00
return VideoChannelModel.unscoped()
2017-12-14 11:38:41 -05:00
.scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
2018-08-17 09:45:42 -04:00
.findOne(query)
2017-11-10 08:34:45 -05:00
}
2017-12-12 11:53:50 -05:00
static loadAndPopulateAccount (id: number) {
2018-09-04 04:22:10 -04:00
return VideoChannelModel.unscoped()
2017-12-14 11:38:41 -05:00
.scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
2019-02-21 08:28:06 -05:00
.findByPk(id)
2017-12-12 11:53:50 -05:00
}
2017-11-10 08:34:45 -05:00
2017-12-12 11:53:50 -05:00
static loadByUUIDAndPopulateAccount (uuid: string) {
2018-08-17 09:45:42 -04:00
const query = {
2017-12-14 11:38:41 -05:00
include: [
{
model: ActorModel,
required: true,
where: {
uuid
}
}
]
2017-12-12 11:53:50 -05:00
}
2017-12-14 11:38:41 -05:00
return VideoChannelModel
2018-08-23 11:58:39 -04:00
.scope([ ScopeNames.WITH_ACCOUNT ])
.findOne(query)
}
static loadByUrlAndPopulateAccount (url: string) {
const query = {
include: [
{
model: ActorModel,
required: true,
where: {
url
}
}
]
}
return VideoChannelModel
.scope([ ScopeNames.WITH_ACCOUNT ])
2018-08-17 09:45:42 -04:00
.findOne(query)
2017-10-24 13:41:09 -04:00
}
2019-02-21 08:06:10 -05:00
static loadByNameWithHostAndPopulateAccount (nameWithHost: string) {
const [ name, host ] = nameWithHost.split('@')
if (!host || host === CONFIG.WEBSERVER.HOST) return VideoChannelModel.loadLocalByNameAndPopulateAccount(name)
return VideoChannelModel.loadByNameAndHostAndPopulateAccount(name, host)
}
2018-08-17 09:45:42 -04:00
static loadLocalByNameAndPopulateAccount (name: string) {
const query = {
2017-12-12 11:53:50 -05:00
include: [
2018-08-17 09:45:42 -04:00
{
model: ActorModel,
required: true,
where: {
preferredUsername: name,
serverId: null
}
}
2017-12-12 11:53:50 -05:00
]
}
2017-10-24 13:41:09 -04:00
2018-09-04 04:22:10 -04:00
return VideoChannelModel.unscoped()
2018-08-17 09:45:42 -04:00
.scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
.findOne(query)
2017-10-24 13:41:09 -04:00
}
2018-08-17 09:45:42 -04:00
static loadByNameAndHostAndPopulateAccount (name: string, host: string) {
const query = {
include: [
{
model: ActorModel,
required: true,
where: {
2018-08-17 09:45:42 -04:00
preferredUsername: name
},
include: [
{
model: ServerModel,
required: true,
where: { host }
}
]
}
]
}
2018-09-04 04:22:10 -04:00
return VideoChannelModel.unscoped()
2018-08-17 09:45:42 -04:00
.scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ])
.findOne(query)
}
static loadAndPopulateAccountAndVideos (id: number) {
const options = {
include: [
VideoModel
]
}
2018-09-04 04:22:10 -04:00
return VideoChannelModel.unscoped()
2018-08-17 09:45:42 -04:00
.scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_VIDEOS ])
2019-02-21 08:28:06 -05:00
.findByPk(id, options)
}
toFormattedJSON (): VideoChannel {
2017-12-14 11:38:41 -05:00
const actor = this.Actor.toFormattedJSON()
2018-04-25 04:21:38 -04:00
const videoChannel = {
2017-12-12 11:53:50 -05:00
id: this.id,
displayName: this.getDisplayName(),
2017-12-12 11:53:50 -05:00
description: this.description,
support: this.support,
2017-12-14 11:38:41 -05:00
isLocal: this.Actor.isOwned(),
2017-12-12 11:53:50 -05:00
createdAt: this.createdAt,
2018-04-25 04:21:38 -04:00
updatedAt: this.updatedAt,
ownerAccount: undefined
2018-04-25 04:21:38 -04:00
}
2018-05-23 05:38:00 -04:00
if (this.Account) videoChannel.ownerAccount = this.Account.toFormattedJSON()
2017-10-24 13:41:09 -04:00
2018-04-25 04:21:38 -04:00
return Object.assign(actor, videoChannel)
2017-10-24 13:41:09 -04:00
}
2019-02-26 04:55:40 -05:00
toFormattedSummaryJSON (): VideoChannelSummary {
const actor = this.Actor.toFormattedJSON()
return {
id: this.id,
uuid: actor.uuid,
name: actor.name,
displayName: this.getDisplayName(),
url: actor.url,
host: actor.host,
avatar: actor.avatar
}
}
2017-12-14 11:38:41 -05:00
toActivityPubObject (): ActivityPubActor {
const obj = this.Actor.toActivityPubObject(this.name, 'VideoChannel')
return Object.assign(obj, {
summary: this.description,
support: this.support,
2017-12-14 11:38:41 -05:00
attributedTo: [
{
type: 'Person' as 'Person',
id: this.Account.Actor.url
}
]
})
2017-10-24 13:41:09 -04:00
}
getDisplayName () {
return this.name
}
2019-01-14 05:30:15 -05:00
isOutdated () {
return this.Actor.isOutdated()
}
2017-10-24 13:41:09 -04:00
}