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
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', true))
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', true))
189 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
193 @Is('ActorFollowingUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'following url', true))
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 (this: MActorAP, name: string) {
435 const extension = extname(this.Avatar.filename)
438 mediaType: extension === '.png' ? 'image/png' : 'image/jpeg',
439 url: this.getAvatarUrl()
446 following: this.getFollowingUrl(),
447 followers: this.getFollowersUrl(),
448 playlists: this.getPlaylistsUrl(),
449 inbox: this.inboxUrl,
450 outbox: this.outboxUrl,
451 preferredUsername: this.preferredUsername,
455 sharedInbox: this.sharedInboxUrl
458 id: this.getPublicKeyUrl(),
460 publicKeyPem: this.publicKey
465 return activityPubContextify(json)
468 getFollowerSharedInboxUrls (t: Sequelize.Transaction) {
470 attributes: [ 'sharedInboxUrl' ],
474 model: ActorFollowModel.unscoped(),
476 as: 'ActorFollowing',
479 targetActorId: this.id
486 return ActorModel.findAll(query)
487 .then(accounts => accounts.map(a => a.sharedInboxUrl))
491 return this.url + '/following'
495 return this.url + '/followers'
499 return this.url + '/playlists'
503 return this.url + '#main-key'
507 return this.serverId === null
510 getWebfingerUrl (this: MActorServer) {
511 return 'acct:' + this.preferredUsername + '@' + this.getHost()
515 return this.Server ? `${this.preferredUsername}@${this.Server.host}` : this.preferredUsername
518 getHost (this: MActorHost) {
519 return this.Server ? this.Server.host : WEBSERVER.HOST
522 getRedundancyAllowed () {
523 return this.Server ? this.Server.redundancyAllowed : false
527 if (!this.avatarId) return undefined
529 return WEBSERVER.URL + this.Avatar.getStaticPath()
533 if (this.isOwned()) return false
535 return isOutdated(this, ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL)