1 import { values } from 'lodash'
2 import { extname } from 'path'
3 import * as Sequelize from 'sequelize'
5 AllowNull, BelongsTo, Column, CreatedAt, DataType, Default, DefaultScope, ForeignKey, HasMany, HasOne, Is, IsUUID, Model, Scopes,
7 } from 'sequelize-typescript'
8 import { ActivityPubActorType } from '../../../shared/models/activitypub'
9 import { Avatar } from '../../../shared/models/avatars/avatar.model'
10 import { activityPubContextify } from '../../helpers/activitypub'
12 isActorFollowersCountValid, isActorFollowingCountValid, isActorPreferredUsernameValid, isActorPrivateKeyValid,
14 } from '../../helpers/custom-validators/activitypub/actor'
15 import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
16 import { ACTIVITY_PUB, ACTIVITY_PUB_ACTOR_TYPES, CONFIG, CONSTRAINTS_FIELDS } from '../../initializers'
17 import { AccountModel } from '../account/account'
18 import { AvatarModel } from '../avatar/avatar'
19 import { ServerModel } from '../server/server'
20 import { throwIfNotValid } from '../utils'
21 import { VideoChannelModel } from '../video/video-channel'
22 import { ActorFollowModel } from './actor-follow'
31 model: () => ServerModel,
35 model: () => AvatarModel,
44 model: () => AccountModel.unscoped(),
48 model: () => VideoChannelModel.unscoped(),
52 model: () => ServerModel,
56 model: () => AvatarModel,
69 fields: [ 'preferredUsername', 'serverId' ],
73 fields: [ 'inboxUrl', 'sharedInboxUrl' ]
77 export class ActorModel extends Model<ActorModel> {
80 @Column(DataType.ENUM(values(ACTIVITY_PUB_ACTOR_TYPES)))
81 type: ActivityPubActorType
84 @Default(DataType.UUIDV4)
86 @Column(DataType.UUID)
90 @Is('ActorPreferredUsername', value => throwIfNotValid(value, isActorPreferredUsernameValid, 'actor preferred username'))
92 preferredUsername: string
95 @Is('ActorUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
96 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
100 @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPublicKeyValid, 'public key'))
101 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.PUBLIC_KEY.max))
105 @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPrivateKeyValid, 'private key'))
106 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.PRIVATE_KEY.max))
110 @Is('ActorFollowersCount', value => throwIfNotValid(value, isActorFollowersCountValid, 'followers count'))
112 followersCount: number
115 @Is('ActorFollowersCount', value => throwIfNotValid(value, isActorFollowingCountValid, 'following count'))
117 followingCount: number
120 @Is('ActorInboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'inbox url'))
121 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
125 @Is('ActorOutboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'outbox url'))
126 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
130 @Is('ActorSharedInboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'shared inbox url'))
131 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
132 sharedInboxUrl: string
135 @Is('ActorFollowersUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'followers url'))
136 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
140 @Is('ActorFollowingUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'following url'))
141 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
150 @ForeignKey(() => AvatarModel)
154 @BelongsTo(() => AvatarModel, {
162 @HasMany(() => ActorFollowModel, {
169 AccountFollowing: ActorFollowModel[]
171 @HasMany(() => ActorFollowModel, {
173 name: 'targetActorId',
179 AccountFollowers: ActorFollowModel[]
181 @ForeignKey(() => ServerModel)
185 @BelongsTo(() => ServerModel, {
193 @HasOne(() => AccountModel, {
199 Account: AccountModel
201 @HasOne(() => VideoChannelModel, {
207 VideoChannel: VideoChannelModel
209 static load (id: number) {
210 return ActorModel.unscoped().findById(id)
213 static listByFollowersUrls (followersUrls: string[], transaction?: Sequelize.Transaction) {
217 [ Sequelize.Op.in ]: followersUrls
223 return ActorModel.scope(ScopeNames.FULL).findAll(query)
226 static loadLocalByName (preferredUsername: string) {
234 return ActorModel.scope(ScopeNames.FULL).findOne(query)
237 static loadByNameAndHost (preferredUsername: string, host: string) {
253 return ActorModel.scope(ScopeNames.FULL).findOne(query)
256 static loadByUrl (url: string, transaction?: Sequelize.Transaction) {
264 return ActorModel.scope(ScopeNames.FULL).findOne(query)
267 static incrementFollows (id: number, column: 'followersCount' | 'followingCount', by: number) {
269 return (ActorModel as any).increment(column, {
278 let avatar: Avatar = null
280 avatar = this.Avatar.toFormattedJSON()
287 name: this.preferredUsername,
288 host: this.getHost(),
289 followingCount: this.followingCount,
290 followersCount: this.followersCount,
292 createdAt: this.createdAt,
293 updatedAt: this.updatedAt
297 toActivityPubObject (name: string, type: 'Account' | 'Application' | 'VideoChannel') {
299 if (type === 'Account') {
300 activityPubType = 'Person' as 'Person'
301 } else if (type === 'Application') {
302 activityPubType = 'Application' as 'Application'
303 } else { // VideoChannel
304 activityPubType = 'Group' as 'Group'
309 const extension = extname(this.Avatar.filename)
312 mediaType: extension === '.png' ? 'image/png' : 'image/jpeg',
313 url: this.getAvatarUrl()
318 type: activityPubType,
320 following: this.getFollowingUrl(),
321 followers: this.getFollowersUrl(),
322 inbox: this.inboxUrl,
323 outbox: this.outboxUrl,
324 preferredUsername: this.preferredUsername,
328 sharedInbox: this.sharedInboxUrl
332 id: this.getPublicKeyUrl(),
334 publicKeyPem: this.publicKey
339 return activityPubContextify(json)
342 getFollowerSharedInboxUrls (t: Sequelize.Transaction) {
344 attributes: [ 'sharedInboxUrl' ],
347 model: ActorFollowModel,
351 targetActorId: this.id
358 return ActorModel.findAll(query)
359 .then(accounts => accounts.map(a => a.sharedInboxUrl))
363 return this.url + '/following'
367 return this.url + '/followers'
371 return this.url + '#main-key'
375 return this.serverId === null
379 return 'acct:' + this.preferredUsername + '@' + this.getHost()
383 return this.Server ? this.Server.host : CONFIG.WEBSERVER.HOST
387 if (!this.avatarId) return undefined
389 return CONFIG.WEBSERVER.URL + this.Avatar.getWebserverPath()
393 if (this.isOwned()) return false
395 const now = Date.now()
396 const createdAtTime = this.createdAt.getTime()
397 const updatedAtTime = this.updatedAt.getTime()
399 return (now - createdAtTime) > ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL &&
400 (now - updatedAtTime) > ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL