X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Fvideo%2Fauthor.ts;h=6f27ea7bde223eaa389990b9e2bd5e7428c71934;hb=72c7248b6fdcdb2175e726ff51b42e7555f2bd84;hp=fd0f44f6b2e74a80699dd4fde49deffcd18524cf;hpb=8113a93a0d9f31aa9e23702bbc31b8a76275ae22;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/video/author.ts b/server/models/video/author.ts index fd0f44f6b..6f27ea7bd 100644 --- a/server/models/video/author.ts +++ b/server/models/video/author.ts @@ -1,6 +1,7 @@ import * as Sequelize from 'sequelize' import { isUserUsernameValid } from '../../helpers' +import { removeVideoAuthorToFriends } from '../../lib' import { addMethodsToModel } from '../utils' import { @@ -11,11 +12,24 @@ import { } from './author-interface' let Author: Sequelize.Model -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('Author', { + uuid: { + type: DataTypes.UUID, + defaultValue: DataTypes.UUIDV4, + allowNull: false, + validate: { + isUUID: 4 + } + }, name: { type: DataTypes.STRING, allowNull: false, @@ -43,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,27 +97,75 @@ function associate (models) { onDelete: 'cascade' }) - Author.hasMany(models.Video, { + Author.hasMany(models.VideoChannel, { foreignKey: { name: 'authorId', allowNull: false }, - onDelete: 'cascade' + onDelete: 'cascade', + hooks: true }) } -findOrCreateAuthor = function (name: string, podId: number, userId: number, transaction: Sequelize.Transaction) { - 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) + } + + return undefined +} + +toAddRemoteJSON = function (this: AuthorInstance) { + const json = { + uuid: this.uuid, + name: this.name + } + + return json +} + +isOwned = function (this: AuthorInstance) { + return this.podId === null +} + +// ------------------------------ STATICS ------------------------------ + +listOwned = function () { + const query: Sequelize.FindOptions = { + where: { + podId: null + } + } + + return Author.findAll(query) +} + +load = function (id: number) { + return Author.findById(id) +} + +loadByUUID = function (uuid: string) { + const query: Sequelize.FindOptions = { + where: { + uuid + } } - const query: Sequelize.FindOrInitializeOptions = { - where: author, - defaults: author, + return Author.findOne(query) +} + +loadAuthorByPodAndUUID = function (uuid: string, podId: number, transaction: Sequelize.Transaction) { + const query: Sequelize.FindOptions = { + where: { + podId, + uuid + }, transaction } - return Author.findOrCreate(query).then(([ authorInstance ]) => authorInstance) + return Author.find(query) }