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 = [
61 model: () => ServerModel,
65 model: () => AvatarModel,
74 model: () => AccountModel.unscoped(),
78 model: () => VideoChannelModel.unscoped(),
82 model: () => AccountModel,
88 model: () => ServerModel,
92 model: () => AvatarModel,
106 fields: [ 'preferredUsername', 'serverId' ],
110 fields: [ 'inboxUrl', 'sharedInboxUrl' ]
113 fields: [ 'sharedInboxUrl' ]
116 fields: [ 'serverId' ]
119 fields: [ 'avatarId' ]
126 fields: [ 'followersUrl' ]
130 export class ActorModel extends Model<ActorModel> {
133 @Column(DataType.ENUM(values(ACTIVITY_PUB_ACTOR_TYPES)))
134 type: ActivityPubActorType
137 @Default(DataType.UUIDV4)
139 @Column(DataType.UUID)
143 @Is('ActorPreferredUsername', value => throwIfNotValid(value, isActorPreferredUsernameValid, 'actor preferred username'))
145 preferredUsername: string
148 @Is('ActorUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
149 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
153 @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPublicKeyValid, 'public key'))
154 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.PUBLIC_KEY.max))
158 @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPrivateKeyValid, 'private key'))
159 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.PRIVATE_KEY.max))
163 @Is('ActorFollowersCount', value => throwIfNotValid(value, isActorFollowersCountValid, 'followers count'))
165 followersCount: number
168 @Is('ActorFollowersCount', value => throwIfNotValid(value, isActorFollowingCountValid, 'following count'))
170 followingCount: number
173 @Is('ActorInboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'inbox url'))
174 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
178 @Is('ActorOutboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'outbox url'))
179 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
183 @Is('ActorSharedInboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'shared inbox url'))
184 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
185 sharedInboxUrl: string
188 @Is('ActorFollowersUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'followers url'))
189 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
193 @Is('ActorFollowingUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'following url'))
194 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
203 @ForeignKey(() => AvatarModel)
207 @BelongsTo(() => AvatarModel, {
211 onDelete: 'set null',
216 @HasMany(() => ActorFollowModel, {
223 ActorFollowing: ActorFollowModel[]
225 @HasMany(() => ActorFollowModel, {
227 name: 'targetActorId',
230 as: 'ActorFollowers',
233 ActorFollowers: ActorFollowModel[]
235 @ForeignKey(() => ServerModel)
239 @BelongsTo(() => ServerModel, {
247 @HasOne(() => AccountModel, {
254 Account: AccountModel
256 @HasOne(() => VideoChannelModel, {
263 VideoChannel: VideoChannelModel
265 static load (id: number) {
266 return ActorModel.unscoped().findById(id)
269 static isActorUrlExist (url: string) {
277 return ActorModel.unscoped().findOne(query)
281 static listByFollowersUrls (followersUrls: string[], transaction?: Sequelize.Transaction) {
285 [ Sequelize.Op.in ]: followersUrls
291 return ActorModel.scope(ScopeNames.FULL).findAll(query)
294 static loadLocalByName (preferredUsername: string, transaction?: Sequelize.Transaction) {
303 return ActorModel.scope(ScopeNames.FULL).findOne(query)
306 static loadByNameAndHost (preferredUsername: string, host: string) {
322 return ActorModel.scope(ScopeNames.FULL).findOne(query)
325 static loadByUrl (url: string, transaction?: Sequelize.Transaction) {
333 return ActorModel.scope(ScopeNames.FULL).findOne(query)
336 static incrementFollows (id: number, column: 'followersCount' | 'followingCount', by: number) {
338 return (ActorModel as any).increment(column, {
347 let avatar: Avatar = null
349 avatar = this.Avatar.toFormattedJSON()
356 name: this.preferredUsername,
357 host: this.getHost(),
358 hostRedundancyAllowed: this.getRedundancyAllowed(),
359 followingCount: this.followingCount,
360 followersCount: this.followersCount,
362 createdAt: this.createdAt,
363 updatedAt: this.updatedAt
367 toActivityPubObject (name: string, type: 'Account' | 'Application' | 'VideoChannel') {
369 if (type === 'Account') {
370 activityPubType = 'Person' as 'Person'
371 } else if (type === 'Application') {
372 activityPubType = 'Application' as 'Application'
373 } else { // VideoChannel
374 activityPubType = 'Group' as 'Group'
379 const extension = extname(this.Avatar.filename)
382 mediaType: extension === '.png' ? 'image/png' : 'image/jpeg',
383 url: this.getAvatarUrl()
388 type: activityPubType,
390 following: this.getFollowingUrl(),
391 followers: this.getFollowersUrl(),
392 inbox: this.inboxUrl,
393 outbox: this.outboxUrl,
394 preferredUsername: this.preferredUsername,
398 sharedInbox: this.sharedInboxUrl
402 id: this.getPublicKeyUrl(),
404 publicKeyPem: this.publicKey
409 return activityPubContextify(json)
412 getFollowerSharedInboxUrls (t: Sequelize.Transaction) {
414 attributes: [ 'sharedInboxUrl' ],
418 model: ActorFollowModel.unscoped(),
420 as: 'ActorFollowing',
423 targetActorId: this.id
430 return ActorModel.findAll(query)
431 .then(accounts => accounts.map(a => a.sharedInboxUrl))
435 return this.url + '/following'
439 return this.url + '/followers'
443 return this.url + '#main-key'
447 return this.serverId === null
451 return 'acct:' + this.preferredUsername + '@' + this.getHost()
455 return this.Server ? `${this.preferredUsername}@${this.Server.host}` : this.preferredUsername
459 return this.Server ? this.Server.host : CONFIG.WEBSERVER.HOST
462 getRedundancyAllowed () {
463 return this.Server ? this.Server.redundancyAllowed : false
467 if (!this.avatarId) return undefined
469 return CONFIG.WEBSERVER.URL + this.Avatar.getWebserverPath()
473 if (this.isOwned()) return false
475 const now = Date.now()
476 const createdAtTime = this.createdAt.getTime()
477 const updatedAtTime = this.updatedAt.getTime()
479 return (now - createdAtTime) > ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL &&
480 (now - updatedAtTime) > ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL