1 import { values } from 'lodash'
2 import { join } from 'path'
3 import * as Sequelize from 'sequelize'
20 } from 'sequelize-typescript'
21 import { ActivityPubActorType } from '../../../shared/models/activitypub'
22 import { Avatar } from '../../../shared/models/avatars/avatar.model'
23 import { activityPubContextify } from '../../helpers'
25 isActivityPubUrlValid,
26 isActorFollowersCountValid,
27 isActorFollowingCountValid,
29 isActorPrivateKeyValid,
31 } from '../../helpers/custom-validators/activitypub'
32 import { ACTIVITY_PUB_ACTOR_TYPES, AVATARS_DIR, CONFIG, CONSTRAINTS_FIELDS } from '../../initializers'
33 import { AccountModel } from '../account/account'
34 import { AvatarModel } from '../avatar/avatar'
35 import { ServerModel } from '../server/server'
36 import { throwIfNotValid } from '../utils'
37 import { VideoChannelModel } from '../video/video-channel'
38 import { ActorFollowModel } from './actor-follow'
48 model: () => AccountModel,
52 model: () => VideoChannelModel,
62 fields: [ 'name', 'serverId' ],
67 export class ActorModel extends Model<ActorModel> {
70 @Column(DataType.ENUM(values(ACTIVITY_PUB_ACTOR_TYPES)))
71 type: ActivityPubActorType
74 @Default(DataType.UUIDV4)
76 @Column(DataType.UUID)
80 @Is('ActorName', value => throwIfNotValid(value, isActorNameValid, 'actor name'))
85 @Is('ActorUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
86 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTOR.URL.max))
90 @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPublicKeyValid, 'public key'))
91 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTOR.PUBLIC_KEY.max))
95 @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPrivateKeyValid, 'private key'))
96 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTOR.PRIVATE_KEY.max))
100 @Is('ActorFollowersCount', value => throwIfNotValid(value, isActorFollowersCountValid, 'followers count'))
102 followersCount: number
105 @Is('ActorFollowersCount', value => throwIfNotValid(value, isActorFollowingCountValid, 'following count'))
107 followingCount: number
110 @Is('ActorInboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'inbox url'))
111 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTOR.URL.max))
115 @Is('ActorOutboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'outbox url'))
116 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTOR.URL.max))
120 @Is('ActorSharedInboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'shared inbox url'))
121 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTOR.URL.max))
122 sharedInboxUrl: string
125 @Is('ActorFollowersUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'followers url'))
126 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTOR.URL.max))
130 @Is('ActorFollowingUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'following url'))
131 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTOR.URL.max))
140 @ForeignKey(() => AvatarModel)
144 @BelongsTo(() => AvatarModel, {
152 @HasMany(() => ActorFollowModel, {
159 AccountFollowing: ActorFollowModel[]
161 @HasMany(() => ActorFollowModel, {
163 name: 'targetActorId',
169 AccountFollowers: ActorFollowModel[]
171 @ForeignKey(() => ServerModel)
175 @BelongsTo(() => ServerModel, {
183 @HasOne(() => AccountModel, {
189 Account: AccountModel
191 @HasOne(() => VideoChannelModel, {
197 VideoChannel: VideoChannelModel
199 static load (id: number) {
200 return ActorModel.scope(ScopeNames.FULL).findById(id)
203 static loadByUUID (uuid: string) {
210 return ActorModel.scope(ScopeNames.FULL).findOne(query)
213 static listByFollowersUrls (followersUrls: string[], transaction?: Sequelize.Transaction) {
217 [ Sequelize.Op.in ]: followersUrls
223 return ActorModel.scope(ScopeNames.FULL).findAll(query)
226 static loadLocalByName (name: string) {
234 return ActorModel.scope(ScopeNames.FULL).findOne(query)
237 static loadByNameAndHost (name: 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)
268 let avatar: Avatar = null
271 path: join(AVATARS_DIR.ACCOUNT, this.Avatar.filename),
272 createdAt: this.Avatar.createdAt,
273 updatedAt: this.Avatar.updatedAt
277 let host = CONFIG.WEBSERVER.HOST
280 host = this.Server.host
281 score = this.Server.score
289 followingCount: this.followingCount,
290 followersCount: this.followersCount,
295 toActivityPubObject (preferredUsername: string, type: 'Account' | 'Application' | 'VideoChannel') {
297 if (type === 'Account') {
298 activityPubType = 'Person' as 'Person'
299 } else if (type === 'Application') {
300 activityPubType = 'Application' as 'Application'
301 } else { // VideoChannel
302 activityPubType = 'Group' as 'Group'
306 type: activityPubType,
308 following: this.getFollowingUrl(),
309 followers: this.getFollowersUrl(),
310 inbox: this.inboxUrl,
311 outbox: this.outboxUrl,
316 sharedInbox: this.sharedInboxUrl
320 id: this.getPublicKeyUrl(),
322 publicKeyPem: this.publicKey
326 return activityPubContextify(json)
329 getFollowerSharedInboxUrls (t: Sequelize.Transaction) {
331 attributes: [ 'sharedInboxUrl' ],
334 model: ActorFollowModel,
338 targetActorId: this.id
345 return ActorModel.findAll(query)
346 .then(accounts => accounts.map(a => a.sharedInboxUrl))
350 return this.url + '/following'
354 return this.url + '/followers'
358 return this.url + '#main-key'
362 return this.serverId === null