]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/video/author.ts
Add video privacy setting
[github/Chocobozzz/PeerTube.git] / server / models / video / author.ts
CommitLineData
e02643f3
C
1import * as Sequelize from 'sequelize'
2
74889a71 3import { isUserUsernameValid } from '../../helpers'
72c7248b 4import { removeVideoAuthorToFriends } from '../../lib'
67bf9b96 5
74889a71 6import { addMethodsToModel } from '../utils'
e02643f3 7import {
e02643f3
C
8 AuthorInstance,
9 AuthorAttributes,
10
11 AuthorMethods
12} from './author-interface'
13
14let Author: Sequelize.Model<AuthorInstance, AuthorAttributes>
72c7248b
C
15let loadAuthorByPodAndUUID: AuthorMethods.LoadAuthorByPodAndUUID
16let load: AuthorMethods.Load
17let loadByUUID: AuthorMethods.LoadByUUID
18let listOwned: AuthorMethods.ListOwned
19let isOwned: AuthorMethods.IsOwned
20let toAddRemoteJSON: AuthorMethods.ToAddRemoteJSON
e02643f3 21
127944aa 22export default function defineAuthor (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
e02643f3 23 Author = sequelize.define<AuthorInstance, AuthorAttributes>('Author',
feb4bdfd 24 {
72c7248b
C
25 uuid: {
26 type: DataTypes.UUID,
27 defaultValue: DataTypes.UUIDV4,
28 allowNull: false,
29 validate: {
30 isUUID: 4
31 }
32 },
feb4bdfd 33 name: {
67bf9b96
C
34 type: DataTypes.STRING,
35 allowNull: false,
36 validate: {
075f16ca 37 usernameValid: value => {
65fcc311 38 const res = isUserUsernameValid(value)
67bf9b96
C
39 if (res === false) throw new Error('Username is not valid.')
40 }
41 }
feb4bdfd
C
42 }
43 },
44 {
319d072e
C
45 indexes: [
46 {
47 fields: [ 'name' ]
48 },
49 {
50 fields: [ 'podId' ]
4712081f
C
51 },
52 {
5d67f289
C
53 fields: [ 'userId' ],
54 unique: true
55 },
56 {
57 fields: [ 'name', 'podId' ],
58 unique: true
319d072e 59 }
72c7248b
C
60 ],
61 hooks: { afterDestroy }
feb4bdfd
C
62 }
63 )
64
72c7248b
C
65 const classMethods = [
66 associate,
67 loadAuthorByPodAndUUID,
68 load,
69 loadByUUID,
70 listOwned
71 ]
72 const instanceMethods = [
73 isOwned,
74 toAddRemoteJSON
75 ]
76 addMethodsToModel(Author, classMethods, instanceMethods)
e02643f3 77
feb4bdfd
C
78 return Author
79}
80
81// ---------------------------------------------------------------------------
82
83function associate (models) {
e02643f3 84 Author.belongsTo(models.Pod, {
feb4bdfd
C
85 foreignKey: {
86 name: 'podId',
87 allowNull: true
88 },
89 onDelete: 'cascade'
90 })
4712081f 91
e02643f3 92 Author.belongsTo(models.User, {
4712081f
C
93 foreignKey: {
94 name: 'userId',
95 allowNull: true
96 },
97 onDelete: 'cascade'
98 })
5c98d3bf 99
72c7248b 100 Author.hasMany(models.VideoChannel, {
5c98d3bf
C
101 foreignKey: {
102 name: 'authorId',
103 allowNull: false
104 },
72c7248b
C
105 onDelete: 'cascade',
106 hooks: true
5c98d3bf 107 })
feb4bdfd 108}
4ff0d862 109
911238e3 110function afterDestroy (author: AuthorInstance) {
72c7248b
C
111 if (author.isOwned()) {
112 const removeVideoAuthorToFriendsParams = {
113 uuid: author.uuid
114 }
115
911238e3 116 return removeVideoAuthorToFriends(removeVideoAuthorToFriendsParams)
72c7248b
C
117 }
118
119 return undefined
120}
121
122toAddRemoteJSON = function (this: AuthorInstance) {
123 const json = {
124 uuid: this.uuid,
125 name: this.name
126 }
127
128 return json
129}
130
131isOwned = function (this: AuthorInstance) {
132 return this.podId === null
133}
134
135// ------------------------------ STATICS ------------------------------
136
137listOwned = function () {
138 const query: Sequelize.FindOptions<AuthorAttributes> = {
139 where: {
140 podId: null
141 }
142 }
143
144 return Author.findAll(query)
145}
146
147load = function (id: number) {
148 return Author.findById(id)
149}
150
151loadByUUID = function (uuid: string) {
152 const query: Sequelize.FindOptions<AuthorAttributes> = {
153 where: {
154 uuid
155 }
4ff0d862
C
156 }
157
72c7248b
C
158 return Author.findOne(query)
159}
160
161loadAuthorByPodAndUUID = function (uuid: string, podId: number, transaction: Sequelize.Transaction) {
162 const query: Sequelize.FindOptions<AuthorAttributes> = {
163 where: {
164 podId,
165 uuid
166 },
6fcd19ba 167 transaction
4ff0d862
C
168 }
169
72c7248b 170 return Author.find(query)
4ff0d862 171}