|
| 1 | +import { differenceInMilliseconds } from 'date-fns'; |
| 2 | + |
1 | 3 | import { logError } from '../../shared/logger'; |
2 | 4 | import type { Link, SettingsState } from '../types'; |
3 | 5 | import type { |
@@ -181,7 +183,13 @@ async function getGitifySubjectForDiscussion( |
181 | 183 | return null; |
182 | 184 | } |
183 | 185 |
|
184 | | - const latestDiscussionComment = getLatestDiscussionComment( |
| 186 | + // Return early if this notification would be hidden by filters |
| 187 | + if (isStateFilteredOut(discussionState, settings)) { |
| 188 | + return null; |
| 189 | + } |
| 190 | + |
| 191 | + const latestDiscussionComment = getClosestDiscussionCommentOrReply( |
| 192 | + notification, |
185 | 193 | discussion.comments.nodes, |
186 | 194 | ); |
187 | 195 |
|
@@ -209,20 +217,33 @@ async function getGitifySubjectForDiscussion( |
209 | 217 | }; |
210 | 218 | } |
211 | 219 |
|
212 | | -export function getLatestDiscussionComment( |
| 220 | +export function getClosestDiscussionCommentOrReply( |
| 221 | + notification: Notification, |
213 | 222 | comments: DiscussionComment[], |
214 | 223 | ): DiscussionComment | null { |
215 | 224 | if (!comments || comments.length === 0) { |
216 | 225 | return null; |
217 | 226 | } |
218 | 227 |
|
219 | | - // Return latest reply if available |
220 | | - if (comments[0].replies.nodes.length === 1) { |
221 | | - return comments[0].replies.nodes[0]; |
222 | | - } |
| 228 | + const targetTimestamp = notification.updated_at; |
| 229 | + |
| 230 | + const allCommentsAndReplies = comments.flatMap((comment) => [ |
| 231 | + comment, |
| 232 | + ...comment.replies.nodes, |
| 233 | + ]); |
| 234 | + |
| 235 | + // Find the closest match using the target timestamp |
| 236 | + const closestComment = allCommentsAndReplies.reduce((prev, curr) => { |
| 237 | + const prevDiff = Math.abs( |
| 238 | + differenceInMilliseconds(prev.createdAt, targetTimestamp), |
| 239 | + ); |
| 240 | + const currDiff = Math.abs( |
| 241 | + differenceInMilliseconds(curr.createdAt, targetTimestamp), |
| 242 | + ); |
| 243 | + return currDiff < prevDiff ? curr : prev; |
| 244 | + }, allCommentsAndReplies[0]); |
223 | 245 |
|
224 | | - // Return latest comment if no replies |
225 | | - return comments[0]; |
| 246 | + return closestComment; |
226 | 247 | } |
227 | 248 |
|
228 | 249 | async function getGitifySubjectForIssue( |
|
0 commit comments