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, CONSTRAINTS_FIELDS, WEBSERVER } from '../../initializers/constants'
34 import { AccountModel } from '../account/account'
35 import { AvatarModel } from '../avatar/avatar'
36 import { ServerModel } from '../server/server'
37 import { isOutdated, 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 = [
59 @DefaultScope(() => ({
75 model: AccountModel.unscoped(),
79 model: VideoChannelModel.unscoped(),
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', true))
155 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.PUBLIC_KEY.max))
159 @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPrivateKeyValid, 'private key', true))
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().findByPk(id)
271 static loadAccountActorByVideoId (videoId: number, transaction: Sequelize.Transaction) {
275 attributes: [ 'id' ],
276 model: AccountModel.unscoped(),
280 attributes: [ 'id' ],
281 model: VideoChannelModel.unscoped(),
285 attributes: [ 'id' ],
286 model: VideoModel.unscoped(),
300 return ActorModel.unscoped().findOne(query)
303 static isActorUrlExist (url: string) {
311 return ActorModel.unscoped().findOne(query)
315 static listByFollowersUrls (followersUrls: string[], transaction?: Sequelize.Transaction) {
319 [ Sequelize.Op.in ]: followersUrls
325 return ActorModel.scope(ScopeNames.FULL).findAll(query)
328 static loadLocalByName (preferredUsername: string, transaction?: Sequelize.Transaction) {
337 return ActorModel.scope(ScopeNames.FULL).findOne(query)
340 static loadByNameAndHost (preferredUsername: string, host: string) {
356 return ActorModel.scope(ScopeNames.FULL).findOne(query)
359 static loadByUrl (url: string, transaction?: Sequelize.Transaction) {
367 attributes: [ 'id' ],
368 model: AccountModel.unscoped(),
372 attributes: [ 'id' ],
373 model: VideoChannelModel.unscoped(),
379 return ActorModel.unscoped().findOne(query)
382 static loadByUrlAndPopulateAccountAndChannel (url: string, transaction?: Sequelize.Transaction) {
390 return ActorModel.scope(ScopeNames.FULL).findOne(query)
393 static incrementFollows (id: number, column: 'followersCount' | 'followingCount', by: number) {
394 return ActorModel.increment(column, {
403 let avatar: Avatar = null
405 avatar = this.Avatar.toFormattedJSON()
412 name: this.preferredUsername,
413 host: this.getHost(),
414 hostRedundancyAllowed: this.getRedundancyAllowed(),
415 followingCount: this.followingCount,
416 followersCount: this.followersCount,
418 createdAt: this.createdAt,
419 updatedAt: this.updatedAt
423 toActivityPubObject (name: string, type: 'Account' | 'Application' | 'VideoChannel') {
425 if (type === 'Account') {
426 activityPubType = 'Person' as 'Person'
427 } else if (type === 'Application') {
428 activityPubType = 'Application' as 'Application'
429 } else { // VideoChannel
430 activityPubType = 'Group' as 'Group'
435 const extension = extname(this.Avatar.filename)
438 mediaType: extension === '.png' ? 'image/png' : 'image/jpeg',
439 url: this.getAvatarUrl()
444 type: activityPubType,
446 following: this.getFollowingUrl(),
447 followers: this.getFollowersUrl(),
448 playlists: this.getPlaylistsUrl(),
449 inbox: this.inboxUrl,
450 outbox: this.outboxUrl,
451 preferredUsername: this.preferredUsername,
455 sharedInbox: this.sharedInboxUrl
459 id: this.getPublicKeyUrl(),
461 publicKeyPem: this.publicKey
466 return activityPubContextify(json)
469 getFollowerSharedInboxUrls (t: Sequelize.Transaction) {
471 attributes: [ 'sharedInboxUrl' ],
475 model: ActorFollowModel.unscoped(),
477 as: 'ActorFollowing',
480 targetActorId: this.id
487 return ActorModel.findAll(query)
488 .then(accounts => accounts.map(a => a.sharedInboxUrl))
492 return this.url + '/following'
496 return this.url + '/followers'
500 return this.url + '/playlists'
504 return this.url + '#main-key'
508 return this.serverId === null
512 return 'acct:' + this.preferredUsername + '@' + this.getHost()
516 return this.Server ? `${this.preferredUsername}@${this.Server.host}` : this.preferredUsername
520 return this.Server ? this.Server.host : WEBSERVER.HOST
523 getRedundancyAllowed () {
524 return this.Server ? this.Server.redundancyAllowed : false
528 if (!this.avatarId) return undefined
530 return WEBSERVER.URL + this.Avatar.getWebserverPath()
534 if (this.isOwned()) return false
536 return isOutdated(this, ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL)