]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/account/account.ts
54ec7cfccaf31c2d98128d9430d6bd387764a499
[github/Chocobozzz/PeerTube.git] / server / models / account / account.ts
1 import * as Sequelize from 'sequelize'
2 import {
3 AllowNull,
4 BeforeDestroy,
5 BelongsTo,
6 Column,
7 CreatedAt,
8 Default,
9 DefaultScope,
10 ForeignKey,
11 HasMany,
12 Is,
13 Model,
14 Table,
15 UpdatedAt
16 } from 'sequelize-typescript'
17 import { Account } from '../../../shared/models/actors'
18 import { isAccountDescriptionValid } from '../../helpers/custom-validators/accounts'
19 import { logger } from '../../helpers/logger'
20 import { sendDeleteActor } from '../../lib/activitypub/send'
21 import { ActorModel } from '../activitypub/actor'
22 import { ApplicationModel } from '../application/application'
23 import { AvatarModel } from '../avatar/avatar'
24 import { ServerModel } from '../server/server'
25 import { getSort, throwIfNotValid } from '../utils'
26 import { VideoChannelModel } from '../video/video-channel'
27 import { VideoCommentModel } from '../video/video-comment'
28 import { UserModel } from './user'
29
30 @DefaultScope({
31 include: [
32 {
33 model: () => ActorModel,
34 required: true,
35 include: [
36 {
37 model: () => ServerModel,
38 required: false
39 },
40 {
41 model: () => AvatarModel,
42 required: false
43 }
44 ]
45 }
46 ]
47 })
48 @Table({
49 tableName: 'account'
50 })
51 export class AccountModel extends Model<AccountModel> {
52
53 @AllowNull(false)
54 @Column
55 name: string
56
57 @AllowNull(true)
58 @Default(null)
59 @Is('AccountDescription', value => throwIfNotValid(value, isAccountDescriptionValid, 'description'))
60 @Column
61 description: string
62
63 @CreatedAt
64 createdAt: Date
65
66 @UpdatedAt
67 updatedAt: Date
68
69 @ForeignKey(() => ActorModel)
70 @Column
71 actorId: number
72
73 @BelongsTo(() => ActorModel, {
74 foreignKey: {
75 allowNull: false
76 },
77 onDelete: 'cascade'
78 })
79 Actor: ActorModel
80
81 @ForeignKey(() => UserModel)
82 @Column
83 userId: number
84
85 @BelongsTo(() => UserModel, {
86 foreignKey: {
87 allowNull: true
88 },
89 onDelete: 'cascade'
90 })
91 User: UserModel
92
93 @ForeignKey(() => ApplicationModel)
94 @Column
95 applicationId: number
96
97 @BelongsTo(() => ApplicationModel, {
98 foreignKey: {
99 allowNull: true
100 },
101 onDelete: 'cascade'
102 })
103 Application: ApplicationModel
104
105 @HasMany(() => VideoChannelModel, {
106 foreignKey: {
107 allowNull: false
108 },
109 onDelete: 'cascade',
110 hooks: true
111 })
112 VideoChannels: VideoChannelModel[]
113
114 @HasMany(() => VideoCommentModel, {
115 foreignKey: {
116 allowNull: false
117 },
118 onDelete: 'cascade',
119 hooks: true
120 })
121 VideoComments: VideoCommentModel[]
122
123 @BeforeDestroy
124 static async sendDeleteIfOwned (instance: AccountModel, options) {
125 if (!instance.Actor) {
126 instance.Actor = await instance.$get('Actor', { transaction: options.transaction }) as ActorModel
127 }
128
129 if (instance.isOwned()) {
130 logger.debug('Sending delete of actor of account %s.', instance.Actor.url)
131 return sendDeleteActor(instance.Actor, options.transaction)
132 }
133
134 return undefined
135 }
136
137 static load (id: number) {
138 return AccountModel.findById(id)
139 }
140
141 static loadByUUID (uuid: string) {
142 const query = {
143 include: [
144 {
145 model: ActorModel,
146 required: true,
147 where: {
148 uuid
149 }
150 }
151 ]
152 }
153
154 return AccountModel.findOne(query)
155 }
156
157 static loadLocalByName (name: string) {
158 const query = {
159 where: {
160 name,
161 [ Sequelize.Op.or ]: [
162 {
163 userId: {
164 [ Sequelize.Op.ne ]: null
165 }
166 },
167 {
168 applicationId: {
169 [ Sequelize.Op.ne ]: null
170 }
171 }
172 ]
173 }
174 }
175
176 return AccountModel.findOne(query)
177 }
178
179 static loadByUrl (url: string, transaction?: Sequelize.Transaction) {
180 const query = {
181 include: [
182 {
183 model: ActorModel,
184 required: true,
185 where: {
186 url
187 }
188 }
189 ],
190 transaction
191 }
192
193 return AccountModel.findOne(query)
194 }
195
196 static listForApi (start: number, count: number, sort: string) {
197 const query = {
198 offset: start,
199 limit: count,
200 order: getSort(sort),
201 }
202
203 return AccountModel.findAndCountAll(query)
204 .then(({ rows, count }) => {
205 return {
206 data: rows,
207 total: count
208 }
209 })
210 }
211
212 toFormattedJSON (): Account {
213 const actor = this.Actor.toFormattedJSON()
214 const account = {
215 id: this.id,
216 displayName: this.name,
217 description: this.description,
218 createdAt: this.createdAt,
219 updatedAt: this.updatedAt
220 }
221
222 return Object.assign(actor, account)
223 }
224
225 toActivityPubObject () {
226 const obj = this.Actor.toActivityPubObject(this.name, 'Account')
227
228 return Object.assign(obj, {
229 summary: this.description
230 })
231 }
232
233 isOwned () {
234 return this.Actor.isOwned()
235 }
236 }