mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2026-07-07 09:51:37 +00:00
fix postgres old user id json lookup
This commit is contained in:
parent
6227a60fed
commit
15d2715a74
2 changed files with 58 additions and 12 deletions
|
|
@ -422,12 +422,24 @@ class User extends Model {
|
||||||
const cachedUser = userCache.getById(userId) || userCache.getByOldId(userId)
|
const cachedUser = userCache.getById(userId) || userCache.getByOldId(userId)
|
||||||
if (cachedUser) return cachedUser
|
if (cachedUser) return cachedUser
|
||||||
|
|
||||||
const idMatcher = this.sequelize.getDialect() === 'postgres' ? sequelize.where(sequelize.cast(sequelize.col('user.id'), 'text'), userId) : { id: userId }
|
const isUuid = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(userId)
|
||||||
|
if (isUuid) {
|
||||||
|
const byId = await this.findByPk(userId, {
|
||||||
|
include: this.sequelize.models.mediaProgress
|
||||||
|
})
|
||||||
|
if (byId) {
|
||||||
|
userCache.set(byId)
|
||||||
|
return byId
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const oldIdMatcher =
|
||||||
|
this.sequelize.getDialect() === 'postgres'
|
||||||
|
? sequelize.where(sequelize.literal(`"extraData"#>>'{oldUserId}'`), userId)
|
||||||
|
: { 'extraData.oldUserId': userId }
|
||||||
|
|
||||||
const user = await this.findOne({
|
const user = await this.findOne({
|
||||||
where: {
|
where: oldIdMatcher,
|
||||||
[sequelize.Op.or]: [idMatcher, { 'extraData.oldUserId': userId }]
|
|
||||||
},
|
|
||||||
include: this.sequelize.models.mediaProgress
|
include: this.sequelize.models.mediaProgress
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,5 @@
|
||||||
const { expect } = require('chai')
|
const { expect } = require('chai')
|
||||||
const sinon = require('sinon')
|
const sinon = require('sinon')
|
||||||
const sequelize = require('sequelize')
|
|
||||||
|
|
||||||
const User = require('../../../server/models/User')
|
const User = require('../../../server/models/User')
|
||||||
|
|
||||||
describe('User model', () => {
|
describe('User model', () => {
|
||||||
|
|
@ -17,7 +15,7 @@ describe('User model', () => {
|
||||||
sinon.restore()
|
sinon.restore()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should cast id to text on postgres when checking legacy token ids', async () => {
|
it('should resolve UUID ids via primary-key lookup on postgres', async () => {
|
||||||
User.sequelize = {
|
User.sequelize = {
|
||||||
getDialect: () => 'postgres',
|
getDialect: () => 'postgres',
|
||||||
models: {
|
models: {
|
||||||
|
|
@ -25,6 +23,46 @@ describe('User model', () => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const user = { id: 'e8e677b2-da16-4220-ab67-443b7714caf9' }
|
||||||
|
const findByPkStub = sinon.stub(User, 'findByPk').resolves(user)
|
||||||
|
const findOneStub = sinon.stub(User, 'findOne').resolves(null)
|
||||||
|
|
||||||
|
const result = await User.getUserByIdOrOldId('e8e677b2-da16-4220-ab67-443b7714caf9')
|
||||||
|
|
||||||
|
expect(result).to.equal(user)
|
||||||
|
expect(findByPkStub.calledOnce).to.equal(true)
|
||||||
|
expect(findOneStub.called).to.equal(false)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should query legacy oldUserId with postgres-safe JSON matcher', async () => {
|
||||||
|
User.sequelize = {
|
||||||
|
getDialect: () => 'postgres',
|
||||||
|
models: {
|
||||||
|
mediaProgress: {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const findByPkStub = sinon.stub(User, 'findByPk').resolves(null)
|
||||||
|
const findOneStub = sinon.stub(User, 'findOne').resolves(null)
|
||||||
|
|
||||||
|
await User.getUserByIdOrOldId('root')
|
||||||
|
|
||||||
|
expect(findByPkStub.called).to.equal(false)
|
||||||
|
expect(findOneStub.calledOnce).to.equal(true)
|
||||||
|
|
||||||
|
const options = findOneStub.firstCall.args[0]
|
||||||
|
expect(options.where.attribute.val).to.equal('"extraData"#>>\'{oldUserId}\'')
|
||||||
|
expect(options.where.logic).to.equal('root')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should keep sqlite oldUserId matcher unchanged', async () => {
|
||||||
|
User.sequelize = {
|
||||||
|
getDialect: () => 'sqlite',
|
||||||
|
models: {
|
||||||
|
mediaProgress: {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const findOneStub = sinon.stub(User, 'findOne').resolves(null)
|
const findOneStub = sinon.stub(User, 'findOne').resolves(null)
|
||||||
|
|
||||||
await User.getUserByIdOrOldId('root')
|
await User.getUserByIdOrOldId('root')
|
||||||
|
|
@ -32,11 +70,7 @@ describe('User model', () => {
|
||||||
expect(findOneStub.calledOnce).to.equal(true)
|
expect(findOneStub.calledOnce).to.equal(true)
|
||||||
|
|
||||||
const options = findOneStub.firstCall.args[0]
|
const options = findOneStub.firstCall.args[0]
|
||||||
const whereOr = options.where[sequelize.Op.or]
|
expect(options.where).to.deep.equal({ 'extraData.oldUserId': 'root' })
|
||||||
|
|
||||||
expect(whereOr).to.have.length(2)
|
|
||||||
expect(whereOr[0].attribute.type).to.equal('text')
|
|
||||||
expect(whereOr[1]).to.deep.equal({ 'extraData.oldUserId': 'root' })
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue