From b528c4aa6350f7316aa7116540fdd9e9fc02a99a Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Tue, 23 Nov 2021 15:17:12 -0600 Subject: [PATCH] Contexts: check for cyclical in_reply_tos and prevent stitching them with tombstones --- app/soapbox/reducers/contexts.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/app/soapbox/reducers/contexts.js b/app/soapbox/reducers/contexts.js index 9bcf87b9a..d6780c124 100644 --- a/app/soapbox/reducers/contexts.js +++ b/app/soapbox/reducers/contexts.js @@ -39,7 +39,25 @@ const importStatuses = (state, statuses) => { }); }; +const isReplyTo = (state, childId, parentId, initialId = null) => { + if (!childId) return false; + + // Prevent cycles + if (childId === initialId) return false; + initialId = initialId || childId; + + if (childId === parentId) { + return true; + } else { + const nextId = state.getIn(['inReplyTos', childId]); + return isReplyTo(state, nextId, parentId, initialId); + } +}; + const insertTombstone = (state, ancestorId, descendantId) => { + // Prevent infinite loop if the API returns a bogus response + if (isReplyTo(state, ancestorId, descendantId)) return state; + const tombstoneId = `${descendantId}-tombstone`; return state.withMutations(state => { importStatus(state, { id: tombstoneId, in_reply_to_id: ancestorId });