1 import { values } from 'lodash'
2 import { join } from 'path'
3 import * as Sequelize from 'sequelize'
10 Default, DefaultScope,
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'
47 model: () => ServerModel,
56 model: () => AccountModel,
60 model: () => VideoChannelModel,
64 model: () => ServerModel,
74 fields: [ 'name', 'serverId' ],
79 export class ActorModel extends Model<ActorModel> {
82 @Column(DataType.ENUM(values(ACTIVITY_PUB_ACTOR_TYPES)))
83 type: ActivityPubActorType
86 @Default(DataType.UUIDV4)
88 @Column(DataType.UUID)
92 @Is('ActorName', value => throwIfNotValid(value, isActorNameValid, 'actor name'))
97 @Is('ActorUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
98 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTOR.URL.max))
102 @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPublicKeyValid, 'public key'))
103 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTOR.PUBLIC_KEY.max))
107 @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPrivateKeyValid, 'private key'))
108 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTOR.PRIVATE_KEY.max))
112 @Is('ActorFollowersCount', value => throwIfNotValid(value, isActorFollowersCountValid, 'followers count'))
114 followersCount: number
117 @Is('ActorFollowersCount', value => throwIfNotValid(value, isActorFollowingCountValid, 'following count'))
119 followingCount: number
122 @Is('ActorInboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'inbox url'))
123 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTOR.URL.max))
127 @Is('ActorOutboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'outbox url'))
128 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTOR.URL.max))
132 @Is('ActorSharedInboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'shared inbox url'))
133 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTOR.URL.max))
134 sharedInboxUrl: string
137 @Is('ActorFollowersUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'followers url'))
138 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTOR.URL.max))
142 @Is('ActorFollowingUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'following url'))
143 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTOR.URL.max))
152 @ForeignKey(() => AvatarModel)
156 @BelongsTo(() => AvatarModel, {
164 @HasMany(() => ActorFollowModel, {
171 AccountFollowing: ActorFollowModel[]
173 @HasMany(() => ActorFollowModel, {
175 name: 'targetActorId',
181 AccountFollowers: ActorFollowModel[]
183 @ForeignKey(() => ServerModel)
187 @BelongsTo(() => ServerModel, {
195 @HasOne(() => AccountModel, {
201 Account: AccountModel
203 @HasOne(() => VideoChannelModel, {
209 VideoChannel: VideoChannelModel
211 static load (id: number) {
212 return ActorModel.scope(ScopeNames.FULL).findById(id)
215 static loadByUUID (uuid: string) {
222 return ActorModel.scope(ScopeNames.FULL).findOne(query)
225 static listByFollowersUrls (followersUrls: string[], transaction?: Sequelize.Transaction) {
229 [ Sequelize.Op.in ]: followersUrls
235 return ActorModel.scope(ScopeNames.FULL).findAll(query)
238 static loadLocalByName (name: string) {
246 return ActorModel.scope(ScopeNames.FULL).findOne(query)
249 static loadByNameAndHost (name: string, host: string) {
265 return ActorModel.scope(ScopeNames.FULL).findOne(query)
268 static loadByUrl (url: string, transaction?: Sequelize.Transaction) {
276 return ActorModel.scope(ScopeNames.FULL).findOne(query)
280 let avatar: Avatar = null
283 path: join(AVATARS_DIR.ACCOUNT, this.Avatar.filename),
284 createdAt: this.Avatar.createdAt,
285 updatedAt: this.Avatar.updatedAt
289 let host = CONFIG.WEBSERVER.HOST
292 host = this.Server.host
293 score = this.Server.score
301 followingCount: this.followingCount,
302 followersCount: this.followersCount,
307 toActivityPubObject (preferredUsername: string, type: 'Account' | 'Application' | 'VideoChannel') {
309 if (type === 'Account') {
310 activityPubType = 'Person' as 'Person'
311 } else if (type === 'Application') {
312 activityPubType = 'Application' as 'Application'
313 } else { // VideoChannel
314 activityPubType = 'Group' as 'Group'
318 type: activityPubType,
320 following: this.getFollowingUrl(),
321 followers: this.getFollowersUrl(),
322 inbox: this.inboxUrl,
323 outbox: this.outboxUrl,
328 sharedInbox: this.sharedInboxUrl
332 id: this.getPublicKeyUrl(),
334 publicKeyPem: this.publicKey
338 return activityPubContextify(json)
341 getFollowerSharedInboxUrls (t: Sequelize.Transaction) {
343 attributes: [ 'sharedInboxUrl' ],
346 model: ActorFollowModel,
350 targetActorId: this.id
357 return ActorModel.findAll(query)
358 .then(accounts => accounts.map(a => a.sharedInboxUrl))
362 return this.url + '/following'
366 return this.url + '/followers'
370 return this.url + '#main-key'
374 return this.serverId === null