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,
48 model: () => VideoChannelModel,
52 model: () => ServerModel,
56 model: () => AvatarModel,
66 fields: [ 'preferredUsername', 'serverId' ],
71 export class ActorModel extends Model<ActorModel> {
74 @Column(DataType.ENUM(values(ACTIVITY_PUB_ACTOR_TYPES)))
75 type: ActivityPubActorType
78 @Default(DataType.UUIDV4)
80 @Column(DataType.UUID)
84 @Is('ActorPreferredUsername', value => throwIfNotValid(value, isActorPreferredUsernameValid, 'actor preferred username'))
86 preferredUsername: string
89 @Is('ActorUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
90 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
94 @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPublicKeyValid, 'public key'))
95 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.PUBLIC_KEY.max))
99 @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPrivateKeyValid, 'private key'))
100 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.PRIVATE_KEY.max))
104 @Is('ActorFollowersCount', value => throwIfNotValid(value, isActorFollowersCountValid, 'followers count'))
106 followersCount: number
109 @Is('ActorFollowersCount', value => throwIfNotValid(value, isActorFollowingCountValid, 'following count'))
111 followingCount: number
114 @Is('ActorInboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'inbox url'))
115 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
119 @Is('ActorOutboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'outbox url'))
120 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
124 @Is('ActorSharedInboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'shared inbox url'))
125 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
126 sharedInboxUrl: string
129 @Is('ActorFollowersUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'followers url'))
130 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
134 @Is('ActorFollowingUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'following url'))
135 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
144 @ForeignKey(() => AvatarModel)
148 @BelongsTo(() => AvatarModel, {
156 @HasMany(() => ActorFollowModel, {
163 AccountFollowing: ActorFollowModel[]
165 @HasMany(() => ActorFollowModel, {
167 name: 'targetActorId',
173 AccountFollowers: ActorFollowModel[]
175 @ForeignKey(() => ServerModel)
179 @BelongsTo(() => ServerModel, {
187 @HasOne(() => AccountModel, {
193 Account: AccountModel
195 @HasOne(() => VideoChannelModel, {
201 VideoChannel: VideoChannelModel
203 static load (id: number) {
204 return ActorModel.scope(ScopeNames.FULL).findById(id)
207 static listByFollowersUrls (followersUrls: string[], transaction?: Sequelize.Transaction) {
211 [ Sequelize.Op.in ]: followersUrls
217 return ActorModel.scope(ScopeNames.FULL).findAll(query)
220 static loadLocalByName (preferredUsername: string) {
228 return ActorModel.scope(ScopeNames.FULL).findOne(query)
231 static loadByNameAndHost (preferredUsername: string, host: string) {
247 return ActorModel.scope(ScopeNames.FULL).findOne(query)
250 static loadByUrl (url: string, transaction?: Sequelize.Transaction) {
258 return ActorModel.scope(ScopeNames.FULL).findOne(query)
262 let avatar: Avatar = null
264 avatar = this.Avatar.toFormattedJSON()
269 score = this.Server.score
276 host: this.getHost(),
278 followingCount: this.followingCount,
279 followersCount: this.followersCount,
284 toActivityPubObject (name: string, type: 'Account' | 'Application' | 'VideoChannel') {
286 if (type === 'Account') {
287 activityPubType = 'Person' as 'Person'
288 } else if (type === 'Application') {
289 activityPubType = 'Application' as 'Application'
290 } else { // VideoChannel
291 activityPubType = 'Group' as 'Group'
296 const extension = extname(this.Avatar.filename)
299 mediaType: extension === '.png' ? 'image/png' : 'image/jpeg',
300 url: this.getAvatarUrl()
305 type: activityPubType,
307 following: this.getFollowingUrl(),
308 followers: this.getFollowersUrl(),
309 inbox: this.inboxUrl,
310 outbox: this.outboxUrl,
311 preferredUsername: this.preferredUsername,
315 sharedInbox: this.sharedInboxUrl
319 id: this.getPublicKeyUrl(),
321 publicKeyPem: this.publicKey
326 return activityPubContextify(json)
329 getFollowerSharedInboxUrls (t: Sequelize.Transaction) {
331 attributes: [ 'sharedInboxUrl' ],
334 model: ActorFollowModel,
338 targetActorId: this.id
345 return ActorModel.findAll(query)
346 .then(accounts => accounts.map(a => a.sharedInboxUrl))
350 return this.url + '/following'
354 return this.url + '/followers'
358 return this.url + '#main-key'
362 return this.serverId === null
366 return 'acct:' + this.preferredUsername + '@' + this.getHost()
370 return this.Server ? this.Server.host : CONFIG.WEBSERVER.HOST
374 if (!this.avatarId) return undefined
376 return CONFIG.WEBSERVER.URL + this.Avatar.getWebserverPath()
380 if (this.isOwned()) return false
382 const now = Date.now()
383 const createdAtTime = this.createdAt.getTime()
384 const updatedAtTime = this.updatedAt.getTime()
386 return (now - createdAtTime) > ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL &&
387 (now - updatedAtTime) > ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL