X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Fvideo%2Fvideo-channel.ts;h=54f12dce3216273d17925ea1e2eaca21e1f9ebc2;hb=c893d4514e6ecbf282c7985fe5f82b8acd8a1137;hp=183ff34368b05425b8792ed04f41f518bcfb2599;hpb=571389d43b8fc8aaf27e77c06f19b320b08dbbc9;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/video/video-channel.ts b/server/models/video/video-channel.ts index 183ff3436..54f12dce3 100644 --- a/server/models/video/video-channel.ts +++ b/server/models/video/video-channel.ts @@ -1,21 +1,19 @@ import * as Sequelize from 'sequelize' - -import { isVideoChannelNameValid, isVideoChannelDescriptionValid } from '../../helpers' +import { isVideoChannelDescriptionValid, isVideoChannelNameValid } from '../../helpers' +import { CONSTRAINTS_FIELDS } from '../../initializers/constants' +import { sendDeleteVideoChannel } from '../../lib/activitypub/send/send-delete' import { addMethodsToModel, getSort } from '../utils' -import { - VideoChannelInstance, - VideoChannelAttributes, - - VideoChannelMethods -} from './video-channel-interface' +import { VideoChannelAttributes, VideoChannelInstance, VideoChannelMethods } from './video-channel-interface' +import { getAnnounceActivityPubUrl } from '../../lib/activitypub/url' +import { activityPubCollection } from '../../helpers/activitypub' +import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc' let VideoChannel: Sequelize.Model let toFormattedJSON: VideoChannelMethods.ToFormattedJSON let toActivityPubObject: VideoChannelMethods.ToActivityPubObject let isOwned: VideoChannelMethods.IsOwned let countByAccount: VideoChannelMethods.CountByAccount -let listOwned: VideoChannelMethods.ListOwned let listForApi: VideoChannelMethods.ListForApi let listByAccount: VideoChannelMethods.ListByAccount let loadByIdAndAccount: VideoChannelMethods.LoadByIdAndAccount @@ -64,10 +62,13 @@ export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.Da defaultValue: false }, url: { - type: DataTypes.STRING, + type: DataTypes.STRING(CONSTRAINTS_FIELDS.VIDEO_CHANNELS.URL.max), allowNull: false, validate: { - isUrl: true + urlValid: value => { + const res = isActivityPubUrlValid(value) + if (res === false) throw new Error('Video channel URL is not valid.') + } } } }, @@ -88,7 +89,6 @@ export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.Da listForApi, listByAccount, - listOwned, loadByIdAndAccount, loadAndPopulateAccount, loadByUUIDAndPopulateAccount, @@ -141,14 +141,27 @@ toFormattedJSON = function (this: VideoChannelInstance) { } toActivityPubObject = function (this: VideoChannelInstance) { + let sharesObject + if (Array.isArray(this.VideoChannelShares)) { + const shares: string[] = [] + + for (const videoChannelShare of this.VideoChannelShares) { + const shareUrl = getAnnounceActivityPubUrl(this.url, videoChannelShare.Account) + shares.push(shareUrl) + } + + sharesObject = activityPubCollection(shares) + } + const json = { type: 'VideoChannel' as 'VideoChannel', id: this.url, uuid: this.uuid, content: this.description, name: this.name, - published: this.createdAt, - updated: this.updatedAt + published: this.createdAt.toISOString(), + updated: this.updatedAt.toISOString(), + shares: sharesObject } return json @@ -176,11 +189,7 @@ function associate (models) { function afterDestroy (videoChannel: VideoChannelInstance) { if (videoChannel.isOwned()) { - const removeVideoChannelToFriendsParams = { - uuid: videoChannel.uuid - } - - // FIXME: send remove event to followers + return sendDeleteVideoChannel(videoChannel, undefined) } return undefined @@ -196,17 +205,6 @@ countByAccount = function (accountId: number) { return VideoChannel.count(query) } -listOwned = function () { - const query = { - where: { - remote: false - }, - include: [ VideoChannel['sequelize'].models.Account ] - } - - return VideoChannel.findAll(query) -} - listForApi = function (start: number, count: number, sort: string) { const query = { offset: start, @@ -216,7 +214,7 @@ listForApi = function (start: number, count: number, sort: string) { { model: VideoChannel['sequelize'].models.Account, required: true, - include: [ { model: VideoChannel['sequelize'].models.Pod, required: false } ] + include: [ { model: VideoChannel['sequelize'].models.Server, required: false } ] } ] } @@ -236,7 +234,7 @@ listByAccount = function (accountId: number) { id: accountId }, required: true, - include: [ { model: VideoChannel['sequelize'].models.Pod, required: false } ] + include: [ { model: VideoChannel['sequelize'].models.Server, required: false } ] } ] } @@ -262,7 +260,8 @@ loadByUrl = function (url: string, t?: Sequelize.Transaction) { const query: Sequelize.FindOptions = { where: { url - } + }, + include: [ VideoChannel['sequelize'].models.Account ] } if (t !== undefined) query.transaction = t @@ -295,7 +294,7 @@ loadByHostAndUUID = function (fromHost: string, uuid: string, t?: Sequelize.Tran model: VideoChannel['sequelize'].models.Account, include: [ { - model: VideoChannel['sequelize'].models.Pod, + model: VideoChannel['sequelize'].models.Server, required: true, where: { host: fromHost @@ -320,7 +319,7 @@ loadByIdAndAccount = function (id: number, accountId: number) { include: [ { model: VideoChannel['sequelize'].models.Account, - include: [ { model: VideoChannel['sequelize'].models.Pod, required: false } ] + include: [ { model: VideoChannel['sequelize'].models.Server, required: false } ] } ] } @@ -333,7 +332,7 @@ loadAndPopulateAccount = function (id: number) { include: [ { model: VideoChannel['sequelize'].models.Account, - include: [ { model: VideoChannel['sequelize'].models.Pod, required: false } ] + include: [ { model: VideoChannel['sequelize'].models.Server, required: false } ] } ] } @@ -349,7 +348,7 @@ loadByUUIDAndPopulateAccount = function (uuid: string) { include: [ { model: VideoChannel['sequelize'].models.Account, - include: [ { model: VideoChannel['sequelize'].models.Pod, required: false } ] + include: [ { model: VideoChannel['sequelize'].models.Server, required: false } ] } ] } @@ -362,7 +361,7 @@ loadAndPopulateAccountAndVideos = function (id: number) { include: [ { model: VideoChannel['sequelize'].models.Account, - include: [ { model: VideoChannel['sequelize'].models.Pod, required: false } ] + include: [ { model: VideoChannel['sequelize'].models.Server, required: false } ] }, VideoChannel['sequelize'].models.Video ]