1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-24 10:27:10 +02:00

Chore: Cleaned up server UserItem interface

This commit is contained in:
Laurent Cozic 2021-09-25 19:51:44 +01:00
parent 8ada059401
commit 9f17b28f85
9 changed files with 18 additions and 29 deletions

View File

@ -2,7 +2,7 @@ const { setupDatabaseAndSynchronizer, switchClient } = require('../testing/test-
const Folder = require('../models/Folder').default;
const Note = require('../models/Note').default;
describe('models_BaseItem', function() {
describe('models/BaseItem', function() {
beforeEach(async (done) => {
await setupDatabaseAndSynchronizer(1);

View File

@ -9,7 +9,7 @@ import ResourceService from '../services/ResourceService';
const testImagePath = `${supportDir}/photo.jpg`;
describe('models_Folder.sharing', function() {
describe('models/Folder.sharing', function() {
beforeEach(async (done) => {
await setupDatabaseAndSynchronizer(1);

View File

@ -9,7 +9,7 @@ async function allItems() {
return folders.concat(notes);
}
describe('models_Folder', function() {
describe('models/Folder', function() {
beforeEach(async (done) => {
await setupDatabaseAndSynchronizer(1);

View File

@ -15,7 +15,7 @@ async function allItems() {
return folders.concat(notes);
}
describe('models_Note', function() {
describe('models/Note', function() {
beforeEach(async (done) => {
await setupDatabaseAndSynchronizer(1);
await switchClient(1);

View File

@ -3,7 +3,7 @@ const { setupDatabaseAndSynchronizer, switchClient } = require('../testing/test-
const Folder = require('../models/Folder').default;
const Note = require('../models/Note').default;
describe('models_Note_CustomSortOrder', function() {
describe('models/Note_CustomSortOrder', function() {
beforeEach(async (done) => {
await setupDatabaseAndSynchronizer(1);
await switchClient(1);

View File

@ -6,7 +6,7 @@ const shim = require('../shim').default;
const testImagePath = `${supportDir}/photo.jpg`;
describe('models_Resource', function() {
describe('models/Resource', function() {
beforeEach(async (done) => {
await setupDatabaseAndSynchronizer(1);

View File

@ -7,7 +7,7 @@ async function loadSettingsFromFile(): Promise<any> {
return JSON.parse(await fs.readFile(Setting.settingFilePath, 'utf8'));
}
describe('models_Setting', function() {
describe('models/Setting', function() {
beforeEach(async (done) => {
await setupDatabaseAndSynchronizer(1);
@ -174,7 +174,7 @@ describe('models_Setting', function() {
}));
it('should not save to file if nothing has changed', (async () => {
Setting.setValue('sync.target', 9);
Setting.setValue('sync.mobileWifiOnly', true);
await Setting.saveAll();
{
@ -182,7 +182,7 @@ describe('models_Setting', function() {
// changed.
const beforeStat = await fs.stat(Setting.settingFilePath);
await msleep(1001);
Setting.setValue('sync.target', 8);
Setting.setValue('sync.mobileWifiOnly', false);
await Setting.saveAll();
const afterStat = await fs.stat(Setting.settingFilePath);
expect(afterStat.mtime.getTime()).toBeGreaterThan(beforeStat.mtime.getTime());
@ -191,7 +191,7 @@ describe('models_Setting', function() {
{
const beforeStat = await fs.stat(Setting.settingFilePath);
await msleep(1001);
Setting.setValue('sync.target', 8);
Setting.setValue('sync.mobileWifiOnly', false);
const afterStat = await fs.stat(Setting.settingFilePath);
await Setting.saveAll();
expect(afterStat.mtime.getTime()).toBe(beforeStat.mtime.getTime());

View File

@ -3,7 +3,7 @@ const Folder = require('../models/Folder').default;
const Note = require('../models/Note').default;
const Tag = require('../models/Tag').default;
describe('models_Tag', function() {
describe('models/Tag', function() {
beforeEach(async (done) => {
await setupDatabaseAndSynchronizer(1);

View File

@ -28,13 +28,6 @@ export default class UserItemModel extends BaseModel<UserItem> {
return false;
}
public async add(userId: Uuid, itemId: Uuid): Promise<UserItem> {
return this.save({
user_id: userId,
item_id: itemId,
});
}
public async remove(userId: Uuid, itemId: Uuid): Promise<void> {
await this.deleteByUserItem(userId, itemId);
}
@ -59,7 +52,6 @@ export default class UserItemModel extends BaseModel<UserItem> {
.leftJoin('items', 'user_items.item_id', 'items.id')
.select(this.selectFields(options, this.defaultFields, 'user_items'))
.where('items.jop_share_id', '=', shareId);
// return this.db(this.tableName).select(this.defaultFields).where('share_id', '=', shareId);
}
public async byShareAndUserId(shareId: Uuid, userId: Uuid, options: LoadOptions = {}): Promise<UserItem[]> {
@ -69,10 +61,6 @@ export default class UserItemModel extends BaseModel<UserItem> {
.select(this.selectFields(options, this.defaultFields, 'user_items'))
.where('items.jop_share_id', '=', shareId)
.where('user_items.user_id', '=', userId);
// return this.db(this.tableName).select(this.defaultFields)
// .where('share_id', '=', shareId)
// .where('user_id', '=', userId);
}
public async byUserId(userId: Uuid): Promise<UserItem[]> {
@ -91,7 +79,6 @@ export default class UserItemModel extends BaseModel<UserItem> {
.select(this.selectFields(options, this.defaultFields, 'user_items'))
.where('items.jop_share_id', '!=', '')
.where('user_items.user_id', '=', userId);
// return this.db(this.tableName).select(this.defaultFields).where('share_id', '!=', '').where('user_id', '=', userId);
}
public async deleteByUserItem(userId: Uuid, itemId: Uuid): Promise<void> {
@ -124,6 +111,11 @@ export default class UserItemModel extends BaseModel<UserItem> {
await this.deleteBy({ byShareId: shareId, byUserId: userId });
}
public async add(userId: Uuid, itemId: Uuid, options: SaveOptions = {}): Promise<void> {
const item = await this.models().item().load(itemId, { fields: ['id', 'name'] });
await this.addMulti(userId, [item], options);
}
public async addMulti(userId: Uuid, itemsQuery: Knex.QueryBuilder | Item[], options: SaveOptions = {}): Promise<void> {
const items: Item[] = Array.isArray(itemsQuery) ? itemsQuery : await itemsQuery.whereNotIn('id', this.db('user_items').select('item_id').where('user_id', '=', userId));
if (!items.length) return;
@ -151,11 +143,8 @@ export default class UserItemModel extends BaseModel<UserItem> {
}, 'UserItemModel::addMulti');
}
public async save(userItem: UserItem, options: SaveOptions = {}): Promise<UserItem> {
if (userItem.id) throw new Error('User items cannot be modified (only created or deleted)'); // Sanity check - shouldn't happen
const item = await this.models().item().load(userItem.item_id, { fields: ['id', 'name'] });
await this.addMulti(userItem.user_id, [item], options);
return this.byUserAndItemId(userItem.user_id, item.id);
public async save(_userItem: UserItem, _options: SaveOptions = {}): Promise<UserItem> {
throw new Error('Call add() or addMulti()');
}
public async delete(_id: string | string[], _options: DeleteOptions = {}): Promise<void> {