]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/author.js
Fix david-dm badge links
[github/Chocobozzz/PeerTube.git] / server / models / author.js
CommitLineData
67bf9b96
C
1'use strict'
2
3const customUsersValidators = require('../helpers/custom-validators').users
4
feb4bdfd
C
5module.exports = function (sequelize, DataTypes) {
6 const Author = sequelize.define('Author',
7 {
8 name: {
67bf9b96
C
9 type: DataTypes.STRING,
10 allowNull: false,
11 validate: {
12 usernameValid: function (value) {
13 const res = customUsersValidators.isUserUsernameValid(value)
14 if (res === false) throw new Error('Username is not valid.')
15 }
16 }
feb4bdfd
C
17 }
18 },
19 {
319d072e
C
20 indexes: [
21 {
22 fields: [ 'name' ]
23 },
24 {
25 fields: [ 'podId' ]
4712081f
C
26 },
27 {
5d67f289
C
28 fields: [ 'userId' ],
29 unique: true
30 },
31 {
32 fields: [ 'name', 'podId' ],
33 unique: true
319d072e
C
34 }
35 ],
feb4bdfd 36 classMethods: {
4ff0d862
C
37 associate,
38
39 findOrCreateAuthor
feb4bdfd
C
40 }
41 }
42 )
43
44 return Author
45}
46
47// ---------------------------------------------------------------------------
48
49function associate (models) {
50 this.belongsTo(models.Pod, {
51 foreignKey: {
52 name: 'podId',
53 allowNull: true
54 },
55 onDelete: 'cascade'
56 })
4712081f
C
57
58 this.belongsTo(models.User, {
59 foreignKey: {
60 name: 'userId',
61 allowNull: true
62 },
63 onDelete: 'cascade'
64 })
feb4bdfd 65}
4ff0d862
C
66
67function findOrCreateAuthor (name, podId, userId, transaction, callback) {
68 if (!callback) {
69 callback = transaction
70 transaction = null
71 }
72
73 const author = {
74 name,
75 podId,
76 userId
77 }
78
79 const query = {
80 where: author,
81 defaults: author
82 }
83
84 if (transaction) query.transaction = transaction
85
86 this.findOrCreate(query).asCallback(function (err, result) {
ad4a8a1c
C
87 if (err) return callback(err)
88
4ff0d862 89 // [ instance, wasCreated ]
ad4a8a1c 90 return callback(null, result[0])
4ff0d862
C
91 })
92}