Skip to content

Commit 9fc819e

Browse files
authored
Also unsubscribe from any children (#2120)
1 parent 838418a commit 9fc819e

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

api/resolvers/item.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -838,8 +838,16 @@ export default {
838838
const data = { itemId: Number(id), userId: me.id }
839839
const old = await models.threadSubscription.findUnique({ where: { userId_itemId: data } })
840840
if (old) {
841-
await models.threadSubscription.delete({ where: { userId_itemId: data } })
842-
} else await models.threadSubscription.create({ data })
841+
await models.$executeRaw`
842+
DELETE FROM "ThreadSubscription" ts
843+
USING "Item" i
844+
WHERE ts."userId" = ${me.id}
845+
AND i.path <@ (SELECT path FROM "Item" WHERE id = ${Number(id)})
846+
AND ts."itemId" = i.id
847+
`
848+
} else {
849+
await models.threadSubscription.create({ data })
850+
}
843851
return { id }
844852
},
845853
deleteItem: async (parent, { id }, { me, models }) => {

components/subscribe.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,25 @@ export default function SubscribeDropdownItem ({ item: { id, meSubscription } })
2020
},
2121
optimistic: true
2222
})
23+
24+
const unsubscribed = !subscribeItem.meSubscription
25+
if (!unsubscribed) return
26+
27+
const cacheState = cache.extract()
28+
Object.keys(cacheState)
29+
.filter(key => key.startsWith('Item:'))
30+
.forEach(key => {
31+
cache.modify({
32+
id: key,
33+
fields: {
34+
meSubscription: (existing, { readField }) => {
35+
const path = readField('path')
36+
return !path || !path.includes(id) ? existing : false
37+
}
38+
},
39+
optimistic: true
40+
})
41+
})
2342
}
2443
}
2544
)

0 commit comments

Comments
 (0)