deduplicatePosts static method

List<Post> deduplicatePosts(
  1. Iterable<Post> posts, {
  2. int timeThresholdMs = 120000,
})

Filters a list of posts, removing likely duplicate posts while preserving order. If a duplicate is found, the one with more engagement (likes/comments) is preserved.

Implementation

static List<Post> deduplicatePosts(Iterable<Post> posts, {int timeThresholdMs = 120000}) {
  final List<Post> unique = [];
  for (final post in posts) {
    final existingIndex = unique.indexWhere(
      (u) => areLikelyDuplicates(u, post, timeThresholdMs: timeThresholdMs),
    );
    if (existingIndex == -1) {
      unique.add(post);
    } else {
      // Keep the one with more engagement or more complete data
      final existing = unique[existingIndex];
      final existingEngagement = existing.likedProfiles.length + existing.commentIds.length;
      final postEngagement = post.likedProfiles.length + post.commentIds.length;
      if (postEngagement > existingEngagement) {
        unique[existingIndex] = post;
      }
    }
  }
  return unique;
}