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'
45 export const unusedActorAttributesForAPI = [
58 model: () => ServerModel,
62 model: () => AvatarModel,
71 model: () => AccountModel.unscoped(),
75 model: () => VideoChannelModel.unscoped(),
79 model: () => ServerModel,
83 model: () => AvatarModel,
97 fields: [ 'preferredUsername', 'serverId' ],
101 fields: [ 'inboxUrl', 'sharedInboxUrl' ]
104 fields: [ 'sharedInboxUrl' ]
107 fields: [ 'serverId' ]
110 fields: [ 'avatarId' ]
117 fields: [ 'followersUrl' ]
121 export class ActorModel extends Model<ActorModel> {
124 @Column(DataType.ENUM(values(ACTIVITY_PUB_ACTOR_TYPES)))
125 type: ActivityPubActorType
128 @Default(DataType.UUIDV4)
130 @Column(DataType.UUID)
134 @Is('ActorPreferredUsername', value => throwIfNotValid(value, isActorPreferredUsernameValid, 'actor preferred username'))
136 preferredUsername: string
139 @Is('ActorUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
140 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
144 @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPublicKeyValid, 'public key'))
145 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.PUBLIC_KEY.max))
149 @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPrivateKeyValid, 'private key'))
150 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.PRIVATE_KEY.max))
154 @Is('ActorFollowersCount', value => throwIfNotValid(value, isActorFollowersCountValid, 'followers count'))
156 followersCount: number
159 @Is('ActorFollowersCount', value => throwIfNotValid(value, isActorFollowingCountValid, 'following count'))
161 followingCount: number
164 @Is('ActorInboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'inbox url'))
165 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
169 @Is('ActorOutboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'outbox url'))
170 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
174 @Is('ActorSharedInboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'shared inbox url'))
175 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
176 sharedInboxUrl: string
179 @Is('ActorFollowersUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'followers url'))
180 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
184 @Is('ActorFollowingUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'following url'))
185 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
194 @ForeignKey(() => AvatarModel)
198 @BelongsTo(() => AvatarModel, {
202 onDelete: 'set null',
207 @HasMany(() => ActorFollowModel, {
214 ActorFollowing: ActorFollowModel[]
216 @HasMany(() => ActorFollowModel, {
218 name: 'targetActorId',
221 as: 'ActorFollowers',
224 ActorFollowers: ActorFollowModel[]
226 @ForeignKey(() => ServerModel)
230 @BelongsTo(() => ServerModel, {
238 @HasOne(() => AccountModel, {
245 Account: AccountModel
247 @HasOne(() => VideoChannelModel, {
254 VideoChannel: VideoChannelModel
256 static load (id: number) {
257 return ActorModel.unscoped().findById(id)
260 static listByFollowersUrls (followersUrls: string[], transaction?: Sequelize.Transaction) {
264 [ Sequelize.Op.in ]: followersUrls
270 return ActorModel.scope(ScopeNames.FULL).findAll(query)
273 static loadLocalByName (preferredUsername: string, transaction?: Sequelize.Transaction) {
282 return ActorModel.scope(ScopeNames.FULL).findOne(query)
285 static loadByNameAndHost (preferredUsername: string, host: string) {
301 return ActorModel.scope(ScopeNames.FULL).findOne(query)
304 static loadByUrl (url: string, transaction?: Sequelize.Transaction) {
312 return ActorModel.scope(ScopeNames.FULL).findOne(query)
315 static incrementFollows (id: number, column: 'followersCount' | 'followingCount', by: number) {
317 return (ActorModel as any).increment(column, {
325 static async getActorsFollowerSharedInboxUrls (actors: ActorModel[], t: Sequelize.Transaction) {
330 [Sequelize.Op.in]: actors.map(a => a.id)
336 model: ActorFollowModel.unscoped(),
338 as: 'ActorFollowers',
344 attributes: [ 'sharedInboxUrl' ],
345 model: ActorModel.unscoped(),
355 const hash: { [ id: number ]: string[] } = {}
356 const res = await ActorModel.findAll(query)
357 for (const actor of res) {
358 hash[actor.id] = actor.ActorFollowers.map(follow => follow.ActorFollower.sharedInboxUrl)
365 let avatar: Avatar = null
367 avatar = this.Avatar.toFormattedJSON()
374 name: this.preferredUsername,
375 host: this.getHost(),
376 followingCount: this.followingCount,
377 followersCount: this.followersCount,
379 createdAt: this.createdAt,
380 updatedAt: this.updatedAt
384 toActivityPubObject (name: string, type: 'Account' | 'Application' | 'VideoChannel') {
386 if (type === 'Account') {
387 activityPubType = 'Person' as 'Person'
388 } else if (type === 'Application') {
389 activityPubType = 'Application' as 'Application'
390 } else { // VideoChannel
391 activityPubType = 'Group' as 'Group'
396 const extension = extname(this.Avatar.filename)
399 mediaType: extension === '.png' ? 'image/png' : 'image/jpeg',
400 url: this.getAvatarUrl()
405 type: activityPubType,
407 following: this.getFollowingUrl(),
408 followers: this.getFollowersUrl(),
409 inbox: this.inboxUrl,
410 outbox: this.outboxUrl,
411 preferredUsername: this.preferredUsername,
415 sharedInbox: this.sharedInboxUrl
419 id: this.getPublicKeyUrl(),
421 publicKeyPem: this.publicKey
426 return activityPubContextify(json)
429 getFollowerSharedInboxUrls (t: Sequelize.Transaction) {
431 attributes: [ 'sharedInboxUrl' ],
435 model: ActorFollowModel.unscoped(),
437 as: 'ActorFollowing',
440 targetActorId: this.id
447 return ActorModel.findAll(query)
448 .then(accounts => accounts.map(a => a.sharedInboxUrl))
452 return this.url + '/following'
456 return this.url + '/followers'
460 return this.url + '#main-key'
464 return this.serverId === null
468 return 'acct:' + this.preferredUsername + '@' + this.getHost()
472 return this.Server ? `${this.preferredUsername}@${this.Server.host}` : this.preferredUsername
476 return this.Server ? this.Server.host : CONFIG.WEBSERVER.HOST
480 if (!this.avatarId) return undefined
482 return CONFIG.WEBSERVER.URL + this.Avatar.getWebserverPath()
486 if (this.isOwned()) return false
488 const now = Date.now()
489 const createdAtTime = this.createdAt.getTime()
490 const updatedAtTime = this.updatedAt.getTime()
492 return (now - createdAtTime) > ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL &&
493 (now - updatedAtTime) > ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL