diff --git a/api/resolvers/item.js b/api/resolvers/item.js index 7e09c97f..720d4ca9 100644 --- a/api/resolvers/item.js +++ b/api/resolvers/item.js @@ -838,8 +838,16 @@ export default { const data = { itemId: Number(id), userId: me.id } const old = await models.threadSubscription.findUnique({ where: { userId_itemId: data } }) if (old) { - await models.threadSubscription.delete({ where: { userId_itemId: data } }) - } else await models.threadSubscription.create({ data }) + await models.$executeRaw` + DELETE FROM "ThreadSubscription" ts + USING "Item" i + WHERE ts."userId" = ${me.id} + AND i.path <@ (SELECT path FROM "Item" WHERE id = ${Number(id)}) + AND ts."itemId" = i.id + ` + } else { + await models.threadSubscription.create({ data }) + } return { id } }, deleteItem: async (parent, { id }, { me, models }) => { diff --git a/components/subscribe.js b/components/subscribe.js index c800f381..f75a0b77 100644 --- a/components/subscribe.js +++ b/components/subscribe.js @@ -20,6 +20,25 @@ export default function SubscribeDropdownItem ({ item: { id, meSubscription } }) }, optimistic: true }) + + const unsubscribed = !subscribeItem.meSubscription + if (!unsubscribed) return + + const cacheState = cache.extract() + Object.keys(cacheState) + .filter(key => key.startsWith('Item:')) + .forEach(key => { + cache.modify({ + id: key, + fields: { + meSubscription: (existing, { readField }) => { + const path = readField('path') + return !path || !path.includes(id) ? existing : false + } + }, + optimistic: true + }) + }) } } )