Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Deduplicate recommendations
Fixes #24

Signed-off-by: Christoph Wurst <[email protected]>
  • Loading branch information
ChristophWurst committed Mar 11, 2019
commit bbc07fc1bc8458d767c3f75c042d7ab62ce57c40
26 changes: 25 additions & 1 deletion lib/Service/RecommendationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,33 @@ public function getRecommendations(IUser $user, int $max = self::MAX_RECOMMENDAT
}, []);

$sorted = $this->sortRecommendations($all);
$topX = array_slice($sorted, 0, $max);
$topX = $this->getDeduplicatedSlice($sorted, $max);

return $this->addPreviews($topX);
}

/**
* Deduplicate the sorted recommendations and return the top $max picks
*
* The first (most recent) recommendation wins, hence eventually show its
* recommendation reason
*
* @param IRecommendation[] $recommendations
* @param int $max
* @return IRecommendation[]
*/
private function getDeduplicatedSlice(array $recommendations, int $max) {
$picks = [];

foreach ($recommendations as $recommendation) {
if (empty(array_filter($picks, function (IRecommendation $rec) use ($recommendation) {
return $recommendation->getNode()->getId() === $rec->getNode()->getId();
}))) {
$picks[] = $recommendation;
}
}

return array_slice($picks, 0, $max);
}

}