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