X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Factor%2Factor.ts;h=80a646c77f59e669119c61a8337a50d1534353a4;hb=5a05c14573ca3c0d16b77bef78d845f96c8c6497;hp=d7afa727d2e50185f4d4ef817f287c0ac3174e22;hpb=4638cd713dcdd007cd7f49b9a95fa62ac7823e7c;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/actor/actor.ts b/server/models/actor/actor.ts index d7afa727d..80a646c77 100644 --- a/server/models/actor/actor.ts +++ b/server/models/actor/actor.ts @@ -1,4 +1,4 @@ -import { literal, Op, QueryTypes, Transaction } from 'sequelize' +import { col, fn, literal, Op, QueryTypes, Transaction, where } from 'sequelize' import { AllowNull, BelongsTo, @@ -17,7 +17,7 @@ import { } 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 { 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' @@ -55,7 +55,7 @@ import { 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' @@ -65,7 +65,7 @@ enum ScopeNames { FULL = 'FULL' } -export const unusedActorAttributesForAPI = [ +export const unusedActorAttributesForAPI: (keyof AttributesOnly)[] = [ 'publicKey', 'privateKey', 'inboxUrl', @@ -130,7 +130,8 @@ export const unusedActorAttributesForAPI = [ unique: true }, { - fields: [ 'preferredUsername', 'serverId' ], + fields: [ fn('lower', col('preferredUsername')), 'serverId' ], + name: 'actor_preferred_username_lower_server_id', unique: true, where: { serverId: { @@ -139,7 +140,8 @@ export const unusedActorAttributesForAPI = [ } }, { - fields: [ 'preferredUsername' ], + fields: [ fn('lower', col('preferredUsername')) ], + name: 'actor_preferred_username_lower', unique: true, where: { serverId: null @@ -306,6 +308,33 @@ export class ActorModel extends Model>> { }) VideoChannel: VideoChannelModel + // --------------------------------------------------------------------------- + + 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 wherePreferredUsername (preferredUsername: string, colName = 'preferredUsername') { + return where(fn('lower', col(colName)), preferredUsername.toLowerCase()) + } + + // --------------------------------------------------------------------------- + static async load (id: number): Promise { const actorServer = await getServerActor() if (id === actorServer.id) return actorServer @@ -351,8 +380,12 @@ export class ActorModel extends Model>> { const fun = () => { const query = { where: { - preferredUsername, - serverId: null + [Op.and]: [ + this.wherePreferredUsername(preferredUsername, '"ActorModel"."preferredUsername"'), + { + serverId: null + } + ] }, transaction } @@ -374,8 +407,12 @@ export class ActorModel extends Model>> { const query = { attributes: [ 'url' ], where: { - preferredUsername, - serverId: null + [Op.and]: [ + this.wherePreferredUsername(preferredUsername), + { + serverId: null + } + ] }, transaction } @@ -384,7 +421,7 @@ export class ActorModel extends Model>> { } return ModelCache.Instance.doCache({ - cacheType: 'local-actor-name', + cacheType: 'local-actor-url', key: preferredUsername, // The server actor never change, so we can easily cache it whitelist: () => preferredUsername === SERVER_ACTOR_NAME, @@ -394,9 +431,7 @@ export class ActorModel extends Model>> { static loadByNameAndHost (preferredUsername: string, host: string): Promise { const query = { - where: { - preferredUsername - }, + where: this.wherePreferredUsername(preferredUsername, '"ActorModel"."preferredUsername"'), include: [ { model: ServerModel,