@@ -10,87 +10,87 @@ |
||
| 10 | 10 | use OCP\Comments\ICommentsManager; |
| 11 | 11 | |
| 12 | 12 | class CommentersSorter implements ISorter { |
| 13 | - public function __construct( |
|
| 14 | - private ICommentsManager $commentsManager, |
|
| 15 | - ) { |
|
| 16 | - } |
|
| 13 | + public function __construct( |
|
| 14 | + private ICommentsManager $commentsManager, |
|
| 15 | + ) { |
|
| 16 | + } |
|
| 17 | 17 | |
| 18 | - public function getId(): string { |
|
| 19 | - return 'commenters'; |
|
| 20 | - } |
|
| 18 | + public function getId(): string { |
|
| 19 | + return 'commenters'; |
|
| 20 | + } |
|
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * Sorts people who commented on the given item atop (descelating) of the |
|
| 24 | - * others |
|
| 25 | - * |
|
| 26 | - * @param array &$sortArray |
|
| 27 | - * @param array $context |
|
| 28 | - */ |
|
| 29 | - public function sort(array &$sortArray, array $context): void { |
|
| 30 | - if (!isset($context['itemType'], $context['itemId'])) { |
|
| 31 | - return; |
|
| 32 | - } |
|
| 22 | + /** |
|
| 23 | + * Sorts people who commented on the given item atop (descelating) of the |
|
| 24 | + * others |
|
| 25 | + * |
|
| 26 | + * @param array &$sortArray |
|
| 27 | + * @param array $context |
|
| 28 | + */ |
|
| 29 | + public function sort(array &$sortArray, array $context): void { |
|
| 30 | + if (!isset($context['itemType'], $context['itemId'])) { |
|
| 31 | + return; |
|
| 32 | + } |
|
| 33 | 33 | |
| 34 | - $commenters = $this->retrieveCommentsInformation($context['itemType'], $context['itemId']); |
|
| 35 | - if (count($commenters) === 0) { |
|
| 36 | - return; |
|
| 37 | - } |
|
| 34 | + $commenters = $this->retrieveCommentsInformation($context['itemType'], $context['itemId']); |
|
| 35 | + if (count($commenters) === 0) { |
|
| 36 | + return; |
|
| 37 | + } |
|
| 38 | 38 | |
| 39 | - foreach ($sortArray as $type => &$byType) { |
|
| 40 | - if (!isset($commenters[$type])) { |
|
| 41 | - continue; |
|
| 42 | - } |
|
| 39 | + foreach ($sortArray as $type => &$byType) { |
|
| 40 | + if (!isset($commenters[$type])) { |
|
| 41 | + continue; |
|
| 42 | + } |
|
| 43 | 43 | |
| 44 | - // at least on PHP 5.6 usort turned out to be not stable. So we add |
|
| 45 | - // the current index to the value and compare it on a draw |
|
| 46 | - $i = 0; |
|
| 47 | - $workArray = array_map(function ($element) use (&$i) { |
|
| 48 | - return [$i++, $element]; |
|
| 49 | - }, $byType); |
|
| 44 | + // at least on PHP 5.6 usort turned out to be not stable. So we add |
|
| 45 | + // the current index to the value and compare it on a draw |
|
| 46 | + $i = 0; |
|
| 47 | + $workArray = array_map(function ($element) use (&$i) { |
|
| 48 | + return [$i++, $element]; |
|
| 49 | + }, $byType); |
|
| 50 | 50 | |
| 51 | - usort($workArray, function ($a, $b) use ($commenters, $type) { |
|
| 52 | - $r = $this->compare($a[1], $b[1], $commenters[$type]); |
|
| 53 | - if ($r === 0) { |
|
| 54 | - $r = $a[0] - $b[0]; |
|
| 55 | - } |
|
| 56 | - return $r; |
|
| 57 | - }); |
|
| 51 | + usort($workArray, function ($a, $b) use ($commenters, $type) { |
|
| 52 | + $r = $this->compare($a[1], $b[1], $commenters[$type]); |
|
| 53 | + if ($r === 0) { |
|
| 54 | + $r = $a[0] - $b[0]; |
|
| 55 | + } |
|
| 56 | + return $r; |
|
| 57 | + }); |
|
| 58 | 58 | |
| 59 | - // and remove the index values again |
|
| 60 | - $byType = array_column($workArray, 1); |
|
| 61 | - } |
|
| 62 | - } |
|
| 59 | + // and remove the index values again |
|
| 60 | + $byType = array_column($workArray, 1); |
|
| 61 | + } |
|
| 62 | + } |
|
| 63 | 63 | |
| 64 | - /** |
|
| 65 | - * @return array<string, array<string, int>> |
|
| 66 | - */ |
|
| 67 | - protected function retrieveCommentsInformation(string $type, string $id): array { |
|
| 68 | - $comments = $this->commentsManager->getForObject($type, $id); |
|
| 69 | - if (count($comments) === 0) { |
|
| 70 | - return []; |
|
| 71 | - } |
|
| 64 | + /** |
|
| 65 | + * @return array<string, array<string, int>> |
|
| 66 | + */ |
|
| 67 | + protected function retrieveCommentsInformation(string $type, string $id): array { |
|
| 68 | + $comments = $this->commentsManager->getForObject($type, $id); |
|
| 69 | + if (count($comments) === 0) { |
|
| 70 | + return []; |
|
| 71 | + } |
|
| 72 | 72 | |
| 73 | - $actors = []; |
|
| 74 | - foreach ($comments as $comment) { |
|
| 75 | - if (!isset($actors[$comment->getActorType()])) { |
|
| 76 | - $actors[$comment->getActorType()] = []; |
|
| 77 | - } |
|
| 78 | - if (!isset($actors[$comment->getActorType()][$comment->getActorId()])) { |
|
| 79 | - $actors[$comment->getActorType()][$comment->getActorId()] = 1; |
|
| 80 | - } else { |
|
| 81 | - $actors[$comment->getActorType()][$comment->getActorId()]++; |
|
| 82 | - } |
|
| 83 | - } |
|
| 84 | - return $actors; |
|
| 85 | - } |
|
| 73 | + $actors = []; |
|
| 74 | + foreach ($comments as $comment) { |
|
| 75 | + if (!isset($actors[$comment->getActorType()])) { |
|
| 76 | + $actors[$comment->getActorType()] = []; |
|
| 77 | + } |
|
| 78 | + if (!isset($actors[$comment->getActorType()][$comment->getActorId()])) { |
|
| 79 | + $actors[$comment->getActorType()][$comment->getActorId()] = 1; |
|
| 80 | + } else { |
|
| 81 | + $actors[$comment->getActorType()][$comment->getActorId()]++; |
|
| 82 | + } |
|
| 83 | + } |
|
| 84 | + return $actors; |
|
| 85 | + } |
|
| 86 | 86 | |
| 87 | - protected function compare(array $a, array $b, array $commenters): int { |
|
| 88 | - $a = $a['value']['shareWith']; |
|
| 89 | - $b = $b['value']['shareWith']; |
|
| 87 | + protected function compare(array $a, array $b, array $commenters): int { |
|
| 88 | + $a = $a['value']['shareWith']; |
|
| 89 | + $b = $b['value']['shareWith']; |
|
| 90 | 90 | |
| 91 | - $valueA = $commenters[$a] ?? 0; |
|
| 92 | - $valueB = $commenters[$b] ?? 0; |
|
| 91 | + $valueA = $commenters[$a] ?? 0; |
|
| 92 | + $valueB = $commenters[$b] ?? 0; |
|
| 93 | 93 | |
| 94 | - return $valueB - $valueA; |
|
| 95 | - } |
|
| 94 | + return $valueB - $valueA; |
|
| 95 | + } |
|
| 96 | 96 | } |