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, {
158 onDelete: 'set null',
163 @HasMany(() => ActorFollowModel, {
170 ActorFollowing: ActorFollowModel[]
172 @HasMany(() => ActorFollowModel, {
174 name: 'targetActorId',
177 as: 'ActorFollowers',
180 ActorFollowers: ActorFollowModel[]
182 @ForeignKey(() => ServerModel)
186 @BelongsTo(() => ServerModel, {
194 @HasOne(() => AccountModel, {
201 Account: AccountModel
203 @HasOne(() => VideoChannelModel, {
210 VideoChannel: VideoChannelModel
212 static load (id: number) {
213 return ActorModel.unscoped().findById(id)
216 static listByFollowersUrls (followersUrls: string[], transaction?: Sequelize.Transaction) {
220 [ Sequelize.Op.in ]: followersUrls
226 return ActorModel.scope(ScopeNames.FULL).findAll(query)
229 static loadLocalByName (preferredUsername: string) {
237 return ActorModel.scope(ScopeNames.FULL).findOne(query)
240 static loadByNameAndHost (preferredUsername: string, host: string) {
256 return ActorModel.scope(ScopeNames.FULL).findOne(query)
259 static loadByUrl (url: string, transaction?: Sequelize.Transaction) {
267 return ActorModel.scope(ScopeNames.FULL).findOne(query)
270 static incrementFollows (id: number, column: 'followersCount' | 'followingCount', by: number) {
272 return (ActorModel as any).increment(column, {
280 static async getActorsFollowerSharedInboxUrls (actors: ActorModel[], t: Sequelize.Transaction) {
285 [Sequelize.Op.in]: actors.map(a => a.id)
291 model: ActorFollowModel.unscoped(),
293 as: 'ActorFollowers',
299 attributes: [ 'sharedInboxUrl' ],
300 model: ActorModel.unscoped(),
310 const hash: { [ id: number ]: string[] } = {}
311 const res = await ActorModel.findAll(query)
312 for (const actor of res) {
313 hash[actor.id] = actor.ActorFollowers.map(follow => follow.ActorFollower.sharedInboxUrl)
320 let avatar: Avatar = null
322 avatar = this.Avatar.toFormattedJSON()
329 name: this.preferredUsername,
330 host: this.getHost(),
331 followingCount: this.followingCount,
332 followersCount: this.followersCount,
334 createdAt: this.createdAt,
335 updatedAt: this.updatedAt
339 toActivityPubObject (name: string, type: 'Account' | 'Application' | 'VideoChannel') {
341 if (type === 'Account') {
342 activityPubType = 'Person' as 'Person'
343 } else if (type === 'Application') {
344 activityPubType = 'Application' as 'Application'
345 } else { // VideoChannel
346 activityPubType = 'Group' as 'Group'
351 const extension = extname(this.Avatar.filename)
354 mediaType: extension === '.png' ? 'image/png' : 'image/jpeg',
355 url: this.getAvatarUrl()
360 type: activityPubType,
362 following: this.getFollowingUrl(),
363 followers: this.getFollowersUrl(),
364 inbox: this.inboxUrl,
365 outbox: this.outboxUrl,
366 preferredUsername: this.preferredUsername,
370 sharedInbox: this.sharedInboxUrl
374 id: this.getPublicKeyUrl(),
376 publicKeyPem: this.publicKey
381 return activityPubContextify(json)
384 getFollowerSharedInboxUrls (t: Sequelize.Transaction) {
386 attributes: [ 'sharedInboxUrl' ],
390 model: ActorFollowModel.unscoped(),
392 as: 'ActorFollowing',
395 targetActorId: this.id
402 return ActorModel.findAll(query)
403 .then(accounts => accounts.map(a => a.sharedInboxUrl))
407 return this.url + '/following'
411 return this.url + '/followers'
415 return this.url + '#main-key'
419 return this.serverId === null
423 return 'acct:' + this.preferredUsername + '@' + this.getHost()
427 return this.Server ? this.Server.host : CONFIG.WEBSERVER.HOST
431 if (!this.avatarId) return undefined
433 return CONFIG.WEBSERVER.URL + this.Avatar.getWebserverPath()
437 if (this.isOwned()) return false
439 const now = Date.now()
440 const createdAtTime = this.createdAt.getTime()
441 const updatedAtTime = this.updatedAt.getTime()
443 return (now - createdAtTime) > ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL &&
444 (now - updatedAtTime) > ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL