]>
Commit | Line | Data |
---|---|---|
3fd3ab2d | 1 | import { |
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 | 18 | import { Account, AccountSummary } from '../../../shared/models/actors' |
2422c46b | 19 | import { isAccountDescriptionValid } from '../../helpers/custom-validators/accounts' |
c5a893d5 | 20 | import { sendDeleteActor } from '../../lib/activitypub/send' |
fadf619a | 21 | import { ActorModel } from '../activitypub/actor' |
3fd3ab2d | 22 | import { ApplicationModel } from '../application/application' |
3fd3ab2d | 23 | import { ServerModel } from '../server/server' |
2422c46b | 24 | import { getSort, throwIfNotValid } from '../utils' |
3fd3ab2d | 25 | import { VideoChannelModel } from '../video/video-channel' |
f05a1c30 | 26 | import { VideoCommentModel } from '../video/video-comment' |
3fd3ab2d | 27 | import { UserModel } from './user' |
418d092a | 28 | import { AvatarModel } from '../avatar/avatar' |
418d092a | 29 | import { VideoPlaylistModel } from '../video/video-playlist' |
241c3357 | 30 | import { CONSTRAINTS_FIELDS, WEBSERVER } from '../../initializers/constants' |
bfbd9128 C |
31 | import { FindOptions, IncludeOptions, Op, Transaction, WhereOptions } from 'sequelize' |
32 | import { AccountBlocklistModel } from './account-blocklist' | |
33 | import { ServerBlocklistModel } from '../server/server-blocklist' | |
44b88f18 | 34 | import { ActorFollowModel } from '../activitypub/actor-follow' |
b5fecbf4 | 35 | import { MAccountActor, MAccountDefault, MAccountSummaryFormattable, MAccountFormattable, MAccountAP } from '../../typings/models' |
453e83ea | 36 | import * as Bluebird from 'bluebird' |
418d092a C |
37 | |
38 | export enum ScopeNames { | |
39 | SUMMARY = 'SUMMARY' | |
40 | } | |
3fd3ab2d | 41 | |
bfbd9128 C |
42 | export 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 | 130 | export 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: { | |
204 | allowNull: false | |
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 | ||
f05a1c30 C |
221 | @BeforeDestroy |
222 | static async sendDeleteIfOwned (instance: AccountModel, options) { | |
223 | if (!instance.Actor) { | |
224 | instance.Actor = await instance.$get('Actor', { transaction: options.transaction }) as ActorModel | |
225 | } | |
226 | ||
44b88f18 | 227 | await ActorFollowModel.removeFollowsOf(instance.Actor.id, options.transaction) |
c5a893d5 | 228 | if (instance.isOwned()) { |
c5a893d5 C |
229 | return sendDeleteActor(instance.Actor, options.transaction) |
230 | } | |
231 | ||
232 | return undefined | |
e4f97bab C |
233 | } |
234 | ||
453e83ea | 235 | static load (id: number, transaction?: Transaction): Bluebird<MAccountDefault> { |
9b39106d | 236 | return AccountModel.findByPk(id, { transaction }) |
3fd3ab2d | 237 | } |
2295ce6c | 238 | |
453e83ea | 239 | static loadByNameWithHost (nameWithHost: string): Bluebird<MAccountDefault> { |
92bf2f62 C |
240 | const [ accountName, host ] = nameWithHost.split('@') |
241 | ||
6dd9de95 | 242 | if (!host || host === WEBSERVER.HOST) return AccountModel.loadLocalByName(accountName) |
92bf2f62 C |
243 | |
244 | return AccountModel.loadByNameAndHost(accountName, host) | |
245 | } | |
246 | ||
453e83ea | 247 | static loadLocalByName (name: string): Bluebird<MAccountDefault> { |
3fd3ab2d C |
248 | const query = { |
249 | where: { | |
1735c825 | 250 | [ Op.or ]: [ |
3fd3ab2d C |
251 | { |
252 | userId: { | |
1735c825 | 253 | [ Op.ne ]: null |
3fd3ab2d C |
254 | } |
255 | }, | |
256 | { | |
257 | applicationId: { | |
1735c825 | 258 | [ Op.ne ]: null |
3fd3ab2d C |
259 | } |
260 | } | |
261 | ] | |
e8cb4409 C |
262 | }, |
263 | include: [ | |
264 | { | |
265 | model: ActorModel, | |
266 | required: true, | |
267 | where: { | |
268 | preferredUsername: name | |
269 | } | |
270 | } | |
271 | ] | |
272 | } | |
273 | ||
274 | return AccountModel.findOne(query) | |
275 | } | |
276 | ||
453e83ea | 277 | static loadByNameAndHost (name: string, host: string): Bluebird<MAccountDefault> { |
e8cb4409 C |
278 | const query = { |
279 | include: [ | |
280 | { | |
281 | model: ActorModel, | |
282 | required: true, | |
283 | where: { | |
284 | preferredUsername: name | |
285 | }, | |
286 | include: [ | |
287 | { | |
288 | model: ServerModel, | |
289 | required: true, | |
290 | where: { | |
291 | host | |
292 | } | |
293 | } | |
294 | ] | |
295 | } | |
296 | ] | |
3fd3ab2d | 297 | } |
7a7724e6 | 298 | |
3fd3ab2d C |
299 | return AccountModel.findOne(query) |
300 | } | |
7a7724e6 | 301 | |
453e83ea | 302 | static loadByUrl (url: string, transaction?: Transaction): Bluebird<MAccountDefault> { |
3fd3ab2d | 303 | const query = { |
fadf619a C |
304 | include: [ |
305 | { | |
306 | model: ActorModel, | |
307 | required: true, | |
308 | where: { | |
309 | url | |
310 | } | |
311 | } | |
312 | ], | |
3fd3ab2d C |
313 | transaction |
314 | } | |
e4f97bab | 315 | |
3fd3ab2d C |
316 | return AccountModel.findOne(query) |
317 | } | |
e4f97bab | 318 | |
265ba139 C |
319 | static listForApi (start: number, count: number, sort: string) { |
320 | const query = { | |
321 | offset: start, | |
322 | limit: count, | |
6ff9c676 | 323 | order: getSort(sort) |
265ba139 C |
324 | } |
325 | ||
326 | return AccountModel.findAndCountAll(query) | |
c5a893d5 C |
327 | .then(({ rows, count }) => { |
328 | return { | |
329 | data: rows, | |
330 | total: count | |
331 | } | |
332 | }) | |
265ba139 C |
333 | } |
334 | ||
453e83ea | 335 | static listLocalsForSitemap (sort: string): Bluebird<MAccountActor[]> { |
2feebf3e C |
336 | const query = { |
337 | attributes: [ ], | |
338 | offset: 0, | |
339 | order: getSort(sort), | |
340 | include: [ | |
341 | { | |
342 | attributes: [ 'preferredUsername', 'serverId' ], | |
343 | model: ActorModel.unscoped(), | |
344 | where: { | |
345 | serverId: null | |
346 | } | |
347 | } | |
348 | ] | |
349 | } | |
350 | ||
351 | return AccountModel | |
352 | .unscoped() | |
353 | .findAll(query) | |
354 | } | |
355 | ||
1ca9f7c3 | 356 | toFormattedJSON (this: MAccountFormattable): Account { |
fadf619a C |
357 | const actor = this.Actor.toFormattedJSON() |
358 | const account = { | |
3fd3ab2d | 359 | id: this.id, |
244e76a5 | 360 | displayName: this.getDisplayName(), |
2422c46b | 361 | description: this.description, |
3fd3ab2d | 362 | createdAt: this.createdAt, |
79bd2632 C |
363 | updatedAt: this.updatedAt, |
364 | userId: this.userId ? this.userId : undefined | |
3fd3ab2d | 365 | } |
fadf619a C |
366 | |
367 | return Object.assign(actor, account) | |
3fd3ab2d | 368 | } |
e4f97bab | 369 | |
1ca9f7c3 C |
370 | toFormattedSummaryJSON (this: MAccountSummaryFormattable): AccountSummary { |
371 | const actor = this.Actor.toFormattedSummaryJSON() | |
418d092a C |
372 | |
373 | return { | |
374 | id: this.id, | |
418d092a C |
375 | name: actor.name, |
376 | displayName: this.getDisplayName(), | |
377 | url: actor.url, | |
378 | host: actor.host, | |
379 | avatar: actor.avatar | |
380 | } | |
381 | } | |
382 | ||
b5fecbf4 | 383 | toActivityPubObject (this: MAccountAP) { |
8424c402 | 384 | const obj = this.Actor.toActivityPubObject(this.name) |
2422c46b C |
385 | |
386 | return Object.assign(obj, { | |
387 | summary: this.description | |
388 | }) | |
e4f97bab C |
389 | } |
390 | ||
3fd3ab2d | 391 | isOwned () { |
fadf619a | 392 | return this.Actor.isOwned() |
3fd3ab2d | 393 | } |
244e76a5 | 394 | |
744d0eca C |
395 | isOutdated () { |
396 | return this.Actor.isOutdated() | |
397 | } | |
398 | ||
244e76a5 RK |
399 | getDisplayName () { |
400 | return this.name | |
401 | } | |
bfbd9128 C |
402 | |
403 | isBlocked () { | |
404 | return this.BlockedAccounts && this.BlockedAccounts.length !== 0 | |
405 | } | |
63c93323 | 406 | } |