]>
Commit | Line | Data |
---|---|---|
1 | import { values } from 'lodash' | |
2 | import { extname } from 'path' | |
3 | import { | |
4 | AllowNull, | |
5 | BelongsTo, | |
6 | Column, | |
7 | CreatedAt, | |
8 | DataType, | |
9 | DefaultScope, | |
10 | ForeignKey, | |
11 | HasMany, | |
12 | HasOne, | |
13 | Is, | |
14 | Model, | |
15 | Scopes, | |
16 | Table, | |
17 | UpdatedAt | |
18 | } from 'sequelize-typescript' | |
19 | import { ActivityIconObject, ActivityPubActorType } from '../../../shared/models/activitypub' | |
20 | import { Avatar } from '../../../shared/models/avatars/avatar.model' | |
21 | import { activityPubContextify } from '../../helpers/activitypub' | |
22 | import { | |
23 | isActorFollowersCountValid, | |
24 | isActorFollowingCountValid, | |
25 | isActorPreferredUsernameValid, | |
26 | isActorPrivateKeyValid, | |
27 | isActorPublicKeyValid | |
28 | } from '../../helpers/custom-validators/activitypub/actor' | |
29 | import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc' | |
30 | import { ACTIVITY_PUB, ACTIVITY_PUB_ACTOR_TYPES, CONSTRAINTS_FIELDS, SERVER_ACTOR_NAME, WEBSERVER } from '../../initializers/constants' | |
31 | import { AccountModel } from '../account/account' | |
32 | import { AvatarModel } from '../avatar/avatar' | |
33 | import { ServerModel } from '../server/server' | |
34 | import { isOutdated, throwIfNotValid } from '../utils' | |
35 | import { VideoChannelModel } from '../video/video-channel' | |
36 | import { ActorFollowModel } from './actor-follow' | |
37 | import { VideoModel } from '../video/video' | |
38 | import { | |
39 | MActor, | |
40 | MActorAccountChannelId, | |
41 | MActorAP, | |
42 | MActorFormattable, | |
43 | MActorFull, | |
44 | MActorHost, | |
45 | MActorServer, | |
46 | MActorSummaryFormattable, MActorUrl, | |
47 | MActorWithInboxes | |
48 | } from '../../types/models' | |
49 | import * as Bluebird from 'bluebird' | |
50 | import { Op, Transaction, literal } from 'sequelize' | |
51 | import { ModelCache } from '@server/models/model-cache' | |
52 | ||
53 | enum ScopeNames { | |
54 | FULL = 'FULL' | |
55 | } | |
56 | ||
57 | export const unusedActorAttributesForAPI = [ | |
58 | 'publicKey', | |
59 | 'privateKey', | |
60 | 'inboxUrl', | |
61 | 'outboxUrl', | |
62 | 'sharedInboxUrl', | |
63 | 'followersUrl', | |
64 | 'followingUrl', | |
65 | 'createdAt', | |
66 | 'updatedAt' | |
67 | ] | |
68 | ||
69 | @DefaultScope(() => ({ | |
70 | include: [ | |
71 | { | |
72 | model: ServerModel, | |
73 | required: false | |
74 | }, | |
75 | { | |
76 | model: AvatarModel, | |
77 | required: false | |
78 | } | |
79 | ] | |
80 | })) | |
81 | @Scopes(() => ({ | |
82 | [ScopeNames.FULL]: { | |
83 | include: [ | |
84 | { | |
85 | model: AccountModel.unscoped(), | |
86 | required: false | |
87 | }, | |
88 | { | |
89 | model: VideoChannelModel.unscoped(), | |
90 | required: false, | |
91 | include: [ | |
92 | { | |
93 | model: AccountModel, | |
94 | required: true | |
95 | } | |
96 | ] | |
97 | }, | |
98 | { | |
99 | model: ServerModel, | |
100 | required: false | |
101 | }, | |
102 | { | |
103 | model: AvatarModel, | |
104 | required: false | |
105 | } | |
106 | ] | |
107 | } | |
108 | })) | |
109 | @Table({ | |
110 | tableName: 'actor', | |
111 | indexes: [ | |
112 | { | |
113 | fields: [ 'url' ], | |
114 | unique: true | |
115 | }, | |
116 | { | |
117 | fields: [ 'preferredUsername', 'serverId' ], | |
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 | } | |
131 | }, | |
132 | { | |
133 | fields: [ 'inboxUrl', 'sharedInboxUrl' ] | |
134 | }, | |
135 | { | |
136 | fields: [ 'sharedInboxUrl' ] | |
137 | }, | |
138 | { | |
139 | fields: [ 'serverId' ] | |
140 | }, | |
141 | { | |
142 | fields: [ 'avatarId' ] | |
143 | }, | |
144 | { | |
145 | fields: [ 'followersUrl' ] | |
146 | } | |
147 | ] | |
148 | }) | |
149 | export class ActorModel extends Model<ActorModel> { | |
150 | ||
151 | @AllowNull(false) | |
152 | @Column(DataType.ENUM(...values(ACTIVITY_PUB_ACTOR_TYPES))) | |
153 | type: ActivityPubActorType | |
154 | ||
155 | @AllowNull(false) | |
156 | @Is('ActorPreferredUsername', value => throwIfNotValid(value, isActorPreferredUsernameValid, 'actor preferred username')) | |
157 | @Column | |
158 | preferredUsername: string | |
159 | ||
160 | @AllowNull(false) | |
161 | @Is('ActorUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url')) | |
162 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max)) | |
163 | url: string | |
164 | ||
165 | @AllowNull(true) | |
166 | @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPublicKeyValid, 'public key', true)) | |
167 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.PUBLIC_KEY.max)) | |
168 | publicKey: string | |
169 | ||
170 | @AllowNull(true) | |
171 | @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPrivateKeyValid, 'private key', true)) | |
172 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.PRIVATE_KEY.max)) | |
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')) | |
187 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max)) | |
188 | inboxUrl: string | |
189 | ||
190 | @AllowNull(true) | |
191 | @Is('ActorOutboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'outbox url', true)) | |
192 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max)) | |
193 | outboxUrl: string | |
194 | ||
195 | @AllowNull(true) | |
196 | @Is('ActorSharedInboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'shared inbox url', true)) | |
197 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max)) | |
198 | sharedInboxUrl: string | |
199 | ||
200 | @AllowNull(true) | |
201 | @Is('ActorFollowersUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'followers url', true)) | |
202 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max)) | |
203 | followersUrl: string | |
204 | ||
205 | @AllowNull(true) | |
206 | @Is('ActorFollowingUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'following url', true)) | |
207 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max)) | |
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 | }, | |
224 | onDelete: 'set null', | |
225 | hooks: true | |
226 | }) | |
227 | Avatar: AvatarModel | |
228 | ||
229 | @HasMany(() => ActorFollowModel, { | |
230 | foreignKey: { | |
231 | name: 'actorId', | |
232 | allowNull: false | |
233 | }, | |
234 | as: 'ActorFollowings', | |
235 | onDelete: 'cascade' | |
236 | }) | |
237 | ActorFollowing: ActorFollowModel[] | |
238 | ||
239 | @HasMany(() => ActorFollowModel, { | |
240 | foreignKey: { | |
241 | name: 'targetActorId', | |
242 | allowNull: false | |
243 | }, | |
244 | as: 'ActorFollowers', | |
245 | onDelete: 'cascade' | |
246 | }) | |
247 | ActorFollowers: ActorFollowModel[] | |
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 | ||
261 | @HasOne(() => AccountModel, { | |
262 | foreignKey: { | |
263 | allowNull: true | |
264 | }, | |
265 | onDelete: 'cascade', | |
266 | hooks: true | |
267 | }) | |
268 | Account: AccountModel | |
269 | ||
270 | @HasOne(() => VideoChannelModel, { | |
271 | foreignKey: { | |
272 | allowNull: true | |
273 | }, | |
274 | onDelete: 'cascade', | |
275 | hooks: true | |
276 | }) | |
277 | VideoChannel: VideoChannelModel | |
278 | ||
279 | static load (id: number): Bluebird<MActor> { | |
280 | return ActorModel.unscoped().findByPk(id) | |
281 | } | |
282 | ||
283 | static loadFull (id: number): Bluebird<MActorFull> { | |
284 | return ActorModel.scope(ScopeNames.FULL).findByPk(id) | |
285 | } | |
286 | ||
287 | static loadFromAccountByVideoId (videoId: number, transaction: Transaction): Bluebird<MActor> { | |
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, | |
299 | include: [ | |
300 | { | |
301 | attributes: [ 'id' ], | |
302 | model: VideoModel.unscoped(), | |
303 | required: true, | |
304 | where: { | |
305 | id: videoId | |
306 | } | |
307 | } | |
308 | ] | |
309 | } | |
310 | ] | |
311 | } | |
312 | ], | |
313 | transaction | |
314 | } | |
315 | ||
316 | return ActorModel.unscoped().findOne(query) | |
317 | } | |
318 | ||
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 | ||
331 | static listByFollowersUrls (followersUrls: string[], transaction?: Transaction): Bluebird<MActorFull[]> { | |
332 | const query = { | |
333 | where: { | |
334 | followersUrl: { | |
335 | [Op.in]: followersUrls | |
336 | } | |
337 | }, | |
338 | transaction | |
339 | } | |
340 | ||
341 | return ActorModel.scope(ScopeNames.FULL).findAll(query) | |
342 | } | |
343 | ||
344 | static loadLocalByName (preferredUsername: string, transaction?: Transaction): Bluebird<MActorFull> { | |
345 | const fun = () => { | |
346 | const query = { | |
347 | where: { | |
348 | preferredUsername, | |
349 | serverId: null | |
350 | }, | |
351 | transaction | |
352 | } | |
353 | ||
354 | return ActorModel.scope(ScopeNames.FULL) | |
355 | .findOne(query) | |
356 | } | |
357 | ||
358 | return ModelCache.Instance.doCache({ | |
359 | cacheType: 'local-actor-name', | |
360 | key: preferredUsername, | |
361 | // The server actor never change, so we can easily cache it | |
362 | whitelist: () => preferredUsername === SERVER_ACTOR_NAME, | |
363 | fun | |
364 | }) | |
365 | } | |
366 | ||
367 | static loadLocalUrlByName (preferredUsername: string, transaction?: Transaction): Bluebird<MActorUrl> { | |
368 | const fun = () => { | |
369 | const query = { | |
370 | attributes: [ 'url' ], | |
371 | where: { | |
372 | preferredUsername, | |
373 | serverId: null | |
374 | }, | |
375 | transaction | |
376 | } | |
377 | ||
378 | return ActorModel.unscoped() | |
379 | .findOne(query) | |
380 | } | |
381 | ||
382 | return ModelCache.Instance.doCache({ | |
383 | cacheType: 'local-actor-name', | |
384 | key: preferredUsername, | |
385 | // The server actor never change, so we can easily cache it | |
386 | whitelist: () => preferredUsername === SERVER_ACTOR_NAME, | |
387 | fun | |
388 | }) | |
389 | } | |
390 | ||
391 | static loadByNameAndHost (preferredUsername: string, host: string): Bluebird<MActorFull> { | |
392 | const query = { | |
393 | where: { | |
394 | preferredUsername | |
395 | }, | |
396 | include: [ | |
397 | { | |
398 | model: ServerModel, | |
399 | required: true, | |
400 | where: { | |
401 | host | |
402 | } | |
403 | } | |
404 | ] | |
405 | } | |
406 | ||
407 | return ActorModel.scope(ScopeNames.FULL).findOne(query) | |
408 | } | |
409 | ||
410 | static loadByUrl (url: string, transaction?: Transaction): Bluebird<MActorAccountChannelId> { | |
411 | const query = { | |
412 | where: { | |
413 | url | |
414 | }, | |
415 | transaction, | |
416 | include: [ | |
417 | { | |
418 | attributes: [ 'id' ], | |
419 | model: AccountModel.unscoped(), | |
420 | required: false | |
421 | }, | |
422 | { | |
423 | attributes: [ 'id' ], | |
424 | model: VideoChannelModel.unscoped(), | |
425 | required: false | |
426 | } | |
427 | ] | |
428 | } | |
429 | ||
430 | return ActorModel.unscoped().findOne(query) | |
431 | } | |
432 | ||
433 | static loadByUrlAndPopulateAccountAndChannel (url: string, transaction?: Transaction): Bluebird<MActorFull> { | |
434 | const query = { | |
435 | where: { | |
436 | url | |
437 | }, | |
438 | transaction | |
439 | } | |
440 | ||
441 | return ActorModel.scope(ScopeNames.FULL).findOne(query) | |
442 | } | |
443 | ||
444 | static rebuildFollowsCount (ofId: number, type: 'followers' | 'following', transaction?: Transaction) { | |
445 | const sanitizedOfId = parseInt(ofId + '', 10) | |
446 | const where = { id: sanitizedOfId } | |
447 | ||
448 | let columnToUpdate: string | |
449 | let columnOfCount: string | |
450 | ||
451 | if (type === 'followers') { | |
452 | columnToUpdate = 'followersCount' | |
453 | columnOfCount = 'targetActorId' | |
454 | } else { | |
455 | columnToUpdate = 'followingCount' | |
456 | columnOfCount = 'actorId' | |
457 | } | |
458 | ||
459 | return ActorModel.update({ | |
460 | [columnToUpdate]: literal(`(SELECT COUNT(*) FROM "actorFollow" WHERE "${columnOfCount}" = ${sanitizedOfId})`) | |
461 | }, { where, transaction }) | |
462 | } | |
463 | ||
464 | static loadAccountActorByVideoId (videoId: number): Bluebird<MActor> { | |
465 | const query = { | |
466 | include: [ | |
467 | { | |
468 | attributes: [ 'id' ], | |
469 | model: AccountModel.unscoped(), | |
470 | required: true, | |
471 | include: [ | |
472 | { | |
473 | attributes: [ 'id', 'accountId' ], | |
474 | model: VideoChannelModel.unscoped(), | |
475 | required: true, | |
476 | include: [ | |
477 | { | |
478 | attributes: [ 'id', 'channelId' ], | |
479 | model: VideoModel.unscoped(), | |
480 | where: { | |
481 | id: videoId | |
482 | } | |
483 | } | |
484 | ] | |
485 | } | |
486 | ] | |
487 | } | |
488 | ] | |
489 | } | |
490 | ||
491 | return ActorModel.unscoped().findOne(query) | |
492 | } | |
493 | ||
494 | getSharedInbox (this: MActorWithInboxes) { | |
495 | return this.sharedInboxUrl || this.inboxUrl | |
496 | } | |
497 | ||
498 | toFormattedSummaryJSON (this: MActorSummaryFormattable) { | |
499 | let avatar: Avatar = null | |
500 | if (this.Avatar) { | |
501 | avatar = this.Avatar.toFormattedJSON() | |
502 | } | |
503 | ||
504 | return { | |
505 | url: this.url, | |
506 | name: this.preferredUsername, | |
507 | host: this.getHost(), | |
508 | avatar | |
509 | } | |
510 | } | |
511 | ||
512 | toFormattedJSON (this: MActorFormattable) { | |
513 | const base = this.toFormattedSummaryJSON() | |
514 | ||
515 | return Object.assign(base, { | |
516 | id: this.id, | |
517 | hostRedundancyAllowed: this.getRedundancyAllowed(), | |
518 | followingCount: this.followingCount, | |
519 | followersCount: this.followersCount, | |
520 | createdAt: this.createdAt, | |
521 | updatedAt: this.updatedAt | |
522 | }) | |
523 | } | |
524 | ||
525 | toActivityPubObject (this: MActorAP, name: string) { | |
526 | let icon: ActivityIconObject | |
527 | ||
528 | if (this.avatarId) { | |
529 | const extension = extname(this.Avatar.filename) | |
530 | ||
531 | icon = { | |
532 | type: 'Image', | |
533 | mediaType: extension === '.png' ? 'image/png' : 'image/jpeg', | |
534 | url: this.getAvatarUrl() | |
535 | } | |
536 | } | |
537 | ||
538 | const json = { | |
539 | type: this.type, | |
540 | id: this.url, | |
541 | following: this.getFollowingUrl(), | |
542 | followers: this.getFollowersUrl(), | |
543 | playlists: this.getPlaylistsUrl(), | |
544 | inbox: this.inboxUrl, | |
545 | outbox: this.outboxUrl, | |
546 | preferredUsername: this.preferredUsername, | |
547 | url: this.url, | |
548 | name, | |
549 | endpoints: { | |
550 | sharedInbox: this.sharedInboxUrl | |
551 | }, | |
552 | publicKey: { | |
553 | id: this.getPublicKeyUrl(), | |
554 | owner: this.url, | |
555 | publicKeyPem: this.publicKey | |
556 | }, | |
557 | icon | |
558 | } | |
559 | ||
560 | return activityPubContextify(json) | |
561 | } | |
562 | ||
563 | getFollowerSharedInboxUrls (t: Transaction) { | |
564 | const query = { | |
565 | attributes: [ 'sharedInboxUrl' ], | |
566 | include: [ | |
567 | { | |
568 | attribute: [], | |
569 | model: ActorFollowModel.unscoped(), | |
570 | required: true, | |
571 | as: 'ActorFollowing', | |
572 | where: { | |
573 | state: 'accepted', | |
574 | targetActorId: this.id | |
575 | } | |
576 | } | |
577 | ], | |
578 | transaction: t | |
579 | } | |
580 | ||
581 | return ActorModel.findAll(query) | |
582 | .then(accounts => accounts.map(a => a.sharedInboxUrl)) | |
583 | } | |
584 | ||
585 | getFollowingUrl () { | |
586 | return this.url + '/following' | |
587 | } | |
588 | ||
589 | getFollowersUrl () { | |
590 | return this.url + '/followers' | |
591 | } | |
592 | ||
593 | getPlaylistsUrl () { | |
594 | return this.url + '/playlists' | |
595 | } | |
596 | ||
597 | getPublicKeyUrl () { | |
598 | return this.url + '#main-key' | |
599 | } | |
600 | ||
601 | isOwned () { | |
602 | return this.serverId === null | |
603 | } | |
604 | ||
605 | getWebfingerUrl (this: MActorServer) { | |
606 | return 'acct:' + this.preferredUsername + '@' + this.getHost() | |
607 | } | |
608 | ||
609 | getIdentifier () { | |
610 | return this.Server ? `${this.preferredUsername}@${this.Server.host}` : this.preferredUsername | |
611 | } | |
612 | ||
613 | getHost (this: MActorHost) { | |
614 | return this.Server ? this.Server.host : WEBSERVER.HOST | |
615 | } | |
616 | ||
617 | getRedundancyAllowed () { | |
618 | return this.Server ? this.Server.redundancyAllowed : false | |
619 | } | |
620 | ||
621 | getAvatarUrl () { | |
622 | if (!this.avatarId) return undefined | |
623 | ||
624 | return WEBSERVER.URL + this.Avatar.getStaticPath() | |
625 | } | |
626 | ||
627 | isOutdated () { | |
628 | if (this.isOwned()) return false | |
629 | ||
630 | return isOutdated(this, ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL) | |
631 | } | |
632 | } |