CommentersSorter.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. private ICommentsManager $commentsManager;
  29. public function __construct(ICommentsManager $commentsManager) {
  30. $this->commentsManager = $commentsManager;
  31. }
  32. public function getId() {
  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) {
  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. protected function retrieveCommentsInformation(string $type, string $id): array {
  69. $comments = $this->commentsManager->getForObject($type, $id);
  70. if (count($comments) === 0) {
  71. return [];
  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. }
  86. protected function compare(array $a, array $b, array $commenters): int {
  87. $a = $a['value']['shareWith'];
  88. $b = $b['value']['shareWith'];
  89. $valueA = $commenters[$a] ?? 0;
  90. $valueB = $commenters[$b] ?? 0;
  91. return $valueB - $valueA;
  92. }
  93. }