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