]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/account/account.ts
Use grid to organise settings in admin, my-account
[github/Chocobozzz/PeerTube.git] / server / models / account / account.ts
CommitLineData
3fd3ab2d 1import {
2422c46b
C
2 AllowNull,
3 BeforeDestroy,
4 BelongsTo,
5 Column,
453e83ea
C
6 CreatedAt,
7 DataType,
2422c46b
C
8 Default,
9 DefaultScope,
10 ForeignKey,
11 HasMany,
12 Is,
09979f89
C
13 Model,
14 Scopes,
2422c46b 15 Table,
3fd3ab2d
C
16 UpdatedAt
17} from 'sequelize-typescript'
418d092a 18import { Account, AccountSummary } from '../../../shared/models/actors'
2422c46b 19import { isAccountDescriptionValid } from '../../helpers/custom-validators/accounts'
c5a893d5 20import { sendDeleteActor } from '../../lib/activitypub/send'
fadf619a 21import { ActorModel } from '../activitypub/actor'
3fd3ab2d 22import { ApplicationModel } from '../application/application'
3fd3ab2d 23import { ServerModel } from '../server/server'
2422c46b 24import { getSort, throwIfNotValid } from '../utils'
3fd3ab2d 25import { VideoChannelModel } from '../video/video-channel'
f05a1c30 26import { VideoCommentModel } from '../video/video-comment'
3fd3ab2d 27import { UserModel } from './user'
418d092a 28import { AvatarModel } from '../avatar/avatar'
418d092a 29import { VideoPlaylistModel } from '../video/video-playlist'
e4a686b4 30import { CONSTRAINTS_FIELDS, SERVER_ACTOR_NAME, WEBSERVER } from '../../initializers/constants'
bfbd9128
C
31import { FindOptions, IncludeOptions, Op, Transaction, WhereOptions } from 'sequelize'
32import { AccountBlocklistModel } from './account-blocklist'
33import { ServerBlocklistModel } from '../server/server-blocklist'
44b88f18 34import { ActorFollowModel } from '../activitypub/actor-follow'
0ffd6d32 35import { MAccountActor, MAccountAP, MAccountDefault, MAccountFormattable, MAccountSummaryFormattable } from '../../typings/models'
453e83ea 36import * as Bluebird from 'bluebird'
0ffd6d32 37import { ModelCache } from '@server/models/model-cache'
418d092a
C
38
39export enum ScopeNames {
40 SUMMARY = 'SUMMARY'
41}
3fd3ab2d 42
bfbd9128
C
43export type SummaryOptions = {
44 whereActor?: WhereOptions
45 withAccountBlockerIds?: number[]
46}
47
3acc5084 48@DefaultScope(() => ({
50d6de9c 49 include: [
3fd3ab2d 50 {
3acc5084 51 model: ActorModel, // Default scope includes avatar and server
f37dc0dd 52 required: true
e4f97bab 53 }
e4f97bab 54 ]
3acc5084
C
55}))
56@Scopes(() => ({
a1587156 57 [ScopeNames.SUMMARY]: (options: SummaryOptions = {}) => {
bfbd9128
C
58 const whereActor = options.whereActor || undefined
59
60 const serverInclude: IncludeOptions = {
61 attributes: [ 'host' ],
62 model: ServerModel.unscoped(),
63 required: false
64 }
65
66 const query: FindOptions = {
418d092a
C
67 attributes: [ 'id', 'name' ],
68 include: [
69 {
57cfff78 70 attributes: [ 'id', 'preferredUsername', 'url', 'serverId', 'avatarId' ],
418d092a
C
71 model: ActorModel.unscoped(),
72 required: true,
73 where: whereActor,
74 include: [
bfbd9128
C
75 serverInclude,
76
418d092a
C
77 {
78 model: AvatarModel.unscoped(),
79 required: false
80 }
81 ]
82 }
83 ]
84 }
bfbd9128
C
85
86 if (options.withAccountBlockerIds) {
87 query.include.push({
88 attributes: [ 'id' ],
89 model: AccountBlocklistModel.unscoped(),
90 as: 'BlockedAccounts',
91 required: false,
92 where: {
93 accountId: {
94 [Op.in]: options.withAccountBlockerIds
95 }
96 }
97 })
98
99 serverInclude.include = [
100 {
101 attributes: [ 'id' ],
102 model: ServerBlocklistModel.unscoped(),
103 required: false,
104 where: {
105 accountId: {
106 [Op.in]: options.withAccountBlockerIds
107 }
108 }
109 }
110 ]
111 }
112
113 return query
418d092a 114 }
3acc5084 115}))
50d6de9c 116@Table({
8cd72bd3
C
117 tableName: 'account',
118 indexes: [
119 {
120 fields: [ 'actorId' ],
121 unique: true
122 },
123 {
124 fields: [ 'applicationId' ]
125 },
126 {
127 fields: [ 'userId' ]
128 }
129 ]
50d6de9c 130})
fadf619a 131export class AccountModel extends Model<AccountModel> {
3fd3ab2d 132
50d6de9c 133 @AllowNull(false)
50d6de9c
C
134 @Column
135 name: string
136
2422c46b
C
137 @AllowNull(true)
138 @Default(null)
1735c825 139 @Is('AccountDescription', value => throwIfNotValid(value, isAccountDescriptionValid, 'description', true))
241c3357 140 @Column(DataType.STRING(CONSTRAINTS_FIELDS.USERS.DESCRIPTION.max))
2422c46b
C
141 description: string
142
3fd3ab2d
C
143 @CreatedAt
144 createdAt: Date
145
146 @UpdatedAt
147 updatedAt: Date
148
fadf619a 149 @ForeignKey(() => ActorModel)
3fd3ab2d 150 @Column
fadf619a 151 actorId: number
e4f97bab 152
fadf619a 153 @BelongsTo(() => ActorModel, {
e4f97bab 154 foreignKey: {
fadf619a 155 allowNull: false
e4f97bab
C
156 },
157 onDelete: 'cascade'
158 })
fadf619a 159 Actor: ActorModel
e4f97bab 160
3fd3ab2d
C
161 @ForeignKey(() => UserModel)
162 @Column
163 userId: number
164
165 @BelongsTo(() => UserModel, {
e4f97bab 166 foreignKey: {
e4f97bab
C
167 allowNull: true
168 },
169 onDelete: 'cascade'
170 })
3fd3ab2d
C
171 User: UserModel
172
173 @ForeignKey(() => ApplicationModel)
174 @Column
175 applicationId: number
e4f97bab 176
3fd3ab2d 177 @BelongsTo(() => ApplicationModel, {
e4f97bab 178 foreignKey: {
e4f97bab
C
179 allowNull: true
180 },
181 onDelete: 'cascade'
182 })
f05a1c30 183 Application: ApplicationModel
e4f97bab 184
3fd3ab2d 185 @HasMany(() => VideoChannelModel, {
e4f97bab 186 foreignKey: {
e4f97bab
C
187 allowNull: false
188 },
189 onDelete: 'cascade',
190 hooks: true
191 })
3fd3ab2d 192 VideoChannels: VideoChannelModel[]
e4f97bab 193
418d092a
C
194 @HasMany(() => VideoPlaylistModel, {
195 foreignKey: {
196 allowNull: false
197 },
198 onDelete: 'cascade',
199 hooks: true
200 })
201 VideoPlaylists: VideoPlaylistModel[]
202
f05a1c30
C
203 @HasMany(() => VideoCommentModel, {
204 foreignKey: {
69222afa 205 allowNull: true
f05a1c30
C
206 },
207 onDelete: 'cascade',
208 hooks: true
209 })
210 VideoComments: VideoCommentModel[]
211
bfbd9128
C
212 @HasMany(() => AccountBlocklistModel, {
213 foreignKey: {
214 name: 'targetAccountId',
215 allowNull: false
216 },
217 as: 'BlockedAccounts',
218 onDelete: 'CASCADE'
219 })
220 BlockedAccounts: AccountBlocklistModel[]
221
f05a1c30
C
222 @BeforeDestroy
223 static async sendDeleteIfOwned (instance: AccountModel, options) {
224 if (!instance.Actor) {
e6122097 225 instance.Actor = await instance.$get('Actor', { transaction: options.transaction })
f05a1c30
C
226 }
227
44b88f18 228 await ActorFollowModel.removeFollowsOf(instance.Actor.id, options.transaction)
c5a893d5 229 if (instance.isOwned()) {
c5a893d5
C
230 return sendDeleteActor(instance.Actor, options.transaction)
231 }
232
233 return undefined
e4f97bab
C
234 }
235
453e83ea 236 static load (id: number, transaction?: Transaction): Bluebird<MAccountDefault> {
9b39106d 237 return AccountModel.findByPk(id, { transaction })
3fd3ab2d 238 }
2295ce6c 239
453e83ea 240 static loadByNameWithHost (nameWithHost: string): Bluebird<MAccountDefault> {
92bf2f62
C
241 const [ accountName, host ] = nameWithHost.split('@')
242
6dd9de95 243 if (!host || host === WEBSERVER.HOST) return AccountModel.loadLocalByName(accountName)
92bf2f62
C
244
245 return AccountModel.loadByNameAndHost(accountName, host)
246 }
247
453e83ea 248 static loadLocalByName (name: string): Bluebird<MAccountDefault> {
0ffd6d32
C
249 const fun = () => {
250 const query = {
251 where: {
252 [Op.or]: [
253 {
254 userId: {
255 [Op.ne]: null
256 }
257 },
258 {
259 applicationId: {
260 [Op.ne]: null
261 }
3fd3ab2d 262 }
0ffd6d32
C
263 ]
264 },
265 include: [
3fd3ab2d 266 {
0ffd6d32
C
267 model: ActorModel,
268 required: true,
269 where: {
270 preferredUsername: name
3fd3ab2d
C
271 }
272 }
273 ]
0ffd6d32 274 }
e8cb4409 275
0ffd6d32
C
276 return AccountModel.findOne(query)
277 }
e4a686b4 278
0ffd6d32
C
279 return ModelCache.Instance.doCache({
280 cacheType: 'local-account-name',
281 key: name,
282 fun,
283 // The server actor never change, so we can easily cache it
284 whitelist: () => name === SERVER_ACTOR_NAME
285 })
e8cb4409
C
286 }
287
453e83ea 288 static loadByNameAndHost (name: string, host: string): Bluebird<MAccountDefault> {
e8cb4409
C
289 const query = {
290 include: [
291 {
292 model: ActorModel,
293 required: true,
294 where: {
295 preferredUsername: name
296 },
297 include: [
298 {
299 model: ServerModel,
300 required: true,
301 where: {
302 host
303 }
304 }
305 ]
306 }
307 ]
3fd3ab2d 308 }
7a7724e6 309
3fd3ab2d
C
310 return AccountModel.findOne(query)
311 }
7a7724e6 312
453e83ea 313 static loadByUrl (url: string, transaction?: Transaction): Bluebird<MAccountDefault> {
3fd3ab2d 314 const query = {
fadf619a
C
315 include: [
316 {
317 model: ActorModel,
318 required: true,
319 where: {
320 url
321 }
322 }
323 ],
3fd3ab2d
C
324 transaction
325 }
e4f97bab 326
3fd3ab2d
C
327 return AccountModel.findOne(query)
328 }
e4f97bab 329
265ba139
C
330 static listForApi (start: number, count: number, sort: string) {
331 const query = {
332 offset: start,
333 limit: count,
6ff9c676 334 order: getSort(sort)
265ba139
C
335 }
336
337 return AccountModel.findAndCountAll(query)
c5a893d5
C
338 .then(({ rows, count }) => {
339 return {
340 data: rows,
341 total: count
342 }
343 })
265ba139
C
344 }
345
453e83ea 346 static listLocalsForSitemap (sort: string): Bluebird<MAccountActor[]> {
2feebf3e
C
347 const query = {
348 attributes: [ ],
349 offset: 0,
350 order: getSort(sort),
351 include: [
352 {
353 attributes: [ 'preferredUsername', 'serverId' ],
354 model: ActorModel.unscoped(),
355 where: {
356 serverId: null
357 }
358 }
359 ]
360 }
361
362 return AccountModel
363 .unscoped()
364 .findAll(query)
365 }
366
1ca9f7c3 367 toFormattedJSON (this: MAccountFormattable): Account {
fadf619a
C
368 const actor = this.Actor.toFormattedJSON()
369 const account = {
3fd3ab2d 370 id: this.id,
244e76a5 371 displayName: this.getDisplayName(),
2422c46b 372 description: this.description,
3fd3ab2d 373 createdAt: this.createdAt,
79bd2632
C
374 updatedAt: this.updatedAt,
375 userId: this.userId ? this.userId : undefined
3fd3ab2d 376 }
fadf619a
C
377
378 return Object.assign(actor, account)
3fd3ab2d 379 }
e4f97bab 380
1ca9f7c3
C
381 toFormattedSummaryJSON (this: MAccountSummaryFormattable): AccountSummary {
382 const actor = this.Actor.toFormattedSummaryJSON()
418d092a
C
383
384 return {
385 id: this.id,
418d092a
C
386 name: actor.name,
387 displayName: this.getDisplayName(),
388 url: actor.url,
389 host: actor.host,
390 avatar: actor.avatar
391 }
392 }
393
b5fecbf4 394 toActivityPubObject (this: MAccountAP) {
8424c402 395 const obj = this.Actor.toActivityPubObject(this.name)
2422c46b
C
396
397 return Object.assign(obj, {
398 summary: this.description
399 })
e4f97bab
C
400 }
401
3fd3ab2d 402 isOwned () {
fadf619a 403 return this.Actor.isOwned()
3fd3ab2d 404 }
244e76a5 405
744d0eca
C
406 isOutdated () {
407 return this.Actor.isOutdated()
408 }
409
244e76a5
RK
410 getDisplayName () {
411 return this.name
412 }
bfbd9128
C
413
414 isBlocked () {
415 return this.BlockedAccounts && this.BlockedAccounts.length !== 0
416 }
63c93323 417}