1 import { values } from 'lodash'
2 import { extname } from 'path'
18 } from 'sequelize-typescript'
19 import { ActivityIconObject, ActivityPubActorType } from '../../../shared/models/activitypub'
20 import { Avatar } from '../../../shared/models/avatars/avatar.model'
21 import { activityPubContextify } from '../../helpers/activitypub'
23 isActorFollowersCountValid,
24 isActorFollowingCountValid,
25 isActorPreferredUsernameValid,
26 isActorPrivateKeyValid,
28 } from '../../helpers/custom-validators/activitypub/actor'
29 import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
30 import { ACTIVITY_PUB, ACTIVITY_PUB_ACTOR_TYPES, CONSTRAINTS_FIELDS, SERVER_ACTOR_NAME, WEBSERVER } from '../../initializers/constants'
31 import { AccountModel } from '../account/account'
32 import { AvatarModel } from '../avatar/avatar'
33 import { ServerModel } from '../server/server'
34 import { isOutdated, throwIfNotValid } from '../utils'
35 import { VideoChannelModel } from '../video/video-channel'
36 import { ActorFollowModel } from './actor-follow'
37 import { VideoModel } from '../video/video'
40 MActorAccountChannelId,
46 MActorSummaryFormattable, MActorUrl,
48 } from '../../typings/models'
49 import * as Bluebird from 'bluebird'
50 import { Op, Transaction, literal } from 'sequelize'
51 import { ModelCache } from '@server/models/model-cache'
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: 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?: Transaction): Bluebird<MActorFull[]> {
336 [Op.in]: followersUrls
342 return ActorModel.scope(ScopeNames.FULL).findAll(query)
345 static loadLocalByName (preferredUsername: string, transaction?: Transaction): Bluebird<MActorFull> {
355 return ActorModel.scope(ScopeNames.FULL)
359 return ModelCache.Instance.doCache({
360 cacheType: 'local-actor-name',
361 key: preferredUsername,
362 // The server actor never change, so we can easily cache it
363 whitelist: () => preferredUsername === SERVER_ACTOR_NAME,
368 static loadLocalUrlByName (preferredUsername: string, transaction?: Transaction): Bluebird<MActorUrl> {
371 attributes: [ 'url' ],
379 return ActorModel.unscoped()
383 return ModelCache.Instance.doCache({
384 cacheType: 'local-actor-name',
385 key: preferredUsername,
386 // The server actor never change, so we can easily cache it
387 whitelist: () => preferredUsername === SERVER_ACTOR_NAME,
392 static loadByNameAndHost (preferredUsername: string, host: string): Bluebird<MActorFull> {
408 return ActorModel.scope(ScopeNames.FULL).findOne(query)
411 static loadByUrl (url: string, transaction?: Transaction): Bluebird<MActorAccountChannelId> {
419 attributes: [ 'id' ],
420 model: AccountModel.unscoped(),
424 attributes: [ 'id' ],
425 model: VideoChannelModel.unscoped(),
431 return ActorModel.unscoped().findOne(query)
434 static loadByUrlAndPopulateAccountAndChannel (url: string, transaction?: Transaction): Bluebird<MActorFull> {
442 return ActorModel.scope(ScopeNames.FULL).findOne(query)
445 static rebuildFollowsCount (ofId: number, type: 'followers' | 'following', transaction?: Transaction) {
446 const sanitizedOfId = parseInt(ofId + '', 10)
447 const where = { id: sanitizedOfId }
449 let columnToUpdate: string
450 let columnOfCount: string
452 if (type === 'followers') {
453 columnToUpdate = 'followersCount'
454 columnOfCount = 'targetActorId'
456 columnToUpdate = 'followingCount'
457 columnOfCount = 'actorId'
460 return ActorModel.update({
461 [columnToUpdate]: literal(`(SELECT COUNT(*) FROM "actorFollow" WHERE "${columnOfCount}" = ${sanitizedOfId})`)
462 }, { where, transaction })
465 static loadAccountActorByVideoId (videoId: number): Bluebird<MActor> {
469 attributes: [ 'id' ],
470 model: AccountModel.unscoped(),
474 attributes: [ 'id', 'accountId' ],
475 model: VideoChannelModel.unscoped(),
479 attributes: [ 'id', 'channelId' ],
480 model: VideoModel.unscoped(),
492 return ActorModel.unscoped().findOne(query)
495 getSharedInbox (this: MActorWithInboxes) {
496 return this.sharedInboxUrl || this.inboxUrl
499 toFormattedSummaryJSON (this: MActorSummaryFormattable) {
500 let avatar: Avatar = null
502 avatar = this.Avatar.toFormattedJSON()
507 name: this.preferredUsername,
508 host: this.getHost(),
513 toFormattedJSON (this: MActorFormattable) {
514 const base = this.toFormattedSummaryJSON()
516 return Object.assign(base, {
518 hostRedundancyAllowed: this.getRedundancyAllowed(),
519 followingCount: this.followingCount,
520 followersCount: this.followersCount,
521 createdAt: this.createdAt,
522 updatedAt: this.updatedAt
526 toActivityPubObject (this: MActorAP, name: string) {
527 let icon: ActivityIconObject
530 const extension = extname(this.Avatar.filename)
534 mediaType: extension === '.png' ? 'image/png' : 'image/jpeg',
535 url: this.getAvatarUrl()
542 following: this.getFollowingUrl(),
543 followers: this.getFollowersUrl(),
544 playlists: this.getPlaylistsUrl(),
545 inbox: this.inboxUrl,
546 outbox: this.outboxUrl,
547 preferredUsername: this.preferredUsername,
551 sharedInbox: this.sharedInboxUrl
554 id: this.getPublicKeyUrl(),
556 publicKeyPem: this.publicKey
561 return activityPubContextify(json)
564 getFollowerSharedInboxUrls (t: Transaction) {
566 attributes: [ 'sharedInboxUrl' ],
570 model: ActorFollowModel.unscoped(),
572 as: 'ActorFollowing',
575 targetActorId: this.id
582 return ActorModel.findAll(query)
583 .then(accounts => accounts.map(a => a.sharedInboxUrl))
587 return this.url + '/following'
591 return this.url + '/followers'
595 return this.url + '/playlists'
599 return this.url + '#main-key'
603 return this.serverId === null
606 getWebfingerUrl (this: MActorServer) {
607 return 'acct:' + this.preferredUsername + '@' + this.getHost()
611 return this.Server ? `${this.preferredUsername}@${this.Server.host}` : this.preferredUsername
614 getHost (this: MActorHost) {
615 return this.Server ? this.Server.host : WEBSERVER.HOST
618 getRedundancyAllowed () {
619 return this.Server ? this.Server.redundancyAllowed : false
623 if (!this.avatarId) return undefined
625 return WEBSERVER.URL + this.Avatar.getStaticPath()
629 if (this.isOwned()) return false
631 return isOutdated(this, ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL)