You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CommentersSorter.php 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Arthur Schiwon <blizzz@arthur-schiwon.de>
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace OCA\Comments\Collaboration;
  24. use OCP\Collaboration\AutoComplete\ISorter;
  25. use OCP\Comments\ICommentsManager;
  26. class CommentersSorter implements ISorter {
  27. /** @var ICommentsManager */
  28. private $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. /**
  69. * @param $type
  70. * @param $id
  71. * @return array
  72. */
  73. protected function retrieveCommentsInformation($type, $id) {
  74. $comments = $this->commentsManager->getForObject($type, $id);
  75. if (count($comments) === 0) {
  76. return [];
  77. }
  78. $actors = [];
  79. foreach ($comments as $comment) {
  80. if (!isset($actors[$comment->getActorType()])) {
  81. $actors[$comment->getActorType()] = [];
  82. }
  83. if (!isset($actors[$comment->getActorType()][$comment->getActorId()])) {
  84. $actors[$comment->getActorType()][$comment->getActorId()] = 1;
  85. } else {
  86. $actors[$comment->getActorType()][$comment->getActorId()]++;
  87. }
  88. }
  89. return $actors;
  90. }
  91. protected function compare(array $a, array $b, array $commenters) {
  92. $a = $a['value']['shareWith'];
  93. $b = $b['value']['shareWith'];
  94. $valueA = isset($commenters[$a]) ? $commenters[$a] : 0;
  95. $valueB = isset($commenters[$b]) ? $commenters[$b] : 0;
  96. return $valueB - $valueA;
  97. }
  98. }