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'
56 export const unusedActorAttributesForAPI = [
69 @DefaultScope(() => ({
85 model: AccountModel.unscoped(),
89 model: VideoChannelModel.unscoped(),
117 fields: [ 'preferredUsername', 'serverId' ],
121 fields: [ 'inboxUrl', 'sharedInboxUrl' ]
124 fields: [ 'sharedInboxUrl' ]
127 fields: [ 'serverId' ]
130 fields: [ 'avatarId' ]
133 fields: [ 'followersUrl' ]
137 export class ActorModel extends Model<ActorModel> {
140 @Column(DataType.ENUM(...values(ACTIVITY_PUB_ACTOR_TYPES)))
141 type: ActivityPubActorType
144 @Is('ActorPreferredUsername', value => throwIfNotValid(value, isActorPreferredUsernameValid, 'actor preferred username'))
146 preferredUsername: string
149 @Is('ActorUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
150 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
154 @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPublicKeyValid, 'public key', true))
155 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.PUBLIC_KEY.max))
159 @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPrivateKeyValid, 'private key', true))
160 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.PRIVATE_KEY.max))
164 @Is('ActorFollowersCount', value => throwIfNotValid(value, isActorFollowersCountValid, 'followers count'))
166 followersCount: number
169 @Is('ActorFollowersCount', value => throwIfNotValid(value, isActorFollowingCountValid, 'following count'))
171 followingCount: number
174 @Is('ActorInboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'inbox url'))
175 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
179 @Is('ActorOutboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'outbox url', true))
180 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
184 @Is('ActorSharedInboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'shared inbox url', true))
185 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
186 sharedInboxUrl: string
189 @Is('ActorFollowersUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'followers url', true))
190 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
194 @Is('ActorFollowingUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'following url', true))
195 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
204 @ForeignKey(() => AvatarModel)
208 @BelongsTo(() => AvatarModel, {
212 onDelete: 'set null',
217 @HasMany(() => ActorFollowModel, {
222 as: 'ActorFollowings',
225 ActorFollowing: ActorFollowModel[]
227 @HasMany(() => ActorFollowModel, {
229 name: 'targetActorId',
232 as: 'ActorFollowers',
235 ActorFollowers: ActorFollowModel[]
237 @ForeignKey(() => ServerModel)
241 @BelongsTo(() => ServerModel, {
249 @HasOne(() => AccountModel, {
256 Account: AccountModel
258 @HasOne(() => VideoChannelModel, {
265 VideoChannel: VideoChannelModel
267 static load (id: number): Bluebird<MActor> {
268 return ActorModel.unscoped().findByPk(id)
271 static loadFull (id: number): Bluebird<MActorFull> {
272 return ActorModel.scope(ScopeNames.FULL).findByPk(id)
275 static loadFromAccountByVideoId (videoId: number, transaction: Sequelize.Transaction): Bluebird<MActor> {
279 attributes: [ 'id' ],
280 model: AccountModel.unscoped(),
284 attributes: [ 'id' ],
285 model: VideoChannelModel.unscoped(),
289 attributes: [ 'id' ],
290 model: VideoModel.unscoped(),
304 return ActorModel.unscoped().findOne(query)
307 static isActorUrlExist (url: string) {
315 return ActorModel.unscoped().findOne(query)
319 static listByFollowersUrls (followersUrls: string[], transaction?: Sequelize.Transaction): Bluebird<MActorFull[]> {
323 [ Sequelize.Op.in ]: followersUrls
329 return ActorModel.scope(ScopeNames.FULL).findAll(query)
332 static loadLocalByName (preferredUsername: string, transaction?: Sequelize.Transaction): Bluebird<MActorFull> {
341 return ActorModel.scope(ScopeNames.FULL).findOne(query)
344 static loadByNameAndHost (preferredUsername: string, host: string): Bluebird<MActorFull> {
360 return ActorModel.scope(ScopeNames.FULL).findOne(query)
363 static loadByUrl (url: string, transaction?: Sequelize.Transaction): Bluebird<MActorAccountChannelId> {
371 attributes: [ 'id' ],
372 model: AccountModel.unscoped(),
376 attributes: [ 'id' ],
377 model: VideoChannelModel.unscoped(),
383 return ActorModel.unscoped().findOne(query)
386 static loadByUrlAndPopulateAccountAndChannel (url: string, transaction?: Sequelize.Transaction): Bluebird<MActorFull> {
394 return ActorModel.scope(ScopeNames.FULL).findOne(query)
397 static incrementFollows (id: number, column: 'followersCount' | 'followingCount', by: number) {
398 return ActorModel.increment(column, {
406 getSharedInbox (this: MActorWithInboxes) {
407 return this.sharedInboxUrl || this.inboxUrl
410 toFormattedSummaryJSON (this: MActorSummaryFormattable) {
411 let avatar: Avatar = null
413 avatar = this.Avatar.toFormattedJSON()
418 name: this.preferredUsername,
419 host: this.getHost(),
424 toFormattedJSON (this: MActorFormattable) {
425 const base = this.toFormattedSummaryJSON()
427 return Object.assign(base, {
429 hostRedundancyAllowed: this.getRedundancyAllowed(),
430 followingCount: this.followingCount,
431 followersCount: this.followersCount,
432 createdAt: this.createdAt,
433 updatedAt: this.updatedAt
437 toActivityPubObject (this: MActorAP, name: string) {
440 const extension = extname(this.Avatar.filename)
443 mediaType: extension === '.png' ? 'image/png' : 'image/jpeg',
444 url: this.getAvatarUrl()
451 following: this.getFollowingUrl(),
452 followers: this.getFollowersUrl(),
453 playlists: this.getPlaylistsUrl(),
454 inbox: this.inboxUrl,
455 outbox: this.outboxUrl,
456 preferredUsername: this.preferredUsername,
460 sharedInbox: this.sharedInboxUrl
463 id: this.getPublicKeyUrl(),
465 publicKeyPem: this.publicKey
470 return activityPubContextify(json)
473 getFollowerSharedInboxUrls (t: Sequelize.Transaction) {
475 attributes: [ 'sharedInboxUrl' ],
479 model: ActorFollowModel.unscoped(),
481 as: 'ActorFollowing',
484 targetActorId: this.id
491 return ActorModel.findAll(query)
492 .then(accounts => accounts.map(a => a.sharedInboxUrl))
496 return this.url + '/following'
500 return this.url + '/followers'
504 return this.url + '/playlists'
508 return this.url + '#main-key'
512 return this.serverId === null
515 getWebfingerUrl (this: MActorServer) {
516 return 'acct:' + this.preferredUsername + '@' + this.getHost()
520 return this.Server ? `${this.preferredUsername}@${this.Server.host}` : this.preferredUsername
523 getHost (this: MActorHost) {
524 return this.Server ? this.Server.host : WEBSERVER.HOST
527 getRedundancyAllowed () {
528 return this.Server ? this.Server.redundancyAllowed : false
532 if (!this.avatarId) return undefined
534 return WEBSERVER.URL + this.Avatar.getStaticPath()
538 if (this.isOwned()) return false
540 return isOutdated(this, ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL)