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