diff options
author | Chocobozzz <me@florianbigard.com> | 2023-05-11 16:16:27 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2023-05-11 16:16:27 +0200 |
commit | 85c20aaeb90ef0e0f44c377e62c323fde275cdde (patch) | |
tree | 79d09e1b6f1746398c2ea4f0279ac54fa2be96dd /server/models/actor/actor.ts | |
parent | 823c34c07fc0df81110098ee1032e9d3ed70b662 (diff) | |
download | PeerTube-85c20aaeb90ef0e0f44c377e62c323fde275cdde.tar.gz PeerTube-85c20aaeb90ef0e0f44c377e62c323fde275cdde.tar.zst PeerTube-85c20aaeb90ef0e0f44c377e62c323fde275cdde.zip |
Set actor preferred name case insensitive
Diffstat (limited to 'server/models/actor/actor.ts')
-rw-r--r-- | server/models/actor/actor.ts | 36 |
1 files changed, 25 insertions, 11 deletions
diff --git a/server/models/actor/actor.ts b/server/models/actor/actor.ts index 1432e8757..1524e0533 100644 --- a/server/models/actor/actor.ts +++ b/server/models/actor/actor.ts | |||
@@ -1,4 +1,4 @@ | |||
1 | import { literal, Op, QueryTypes, Transaction } from 'sequelize' | 1 | import { col, fn, literal, Op, QueryTypes, Transaction, where } from 'sequelize' |
2 | import { | 2 | import { |
3 | AllowNull, | 3 | AllowNull, |
4 | BelongsTo, | 4 | BelongsTo, |
@@ -130,7 +130,8 @@ export const unusedActorAttributesForAPI: (keyof AttributesOnly<ActorModel>)[] = | |||
130 | unique: true | 130 | unique: true |
131 | }, | 131 | }, |
132 | { | 132 | { |
133 | fields: [ 'preferredUsername', 'serverId' ], | 133 | fields: [ fn('lower', col('preferredUsername')), 'serverId' ], |
134 | name: 'actor_preferred_username_lower_server_id', | ||
134 | unique: true, | 135 | unique: true, |
135 | where: { | 136 | where: { |
136 | serverId: { | 137 | serverId: { |
@@ -139,7 +140,8 @@ export const unusedActorAttributesForAPI: (keyof AttributesOnly<ActorModel>)[] = | |||
139 | } | 140 | } |
140 | }, | 141 | }, |
141 | { | 142 | { |
142 | fields: [ 'preferredUsername' ], | 143 | fields: [ fn('lower', col('preferredUsername')) ], |
144 | name: 'actor_preferred_username_lower', | ||
143 | unique: true, | 145 | unique: true, |
144 | where: { | 146 | where: { |
145 | serverId: null | 147 | serverId: null |
@@ -327,6 +329,12 @@ export class ActorModel extends Model<Partial<AttributesOnly<ActorModel>>> { | |||
327 | 329 | ||
328 | // --------------------------------------------------------------------------- | 330 | // --------------------------------------------------------------------------- |
329 | 331 | ||
332 | static wherePreferredUsername (preferredUsername: string, colName = 'preferredUsername') { | ||
333 | return where(fn('lower', col(colName)), preferredUsername.toLowerCase()) | ||
334 | } | ||
335 | |||
336 | // --------------------------------------------------------------------------- | ||
337 | |||
330 | static async load (id: number): Promise<MActor> { | 338 | static async load (id: number): Promise<MActor> { |
331 | const actorServer = await getServerActor() | 339 | const actorServer = await getServerActor() |
332 | if (id === actorServer.id) return actorServer | 340 | if (id === actorServer.id) return actorServer |
@@ -372,8 +380,12 @@ export class ActorModel extends Model<Partial<AttributesOnly<ActorModel>>> { | |||
372 | const fun = () => { | 380 | const fun = () => { |
373 | const query = { | 381 | const query = { |
374 | where: { | 382 | where: { |
375 | preferredUsername, | 383 | [Op.and]: [ |
376 | serverId: null | 384 | this.wherePreferredUsername(preferredUsername), |
385 | { | ||
386 | serverId: null | ||
387 | } | ||
388 | ] | ||
377 | }, | 389 | }, |
378 | transaction | 390 | transaction |
379 | } | 391 | } |
@@ -395,8 +407,12 @@ export class ActorModel extends Model<Partial<AttributesOnly<ActorModel>>> { | |||
395 | const query = { | 407 | const query = { |
396 | attributes: [ 'url' ], | 408 | attributes: [ 'url' ], |
397 | where: { | 409 | where: { |
398 | preferredUsername, | 410 | [Op.and]: [ |
399 | serverId: null | 411 | this.wherePreferredUsername(preferredUsername), |
412 | { | ||
413 | serverId: null | ||
414 | } | ||
415 | ] | ||
400 | }, | 416 | }, |
401 | transaction | 417 | transaction |
402 | } | 418 | } |
@@ -405,7 +421,7 @@ export class ActorModel extends Model<Partial<AttributesOnly<ActorModel>>> { | |||
405 | } | 421 | } |
406 | 422 | ||
407 | return ModelCache.Instance.doCache({ | 423 | return ModelCache.Instance.doCache({ |
408 | cacheType: 'local-actor-name', | 424 | cacheType: 'local-actor-url', |
409 | key: preferredUsername, | 425 | key: preferredUsername, |
410 | // The server actor never change, so we can easily cache it | 426 | // The server actor never change, so we can easily cache it |
411 | whitelist: () => preferredUsername === SERVER_ACTOR_NAME, | 427 | whitelist: () => preferredUsername === SERVER_ACTOR_NAME, |
@@ -415,9 +431,7 @@ export class ActorModel extends Model<Partial<AttributesOnly<ActorModel>>> { | |||
415 | 431 | ||
416 | static loadByNameAndHost (preferredUsername: string, host: string): Promise<MActorFull> { | 432 | static loadByNameAndHost (preferredUsername: string, host: string): Promise<MActorFull> { |
417 | const query = { | 433 | const query = { |
418 | where: { | 434 | where: this.wherePreferredUsername(preferredUsername), |
419 | preferredUsername | ||
420 | }, | ||
421 | include: [ | 435 | include: [ |
422 | { | 436 | { |
423 | model: ServerModel, | 437 | model: ServerModel, |