]>
Commit | Line | Data |
---|---|---|
3396e653 | 1 | import { literal, Op, QueryTypes, Transaction } from 'sequelize' |
fadf619a | 2 | import { |
2422c46b C |
3 | AllowNull, |
4 | BelongsTo, | |
5 | Column, | |
6 | CreatedAt, | |
7 | DataType, | |
2422c46b C |
8 | DefaultScope, |
9 | ForeignKey, | |
10 | HasMany, | |
11 | HasOne, | |
12 | Is, | |
2422c46b C |
13 | Model, |
14 | Scopes, | |
15 | Table, | |
16 | UpdatedAt | |
fadf619a | 17 | } from 'sequelize-typescript' |
7e98a7df | 18 | import { activityPubContextify } from '@server/lib/activitypub/context' |
d0800f76 | 19 | import { getBiggestActorImage } from '@server/lib/actor-image' |
8c4bbd94 | 20 | import { ModelCache } from '@server/models/shared/model-cache' |
4638cd71 | 21 | import { forceNumber, getLowercaseExtension } from '@shared/core-utils' |
d0800f76 | 22 | import { ActivityIconObject, ActivityPubActorType, ActorImageType } from '@shared/models' |
6b5f72be | 23 | import { AttributesOnly } from '@shared/typescript-utils' |
fadf619a | 24 | import { |
2422c46b C |
25 | isActorFollowersCountValid, |
26 | isActorFollowingCountValid, | |
27 | isActorPreferredUsernameValid, | |
28 | isActorPrivateKeyValid, | |
da854ddd C |
29 | isActorPublicKeyValid |
30 | } from '../../helpers/custom-validators/activitypub/actor' | |
31 | import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc' | |
2cb03dc1 C |
32 | import { |
33 | ACTIVITY_PUB, | |
34 | ACTIVITY_PUB_ACTOR_TYPES, | |
35 | CONSTRAINTS_FIELDS, | |
36 | MIMETYPES, | |
37 | SERVER_ACTOR_NAME, | |
38 | WEBSERVER | |
39 | } from '../../initializers/constants' | |
1ca9f7c3 C |
40 | import { |
41 | MActor, | |
42 | MActorAccountChannelId, | |
2cb03dc1 C |
43 | MActorAPAccount, |
44 | MActorAPChannel, | |
3396e653 | 45 | MActorFollowersUrl, |
1ca9f7c3 | 46 | MActorFormattable, |
b5fecbf4 C |
47 | MActorFull, |
48 | MActorHost, | |
3396e653 | 49 | MActorId, |
1ca9f7c3 | 50 | MActorServer, |
b49f22d8 C |
51 | MActorSummaryFormattable, |
52 | MActorUrl, | |
47581df0 | 53 | MActorWithInboxes |
26d6bf65 | 54 | } from '../../types/models' |
b49f22d8 | 55 | import { AccountModel } from '../account/account' |
3396e653 | 56 | import { getServerActor } from '../application/application' |
b49f22d8 | 57 | import { ServerModel } from '../server/server' |
8c4bbd94 | 58 | import { buildSQLAttributes, isOutdated, throwIfNotValid } from '../shared' |
b49f22d8 C |
59 | import { VideoModel } from '../video/video' |
60 | import { VideoChannelModel } from '../video/video-channel' | |
61 | import { ActorFollowModel } from './actor-follow' | |
7d9ba5c0 | 62 | import { ActorImageModel } from './actor-image' |
fadf619a | 63 | |
50d6de9c C |
64 | enum ScopeNames { |
65 | FULL = 'FULL' | |
66 | } | |
67 | ||
eb66ee88 | 68 | export const unusedActorAttributesForAPI: (keyof AttributesOnly<ActorModel>)[] = [ |
f37dc0dd C |
69 | 'publicKey', |
70 | 'privateKey', | |
71 | 'inboxUrl', | |
72 | 'outboxUrl', | |
73 | 'sharedInboxUrl', | |
74 | 'followersUrl', | |
a66c2e32 | 75 | 'followingUrl' |
f37dc0dd C |
76 | ] |
77 | ||
3acc5084 | 78 | @DefaultScope(() => ({ |
ce33ee01 C |
79 | include: [ |
80 | { | |
3acc5084 | 81 | model: ServerModel, |
ce33ee01 | 82 | required: false |
c5911fd3 C |
83 | }, |
84 | { | |
f4796856 | 85 | model: ActorImageModel, |
d0800f76 | 86 | as: 'Avatars', |
c5911fd3 | 87 | required: false |
ce33ee01 C |
88 | } |
89 | ] | |
3acc5084 C |
90 | })) |
91 | @Scopes(() => ({ | |
50d6de9c C |
92 | [ScopeNames.FULL]: { |
93 | include: [ | |
94 | { | |
3acc5084 | 95 | model: AccountModel.unscoped(), |
50d6de9c C |
96 | required: false |
97 | }, | |
98 | { | |
3acc5084 | 99 | model: VideoChannelModel.unscoped(), |
c48e82b5 C |
100 | required: false, |
101 | include: [ | |
102 | { | |
3acc5084 | 103 | model: AccountModel, |
c48e82b5 C |
104 | required: true |
105 | } | |
106 | ] | |
ce33ee01 C |
107 | }, |
108 | { | |
3acc5084 | 109 | model: ServerModel, |
ce33ee01 | 110 | required: false |
c5911fd3 C |
111 | }, |
112 | { | |
f4796856 | 113 | model: ActorImageModel, |
d0800f76 | 114 | as: 'Avatars', |
c5911fd3 | 115 | required: false |
2cb03dc1 C |
116 | }, |
117 | { | |
118 | model: ActorImageModel, | |
d0800f76 | 119 | as: 'Banners', |
2cb03dc1 | 120 | required: false |
50d6de9c | 121 | } |
3acc5084 | 122 | ] |
50d6de9c | 123 | } |
3acc5084 | 124 | })) |
fadf619a | 125 | @Table({ |
50d6de9c C |
126 | tableName: 'actor', |
127 | indexes: [ | |
2ccaeeb3 | 128 | { |
8cd72bd3 C |
129 | fields: [ 'url' ], |
130 | unique: true | |
2ccaeeb3 | 131 | }, |
50d6de9c | 132 | { |
e12a0092 | 133 | fields: [ 'preferredUsername', 'serverId' ], |
77e08517 C |
134 | unique: true, |
135 | where: { | |
136 | serverId: { | |
137 | [Op.ne]: null | |
138 | } | |
139 | } | |
140 | }, | |
0f06c4de C |
141 | { |
142 | fields: [ 'preferredUsername' ], | |
143 | unique: true, | |
144 | where: { | |
145 | serverId: null | |
146 | } | |
147 | }, | |
6502c3d4 C |
148 | { |
149 | fields: [ 'inboxUrl', 'sharedInboxUrl' ] | |
57c36b27 | 150 | }, |
a3d1026b C |
151 | { |
152 | fields: [ 'sharedInboxUrl' ] | |
153 | }, | |
57c36b27 C |
154 | { |
155 | fields: [ 'serverId' ] | |
156 | }, | |
8cd72bd3 C |
157 | { |
158 | fields: [ 'followersUrl' ] | |
50d6de9c C |
159 | } |
160 | ] | |
fadf619a | 161 | }) |
16c016e8 | 162 | export class ActorModel extends Model<Partial<AttributesOnly<ActorModel>>> { |
fadf619a | 163 | |
50d6de9c | 164 | @AllowNull(false) |
690bb8f9 | 165 | @Column(DataType.ENUM(...Object.values(ACTIVITY_PUB_ACTOR_TYPES))) |
50d6de9c C |
166 | type: ActivityPubActorType |
167 | ||
fadf619a | 168 | @AllowNull(false) |
e12a0092 | 169 | @Is('ActorPreferredUsername', value => throwIfNotValid(value, isActorPreferredUsernameValid, 'actor preferred username')) |
fadf619a | 170 | @Column |
e12a0092 | 171 | preferredUsername: string |
fadf619a C |
172 | |
173 | @AllowNull(false) | |
174 | @Is('ActorUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url')) | |
01de67b9 | 175 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max)) |
fadf619a C |
176 | url: string |
177 | ||
178 | @AllowNull(true) | |
1735c825 | 179 | @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPublicKeyValid, 'public key', true)) |
01de67b9 | 180 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.PUBLIC_KEY.max)) |
fadf619a C |
181 | publicKey: string |
182 | ||
183 | @AllowNull(true) | |
1735c825 | 184 | @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPrivateKeyValid, 'private key', true)) |
01de67b9 | 185 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.PRIVATE_KEY.max)) |
fadf619a C |
186 | privateKey: string |
187 | ||
188 | @AllowNull(false) | |
189 | @Is('ActorFollowersCount', value => throwIfNotValid(value, isActorFollowersCountValid, 'followers count')) | |
190 | @Column | |
191 | followersCount: number | |
192 | ||
193 | @AllowNull(false) | |
194 | @Is('ActorFollowersCount', value => throwIfNotValid(value, isActorFollowingCountValid, 'following count')) | |
195 | @Column | |
196 | followingCount: number | |
197 | ||
198 | @AllowNull(false) | |
199 | @Is('ActorInboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'inbox url')) | |
01de67b9 | 200 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max)) |
fadf619a C |
201 | inboxUrl: string |
202 | ||
0b5c385b C |
203 | @AllowNull(true) |
204 | @Is('ActorOutboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'outbox url', true)) | |
01de67b9 | 205 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max)) |
fadf619a C |
206 | outboxUrl: string |
207 | ||
47581df0 C |
208 | @AllowNull(true) |
209 | @Is('ActorSharedInboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'shared inbox url', true)) | |
01de67b9 | 210 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max)) |
fadf619a C |
211 | sharedInboxUrl: string |
212 | ||
0b5c385b C |
213 | @AllowNull(true) |
214 | @Is('ActorFollowersUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'followers url', true)) | |
01de67b9 | 215 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max)) |
fadf619a C |
216 | followersUrl: string |
217 | ||
0b5c385b C |
218 | @AllowNull(true) |
219 | @Is('ActorFollowingUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'following url', true)) | |
01de67b9 | 220 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max)) |
fadf619a C |
221 | followingUrl: string |
222 | ||
a66c2e32 C |
223 | @AllowNull(true) |
224 | @Column | |
225 | remoteCreatedAt: Date | |
226 | ||
fadf619a C |
227 | @CreatedAt |
228 | createdAt: Date | |
229 | ||
230 | @UpdatedAt | |
231 | updatedAt: Date | |
232 | ||
d0800f76 | 233 | @HasMany(() => ActorImageModel, { |
234 | as: 'Avatars', | |
235 | onDelete: 'cascade', | |
236 | hooks: true, | |
f4796856 | 237 | foreignKey: { |
d0800f76 | 238 | allowNull: false |
f4796856 | 239 | }, |
d0800f76 | 240 | scope: { |
241 | type: ActorImageType.AVATAR | |
242 | } | |
f4796856 | 243 | }) |
d0800f76 | 244 | Avatars: ActorImageModel[] |
f4796856 | 245 | |
d0800f76 | 246 | @HasMany(() => ActorImageModel, { |
247 | as: 'Banners', | |
248 | onDelete: 'cascade', | |
249 | hooks: true, | |
fadf619a | 250 | foreignKey: { |
d0800f76 | 251 | allowNull: false |
fadf619a | 252 | }, |
d0800f76 | 253 | scope: { |
254 | type: ActorImageType.BANNER | |
255 | } | |
fadf619a | 256 | }) |
d0800f76 | 257 | Banners: ActorImageModel[] |
fadf619a | 258 | |
50d6de9c | 259 | @HasMany(() => ActorFollowModel, { |
fadf619a | 260 | foreignKey: { |
50d6de9c | 261 | name: 'actorId', |
fadf619a C |
262 | allowNull: false |
263 | }, | |
cef534ed | 264 | as: 'ActorFollowings', |
fadf619a C |
265 | onDelete: 'cascade' |
266 | }) | |
54e74059 | 267 | ActorFollowing: ActorFollowModel[] |
fadf619a | 268 | |
50d6de9c | 269 | @HasMany(() => ActorFollowModel, { |
fadf619a | 270 | foreignKey: { |
50d6de9c | 271 | name: 'targetActorId', |
fadf619a C |
272 | allowNull: false |
273 | }, | |
54e74059 | 274 | as: 'ActorFollowers', |
fadf619a C |
275 | onDelete: 'cascade' |
276 | }) | |
54e74059 | 277 | ActorFollowers: ActorFollowModel[] |
fadf619a C |
278 | |
279 | @ForeignKey(() => ServerModel) | |
280 | @Column | |
281 | serverId: number | |
282 | ||
283 | @BelongsTo(() => ServerModel, { | |
284 | foreignKey: { | |
285 | allowNull: true | |
286 | }, | |
287 | onDelete: 'cascade' | |
288 | }) | |
289 | Server: ServerModel | |
290 | ||
50d6de9c C |
291 | @HasOne(() => AccountModel, { |
292 | foreignKey: { | |
c5a893d5 C |
293 | allowNull: true |
294 | }, | |
295 | onDelete: 'cascade', | |
296 | hooks: true | |
50d6de9c C |
297 | }) |
298 | Account: AccountModel | |
299 | ||
300 | @HasOne(() => VideoChannelModel, { | |
301 | foreignKey: { | |
c5a893d5 C |
302 | allowNull: true |
303 | }, | |
304 | onDelete: 'cascade', | |
305 | hooks: true | |
50d6de9c C |
306 | }) |
307 | VideoChannel: VideoChannelModel | |
308 | ||
eb66ee88 C |
309 | // --------------------------------------------------------------------------- |
310 | ||
311 | static getSQLAttributes (tableName: string, aliasPrefix = '') { | |
312 | return buildSQLAttributes({ | |
313 | model: this, | |
314 | tableName, | |
315 | aliasPrefix | |
316 | }) | |
317 | } | |
318 | ||
319 | static getSQLAPIAttributes (tableName: string, aliasPrefix = '') { | |
320 | return buildSQLAttributes({ | |
321 | model: this, | |
322 | tableName, | |
323 | aliasPrefix, | |
324 | excludeAttributes: unusedActorAttributesForAPI | |
325 | }) | |
326 | } | |
327 | ||
328 | // --------------------------------------------------------------------------- | |
329 | ||
3396e653 C |
330 | static async load (id: number): Promise<MActor> { |
331 | const actorServer = await getServerActor() | |
332 | if (id === actorServer.id) return actorServer | |
333 | ||
9b39106d | 334 | return ActorModel.unscoped().findByPk(id) |
50d6de9c C |
335 | } |
336 | ||
b49f22d8 | 337 | static loadFull (id: number): Promise<MActorFull> { |
453e83ea C |
338 | return ActorModel.scope(ScopeNames.FULL).findByPk(id) |
339 | } | |
340 | ||
3396e653 C |
341 | static loadAccountActorFollowerUrlByVideoId (videoId: number, transaction: Transaction) { |
342 | const query = `SELECT "actor"."id" AS "id", "actor"."followersUrl" AS "followersUrl" ` + | |
343 | `FROM "actor" ` + | |
344 | `INNER JOIN "account" ON "actor"."id" = "account"."actorId" ` + | |
345 | `INNER JOIN "videoChannel" ON "videoChannel"."accountId" = "account"."id" ` + | |
346 | `INNER JOIN "video" ON "video"."channelId" = "videoChannel"."id" AND "video"."id" = :videoId` | |
347 | ||
348 | const options = { | |
349 | type: QueryTypes.SELECT as QueryTypes.SELECT, | |
350 | replacements: { videoId }, | |
351 | plain: true as true, | |
e5565833 C |
352 | transaction |
353 | } | |
354 | ||
3396e653 | 355 | return ActorModel.sequelize.query<MActorId & MActorFollowersUrl>(query, options) |
d4defe07 C |
356 | } |
357 | ||
b49f22d8 | 358 | static listByFollowersUrls (followersUrls: string[], transaction?: Transaction): Promise<MActorFull[]> { |
fadf619a C |
359 | const query = { |
360 | where: { | |
361 | followersUrl: { | |
a1587156 | 362 | [Op.in]: followersUrls |
fadf619a C |
363 | } |
364 | }, | |
365 | transaction | |
366 | } | |
367 | ||
50d6de9c C |
368 | return ActorModel.scope(ScopeNames.FULL).findAll(query) |
369 | } | |
370 | ||
b49f22d8 | 371 | static loadLocalByName (preferredUsername: string, transaction?: Transaction): Promise<MActorFull> { |
0ffd6d32 C |
372 | const fun = () => { |
373 | const query = { | |
374 | where: { | |
375 | preferredUsername, | |
376 | serverId: null | |
377 | }, | |
378 | transaction | |
379 | } | |
e4a686b4 | 380 | |
d0800f76 | 381 | return ActorModel.scope(ScopeNames.FULL).findOne(query) |
50d6de9c C |
382 | } |
383 | ||
0ffd6d32 C |
384 | return ModelCache.Instance.doCache({ |
385 | cacheType: 'local-actor-name', | |
386 | key: preferredUsername, | |
387 | // The server actor never change, so we can easily cache it | |
388 | whitelist: () => preferredUsername === SERVER_ACTOR_NAME, | |
389 | fun | |
390 | }) | |
0374b6b5 C |
391 | } |
392 | ||
b49f22d8 | 393 | static loadLocalUrlByName (preferredUsername: string, transaction?: Transaction): Promise<MActorUrl> { |
0ffd6d32 C |
394 | const fun = () => { |
395 | const query = { | |
396 | attributes: [ 'url' ], | |
397 | where: { | |
398 | preferredUsername, | |
399 | serverId: null | |
400 | }, | |
401 | transaction | |
402 | } | |
0374b6b5 | 403 | |
d0800f76 | 404 | return ActorModel.unscoped().findOne(query) |
0374b6b5 C |
405 | } |
406 | ||
0ffd6d32 C |
407 | return ModelCache.Instance.doCache({ |
408 | cacheType: 'local-actor-name', | |
409 | key: preferredUsername, | |
410 | // The server actor never change, so we can easily cache it | |
411 | whitelist: () => preferredUsername === SERVER_ACTOR_NAME, | |
412 | fun | |
413 | }) | |
50d6de9c C |
414 | } |
415 | ||
b49f22d8 | 416 | static loadByNameAndHost (preferredUsername: string, host: string): Promise<MActorFull> { |
50d6de9c C |
417 | const query = { |
418 | where: { | |
e12a0092 | 419 | preferredUsername |
50d6de9c C |
420 | }, |
421 | include: [ | |
422 | { | |
423 | model: ServerModel, | |
424 | required: true, | |
425 | where: { | |
426 | host | |
427 | } | |
428 | } | |
429 | ] | |
430 | } | |
431 | ||
432 | return ActorModel.scope(ScopeNames.FULL).findOne(query) | |
433 | } | |
434 | ||
b49f22d8 | 435 | static loadByUrl (url: string, transaction?: Transaction): Promise<MActorAccountChannelId> { |
e587e0ec C |
436 | const query = { |
437 | where: { | |
438 | url | |
439 | }, | |
440 | transaction, | |
441 | include: [ | |
442 | { | |
443 | attributes: [ 'id' ], | |
444 | model: AccountModel.unscoped(), | |
445 | required: false | |
446 | }, | |
447 | { | |
448 | attributes: [ 'id' ], | |
449 | model: VideoChannelModel.unscoped(), | |
450 | required: false | |
451 | } | |
452 | ] | |
453 | } | |
454 | ||
455 | return ActorModel.unscoped().findOne(query) | |
456 | } | |
457 | ||
b49f22d8 | 458 | static loadByUrlAndPopulateAccountAndChannel (url: string, transaction?: Transaction): Promise<MActorFull> { |
50d6de9c C |
459 | const query = { |
460 | where: { | |
461 | url | |
462 | }, | |
463 | transaction | |
464 | } | |
465 | ||
466 | return ActorModel.scope(ScopeNames.FULL).findOne(query) | |
fadf619a C |
467 | } |
468 | ||
e6122097 | 469 | static rebuildFollowsCount (ofId: number, type: 'followers' | 'following', transaction?: Transaction) { |
4638cd71 | 470 | const sanitizedOfId = forceNumber(ofId) |
e6122097 C |
471 | const where = { id: sanitizedOfId } |
472 | ||
473 | let columnToUpdate: string | |
474 | let columnOfCount: string | |
475 | ||
476 | if (type === 'followers') { | |
477 | columnToUpdate = 'followersCount' | |
478 | columnOfCount = 'targetActorId' | |
479 | } else { | |
480 | columnToUpdate = 'followingCount' | |
481 | columnOfCount = 'actorId' | |
482 | } | |
483 | ||
484 | return ActorModel.update({ | |
927fa4b1 | 485 | [columnToUpdate]: literal(`(SELECT COUNT(*) FROM "actorFollow" WHERE "${columnOfCount}" = ${sanitizedOfId} AND "state" = 'accepted')`) |
e6122097 | 486 | }, { where, transaction }) |
32b2b43c C |
487 | } |
488 | ||
06c27593 | 489 | static loadAccountActorByVideoId (videoId: number, transaction: Transaction): Promise<MActor> { |
2c8776fc C |
490 | const query = { |
491 | include: [ | |
492 | { | |
493 | attributes: [ 'id' ], | |
494 | model: AccountModel.unscoped(), | |
495 | required: true, | |
496 | include: [ | |
497 | { | |
498 | attributes: [ 'id', 'accountId' ], | |
499 | model: VideoChannelModel.unscoped(), | |
500 | required: true, | |
501 | include: [ | |
502 | { | |
503 | attributes: [ 'id', 'channelId' ], | |
504 | model: VideoModel.unscoped(), | |
505 | where: { | |
506 | id: videoId | |
507 | } | |
508 | } | |
509 | ] | |
510 | } | |
511 | ] | |
512 | } | |
06c27593 C |
513 | ], |
514 | transaction | |
2c8776fc C |
515 | } |
516 | ||
517 | return ActorModel.unscoped().findOne(query) | |
518 | } | |
519 | ||
47581df0 C |
520 | getSharedInbox (this: MActorWithInboxes) { |
521 | return this.sharedInboxUrl || this.inboxUrl | |
522 | } | |
523 | ||
1ca9f7c3 | 524 | toFormattedSummaryJSON (this: MActorSummaryFormattable) { |
fadf619a | 525 | return { |
4cb6d457 | 526 | url: this.url, |
60650c77 | 527 | name: this.preferredUsername, |
e12a0092 | 528 | host: this.getHost(), |
d0800f76 | 529 | avatars: (this.Avatars || []).map(a => a.toFormattedJSON()), |
530 | ||
531 | // TODO: remove, deprecated in 4.2 | |
532 | avatar: this.hasImage(ActorImageType.AVATAR) | |
533 | ? this.Avatars[0].toFormattedJSON() | |
534 | : undefined | |
1ca9f7c3 C |
535 | } |
536 | } | |
537 | ||
538 | toFormattedJSON (this: MActorFormattable) { | |
d0800f76 | 539 | return { |
540 | ...this.toFormattedSummaryJSON(), | |
2cb03dc1 | 541 | |
1ca9f7c3 | 542 | id: this.id, |
c48e82b5 | 543 | hostRedundancyAllowed: this.getRedundancyAllowed(), |
fadf619a C |
544 | followingCount: this.followingCount, |
545 | followersCount: this.followersCount, | |
d0800f76 | 546 | createdAt: this.getCreatedAt(), |
547 | ||
548 | banners: (this.Banners || []).map(b => b.toFormattedJSON()), | |
549 | ||
550 | // TODO: remove, deprecated in 4.2 | |
551 | banner: this.hasImage(ActorImageType.BANNER) | |
552 | ? this.Banners[0].toFormattedJSON() | |
553 | : undefined | |
554 | } | |
fadf619a C |
555 | } |
556 | ||
2cb03dc1 | 557 | toActivityPubObject (this: MActorAPChannel | MActorAPAccount, name: string) { |
a1587156 | 558 | let icon: ActivityIconObject |
d0800f76 | 559 | let icons: ActivityIconObject[] |
2cb03dc1 | 560 | let image: ActivityIconObject |
a1587156 | 561 | |
d0800f76 | 562 | if (this.hasImage(ActorImageType.AVATAR)) { |
563 | icon = getBiggestActorImage(this.Avatars).toActivityPubObject() | |
564 | icons = this.Avatars.map(a => a.toActivityPubObject()) | |
c5911fd3 C |
565 | } |
566 | ||
d0800f76 | 567 | if (this.hasImage(ActorImageType.BANNER)) { |
568 | const banner = getBiggestActorImage((this as MActorAPChannel).Banners) | |
ea54cd04 | 569 | const extension = getLowercaseExtension(banner.filename) |
2cb03dc1 C |
570 | |
571 | image = { | |
572 | type: 'Image', | |
573 | mediaType: MIMETYPES.IMAGE.EXT_MIMETYPE[extension], | |
84531547 C |
574 | height: banner.height, |
575 | width: banner.width, | |
d0800f76 | 576 | url: ActorImageModel.getImageUrl(banner) |
2cb03dc1 C |
577 | } |
578 | } | |
579 | ||
fadf619a | 580 | const json = { |
8424c402 | 581 | type: this.type, |
fadf619a C |
582 | id: this.url, |
583 | following: this.getFollowingUrl(), | |
584 | followers: this.getFollowersUrl(), | |
418d092a | 585 | playlists: this.getPlaylistsUrl(), |
fadf619a C |
586 | inbox: this.inboxUrl, |
587 | outbox: this.outboxUrl, | |
e12a0092 | 588 | preferredUsername: this.preferredUsername, |
fadf619a | 589 | url: this.url, |
e12a0092 | 590 | name, |
fadf619a C |
591 | endpoints: { |
592 | sharedInbox: this.sharedInboxUrl | |
593 | }, | |
fadf619a C |
594 | publicKey: { |
595 | id: this.getPublicKeyUrl(), | |
596 | owner: this.url, | |
597 | publicKeyPem: this.publicKey | |
c5911fd3 | 598 | }, |
a66c2e32 | 599 | published: this.getCreatedAt().toISOString(), |
d0800f76 | 600 | |
2cb03dc1 | 601 | icon, |
d0800f76 | 602 | icons, |
603 | ||
2cb03dc1 | 604 | image |
fadf619a C |
605 | } |
606 | ||
a219c910 | 607 | return activityPubContextify(json, 'Actor') |
fadf619a C |
608 | } |
609 | ||
941c5eac | 610 | getFollowerSharedInboxUrls (t: Transaction) { |
fadf619a C |
611 | const query = { |
612 | attributes: [ 'sharedInboxUrl' ], | |
613 | include: [ | |
614 | { | |
54e74059 C |
615 | attribute: [], |
616 | model: ActorFollowModel.unscoped(), | |
fadf619a | 617 | required: true, |
d6e99e53 | 618 | as: 'ActorFollowing', |
fadf619a | 619 | where: { |
54e74059 | 620 | state: 'accepted', |
50d6de9c | 621 | targetActorId: this.id |
fadf619a C |
622 | } |
623 | } | |
624 | ], | |
625 | transaction: t | |
626 | } | |
627 | ||
628 | return ActorModel.findAll(query) | |
629 | .then(accounts => accounts.map(a => a.sharedInboxUrl)) | |
630 | } | |
631 | ||
632 | getFollowingUrl () { | |
633 | return this.url + '/following' | |
634 | } | |
635 | ||
636 | getFollowersUrl () { | |
637 | return this.url + '/followers' | |
638 | } | |
639 | ||
418d092a C |
640 | getPlaylistsUrl () { |
641 | return this.url + '/playlists' | |
642 | } | |
643 | ||
fadf619a C |
644 | getPublicKeyUrl () { |
645 | return this.url + '#main-key' | |
646 | } | |
647 | ||
648 | isOwned () { | |
649 | return this.serverId === null | |
650 | } | |
e12a0092 | 651 | |
1ca9f7c3 | 652 | getWebfingerUrl (this: MActorServer) { |
e12a0092 C |
653 | return 'acct:' + this.preferredUsername + '@' + this.getHost() |
654 | } | |
655 | ||
80e36cd9 AB |
656 | getIdentifier () { |
657 | return this.Server ? `${this.preferredUsername}@${this.Server.host}` : this.preferredUsername | |
658 | } | |
659 | ||
1ca9f7c3 | 660 | getHost (this: MActorHost) { |
6dd9de95 | 661 | return this.Server ? this.Server.host : WEBSERVER.HOST |
e12a0092 | 662 | } |
c5911fd3 | 663 | |
b5fecbf4 | 664 | getRedundancyAllowed () { |
c48e82b5 C |
665 | return this.Server ? this.Server.redundancyAllowed : false |
666 | } | |
667 | ||
d0800f76 | 668 | hasImage (type: ActorImageType) { |
669 | const images = type === ActorImageType.AVATAR | |
670 | ? this.Avatars | |
671 | : this.Banners | |
2cb03dc1 | 672 | |
d0800f76 | 673 | return Array.isArray(images) && images.length !== 0 |
2cb03dc1 C |
674 | } |
675 | ||
a5625b41 C |
676 | isOutdated () { |
677 | if (this.isOwned()) return false | |
678 | ||
9f79ade6 | 679 | return isOutdated(this, ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL) |
a5625b41 | 680 | } |
a66c2e32 C |
681 | |
682 | getCreatedAt (this: MActorAPChannel | MActorAPAccount | MActorFormattable) { | |
683 | return this.remoteCreatedAt || this.createdAt | |
684 | } | |
fadf619a | 685 | } |