X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Factivitypub%2Factor.ts;h=e16bd5d7943de0b36e55cba50e56c64ea58e7516;hb=aa55a4da422330fe2816f1764b64f6607a0ca4aa;hp=8cedcc2bc7dafa41be9802f6ec8a6e12c6511342;hpb=ce33ee01cd3806201b676c318e9aa930032921b2;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/activitypub/actor.ts b/server/models/activitypub/actor.ts index 8cedcc2bc..e16bd5d79 100644 --- a/server/models/activitypub/actor.ts +++ b/server/models/activitypub/actor.ts @@ -1,5 +1,5 @@ import { values } from 'lodash' -import { join } from 'path' +import { extname } from 'path' import * as Sequelize from 'sequelize' import { AllowNull, @@ -7,7 +7,8 @@ import { Column, CreatedAt, DataType, - Default, DefaultScope, + Default, + DefaultScope, ForeignKey, HasMany, HasOne, @@ -20,16 +21,16 @@ import { } from 'sequelize-typescript' import { ActivityPubActorType } from '../../../shared/models/activitypub' import { Avatar } from '../../../shared/models/avatars/avatar.model' -import { activityPubContextify } from '../../helpers' +import { activityPubContextify } from '../../helpers/activitypub' import { - isActivityPubUrlValid, isActorFollowersCountValid, isActorFollowingCountValid, - isActorNameValid, + isActorPreferredUsernameValid, isActorPrivateKeyValid, isActorPublicKeyValid -} from '../../helpers/custom-validators/activitypub' -import { ACTIVITY_PUB_ACTOR_TYPES, AVATARS_DIR, CONFIG, CONSTRAINTS_FIELDS } from '../../initializers' +} from '../../helpers/custom-validators/activitypub/actor' +import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc' +import { ACTIVITY_PUB, ACTIVITY_PUB_ACTOR_TYPES, CONFIG, CONSTRAINTS_FIELDS } from '../../initializers' import { AccountModel } from '../account/account' import { AvatarModel } from '../avatar/avatar' import { ServerModel } from '../server/server' @@ -41,11 +42,26 @@ enum ScopeNames { FULL = 'FULL' } +export const unusedActorAttributesForAPI = [ + 'publicKey', + 'privateKey', + 'inboxUrl', + 'outboxUrl', + 'sharedInboxUrl', + 'followersUrl', + 'followingUrl', + 'url' +] + @DefaultScope({ include: [ { model: () => ServerModel, required: false + }, + { + model: () => AvatarModel, + required: false } ] }) @@ -53,16 +69,20 @@ enum ScopeNames { [ScopeNames.FULL]: { include: [ { - model: () => AccountModel, + model: () => AccountModel.unscoped(), required: false }, { - model: () => VideoChannelModel, + model: () => VideoChannelModel.unscoped(), required: false }, { model: () => ServerModel, required: false + }, + { + model: () => AvatarModel, + required: false } ] } @@ -71,8 +91,31 @@ enum ScopeNames { tableName: 'actor', indexes: [ { - fields: [ 'name', 'serverId' ], + fields: [ 'url' ], + unique: true + }, + { + fields: [ 'preferredUsername', 'serverId' ], + unique: true + }, + { + fields: [ 'inboxUrl', 'sharedInboxUrl' ] + }, + { + fields: [ 'sharedInboxUrl' ] + }, + { + fields: [ 'serverId' ] + }, + { + fields: [ 'avatarId' ] + }, + { + fields: [ 'uuid' ], unique: true + }, + { + fields: [ 'followersUrl' ] } ] }) @@ -89,23 +132,23 @@ export class ActorModel extends Model { uuid: string @AllowNull(false) - @Is('ActorName', value => throwIfNotValid(value, isActorNameValid, 'actor name')) + @Is('ActorPreferredUsername', value => throwIfNotValid(value, isActorPreferredUsernameValid, 'actor preferred username')) @Column - name: string + preferredUsername: string @AllowNull(false) @Is('ActorUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url')) - @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTOR.URL.max)) + @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max)) url: string @AllowNull(true) @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPublicKeyValid, 'public key')) - @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTOR.PUBLIC_KEY.max)) + @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.PUBLIC_KEY.max)) publicKey: string @AllowNull(true) @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPrivateKeyValid, 'private key')) - @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTOR.PRIVATE_KEY.max)) + @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.PRIVATE_KEY.max)) privateKey: string @AllowNull(false) @@ -120,27 +163,27 @@ export class ActorModel extends Model { @AllowNull(false) @Is('ActorInboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'inbox url')) - @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTOR.URL.max)) + @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max)) inboxUrl: string @AllowNull(false) @Is('ActorOutboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'outbox url')) - @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTOR.URL.max)) + @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max)) outboxUrl: string @AllowNull(false) @Is('ActorSharedInboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'shared inbox url')) - @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTOR.URL.max)) + @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max)) sharedInboxUrl: string @AllowNull(false) @Is('ActorFollowersUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'followers url')) - @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTOR.URL.max)) + @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max)) followersUrl: string @AllowNull(false) @Is('ActorFollowingUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'following url')) - @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTOR.URL.max)) + @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max)) followingUrl: string @CreatedAt @@ -157,7 +200,8 @@ export class ActorModel extends Model { foreignKey: { allowNull: true }, - onDelete: 'cascade' + onDelete: 'set null', + hooks: true }) Avatar: AvatarModel @@ -168,17 +212,17 @@ export class ActorModel extends Model { }, onDelete: 'cascade' }) - AccountFollowing: ActorFollowModel[] + ActorFollowing: ActorFollowModel[] @HasMany(() => ActorFollowModel, { foreignKey: { name: 'targetActorId', allowNull: false }, - as: 'followers', + as: 'ActorFollowers', onDelete: 'cascade' }) - AccountFollowers: ActorFollowModel[] + ActorFollowers: ActorFollowModel[] @ForeignKey(() => ServerModel) @Column @@ -196,7 +240,8 @@ export class ActorModel extends Model { foreignKey: { allowNull: true }, - onDelete: 'cascade' + onDelete: 'cascade', + hooks: true }) Account: AccountModel @@ -204,22 +249,13 @@ export class ActorModel extends Model { foreignKey: { allowNull: true }, - onDelete: 'cascade' + onDelete: 'cascade', + hooks: true }) VideoChannel: VideoChannelModel static load (id: number) { - return ActorModel.scope(ScopeNames.FULL).findById(id) - } - - static loadByUUID (uuid: string) { - const query = { - where: { - uuid - } - } - - return ActorModel.scope(ScopeNames.FULL).findOne(query) + return ActorModel.unscoped().findById(id) } static listByFollowersUrls (followersUrls: string[], transaction?: Sequelize.Transaction) { @@ -235,21 +271,22 @@ export class ActorModel extends Model { return ActorModel.scope(ScopeNames.FULL).findAll(query) } - static loadLocalByName (name: string) { + static loadLocalByName (preferredUsername: string, transaction?: Sequelize.Transaction) { const query = { where: { - name, + preferredUsername, serverId: null - } + }, + transaction } return ActorModel.scope(ScopeNames.FULL).findOne(query) } - static loadByNameAndHost (name: string, host: string) { + static loadByNameAndHost (preferredUsername: string, host: string) { const query = { where: { - name + preferredUsername }, include: [ { @@ -276,35 +313,37 @@ export class ActorModel extends Model { return ActorModel.scope(ScopeNames.FULL).findOne(query) } + static incrementFollows (id: number, column: 'followersCount' | 'followingCount', by: number) { + // FIXME: typings + return (ActorModel as any).increment(column, { + by, + where: { + id + } + }) + } + toFormattedJSON () { let avatar: Avatar = null if (this.Avatar) { - avatar = { - path: join(AVATARS_DIR.ACCOUNT, this.Avatar.filename), - createdAt: this.Avatar.createdAt, - updatedAt: this.Avatar.updatedAt - } - } - - let host = CONFIG.WEBSERVER.HOST - let score: number - if (this.Server) { - host = this.Server.host - score = this.Server.score + avatar = this.Avatar.toFormattedJSON() } return { id: this.id, + url: this.url, uuid: this.uuid, - host, - score, + name: this.preferredUsername, + host: this.getHost(), followingCount: this.followingCount, followersCount: this.followersCount, - avatar + avatar, + createdAt: this.createdAt, + updatedAt: this.updatedAt } } - toActivityPubObject (preferredUsername: string, type: 'Account' | 'Application' | 'VideoChannel') { + toActivityPubObject (name: string, type: 'Account' | 'Application' | 'VideoChannel') { let activityPubType if (type === 'Account') { activityPubType = 'Person' as 'Person' @@ -314,6 +353,16 @@ export class ActorModel extends Model { activityPubType = 'Group' as 'Group' } + let icon = undefined + if (this.avatarId) { + const extension = extname(this.Avatar.filename) + icon = { + type: 'Image', + mediaType: extension === '.png' ? 'image/png' : 'image/jpeg', + url: this.getAvatarUrl() + } + } + const json = { type: activityPubType, id: this.url, @@ -321,9 +370,9 @@ export class ActorModel extends Model { followers: this.getFollowersUrl(), inbox: this.inboxUrl, outbox: this.outboxUrl, - preferredUsername, + preferredUsername: this.preferredUsername, url: this.url, - name: this.name, + name, endpoints: { sharedInbox: this.sharedInboxUrl }, @@ -332,7 +381,8 @@ export class ActorModel extends Model { id: this.getPublicKeyUrl(), owner: this.url, publicKeyPem: this.publicKey - } + }, + icon } return activityPubContextify(json) @@ -343,10 +393,12 @@ export class ActorModel extends Model { attributes: [ 'sharedInboxUrl' ], include: [ { - model: ActorFollowModel, + attribute: [], + model: ActorFollowModel.unscoped(), required: true, - as: 'followers', + as: 'ActorFollowing', where: { + state: 'accepted', targetActorId: this.id } } @@ -373,4 +425,33 @@ export class ActorModel extends Model { isOwned () { return this.serverId === null } + + getWebfingerUrl () { + return 'acct:' + this.preferredUsername + '@' + this.getHost() + } + + getIdentifier () { + return this.Server ? `${this.preferredUsername}@${this.Server.host}` : this.preferredUsername + } + + getHost () { + return this.Server ? this.Server.host : CONFIG.WEBSERVER.HOST + } + + getAvatarUrl () { + if (!this.avatarId) return undefined + + return CONFIG.WEBSERVER.URL + this.Avatar.getWebserverPath() + } + + isOutdated () { + if (this.isOwned()) return false + + const now = Date.now() + const createdAtTime = this.createdAt.getTime() + const updatedAtTime = this.updatedAt.getTime() + + return (now - createdAtTime) > ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL && + (now - updatedAtTime) > ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL + } }