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'
12 isActivityPubUrlValid, isActorFollowersCountValid, isActorFollowingCountValid, isActorPreferredUsernameValid,
13 isActorPrivateKeyValid, isActorPublicKeyValid
14 } from '../../helpers/custom-validators/activitypub'
15 import { ACTIVITY_PUB_ACTOR_TYPES, AVATARS_DIR, CONFIG, CONSTRAINTS_FIELDS } from '../../initializers'
16 import { AccountModel } from '../account/account'
17 import { AvatarModel } from '../avatar/avatar'
18 import { ServerModel } from '../server/server'
19 import { throwIfNotValid } from '../utils'
20 import { VideoChannelModel } from '../video/video-channel'
21 import { ActorFollowModel } from './actor-follow'
30 model: () => ServerModel,
39 model: () => AccountModel,
43 model: () => VideoChannelModel,
47 model: () => ServerModel,
57 fields: [ 'preferredUsername', 'serverId' ],
62 export class ActorModel extends Model<ActorModel> {
65 @Column(DataType.ENUM(values(ACTIVITY_PUB_ACTOR_TYPES)))
66 type: ActivityPubActorType
69 @Default(DataType.UUIDV4)
71 @Column(DataType.UUID)
75 @Is('ActorPreferredUsername', value => throwIfNotValid(value, isActorPreferredUsernameValid, 'actor preferred username'))
77 preferredUsername: string
80 @Is('ActorUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
81 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTOR.URL.max))
85 @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPublicKeyValid, 'public key'))
86 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTOR.PUBLIC_KEY.max))
90 @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPrivateKeyValid, 'private key'))
91 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTOR.PRIVATE_KEY.max))
95 @Is('ActorFollowersCount', value => throwIfNotValid(value, isActorFollowersCountValid, 'followers count'))
97 followersCount: number
100 @Is('ActorFollowersCount', value => throwIfNotValid(value, isActorFollowingCountValid, 'following count'))
102 followingCount: number
105 @Is('ActorInboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'inbox url'))
106 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTOR.URL.max))
110 @Is('ActorOutboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'outbox url'))
111 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTOR.URL.max))
115 @Is('ActorSharedInboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'shared inbox url'))
116 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTOR.URL.max))
117 sharedInboxUrl: string
120 @Is('ActorFollowersUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'followers url'))
121 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTOR.URL.max))
125 @Is('ActorFollowingUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'following url'))
126 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTOR.URL.max))
135 @ForeignKey(() => AvatarModel)
139 @BelongsTo(() => AvatarModel, {
147 @HasMany(() => ActorFollowModel, {
154 AccountFollowing: ActorFollowModel[]
156 @HasMany(() => ActorFollowModel, {
158 name: 'targetActorId',
164 AccountFollowers: ActorFollowModel[]
166 @ForeignKey(() => ServerModel)
170 @BelongsTo(() => ServerModel, {
178 @HasOne(() => AccountModel, {
184 Account: AccountModel
186 @HasOne(() => VideoChannelModel, {
192 VideoChannel: VideoChannelModel
194 static load (id: number) {
195 return ActorModel.scope(ScopeNames.FULL).findById(id)
198 static listByFollowersUrls (followersUrls: string[], transaction?: Sequelize.Transaction) {
202 [ Sequelize.Op.in ]: followersUrls
208 return ActorModel.scope(ScopeNames.FULL).findAll(query)
211 static loadLocalByName (preferredUsername: string) {
219 return ActorModel.scope(ScopeNames.FULL).findOne(query)
222 static loadByNameAndHost (preferredUsername: string, host: string) {
238 return ActorModel.scope(ScopeNames.FULL).findOne(query)
241 static loadByUrl (url: string, transaction?: Sequelize.Transaction) {
249 return ActorModel.scope(ScopeNames.FULL).findOne(query)
253 let avatar: Avatar = null
256 path: join(AVATARS_DIR.ACCOUNT, this.Avatar.filename),
257 createdAt: this.Avatar.createdAt,
258 updatedAt: this.Avatar.updatedAt
264 score = this.Server.score
270 host: this.getHost(),
272 followingCount: this.followingCount,
273 followersCount: this.followersCount,
278 toActivityPubObject (name: string, type: 'Account' | 'Application' | 'VideoChannel') {
280 if (type === 'Account') {
281 activityPubType = 'Person' as 'Person'
282 } else if (type === 'Application') {
283 activityPubType = 'Application' as 'Application'
284 } else { // VideoChannel
285 activityPubType = 'Group' as 'Group'
289 type: activityPubType,
291 following: this.getFollowingUrl(),
292 followers: this.getFollowersUrl(),
293 inbox: this.inboxUrl,
294 outbox: this.outboxUrl,
295 preferredUsername: this.preferredUsername,
299 sharedInbox: this.sharedInboxUrl
303 id: this.getPublicKeyUrl(),
305 publicKeyPem: this.publicKey
309 return activityPubContextify(json)
312 getFollowerSharedInboxUrls (t: Sequelize.Transaction) {
314 attributes: [ 'sharedInboxUrl' ],
317 model: ActorFollowModel,
321 targetActorId: this.id
328 return ActorModel.findAll(query)
329 .then(accounts => accounts.map(a => a.sharedInboxUrl))
333 return this.url + '/following'
337 return this.url + '/followers'
341 return this.url + '#main-key'
345 return this.serverId === null
349 return 'acct:' + this.preferredUsername + '@' + this.getHost()
353 return this.Server ? this.Server.host : CONFIG.WEBSERVER.HOST