1 import { values } from 'lodash'
2 import { extname } from 'path'
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'
23 isActorFollowersCountValid,
24 isActorFollowingCountValid,
25 isActorPreferredUsernameValid,
26 isActorPrivateKeyValid,
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'
40 MActorAccountChannelId,
46 MActorSummaryFormattable, MActorUrl,
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'
57 export const unusedActorAttributesForAPI = [
69 @DefaultScope(() => ({
85 model: AccountModel.unscoped(),
89 model: VideoChannelModel.unscoped(),
117 fields: [ 'preferredUsername', 'serverId' ],
126 fields: [ 'preferredUsername' ],
133 fields: [ 'inboxUrl', 'sharedInboxUrl' ]
136 fields: [ 'sharedInboxUrl' ]
139 fields: [ 'serverId' ]
142 fields: [ 'avatarId' ]
145 fields: [ 'followersUrl' ]
149 export class ActorModel extends Model<ActorModel> {
152 @Column(DataType.ENUM(...values(ACTIVITY_PUB_ACTOR_TYPES)))
153 type: ActivityPubActorType
156 @Is('ActorPreferredUsername', value => throwIfNotValid(value, isActorPreferredUsernameValid, 'actor preferred username'))
158 preferredUsername: string
161 @Is('ActorUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
162 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
166 @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPublicKeyValid, 'public key', true))
167 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.PUBLIC_KEY.max))
171 @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPrivateKeyValid, 'private key', true))
172 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.PRIVATE_KEY.max))
176 @Is('ActorFollowersCount', value => throwIfNotValid(value, isActorFollowersCountValid, 'followers count'))
178 followersCount: number
181 @Is('ActorFollowersCount', value => throwIfNotValid(value, isActorFollowingCountValid, 'following count'))
183 followingCount: number
186 @Is('ActorInboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'inbox url'))
187 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
191 @Is('ActorOutboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'outbox url', true))
192 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
196 @Is('ActorSharedInboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'shared inbox url', true))
197 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
198 sharedInboxUrl: string
201 @Is('ActorFollowersUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'followers url', true))
202 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
206 @Is('ActorFollowingUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'following url', true))
207 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
216 @ForeignKey(() => AvatarModel)
220 @BelongsTo(() => AvatarModel, {
224 onDelete: 'set null',
229 @HasMany(() => ActorFollowModel, {
234 as: 'ActorFollowings',
237 ActorFollowing: ActorFollowModel[]
239 @HasMany(() => ActorFollowModel, {
241 name: 'targetActorId',
244 as: 'ActorFollowers',
247 ActorFollowers: ActorFollowModel[]
249 @ForeignKey(() => ServerModel)
253 @BelongsTo(() => ServerModel, {
261 @HasOne(() => AccountModel, {
268 Account: AccountModel
270 @HasOne(() => VideoChannelModel, {
277 VideoChannel: VideoChannelModel
279 static load (id: number): Bluebird<MActor> {
280 return ActorModel.unscoped().findByPk(id)
283 static loadFull (id: number): Bluebird<MActorFull> {
284 return ActorModel.scope(ScopeNames.FULL).findByPk(id)
287 static loadFromAccountByVideoId (videoId: number, transaction: Transaction): Bluebird<MActor> {
291 attributes: [ 'id' ],
292 model: AccountModel.unscoped(),
296 attributes: [ 'id' ],
297 model: VideoChannelModel.unscoped(),
301 attributes: [ 'id' ],
302 model: VideoModel.unscoped(),
316 return ActorModel.unscoped().findOne(query)
319 static isActorUrlExist (url: string) {
327 return ActorModel.unscoped().findOne(query)
331 static listByFollowersUrls (followersUrls: string[], transaction?: Transaction): Bluebird<MActorFull[]> {
335 [Op.in]: followersUrls
341 return ActorModel.scope(ScopeNames.FULL).findAll(query)
344 static loadLocalByName (preferredUsername: string, transaction?: Transaction): Bluebird<MActorFull> {
354 return ActorModel.scope(ScopeNames.FULL)
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,
367 static loadLocalUrlByName (preferredUsername: string, transaction?: Transaction): Bluebird<MActorUrl> {
370 attributes: [ 'url' ],
378 return ActorModel.unscoped()
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,
391 static loadByNameAndHost (preferredUsername: string, host: string): Bluebird<MActorFull> {
407 return ActorModel.scope(ScopeNames.FULL).findOne(query)
410 static loadByUrl (url: string, transaction?: Transaction): Bluebird<MActorAccountChannelId> {
418 attributes: [ 'id' ],
419 model: AccountModel.unscoped(),
423 attributes: [ 'id' ],
424 model: VideoChannelModel.unscoped(),
430 return ActorModel.unscoped().findOne(query)
433 static loadByUrlAndPopulateAccountAndChannel (url: string, transaction?: Transaction): Bluebird<MActorFull> {
441 return ActorModel.scope(ScopeNames.FULL).findOne(query)
444 static rebuildFollowsCount (ofId: number, type: 'followers' | 'following', transaction?: Transaction) {
445 const sanitizedOfId = parseInt(ofId + '', 10)
446 const where = { id: sanitizedOfId }
448 let columnToUpdate: string
449 let columnOfCount: string
451 if (type === 'followers') {
452 columnToUpdate = 'followersCount'
453 columnOfCount = 'targetActorId'
455 columnToUpdate = 'followingCount'
456 columnOfCount = 'actorId'
459 return ActorModel.update({
460 [columnToUpdate]: literal(`(SELECT COUNT(*) FROM "actorFollow" WHERE "${columnOfCount}" = ${sanitizedOfId})`)
461 }, { where, transaction })
464 static loadAccountActorByVideoId (videoId: number): Bluebird<MActor> {
468 attributes: [ 'id' ],
469 model: AccountModel.unscoped(),
473 attributes: [ 'id', 'accountId' ],
474 model: VideoChannelModel.unscoped(),
478 attributes: [ 'id', 'channelId' ],
479 model: VideoModel.unscoped(),
491 return ActorModel.unscoped().findOne(query)
494 getSharedInbox (this: MActorWithInboxes) {
495 return this.sharedInboxUrl || this.inboxUrl
498 toFormattedSummaryJSON (this: MActorSummaryFormattable) {
499 let avatar: Avatar = null
501 avatar = this.Avatar.toFormattedJSON()
506 name: this.preferredUsername,
507 host: this.getHost(),
512 toFormattedJSON (this: MActorFormattable) {
513 const base = this.toFormattedSummaryJSON()
515 return Object.assign(base, {
517 hostRedundancyAllowed: this.getRedundancyAllowed(),
518 followingCount: this.followingCount,
519 followersCount: this.followersCount,
520 createdAt: this.createdAt,
521 updatedAt: this.updatedAt
525 toActivityPubObject (this: MActorAP, name: string) {
526 let icon: ActivityIconObject
529 const extension = extname(this.Avatar.filename)
533 mediaType: extension === '.png' ? 'image/png' : 'image/jpeg',
534 url: this.getAvatarUrl()
541 following: this.getFollowingUrl(),
542 followers: this.getFollowersUrl(),
543 playlists: this.getPlaylistsUrl(),
544 inbox: this.inboxUrl,
545 outbox: this.outboxUrl,
546 preferredUsername: this.preferredUsername,
550 sharedInbox: this.sharedInboxUrl
553 id: this.getPublicKeyUrl(),
555 publicKeyPem: this.publicKey
560 return activityPubContextify(json)
563 getFollowerSharedInboxUrls (t: Transaction) {
565 attributes: [ 'sharedInboxUrl' ],
569 model: ActorFollowModel.unscoped(),
571 as: 'ActorFollowing',
574 targetActorId: this.id
581 return ActorModel.findAll(query)
582 .then(accounts => accounts.map(a => a.sharedInboxUrl))
586 return this.url + '/following'
590 return this.url + '/followers'
594 return this.url + '/playlists'
598 return this.url + '#main-key'
602 return this.serverId === null
605 getWebfingerUrl (this: MActorServer) {
606 return 'acct:' + this.preferredUsername + '@' + this.getHost()
610 return this.Server ? `${this.preferredUsername}@${this.Server.host}` : this.preferredUsername
613 getHost (this: MActorHost) {
614 return this.Server ? this.Server.host : WEBSERVER.HOST
617 getRedundancyAllowed () {
618 return this.Server ? this.Server.redundancyAllowed : false
622 if (!this.avatarId) return undefined
624 return WEBSERVER.URL + this.Avatar.getStaticPath()
628 if (this.isOwned()) return false
630 return isOutdated(this, ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL)