]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/account/account.ts
Move user moderation tool in a separate component
[github/Chocobozzz/PeerTube.git] / server / models / account / account.ts
CommitLineData
e4f97bab 1import * as Sequelize from 'sequelize'
3fd3ab2d 2import {
2422c46b
C
3 AllowNull,
4 BeforeDestroy,
5 BelongsTo,
6 Column,
7 CreatedAt,
8 Default,
9 DefaultScope,
10 ForeignKey,
11 HasMany,
12 Is,
13 Model,
14 Table,
3fd3ab2d
C
15 UpdatedAt
16} from 'sequelize-typescript'
c5911fd3 17import { Account } from '../../../shared/models/actors'
2422c46b 18import { isAccountDescriptionValid } from '../../helpers/custom-validators/accounts'
c5a893d5 19import { sendDeleteActor } from '../../lib/activitypub/send'
fadf619a 20import { ActorModel } from '../activitypub/actor'
3fd3ab2d 21import { ApplicationModel } from '../application/application'
3fd3ab2d 22import { ServerModel } from '../server/server'
2422c46b 23import { getSort, throwIfNotValid } from '../utils'
3fd3ab2d 24import { VideoChannelModel } from '../video/video-channel'
f05a1c30 25import { VideoCommentModel } from '../video/video-comment'
3fd3ab2d
C
26import { UserModel } from './user'
27
50d6de9c
C
28@DefaultScope({
29 include: [
3fd3ab2d 30 {
f37dc0dd
C
31 model: () => ActorModel, // Default scope includes avatar and server
32 required: true
e4f97bab 33 }
e4f97bab 34 ]
3fd3ab2d 35})
50d6de9c 36@Table({
8cd72bd3
C
37 tableName: 'account',
38 indexes: [
39 {
40 fields: [ 'actorId' ],
41 unique: true
42 },
43 {
44 fields: [ 'applicationId' ]
45 },
46 {
47 fields: [ 'userId' ]
48 }
49 ]
50d6de9c 50})
fadf619a 51export class AccountModel extends Model<AccountModel> {
3fd3ab2d 52
50d6de9c 53 @AllowNull(false)
50d6de9c
C
54 @Column
55 name: string
56
2422c46b
C
57 @AllowNull(true)
58 @Default(null)
59 @Is('AccountDescription', value => throwIfNotValid(value, isAccountDescriptionValid, 'description'))
60 @Column
61 description: string
62
3fd3ab2d
C
63 @CreatedAt
64 createdAt: Date
65
66 @UpdatedAt
67 updatedAt: Date
68
fadf619a 69 @ForeignKey(() => ActorModel)
3fd3ab2d 70 @Column
fadf619a 71 actorId: number
e4f97bab 72
fadf619a 73 @BelongsTo(() => ActorModel, {
e4f97bab 74 foreignKey: {
fadf619a 75 allowNull: false
e4f97bab
C
76 },
77 onDelete: 'cascade'
78 })
fadf619a 79 Actor: ActorModel
e4f97bab 80
3fd3ab2d
C
81 @ForeignKey(() => UserModel)
82 @Column
83 userId: number
84
85 @BelongsTo(() => UserModel, {
e4f97bab 86 foreignKey: {
e4f97bab
C
87 allowNull: true
88 },
89 onDelete: 'cascade'
90 })
3fd3ab2d
C
91 User: UserModel
92
93 @ForeignKey(() => ApplicationModel)
94 @Column
95 applicationId: number
e4f97bab 96
3fd3ab2d 97 @BelongsTo(() => ApplicationModel, {
e4f97bab 98 foreignKey: {
e4f97bab
C
99 allowNull: true
100 },
101 onDelete: 'cascade'
102 })
f05a1c30 103 Application: ApplicationModel
e4f97bab 104
3fd3ab2d 105 @HasMany(() => VideoChannelModel, {
e4f97bab 106 foreignKey: {
e4f97bab
C
107 allowNull: false
108 },
109 onDelete: 'cascade',
110 hooks: true
111 })
3fd3ab2d 112 VideoChannels: VideoChannelModel[]
e4f97bab 113
f05a1c30
C
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
c5a893d5 129 if (instance.isOwned()) {
c5a893d5
C
130 return sendDeleteActor(instance.Actor, options.transaction)
131 }
132
133 return undefined
e4f97bab
C
134 }
135
91411dba
C
136 static load (id: number, transaction?: Sequelize.Transaction) {
137 return AccountModel.findById(id, { transaction })
3fd3ab2d 138 }
2295ce6c 139
3fd3ab2d
C
140 static loadByUUID (uuid: string) {
141 const query = {
50d6de9c
C
142 include: [
143 {
144 model: ActorModel,
145 required: true,
146 where: {
147 uuid
148 }
149 }
150 ]
2295ce6c 151 }
60862425 152
3fd3ab2d 153 return AccountModel.findOne(query)
60862425 154 }
51548b31 155
3fd3ab2d
C
156 static loadLocalByName (name: string) {
157 const query = {
158 where: {
3fd3ab2d
C
159 [ Sequelize.Op.or ]: [
160 {
161 userId: {
162 [ Sequelize.Op.ne ]: null
163 }
164 },
165 {
166 applicationId: {
167 [ Sequelize.Op.ne ]: null
168 }
169 }
170 ]
e8cb4409
C
171 },
172 include: [
173 {
174 model: ActorModel,
175 required: true,
176 where: {
177 preferredUsername: name
178 }
179 }
180 ]
181 }
182
183 return AccountModel.findOne(query)
184 }
185
8a19bee1 186 static loadByNameAndHost (name: string, host: string) {
e8cb4409
C
187 const query = {
188 include: [
189 {
190 model: ActorModel,
191 required: true,
192 where: {
193 preferredUsername: name
194 },
195 include: [
196 {
197 model: ServerModel,
198 required: true,
199 where: {
200 host
201 }
202 }
203 ]
204 }
205 ]
3fd3ab2d 206 }
7a7724e6 207
3fd3ab2d
C
208 return AccountModel.findOne(query)
209 }
7a7724e6 210
3fd3ab2d
C
211 static loadByUrl (url: string, transaction?: Sequelize.Transaction) {
212 const query = {
fadf619a
C
213 include: [
214 {
215 model: ActorModel,
216 required: true,
217 where: {
218 url
219 }
220 }
221 ],
3fd3ab2d
C
222 transaction
223 }
e4f97bab 224
3fd3ab2d
C
225 return AccountModel.findOne(query)
226 }
e4f97bab 227
265ba139
C
228 static listForApi (start: number, count: number, sort: string) {
229 const query = {
230 offset: start,
231 limit: count,
6ff9c676 232 order: getSort(sort)
265ba139
C
233 }
234
235 return AccountModel.findAndCountAll(query)
c5a893d5
C
236 .then(({ rows, count }) => {
237 return {
238 data: rows,
239 total: count
240 }
241 })
265ba139
C
242 }
243
c5911fd3 244 toFormattedJSON (): Account {
fadf619a
C
245 const actor = this.Actor.toFormattedJSON()
246 const account = {
3fd3ab2d 247 id: this.id,
244e76a5 248 displayName: this.getDisplayName(),
2422c46b 249 description: this.description,
3fd3ab2d 250 createdAt: this.createdAt,
fadf619a 251 updatedAt: this.updatedAt
3fd3ab2d 252 }
fadf619a
C
253
254 return Object.assign(actor, account)
3fd3ab2d 255 }
e4f97bab 256
3fd3ab2d 257 toActivityPubObject () {
2422c46b
C
258 const obj = this.Actor.toActivityPubObject(this.name, 'Account')
259
260 return Object.assign(obj, {
261 summary: this.description
262 })
e4f97bab
C
263 }
264
3fd3ab2d 265 isOwned () {
fadf619a 266 return this.Actor.isOwned()
3fd3ab2d 267 }
244e76a5
RK
268
269 getDisplayName () {
270 return this.name
271 }
63c93323 272}