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 listByFollowersUrls (followersUrls: string[], transaction?: Sequelize.Transaction) {
273 [ Sequelize.Op.in ]: followersUrls
279 return ActorModel.scope(ScopeNames.FULL).findAll(query)
282 static loadLocalByName (preferredUsername: string, transaction?: Sequelize.Transaction) {
291 return ActorModel.scope(ScopeNames.FULL).findOne(query)
294 static loadByNameAndHost (preferredUsername: string, host: string) {
310 return ActorModel.scope(ScopeNames.FULL).findOne(query)
313 static loadByUrl (url: string, transaction?: Sequelize.Transaction) {
321 return ActorModel.scope(ScopeNames.FULL).findOne(query)
324 static incrementFollows (id: number, column: 'followersCount' | 'followingCount', by: number) {
326 return (ActorModel as any).increment(column, {
335 let avatar: Avatar = null
337 avatar = this.Avatar.toFormattedJSON()
344 name: this.preferredUsername,
345 host: this.getHost(),
346 hostRedundancyAllowed: this.getRedundancyAllowed(),
347 followingCount: this.followingCount,
348 followersCount: this.followersCount,
350 createdAt: this.createdAt,
351 updatedAt: this.updatedAt
355 toActivityPubObject (name: string, type: 'Account' | 'Application' | 'VideoChannel') {
357 if (type === 'Account') {
358 activityPubType = 'Person' as 'Person'
359 } else if (type === 'Application') {
360 activityPubType = 'Application' as 'Application'
361 } else { // VideoChannel
362 activityPubType = 'Group' as 'Group'
367 const extension = extname(this.Avatar.filename)
370 mediaType: extension === '.png' ? 'image/png' : 'image/jpeg',
371 url: this.getAvatarUrl()
376 type: activityPubType,
378 following: this.getFollowingUrl(),
379 followers: this.getFollowersUrl(),
380 inbox: this.inboxUrl,
381 outbox: this.outboxUrl,
382 preferredUsername: this.preferredUsername,
386 sharedInbox: this.sharedInboxUrl
390 id: this.getPublicKeyUrl(),
392 publicKeyPem: this.publicKey
397 return activityPubContextify(json)
400 getFollowerSharedInboxUrls (t: Sequelize.Transaction) {
402 attributes: [ 'sharedInboxUrl' ],
406 model: ActorFollowModel.unscoped(),
408 as: 'ActorFollowing',
411 targetActorId: this.id
418 return ActorModel.findAll(query)
419 .then(accounts => accounts.map(a => a.sharedInboxUrl))
423 return this.url + '/following'
427 return this.url + '/followers'
431 return this.url + '#main-key'
435 return this.serverId === null
439 return 'acct:' + this.preferredUsername + '@' + this.getHost()
443 return this.Server ? `${this.preferredUsername}@${this.Server.host}` : this.preferredUsername
447 return this.Server ? this.Server.host : CONFIG.WEBSERVER.HOST
450 getRedundancyAllowed () {
451 return this.Server ? this.Server.redundancyAllowed : false
455 if (!this.avatarId) return undefined
457 return CONFIG.WEBSERVER.URL + this.Avatar.getWebserverPath()
461 if (this.isOwned()) return false
463 const now = Date.now()
464 const createdAtTime = this.createdAt.getTime()
465 const updatedAtTime = this.updatedAt.getTime()
467 return (now - createdAtTime) > ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL &&
468 (now - updatedAtTime) > ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL