X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Factor%2Factor.ts;h=1432e87574468e493a8d9ec3e225c4c6b927e4dd;hb=5e47f6ab984a7d00782e4c7030afffa1ba480add;hp=08cb2fd2406bb9020c19d901e26751bbf82732ff;hpb=ab4b8974997777373a6032073f9c1aaf33ba9931;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/actor/actor.ts b/server/models/actor/actor.ts index 08cb2fd24..1432e8757 100644 --- a/server/models/actor/actor.ts +++ b/server/models/actor/actor.ts @@ -1,5 +1,4 @@ -import { values } from 'lodash' -import { literal, Op, Transaction } from 'sequelize' +import { literal, Op, QueryTypes, Transaction } from 'sequelize' import { AllowNull, BelongsTo, @@ -16,12 +15,12 @@ import { Table, UpdatedAt } from 'sequelize-typescript' +import { activityPubContextify } from '@server/lib/activitypub/context' import { getBiggestActorImage } from '@server/lib/actor-image' -import { ModelCache } from '@server/models/model-cache' -import { getLowercaseExtension } from '@shared/core-utils' +import { ModelCache } from '@server/models/shared/model-cache' +import { forceNumber, getLowercaseExtension } from '@shared/core-utils' import { ActivityIconObject, ActivityPubActorType, ActorImageType } from '@shared/models' import { AttributesOnly } from '@shared/typescript-utils' -import { activityPubContextify } from '../../helpers/activitypub' import { isActorFollowersCountValid, isActorFollowingCountValid, @@ -43,17 +42,20 @@ import { MActorAccountChannelId, MActorAPAccount, MActorAPChannel, + MActorFollowersUrl, MActorFormattable, MActorFull, MActorHost, + MActorId, MActorServer, MActorSummaryFormattable, MActorUrl, MActorWithInboxes } from '../../types/models' import { AccountModel } from '../account/account' +import { getServerActor } from '../application/application' import { ServerModel } from '../server/server' -import { isOutdated, throwIfNotValid } from '../utils' +import { buildSQLAttributes, isOutdated, throwIfNotValid } from '../shared' import { VideoModel } from '../video/video' import { VideoChannelModel } from '../video/video-channel' import { ActorFollowModel } from './actor-follow' @@ -63,7 +65,7 @@ enum ScopeNames { FULL = 'FULL' } -export const unusedActorAttributesForAPI = [ +export const unusedActorAttributesForAPI: (keyof AttributesOnly)[] = [ 'publicKey', 'privateKey', 'inboxUrl', @@ -160,7 +162,7 @@ export const unusedActorAttributesForAPI = [ export class ActorModel extends Model>> { @AllowNull(false) - @Column(DataType.ENUM(...values(ACTIVITY_PUB_ACTOR_TYPES))) + @Column(DataType.ENUM(...Object.values(ACTIVITY_PUB_ACTOR_TYPES))) type: ActivityPubActorType @AllowNull(false) @@ -304,7 +306,31 @@ export class ActorModel extends Model>> { }) VideoChannel: VideoChannelModel - static load (id: number): Promise { + // --------------------------------------------------------------------------- + + static getSQLAttributes (tableName: string, aliasPrefix = '') { + return buildSQLAttributes({ + model: this, + tableName, + aliasPrefix + }) + } + + static getSQLAPIAttributes (tableName: string, aliasPrefix = '') { + return buildSQLAttributes({ + model: this, + tableName, + aliasPrefix, + excludeAttributes: unusedActorAttributesForAPI + }) + } + + // --------------------------------------------------------------------------- + + static async load (id: number): Promise { + const actorServer = await getServerActor() + if (id === actorServer.id) return actorServer + return ActorModel.unscoped().findByPk(id) } @@ -312,48 +338,21 @@ export class ActorModel extends Model>> { return ActorModel.scope(ScopeNames.FULL).findByPk(id) } - static loadFromAccountByVideoId (videoId: number, transaction: Transaction): Promise { - 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 - } - } - ] - } - ] - } - ], + static loadAccountActorFollowerUrlByVideoId (videoId: number, transaction: Transaction) { + const query = `SELECT "actor"."id" AS "id", "actor"."followersUrl" AS "followersUrl" ` + + `FROM "actor" ` + + `INNER JOIN "account" ON "actor"."id" = "account"."actorId" ` + + `INNER JOIN "videoChannel" ON "videoChannel"."accountId" = "account"."id" ` + + `INNER JOIN "video" ON "video"."channelId" = "videoChannel"."id" AND "video"."id" = :videoId` + + const options = { + type: QueryTypes.SELECT as QueryTypes.SELECT, + replacements: { videoId }, + plain: true as true, transaction } - return ActorModel.unscoped().findOne(query) - } - - static isActorUrlExist (url: string) { - const query = { - raw: true, - where: { - url - } - } - - return ActorModel.unscoped().findOne(query) - .then(a => !!a) + return ActorModel.sequelize.query(query, options) } static listByFollowersUrls (followersUrls: string[], transaction?: Transaction): Promise { @@ -468,7 +467,7 @@ export class ActorModel extends Model>> { } static rebuildFollowsCount (ofId: number, type: 'followers' | 'following', transaction?: Transaction) { - const sanitizedOfId = parseInt(ofId + '', 10) + const sanitizedOfId = forceNumber(ofId) const where = { id: sanitizedOfId } let columnToUpdate: string @@ -483,7 +482,7 @@ export class ActorModel extends Model>> { } return ActorModel.update({ - [columnToUpdate]: literal(`(SELECT COUNT(*) FROM "actorFollow" WHERE "${columnOfCount}" = ${sanitizedOfId})`) + [columnToUpdate]: literal(`(SELECT COUNT(*) FROM "actorFollow" WHERE "${columnOfCount}" = ${sanitizedOfId} AND "state" = 'accepted')`) }, { where, transaction }) } @@ -605,7 +604,7 @@ export class ActorModel extends Model>> { image } - return activityPubContextify(json) + return activityPubContextify(json, 'Actor') } getFollowerSharedInboxUrls (t: Transaction) {