APIController.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2017 Joas Schilling <coding@schilljs.com>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OCA\UpdateNotification\Controller;
  26. use OC\App\AppStore\Fetcher\AppFetcher;
  27. use OCP\App\AppPathNotFoundException;
  28. use OCP\App\IAppManager;
  29. use OCP\AppFramework\Http;
  30. use OCP\AppFramework\Http\DataResponse;
  31. use OCP\AppFramework\OCSController;
  32. use OCP\IConfig;
  33. use OCP\IRequest;
  34. class APIController extends OCSController {
  35. /** @var IConfig */
  36. protected $config;
  37. /** @var IAppManager */
  38. protected $appManager;
  39. /** @var AppFetcher */
  40. protected $appFetcher;
  41. /**
  42. * @param string $appName
  43. * @param IRequest $request
  44. * @param IConfig $config
  45. * @param IAppManager $appManager
  46. * @param AppFetcher $appFetcher
  47. */
  48. public function __construct($appName,
  49. IRequest $request,
  50. IConfig $config,
  51. IAppManager $appManager,
  52. AppFetcher $appFetcher) {
  53. parent::__construct($appName, $request);
  54. $this->config = $config;
  55. $this->appManager = $appManager;
  56. $this->appFetcher = $appFetcher;
  57. }
  58. /**
  59. * @param string $newVersion
  60. * @return DataResponse
  61. */
  62. public function getAppList(string $newVersion): DataResponse {
  63. if (!$this->config->getSystemValue('appstoreenabled', true)) {
  64. return new DataResponse([
  65. 'appstore_disabled' => true,
  66. ], Http::STATUS_NOT_FOUND);
  67. }
  68. // Get list of installed custom apps
  69. $installedApps = $this->appManager->getInstalledApps();
  70. $installedApps = array_filter($installedApps, function ($app) {
  71. try {
  72. $this->appManager->getAppPath($app);
  73. } catch (AppPathNotFoundException $e) {
  74. return false;
  75. }
  76. return !$this->appManager->isShipped($app);
  77. });
  78. if (empty($installedApps)) {
  79. return new DataResponse([
  80. 'missing' => [],
  81. 'available' => [],
  82. ]);
  83. }
  84. $this->appFetcher->setVersion($newVersion, 'future-apps.json', false);
  85. // Apps available on the app store for that version
  86. $availableApps = array_map(function (array $app) {
  87. return $app['id'];
  88. }, $this->appFetcher->get());
  89. if (empty($availableApps)) {
  90. return new DataResponse([
  91. 'appstore_disabled' => false,
  92. 'already_on_latest' => false,
  93. ], Http::STATUS_NOT_FOUND);
  94. }
  95. $missing = array_diff($installedApps, $availableApps);
  96. $missing = array_map([$this, 'getAppDetails'], $missing);
  97. sort($missing);
  98. $available = array_intersect($installedApps, $availableApps);
  99. $available = array_map([$this, 'getAppDetails'], $available);
  100. sort($available);
  101. return new DataResponse([
  102. 'missing' => $missing,
  103. 'available' => $available,
  104. ]);
  105. }
  106. /**
  107. * Get translated app name
  108. *
  109. * @param string $appId
  110. * @return string[]
  111. */
  112. protected function getAppDetails($appId): array {
  113. $app = $this->appManager->getAppInfo($appId);
  114. return [
  115. 'appId' => $appId,
  116. 'appName' => $app['name'] ?? $appId,
  117. ];
  118. }
  119. }