diff options
Diffstat (limited to 'server/models/video/author.ts')
-rw-r--r-- | server/models/video/author.ts | 103 |
1 files changed, 88 insertions, 15 deletions
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 @@ | |||
1 | import * as Sequelize from 'sequelize' | 1 | import * as Sequelize from 'sequelize' |
2 | 2 | ||
3 | import { isUserUsernameValid } from '../../helpers' | 3 | import { isUserUsernameValid } from '../../helpers' |
4 | import { removeVideoAuthorToFriends } from '../../lib' | ||
4 | 5 | ||
5 | import { addMethodsToModel } from '../utils' | 6 | import { addMethodsToModel } from '../utils' |
6 | import { | 7 | import { |
@@ -11,11 +12,24 @@ import { | |||
11 | } from './author-interface' | 12 | } from './author-interface' |
12 | 13 | ||
13 | let Author: Sequelize.Model<AuthorInstance, AuthorAttributes> | 14 | let Author: Sequelize.Model<AuthorInstance, AuthorAttributes> |
14 | let findOrCreateAuthor: AuthorMethods.FindOrCreateAuthor | 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 | ||
15 | 21 | ||
16 | export default function defineAuthor (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) { | 22 | export default function defineAuthor (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) { |
17 | Author = sequelize.define<AuthorInstance, AuthorAttributes>('Author', | 23 | Author = sequelize.define<AuthorInstance, AuthorAttributes>('Author', |
18 | { | 24 | { |
25 | uuid: { | ||
26 | type: DataTypes.UUID, | ||
27 | defaultValue: DataTypes.UUIDV4, | ||
28 | allowNull: false, | ||
29 | validate: { | ||
30 | isUUID: 4 | ||
31 | } | ||
32 | }, | ||
19 | name: { | 33 | name: { |
20 | type: DataTypes.STRING, | 34 | type: DataTypes.STRING, |
21 | allowNull: false, | 35 | allowNull: false, |
@@ -43,12 +57,23 @@ export default function defineAuthor (sequelize: Sequelize.Sequelize, DataTypes: | |||
43 | fields: [ 'name', 'podId' ], | 57 | fields: [ 'name', 'podId' ], |
44 | unique: true | 58 | unique: true |
45 | } | 59 | } |
46 | ] | 60 | ], |
61 | hooks: { afterDestroy } | ||
47 | } | 62 | } |
48 | ) | 63 | ) |
49 | 64 | ||
50 | const classMethods = [ associate, findOrCreateAuthor ] | 65 | const classMethods = [ |
51 | addMethodsToModel(Author, 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) | ||
52 | 77 | ||
53 | return Author | 78 | return Author |
54 | } | 79 | } |
@@ -72,27 +97,75 @@ function associate (models) { | |||
72 | onDelete: 'cascade' | 97 | onDelete: 'cascade' |
73 | }) | 98 | }) |
74 | 99 | ||
75 | Author.hasMany(models.Video, { | 100 | Author.hasMany(models.VideoChannel, { |
76 | foreignKey: { | 101 | foreignKey: { |
77 | name: 'authorId', | 102 | name: 'authorId', |
78 | allowNull: false | 103 | allowNull: false |
79 | }, | 104 | }, |
80 | onDelete: 'cascade' | 105 | onDelete: 'cascade', |
106 | hooks: true | ||
81 | }) | 107 | }) |
82 | } | 108 | } |
83 | 109 | ||
84 | findOrCreateAuthor = function (name: string, podId: number, userId: number, transaction: Sequelize.Transaction) { | 110 | function afterDestroy (author: AuthorInstance, options: { transaction: Sequelize.Transaction }) { |
85 | const author = { | 111 | if (author.isOwned()) { |
86 | name, | 112 | const removeVideoAuthorToFriendsParams = { |
87 | podId, | 113 | uuid: author.uuid |
88 | userId | 114 | } |
115 | |||
116 | return removeVideoAuthorToFriends(removeVideoAuthorToFriendsParams, options.transaction) | ||
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 | } | ||
89 | } | 156 | } |
90 | 157 | ||
91 | const query: Sequelize.FindOrInitializeOptions<AuthorAttributes> = { | 158 | return Author.findOne(query) |
92 | where: author, | 159 | } |
93 | defaults: author, | 160 | |
161 | loadAuthorByPodAndUUID = function (uuid: string, podId: number, transaction: Sequelize.Transaction) { | ||
162 | const query: Sequelize.FindOptions<AuthorAttributes> = { | ||
163 | where: { | ||
164 | podId, | ||
165 | uuid | ||
166 | }, | ||
94 | transaction | 167 | transaction |
95 | } | 168 | } |
96 | 169 | ||
97 | return Author.findOrCreate(query).then(([ authorInstance ]) => authorInstance) | 170 | return Author.find(query) |
98 | } | 171 | } |