]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/video/video-channel.ts
Fix video upload and videos list
[github/Chocobozzz/PeerTube.git] / server / models / video / video-channel.ts
index c17828f3e84963e2d38a3ed6cf6004e8cf106097..3cb4a33b907cd90359666efc77cd7099ec288593 100644 (file)
@@ -1,7 +1,6 @@
 import * as Sequelize from 'sequelize'
 
 import { isVideoChannelNameValid, isVideoChannelDescriptionValid } from '../../helpers'
-import { removeVideoChannelToFriends } from '../../lib'
 
 import { addMethodsToModel, getSort } from '../utils'
 import {
@@ -10,6 +9,9 @@ import {
 
   VideoChannelMethods
 } from './video-channel-interface'
+import { sendDeleteVideoChannel } from '../../lib/activitypub/send-request'
+import { isVideoChannelUrlValid } from '../../helpers/custom-validators/video-channels'
+import { CONSTRAINTS_FIELDS } from '../../initializers/constants'
 
 let VideoChannel: Sequelize.Model<VideoChannelInstance, VideoChannelAttributes>
 let toFormattedJSON: VideoChannelMethods.ToFormattedJSON
@@ -25,6 +27,8 @@ let loadAndPopulateAccount: VideoChannelMethods.LoadAndPopulateAccount
 let loadByUUIDAndPopulateAccount: VideoChannelMethods.LoadByUUIDAndPopulateAccount
 let loadByHostAndUUID: VideoChannelMethods.LoadByHostAndUUID
 let loadAndPopulateAccountAndVideos: VideoChannelMethods.LoadAndPopulateAccountAndVideos
+let loadByUrl: VideoChannelMethods.LoadByUrl
+let loadByUUIDOrUrl: VideoChannelMethods.LoadByUUIDOrUrl
 
 export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
   VideoChannel = sequelize.define<VideoChannelInstance, VideoChannelAttributes>('VideoChannel',
@@ -63,10 +67,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 = isVideoChannelUrlValid(value)
+            if (res === false) throw new Error('Video channel URL is not valid.')
+          }
         }
       }
     },
@@ -94,12 +101,14 @@ export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.Da
     loadByUUID,
     loadByHostAndUUID,
     loadAndPopulateAccountAndVideos,
-    countByAccount
+    countByAccount,
+    loadByUrl,
+    loadByUUIDOrUrl
   ]
   const instanceMethods = [
     isOwned,
     toFormattedJSON,
-    toActivityPubObject,
+    toActivityPubObject
   ]
   addMethodsToModel(VideoChannel, classMethods, instanceMethods)
 
@@ -139,12 +148,13 @@ toFormattedJSON = function (this: VideoChannelInstance) {
 
 toActivityPubObject = function (this: VideoChannelInstance) {
   const json = {
+    type: 'VideoChannel' as 'VideoChannel',
+    id: this.url,
     uuid: this.uuid,
+    content: this.description,
     name: this.name,
-    description: this.description,
-    createdAt: this.createdAt,
-    updatedAt: this.updatedAt,
-    ownerUUID: this.Account.uuid
+    published: this.createdAt,
+    updated: this.updatedAt
   }
 
   return json
@@ -172,11 +182,7 @@ function associate (models) {
 
 function afterDestroy (videoChannel: VideoChannelInstance) {
   if (videoChannel.isOwned()) {
-    const removeVideoChannelToFriendsParams = {
-      uuid: videoChannel.uuid
-    }
-
-    return removeVideoChannelToFriends(removeVideoChannelToFriendsParams)
+    return sendDeleteVideoChannel(videoChannel, undefined)
   }
 
   return undefined
@@ -212,7 +218,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 } ]
       }
     ]
   }
@@ -232,7 +238,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 } ]
       }
     ]
   }
@@ -254,6 +260,33 @@ loadByUUID = function (uuid: string, t?: Sequelize.Transaction) {
   return VideoChannel.findOne(query)
 }
 
+loadByUrl = function (url: string, t?: Sequelize.Transaction) {
+  const query: Sequelize.FindOptions<VideoChannelAttributes> = {
+    where: {
+      url
+    }
+  }
+
+  if (t !== undefined) query.transaction = t
+
+  return VideoChannel.findOne(query)
+}
+
+loadByUUIDOrUrl = function (uuid: string, url: string, t?: Sequelize.Transaction) {
+  const query: Sequelize.FindOptions<VideoChannelAttributes> = {
+    where: {
+      [Sequelize.Op.or]: [
+        { uuid },
+        { url }
+      ]
+    }
+  }
+
+  if (t !== undefined) query.transaction = t
+
+  return VideoChannel.findOne(query)
+}
+
 loadByHostAndUUID = function (fromHost: string, uuid: string, t?: Sequelize.Transaction) {
   const query: Sequelize.FindOptions<VideoChannelAttributes> = {
     where: {
@@ -264,7 +297,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
@@ -289,7 +322,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 } ]
       }
     ]
   }
@@ -302,7 +335,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 } ]
       }
     ]
   }
@@ -318,7 +351,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 } ]
       }
     ]
   }
@@ -331,7 +364,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
     ]