1
0
Fork 0
peertube/server/models/video/author.ts

172 lines
3.5 KiB
TypeScript
Raw Normal View History

2017-05-22 14:58:25 -04:00
import * as Sequelize from 'sequelize'
2017-06-16 03:45:46 -04:00
import { isUserUsernameValid } from '../../helpers'
2017-10-24 13:41:09 -04:00
import { removeVideoAuthorToFriends } from '../../lib'
2016-12-28 09:49:23 -05:00
2017-06-16 03:45:46 -04:00
import { addMethodsToModel } from '../utils'
2017-05-22 14:58:25 -04:00
import {
AuthorInstance,
AuthorAttributes,
AuthorMethods
} from './author-interface'
let Author: Sequelize.Model<AuthorInstance, AuthorAttributes>
2017-10-24 13:41:09 -04:00
let loadAuthorByPodAndUUID: AuthorMethods.LoadAuthorByPodAndUUID
let load: AuthorMethods.Load
let loadByUUID: AuthorMethods.LoadByUUID
let listOwned: AuthorMethods.ListOwned
let isOwned: AuthorMethods.IsOwned
let toAddRemoteJSON: AuthorMethods.ToAddRemoteJSON
2017-05-22 14:58:25 -04:00
2017-06-11 11:35:32 -04:00
export default function defineAuthor (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
2017-05-22 14:58:25 -04:00
Author = sequelize.define<AuthorInstance, AuthorAttributes>('Author',
2016-12-11 15:50:51 -05:00
{
2017-10-24 13:41:09 -04:00
uuid: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
allowNull: false,
validate: {
isUUID: 4
}
},
2016-12-11 15:50:51 -05:00
name: {
2016-12-28 09:49:23 -05:00
type: DataTypes.STRING,
allowNull: false,
validate: {
2017-07-11 11:04:57 -04:00
usernameValid: value => {
2017-05-15 16:22:03 -04:00
const res = isUserUsernameValid(value)
2016-12-28 09:49:23 -05:00
if (res === false) throw new Error('Username is not valid.')
}
}
2016-12-11 15:50:51 -05:00
}
},
{
2016-12-29 03:33:28 -05:00
indexes: [
{
fields: [ 'name' ]
},
{
fields: [ 'podId' ]
},
{
2017-02-16 13:24:34 -05:00
fields: [ 'userId' ],
unique: true
},
{
fields: [ 'name', 'podId' ],
unique: true
2016-12-29 03:33:28 -05:00
}
2017-10-24 13:41:09 -04:00
],
hooks: { afterDestroy }
2016-12-11 15:50:51 -05:00
}
)
2017-10-24 13:41:09 -04:00
const classMethods = [
associate,
loadAuthorByPodAndUUID,
load,
loadByUUID,
listOwned
]
const instanceMethods = [
isOwned,
toAddRemoteJSON
]
addMethodsToModel(Author, classMethods, instanceMethods)
2017-05-22 14:58:25 -04:00
2016-12-11 15:50:51 -05:00
return Author
}
// ---------------------------------------------------------------------------
function associate (models) {
2017-05-22 14:58:25 -04:00
Author.belongsTo(models.Pod, {
2016-12-11 15:50:51 -05:00
foreignKey: {
name: 'podId',
allowNull: true
},
onDelete: 'cascade'
})
2017-05-22 14:58:25 -04:00
Author.belongsTo(models.User, {
foreignKey: {
name: 'userId',
allowNull: true
},
onDelete: 'cascade'
})
2017-09-05 16:09:16 -04:00
2017-10-24 13:41:09 -04:00
Author.hasMany(models.VideoChannel, {
2017-09-05 16:09:16 -04:00
foreignKey: {
name: 'authorId',
allowNull: false
},
2017-10-24 13:41:09 -04:00
onDelete: 'cascade',
hooks: true
2017-09-05 16:09:16 -04:00
})
2016-12-11 15:50:51 -05:00
}
2016-12-29 12:02:03 -05:00
function afterDestroy (author: AuthorInstance) {
2017-10-24 13:41:09 -04:00
if (author.isOwned()) {
const removeVideoAuthorToFriendsParams = {
uuid: author.uuid
}
return removeVideoAuthorToFriends(removeVideoAuthorToFriendsParams)
2017-10-24 13:41:09 -04:00
}
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<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
}
2016-12-29 12:02:03 -05:00
}
2017-10-24 13:41:09 -04:00
return Author.findOne(query)
}
loadAuthorByPodAndUUID = function (uuid: string, podId: number, transaction: Sequelize.Transaction) {
const query: Sequelize.FindOptions<AuthorAttributes> = {
where: {
podId,
uuid
},
transaction
2016-12-29 12:02:03 -05:00
}
2017-10-24 13:41:09 -04:00
return Author.find(query)
2016-12-29 12:02:03 -05:00
}