1 import { values } from 'lodash'
2 import { extname } from 'path'
3 import * as Sequelize from 'sequelize'
22 } from 'sequelize-typescript'
23 import { ActivityPubActorType } from '../../../shared/models/activitypub'
24 import { Avatar } from '../../../shared/models/avatars/avatar.model'
25 import { activityPubContextify } from '../../helpers/activitypub'
27 isActorFollowersCountValid,
28 isActorFollowingCountValid,
29 isActorPreferredUsernameValid,
30 isActorPrivateKeyValid,
32 } from '../../helpers/custom-validators/activitypub/actor'
33 import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
34 import { ACTIVITY_PUB, ACTIVITY_PUB_ACTOR_TYPES, CONFIG, CONSTRAINTS_FIELDS } from '../../initializers'
35 import { AccountModel } from '../account/account'
36 import { AvatarModel } from '../avatar/avatar'
37 import { ServerModel } from '../server/server'
38 import { throwIfNotValid } from '../utils'
39 import { VideoChannelModel } from '../video/video-channel'
40 import { ActorFollowModel } from './actor-follow'
41 import { logger } from '../../helpers/logger'
42 import { sendDeleteActor } from '../../lib/activitypub/send'
51 model: () => ServerModel,
55 model: () => AvatarModel,
64 model: () => AccountModel.unscoped(),
68 model: () => VideoChannelModel.unscoped(),
72 model: () => ServerModel,
76 model: () => AvatarModel,
90 fields: [ 'preferredUsername', 'serverId' ],
94 fields: [ 'inboxUrl', 'sharedInboxUrl' ]
97 fields: [ 'serverId' ]
100 fields: [ 'avatarId' ]
107 fields: [ 'followersUrl' ]
111 export class ActorModel extends Model<ActorModel> {
114 @Column(DataType.ENUM(values(ACTIVITY_PUB_ACTOR_TYPES)))
115 type: ActivityPubActorType
118 @Default(DataType.UUIDV4)
120 @Column(DataType.UUID)
124 @Is('ActorPreferredUsername', value => throwIfNotValid(value, isActorPreferredUsernameValid, 'actor preferred username'))
126 preferredUsername: string
129 @Is('ActorUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
130 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
134 @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPublicKeyValid, 'public key'))
135 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.PUBLIC_KEY.max))
139 @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPrivateKeyValid, 'private key'))
140 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.PRIVATE_KEY.max))
144 @Is('ActorFollowersCount', value => throwIfNotValid(value, isActorFollowersCountValid, 'followers count'))
146 followersCount: number
149 @Is('ActorFollowersCount', value => throwIfNotValid(value, isActorFollowingCountValid, 'following count'))
151 followingCount: number
154 @Is('ActorInboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'inbox url'))
155 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
159 @Is('ActorOutboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'outbox url'))
160 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
164 @Is('ActorSharedInboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'shared inbox url'))
165 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
166 sharedInboxUrl: string
169 @Is('ActorFollowersUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'followers url'))
170 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
174 @Is('ActorFollowingUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'following url'))
175 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
184 @ForeignKey(() => AvatarModel)
188 @BelongsTo(() => AvatarModel, {
192 onDelete: 'set null',
197 @HasMany(() => ActorFollowModel, {
204 ActorFollowing: ActorFollowModel[]
206 @HasMany(() => ActorFollowModel, {
208 name: 'targetActorId',
211 as: 'ActorFollowers',
214 ActorFollowers: ActorFollowModel[]
216 @ForeignKey(() => ServerModel)
220 @BelongsTo(() => ServerModel, {
228 @HasOne(() => AccountModel, {
233 Account: AccountModel
235 @HasOne(() => VideoChannelModel, {
240 VideoChannel: VideoChannelModel
243 static async sendDeleteIfOwned (instance: ActorModel, options) {
244 if (instance.isOwned()) {
245 logger.debug('Sending delete of actor %s.', instance.url)
246 return sendDeleteActor(instance, options.transaction)
252 static load (id: number) {
253 return ActorModel.unscoped().findById(id)
256 static listByFollowersUrls (followersUrls: string[], transaction?: Sequelize.Transaction) {
260 [ Sequelize.Op.in ]: followersUrls
266 return ActorModel.scope(ScopeNames.FULL).findAll(query)
269 static loadLocalByName (preferredUsername: string) {
277 return ActorModel.scope(ScopeNames.FULL).findOne(query)
280 static loadByNameAndHost (preferredUsername: string, host: string) {
296 return ActorModel.scope(ScopeNames.FULL).findOne(query)
299 static loadByUrl (url: string, transaction?: Sequelize.Transaction) {
307 return ActorModel.scope(ScopeNames.FULL).findOne(query)
310 static incrementFollows (id: number, column: 'followersCount' | 'followingCount', by: number) {
312 return (ActorModel as any).increment(column, {
320 static async getActorsFollowerSharedInboxUrls (actors: ActorModel[], t: Sequelize.Transaction) {
325 [Sequelize.Op.in]: actors.map(a => a.id)
331 model: ActorFollowModel.unscoped(),
333 as: 'ActorFollowers',
339 attributes: [ 'sharedInboxUrl' ],
340 model: ActorModel.unscoped(),
350 const hash: { [ id: number ]: string[] } = {}
351 const res = await ActorModel.findAll(query)
352 for (const actor of res) {
353 hash[actor.id] = actor.ActorFollowers.map(follow => follow.ActorFollower.sharedInboxUrl)
360 let avatar: Avatar = null
362 avatar = this.Avatar.toFormattedJSON()
369 name: this.preferredUsername,
370 host: this.getHost(),
371 followingCount: this.followingCount,
372 followersCount: this.followersCount,
374 createdAt: this.createdAt,
375 updatedAt: this.updatedAt
379 toActivityPubObject (name: string, type: 'Account' | 'Application' | 'VideoChannel') {
381 if (type === 'Account') {
382 activityPubType = 'Person' as 'Person'
383 } else if (type === 'Application') {
384 activityPubType = 'Application' as 'Application'
385 } else { // VideoChannel
386 activityPubType = 'Group' as 'Group'
391 const extension = extname(this.Avatar.filename)
394 mediaType: extension === '.png' ? 'image/png' : 'image/jpeg',
395 url: this.getAvatarUrl()
400 type: activityPubType,
402 following: this.getFollowingUrl(),
403 followers: this.getFollowersUrl(),
404 inbox: this.inboxUrl,
405 outbox: this.outboxUrl,
406 preferredUsername: this.preferredUsername,
410 sharedInbox: this.sharedInboxUrl
414 id: this.getPublicKeyUrl(),
416 publicKeyPem: this.publicKey
421 return activityPubContextify(json)
424 getFollowerSharedInboxUrls (t: Sequelize.Transaction) {
426 attributes: [ 'sharedInboxUrl' ],
430 model: ActorFollowModel.unscoped(),
432 as: 'ActorFollowing',
435 targetActorId: this.id
442 return ActorModel.findAll(query)
443 .then(accounts => accounts.map(a => a.sharedInboxUrl))
447 return this.url + '/following'
451 return this.url + '/followers'
455 return this.url + '#main-key'
459 return this.serverId === null
463 return 'acct:' + this.preferredUsername + '@' + this.getHost()
467 return this.Server ? this.Server.host : CONFIG.WEBSERVER.HOST
471 if (!this.avatarId) return undefined
473 return CONFIG.WEBSERVER.URL + this.Avatar.getWebserverPath()
477 if (this.isOwned()) return false
479 const now = Date.now()
480 const createdAtTime = this.createdAt.getTime()
481 const updatedAtTime = this.updatedAt.getTime()
483 return (now - createdAtTime) > ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL &&
484 (now - updatedAtTime) > ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL