1 import { values } from 'lodash'
2 import { join } 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_ACTOR_TYPES, AVATARS_DIR, 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,
40 model: () => AccountModel,
44 model: () => VideoChannelModel,
48 model: () => ServerModel,
58 fields: [ 'preferredUsername', 'serverId' ],
63 export class ActorModel extends Model<ActorModel> {
66 @Column(DataType.ENUM(values(ACTIVITY_PUB_ACTOR_TYPES)))
67 type: ActivityPubActorType
70 @Default(DataType.UUIDV4)
72 @Column(DataType.UUID)
76 @Is('ActorPreferredUsername', value => throwIfNotValid(value, isActorPreferredUsernameValid, 'actor preferred username'))
78 preferredUsername: string
81 @Is('ActorUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
82 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTOR.URL.max))
86 @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPublicKeyValid, 'public key'))
87 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTOR.PUBLIC_KEY.max))
91 @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPrivateKeyValid, 'private key'))
92 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTOR.PRIVATE_KEY.max))
96 @Is('ActorFollowersCount', value => throwIfNotValid(value, isActorFollowersCountValid, 'followers count'))
98 followersCount: number
101 @Is('ActorFollowersCount', value => throwIfNotValid(value, isActorFollowingCountValid, 'following count'))
103 followingCount: number
106 @Is('ActorInboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'inbox url'))
107 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTOR.URL.max))
111 @Is('ActorOutboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'outbox url'))
112 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTOR.URL.max))
116 @Is('ActorSharedInboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'shared inbox url'))
117 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTOR.URL.max))
118 sharedInboxUrl: string
121 @Is('ActorFollowersUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'followers url'))
122 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTOR.URL.max))
126 @Is('ActorFollowingUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'following url'))
127 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTOR.URL.max))
136 @ForeignKey(() => AvatarModel)
140 @BelongsTo(() => AvatarModel, {
148 @HasMany(() => ActorFollowModel, {
155 AccountFollowing: ActorFollowModel[]
157 @HasMany(() => ActorFollowModel, {
159 name: 'targetActorId',
165 AccountFollowers: ActorFollowModel[]
167 @ForeignKey(() => ServerModel)
171 @BelongsTo(() => ServerModel, {
179 @HasOne(() => AccountModel, {
185 Account: AccountModel
187 @HasOne(() => VideoChannelModel, {
193 VideoChannel: VideoChannelModel
195 static load (id: number) {
196 return ActorModel.scope(ScopeNames.FULL).findById(id)
199 static listByFollowersUrls (followersUrls: string[], transaction?: Sequelize.Transaction) {
203 [ Sequelize.Op.in ]: followersUrls
209 return ActorModel.scope(ScopeNames.FULL).findAll(query)
212 static loadLocalByName (preferredUsername: string) {
220 return ActorModel.scope(ScopeNames.FULL).findOne(query)
223 static loadByNameAndHost (preferredUsername: string, host: string) {
239 return ActorModel.scope(ScopeNames.FULL).findOne(query)
242 static loadByUrl (url: string, transaction?: Sequelize.Transaction) {
250 return ActorModel.scope(ScopeNames.FULL).findOne(query)
254 let avatar: Avatar = null
257 path: join(AVATARS_DIR.ACCOUNT, this.Avatar.filename),
258 createdAt: this.Avatar.createdAt,
259 updatedAt: this.Avatar.updatedAt
265 score = this.Server.score
271 host: this.getHost(),
273 followingCount: this.followingCount,
274 followersCount: this.followersCount,
279 toActivityPubObject (name: string, type: 'Account' | 'Application' | 'VideoChannel') {
281 if (type === 'Account') {
282 activityPubType = 'Person' as 'Person'
283 } else if (type === 'Application') {
284 activityPubType = 'Application' as 'Application'
285 } else { // VideoChannel
286 activityPubType = 'Group' as 'Group'
290 type: activityPubType,
292 following: this.getFollowingUrl(),
293 followers: this.getFollowersUrl(),
294 inbox: this.inboxUrl,
295 outbox: this.outboxUrl,
296 preferredUsername: this.preferredUsername,
300 sharedInbox: this.sharedInboxUrl
304 id: this.getPublicKeyUrl(),
306 publicKeyPem: this.publicKey
310 return activityPubContextify(json)
313 getFollowerSharedInboxUrls (t: Sequelize.Transaction) {
315 attributes: [ 'sharedInboxUrl' ],
318 model: ActorFollowModel,
322 targetActorId: this.id
329 return ActorModel.findAll(query)
330 .then(accounts => accounts.map(a => a.sharedInboxUrl))
334 return this.url + '/following'
338 return this.url + '/followers'
342 return this.url + '#main-key'
346 return this.serverId === null
350 return 'acct:' + this.preferredUsername + '@' + this.getHost()
354 return this.Server ? this.Server.host : CONFIG.WEBSERVER.HOST