Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
27 changes: 5 additions & 22 deletions apps/comments/lib/Collaboration/CommentersSorter.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,8 @@ public function getId(): string {
}

/**
* Sorts people who commented on the given item atop (descelating) of the
* others
*
* @param array &$sortArray
* @param array $context
* Sorts people who commented on the given item ahead of others, in
* descending order
*/
#[\Override]
public function sort(array &$sortArray, array $context): void {
Expand All @@ -35,7 +32,7 @@ public function sort(array &$sortArray, array $context): void {
}

$commenters = $this->retrieveCommentsInformation($context['itemType'], $context['itemId']);
if (count($commenters) === 0) {
if ($commenters === []) {
return;
}

Expand All @@ -44,23 +41,9 @@ public function sort(array &$sortArray, array $context): void {
continue;
}

// at least on PHP 5.6 usort turned out to be not stable. So we add
// the current index to the value and compare it on a draw
$i = 0;
$workArray = array_map(function ($element) use (&$i) {
return [$i++, $element];
}, $byType);

usort($workArray, function ($a, $b) use ($commenters, $type) {
$r = $this->compare($a[1], $b[1], $commenters[$type]);
if ($r === 0) {
$r = $a[0] - $b[0];
}
return $r;
usort($byType, function ($a, $b) use ($commenters, $type) {
return $this->compare($a, $b, $commenters[$type]);
});

// and remove the index values again
$byType = array_column($workArray, 1);
}
}

Expand Down
28 changes: 4 additions & 24 deletions apps/files_sharing/lib/Collaboration/ShareRecipientSorter.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function getId(): string {
}

#[\Override]
public function sort(array &$sortArray, array $context) {
public function sort(array &$sortArray, array $context): void {
// let's be tolerant. Comments uses "files" by default, other usages are often singular
if ($context['itemType'] !== 'files' && $context['itemType'] !== 'file') {
return;
Expand All @@ -50,33 +50,13 @@ public function sort(array &$sortArray, array $context) {
continue;
}

// at least on PHP 5.6 usort turned out to be not stable. So we add
// the current index to the value and compare it on a draw
$i = 0;
$workArray = array_map(function ($element) use (&$i) {
return [$i++, $element];
}, $byType);

usort($workArray, function ($a, $b) use ($al, $type) {
$result = $this->compare($a[1], $b[1], $al[$type]);
if ($result === 0) {
$result = $a[0] - $b[0];
}
return $result;
usort($byType, function ($a, $b) use ($al, $type) {
return $this->compare($a, $b, $al[$type]);
});

// and remove the index values again
$byType = array_column($workArray, 1);
}
}

/**
* @param array $a
* @param array $b
* @param array $al
* @return int
*/
protected function compare(array $a, array $b, array $al) {
protected function compare(array $a, array $b, array $al): int {
$a = $a['value']['shareWith'];
$b = $b['value']['shareWith'];

Expand Down
Loading