您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ShareRecipientSorter.php 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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\Files_Sharing\Collaboration;
  25. use OCP\Collaboration\AutoComplete\ISorter;
  26. use OCP\Files\IRootFolder;
  27. use OCP\Files\Node;
  28. use OCP\IUserSession;
  29. use OCP\Share\IManager;
  30. class ShareRecipientSorter implements ISorter {
  31. private IManager $shareManager;
  32. private IRootFolder $rootFolder;
  33. private IUserSession $userSession;
  34. public function __construct(IManager $shareManager, IRootFolder $rootFolder, IUserSession $userSession) {
  35. $this->shareManager = $shareManager;
  36. $this->rootFolder = $rootFolder;
  37. $this->userSession = $userSession;
  38. }
  39. public function getId(): string {
  40. return 'share-recipients';
  41. }
  42. public function sort(array &$sortArray, array $context) {
  43. // let's be tolerant. Comments uses "files" by default, other usages are often singular
  44. if ($context['itemType'] !== 'files' && $context['itemType'] !== 'file') {
  45. return;
  46. }
  47. $user = $this->userSession->getUser();
  48. if ($user === null) {
  49. return;
  50. }
  51. $userFolder = $this->rootFolder->getUserFolder($user->getUID());
  52. /** @var Node[] $nodes */
  53. $node = $userFolder->getFirstNodeById((int)$context['itemId']);
  54. if (!$node) {
  55. return;
  56. }
  57. $al = $this->shareManager->getAccessList($node);
  58. foreach ($sortArray as $type => &$byType) {
  59. if (!isset($al[$type]) || !is_array($al[$type])) {
  60. continue;
  61. }
  62. // at least on PHP 5.6 usort turned out to be not stable. So we add
  63. // the current index to the value and compare it on a draw
  64. $i = 0;
  65. $workArray = array_map(function ($element) use (&$i) {
  66. return [$i++, $element];
  67. }, $byType);
  68. usort($workArray, function ($a, $b) use ($al, $type) {
  69. $result = $this->compare($a[1], $b[1], $al[$type]);
  70. if ($result === 0) {
  71. $result = $a[0] - $b[0];
  72. }
  73. return $result;
  74. });
  75. // and remove the index values again
  76. $byType = array_column($workArray, 1);
  77. }
  78. }
  79. /**
  80. * @param array $a
  81. * @param array $b
  82. * @param array $al
  83. * @return int
  84. */
  85. protected function compare(array $a, array $b, array $al) {
  86. $a = $a['value']['shareWith'];
  87. $b = $b['value']['shareWith'];
  88. $valueA = (int)in_array($a, $al, true);
  89. $valueB = (int)in_array($b, $al, true);
  90. return $valueB - $valueA;
  91. }
  92. }