diff options
Diffstat (limited to 'server/models/activitypub/actor.ts')
-rw-r--r-- | server/models/activitypub/actor.ts | 71 |
1 files changed, 43 insertions, 28 deletions
diff --git a/server/models/activitypub/actor.ts b/server/models/activitypub/actor.ts index 9cc53f78a..05de1905d 100644 --- a/server/models/activitypub/actor.ts +++ b/server/models/activitypub/actor.ts | |||
@@ -36,6 +36,17 @@ import { isOutdated, throwIfNotValid } from '../utils' | |||
36 | import { VideoChannelModel } from '../video/video-channel' | 36 | import { VideoChannelModel } from '../video/video-channel' |
37 | import { ActorFollowModel } from './actor-follow' | 37 | import { ActorFollowModel } from './actor-follow' |
38 | import { VideoModel } from '../video/video' | 38 | import { VideoModel } from '../video/video' |
39 | import { | ||
40 | MActor, | ||
41 | MActorAccountChannelId, | ||
42 | MActorAP, | ||
43 | MActorFormattable, | ||
44 | MActorFull, | ||
45 | MActorHost, | ||
46 | MActorServer, | ||
47 | MActorSummaryFormattable | ||
48 | } from '../../typings/models' | ||
49 | import * as Bluebird from 'bluebird' | ||
39 | 50 | ||
40 | enum ScopeNames { | 51 | enum ScopeNames { |
41 | FULL = 'FULL' | 52 | FULL = 'FULL' |
@@ -163,8 +174,8 @@ export class ActorModel extends Model<ActorModel> { | |||
163 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max)) | 174 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max)) |
164 | inboxUrl: string | 175 | inboxUrl: string |
165 | 176 | ||
166 | @AllowNull(false) | 177 | @AllowNull(true) |
167 | @Is('ActorOutboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'outbox url')) | 178 | @Is('ActorOutboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'outbox url', true)) |
168 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max)) | 179 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max)) |
169 | outboxUrl: string | 180 | outboxUrl: string |
170 | 181 | ||
@@ -173,13 +184,13 @@ export class ActorModel extends Model<ActorModel> { | |||
173 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max)) | 184 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max)) |
174 | sharedInboxUrl: string | 185 | sharedInboxUrl: string |
175 | 186 | ||
176 | @AllowNull(false) | 187 | @AllowNull(true) |
177 | @Is('ActorFollowersUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'followers url')) | 188 | @Is('ActorFollowersUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'followers url', true)) |
178 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max)) | 189 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max)) |
179 | followersUrl: string | 190 | followersUrl: string |
180 | 191 | ||
181 | @AllowNull(false) | 192 | @AllowNull(true) |
182 | @Is('ActorFollowingUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'following url')) | 193 | @Is('ActorFollowingUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'following url', true)) |
183 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max)) | 194 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max)) |
184 | followingUrl: string | 195 | followingUrl: string |
185 | 196 | ||
@@ -252,11 +263,15 @@ export class ActorModel extends Model<ActorModel> { | |||
252 | }) | 263 | }) |
253 | VideoChannel: VideoChannelModel | 264 | VideoChannel: VideoChannelModel |
254 | 265 | ||
255 | static load (id: number) { | 266 | static load (id: number): Bluebird<MActor> { |
256 | return ActorModel.unscoped().findByPk(id) | 267 | return ActorModel.unscoped().findByPk(id) |
257 | } | 268 | } |
258 | 269 | ||
259 | static loadAccountActorByVideoId (videoId: number, transaction: Sequelize.Transaction) { | 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> { | ||
260 | const query = { | 275 | const query = { |
261 | include: [ | 276 | include: [ |
262 | { | 277 | { |
@@ -300,7 +315,7 @@ export class ActorModel extends Model<ActorModel> { | |||
300 | .then(a => !!a) | 315 | .then(a => !!a) |
301 | } | 316 | } |
302 | 317 | ||
303 | static listByFollowersUrls (followersUrls: string[], transaction?: Sequelize.Transaction) { | 318 | static listByFollowersUrls (followersUrls: string[], transaction?: Sequelize.Transaction): Bluebird<MActorFull[]> { |
304 | const query = { | 319 | const query = { |
305 | where: { | 320 | where: { |
306 | followersUrl: { | 321 | followersUrl: { |
@@ -313,7 +328,7 @@ export class ActorModel extends Model<ActorModel> { | |||
313 | return ActorModel.scope(ScopeNames.FULL).findAll(query) | 328 | return ActorModel.scope(ScopeNames.FULL).findAll(query) |
314 | } | 329 | } |
315 | 330 | ||
316 | static loadLocalByName (preferredUsername: string, transaction?: Sequelize.Transaction) { | 331 | static loadLocalByName (preferredUsername: string, transaction?: Sequelize.Transaction): Bluebird<MActorFull> { |
317 | const query = { | 332 | const query = { |
318 | where: { | 333 | where: { |
319 | preferredUsername, | 334 | preferredUsername, |
@@ -325,7 +340,7 @@ export class ActorModel extends Model<ActorModel> { | |||
325 | return ActorModel.scope(ScopeNames.FULL).findOne(query) | 340 | return ActorModel.scope(ScopeNames.FULL).findOne(query) |
326 | } | 341 | } |
327 | 342 | ||
328 | static loadByNameAndHost (preferredUsername: string, host: string) { | 343 | static loadByNameAndHost (preferredUsername: string, host: string): Bluebird<MActorFull> { |
329 | const query = { | 344 | const query = { |
330 | where: { | 345 | where: { |
331 | preferredUsername | 346 | preferredUsername |
@@ -344,7 +359,7 @@ export class ActorModel extends Model<ActorModel> { | |||
344 | return ActorModel.scope(ScopeNames.FULL).findOne(query) | 359 | return ActorModel.scope(ScopeNames.FULL).findOne(query) |
345 | } | 360 | } |
346 | 361 | ||
347 | static loadByUrl (url: string, transaction?: Sequelize.Transaction) { | 362 | static loadByUrl (url: string, transaction?: Sequelize.Transaction): Bluebird<MActorAccountChannelId> { |
348 | const query = { | 363 | const query = { |
349 | where: { | 364 | where: { |
350 | url | 365 | url |
@@ -367,7 +382,7 @@ export class ActorModel extends Model<ActorModel> { | |||
367 | return ActorModel.unscoped().findOne(query) | 382 | return ActorModel.unscoped().findOne(query) |
368 | } | 383 | } |
369 | 384 | ||
370 | static loadByUrlAndPopulateAccountAndChannel (url: string, transaction?: Sequelize.Transaction) { | 385 | static loadByUrlAndPopulateAccountAndChannel (url: string, transaction?: Sequelize.Transaction): Bluebird<MActorFull> { |
371 | const query = { | 386 | const query = { |
372 | where: { | 387 | where: { |
373 | url | 388 | url |
@@ -387,35 +402,35 @@ export class ActorModel extends Model<ActorModel> { | |||
387 | }) | 402 | }) |
388 | } | 403 | } |
389 | 404 | ||
390 | toFormattedJSON () { | 405 | toFormattedSummaryJSON (this: MActorSummaryFormattable) { |
391 | let avatar: Avatar = null | 406 | let avatar: Avatar = null |
392 | if (this.Avatar) { | 407 | if (this.Avatar) { |
393 | avatar = this.Avatar.toFormattedJSON() | 408 | avatar = this.Avatar.toFormattedJSON() |
394 | } | 409 | } |
395 | 410 | ||
396 | return { | 411 | return { |
397 | id: this.id, | ||
398 | url: this.url, | 412 | url: this.url, |
399 | name: this.preferredUsername, | 413 | name: this.preferredUsername, |
400 | host: this.getHost(), | 414 | host: this.getHost(), |
415 | avatar | ||
416 | } | ||
417 | } | ||
418 | |||
419 | toFormattedJSON (this: MActorFormattable) { | ||
420 | const base = this.toFormattedSummaryJSON() | ||
421 | |||
422 | return Object.assign(base, { | ||
423 | id: this.id, | ||
401 | hostRedundancyAllowed: this.getRedundancyAllowed(), | 424 | hostRedundancyAllowed: this.getRedundancyAllowed(), |
402 | followingCount: this.followingCount, | 425 | followingCount: this.followingCount, |
403 | followersCount: this.followersCount, | 426 | followersCount: this.followersCount, |
404 | avatar, | ||
405 | createdAt: this.createdAt, | 427 | createdAt: this.createdAt, |
406 | updatedAt: this.updatedAt | 428 | updatedAt: this.updatedAt |
407 | } | 429 | }) |
408 | } | 430 | } |
409 | 431 | ||
410 | toActivityPubObject (name: string, type: 'Account' | 'Application' | 'VideoChannel') { | 432 | toActivityPubObject (this: MActorAP, name: string) { |
411 | let activityPubType | 433 | let activityPubType |
412 | if (type === 'Account') { | ||
413 | activityPubType = 'Person' as 'Person' | ||
414 | } else if (type === 'Application') { | ||
415 | activityPubType = 'Application' as 'Application' | ||
416 | } else { // VideoChannel | ||
417 | activityPubType = 'Group' as 'Group' | ||
418 | } | ||
419 | 434 | ||
420 | let icon = undefined | 435 | let icon = undefined |
421 | if (this.avatarId) { | 436 | if (this.avatarId) { |
@@ -428,7 +443,7 @@ export class ActorModel extends Model<ActorModel> { | |||
428 | } | 443 | } |
429 | 444 | ||
430 | const json = { | 445 | const json = { |
431 | type: activityPubType, | 446 | type: this.type, |
432 | id: this.url, | 447 | id: this.url, |
433 | following: this.getFollowingUrl(), | 448 | following: this.getFollowingUrl(), |
434 | followers: this.getFollowersUrl(), | 449 | followers: this.getFollowersUrl(), |
@@ -494,7 +509,7 @@ export class ActorModel extends Model<ActorModel> { | |||
494 | return this.serverId === null | 509 | return this.serverId === null |
495 | } | 510 | } |
496 | 511 | ||
497 | getWebfingerUrl () { | 512 | getWebfingerUrl (this: MActorServer) { |
498 | return 'acct:' + this.preferredUsername + '@' + this.getHost() | 513 | return 'acct:' + this.preferredUsername + '@' + this.getHost() |
499 | } | 514 | } |
500 | 515 | ||
@@ -502,7 +517,7 @@ export class ActorModel extends Model<ActorModel> { | |||
502 | return this.Server ? `${this.preferredUsername}@${this.Server.host}` : this.preferredUsername | 517 | return this.Server ? `${this.preferredUsername}@${this.Server.host}` : this.preferredUsername |
503 | } | 518 | } |
504 | 519 | ||
505 | getHost () { | 520 | getHost (this: MActorHost) { |
506 | return this.Server ? this.Server.host : WEBSERVER.HOST | 521 | return this.Server ? this.Server.host : WEBSERVER.HOST |
507 | } | 522 | } |
508 | 523 | ||