1 import { values } from 'lodash'
2 import { extname } from 'path'
3 import * as Sequelize from 'sequelize'
19 } from 'sequelize-typescript'
20 import { ActivityPubActorType } from '../../../shared/models/activitypub'
21 import { Avatar } from '../../../shared/models/avatars/avatar.model'
22 import { activityPubContextify } from '../../helpers/activitypub'
24 isActorFollowersCountValid,
25 isActorFollowingCountValid,
26 isActorPreferredUsernameValid,
27 isActorPrivateKeyValid,
29 } from '../../helpers/custom-validators/activitypub/actor'
30 import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
31 import { ACTIVITY_PUB, ACTIVITY_PUB_ACTOR_TYPES, CONSTRAINTS_FIELDS, WEBSERVER } from '../../initializers/constants'
32 import { AccountModel } from '../account/account'
33 import { AvatarModel } from '../avatar/avatar'
34 import { ServerModel } from '../server/server'
35 import { isOutdated, throwIfNotValid } from '../utils'
36 import { VideoChannelModel } from '../video/video-channel'
37 import { ActorFollowModel } from './actor-follow'
38 import { VideoModel } from '../video/video'
41 MActorAccountChannelId,
46 MActorRedundancyAllowedOpt,
48 MActorSummaryFormattable
49 } from '../../typings/models'
50 import * as Bluebird from 'bluebird'
56 export const unusedActorAttributesForAPI = [
69 @DefaultScope(() => ({
85 model: AccountModel.unscoped(),
89 model: VideoChannelModel.unscoped(),
117 fields: [ 'preferredUsername', 'serverId' ],
121 fields: [ 'inboxUrl', 'sharedInboxUrl' ]
124 fields: [ 'sharedInboxUrl' ]
127 fields: [ 'serverId' ]
130 fields: [ 'avatarId' ]
133 fields: [ 'followersUrl' ]
137 export class ActorModel extends Model<ActorModel> {
140 @Column(DataType.ENUM(...values(ACTIVITY_PUB_ACTOR_TYPES)))
141 type: ActivityPubActorType
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', true))
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', true))
190 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
194 @Is('ActorFollowingUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'following url', true))
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): Bluebird<MActor> {
268 return ActorModel.unscoped().findByPk(id)
271 static loadFull (id: number): Bluebird<MActorFull> {
272 return ActorModel.scope(ScopeNames.FULL).findByPk(id)
275 static loadFromAccountByVideoId (videoId: number, transaction: Sequelize.Transaction): Bluebird<MActor> {
279 attributes: [ 'id' ],
280 model: AccountModel.unscoped(),
284 attributes: [ 'id' ],
285 model: VideoChannelModel.unscoped(),
289 attributes: [ 'id' ],
290 model: VideoModel.unscoped(),
304 return ActorModel.unscoped().findOne(query)
307 static isActorUrlExist (url: string) {
315 return ActorModel.unscoped().findOne(query)
319 static listByFollowersUrls (followersUrls: string[], transaction?: Sequelize.Transaction): Bluebird<MActorFull[]> {
323 [ Sequelize.Op.in ]: followersUrls
329 return ActorModel.scope(ScopeNames.FULL).findAll(query)
332 static loadLocalByName (preferredUsername: string, transaction?: Sequelize.Transaction): Bluebird<MActorFull> {
341 return ActorModel.scope(ScopeNames.FULL).findOne(query)
344 static loadByNameAndHost (preferredUsername: string, host: string): Bluebird<MActorFull> {
360 return ActorModel.scope(ScopeNames.FULL).findOne(query)
363 static loadByUrl (url: string, transaction?: Sequelize.Transaction): Bluebird<MActorAccountChannelId> {
371 attributes: [ 'id' ],
372 model: AccountModel.unscoped(),
376 attributes: [ 'id' ],
377 model: VideoChannelModel.unscoped(),
383 return ActorModel.unscoped().findOne(query)
386 static loadByUrlAndPopulateAccountAndChannel (url: string, transaction?: Sequelize.Transaction): Bluebird<MActorFull> {
394 return ActorModel.scope(ScopeNames.FULL).findOne(query)
397 static incrementFollows (id: number, column: 'followersCount' | 'followingCount', by: number) {
398 return ActorModel.increment(column, {
406 toFormattedSummaryJSON (this: MActorSummaryFormattable) {
407 let avatar: Avatar = null
409 avatar = this.Avatar.toFormattedJSON()
414 name: this.preferredUsername,
415 host: this.getHost(),
420 toFormattedJSON (this: MActorFormattable) {
421 const base = this.toFormattedSummaryJSON()
423 return Object.assign(base, {
425 hostRedundancyAllowed: this.getRedundancyAllowed(),
426 followingCount: this.followingCount,
427 followersCount: this.followersCount,
428 createdAt: this.createdAt,
429 updatedAt: this.updatedAt
433 toActivityPubObject (this: MActorAP, name: string, type: 'Account' | 'Application' | 'VideoChannel') {
435 if (type === 'Account') {
436 activityPubType = 'Person' as 'Person'
437 } else if (type === 'Application') {
438 activityPubType = 'Application' as 'Application'
439 } else { // VideoChannel
440 activityPubType = 'Group' as 'Group'
445 const extension = extname(this.Avatar.filename)
448 mediaType: extension === '.png' ? 'image/png' : 'image/jpeg',
449 url: this.getAvatarUrl()
454 type: activityPubType,
456 following: this.getFollowingUrl(),
457 followers: this.getFollowersUrl(),
458 playlists: this.getPlaylistsUrl(),
459 inbox: this.inboxUrl,
460 outbox: this.outboxUrl,
461 preferredUsername: this.preferredUsername,
465 sharedInbox: this.sharedInboxUrl
468 id: this.getPublicKeyUrl(),
470 publicKeyPem: this.publicKey
475 return activityPubContextify(json)
478 getFollowerSharedInboxUrls (t: Sequelize.Transaction) {
480 attributes: [ 'sharedInboxUrl' ],
484 model: ActorFollowModel.unscoped(),
486 as: 'ActorFollowing',
489 targetActorId: this.id
496 return ActorModel.findAll(query)
497 .then(accounts => accounts.map(a => a.sharedInboxUrl))
501 return this.url + '/following'
505 return this.url + '/followers'
509 return this.url + '/playlists'
513 return this.url + '#main-key'
517 return this.serverId === null
520 getWebfingerUrl (this: MActorServer) {
521 return 'acct:' + this.preferredUsername + '@' + this.getHost()
525 return this.Server ? `${this.preferredUsername}@${this.Server.host}` : this.preferredUsername
528 getHost (this: MActorHost) {
529 return this.Server ? this.Server.host : WEBSERVER.HOST
532 getRedundancyAllowed () {
533 return this.Server ? this.Server.redundancyAllowed : false
537 if (!this.avatarId) return undefined
539 return WEBSERVER.URL + this.Avatar.getStaticPath()
543 if (this.isOwned()) return false
545 return isOutdated(this, ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL)