]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/video/author.ts
Fix lint
[github/Chocobozzz/PeerTube.git] / server / models / video / author.ts
index 4a115e328fe55836b39719d1eb90ccc08c102253..6f27ea7bde223eaa389990b9e2bd5e7428c71934 100644 (file)
@@ -1,10 +1,10 @@
 import * as Sequelize from 'sequelize'
 
 import { isUserUsernameValid } from '../../helpers'
+import { removeVideoAuthorToFriends } from '../../lib'
 
 import { addMethodsToModel } from '../utils'
 import {
-  AuthorClass,
   AuthorInstance,
   AuthorAttributes,
 
@@ -12,16 +12,29 @@ import {
 } from './author-interface'
 
 let Author: Sequelize.Model<AuthorInstance, AuthorAttributes>
-let findOrCreateAuthor: AuthorMethods.FindOrCreateAuthor
+let loadAuthorByPodAndUUID: AuthorMethods.LoadAuthorByPodAndUUID
+let load: AuthorMethods.Load
+let loadByUUID: AuthorMethods.LoadByUUID
+let listOwned: AuthorMethods.ListOwned
+let isOwned: AuthorMethods.IsOwned
+let toAddRemoteJSON: AuthorMethods.ToAddRemoteJSON
 
 export default function defineAuthor (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
   Author = sequelize.define<AuthorInstance, AuthorAttributes>('Author',
     {
+      uuid: {
+        type: DataTypes.UUID,
+        defaultValue: DataTypes.UUIDV4,
+        allowNull: false,
+        validate: {
+          isUUID: 4
+        }
+      },
       name: {
         type: DataTypes.STRING,
         allowNull: false,
         validate: {
-          usernameValid: function (value) {
+          usernameValid: value => {
             const res = isUserUsernameValid(value)
             if (res === false) throw new Error('Username is not valid.')
           }
@@ -44,12 +57,23 @@ export default function defineAuthor (sequelize: Sequelize.Sequelize, DataTypes:
           fields: [ 'name', 'podId' ],
           unique: true
         }
-      ]
+      ],
+      hooks: { afterDestroy }
     }
   )
 
-  const classMethods = [ associate, findOrCreateAuthor ]
-  addMethodsToModel(Author, classMethods)
+  const classMethods = [
+    associate,
+    loadAuthorByPodAndUUID,
+    load,
+    loadByUUID,
+    listOwned
+  ]
+  const instanceMethods = [
+    isOwned,
+    toAddRemoteJSON
+  ]
+  addMethodsToModel(Author, classMethods, instanceMethods)
 
   return Author
 }
@@ -72,32 +96,76 @@ function associate (models) {
     },
     onDelete: 'cascade'
   })
+
+  Author.hasMany(models.VideoChannel, {
+    foreignKey: {
+      name: 'authorId',
+      allowNull: false
+    },
+    onDelete: 'cascade',
+    hooks: true
+  })
 }
 
-findOrCreateAuthor = function (
-  name: string,
-  podId: number,
-  userId: number,
-  transaction: Sequelize.Transaction,
-  callback: AuthorMethods.FindOrCreateAuthorCallback
-) {
-  const author = {
-    name,
-    podId,
-    userId
+function afterDestroy (author: AuthorInstance, options: { transaction: Sequelize.Transaction }) {
+  if (author.isOwned()) {
+    const removeVideoAuthorToFriendsParams = {
+      uuid: author.uuid
+    }
+
+    return removeVideoAuthorToFriends(removeVideoAuthorToFriendsParams, options.transaction)
   }
 
-  const query: any = {
-    where: author,
-    defaults: author
+  return undefined
+}
+
+toAddRemoteJSON = function (this: AuthorInstance) {
+  const json = {
+    uuid: this.uuid,
+    name: this.name
   }
 
-  if (transaction !== null) query.transaction = transaction
+  return json
+}
 
-  Author.findOrCreate(query).asCallback(function (err, result) {
-    if (err) return callback(err)
+isOwned = function (this: AuthorInstance) {
+  return this.podId === null
+}
 
-    // [ instance, wasCreated ]
-    return callback(null, result[0])
-  })
+// ------------------------------ STATICS ------------------------------
+
+listOwned = function () {
+  const query: Sequelize.FindOptions<AuthorAttributes> = {
+    where: {
+      podId: null
+    }
+  }
+
+  return Author.findAll(query)
+}
+
+load = function (id: number) {
+  return Author.findById(id)
+}
+
+loadByUUID = function (uuid: string) {
+  const query: Sequelize.FindOptions<AuthorAttributes> = {
+    where: {
+      uuid
+    }
+  }
+
+  return Author.findOne(query)
+}
+
+loadAuthorByPodAndUUID = function (uuid: string, podId: number, transaction: Sequelize.Transaction) {
+  const query: Sequelize.FindOptions<AuthorAttributes> = {
+    where: {
+      podId,
+      uuid
+    },
+    transaction
+  }
+
+  return Author.find(query)
 }