aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/video/author.ts
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-10-24 19:41:09 +0200
committerChocobozzz <florian.bigard@gmail.com>2017-10-26 09:11:38 +0200
commit72c7248b6fdcdb2175e726ff51b42e7555f2bd84 (patch)
tree1bfdee99dbe2392cc997edba8e314e2a8a401c72 /server/models/video/author.ts
parent8113a93a0d9f31aa9e23702bbc31b8a76275ae22 (diff)
downloadPeerTube-72c7248b6fdcdb2175e726ff51b42e7555f2bd84.tar.gz
PeerTube-72c7248b6fdcdb2175e726ff51b42e7555f2bd84.tar.zst
PeerTube-72c7248b6fdcdb2175e726ff51b42e7555f2bd84.zip
Add video channels
Diffstat (limited to 'server/models/video/author.ts')
-rw-r--r--server/models/video/author.ts103
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 @@
1import * as Sequelize from 'sequelize' 1import * as Sequelize from 'sequelize'
2 2
3import { isUserUsernameValid } from '../../helpers' 3import { isUserUsernameValid } from '../../helpers'
4import { removeVideoAuthorToFriends } from '../../lib'
4 5
5import { addMethodsToModel } from '../utils' 6import { addMethodsToModel } from '../utils'
6import { 7import {
@@ -11,11 +12,24 @@ import {
11} from './author-interface' 12} from './author-interface'
12 13
13let Author: Sequelize.Model<AuthorInstance, AuthorAttributes> 14let Author: Sequelize.Model<AuthorInstance, AuthorAttributes>
14let findOrCreateAuthor: AuthorMethods.FindOrCreateAuthor 15let loadAuthorByPodAndUUID: AuthorMethods.LoadAuthorByPodAndUUID
16let load: AuthorMethods.Load
17let loadByUUID: AuthorMethods.LoadByUUID
18let listOwned: AuthorMethods.ListOwned
19let isOwned: AuthorMethods.IsOwned
20let toAddRemoteJSON: AuthorMethods.ToAddRemoteJSON
15 21
16export default function defineAuthor (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) { 22export 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
84findOrCreateAuthor = function (name: string, podId: number, userId: number, transaction: Sequelize.Transaction) { 110function 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
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 }
89 } 156 }
90 157
91 const query: Sequelize.FindOrInitializeOptions<AuthorAttributes> = { 158 return Author.findOne(query)
92 where: author, 159}
93 defaults: author, 160
161loadAuthorByPodAndUUID = 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}