]>
Commit | Line | Data |
---|---|---|
50d6de9c | 1 | import { values } from 'lodash' |
47564bbe | 2 | import { extname } from 'path' |
fadf619a | 3 | import { |
2422c46b C |
4 | AllowNull, |
5 | BelongsTo, | |
6 | Column, | |
7 | CreatedAt, | |
8 | DataType, | |
2422c46b C |
9 | DefaultScope, |
10 | ForeignKey, | |
11 | HasMany, | |
12 | HasOne, | |
13 | Is, | |
2422c46b C |
14 | Model, |
15 | Scopes, | |
16 | Table, | |
17 | UpdatedAt | |
fadf619a | 18 | } from 'sequelize-typescript' |
50d6de9c | 19 | import { ActivityPubActorType } from '../../../shared/models/activitypub' |
fadf619a | 20 | import { Avatar } from '../../../shared/models/avatars/avatar.model' |
da854ddd | 21 | import { activityPubContextify } from '../../helpers/activitypub' |
fadf619a | 22 | import { |
2422c46b C |
23 | isActorFollowersCountValid, |
24 | isActorFollowingCountValid, | |
25 | isActorPreferredUsernameValid, | |
26 | isActorPrivateKeyValid, | |
da854ddd C |
27 | isActorPublicKeyValid |
28 | } from '../../helpers/custom-validators/activitypub/actor' | |
29 | import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc' | |
74dc3bca | 30 | import { ACTIVITY_PUB, ACTIVITY_PUB_ACTOR_TYPES, CONSTRAINTS_FIELDS, WEBSERVER } from '../../initializers/constants' |
50d6de9c | 31 | import { AccountModel } from '../account/account' |
fadf619a C |
32 | import { AvatarModel } from '../avatar/avatar' |
33 | import { ServerModel } from '../server/server' | |
9f79ade6 | 34 | import { isOutdated, throwIfNotValid } from '../utils' |
50d6de9c C |
35 | import { VideoChannelModel } from '../video/video-channel' |
36 | import { ActorFollowModel } from './actor-follow' | |
e5565833 | 37 | import { VideoModel } from '../video/video' |
1ca9f7c3 C |
38 | import { |
39 | MActor, | |
40 | MActorAccountChannelId, | |
b5fecbf4 | 41 | MActorAP, |
1ca9f7c3 | 42 | MActorFormattable, |
b5fecbf4 C |
43 | MActorFull, |
44 | MActorHost, | |
1ca9f7c3 | 45 | MActorServer, |
47581df0 C |
46 | MActorSummaryFormattable, |
47 | MActorWithInboxes | |
1ca9f7c3 | 48 | } from '../../typings/models' |
453e83ea | 49 | import * as Bluebird from 'bluebird' |
941c5eac | 50 | import { Op, Transaction } from 'sequelize' |
fadf619a | 51 | |
50d6de9c C |
52 | enum ScopeNames { |
53 | FULL = 'FULL' | |
54 | } | |
55 | ||
f37dc0dd C |
56 | export const unusedActorAttributesForAPI = [ |
57 | 'publicKey', | |
58 | 'privateKey', | |
59 | 'inboxUrl', | |
60 | 'outboxUrl', | |
61 | 'sharedInboxUrl', | |
62 | 'followersUrl', | |
aa55a4da | 63 | 'followingUrl', |
f5b0af50 C |
64 | 'url', |
65 | 'createdAt', | |
66 | 'updatedAt' | |
f37dc0dd C |
67 | ] |
68 | ||
3acc5084 | 69 | @DefaultScope(() => ({ |
ce33ee01 C |
70 | include: [ |
71 | { | |
3acc5084 | 72 | model: ServerModel, |
ce33ee01 | 73 | required: false |
c5911fd3 C |
74 | }, |
75 | { | |
3acc5084 | 76 | model: AvatarModel, |
c5911fd3 | 77 | required: false |
ce33ee01 C |
78 | } |
79 | ] | |
3acc5084 C |
80 | })) |
81 | @Scopes(() => ({ | |
50d6de9c C |
82 | [ScopeNames.FULL]: { |
83 | include: [ | |
84 | { | |
3acc5084 | 85 | model: AccountModel.unscoped(), |
50d6de9c C |
86 | required: false |
87 | }, | |
88 | { | |
3acc5084 | 89 | model: VideoChannelModel.unscoped(), |
c48e82b5 C |
90 | required: false, |
91 | include: [ | |
92 | { | |
3acc5084 | 93 | model: AccountModel, |
c48e82b5 C |
94 | required: true |
95 | } | |
96 | ] | |
ce33ee01 C |
97 | }, |
98 | { | |
3acc5084 | 99 | model: ServerModel, |
ce33ee01 | 100 | required: false |
c5911fd3 C |
101 | }, |
102 | { | |
3acc5084 | 103 | model: AvatarModel, |
c5911fd3 | 104 | required: false |
50d6de9c | 105 | } |
3acc5084 | 106 | ] |
50d6de9c | 107 | } |
3acc5084 | 108 | })) |
fadf619a | 109 | @Table({ |
50d6de9c C |
110 | tableName: 'actor', |
111 | indexes: [ | |
2ccaeeb3 | 112 | { |
8cd72bd3 C |
113 | fields: [ 'url' ], |
114 | unique: true | |
2ccaeeb3 | 115 | }, |
50d6de9c | 116 | { |
e12a0092 | 117 | fields: [ 'preferredUsername', 'serverId' ], |
77e08517 C |
118 | unique: true, |
119 | where: { | |
120 | serverId: { | |
121 | [Op.ne]: null | |
122 | } | |
123 | } | |
124 | }, | |
125 | { | |
126 | fields: [ 'preferredUsername' ], | |
127 | unique: true, | |
128 | where: { | |
129 | serverId: null | |
130 | } | |
6502c3d4 C |
131 | }, |
132 | { | |
133 | fields: [ 'inboxUrl', 'sharedInboxUrl' ] | |
57c36b27 | 134 | }, |
a3d1026b C |
135 | { |
136 | fields: [ 'sharedInboxUrl' ] | |
137 | }, | |
57c36b27 C |
138 | { |
139 | fields: [ 'serverId' ] | |
140 | }, | |
141 | { | |
142 | fields: [ 'avatarId' ] | |
8cd72bd3 | 143 | }, |
8cd72bd3 C |
144 | { |
145 | fields: [ 'followersUrl' ] | |
50d6de9c C |
146 | } |
147 | ] | |
fadf619a C |
148 | }) |
149 | export class ActorModel extends Model<ActorModel> { | |
150 | ||
50d6de9c | 151 | @AllowNull(false) |
3acc5084 | 152 | @Column(DataType.ENUM(...values(ACTIVITY_PUB_ACTOR_TYPES))) |
50d6de9c C |
153 | type: ActivityPubActorType |
154 | ||
fadf619a | 155 | @AllowNull(false) |
e12a0092 | 156 | @Is('ActorPreferredUsername', value => throwIfNotValid(value, isActorPreferredUsernameValid, 'actor preferred username')) |
fadf619a | 157 | @Column |
e12a0092 | 158 | preferredUsername: string |
fadf619a C |
159 | |
160 | @AllowNull(false) | |
161 | @Is('ActorUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url')) | |
01de67b9 | 162 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max)) |
fadf619a C |
163 | url: string |
164 | ||
165 | @AllowNull(true) | |
1735c825 | 166 | @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPublicKeyValid, 'public key', true)) |
01de67b9 | 167 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.PUBLIC_KEY.max)) |
fadf619a C |
168 | publicKey: string |
169 | ||
170 | @AllowNull(true) | |
1735c825 | 171 | @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPrivateKeyValid, 'private key', true)) |
01de67b9 | 172 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.PRIVATE_KEY.max)) |
fadf619a C |
173 | privateKey: string |
174 | ||
175 | @AllowNull(false) | |
176 | @Is('ActorFollowersCount', value => throwIfNotValid(value, isActorFollowersCountValid, 'followers count')) | |
177 | @Column | |
178 | followersCount: number | |
179 | ||
180 | @AllowNull(false) | |
181 | @Is('ActorFollowersCount', value => throwIfNotValid(value, isActorFollowingCountValid, 'following count')) | |
182 | @Column | |
183 | followingCount: number | |
184 | ||
185 | @AllowNull(false) | |
186 | @Is('ActorInboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'inbox url')) | |
01de67b9 | 187 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max)) |
fadf619a C |
188 | inboxUrl: string |
189 | ||
0b5c385b C |
190 | @AllowNull(true) |
191 | @Is('ActorOutboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'outbox url', true)) | |
01de67b9 | 192 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max)) |
fadf619a C |
193 | outboxUrl: string |
194 | ||
47581df0 C |
195 | @AllowNull(true) |
196 | @Is('ActorSharedInboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'shared inbox url', true)) | |
01de67b9 | 197 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max)) |
fadf619a C |
198 | sharedInboxUrl: string |
199 | ||
0b5c385b C |
200 | @AllowNull(true) |
201 | @Is('ActorFollowersUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'followers url', true)) | |
01de67b9 | 202 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max)) |
fadf619a C |
203 | followersUrl: string |
204 | ||
0b5c385b C |
205 | @AllowNull(true) |
206 | @Is('ActorFollowingUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'following url', true)) | |
01de67b9 | 207 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max)) |
fadf619a C |
208 | followingUrl: string |
209 | ||
210 | @CreatedAt | |
211 | createdAt: Date | |
212 | ||
213 | @UpdatedAt | |
214 | updatedAt: Date | |
215 | ||
216 | @ForeignKey(() => AvatarModel) | |
217 | @Column | |
218 | avatarId: number | |
219 | ||
220 | @BelongsTo(() => AvatarModel, { | |
221 | foreignKey: { | |
222 | allowNull: true | |
223 | }, | |
f05a1c30 C |
224 | onDelete: 'set null', |
225 | hooks: true | |
fadf619a C |
226 | }) |
227 | Avatar: AvatarModel | |
228 | ||
50d6de9c | 229 | @HasMany(() => ActorFollowModel, { |
fadf619a | 230 | foreignKey: { |
50d6de9c | 231 | name: 'actorId', |
fadf619a C |
232 | allowNull: false |
233 | }, | |
cef534ed | 234 | as: 'ActorFollowings', |
fadf619a C |
235 | onDelete: 'cascade' |
236 | }) | |
54e74059 | 237 | ActorFollowing: ActorFollowModel[] |
fadf619a | 238 | |
50d6de9c | 239 | @HasMany(() => ActorFollowModel, { |
fadf619a | 240 | foreignKey: { |
50d6de9c | 241 | name: 'targetActorId', |
fadf619a C |
242 | allowNull: false |
243 | }, | |
54e74059 | 244 | as: 'ActorFollowers', |
fadf619a C |
245 | onDelete: 'cascade' |
246 | }) | |
54e74059 | 247 | ActorFollowers: ActorFollowModel[] |
fadf619a C |
248 | |
249 | @ForeignKey(() => ServerModel) | |
250 | @Column | |
251 | serverId: number | |
252 | ||
253 | @BelongsTo(() => ServerModel, { | |
254 | foreignKey: { | |
255 | allowNull: true | |
256 | }, | |
257 | onDelete: 'cascade' | |
258 | }) | |
259 | Server: ServerModel | |
260 | ||
50d6de9c C |
261 | @HasOne(() => AccountModel, { |
262 | foreignKey: { | |
c5a893d5 C |
263 | allowNull: true |
264 | }, | |
265 | onDelete: 'cascade', | |
266 | hooks: true | |
50d6de9c C |
267 | }) |
268 | Account: AccountModel | |
269 | ||
270 | @HasOne(() => VideoChannelModel, { | |
271 | foreignKey: { | |
c5a893d5 C |
272 | allowNull: true |
273 | }, | |
274 | onDelete: 'cascade', | |
275 | hooks: true | |
50d6de9c C |
276 | }) |
277 | VideoChannel: VideoChannelModel | |
278 | ||
453e83ea | 279 | static load (id: number): Bluebird<MActor> { |
9b39106d | 280 | return ActorModel.unscoped().findByPk(id) |
50d6de9c C |
281 | } |
282 | ||
453e83ea C |
283 | static loadFull (id: number): Bluebird<MActorFull> { |
284 | return ActorModel.scope(ScopeNames.FULL).findByPk(id) | |
285 | } | |
286 | ||
941c5eac | 287 | static loadFromAccountByVideoId (videoId: number, transaction: Transaction): Bluebird<MActor> { |
e5565833 C |
288 | const query = { |
289 | include: [ | |
290 | { | |
291 | attributes: [ 'id' ], | |
292 | model: AccountModel.unscoped(), | |
293 | required: true, | |
294 | include: [ | |
295 | { | |
296 | attributes: [ 'id' ], | |
297 | model: VideoChannelModel.unscoped(), | |
298 | required: true, | |
3acc5084 C |
299 | include: [ |
300 | { | |
301 | attributes: [ 'id' ], | |
302 | model: VideoModel.unscoped(), | |
303 | required: true, | |
304 | where: { | |
305 | id: videoId | |
306 | } | |
e5565833 | 307 | } |
3acc5084 | 308 | ] |
e5565833 C |
309 | } |
310 | ] | |
311 | } | |
312 | ], | |
313 | transaction | |
314 | } | |
315 | ||
3acc5084 | 316 | return ActorModel.unscoped().findOne(query) |
e5565833 C |
317 | } |
318 | ||
d4defe07 C |
319 | static isActorUrlExist (url: string) { |
320 | const query = { | |
321 | raw: true, | |
322 | where: { | |
323 | url | |
324 | } | |
325 | } | |
326 | ||
327 | return ActorModel.unscoped().findOne(query) | |
328 | .then(a => !!a) | |
329 | } | |
330 | ||
941c5eac | 331 | static listByFollowersUrls (followersUrls: string[], transaction?: Transaction): Bluebird<MActorFull[]> { |
fadf619a C |
332 | const query = { |
333 | where: { | |
334 | followersUrl: { | |
941c5eac | 335 | [ Op.in ]: followersUrls |
fadf619a C |
336 | } |
337 | }, | |
338 | transaction | |
339 | } | |
340 | ||
50d6de9c C |
341 | return ActorModel.scope(ScopeNames.FULL).findAll(query) |
342 | } | |
343 | ||
941c5eac | 344 | static loadLocalByName (preferredUsername: string, transaction?: Transaction): Bluebird<MActorFull> { |
50d6de9c C |
345 | const query = { |
346 | where: { | |
e12a0092 | 347 | preferredUsername, |
50d6de9c | 348 | serverId: null |
8a19bee1 C |
349 | }, |
350 | transaction | |
50d6de9c C |
351 | } |
352 | ||
353 | return ActorModel.scope(ScopeNames.FULL).findOne(query) | |
354 | } | |
355 | ||
453e83ea | 356 | static loadByNameAndHost (preferredUsername: string, host: string): Bluebird<MActorFull> { |
50d6de9c C |
357 | const query = { |
358 | where: { | |
e12a0092 | 359 | preferredUsername |
50d6de9c C |
360 | }, |
361 | include: [ | |
362 | { | |
363 | model: ServerModel, | |
364 | required: true, | |
365 | where: { | |
366 | host | |
367 | } | |
368 | } | |
369 | ] | |
370 | } | |
371 | ||
372 | return ActorModel.scope(ScopeNames.FULL).findOne(query) | |
373 | } | |
374 | ||
941c5eac | 375 | static loadByUrl (url: string, transaction?: Transaction): Bluebird<MActorAccountChannelId> { |
e587e0ec C |
376 | const query = { |
377 | where: { | |
378 | url | |
379 | }, | |
380 | transaction, | |
381 | include: [ | |
382 | { | |
383 | attributes: [ 'id' ], | |
384 | model: AccountModel.unscoped(), | |
385 | required: false | |
386 | }, | |
387 | { | |
388 | attributes: [ 'id' ], | |
389 | model: VideoChannelModel.unscoped(), | |
390 | required: false | |
391 | } | |
392 | ] | |
393 | } | |
394 | ||
395 | return ActorModel.unscoped().findOne(query) | |
396 | } | |
397 | ||
941c5eac | 398 | static loadByUrlAndPopulateAccountAndChannel (url: string, transaction?: Transaction): Bluebird<MActorFull> { |
50d6de9c C |
399 | const query = { |
400 | where: { | |
401 | url | |
402 | }, | |
403 | transaction | |
404 | } | |
405 | ||
406 | return ActorModel.scope(ScopeNames.FULL).findOne(query) | |
fadf619a C |
407 | } |
408 | ||
32b2b43c | 409 | static incrementFollows (id: number, column: 'followersCount' | 'followingCount', by: number) { |
3acc5084 | 410 | return ActorModel.increment(column, { |
32b2b43c C |
411 | by, |
412 | where: { | |
413 | id | |
414 | } | |
415 | }) | |
416 | } | |
417 | ||
47581df0 C |
418 | getSharedInbox (this: MActorWithInboxes) { |
419 | return this.sharedInboxUrl || this.inboxUrl | |
420 | } | |
421 | ||
1ca9f7c3 | 422 | toFormattedSummaryJSON (this: MActorSummaryFormattable) { |
fadf619a C |
423 | let avatar: Avatar = null |
424 | if (this.Avatar) { | |
c5911fd3 | 425 | avatar = this.Avatar.toFormattedJSON() |
fadf619a C |
426 | } |
427 | ||
fadf619a | 428 | return { |
4cb6d457 | 429 | url: this.url, |
60650c77 | 430 | name: this.preferredUsername, |
e12a0092 | 431 | host: this.getHost(), |
1ca9f7c3 C |
432 | avatar |
433 | } | |
434 | } | |
435 | ||
436 | toFormattedJSON (this: MActorFormattable) { | |
437 | const base = this.toFormattedSummaryJSON() | |
438 | ||
439 | return Object.assign(base, { | |
440 | id: this.id, | |
c48e82b5 | 441 | hostRedundancyAllowed: this.getRedundancyAllowed(), |
fadf619a C |
442 | followingCount: this.followingCount, |
443 | followersCount: this.followersCount, | |
60650c77 C |
444 | createdAt: this.createdAt, |
445 | updatedAt: this.updatedAt | |
1ca9f7c3 | 446 | }) |
fadf619a C |
447 | } |
448 | ||
8424c402 | 449 | toActivityPubObject (this: MActorAP, name: string) { |
c5911fd3 C |
450 | let icon = undefined |
451 | if (this.avatarId) { | |
452 | const extension = extname(this.Avatar.filename) | |
453 | icon = { | |
454 | type: 'Image', | |
455 | mediaType: extension === '.png' ? 'image/png' : 'image/jpeg', | |
456 | url: this.getAvatarUrl() | |
457 | } | |
458 | } | |
459 | ||
fadf619a | 460 | const json = { |
8424c402 | 461 | type: this.type, |
fadf619a C |
462 | id: this.url, |
463 | following: this.getFollowingUrl(), | |
464 | followers: this.getFollowersUrl(), | |
418d092a | 465 | playlists: this.getPlaylistsUrl(), |
fadf619a C |
466 | inbox: this.inboxUrl, |
467 | outbox: this.outboxUrl, | |
e12a0092 | 468 | preferredUsername: this.preferredUsername, |
fadf619a | 469 | url: this.url, |
e12a0092 | 470 | name, |
fadf619a C |
471 | endpoints: { |
472 | sharedInbox: this.sharedInboxUrl | |
473 | }, | |
fadf619a C |
474 | publicKey: { |
475 | id: this.getPublicKeyUrl(), | |
476 | owner: this.url, | |
477 | publicKeyPem: this.publicKey | |
c5911fd3 C |
478 | }, |
479 | icon | |
fadf619a C |
480 | } |
481 | ||
482 | return activityPubContextify(json) | |
483 | } | |
484 | ||
941c5eac | 485 | getFollowerSharedInboxUrls (t: Transaction) { |
fadf619a C |
486 | const query = { |
487 | attributes: [ 'sharedInboxUrl' ], | |
488 | include: [ | |
489 | { | |
54e74059 C |
490 | attribute: [], |
491 | model: ActorFollowModel.unscoped(), | |
fadf619a | 492 | required: true, |
d6e99e53 | 493 | as: 'ActorFollowing', |
fadf619a | 494 | where: { |
54e74059 | 495 | state: 'accepted', |
50d6de9c | 496 | targetActorId: this.id |
fadf619a C |
497 | } |
498 | } | |
499 | ], | |
500 | transaction: t | |
501 | } | |
502 | ||
503 | return ActorModel.findAll(query) | |
504 | .then(accounts => accounts.map(a => a.sharedInboxUrl)) | |
505 | } | |
506 | ||
507 | getFollowingUrl () { | |
508 | return this.url + '/following' | |
509 | } | |
510 | ||
511 | getFollowersUrl () { | |
512 | return this.url + '/followers' | |
513 | } | |
514 | ||
418d092a C |
515 | getPlaylistsUrl () { |
516 | return this.url + '/playlists' | |
517 | } | |
518 | ||
fadf619a C |
519 | getPublicKeyUrl () { |
520 | return this.url + '#main-key' | |
521 | } | |
522 | ||
523 | isOwned () { | |
524 | return this.serverId === null | |
525 | } | |
e12a0092 | 526 | |
1ca9f7c3 | 527 | getWebfingerUrl (this: MActorServer) { |
e12a0092 C |
528 | return 'acct:' + this.preferredUsername + '@' + this.getHost() |
529 | } | |
530 | ||
80e36cd9 AB |
531 | getIdentifier () { |
532 | return this.Server ? `${this.preferredUsername}@${this.Server.host}` : this.preferredUsername | |
533 | } | |
534 | ||
1ca9f7c3 | 535 | getHost (this: MActorHost) { |
6dd9de95 | 536 | return this.Server ? this.Server.host : WEBSERVER.HOST |
e12a0092 | 537 | } |
c5911fd3 | 538 | |
b5fecbf4 | 539 | getRedundancyAllowed () { |
c48e82b5 C |
540 | return this.Server ? this.Server.redundancyAllowed : false |
541 | } | |
542 | ||
c5911fd3 C |
543 | getAvatarUrl () { |
544 | if (!this.avatarId) return undefined | |
545 | ||
557b13ae | 546 | return WEBSERVER.URL + this.Avatar.getStaticPath() |
c5911fd3 | 547 | } |
a5625b41 C |
548 | |
549 | isOutdated () { | |
550 | if (this.isOwned()) return false | |
551 | ||
9f79ade6 | 552 | return isOutdated(this, ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL) |
a5625b41 | 553 | } |
fadf619a | 554 | } |