2020-12-08 08:30:29 -05:00
|
|
|
import { FindOptions, Includeable, literal, Op, ScopeOptions } from 'sequelize'
|
2017-12-12 11:53:50 -05:00
|
|
|
import {
|
2018-08-16 09:25:20 -04:00
|
|
|
AllowNull,
|
|
|
|
BeforeDestroy,
|
|
|
|
BelongsTo,
|
|
|
|
Column,
|
|
|
|
CreatedAt,
|
|
|
|
DataType,
|
|
|
|
Default,
|
|
|
|
DefaultScope,
|
|
|
|
ForeignKey,
|
2019-04-11 05:33:44 -04:00
|
|
|
HasMany,
|
2018-08-16 09:25:20 -04:00
|
|
|
Is,
|
|
|
|
Model,
|
|
|
|
Scopes,
|
2018-08-23 11:58:39 -04:00
|
|
|
Sequelize,
|
2018-08-16 09:25:20 -04:00
|
|
|
Table,
|
|
|
|
UpdatedAt
|
2017-12-12 11:53:50 -05:00
|
|
|
} from 'sequelize-typescript'
|
2020-12-08 04:53:41 -05:00
|
|
|
import { MAccountActor } from '@server/types/models'
|
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'
|
2018-02-15 08:46:26 -05:00
|
|
|
import {
|
2018-08-16 09:25:20 -04:00
|
|
|
isVideoChannelDescriptionValid,
|
|
|
|
isVideoChannelNameValid,
|
2018-02-15 08:46:26 -05:00
|
|
|
isVideoChannelSupportValid
|
|
|
|
} from '../../helpers/custom-validators/video-channels'
|
2019-04-11 08:26:41 -04:00
|
|
|
import { CONSTRAINTS_FIELDS, WEBSERVER } from '../../initializers/constants'
|
2020-11-10 10:29:35 -05:00
|
|
|
import { sendDeleteActor } from '../../lib/activitypub/send'
|
2019-08-15 05:53:26 -04:00
|
|
|
import {
|
|
|
|
MChannelAccountDefault,
|
|
|
|
MChannelActor,
|
2019-08-21 08:31:57 -04:00
|
|
|
MChannelActorAccountDefaultVideos,
|
|
|
|
MChannelAP,
|
|
|
|
MChannelFormattable,
|
|
|
|
MChannelSummaryFormattable
|
2020-06-18 04:45:25 -04:00
|
|
|
} from '../../types/models/video'
|
2020-11-10 10:29:35 -05:00
|
|
|
import { AccountModel, ScopeNames as AccountModelScopeNames, SummaryOptions as AccountSummaryOptions } from '../account/account'
|
|
|
|
import { ActorModel, unusedActorAttributesForAPI } from '../activitypub/actor'
|
|
|
|
import { ActorFollowModel } from '../activitypub/actor-follow'
|
|
|
|
import { AvatarModel } from '../avatar/avatar'
|
|
|
|
import { ServerModel } from '../server/server'
|
|
|
|
import { buildServerIdsFollowedBy, buildTrigramSearchIndex, createSimilarityAttribute, getSort, throwIfNotValid } from '../utils'
|
|
|
|
import { VideoModel } from './video'
|
|
|
|
import { VideoPlaylistModel } from './video-playlist'
|
2018-08-23 11:58:39 -04:00
|
|
|
|
2019-02-26 04:55:40 -05:00
|
|
|
export enum ScopeNames {
|
2019-08-15 05:53:26 -04:00
|
|
|
FOR_API = 'FOR_API',
|
2020-03-23 05:14:05 -04:00
|
|
|
SUMMARY = 'SUMMARY',
|
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',
|
2020-03-23 05:14:05 -04:00
|
|
|
WITH_STATS = 'WITH_STATS'
|
2017-12-14 04:07:57 -05:00
|
|
|
}
|
|
|
|
|
2018-08-23 11:58:39 -04:00
|
|
|
type AvailableForListOptions = {
|
|
|
|
actorId: number
|
2020-07-15 05:17:03 -04:00
|
|
|
search?: string
|
2018-08-23 11:58:39 -04:00
|
|
|
}
|
|
|
|
|
2020-03-23 05:14:05 -04:00
|
|
|
type AvailableWithStatsOptions = {
|
|
|
|
daysPrior: number
|
|
|
|
}
|
|
|
|
|
2019-07-31 09:57:32 -04:00
|
|
|
export type SummaryOptions = {
|
2020-07-07 08:34:16 -04:00
|
|
|
actorRequired?: boolean // Default: true
|
2019-07-31 09:57:32 -04:00
|
|
|
withAccount?: boolean // Default: false
|
|
|
|
withAccountBlockerIds?: number[]
|
|
|
|
}
|
|
|
|
|
2019-04-23 03:50:57 -04:00
|
|
|
@DefaultScope(() => ({
|
2017-12-14 11:38:41 -05:00
|
|
|
include: [
|
|
|
|
{
|
2019-04-23 03:50:57 -04:00
|
|
|
model: ActorModel,
|
2017-12-14 11:38:41 -05:00
|
|
|
required: true
|
|
|
|
}
|
|
|
|
]
|
2019-04-23 03:50:57 -04:00
|
|
|
}))
|
|
|
|
@Scopes(() => ({
|
2019-08-15 05:53:26 -04:00
|
|
|
[ScopeNames.FOR_API]: (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: {
|
2019-04-18 05:28:17 -04:00
|
|
|
[Op.or]: [
|
2018-06-18 05:34:14 -04:00
|
|
|
{
|
2018-08-23 11:58:39 -04:00
|
|
|
serverId: null
|
|
|
|
},
|
|
|
|
{
|
|
|
|
serverId: {
|
2020-01-31 10:56:52 -05:00
|
|
|
[Op.in]: Sequelize.literal(inQueryInstanceFollow)
|
2018-08-23 11:58:39 -04:00
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
},
|
2020-03-23 05:14:05 -04:00
|
|
|
[ScopeNames.SUMMARY]: (options: SummaryOptions = {}) => {
|
2020-12-08 08:30:29 -05:00
|
|
|
const include: Includeable[] = [
|
|
|
|
{
|
|
|
|
attributes: [ 'id', 'preferredUsername', 'url', 'serverId', 'avatarId' ],
|
|
|
|
model: ActorModel.unscoped(),
|
|
|
|
required: options.actorRequired ?? true,
|
|
|
|
include: [
|
|
|
|
{
|
|
|
|
attributes: [ 'host' ],
|
|
|
|
model: ServerModel.unscoped(),
|
|
|
|
required: false
|
|
|
|
},
|
|
|
|
{
|
|
|
|
model: AvatarModel.unscoped(),
|
|
|
|
required: false
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
2020-03-23 05:14:05 -04:00
|
|
|
const base: FindOptions = {
|
2020-12-08 08:30:29 -05:00
|
|
|
attributes: [ 'id', 'name', 'description', 'actorId' ]
|
2020-03-23 05:14:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (options.withAccount === true) {
|
2020-12-08 08:30:29 -05:00
|
|
|
include.push({
|
2020-03-23 05:14:05 -04:00
|
|
|
model: AccountModel.scope({
|
|
|
|
method: [ AccountModelScopeNames.SUMMARY, { withAccountBlockerIds: options.withAccountBlockerIds } as AccountSummaryOptions ]
|
|
|
|
}),
|
|
|
|
required: true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-12-08 08:30:29 -05:00
|
|
|
base.include = include
|
|
|
|
|
2020-03-23 05:14:05 -04:00
|
|
|
return base
|
|
|
|
},
|
2018-08-23 11:58:39 -04:00
|
|
|
[ScopeNames.WITH_ACCOUNT]: {
|
|
|
|
include: [
|
|
|
|
{
|
2019-04-23 03:50:57 -04:00
|
|
|
model: AccountModel,
|
2018-08-23 11:58:39 -04:00
|
|
|
required: true
|
2017-12-14 04:07:57 -05:00
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
2020-03-23 05:14:05 -04:00
|
|
|
[ScopeNames.WITH_ACTOR]: {
|
2017-12-14 04:07:57 -05:00
|
|
|
include: [
|
2020-03-23 05:14:05 -04:00
|
|
|
ActorModel
|
2017-12-14 04:07:57 -05:00
|
|
|
]
|
2017-12-14 11:38:41 -05:00
|
|
|
},
|
2020-03-23 05:14:05 -04:00
|
|
|
[ScopeNames.WITH_VIDEOS]: {
|
2017-12-14 11:38:41 -05:00
|
|
|
include: [
|
2020-03-23 05:14:05 -04:00
|
|
|
VideoModel
|
2017-12-14 11:38:41 -05:00
|
|
|
]
|
2020-03-23 05:14:05 -04:00
|
|
|
},
|
2020-03-30 06:06:46 -04:00
|
|
|
[ScopeNames.WITH_STATS]: (options: AvailableWithStatsOptions = { daysPrior: 30 }) => {
|
|
|
|
const daysPrior = parseInt(options.daysPrior + '', 10)
|
|
|
|
|
|
|
|
return {
|
|
|
|
attributes: {
|
|
|
|
include: [
|
2020-06-16 08:13:01 -04:00
|
|
|
[
|
|
|
|
literal('(SELECT COUNT(*) FROM "video" WHERE "channelId" = "VideoChannelModel"."id")'),
|
|
|
|
'videosCount'
|
|
|
|
],
|
2020-03-30 06:06:46 -04:00
|
|
|
[
|
|
|
|
literal(
|
|
|
|
'(' +
|
|
|
|
`SELECT string_agg(concat_ws('|', t.day, t.views), ',') ` +
|
|
|
|
'FROM ( ' +
|
|
|
|
'WITH ' +
|
|
|
|
'days AS ( ' +
|
|
|
|
`SELECT generate_series(date_trunc('day', now()) - '${daysPrior} day'::interval, ` +
|
|
|
|
`date_trunc('day', now()), '1 day'::interval) AS day ` +
|
2020-03-23 05:14:05 -04:00
|
|
|
') ' +
|
2020-06-12 10:01:42 -04:00
|
|
|
'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' +
|
2020-03-30 06:06:46 -04:00
|
|
|
')'
|
|
|
|
),
|
|
|
|
'viewsPerDay'
|
|
|
|
]
|
2020-03-23 05:14:05 -04:00
|
|
|
]
|
2020-03-30 06:06:46 -04:00
|
|
|
}
|
2020-03-23 05:14:05 -04:00
|
|
|
}
|
2020-03-30 06:06:46 -04:00
|
|
|
}
|
2019-04-23 03:50:57 -04:00
|
|
|
}))
|
2017-12-12 11:53:50 -05:00
|
|
|
@Table({
|
|
|
|
tableName: 'videoChannel',
|
2020-01-28 08:45:17 -05:00
|
|
|
indexes: [
|
|
|
|
buildTrigramSearchIndex('video_channel_name_trigram', 'name'),
|
|
|
|
|
|
|
|
{
|
|
|
|
fields: [ 'accountId' ]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
fields: [ 'actorId' ]
|
|
|
|
}
|
|
|
|
]
|
2017-12-12 11:53:50 -05:00
|
|
|
})
|
2020-12-08 08:30:29 -05:00
|
|
|
export class VideoChannelModel extends Model {
|
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)
|
2018-02-15 08:46:26 -05:00
|
|
|
@Default(null)
|
2019-04-18 05:28:17 -04:00
|
|
|
@Is('VideoChannelDescription', value => throwIfNotValid(value, isVideoChannelDescriptionValid, 'description', true))
|
2018-05-09 07:32:44 -04:00
|
|
|
@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
|
|
|
|
2018-02-15 08:46:26 -05:00
|
|
|
@AllowNull(true)
|
|
|
|
@Default(null)
|
2019-04-18 05:28:17 -04:00
|
|
|
@Is('VideoChannelSupport', value => throwIfNotValid(value, isVideoChannelSupportValid, 'support', true))
|
2018-05-09 07:32:44 -04:00
|
|
|
@Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_CHANNELS.SUPPORT.max))
|
2018-02-15 08:46:26 -05:00
|
|
|
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-11-27 08:44:51 -05:00
|
|
|
|
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-11-27 08:44:51 -05:00
|
|
|
|
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
|
|
|
|
},
|
2018-01-18 04:53:54 -05:00
|
|
|
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
|
|
|
},
|
2019-03-05 04:58:44 -05:00
|
|
|
onDelete: 'CASCADE',
|
2019-02-26 04:55:40 -05:00
|
|
|
hooks: true
|
|
|
|
})
|
|
|
|
VideoPlaylists: VideoPlaylistModel[]
|
|
|
|
|
2018-01-18 04:53:54 -05:00
|
|
|
@BeforeDestroy
|
|
|
|
static async sendDeleteIfOwned (instance: VideoChannelModel, options) {
|
|
|
|
if (!instance.Actor) {
|
2020-01-08 09:11:38 -05:00
|
|
|
instance.Actor = await instance.$get('Actor', { transaction: options.transaction })
|
2018-01-18 04:53:54 -05:00
|
|
|
}
|
|
|
|
|
2020-11-10 10:29:35 -05:00
|
|
|
await ActorFollowModel.removeFollowsOf(instance.Actor.id, options.transaction)
|
|
|
|
|
2018-07-30 07:39:20 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-07-15 05:17:03 -04:00
|
|
|
static listForApi (parameters: {
|
|
|
|
actorId: number
|
|
|
|
start: number
|
|
|
|
count: number
|
|
|
|
sort: string
|
|
|
|
}) {
|
2020-07-23 15:30:04 -04:00
|
|
|
const { actorId } = parameters
|
2020-07-15 05:17:03 -04:00
|
|
|
|
2017-12-12 11:53:50 -05:00
|
|
|
const query = {
|
2020-07-15 05:17:03 -04:00
|
|
|
offset: parameters.start,
|
|
|
|
limit: parameters.count,
|
|
|
|
order: getSort(parameters.sort)
|
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
|
2020-12-08 08:30:29 -05:00
|
|
|
.scope({
|
|
|
|
method: [ ScopeNames.FOR_API, { actorId } as AvailableForListOptions ]
|
|
|
|
})
|
2018-08-23 11:58:39 -04:00
|
|
|
.findAndCountAll(query)
|
|
|
|
.then(({ rows, count }) => {
|
|
|
|
return { total: count, data: rows }
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-12-08 08:30:29 -05:00
|
|
|
static listLocalsForSitemap (sort: string): Promise<MChannelActor[]> {
|
2018-12-05 11:27:24 -05:00
|
|
|
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: {
|
2019-04-18 05:28:17 -04:00
|
|
|
[Op.or]: [
|
2018-08-28 09:16:04 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return VideoChannelModel
|
2020-12-08 08:30:29 -05:00
|
|
|
.scope({
|
|
|
|
method: [ ScopeNames.FOR_API, { actorId: options.actorId } as AvailableForListOptions ]
|
|
|
|
})
|
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
|
|
|
}
|
|
|
|
|
2019-05-29 09:09:38 -04:00
|
|
|
static listByAccount (options: {
|
2020-01-31 10:56:52 -05:00
|
|
|
accountId: number
|
|
|
|
start: number
|
|
|
|
count: number
|
2019-05-29 09:09:38 -04:00
|
|
|
sort: string
|
2020-03-23 05:14:05 -04:00
|
|
|
withStats?: boolean
|
2020-07-23 15:30:04 -04:00
|
|
|
search?: string
|
2019-05-29 09:09:38 -04:00
|
|
|
}) {
|
2020-07-23 15:30:04 -04:00
|
|
|
const escapedSearch = VideoModel.sequelize.escape(options.search)
|
|
|
|
const escapedLikeSearch = VideoModel.sequelize.escape('%' + options.search + '%')
|
|
|
|
const where = options.search
|
|
|
|
? {
|
|
|
|
[Op.or]: [
|
|
|
|
Sequelize.literal(
|
|
|
|
'lower(immutable_unaccent("VideoChannelModel"."name")) % lower(immutable_unaccent(' + escapedSearch + '))'
|
|
|
|
),
|
|
|
|
Sequelize.literal(
|
|
|
|
'lower(immutable_unaccent("VideoChannelModel"."name")) LIKE lower(immutable_unaccent(' + escapedLikeSearch + '))'
|
|
|
|
)
|
|
|
|
]
|
|
|
|
}
|
|
|
|
: null
|
|
|
|
|
2017-12-12 11:53:50 -05:00
|
|
|
const query = {
|
2019-05-29 09:09:38 -04:00
|
|
|
offset: options.start,
|
|
|
|
limit: options.count,
|
|
|
|
order: getSort(options.sort),
|
2017-12-12 11:53:50 -05:00
|
|
|
include: [
|
|
|
|
{
|
|
|
|
model: AccountModel,
|
|
|
|
where: {
|
2019-05-29 09:09:38 -04:00
|
|
|
id: options.accountId
|
2017-12-12 11:53:50 -05:00
|
|
|
},
|
2017-12-14 11:38:41 -05:00
|
|
|
required: true
|
2017-12-12 11:53:50 -05:00
|
|
|
}
|
2020-07-23 15:30:04 -04:00
|
|
|
],
|
|
|
|
where
|
2017-12-12 11:53:50 -05:00
|
|
|
}
|
2017-10-24 13:41:09 -04:00
|
|
|
|
2020-03-23 05:14:05 -04:00
|
|
|
const scopes: string | ScopeOptions | (string | ScopeOptions)[] = [ ScopeNames.WITH_ACTOR ]
|
|
|
|
|
2020-06-12 10:01:42 -04:00
|
|
|
if (options.withStats === true) {
|
2020-03-23 05:14:05 -04:00
|
|
|
scopes.push({
|
|
|
|
method: [ ScopeNames.WITH_STATS, { daysPrior: 30 } as AvailableWithStatsOptions ]
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-12-14 11:38:41 -05:00
|
|
|
return VideoChannelModel
|
2020-03-23 05:14:05 -04:00
|
|
|
.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
|
|
|
}
|
|
|
|
|
2020-12-08 08:30:29 -05:00
|
|
|
static loadByIdAndPopulateAccount (id: number): Promise<MChannelAccountDefault> {
|
2018-09-04 04:22:10 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-12-08 08:30:29 -05:00
|
|
|
static loadByIdAndAccount (id: number, accountId: number): Promise<MChannelAccountDefault> {
|
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
|
|
|
}
|
|
|
|
|
2020-12-08 08:30:29 -05:00
|
|
|
static loadAndPopulateAccount (id: number): Promise<MChannelAccountDefault> {
|
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
|
|
|
|
2020-12-08 08:30:29 -05:00
|
|
|
static loadByUrlAndPopulateAccount (url: string): Promise<MChannelAccountDefault> {
|
2018-08-23 11:58:39 -04:00
|
|
|
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('@')
|
|
|
|
|
2019-04-11 05:33:44 -04:00
|
|
|
if (!host || host === WEBSERVER.HOST) return VideoChannelModel.loadLocalByNameAndPopulateAccount(name)
|
2019-02-21 08:06:10 -05:00
|
|
|
|
|
|
|
return VideoChannelModel.loadByNameAndHostAndPopulateAccount(name, host)
|
|
|
|
}
|
|
|
|
|
2020-12-08 08:30:29 -05:00
|
|
|
static loadLocalByNameAndPopulateAccount (name: string): Promise<MChannelAccountDefault> {
|
2018-08-17 09:45:42 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-12-08 08:30:29 -05:00
|
|
|
static loadByNameAndHostAndPopulateAccount (name: string, host: string): Promise<MChannelAccountDefault> {
|
2018-08-16 09:25:20 -04:00
|
|
|
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-08-16 09:25:20 -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)
|
|
|
|
}
|
|
|
|
|
2020-12-08 08:30:29 -05:00
|
|
|
static loadAndPopulateAccountAndVideos (id: number): Promise<MChannelActorAccountDefaultVideos> {
|
2018-08-17 09:45:42 -04:00
|
|
|
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)
|
2018-08-16 09:25:20 -04:00
|
|
|
}
|
|
|
|
|
2019-08-20 13:05:31 -04:00
|
|
|
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,
|
|
|
|
avatar: actor.avatar
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
toFormattedJSON (this: MChannelFormattable): VideoChannel {
|
2020-06-16 08:13:01 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2020-03-23 05:14:05 -04:00
|
|
|
|
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,
|
2018-06-13 09:07:25 -04:00
|
|
|
displayName: this.getDisplayName(),
|
2017-12-12 11:53:50 -05:00
|
|
|
description: this.description,
|
2018-02-15 08:46:26 -05:00
|
|
|
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,
|
2020-03-23 05:14:05 -04:00
|
|
|
ownerAccount: undefined,
|
2020-06-16 08:13:01 -04:00
|
|
|
videosCount,
|
|
|
|
viewsPerDay
|
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-08-21 08:31:57 -04:00
|
|
|
toActivityPubObject (this: MChannelAP): ActivityPubActor {
|
2019-08-30 10:50:12 -04:00
|
|
|
const obj = this.Actor.toActivityPubObject(this.name)
|
2017-12-14 11:38:41 -05:00
|
|
|
|
|
|
|
return Object.assign(obj, {
|
|
|
|
summary: this.description,
|
2018-02-15 08:46:26 -05:00
|
|
|
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
|
|
|
}
|
2018-06-13 09:07:25 -04:00
|
|
|
|
2020-12-08 04:53:41 -05:00
|
|
|
getLocalUrl (this: MAccountActor | MChannelActor) {
|
|
|
|
return WEBSERVER.URL + `/video-channels/` + this.Actor.preferredUsername
|
|
|
|
}
|
|
|
|
|
2018-06-13 09:07:25 -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
|
|
|
}
|