CommentersSorter.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Arthur Schiwon <blizzz@arthur-schiwon.de>
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\Comments\Collaboration;
  25. use OCP\Collaboration\AutoComplete\ISorter;
  26. use OCP\Comments\ICommentsManager;
  27. class CommentersSorter implements ISorter {
  28. public function __construct(
  29. private ICommentsManager $commentsManager,
  30. ) {
  31. }
  32. public function getId(): string {
  33. return 'commenters';
  34. }
  35. /**
  36. * Sorts people who commented on the given item atop (descelating) of the
  37. * others
  38. *
  39. * @param array &$sortArray
  40. * @param array $context
  41. */
  42. public function sort(array &$sortArray, array $context): void {
  43. $commenters = $this->retrieveCommentsInformation($context['itemType'], $context['itemId']);
  44. if (count($commenters) === 0) {
  45. return;
  46. }
  47. foreach ($sortArray as $type => &$byType) {
  48. if (!isset($commenters[$type])) {
  49. continue;
  50. }
  51. // at least on PHP 5.6 usort turned out to be not stable. So we add
  52. // the current index to the value and compare it on a draw
  53. $i = 0;
  54. $workArray = array_map(function ($element) use (&$i) {
  55. return [$i++, $element];
  56. }, $byType);
  57. usort($workArray, function ($a, $b) use ($commenters, $type) {
  58. $r = $this->compare($a[1], $b[1], $commenters[$type]);
  59. if ($r === 0) {
  60. $r = $a[0] - $b[0];
  61. }
  62. return $r;
  63. });
  64. // and remove the index values again
  65. $byType = array_column($workArray, 1);
  66. }
  67. }
  68. /**
  69. * @return array<string, array<string, int>>
  70. */
  71. protected function retrieveCommentsInformation(string $type, string $id): array {
  72. $comments = $this->commentsManager->getForObject($type, $id);
  73. if (count($comments) === 0) {
  74. return [];
  75. }
  76. $actors = [];
  77. foreach ($comments as $comment) {
  78. if (!isset($actors[$comment->getActorType()])) {
  79. $actors[$comment->getActorType()] = [];
  80. }
  81. if (!isset($actors[$comment->getActorType()][$comment->getActorId()])) {
  82. $actors[$comment->getActorType()][$comment->getActorId()] = 1;
  83. } else {
  84. $actors[$comment->getActorType()][$comment->getActorId()]++;
  85. }
  86. }
  87. return $actors;
  88. }
  89. protected function compare(array $a, array $b, array $commenters): int {
  90. $a = $a['value']['shareWith'];
  91. $b = $b['value']['shareWith'];
  92. $valueA = $commenters[$a] ?? 0;
  93. $valueB = $commenters[$b] ?? 0;
  94. return $valueB - $valueA;
  95. }
  96. }