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 = [
59 model: () => ServerModel,
63 model: () => AvatarModel,
72 model: () => AccountModel.unscoped(),
76 model: () => VideoChannelModel.unscoped(),
80 model: () => ServerModel,
84 model: () => AvatarModel,
98 fields: [ 'preferredUsername', 'serverId' ],
102 fields: [ 'inboxUrl', 'sharedInboxUrl' ]
105 fields: [ 'sharedInboxUrl' ]
108 fields: [ 'serverId' ]
111 fields: [ 'avatarId' ]
118 fields: [ 'followersUrl' ]
122 export class ActorModel extends Model<ActorModel> {
125 @Column(DataType.ENUM(values(ACTIVITY_PUB_ACTOR_TYPES)))
126 type: ActivityPubActorType
129 @Default(DataType.UUIDV4)
131 @Column(DataType.UUID)
135 @Is('ActorPreferredUsername', value => throwIfNotValid(value, isActorPreferredUsernameValid, 'actor preferred username'))
137 preferredUsername: string
140 @Is('ActorUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
141 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
145 @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPublicKeyValid, 'public key'))
146 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.PUBLIC_KEY.max))
150 @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPrivateKeyValid, 'private key'))
151 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.PRIVATE_KEY.max))
155 @Is('ActorFollowersCount', value => throwIfNotValid(value, isActorFollowersCountValid, 'followers count'))
157 followersCount: number
160 @Is('ActorFollowersCount', value => throwIfNotValid(value, isActorFollowingCountValid, 'following count'))
162 followingCount: number
165 @Is('ActorInboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'inbox url'))
166 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
170 @Is('ActorOutboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'outbox url'))
171 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
175 @Is('ActorSharedInboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'shared inbox url'))
176 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
177 sharedInboxUrl: string
180 @Is('ActorFollowersUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'followers url'))
181 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
185 @Is('ActorFollowingUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'following url'))
186 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
195 @ForeignKey(() => AvatarModel)
199 @BelongsTo(() => AvatarModel, {
203 onDelete: 'set null',
208 @HasMany(() => ActorFollowModel, {
215 ActorFollowing: ActorFollowModel[]
217 @HasMany(() => ActorFollowModel, {
219 name: 'targetActorId',
222 as: 'ActorFollowers',
225 ActorFollowers: ActorFollowModel[]
227 @ForeignKey(() => ServerModel)
231 @BelongsTo(() => ServerModel, {
239 @HasOne(() => AccountModel, {
246 Account: AccountModel
248 @HasOne(() => VideoChannelModel, {
255 VideoChannel: VideoChannelModel
257 static load (id: number) {
258 return ActorModel.unscoped().findById(id)
261 static listByFollowersUrls (followersUrls: string[], transaction?: Sequelize.Transaction) {
265 [ Sequelize.Op.in ]: followersUrls
271 return ActorModel.scope(ScopeNames.FULL).findAll(query)
274 static loadLocalByName (preferredUsername: string, transaction?: Sequelize.Transaction) {
283 return ActorModel.scope(ScopeNames.FULL).findOne(query)
286 static loadByNameAndHost (preferredUsername: string, host: string) {
302 return ActorModel.scope(ScopeNames.FULL).findOne(query)
305 static loadByUrl (url: string, transaction?: Sequelize.Transaction) {
313 return ActorModel.scope(ScopeNames.FULL).findOne(query)
316 static incrementFollows (id: number, column: 'followersCount' | 'followingCount', by: number) {
318 return (ActorModel as any).increment(column, {
327 let avatar: Avatar = null
329 avatar = this.Avatar.toFormattedJSON()
336 name: this.preferredUsername,
337 host: this.getHost(),
338 followingCount: this.followingCount,
339 followersCount: this.followersCount,
341 createdAt: this.createdAt,
342 updatedAt: this.updatedAt
346 toActivityPubObject (name: string, type: 'Account' | 'Application' | 'VideoChannel') {
348 if (type === 'Account') {
349 activityPubType = 'Person' as 'Person'
350 } else if (type === 'Application') {
351 activityPubType = 'Application' as 'Application'
352 } else { // VideoChannel
353 activityPubType = 'Group' as 'Group'
358 const extension = extname(this.Avatar.filename)
361 mediaType: extension === '.png' ? 'image/png' : 'image/jpeg',
362 url: this.getAvatarUrl()
367 type: activityPubType,
369 following: this.getFollowingUrl(),
370 followers: this.getFollowersUrl(),
371 inbox: this.inboxUrl,
372 outbox: this.outboxUrl,
373 preferredUsername: this.preferredUsername,
377 sharedInbox: this.sharedInboxUrl
381 id: this.getPublicKeyUrl(),
383 publicKeyPem: this.publicKey
388 return activityPubContextify(json)
391 getFollowerSharedInboxUrls (t: Sequelize.Transaction) {
393 attributes: [ 'sharedInboxUrl' ],
397 model: ActorFollowModel.unscoped(),
399 as: 'ActorFollowing',
402 targetActorId: this.id
409 return ActorModel.findAll(query)
410 .then(accounts => accounts.map(a => a.sharedInboxUrl))
414 return this.url + '/following'
418 return this.url + '/followers'
422 return this.url + '#main-key'
426 return this.serverId === null
430 return 'acct:' + this.preferredUsername + '@' + this.getHost()
434 return this.Server ? `${this.preferredUsername}@${this.Server.host}` : this.preferredUsername
438 return this.Server ? this.Server.host : CONFIG.WEBSERVER.HOST
442 if (!this.avatarId) return undefined
444 return CONFIG.WEBSERVER.URL + this.Avatar.getWebserverPath()
448 if (this.isOwned()) return false
450 const now = Date.now()
451 const createdAtTime = this.createdAt.getTime()
452 const updatedAtTime = this.updatedAt.getTime()
454 return (now - createdAtTime) > ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL &&
455 (now - updatedAtTime) > ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL