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'
48 model: () => ServerModel,
52 model: () => AvatarModel,
61 model: () => AccountModel.unscoped(),
65 model: () => VideoChannelModel.unscoped(),
69 model: () => ServerModel,
73 model: () => AvatarModel,
86 fields: [ 'preferredUsername', 'serverId' ],
90 fields: [ 'inboxUrl', 'sharedInboxUrl' ]
93 fields: [ 'serverId' ]
96 fields: [ 'avatarId' ]
100 export class ActorModel extends Model<ActorModel> {
103 @Column(DataType.ENUM(values(ACTIVITY_PUB_ACTOR_TYPES)))
104 type: ActivityPubActorType
107 @Default(DataType.UUIDV4)
109 @Column(DataType.UUID)
113 @Is('ActorPreferredUsername', value => throwIfNotValid(value, isActorPreferredUsernameValid, 'actor preferred username'))
115 preferredUsername: string
118 @Is('ActorUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
119 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
123 @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPublicKeyValid, 'public key'))
124 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.PUBLIC_KEY.max))
128 @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPrivateKeyValid, 'private key'))
129 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.PRIVATE_KEY.max))
133 @Is('ActorFollowersCount', value => throwIfNotValid(value, isActorFollowersCountValid, 'followers count'))
135 followersCount: number
138 @Is('ActorFollowersCount', value => throwIfNotValid(value, isActorFollowingCountValid, 'following count'))
140 followingCount: number
143 @Is('ActorInboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'inbox url'))
144 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
148 @Is('ActorOutboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'outbox url'))
149 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
153 @Is('ActorSharedInboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'shared inbox url'))
154 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
155 sharedInboxUrl: string
158 @Is('ActorFollowersUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'followers url'))
159 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
163 @Is('ActorFollowingUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'following url'))
164 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
173 @ForeignKey(() => AvatarModel)
177 @BelongsTo(() => AvatarModel, {
181 onDelete: 'set null',
186 @HasMany(() => ActorFollowModel, {
193 ActorFollowing: ActorFollowModel[]
195 @HasMany(() => ActorFollowModel, {
197 name: 'targetActorId',
200 as: 'ActorFollowers',
203 ActorFollowers: ActorFollowModel[]
205 @ForeignKey(() => ServerModel)
209 @BelongsTo(() => ServerModel, {
217 @HasOne(() => AccountModel, {
224 Account: AccountModel
226 @HasOne(() => VideoChannelModel, {
233 VideoChannel: VideoChannelModel
235 static load (id: number) {
236 return ActorModel.unscoped().findById(id)
239 static listByFollowersUrls (followersUrls: string[], transaction?: Sequelize.Transaction) {
243 [ Sequelize.Op.in ]: followersUrls
249 return ActorModel.scope(ScopeNames.FULL).findAll(query)
252 static loadLocalByName (preferredUsername: string) {
260 return ActorModel.scope(ScopeNames.FULL).findOne(query)
263 static loadByNameAndHost (preferredUsername: string, host: string) {
279 return ActorModel.scope(ScopeNames.FULL).findOne(query)
282 static loadByUrl (url: string, transaction?: Sequelize.Transaction) {
290 return ActorModel.scope(ScopeNames.FULL).findOne(query)
293 static incrementFollows (id: number, column: 'followersCount' | 'followingCount', by: number) {
295 return (ActorModel as any).increment(column, {
303 static async getActorsFollowerSharedInboxUrls (actors: ActorModel[], t: Sequelize.Transaction) {
308 [Sequelize.Op.in]: actors.map(a => a.id)
314 model: ActorFollowModel.unscoped(),
316 as: 'ActorFollowers',
322 attributes: [ 'sharedInboxUrl' ],
323 model: ActorModel.unscoped(),
333 const hash: { [ id: number ]: string[] } = {}
334 const res = await ActorModel.findAll(query)
335 for (const actor of res) {
336 hash[actor.id] = actor.ActorFollowers.map(follow => follow.ActorFollower.sharedInboxUrl)
343 let avatar: Avatar = null
345 avatar = this.Avatar.toFormattedJSON()
352 name: this.preferredUsername,
353 host: this.getHost(),
354 followingCount: this.followingCount,
355 followersCount: this.followersCount,
357 createdAt: this.createdAt,
358 updatedAt: this.updatedAt
362 toActivityPubObject (name: string, type: 'Account' | 'Application' | 'VideoChannel') {
364 if (type === 'Account') {
365 activityPubType = 'Person' as 'Person'
366 } else if (type === 'Application') {
367 activityPubType = 'Application' as 'Application'
368 } else { // VideoChannel
369 activityPubType = 'Group' as 'Group'
374 const extension = extname(this.Avatar.filename)
377 mediaType: extension === '.png' ? 'image/png' : 'image/jpeg',
378 url: this.getAvatarUrl()
383 type: activityPubType,
385 following: this.getFollowingUrl(),
386 followers: this.getFollowersUrl(),
387 inbox: this.inboxUrl,
388 outbox: this.outboxUrl,
389 preferredUsername: this.preferredUsername,
393 sharedInbox: this.sharedInboxUrl
397 id: this.getPublicKeyUrl(),
399 publicKeyPem: this.publicKey
404 return activityPubContextify(json)
407 getFollowerSharedInboxUrls (t: Sequelize.Transaction) {
409 attributes: [ 'sharedInboxUrl' ],
413 model: ActorFollowModel.unscoped(),
415 as: 'ActorFollowing',
418 targetActorId: this.id
425 return ActorModel.findAll(query)
426 .then(accounts => accounts.map(a => a.sharedInboxUrl))
430 return this.url + '/following'
434 return this.url + '/followers'
438 return this.url + '#main-key'
442 return this.serverId === null
446 return 'acct:' + this.preferredUsername + '@' + this.getHost()
450 return this.Server ? this.Server.host : CONFIG.WEBSERVER.HOST
454 if (!this.avatarId) return undefined
456 return CONFIG.WEBSERVER.URL + this.Avatar.getWebserverPath()
460 if (this.isOwned()) return false
462 const now = Date.now()
463 const createdAtTime = this.createdAt.getTime()
464 const updatedAtTime = this.updatedAt.getTime()
466 return (now - createdAtTime) > ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL &&
467 (now - updatedAtTime) > ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL