mirror of
https://github.com/laurent22/joplin.git
synced 2025-01-11 18:24:43 +02:00
Server: Allow filtering users with replication
This commit is contained in:
parent
d49b2ec0e9
commit
13116fec76
30
packages/server/src/models/BaseModel.test.ts
Normal file
30
packages/server/src/models/BaseModel.test.ts
Normal file
@ -0,0 +1,30 @@
|
||||
import { beforeAllDb, afterAllTests, beforeEachDb, models } from '../utils/testing/testUtils';
|
||||
|
||||
describe('BaseModel', () => {
|
||||
|
||||
beforeAll(async () => {
|
||||
await beforeAllDb('BaseModel');
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await afterAllTests();
|
||||
});
|
||||
|
||||
beforeEach(async () => {
|
||||
await beforeEachDb();
|
||||
});
|
||||
|
||||
test('should check if a user has replication', async () => {
|
||||
// cSpell:disable
|
||||
const itemModel = models().item();
|
||||
itemModel.usersWithReplication_ = ['A', 'B', 'EYE1m66mGmA01sDDDDKE19'];
|
||||
expect(itemModel.isUserWithReplication('AAAAAAAAAAAA')).toBe(true);
|
||||
expect(itemModel.isUserWithReplication('AAAAAAAAAAAAEEEEEEE')).toBe(true);
|
||||
expect(itemModel.isUserWithReplication('bbbbbbbb')).toBe(false);
|
||||
expect(itemModel.isUserWithReplication('BBBBBBBBBB')).toBe(true);
|
||||
expect(itemModel.isUserWithReplication('')).toBe(false);
|
||||
expect(itemModel.isUserWithReplication('EYE1m66mGmA01sDDDDKE19')).toBe(true);
|
||||
// cSpell:enable
|
||||
});
|
||||
|
||||
});
|
@ -117,8 +117,19 @@ export default abstract class BaseModel<T> {
|
||||
return this.db_;
|
||||
}
|
||||
|
||||
public isUserWithReplication(userId: Uuid) {
|
||||
if (!userId) return false;
|
||||
|
||||
for (const id of this.usersWithReplication_) {
|
||||
if (id === userId) return true;
|
||||
if (id.length < 22 && userId.startsWith(id)) return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public dbSlave(userId: Uuid = ''): DbConnection {
|
||||
if (userId && this.usersWithReplication_.includes(userId)) {
|
||||
if (this.isUserWithReplication(userId)) {
|
||||
logger.info(`Using slave database for user: ${userId}`);
|
||||
return this.dbSlave_;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user