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,
43 MActorFull, MActorHost,
45 MActorSummaryFormattable,
47 MActorRedundancyAllowed
48 } from '../../typings/models'
49 import * as Bluebird from 'bluebird'
55 export const unusedActorAttributesForAPI = [
68 @DefaultScope(() => ({
84 model: AccountModel.unscoped(),
88 model: VideoChannelModel.unscoped(),
116 fields: [ 'preferredUsername', 'serverId' ],
120 fields: [ 'inboxUrl', 'sharedInboxUrl' ]
123 fields: [ 'sharedInboxUrl' ]
126 fields: [ 'serverId' ]
129 fields: [ 'avatarId' ]
132 fields: [ 'followersUrl' ]
136 export class ActorModel extends Model<ActorModel> {
139 @Column(DataType.ENUM(...values(ACTIVITY_PUB_ACTOR_TYPES)))
140 type: ActivityPubActorType
143 @Is('ActorPreferredUsername', value => throwIfNotValid(value, isActorPreferredUsernameValid, 'actor preferred username'))
145 preferredUsername: string
148 @Is('ActorUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
149 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
153 @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPublicKeyValid, 'public key', true))
154 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.PUBLIC_KEY.max))
158 @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPrivateKeyValid, 'private key', true))
159 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.PRIVATE_KEY.max))
163 @Is('ActorFollowersCount', value => throwIfNotValid(value, isActorFollowersCountValid, 'followers count'))
165 followersCount: number
168 @Is('ActorFollowersCount', value => throwIfNotValid(value, isActorFollowingCountValid, 'following count'))
170 followingCount: number
173 @Is('ActorInboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'inbox url'))
174 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
178 @Is('ActorOutboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'outbox url'))
179 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
183 @Is('ActorSharedInboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'shared inbox url'))
184 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
185 sharedInboxUrl: string
188 @Is('ActorFollowersUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'followers url'))
189 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
193 @Is('ActorFollowingUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'following url'))
194 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
203 @ForeignKey(() => AvatarModel)
207 @BelongsTo(() => AvatarModel, {
211 onDelete: 'set null',
216 @HasMany(() => ActorFollowModel, {
221 as: 'ActorFollowings',
224 ActorFollowing: ActorFollowModel[]
226 @HasMany(() => ActorFollowModel, {
228 name: 'targetActorId',
231 as: 'ActorFollowers',
234 ActorFollowers: ActorFollowModel[]
236 @ForeignKey(() => ServerModel)
240 @BelongsTo(() => ServerModel, {
248 @HasOne(() => AccountModel, {
255 Account: AccountModel
257 @HasOne(() => VideoChannelModel, {
264 VideoChannel: VideoChannelModel
266 static load (id: number): Bluebird<MActor> {
267 return ActorModel.unscoped().findByPk(id)
270 static loadFull (id: number): Bluebird<MActorFull> {
271 return ActorModel.scope(ScopeNames.FULL).findByPk(id)
274 static loadFromAccountByVideoId (videoId: number, transaction: Sequelize.Transaction): Bluebird<MActor> {
278 attributes: [ 'id' ],
279 model: AccountModel.unscoped(),
283 attributes: [ 'id' ],
284 model: VideoChannelModel.unscoped(),
288 attributes: [ 'id' ],
289 model: VideoModel.unscoped(),
303 return ActorModel.unscoped().findOne(query)
306 static isActorUrlExist (url: string) {
314 return ActorModel.unscoped().findOne(query)
318 static listByFollowersUrls (followersUrls: string[], transaction?: Sequelize.Transaction): Bluebird<MActorFull[]> {
322 [ Sequelize.Op.in ]: followersUrls
328 return ActorModel.scope(ScopeNames.FULL).findAll(query)
331 static loadLocalByName (preferredUsername: string, transaction?: Sequelize.Transaction): Bluebird<MActorFull> {
340 return ActorModel.scope(ScopeNames.FULL).findOne(query)
343 static loadByNameAndHost (preferredUsername: string, host: string): Bluebird<MActorFull> {
359 return ActorModel.scope(ScopeNames.FULL).findOne(query)
362 static loadByUrl (url: string, transaction?: Sequelize.Transaction): Bluebird<MActorAccountChannelId> {
370 attributes: [ 'id' ],
371 model: AccountModel.unscoped(),
375 attributes: [ 'id' ],
376 model: VideoChannelModel.unscoped(),
382 return ActorModel.unscoped().findOne(query)
385 static loadByUrlAndPopulateAccountAndChannel (url: string, transaction?: Sequelize.Transaction): Bluebird<MActorFull> {
393 return ActorModel.scope(ScopeNames.FULL).findOne(query)
396 static incrementFollows (id: number, column: 'followersCount' | 'followingCount', by: number) {
397 return ActorModel.increment(column, {
405 toFormattedSummaryJSON (this: MActorSummaryFormattable) {
406 let avatar: Avatar = null
408 avatar = this.Avatar.toFormattedJSON()
413 name: this.preferredUsername,
414 host: this.getHost(),
419 toFormattedJSON (this: MActorFormattable) {
420 const base = this.toFormattedSummaryJSON()
422 return Object.assign(base, {
424 hostRedundancyAllowed: this.getRedundancyAllowed(),
425 followingCount: this.followingCount,
426 followersCount: this.followersCount,
427 createdAt: this.createdAt,
428 updatedAt: this.updatedAt
432 toActivityPubObject (name: string, type: 'Account' | 'Application' | 'VideoChannel') {
434 if (type === 'Account') {
435 activityPubType = 'Person' as 'Person'
436 } else if (type === 'Application') {
437 activityPubType = 'Application' as 'Application'
438 } else { // VideoChannel
439 activityPubType = 'Group' as 'Group'
444 const extension = extname(this.Avatar.filename)
447 mediaType: extension === '.png' ? 'image/png' : 'image/jpeg',
448 url: this.getAvatarUrl()
453 type: activityPubType,
455 following: this.getFollowingUrl(),
456 followers: this.getFollowersUrl(),
457 playlists: this.getPlaylistsUrl(),
458 inbox: this.inboxUrl,
459 outbox: this.outboxUrl,
460 preferredUsername: this.preferredUsername,
464 sharedInbox: this.sharedInboxUrl
467 id: this.getPublicKeyUrl(),
469 publicKeyPem: this.publicKey
474 return activityPubContextify(json)
477 getFollowerSharedInboxUrls (t: Sequelize.Transaction) {
479 attributes: [ 'sharedInboxUrl' ],
483 model: ActorFollowModel.unscoped(),
485 as: 'ActorFollowing',
488 targetActorId: this.id
495 return ActorModel.findAll(query)
496 .then(accounts => accounts.map(a => a.sharedInboxUrl))
500 return this.url + '/following'
504 return this.url + '/followers'
508 return this.url + '/playlists'
512 return this.url + '#main-key'
516 return this.serverId === null
519 getWebfingerUrl (this: MActorServer) {
520 return 'acct:' + this.preferredUsername + '@' + this.getHost()
524 return this.Server ? `${this.preferredUsername}@${this.Server.host}` : this.preferredUsername
527 getHost (this: MActorHost) {
528 return this.Server ? this.Server.host : WEBSERVER.HOST
531 getRedundancyAllowed (this: MActorRedundancyAllowed) {
532 return this.Server ? this.Server.redundancyAllowed : false
536 if (!this.avatarId) return undefined
538 return WEBSERVER.URL + this.Avatar.getStaticPath()
542 if (this.isOwned()) return false
544 return isOutdated(this, ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL)