]>
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 { 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, | |
47 | MActorWithInboxes | |
48 | } from '../../typings/models' | |
49 | import * as Bluebird from 'bluebird' | |
50 | import { Op, Transaction } from 'sequelize' | |
51 | ||
52 | enum ScopeNames { | |
53 | FULL = 'FULL' | |
54 | } | |
55 | ||
56 | export const unusedActorAttributesForAPI = [ | |
57 | 'publicKey', | |
58 | 'privateKey', | |
59 | 'inboxUrl', | |
60 | 'outboxUrl', | |
61 | 'sharedInboxUrl', | |
62 | 'followersUrl', | |
63 | 'followingUrl', | |
64 | 'url', | |
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 | private static cache: { [ id: string ]: any } = {} | |
280 | ||
281 | static load (id: number): Bluebird<MActor> { | |
282 | return ActorModel.unscoped().findByPk(id) | |
283 | } | |
284 | ||
285 | static loadFull (id: number): Bluebird<MActorFull> { | |
286 | return ActorModel.scope(ScopeNames.FULL).findByPk(id) | |
287 | } | |
288 | ||
289 | static loadFromAccountByVideoId (videoId: number, transaction: Transaction): Bluebird<MActor> { | |
290 | const query = { | |
291 | include: [ | |
292 | { | |
293 | attributes: [ 'id' ], | |
294 | model: AccountModel.unscoped(), | |
295 | required: true, | |
296 | include: [ | |
297 | { | |
298 | attributes: [ 'id' ], | |
299 | model: VideoChannelModel.unscoped(), | |
300 | required: true, | |
301 | include: [ | |
302 | { | |
303 | attributes: [ 'id' ], | |
304 | model: VideoModel.unscoped(), | |
305 | required: true, | |
306 | where: { | |
307 | id: videoId | |
308 | } | |
309 | } | |
310 | ] | |
311 | } | |
312 | ] | |
313 | } | |
314 | ], | |
315 | transaction | |
316 | } | |
317 | ||
318 | return ActorModel.unscoped().findOne(query) | |
319 | } | |
320 | ||
321 | static isActorUrlExist (url: string) { | |
322 | const query = { | |
323 | raw: true, | |
324 | where: { | |
325 | url | |
326 | } | |
327 | } | |
328 | ||
329 | return ActorModel.unscoped().findOne(query) | |
330 | .then(a => !!a) | |
331 | } | |
332 | ||
333 | static listByFollowersUrls (followersUrls: string[], transaction?: Transaction): Bluebird<MActorFull[]> { | |
334 | const query = { | |
335 | where: { | |
336 | followersUrl: { | |
337 | [ Op.in ]: followersUrls | |
338 | } | |
339 | }, | |
340 | transaction | |
341 | } | |
342 | ||
343 | return ActorModel.scope(ScopeNames.FULL).findAll(query) | |
344 | } | |
345 | ||
346 | static loadLocalByName (preferredUsername: string, transaction?: Transaction): Bluebird<MActorFull> { | |
347 | // The server actor never change, so we can easily cache it | |
348 | if (preferredUsername === SERVER_ACTOR_NAME && ActorModel.cache[preferredUsername]) { | |
349 | return Bluebird.resolve(ActorModel.cache[preferredUsername]) | |
350 | } | |
351 | ||
352 | const query = { | |
353 | where: { | |
354 | preferredUsername, | |
355 | serverId: null | |
356 | }, | |
357 | transaction | |
358 | } | |
359 | ||
360 | return ActorModel.scope(ScopeNames.FULL) | |
361 | .findOne(query) | |
362 | .then(actor => { | |
363 | if (preferredUsername === SERVER_ACTOR_NAME) { | |
364 | ActorModel.cache[ preferredUsername ] = actor | |
365 | } | |
366 | ||
367 | return actor | |
368 | }) | |
369 | } | |
370 | ||
371 | static loadByNameAndHost (preferredUsername: string, host: string): Bluebird<MActorFull> { | |
372 | const query = { | |
373 | where: { | |
374 | preferredUsername | |
375 | }, | |
376 | include: [ | |
377 | { | |
378 | model: ServerModel, | |
379 | required: true, | |
380 | where: { | |
381 | host | |
382 | } | |
383 | } | |
384 | ] | |
385 | } | |
386 | ||
387 | return ActorModel.scope(ScopeNames.FULL).findOne(query) | |
388 | } | |
389 | ||
390 | static loadByUrl (url: string, transaction?: Transaction): Bluebird<MActorAccountChannelId> { | |
391 | const query = { | |
392 | where: { | |
393 | url | |
394 | }, | |
395 | transaction, | |
396 | include: [ | |
397 | { | |
398 | attributes: [ 'id' ], | |
399 | model: AccountModel.unscoped(), | |
400 | required: false | |
401 | }, | |
402 | { | |
403 | attributes: [ 'id' ], | |
404 | model: VideoChannelModel.unscoped(), | |
405 | required: false | |
406 | } | |
407 | ] | |
408 | } | |
409 | ||
410 | return ActorModel.unscoped().findOne(query) | |
411 | } | |
412 | ||
413 | static loadByUrlAndPopulateAccountAndChannel (url: string, transaction?: Transaction): Bluebird<MActorFull> { | |
414 | const query = { | |
415 | where: { | |
416 | url | |
417 | }, | |
418 | transaction | |
419 | } | |
420 | ||
421 | return ActorModel.scope(ScopeNames.FULL).findOne(query) | |
422 | } | |
423 | ||
424 | static incrementFollows (id: number, column: 'followersCount' | 'followingCount', by: number) { | |
425 | return ActorModel.increment(column, { | |
426 | by, | |
427 | where: { | |
428 | id | |
429 | } | |
430 | }) | |
431 | } | |
432 | ||
433 | getSharedInbox (this: MActorWithInboxes) { | |
434 | return this.sharedInboxUrl || this.inboxUrl | |
435 | } | |
436 | ||
437 | toFormattedSummaryJSON (this: MActorSummaryFormattable) { | |
438 | let avatar: Avatar = null | |
439 | if (this.Avatar) { | |
440 | avatar = this.Avatar.toFormattedJSON() | |
441 | } | |
442 | ||
443 | return { | |
444 | url: this.url, | |
445 | name: this.preferredUsername, | |
446 | host: this.getHost(), | |
447 | avatar | |
448 | } | |
449 | } | |
450 | ||
451 | toFormattedJSON (this: MActorFormattable) { | |
452 | const base = this.toFormattedSummaryJSON() | |
453 | ||
454 | return Object.assign(base, { | |
455 | id: this.id, | |
456 | hostRedundancyAllowed: this.getRedundancyAllowed(), | |
457 | followingCount: this.followingCount, | |
458 | followersCount: this.followersCount, | |
459 | createdAt: this.createdAt, | |
460 | updatedAt: this.updatedAt | |
461 | }) | |
462 | } | |
463 | ||
464 | toActivityPubObject (this: MActorAP, name: string) { | |
465 | let icon = undefined | |
466 | if (this.avatarId) { | |
467 | const extension = extname(this.Avatar.filename) | |
468 | icon = { | |
469 | type: 'Image', | |
470 | mediaType: extension === '.png' ? 'image/png' : 'image/jpeg', | |
471 | url: this.getAvatarUrl() | |
472 | } | |
473 | } | |
474 | ||
475 | const json = { | |
476 | type: this.type, | |
477 | id: this.url, | |
478 | following: this.getFollowingUrl(), | |
479 | followers: this.getFollowersUrl(), | |
480 | playlists: this.getPlaylistsUrl(), | |
481 | inbox: this.inboxUrl, | |
482 | outbox: this.outboxUrl, | |
483 | preferredUsername: this.preferredUsername, | |
484 | url: this.url, | |
485 | name, | |
486 | endpoints: { | |
487 | sharedInbox: this.sharedInboxUrl | |
488 | }, | |
489 | publicKey: { | |
490 | id: this.getPublicKeyUrl(), | |
491 | owner: this.url, | |
492 | publicKeyPem: this.publicKey | |
493 | }, | |
494 | icon | |
495 | } | |
496 | ||
497 | return activityPubContextify(json) | |
498 | } | |
499 | ||
500 | getFollowerSharedInboxUrls (t: Transaction) { | |
501 | const query = { | |
502 | attributes: [ 'sharedInboxUrl' ], | |
503 | include: [ | |
504 | { | |
505 | attribute: [], | |
506 | model: ActorFollowModel.unscoped(), | |
507 | required: true, | |
508 | as: 'ActorFollowing', | |
509 | where: { | |
510 | state: 'accepted', | |
511 | targetActorId: this.id | |
512 | } | |
513 | } | |
514 | ], | |
515 | transaction: t | |
516 | } | |
517 | ||
518 | return ActorModel.findAll(query) | |
519 | .then(accounts => accounts.map(a => a.sharedInboxUrl)) | |
520 | } | |
521 | ||
522 | getFollowingUrl () { | |
523 | return this.url + '/following' | |
524 | } | |
525 | ||
526 | getFollowersUrl () { | |
527 | return this.url + '/followers' | |
528 | } | |
529 | ||
530 | getPlaylistsUrl () { | |
531 | return this.url + '/playlists' | |
532 | } | |
533 | ||
534 | getPublicKeyUrl () { | |
535 | return this.url + '#main-key' | |
536 | } | |
537 | ||
538 | isOwned () { | |
539 | return this.serverId === null | |
540 | } | |
541 | ||
542 | getWebfingerUrl (this: MActorServer) { | |
543 | return 'acct:' + this.preferredUsername + '@' + this.getHost() | |
544 | } | |
545 | ||
546 | getIdentifier () { | |
547 | return this.Server ? `${this.preferredUsername}@${this.Server.host}` : this.preferredUsername | |
548 | } | |
549 | ||
550 | getHost (this: MActorHost) { | |
551 | return this.Server ? this.Server.host : WEBSERVER.HOST | |
552 | } | |
553 | ||
554 | getRedundancyAllowed () { | |
555 | return this.Server ? this.Server.redundancyAllowed : false | |
556 | } | |
557 | ||
558 | getAvatarUrl () { | |
559 | if (!this.avatarId) return undefined | |
560 | ||
561 | return WEBSERVER.URL + this.Avatar.getStaticPath() | |
562 | } | |
563 | ||
564 | isOutdated () { | |
565 | if (this.isOwned()) return false | |
566 | ||
567 | return isOutdated(this, ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL) | |
568 | } | |
569 | } |