From 107f2e128ee40aaac9793a06df0622de72400363 Mon Sep 17 00:00:00 2001 From: Laurent Cozic Date: Thu, 9 Feb 2023 19:42:16 +0000 Subject: [PATCH] Server: Clean up share logic --- packages/server/src/models/ShareModel.ts | 31 ++++++++++++------------ 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/packages/server/src/models/ShareModel.ts b/packages/server/src/models/ShareModel.ts index 97b1693e0..dba25efe0 100644 --- a/packages/server/src/models/ShareModel.ts +++ b/packages/server/src/models/ShareModel.ts @@ -199,6 +199,20 @@ export default class ShareModel extends BaseModel { const handleCreated = async (change: Change, item: Item, share: Share) => { if (!item.jop_share_id) return; + // When a folder is unshared, the share object is deleted, then all + // items that were shared get their 'share_id' property set to an + // empty string. This is all done client side. + // + // However it means that if a share object is deleted but the items + // are not synced, we'll find items that are associated with a share + // that no longer exists. This is fine, but we need to handle it + // properly below, otherwise the share update process will fail. + + if (!share) { + logger.warn(`Found an item (${item.id}) associated with a share that no longer exists (${item.jop_share_id}) - skipping it`); + return; + } + const shareUserIds = await this.allShareUserIds(share); for (const shareUserId of shareUserIds) { if (shareUserId === change.user_id) continue; @@ -301,25 +315,10 @@ export default class ShareModel extends BaseModel { // Item associated with the change may have been // deleted, so take this into account. if (item) { - // When a folder is unshared, the share object is - // deleted, then all items that were shared get their - // 'share_id' property set to an empty string. This is - // all done client side. - // - // However it means that if a share object is deleted - // but the items are not synced, we'll find items that - // are associated with a share that no longer exists. - // This is fine, but we need to handle it properly - // below, otherwise the share update process will fail. - const itemShare = shares.find(s => s.id === item.jop_share_id); if (change.type === ChangeType.Create) { - if (!itemShare) { - logger.warn(`Found an item (${item.id}) associated with a share that no longer exists (${item.jop_share_id}) - skipping it`); - } else { - await handleCreated(change, item, itemShare); - } + await handleCreated(change, item, itemShare); } if (change.type === ChangeType.Update) {