X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Factivitypub%2Factor.ts;h=71db44b2f8948163c46e45241f61dac6b806c8fc;hb=f27a885a4368776ffb8158c917c6e3f3d21ef670;hp=b88e06b41a6eb2db3702cca9767db42df54132de;hpb=2ccaeeb341ffe8c2609039bf4c6d8835b4650316;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/activitypub/actor.ts b/server/models/activitypub/actor.ts index b88e06b41..71db44b2f 100644 --- a/server/models/activitypub/actor.ts +++ b/server/models/activitypub/actor.ts @@ -1,88 +1,157 @@ import { values } from 'lodash' import { extname } from 'path' -import * as Sequelize from 'sequelize' import { - AllowNull, BelongsTo, Column, CreatedAt, DataType, Default, DefaultScope, ForeignKey, HasMany, HasOne, Is, IsUUID, Model, Scopes, - Table, UpdatedAt + AllowNull, + BelongsTo, + Column, + CreatedAt, + DataType, + DefaultScope, + ForeignKey, + HasMany, + HasOne, + Is, + Model, + Scopes, + Table, + UpdatedAt } from 'sequelize-typescript' import { ActivityPubActorType } from '../../../shared/models/activitypub' import { Avatar } from '../../../shared/models/avatars/avatar.model' import { activityPubContextify } from '../../helpers/activitypub' import { - isActorFollowersCountValid, isActorFollowingCountValid, isActorPreferredUsernameValid, isActorPrivateKeyValid, + isActorFollowersCountValid, + isActorFollowingCountValid, + isActorPreferredUsernameValid, + isActorPrivateKeyValid, isActorPublicKeyValid } 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 { ACTIVITY_PUB, ACTIVITY_PUB_ACTOR_TYPES, CONSTRAINTS_FIELDS, WEBSERVER } from '../../initializers/constants' import { AccountModel } from '../account/account' import { AvatarModel } from '../avatar/avatar' import { ServerModel } from '../server/server' -import { throwIfNotValid } from '../utils' +import { isOutdated, throwIfNotValid } from '../utils' import { VideoChannelModel } from '../video/video-channel' import { ActorFollowModel } from './actor-follow' +import { VideoModel } from '../video/video' +import { + MActor, + MActorAccountChannelId, + MActorAP, + MActorFormattable, + MActorFull, + MActorHost, + MActorServer, + MActorSummaryFormattable, + MActorWithInboxes +} from '../../typings/models' +import * as Bluebird from 'bluebird' +import { Op, Transaction } from 'sequelize' enum ScopeNames { FULL = 'FULL' } -@DefaultScope({ +export const unusedActorAttributesForAPI = [ + 'publicKey', + 'privateKey', + 'inboxUrl', + 'outboxUrl', + 'sharedInboxUrl', + 'followersUrl', + 'followingUrl', + 'url', + 'createdAt', + 'updatedAt' +] + +@DefaultScope(() => ({ include: [ { - model: () => ServerModel, + model: ServerModel, required: false }, { - model: () => AvatarModel, + model: AvatarModel, required: false } ] -}) -@Scopes({ +})) +@Scopes(() => ({ [ScopeNames.FULL]: { include: [ { - model: () => AccountModel, + model: AccountModel.unscoped(), required: false }, { - model: () => VideoChannelModel, - required: false + model: VideoChannelModel.unscoped(), + required: false, + include: [ + { + model: AccountModel, + required: true + } + ] }, { - model: () => ServerModel, + model: ServerModel, required: false }, { - model: () => AvatarModel, + model: AvatarModel, required: false } ] } -}) +})) @Table({ tableName: 'actor', indexes: [ { - fields: [ 'url' ] + fields: [ 'url' ], + unique: true }, { fields: [ 'preferredUsername', 'serverId' ], - unique: true + unique: true, + where: { + serverId: { + [Op.ne]: null + } + } + }, + // { + // fields: [ 'preferredUsername' ], + // unique: true, + // where: { + // serverId: null + // } + // }, + { + fields: [ 'inboxUrl', 'sharedInboxUrl' ] + }, + { + fields: [ 'sharedInboxUrl' ] + }, + { + fields: [ 'serverId' ] + }, + { + fields: [ 'avatarId' ] + }, + { + fields: [ 'followersUrl' ] } ] }) export class ActorModel extends Model { @AllowNull(false) - @Column(DataType.ENUM(values(ACTIVITY_PUB_ACTOR_TYPES))) + @Column(DataType.ENUM(...values(ACTIVITY_PUB_ACTOR_TYPES))) type: ActivityPubActorType - @AllowNull(false) - @Default(DataType.UUIDV4) - @IsUUID(4) - @Column(DataType.UUID) - uuid: string - @AllowNull(false) @Is('ActorPreferredUsername', value => throwIfNotValid(value, isActorPreferredUsernameValid, 'actor preferred username')) @Column @@ -94,12 +163,12 @@ export class ActorModel extends Model { url: string @AllowNull(true) - @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPublicKeyValid, 'public key')) + @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPublicKeyValid, 'public key', true)) @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.PUBLIC_KEY.max)) publicKey: string @AllowNull(true) - @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPrivateKeyValid, 'private key')) + @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPrivateKeyValid, 'private key', true)) @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.PRIVATE_KEY.max)) privateKey: string @@ -118,23 +187,23 @@ export class ActorModel extends Model { @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max)) inboxUrl: string - @AllowNull(false) - @Is('ActorOutboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'outbox url')) + @AllowNull(true) + @Is('ActorOutboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'outbox url', true)) @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max)) outboxUrl: string - @AllowNull(false) - @Is('ActorSharedInboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'shared inbox url')) + @AllowNull(true) + @Is('ActorSharedInboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'shared inbox url', true)) @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max)) sharedInboxUrl: string - @AllowNull(false) - @Is('ActorFollowersUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'followers url')) + @AllowNull(true) + @Is('ActorFollowersUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'followers url', true)) @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max)) followersUrl: string - @AllowNull(false) - @Is('ActorFollowingUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'following url')) + @AllowNull(true) + @Is('ActorFollowingUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'following url', true)) @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max)) followingUrl: string @@ -152,7 +221,8 @@ export class ActorModel extends Model { foreignKey: { allowNull: true }, - onDelete: 'set null' + onDelete: 'set null', + hooks: true }) Avatar: AvatarModel @@ -161,19 +231,20 @@ export class ActorModel extends Model { name: 'actorId', allowNull: false }, + as: 'ActorFollowings', 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 @@ -191,7 +262,8 @@ export class ActorModel extends Model { foreignKey: { allowNull: true }, - onDelete: 'cascade' + onDelete: 'cascade', + hooks: true }) Account: AccountModel @@ -199,19 +271,68 @@ 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 load (id: number): Bluebird { + return ActorModel.unscoped().findByPk(id) + } + + static loadFull (id: number): Bluebird { + return ActorModel.scope(ScopeNames.FULL).findByPk(id) } - static listByFollowersUrls (followersUrls: string[], transaction?: Sequelize.Transaction) { + static loadFromAccountByVideoId (videoId: number, transaction: Transaction): Bluebird { + const query = { + include: [ + { + attributes: [ 'id' ], + model: AccountModel.unscoped(), + required: true, + include: [ + { + attributes: [ 'id' ], + model: VideoChannelModel.unscoped(), + required: true, + include: [ + { + attributes: [ 'id' ], + model: VideoModel.unscoped(), + required: true, + where: { + id: videoId + } + } + ] + } + ] + } + ], + transaction + } + + return ActorModel.unscoped().findOne(query) + } + + static isActorUrlExist (url: string) { + const query = { + raw: true, + where: { + url + } + } + + return ActorModel.unscoped().findOne(query) + .then(a => !!a) + } + + static listByFollowersUrls (followersUrls: string[], transaction?: Transaction): Bluebird { const query = { where: { followersUrl: { - [ Sequelize.Op.in ]: followersUrls + [ Op.in ]: followersUrls } }, transaction @@ -220,18 +341,19 @@ export class ActorModel extends Model { return ActorModel.scope(ScopeNames.FULL).findAll(query) } - static loadLocalByName (preferredUsername: string) { + static loadLocalByName (preferredUsername: string, transaction?: Transaction): Bluebird { const query = { where: { preferredUsername, serverId: null - } + }, + transaction } return ActorModel.scope(ScopeNames.FULL).findOne(query) } - static loadByNameAndHost (preferredUsername: string, host: string) { + static loadByNameAndHost (preferredUsername: string, host: string): Bluebird { const query = { where: { preferredUsername @@ -250,7 +372,30 @@ export class ActorModel extends Model { return ActorModel.scope(ScopeNames.FULL).findOne(query) } - static loadByUrl (url: string, transaction?: Sequelize.Transaction) { + static loadByUrl (url: string, transaction?: Transaction): Bluebird { + const query = { + where: { + url + }, + transaction, + include: [ + { + attributes: [ 'id' ], + model: AccountModel.unscoped(), + required: false + }, + { + attributes: [ 'id' ], + model: VideoChannelModel.unscoped(), + required: false + } + ] + } + + return ActorModel.unscoped().findOne(query) + } + + static loadByUrlAndPopulateAccountAndChannel (url: string, transaction?: Transaction): Bluebird { const query = { where: { url @@ -261,39 +406,47 @@ export class ActorModel extends Model { return ActorModel.scope(ScopeNames.FULL).findOne(query) } - toFormattedJSON () { + static incrementFollows (id: number, column: 'followersCount' | 'followingCount', by: number) { + return ActorModel.increment(column, { + by, + where: { + id + } + }) + } + + getSharedInbox (this: MActorWithInboxes) { + return this.sharedInboxUrl || this.inboxUrl + } + + toFormattedSummaryJSON (this: MActorSummaryFormattable) { let avatar: Avatar = null if (this.Avatar) { avatar = this.Avatar.toFormattedJSON() } - let score: number - if (this.Server) { - score = this.Server.score - } - return { - id: this.id, url: this.url, - uuid: this.uuid, + name: this.preferredUsername, host: this.getHost(), - score, - followingCount: this.followingCount, - followersCount: this.followersCount, avatar } } - toActivityPubObject (name: string, type: 'Account' | 'Application' | 'VideoChannel') { - let activityPubType - if (type === 'Account') { - activityPubType = 'Person' as 'Person' - } else if (type === 'Application') { - activityPubType = 'Application' as 'Application' - } else { // VideoChannel - activityPubType = 'Group' as 'Group' - } + toFormattedJSON (this: MActorFormattable) { + const base = this.toFormattedSummaryJSON() + return Object.assign(base, { + id: this.id, + hostRedundancyAllowed: this.getRedundancyAllowed(), + followingCount: this.followingCount, + followersCount: this.followersCount, + createdAt: this.createdAt, + updatedAt: this.updatedAt + }) + } + + toActivityPubObject (this: MActorAP, name: string) { let icon = undefined if (this.avatarId) { const extension = extname(this.Avatar.filename) @@ -305,10 +458,11 @@ export class ActorModel extends Model { } const json = { - type: activityPubType, + type: this.type, id: this.url, following: this.getFollowingUrl(), followers: this.getFollowersUrl(), + playlists: this.getPlaylistsUrl(), inbox: this.inboxUrl, outbox: this.outboxUrl, preferredUsername: this.preferredUsername, @@ -317,7 +471,6 @@ export class ActorModel extends Model { endpoints: { sharedInbox: this.sharedInboxUrl }, - uuid: this.uuid, publicKey: { id: this.getPublicKeyUrl(), owner: this.url, @@ -329,15 +482,17 @@ export class ActorModel extends Model { return activityPubContextify(json) } - getFollowerSharedInboxUrls (t: Sequelize.Transaction) { + getFollowerSharedInboxUrls (t: Transaction) { const query = { attributes: [ 'sharedInboxUrl' ], include: [ { - model: ActorFollowModel, + attribute: [], + model: ActorFollowModel.unscoped(), required: true, - as: 'followers', + as: 'ActorFollowing', where: { + state: 'accepted', targetActorId: this.id } } @@ -357,6 +512,10 @@ export class ActorModel extends Model { return this.url + '/followers' } + getPlaylistsUrl () { + return this.url + '/playlists' + } + getPublicKeyUrl () { return this.url + '#main-key' } @@ -365,28 +524,31 @@ export class ActorModel extends Model { return this.serverId === null } - getWebfingerUrl () { + getWebfingerUrl (this: MActorServer) { return 'acct:' + this.preferredUsername + '@' + this.getHost() } - getHost () { - return this.Server ? this.Server.host : CONFIG.WEBSERVER.HOST + getIdentifier () { + return this.Server ? `${this.preferredUsername}@${this.Server.host}` : this.preferredUsername + } + + getHost (this: MActorHost) { + return this.Server ? this.Server.host : WEBSERVER.HOST + } + + getRedundancyAllowed () { + return this.Server ? this.Server.redundancyAllowed : false } getAvatarUrl () { if (!this.avatarId) return undefined - return CONFIG.WEBSERVER.URL + this.Avatar.getWebserverPath() + return WEBSERVER.URL + this.Avatar.getStaticPath() } 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 + return isOutdated(this, ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL) } }