aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/actor
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2023-05-11 16:16:27 +0200
committerChocobozzz <me@florianbigard.com>2023-05-11 16:16:27 +0200
commit85c20aaeb90ef0e0f44c377e62c323fde275cdde (patch)
tree79d09e1b6f1746398c2ea4f0279ac54fa2be96dd /server/models/actor
parent823c34c07fc0df81110098ee1032e9d3ed70b662 (diff)
downloadPeerTube-85c20aaeb90ef0e0f44c377e62c323fde275cdde.tar.gz
PeerTube-85c20aaeb90ef0e0f44c377e62c323fde275cdde.tar.zst
PeerTube-85c20aaeb90ef0e0f44c377e62c323fde275cdde.zip
Set actor preferred name case insensitive
Diffstat (limited to 'server/models/actor')
-rw-r--r--server/models/actor/actor-follow.ts22
-rw-r--r--server/models/actor/actor.ts36
2 files changed, 31 insertions, 27 deletions
diff --git a/server/models/actor/actor-follow.ts b/server/models/actor/actor-follow.ts
index 32e5d78b0..0f199d208 100644
--- a/server/models/actor/actor-follow.ts
+++ b/server/models/actor/actor-follow.ts
@@ -37,8 +37,8 @@ import { logger } from '../../helpers/logger'
37import { ACTOR_FOLLOW_SCORE, CONSTRAINTS_FIELDS, FOLLOW_STATES, SERVER_ACTOR_NAME, SORTABLE_COLUMNS } from '../../initializers/constants' 37import { ACTOR_FOLLOW_SCORE, CONSTRAINTS_FIELDS, FOLLOW_STATES, SERVER_ACTOR_NAME, SORTABLE_COLUMNS } from '../../initializers/constants'
38import { AccountModel } from '../account/account' 38import { AccountModel } from '../account/account'
39import { ServerModel } from '../server/server' 39import { ServerModel } from '../server/server'
40import { doesExist } from '../shared/query'
41import { buildSQLAttributes, createSafeIn, getSort, searchAttribute, throwIfNotValid } from '../shared' 40import { buildSQLAttributes, createSafeIn, getSort, searchAttribute, throwIfNotValid } from '../shared'
41import { doesExist } from '../shared/query'
42import { VideoChannelModel } from '../video/video-channel' 42import { VideoChannelModel } from '../video/video-channel'
43import { ActorModel, unusedActorAttributesForAPI } from './actor' 43import { ActorModel, unusedActorAttributesForAPI } from './actor'
44import { InstanceListFollowersQueryBuilder, ListFollowersOptions } from './sql/instance-list-followers-query-builder' 44import { InstanceListFollowersQueryBuilder, ListFollowersOptions } from './sql/instance-list-followers-query-builder'
@@ -265,9 +265,7 @@ export class ActorFollowModel extends Model<Partial<AttributesOnly<ActorFollowMo
265 model: ActorModel, 265 model: ActorModel,
266 required: true, 266 required: true,
267 as: 'ActorFollowing', 267 as: 'ActorFollowing',
268 where: { 268 where: ActorModel.wherePreferredUsername(targetName),
269 preferredUsername: targetName
270 },
271 include: [ 269 include: [
272 { 270 {
273 model: VideoChannelModel.unscoped(), 271 model: VideoChannelModel.unscoped(),
@@ -313,24 +311,16 @@ export class ActorFollowModel extends Model<Partial<AttributesOnly<ActorFollowMo
313 if (t.host) { 311 if (t.host) {
314 return { 312 return {
315 [Op.and]: [ 313 [Op.and]: [
316 { 314 ActorModel.wherePreferredUsername(t.name, '$preferredUsername$'),
317 $preferredUsername$: t.name 315 { $host$: t.host }
318 },
319 {
320 $host$: t.host
321 }
322 ] 316 ]
323 } 317 }
324 } 318 }
325 319
326 return { 320 return {
327 [Op.and]: [ 321 [Op.and]: [
328 { 322 ActorModel.wherePreferredUsername(t.name, '$preferredUsername$'),
329 $preferredUsername$: t.name 323 { $serverId$: null }
330 },
331 {
332 $serverId$: null
333 }
334 ] 324 ]
335 } 325 }
336 }) 326 })
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 @@
1import { literal, Op, QueryTypes, Transaction } from 'sequelize' 1import { col, fn, literal, Op, QueryTypes, Transaction, where } from 'sequelize'
2import { 2import {
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,