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'
44 export const unusedActorAttributesForAPI = [
57 @DefaultScope(() => ({
73 model: AccountModel.unscoped(),
77 model: VideoChannelModel.unscoped(),
105 fields: [ 'preferredUsername', 'serverId' ],
109 fields: [ 'inboxUrl', 'sharedInboxUrl' ]
112 fields: [ 'sharedInboxUrl' ]
115 fields: [ 'serverId' ]
118 fields: [ 'avatarId' ]
121 fields: [ 'followersUrl' ]
125 export class ActorModel extends Model<ActorModel> {
128 @Column(DataType.ENUM(...values(ACTIVITY_PUB_ACTOR_TYPES)))
129 type: ActivityPubActorType
132 @Is('ActorPreferredUsername', value => throwIfNotValid(value, isActorPreferredUsernameValid, 'actor preferred username'))
134 preferredUsername: string
137 @Is('ActorUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
138 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
142 @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPublicKeyValid, 'public key', true))
143 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.PUBLIC_KEY.max))
147 @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPrivateKeyValid, 'private key', true))
148 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.PRIVATE_KEY.max))
152 @Is('ActorFollowersCount', value => throwIfNotValid(value, isActorFollowersCountValid, 'followers count'))
154 followersCount: number
157 @Is('ActorFollowersCount', value => throwIfNotValid(value, isActorFollowingCountValid, 'following count'))
159 followingCount: number
162 @Is('ActorInboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'inbox url'))
163 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
167 @Is('ActorOutboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'outbox url'))
168 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
172 @Is('ActorSharedInboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'shared inbox url'))
173 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
174 sharedInboxUrl: string
177 @Is('ActorFollowersUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'followers url'))
178 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
182 @Is('ActorFollowingUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'following url'))
183 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
192 @ForeignKey(() => AvatarModel)
196 @BelongsTo(() => AvatarModel, {
200 onDelete: 'set null',
205 @HasMany(() => ActorFollowModel, {
210 as: 'ActorFollowings',
213 ActorFollowing: ActorFollowModel[]
215 @HasMany(() => ActorFollowModel, {
217 name: 'targetActorId',
220 as: 'ActorFollowers',
223 ActorFollowers: ActorFollowModel[]
225 @ForeignKey(() => ServerModel)
229 @BelongsTo(() => ServerModel, {
237 @HasOne(() => AccountModel, {
244 Account: AccountModel
246 @HasOne(() => VideoChannelModel, {
253 VideoChannel: VideoChannelModel
255 static load (id: number) {
256 return ActorModel.unscoped().findByPk(id)
259 static loadAccountActorByVideoId (videoId: number, transaction: Sequelize.Transaction) {
263 attributes: [ 'id' ],
264 model: AccountModel.unscoped(),
268 attributes: [ 'id' ],
269 model: VideoChannelModel.unscoped(),
273 attributes: [ 'id' ],
274 model: VideoModel.unscoped(),
288 return ActorModel.unscoped().findOne(query)
291 static isActorUrlExist (url: string) {
299 return ActorModel.unscoped().findOne(query)
303 static listByFollowersUrls (followersUrls: string[], transaction?: Sequelize.Transaction) {
307 [ Sequelize.Op.in ]: followersUrls
313 return ActorModel.scope(ScopeNames.FULL).findAll(query)
316 static loadLocalByName (preferredUsername: string, transaction?: Sequelize.Transaction) {
325 return ActorModel.scope(ScopeNames.FULL).findOne(query)
328 static loadByNameAndHost (preferredUsername: string, host: string) {
344 return ActorModel.scope(ScopeNames.FULL).findOne(query)
347 static loadByUrl (url: string, transaction?: Sequelize.Transaction) {
355 attributes: [ 'id' ],
356 model: AccountModel.unscoped(),
360 attributes: [ 'id' ],
361 model: VideoChannelModel.unscoped(),
367 return ActorModel.unscoped().findOne(query)
370 static loadByUrlAndPopulateAccountAndChannel (url: string, transaction?: Sequelize.Transaction) {
378 return ActorModel.scope(ScopeNames.FULL).findOne(query)
381 static incrementFollows (id: number, column: 'followersCount' | 'followingCount', by: number) {
382 return ActorModel.increment(column, {
391 let avatar: Avatar = null
393 avatar = this.Avatar.toFormattedJSON()
399 name: this.preferredUsername,
400 host: this.getHost(),
401 hostRedundancyAllowed: this.getRedundancyAllowed(),
402 followingCount: this.followingCount,
403 followersCount: this.followersCount,
405 createdAt: this.createdAt,
406 updatedAt: this.updatedAt
410 toActivityPubObject (name: string, type: 'Account' | 'Application' | 'VideoChannel') {
412 if (type === 'Account') {
413 activityPubType = 'Person' as 'Person'
414 } else if (type === 'Application') {
415 activityPubType = 'Application' as 'Application'
416 } else { // VideoChannel
417 activityPubType = 'Group' as 'Group'
422 const extension = extname(this.Avatar.filename)
425 mediaType: extension === '.png' ? 'image/png' : 'image/jpeg',
426 url: this.getAvatarUrl()
431 type: activityPubType,
433 following: this.getFollowingUrl(),
434 followers: this.getFollowersUrl(),
435 playlists: this.getPlaylistsUrl(),
436 inbox: this.inboxUrl,
437 outbox: this.outboxUrl,
438 preferredUsername: this.preferredUsername,
442 sharedInbox: this.sharedInboxUrl
445 id: this.getPublicKeyUrl(),
447 publicKeyPem: this.publicKey
452 return activityPubContextify(json)
455 getFollowerSharedInboxUrls (t: Sequelize.Transaction) {
457 attributes: [ 'sharedInboxUrl' ],
461 model: ActorFollowModel.unscoped(),
463 as: 'ActorFollowing',
466 targetActorId: this.id
473 return ActorModel.findAll(query)
474 .then(accounts => accounts.map(a => a.sharedInboxUrl))
478 return this.url + '/following'
482 return this.url + '/followers'
486 return this.url + '/playlists'
490 return this.url + '#main-key'
494 return this.serverId === null
498 return 'acct:' + this.preferredUsername + '@' + this.getHost()
502 return this.Server ? `${this.preferredUsername}@${this.Server.host}` : this.preferredUsername
506 return this.Server ? this.Server.host : WEBSERVER.HOST
509 getRedundancyAllowed () {
510 return this.Server ? this.Server.redundancyAllowed : false
514 if (!this.avatarId) return undefined
516 return WEBSERVER.URL + this.Avatar.getWebserverPath()
520 if (this.isOwned()) return false
522 return isOutdated(this, ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL)