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,
47 MActorSummaryFormattable,
49 } from '../../typings/models'
50 import * as Bluebird from 'bluebird'
51 import { Op } from 'sequelize'
57 export const unusedActorAttributesForAPI = [
70 @DefaultScope(() => ({
86 model: AccountModel.unscoped(),
90 model: VideoChannelModel.unscoped(),
118 fields: [ 'preferredUsername', 'serverId' ],
127 fields: [ 'preferredUsername' ],
134 fields: [ 'inboxUrl', 'sharedInboxUrl' ]
137 fields: [ 'sharedInboxUrl' ]
140 fields: [ 'serverId' ]
143 fields: [ 'avatarId' ]
146 fields: [ 'followersUrl' ]
150 export class ActorModel extends Model<ActorModel> {
153 @Column(DataType.ENUM(...values(ACTIVITY_PUB_ACTOR_TYPES)))
154 type: ActivityPubActorType
157 @Is('ActorPreferredUsername', value => throwIfNotValid(value, isActorPreferredUsernameValid, 'actor preferred username'))
159 preferredUsername: string
162 @Is('ActorUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
163 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
167 @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPublicKeyValid, 'public key', true))
168 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.PUBLIC_KEY.max))
172 @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPrivateKeyValid, 'private key', true))
173 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.PRIVATE_KEY.max))
177 @Is('ActorFollowersCount', value => throwIfNotValid(value, isActorFollowersCountValid, 'followers count'))
179 followersCount: number
182 @Is('ActorFollowersCount', value => throwIfNotValid(value, isActorFollowingCountValid, 'following count'))
184 followingCount: number
187 @Is('ActorInboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'inbox url'))
188 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
192 @Is('ActorOutboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'outbox url', true))
193 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
197 @Is('ActorSharedInboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'shared inbox url', true))
198 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
199 sharedInboxUrl: string
202 @Is('ActorFollowersUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'followers url', true))
203 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
207 @Is('ActorFollowingUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'following url', true))
208 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
217 @ForeignKey(() => AvatarModel)
221 @BelongsTo(() => AvatarModel, {
225 onDelete: 'set null',
230 @HasMany(() => ActorFollowModel, {
235 as: 'ActorFollowings',
238 ActorFollowing: ActorFollowModel[]
240 @HasMany(() => ActorFollowModel, {
242 name: 'targetActorId',
245 as: 'ActorFollowers',
248 ActorFollowers: ActorFollowModel[]
250 @ForeignKey(() => ServerModel)
254 @BelongsTo(() => ServerModel, {
262 @HasOne(() => AccountModel, {
269 Account: AccountModel
271 @HasOne(() => VideoChannelModel, {
278 VideoChannel: VideoChannelModel
280 static load (id: number): Bluebird<MActor> {
281 return ActorModel.unscoped().findByPk(id)
284 static loadFull (id: number): Bluebird<MActorFull> {
285 return ActorModel.scope(ScopeNames.FULL).findByPk(id)
288 static loadFromAccountByVideoId (videoId: number, transaction: Sequelize.Transaction): Bluebird<MActor> {
292 attributes: [ 'id' ],
293 model: AccountModel.unscoped(),
297 attributes: [ 'id' ],
298 model: VideoChannelModel.unscoped(),
302 attributes: [ 'id' ],
303 model: VideoModel.unscoped(),
317 return ActorModel.unscoped().findOne(query)
320 static isActorUrlExist (url: string) {
328 return ActorModel.unscoped().findOne(query)
332 static listByFollowersUrls (followersUrls: string[], transaction?: Sequelize.Transaction): Bluebird<MActorFull[]> {
336 [ Sequelize.Op.in ]: followersUrls
342 return ActorModel.scope(ScopeNames.FULL).findAll(query)
345 static loadLocalByName (preferredUsername: string, transaction?: Sequelize.Transaction): Bluebird<MActorFull> {
354 return ActorModel.scope(ScopeNames.FULL).findOne(query)
357 static loadByNameAndHost (preferredUsername: string, host: string): Bluebird<MActorFull> {
373 return ActorModel.scope(ScopeNames.FULL).findOne(query)
376 static loadByUrl (url: string, transaction?: Sequelize.Transaction): Bluebird<MActorAccountChannelId> {
384 attributes: [ 'id' ],
385 model: AccountModel.unscoped(),
389 attributes: [ 'id' ],
390 model: VideoChannelModel.unscoped(),
396 return ActorModel.unscoped().findOne(query)
399 static loadByUrlAndPopulateAccountAndChannel (url: string, transaction?: Sequelize.Transaction): Bluebird<MActorFull> {
407 return ActorModel.scope(ScopeNames.FULL).findOne(query)
410 static incrementFollows (id: number, column: 'followersCount' | 'followingCount', by: number) {
411 return ActorModel.increment(column, {
419 getSharedInbox (this: MActorWithInboxes) {
420 return this.sharedInboxUrl || this.inboxUrl
423 toFormattedSummaryJSON (this: MActorSummaryFormattable) {
424 let avatar: Avatar = null
426 avatar = this.Avatar.toFormattedJSON()
431 name: this.preferredUsername,
432 host: this.getHost(),
437 toFormattedJSON (this: MActorFormattable) {
438 const base = this.toFormattedSummaryJSON()
440 return Object.assign(base, {
442 hostRedundancyAllowed: this.getRedundancyAllowed(),
443 followingCount: this.followingCount,
444 followersCount: this.followersCount,
445 createdAt: this.createdAt,
446 updatedAt: this.updatedAt
450 toActivityPubObject (this: MActorAP, name: string) {
453 const extension = extname(this.Avatar.filename)
456 mediaType: extension === '.png' ? 'image/png' : 'image/jpeg',
457 url: this.getAvatarUrl()
464 following: this.getFollowingUrl(),
465 followers: this.getFollowersUrl(),
466 playlists: this.getPlaylistsUrl(),
467 inbox: this.inboxUrl,
468 outbox: this.outboxUrl,
469 preferredUsername: this.preferredUsername,
473 sharedInbox: this.sharedInboxUrl
476 id: this.getPublicKeyUrl(),
478 publicKeyPem: this.publicKey
483 return activityPubContextify(json)
486 getFollowerSharedInboxUrls (t: Sequelize.Transaction) {
488 attributes: [ 'sharedInboxUrl' ],
492 model: ActorFollowModel.unscoped(),
494 as: 'ActorFollowing',
497 targetActorId: this.id
504 return ActorModel.findAll(query)
505 .then(accounts => accounts.map(a => a.sharedInboxUrl))
509 return this.url + '/following'
513 return this.url + '/followers'
517 return this.url + '/playlists'
521 return this.url + '#main-key'
525 return this.serverId === null
528 getWebfingerUrl (this: MActorServer) {
529 return 'acct:' + this.preferredUsername + '@' + this.getHost()
533 return this.Server ? `${this.preferredUsername}@${this.Server.host}` : this.preferredUsername
536 getHost (this: MActorHost) {
537 return this.Server ? this.Server.host : WEBSERVER.HOST
540 getRedundancyAllowed () {
541 return this.Server ? this.Server.redundancyAllowed : false
545 if (!this.avatarId) return undefined
547 return WEBSERVER.URL + this.Avatar.getStaticPath()
551 if (this.isOwned()) return false
553 return isOutdated(this, ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL)