X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Factivitypub%2Factor.ts;h=38a689fea5da9ac7481d3c5480c5b188077bdfd8;hb=57c36b277e68b764dd34cb2e449f6e2ca3d1e9b6;hp=ff5ab2e327a9fcdc1c256cb9715789b0a8397337;hpb=01de67b9a4fcdf01102ccc3cb7dc24beebf6c7ea;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/activitypub/actor.ts b/server/models/activitypub/actor.ts index ff5ab2e32..38a689fea 100644 --- a/server/models/activitypub/actor.ts +++ b/server/models/activitypub/actor.ts @@ -2,18 +2,35 @@ 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, + Default, + DefaultScope, + ForeignKey, + HasMany, + HasOne, + Is, + IsUUID, + 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_ACTOR_TYPES, CONFIG, CONSTRAINTS_FIELDS } from '../../initializers' +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 +58,11 @@ enum ScopeNames { [ScopeNames.FULL]: { include: [ { - model: () => AccountModel, + model: () => AccountModel.unscoped(), required: false }, { - model: () => VideoChannelModel, + model: () => VideoChannelModel.unscoped(), required: false }, { @@ -62,9 +79,21 @@ enum ScopeNames { @Table({ tableName: 'actor', indexes: [ + { + fields: [ 'url' ] + }, { fields: [ 'preferredUsername', 'serverId' ], unique: true + }, + { + fields: [ 'inboxUrl', 'sharedInboxUrl' ] + }, + { + fields: [ 'serverId' ] + }, + { + fields: [ 'avatarId' ] } ] }) @@ -149,7 +178,8 @@ export class ActorModel extends Model { foreignKey: { allowNull: true }, - onDelete: 'set null' + onDelete: 'set null', + hooks: true }) Avatar: AvatarModel @@ -160,17 +190,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 @@ -188,7 +218,8 @@ export class ActorModel extends Model { foreignKey: { allowNull: true }, - onDelete: 'cascade' + onDelete: 'cascade', + hooks: true }) Account: AccountModel @@ -196,12 +227,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) + return ActorModel.unscoped().findById(id) } static listByFollowersUrls (followersUrls: string[], transaction?: Sequelize.Transaction) { @@ -258,25 +290,72 @@ 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 + } + }) + } + + static async getActorsFollowerSharedInboxUrls (actors: ActorModel[], t: Sequelize.Transaction) { + const query = { + // attribute: [], + where: { + id: { + [Sequelize.Op.in]: actors.map(a => a.id) + } + }, + include: [ + { + // attributes: [ ], + model: ActorFollowModel.unscoped(), + required: true, + as: 'ActorFollowers', + where: { + state: 'accepted' + }, + include: [ + { + attributes: [ 'sharedInboxUrl' ], + model: ActorModel.unscoped(), + as: 'ActorFollower', + required: true + } + ] + } + ], + transaction: t + } + + const hash: { [ id: number ]: string[] } = {} + const res = await ActorModel.findAll(query) + for (const actor of res) { + hash[actor.id] = actor.ActorFollowers.map(follow => follow.ActorFollower.sharedInboxUrl) + } + + return hash + } + toFormattedJSON () { 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 + avatar, + createdAt: this.createdAt, + updatedAt: this.updatedAt } } @@ -330,10 +409,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 } } @@ -372,6 +453,17 @@ export class ActorModel extends Model { getAvatarUrl () { if (!this.avatarId) return undefined - return CONFIG.WEBSERVER.URL + this.Avatar.getWebserverPath + 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 } }