Skip to content

Commit 111f896

Browse files
Deduplicate recommendations
Fixes #24 Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
1 parent 38aba74 commit 111f896

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

lib/Service/RecommendationService.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,33 @@ public function getRecommendations(IUser $user, int $max = self::MAX_RECOMMENDAT
9292
}, []);
9393

9494
$sorted = $this->sortRecommendations($all);
95-
$topX = array_slice($sorted, 0, $max);
95+
$topX = $this->getDeplicatedSlice($sorted, $max);
9696

9797
return $this->addPreviews($topX);
9898
}
9999

100+
/**
101+
* Deduplicate the sorted recommendations and return the top $max picks
102+
*
103+
* The first (most recent) recommendation wins, hence eventually show its
104+
* recommendation reason
105+
*
106+
* @param IRecommendation[] $recommendations
107+
* @param int $max
108+
* @return IRecommendation[]
109+
*/
110+
private function getDeplicatedSlice(array $recommendations, int $max) {
111+
$picks = [];
112+
113+
foreach ($recommendations as $recommendation) {
114+
if (empty(array_filter($picks, function (IRecommendation $rec) use ($recommendation) {
115+
return $recommendation->getNode()->getId() === $rec->getNode()->getId();
116+
}))) {
117+
$picks[] = $recommendation;
118+
}
119+
}
120+
121+
return array_slice($picks, 0, $max);
122+
}
123+
100124
}

0 commit comments

Comments
 (0)