1 import { values } from 'lodash'
2 import { extname } from 'path'
18 } from 'sequelize-typescript'
19 import { 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,
48 } from '../../typings/models'
49 import * as Bluebird from 'bluebird'
50 import { Op, Transaction } from 'sequelize'
56 export const unusedActorAttributesForAPI = [
69 @DefaultScope(() => ({
85 model: AccountModel.unscoped(),
89 model: VideoChannelModel.unscoped(),
117 fields: [ 'preferredUsername', 'serverId' ],
126 // fields: [ 'preferredUsername' ],
133 fields: [ 'inboxUrl', 'sharedInboxUrl' ]
136 fields: [ 'sharedInboxUrl' ]
139 fields: [ 'serverId' ]
142 fields: [ 'avatarId' ]
145 fields: [ 'followersUrl' ]
149 export class ActorModel extends Model<ActorModel> {
152 @Column(DataType.ENUM(...values(ACTIVITY_PUB_ACTOR_TYPES)))
153 type: ActivityPubActorType
156 @Is('ActorPreferredUsername', value => throwIfNotValid(value, isActorPreferredUsernameValid, 'actor preferred username'))
158 preferredUsername: string
161 @Is('ActorUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
162 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
166 @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPublicKeyValid, 'public key', true))
167 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.PUBLIC_KEY.max))
171 @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPrivateKeyValid, 'private key', true))
172 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.PRIVATE_KEY.max))
176 @Is('ActorFollowersCount', value => throwIfNotValid(value, isActorFollowersCountValid, 'followers count'))
178 followersCount: number
181 @Is('ActorFollowersCount', value => throwIfNotValid(value, isActorFollowingCountValid, 'following count'))
183 followingCount: number
186 @Is('ActorInboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'inbox url'))
187 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
191 @Is('ActorOutboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'outbox url', true))
192 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
196 @Is('ActorSharedInboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'shared inbox url', true))
197 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
198 sharedInboxUrl: string
201 @Is('ActorFollowersUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'followers url', true))
202 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
206 @Is('ActorFollowingUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'following url', true))
207 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
216 @ForeignKey(() => AvatarModel)
220 @BelongsTo(() => AvatarModel, {
224 onDelete: 'set null',
229 @HasMany(() => ActorFollowModel, {
234 as: 'ActorFollowings',
237 ActorFollowing: ActorFollowModel[]
239 @HasMany(() => ActorFollowModel, {
241 name: 'targetActorId',
244 as: 'ActorFollowers',
247 ActorFollowers: ActorFollowModel[]
249 @ForeignKey(() => ServerModel)
253 @BelongsTo(() => ServerModel, {
261 @HasOne(() => AccountModel, {
268 Account: AccountModel
270 @HasOne(() => VideoChannelModel, {
277 VideoChannel: VideoChannelModel
279 private static cache: { [ id: string ]: any } = {}
281 static load (id: number): Bluebird<MActor> {
282 return ActorModel.unscoped().findByPk(id)
285 static loadFull (id: number): Bluebird<MActorFull> {
286 return ActorModel.scope(ScopeNames.FULL).findByPk(id)
289 static loadFromAccountByVideoId (videoId: number, transaction: Transaction): Bluebird<MActor> {
293 attributes: [ 'id' ],
294 model: AccountModel.unscoped(),
298 attributes: [ 'id' ],
299 model: VideoChannelModel.unscoped(),
303 attributes: [ 'id' ],
304 model: VideoModel.unscoped(),
318 return ActorModel.unscoped().findOne(query)
321 static isActorUrlExist (url: string) {
329 return ActorModel.unscoped().findOne(query)
333 static listByFollowersUrls (followersUrls: string[], transaction?: Transaction): Bluebird<MActorFull[]> {
337 [ Op.in ]: followersUrls
343 return ActorModel.scope(ScopeNames.FULL).findAll(query)
346 static loadLocalByName (preferredUsername: string, transaction?: Transaction): Bluebird<MActorFull> {
347 // The server actor never change, so we can easily cache it
348 if (preferredUsername === SERVER_ACTOR_NAME && ActorModel.cache[preferredUsername]) {
349 return Bluebird.resolve(ActorModel.cache[preferredUsername])
360 return ActorModel.scope(ScopeNames.FULL)
363 if (preferredUsername === SERVER_ACTOR_NAME) {
364 ActorModel.cache[ preferredUsername ] = actor
371 static loadByNameAndHost (preferredUsername: string, host: string): Bluebird<MActorFull> {
387 return ActorModel.scope(ScopeNames.FULL).findOne(query)
390 static loadByUrl (url: string, transaction?: Transaction): Bluebird<MActorAccountChannelId> {
398 attributes: [ 'id' ],
399 model: AccountModel.unscoped(),
403 attributes: [ 'id' ],
404 model: VideoChannelModel.unscoped(),
410 return ActorModel.unscoped().findOne(query)
413 static loadByUrlAndPopulateAccountAndChannel (url: string, transaction?: Transaction): Bluebird<MActorFull> {
421 return ActorModel.scope(ScopeNames.FULL).findOne(query)
424 static incrementFollows (id: number, column: 'followersCount' | 'followingCount', by: number) {
425 return ActorModel.increment(column, {
433 getSharedInbox (this: MActorWithInboxes) {
434 return this.sharedInboxUrl || this.inboxUrl
437 toFormattedSummaryJSON (this: MActorSummaryFormattable) {
438 let avatar: Avatar = null
440 avatar = this.Avatar.toFormattedJSON()
445 name: this.preferredUsername,
446 host: this.getHost(),
451 toFormattedJSON (this: MActorFormattable) {
452 const base = this.toFormattedSummaryJSON()
454 return Object.assign(base, {
456 hostRedundancyAllowed: this.getRedundancyAllowed(),
457 followingCount: this.followingCount,
458 followersCount: this.followersCount,
459 createdAt: this.createdAt,
460 updatedAt: this.updatedAt
464 toActivityPubObject (this: MActorAP, name: string) {
467 const extension = extname(this.Avatar.filename)
470 mediaType: extension === '.png' ? 'image/png' : 'image/jpeg',
471 url: this.getAvatarUrl()
478 following: this.getFollowingUrl(),
479 followers: this.getFollowersUrl(),
480 playlists: this.getPlaylistsUrl(),
481 inbox: this.inboxUrl,
482 outbox: this.outboxUrl,
483 preferredUsername: this.preferredUsername,
487 sharedInbox: this.sharedInboxUrl
490 id: this.getPublicKeyUrl(),
492 publicKeyPem: this.publicKey
497 return activityPubContextify(json)
500 getFollowerSharedInboxUrls (t: Transaction) {
502 attributes: [ 'sharedInboxUrl' ],
506 model: ActorFollowModel.unscoped(),
508 as: 'ActorFollowing',
511 targetActorId: this.id
518 return ActorModel.findAll(query)
519 .then(accounts => accounts.map(a => a.sharedInboxUrl))
523 return this.url + '/following'
527 return this.url + '/followers'
531 return this.url + '/playlists'
535 return this.url + '#main-key'
539 return this.serverId === null
542 getWebfingerUrl (this: MActorServer) {
543 return 'acct:' + this.preferredUsername + '@' + this.getHost()
547 return this.Server ? `${this.preferredUsername}@${this.Server.host}` : this.preferredUsername
550 getHost (this: MActorHost) {
551 return this.Server ? this.Server.host : WEBSERVER.HOST
554 getRedundancyAllowed () {
555 return this.Server ? this.Server.redundancyAllowed : false
559 if (!this.avatarId) return undefined
561 return WEBSERVER.URL + this.Avatar.getStaticPath()
565 if (this.isOwned()) return false
567 return isOutdated(this, ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL)