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