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'
40 import { VideoModel } from '../video/video'
46 export const unusedActorAttributesForAPI = [
62 model: () => ServerModel,
66 model: () => AvatarModel,
75 model: () => AccountModel.unscoped(),
79 model: () => VideoChannelModel.unscoped(),
83 model: () => AccountModel,
89 model: () => ServerModel,
93 model: () => AvatarModel,
107 fields: [ 'preferredUsername', 'serverId' ],
111 fields: [ 'inboxUrl', 'sharedInboxUrl' ]
114 fields: [ 'sharedInboxUrl' ]
117 fields: [ 'serverId' ]
120 fields: [ 'avatarId' ]
127 fields: [ 'followersUrl' ]
131 export class ActorModel extends Model<ActorModel> {
134 @Column(DataType.ENUM(values(ACTIVITY_PUB_ACTOR_TYPES)))
135 type: ActivityPubActorType
138 @Default(DataType.UUIDV4)
140 @Column(DataType.UUID)
144 @Is('ActorPreferredUsername', value => throwIfNotValid(value, isActorPreferredUsernameValid, 'actor preferred username'))
146 preferredUsername: string
149 @Is('ActorUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
150 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
154 @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPublicKeyValid, 'public key'))
155 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.PUBLIC_KEY.max))
159 @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPrivateKeyValid, 'private key'))
160 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.PRIVATE_KEY.max))
164 @Is('ActorFollowersCount', value => throwIfNotValid(value, isActorFollowersCountValid, 'followers count'))
166 followersCount: number
169 @Is('ActorFollowersCount', value => throwIfNotValid(value, isActorFollowingCountValid, 'following count'))
171 followingCount: number
174 @Is('ActorInboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'inbox url'))
175 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
179 @Is('ActorOutboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'outbox url'))
180 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
184 @Is('ActorSharedInboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'shared inbox url'))
185 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
186 sharedInboxUrl: string
189 @Is('ActorFollowersUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'followers url'))
190 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
194 @Is('ActorFollowingUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'following url'))
195 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
204 @ForeignKey(() => AvatarModel)
208 @BelongsTo(() => AvatarModel, {
212 onDelete: 'set null',
217 @HasMany(() => ActorFollowModel, {
222 as: 'ActorFollowings',
225 ActorFollowing: ActorFollowModel[]
227 @HasMany(() => ActorFollowModel, {
229 name: 'targetActorId',
232 as: 'ActorFollowers',
235 ActorFollowers: ActorFollowModel[]
237 @ForeignKey(() => ServerModel)
241 @BelongsTo(() => ServerModel, {
249 @HasOne(() => AccountModel, {
256 Account: AccountModel
258 @HasOne(() => VideoChannelModel, {
265 VideoChannel: VideoChannelModel
267 static load (id: number) {
268 return ActorModel.unscoped().findById(id)
271 static loadAccountActorByVideoId (videoId: number, transaction: Sequelize.Transaction) {
275 attributes: [ 'id' ],
276 model: AccountModel.unscoped(),
280 attributes: [ 'id' ],
281 model: VideoChannelModel.unscoped(),
284 attributes: [ 'id' ],
285 model: VideoModel.unscoped(),
298 return ActorModel.unscoped().findOne(query as any) // FIXME: typings
301 static isActorUrlExist (url: string) {
309 return ActorModel.unscoped().findOne(query)
313 static listByFollowersUrls (followersUrls: string[], transaction?: Sequelize.Transaction) {
317 [ Sequelize.Op.in ]: followersUrls
323 return ActorModel.scope(ScopeNames.FULL).findAll(query)
326 static loadLocalByName (preferredUsername: string, transaction?: Sequelize.Transaction) {
335 return ActorModel.scope(ScopeNames.FULL).findOne(query)
338 static loadByNameAndHost (preferredUsername: string, host: string) {
354 return ActorModel.scope(ScopeNames.FULL).findOne(query)
357 static loadByUrl (url: string, transaction?: Sequelize.Transaction) {
365 attributes: [ 'id' ],
366 model: AccountModel.unscoped(),
370 attributes: [ 'id' ],
371 model: VideoChannelModel.unscoped(),
377 return ActorModel.unscoped().findOne(query)
380 static loadByUrlAndPopulateAccountAndChannel (url: string, transaction?: Sequelize.Transaction) {
388 return ActorModel.scope(ScopeNames.FULL).findOne(query)
391 static incrementFollows (id: number, column: 'followersCount' | 'followingCount', by: number) {
393 return (ActorModel as any).increment(column, {
402 let avatar: Avatar = null
404 avatar = this.Avatar.toFormattedJSON()
411 name: this.preferredUsername,
412 host: this.getHost(),
413 hostRedundancyAllowed: this.getRedundancyAllowed(),
414 followingCount: this.followingCount,
415 followersCount: this.followersCount,
417 createdAt: this.createdAt,
418 updatedAt: this.updatedAt
422 toActivityPubObject (name: string, type: 'Account' | 'Application' | 'VideoChannel') {
424 if (type === 'Account') {
425 activityPubType = 'Person' as 'Person'
426 } else if (type === 'Application') {
427 activityPubType = 'Application' as 'Application'
428 } else { // VideoChannel
429 activityPubType = 'Group' as 'Group'
434 const extension = extname(this.Avatar.filename)
437 mediaType: extension === '.png' ? 'image/png' : 'image/jpeg',
438 url: this.getAvatarUrl()
443 type: activityPubType,
445 following: this.getFollowingUrl(),
446 followers: this.getFollowersUrl(),
447 inbox: this.inboxUrl,
448 outbox: this.outboxUrl,
449 preferredUsername: this.preferredUsername,
453 sharedInbox: this.sharedInboxUrl
457 id: this.getPublicKeyUrl(),
459 publicKeyPem: this.publicKey
464 return activityPubContextify(json)
467 getFollowerSharedInboxUrls (t: Sequelize.Transaction) {
469 attributes: [ 'sharedInboxUrl' ],
473 model: ActorFollowModel.unscoped(),
475 as: 'ActorFollowing',
478 targetActorId: this.id
485 return ActorModel.findAll(query)
486 .then(accounts => accounts.map(a => a.sharedInboxUrl))
490 return this.url + '/following'
494 return this.url + '/followers'
498 return this.url + '#main-key'
502 return this.serverId === null
506 return 'acct:' + this.preferredUsername + '@' + this.getHost()
510 return this.Server ? `${this.preferredUsername}@${this.Server.host}` : this.preferredUsername
514 return this.Server ? this.Server.host : CONFIG.WEBSERVER.HOST
517 getRedundancyAllowed () {
518 return this.Server ? this.Server.redundancyAllowed : false
522 if (!this.avatarId) return undefined
524 return CONFIG.WEBSERVER.URL + this.Avatar.getWebserverPath()
528 if (this.isOwned()) return false
530 const now = Date.now()
531 const createdAtTime = this.createdAt.getTime()
532 const updatedAtTime = this.updatedAt.getTime()
534 return (now - createdAtTime) > ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL &&
535 (now - updatedAtTime) > ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL