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,
48 model: () => VideoChannelModel,
52 model: () => ServerModel,
56 model: () => AvatarModel,
69 fields: [ 'preferredUsername', 'serverId' ],
74 export class ActorModel extends Model<ActorModel> {
77 @Column(DataType.ENUM(values(ACTIVITY_PUB_ACTOR_TYPES)))
78 type: ActivityPubActorType
81 @Default(DataType.UUIDV4)
83 @Column(DataType.UUID)
87 @Is('ActorPreferredUsername', value => throwIfNotValid(value, isActorPreferredUsernameValid, 'actor preferred username'))
89 preferredUsername: string
92 @Is('ActorUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
93 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
97 @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPublicKeyValid, 'public key'))
98 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.PUBLIC_KEY.max))
102 @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPrivateKeyValid, 'private key'))
103 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.PRIVATE_KEY.max))
107 @Is('ActorFollowersCount', value => throwIfNotValid(value, isActorFollowersCountValid, 'followers count'))
109 followersCount: number
112 @Is('ActorFollowersCount', value => throwIfNotValid(value, isActorFollowingCountValid, 'following count'))
114 followingCount: number
117 @Is('ActorInboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'inbox url'))
118 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
122 @Is('ActorOutboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'outbox url'))
123 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
127 @Is('ActorSharedInboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'shared inbox url'))
128 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
129 sharedInboxUrl: string
132 @Is('ActorFollowersUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'followers url'))
133 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
137 @Is('ActorFollowingUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'following url'))
138 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
147 @ForeignKey(() => AvatarModel)
151 @BelongsTo(() => AvatarModel, {
159 @HasMany(() => ActorFollowModel, {
166 AccountFollowing: ActorFollowModel[]
168 @HasMany(() => ActorFollowModel, {
170 name: 'targetActorId',
176 AccountFollowers: ActorFollowModel[]
178 @ForeignKey(() => ServerModel)
182 @BelongsTo(() => ServerModel, {
190 @HasOne(() => AccountModel, {
196 Account: AccountModel
198 @HasOne(() => VideoChannelModel, {
204 VideoChannel: VideoChannelModel
206 static load (id: number) {
207 return ActorModel.scope(ScopeNames.FULL).findById(id)
210 static listByFollowersUrls (followersUrls: string[], transaction?: Sequelize.Transaction) {
214 [ Sequelize.Op.in ]: followersUrls
220 return ActorModel.scope(ScopeNames.FULL).findAll(query)
223 static loadLocalByName (preferredUsername: string) {
231 return ActorModel.scope(ScopeNames.FULL).findOne(query)
234 static loadByNameAndHost (preferredUsername: string, host: string) {
250 return ActorModel.scope(ScopeNames.FULL).findOne(query)
253 static loadByUrl (url: string, transaction?: Sequelize.Transaction) {
261 return ActorModel.scope(ScopeNames.FULL).findOne(query)
265 let avatar: Avatar = null
267 avatar = this.Avatar.toFormattedJSON()
272 score = this.Server.score
279 host: this.getHost(),
281 followingCount: this.followingCount,
282 followersCount: this.followersCount,
287 toActivityPubObject (name: string, type: 'Account' | 'Application' | 'VideoChannel') {
289 if (type === 'Account') {
290 activityPubType = 'Person' as 'Person'
291 } else if (type === 'Application') {
292 activityPubType = 'Application' as 'Application'
293 } else { // VideoChannel
294 activityPubType = 'Group' as 'Group'
299 const extension = extname(this.Avatar.filename)
302 mediaType: extension === '.png' ? 'image/png' : 'image/jpeg',
303 url: this.getAvatarUrl()
308 type: activityPubType,
310 following: this.getFollowingUrl(),
311 followers: this.getFollowersUrl(),
312 inbox: this.inboxUrl,
313 outbox: this.outboxUrl,
314 preferredUsername: this.preferredUsername,
318 sharedInbox: this.sharedInboxUrl
322 id: this.getPublicKeyUrl(),
324 publicKeyPem: this.publicKey
329 return activityPubContextify(json)
332 getFollowerSharedInboxUrls (t: Sequelize.Transaction) {
334 attributes: [ 'sharedInboxUrl' ],
337 model: ActorFollowModel,
341 targetActorId: this.id
348 return ActorModel.findAll(query)
349 .then(accounts => accounts.map(a => a.sharedInboxUrl))
353 return this.url + '/following'
357 return this.url + '/followers'
361 return this.url + '#main-key'
365 return this.serverId === null
369 return 'acct:' + this.preferredUsername + '@' + this.getHost()
373 return this.Server ? this.Server.host : CONFIG.WEBSERVER.HOST
377 if (!this.avatarId) return undefined
379 return CONFIG.WEBSERVER.URL + this.Avatar.getWebserverPath()
383 if (this.isOwned()) return false
385 const now = Date.now()
386 const createdAtTime = this.createdAt.getTime()
387 const updatedAtTime = this.updatedAt.getTime()
389 return (now - createdAtTime) > ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL &&
390 (now - updatedAtTime) > ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL