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,
86 fields: [ 'preferredUsername', 'serverId' ],
90 fields: [ 'inboxUrl', 'sharedInboxUrl' ]
94 export class ActorModel extends Model<ActorModel> {
97 @Column(DataType.ENUM(values(ACTIVITY_PUB_ACTOR_TYPES)))
98 type: ActivityPubActorType
101 @Default(DataType.UUIDV4)
103 @Column(DataType.UUID)
107 @Is('ActorPreferredUsername', value => throwIfNotValid(value, isActorPreferredUsernameValid, 'actor preferred username'))
109 preferredUsername: string
112 @Is('ActorUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
113 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
117 @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPublicKeyValid, 'public key'))
118 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.PUBLIC_KEY.max))
122 @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPrivateKeyValid, 'private key'))
123 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.PRIVATE_KEY.max))
127 @Is('ActorFollowersCount', value => throwIfNotValid(value, isActorFollowersCountValid, 'followers count'))
129 followersCount: number
132 @Is('ActorFollowersCount', value => throwIfNotValid(value, isActorFollowingCountValid, 'following count'))
134 followingCount: number
137 @Is('ActorInboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'inbox url'))
138 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
142 @Is('ActorOutboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'outbox url'))
143 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
147 @Is('ActorSharedInboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'shared inbox url'))
148 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
149 sharedInboxUrl: string
152 @Is('ActorFollowersUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'followers url'))
153 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
157 @Is('ActorFollowingUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'following url'))
158 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
167 @ForeignKey(() => AvatarModel)
171 @BelongsTo(() => AvatarModel, {
175 onDelete: 'set null',
180 @HasMany(() => ActorFollowModel, {
187 ActorFollowing: ActorFollowModel[]
189 @HasMany(() => ActorFollowModel, {
191 name: 'targetActorId',
194 as: 'ActorFollowers',
197 ActorFollowers: ActorFollowModel[]
199 @ForeignKey(() => ServerModel)
203 @BelongsTo(() => ServerModel, {
211 @HasOne(() => AccountModel, {
218 Account: AccountModel
220 @HasOne(() => VideoChannelModel, {
227 VideoChannel: VideoChannelModel
229 static load (id: number) {
230 return ActorModel.unscoped().findById(id)
233 static listByFollowersUrls (followersUrls: string[], transaction?: Sequelize.Transaction) {
237 [ Sequelize.Op.in ]: followersUrls
243 return ActorModel.scope(ScopeNames.FULL).findAll(query)
246 static loadLocalByName (preferredUsername: string) {
254 return ActorModel.scope(ScopeNames.FULL).findOne(query)
257 static loadByNameAndHost (preferredUsername: string, host: string) {
273 return ActorModel.scope(ScopeNames.FULL).findOne(query)
276 static loadByUrl (url: string, transaction?: Sequelize.Transaction) {
284 return ActorModel.scope(ScopeNames.FULL).findOne(query)
287 static incrementFollows (id: number, column: 'followersCount' | 'followingCount', by: number) {
289 return (ActorModel as any).increment(column, {
297 static async getActorsFollowerSharedInboxUrls (actors: ActorModel[], t: Sequelize.Transaction) {
302 [Sequelize.Op.in]: actors.map(a => a.id)
308 model: ActorFollowModel.unscoped(),
310 as: 'ActorFollowers',
316 attributes: [ 'sharedInboxUrl' ],
317 model: ActorModel.unscoped(),
327 const hash: { [ id: number ]: string[] } = {}
328 const res = await ActorModel.findAll(query)
329 for (const actor of res) {
330 hash[actor.id] = actor.ActorFollowers.map(follow => follow.ActorFollower.sharedInboxUrl)
337 let avatar: Avatar = null
339 avatar = this.Avatar.toFormattedJSON()
346 name: this.preferredUsername,
347 host: this.getHost(),
348 followingCount: this.followingCount,
349 followersCount: this.followersCount,
351 createdAt: this.createdAt,
352 updatedAt: this.updatedAt
356 toActivityPubObject (name: string, type: 'Account' | 'Application' | 'VideoChannel') {
358 if (type === 'Account') {
359 activityPubType = 'Person' as 'Person'
360 } else if (type === 'Application') {
361 activityPubType = 'Application' as 'Application'
362 } else { // VideoChannel
363 activityPubType = 'Group' as 'Group'
368 const extension = extname(this.Avatar.filename)
371 mediaType: extension === '.png' ? 'image/png' : 'image/jpeg',
372 url: this.getAvatarUrl()
377 type: activityPubType,
379 following: this.getFollowingUrl(),
380 followers: this.getFollowersUrl(),
381 inbox: this.inboxUrl,
382 outbox: this.outboxUrl,
383 preferredUsername: this.preferredUsername,
387 sharedInbox: this.sharedInboxUrl
391 id: this.getPublicKeyUrl(),
393 publicKeyPem: this.publicKey
398 return activityPubContextify(json)
401 getFollowerSharedInboxUrls (t: Sequelize.Transaction) {
403 attributes: [ 'sharedInboxUrl' ],
407 model: ActorFollowModel.unscoped(),
409 as: 'ActorFollowing',
412 targetActorId: this.id
419 return ActorModel.findAll(query)
420 .then(accounts => accounts.map(a => a.sharedInboxUrl))
424 return this.url + '/following'
428 return this.url + '/followers'
432 return this.url + '#main-key'
436 return this.serverId === null
440 return 'acct:' + this.preferredUsername + '@' + this.getHost()
444 return this.Server ? this.Server.host : CONFIG.WEBSERVER.HOST
448 if (!this.avatarId) return undefined
450 return CONFIG.WEBSERVER.URL + this.Avatar.getWebserverPath()
454 if (this.isOwned()) return false
456 const now = Date.now()
457 const createdAtTime = this.createdAt.getTime()
458 const updatedAtTime = this.updatedAt.getTime()
460 return (now - createdAtTime) > ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL &&
461 (now - updatedAtTime) > ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL