1 import { values } from 'lodash'
2 import { extname } from 'path'
3 import * as Sequelize from 'sequelize'
21 } from 'sequelize-typescript'
22 import { ActivityPubActorType } from '../../../shared/models/activitypub'
23 import { Avatar } from '../../../shared/models/avatars/avatar.model'
24 import { activityPubContextify } from '../../helpers/activitypub'
26 isActorFollowersCountValid,
27 isActorFollowingCountValid,
28 isActorPreferredUsernameValid,
29 isActorPrivateKeyValid,
31 } from '../../helpers/custom-validators/activitypub/actor'
32 import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
33 import { ACTIVITY_PUB, ACTIVITY_PUB_ACTOR_TYPES, CONFIG, CONSTRAINTS_FIELDS } from '../../initializers'
34 import { AccountModel } from '../account/account'
35 import { AvatarModel } from '../avatar/avatar'
36 import { ServerModel } from '../server/server'
37 import { throwIfNotValid } from '../utils'
38 import { VideoChannelModel } from '../video/video-channel'
39 import { ActorFollowModel } from './actor-follow'
48 model: () => ServerModel,
52 model: () => AvatarModel,
61 model: () => AccountModel.unscoped(),
65 model: () => VideoChannelModel.unscoped(),
69 model: () => ServerModel,
73 model: () => AvatarModel,
87 fields: [ 'preferredUsername', 'serverId' ],
91 fields: [ 'inboxUrl', 'sharedInboxUrl' ]
94 fields: [ 'serverId' ]
97 fields: [ 'avatarId' ]
104 fields: [ 'followersUrl' ]
108 export class ActorModel extends Model<ActorModel> {
111 @Column(DataType.ENUM(values(ACTIVITY_PUB_ACTOR_TYPES)))
112 type: ActivityPubActorType
115 @Default(DataType.UUIDV4)
117 @Column(DataType.UUID)
121 @Is('ActorPreferredUsername', value => throwIfNotValid(value, isActorPreferredUsernameValid, 'actor preferred username'))
123 preferredUsername: string
126 @Is('ActorUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
127 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
131 @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPublicKeyValid, 'public key'))
132 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.PUBLIC_KEY.max))
136 @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPrivateKeyValid, 'private key'))
137 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.PRIVATE_KEY.max))
141 @Is('ActorFollowersCount', value => throwIfNotValid(value, isActorFollowersCountValid, 'followers count'))
143 followersCount: number
146 @Is('ActorFollowersCount', value => throwIfNotValid(value, isActorFollowingCountValid, 'following count'))
148 followingCount: number
151 @Is('ActorInboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'inbox url'))
152 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
156 @Is('ActorOutboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'outbox url'))
157 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
161 @Is('ActorSharedInboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'shared inbox url'))
162 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
163 sharedInboxUrl: string
166 @Is('ActorFollowersUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'followers url'))
167 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
171 @Is('ActorFollowingUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'following url'))
172 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
181 @ForeignKey(() => AvatarModel)
185 @BelongsTo(() => AvatarModel, {
189 onDelete: 'set null',
194 @HasMany(() => ActorFollowModel, {
201 ActorFollowing: ActorFollowModel[]
203 @HasMany(() => ActorFollowModel, {
205 name: 'targetActorId',
208 as: 'ActorFollowers',
211 ActorFollowers: ActorFollowModel[]
213 @ForeignKey(() => ServerModel)
217 @BelongsTo(() => ServerModel, {
225 @HasOne(() => AccountModel, {
232 Account: AccountModel
234 @HasOne(() => VideoChannelModel, {
241 VideoChannel: VideoChannelModel
243 static load (id: number) {
244 return ActorModel.unscoped().findById(id)
247 static listByFollowersUrls (followersUrls: string[], transaction?: Sequelize.Transaction) {
251 [ Sequelize.Op.in ]: followersUrls
257 return ActorModel.scope(ScopeNames.FULL).findAll(query)
260 static loadLocalByName (preferredUsername: string) {
268 return ActorModel.scope(ScopeNames.FULL).findOne(query)
271 static loadByNameAndHost (preferredUsername: string, host: string) {
287 return ActorModel.scope(ScopeNames.FULL).findOne(query)
290 static loadByUrl (url: string, transaction?: Sequelize.Transaction) {
298 return ActorModel.scope(ScopeNames.FULL).findOne(query)
301 static incrementFollows (id: number, column: 'followersCount' | 'followingCount', by: number) {
303 return (ActorModel as any).increment(column, {
311 static async getActorsFollowerSharedInboxUrls (actors: ActorModel[], t: Sequelize.Transaction) {
316 [Sequelize.Op.in]: actors.map(a => a.id)
322 model: ActorFollowModel.unscoped(),
324 as: 'ActorFollowers',
330 attributes: [ 'sharedInboxUrl' ],
331 model: ActorModel.unscoped(),
341 const hash: { [ id: number ]: string[] } = {}
342 const res = await ActorModel.findAll(query)
343 for (const actor of res) {
344 hash[actor.id] = actor.ActorFollowers.map(follow => follow.ActorFollower.sharedInboxUrl)
351 let avatar: Avatar = null
353 avatar = this.Avatar.toFormattedJSON()
360 name: this.preferredUsername,
361 host: this.getHost(),
362 followingCount: this.followingCount,
363 followersCount: this.followersCount,
365 createdAt: this.createdAt,
366 updatedAt: this.updatedAt
370 toActivityPubObject (name: string, type: 'Account' | 'Application' | 'VideoChannel') {
372 if (type === 'Account') {
373 activityPubType = 'Person' as 'Person'
374 } else if (type === 'Application') {
375 activityPubType = 'Application' as 'Application'
376 } else { // VideoChannel
377 activityPubType = 'Group' as 'Group'
382 const extension = extname(this.Avatar.filename)
385 mediaType: extension === '.png' ? 'image/png' : 'image/jpeg',
386 url: this.getAvatarUrl()
391 type: activityPubType,
393 following: this.getFollowingUrl(),
394 followers: this.getFollowersUrl(),
395 inbox: this.inboxUrl,
396 outbox: this.outboxUrl,
397 preferredUsername: this.preferredUsername,
401 sharedInbox: this.sharedInboxUrl
405 id: this.getPublicKeyUrl(),
407 publicKeyPem: this.publicKey
412 return activityPubContextify(json)
415 getFollowerSharedInboxUrls (t: Sequelize.Transaction) {
417 attributes: [ 'sharedInboxUrl' ],
421 model: ActorFollowModel.unscoped(),
423 as: 'ActorFollowing',
426 targetActorId: this.id
433 return ActorModel.findAll(query)
434 .then(accounts => accounts.map(a => a.sharedInboxUrl))
438 return this.url + '/following'
442 return this.url + '/followers'
446 return this.url + '#main-key'
450 return this.serverId === null
454 return 'acct:' + this.preferredUsername + '@' + this.getHost()
458 return this.Server ? this.Server.host : CONFIG.WEBSERVER.HOST
462 if (!this.avatarId) return undefined
464 return CONFIG.WEBSERVER.URL + this.Avatar.getWebserverPath()
468 if (this.isOwned()) return false
470 const now = Date.now()
471 const createdAtTime = this.createdAt.getTime()
472 const updatedAtTime = this.updatedAt.getTime()
474 return (now - createdAtTime) > ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL &&
475 (now - updatedAtTime) > ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL