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