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

Server: Fixed sharing issue when a user no longer has a user item associated with their account

This commit is contained in:
Laurent Cozic 2023-02-09 18:23:27 +00:00
parent c5b551bbcb
commit 293f621e46
3 changed files with 48 additions and 3 deletions

View File

@ -219,7 +219,15 @@ export default class ShareModel extends BaseModel<Share> {
const shareUserIds = await this.allShareUserIds(previousShare);
for (const shareUserId of shareUserIds) {
if (shareUserId === change.user_id) continue;
await removeUserItem(shareUserId, item.id);
try {
await removeUserItem(shareUserId, item.id);
} catch (error) {
if (error.httpCode === ErrorNotFound.httpCode) {
logger.warn('Could not remove a user item because it has already been removed:', error);
} else {
throw error;
}
}
}
}

View File

@ -82,10 +82,10 @@ export default class UserItemModel extends BaseModel<UserItem> {
.where('user_items.user_id', '=', userId);
}
public async deleteByUserItem(userId: Uuid, itemId: Uuid): Promise<void> {
public async deleteByUserItem(userId: Uuid, itemId: Uuid, options: UserItemDeleteOptions = null): Promise<void> {
const userItem = await this.byUserAndItemId(userId, itemId);
if (!userItem) throw new ErrorNotFound(`No such user_item: ${userId} / ${itemId}`);
await this.deleteBy({ byUserItem: userItem });
await this.deleteBy({ ...options, byUserItem: userItem });
}
public async deleteByItemIds(itemIds: Uuid[], options: UserItemDeleteOptions = null): Promise<void> {

View File

@ -414,6 +414,43 @@ describe('shares.folder', function() {
await expectNotThrow(async () => await models().share().updateSharedItems3());
});
test('should not throw an error if a user no longer has a user item, and the target share changes', async function() {
const { user: user1, session: session1 } = await createUserAndSession(1);
const { user: user2, session: session2 } = await createUserAndSession(2);
await shareFolderWithUser(session1.id, session2.id, '000000000000000000000000000000F1', [
{
id: '000000000000000000000000000000F1',
children: [
{
id: '00000000000000000000000000000001',
},
],
},
]);
const { share: share2 } = await shareFolderWithUser(session1.id, session2.id, '000000000000000000000000000000F5', [
{
id: '000000000000000000000000000000F5',
children: [],
},
]);
const noteItem = await models().item().loadByJopId(user1.id, '00000000000000000000000000000001');
// Note is moved from a shared folder to a different shared folder
await models().item().saveForUser(user1.id, {
id: noteItem.id,
jop_parent_id: '000000000000000000000000000000F5',
jop_share_id: share2.id,
});
await models().userItem().deleteByUserItem(user2.id, noteItem.id, { recordChanges: false });
await expectNotThrow(async () => await models().share().updateSharedItems3());
});
test('should unshare a deleted item', async function() {
const { user: user1, session: session1 } = await createUserAndSession(1);
const { user: user2, session: session2 } = await createUserAndSession(2);