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.2KB

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