@@ -39,122 +39,122 @@ |
||
| 39 | 39 | |
| 40 | 40 | class APIController extends OCSController { |
| 41 | 41 | |
| 42 | - /** @var IConfig */ |
|
| 43 | - protected $config; |
|
| 44 | - |
|
| 45 | - /** @var IAppManager */ |
|
| 46 | - protected $appManager; |
|
| 47 | - |
|
| 48 | - /** @var AppFetcher */ |
|
| 49 | - protected $appFetcher; |
|
| 50 | - |
|
| 51 | - /** @var IFactory */ |
|
| 52 | - protected $l10nFactory; |
|
| 53 | - |
|
| 54 | - /** @var IUserSession */ |
|
| 55 | - protected $userSession; |
|
| 56 | - |
|
| 57 | - /** @var string */ |
|
| 58 | - protected $language; |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * List of apps that were in the appstore but are now shipped and don't have |
|
| 62 | - * a compatible update available. |
|
| 63 | - * |
|
| 64 | - * @var array<string, int> |
|
| 65 | - */ |
|
| 66 | - protected array $appsShippedInFutureVersion = [ |
|
| 67 | - 'bruteforcesettings' => 25, |
|
| 68 | - 'suspicious_login' => 25, |
|
| 69 | - 'twofactor_totp' => 25, |
|
| 70 | - ]; |
|
| 71 | - |
|
| 72 | - public function __construct(string $appName, |
|
| 73 | - IRequest $request, |
|
| 74 | - IConfig $config, |
|
| 75 | - IAppManager $appManager, |
|
| 76 | - AppFetcher $appFetcher, |
|
| 77 | - IFactory $l10nFactory, |
|
| 78 | - IUserSession $userSession) { |
|
| 79 | - parent::__construct($appName, $request); |
|
| 80 | - |
|
| 81 | - $this->config = $config; |
|
| 82 | - $this->appManager = $appManager; |
|
| 83 | - $this->appFetcher = $appFetcher; |
|
| 84 | - $this->l10nFactory = $l10nFactory; |
|
| 85 | - $this->userSession = $userSession; |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - /** |
|
| 89 | - * @param string $newVersion |
|
| 90 | - * @return DataResponse |
|
| 91 | - */ |
|
| 92 | - public function getAppList(string $newVersion): DataResponse { |
|
| 93 | - if (!$this->config->getSystemValue('appstoreenabled', true)) { |
|
| 94 | - return new DataResponse([ |
|
| 95 | - 'appstore_disabled' => true, |
|
| 96 | - ], Http::STATUS_NOT_FOUND); |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - // Get list of installed custom apps |
|
| 100 | - $installedApps = $this->appManager->getInstalledApps(); |
|
| 101 | - $installedApps = array_filter($installedApps, function ($app) { |
|
| 102 | - try { |
|
| 103 | - $this->appManager->getAppPath($app); |
|
| 104 | - } catch (AppPathNotFoundException $e) { |
|
| 105 | - return false; |
|
| 106 | - } |
|
| 107 | - return !$this->appManager->isShipped($app) && !isset($this->appsShippedInFutureVersion[$app]); |
|
| 108 | - }); |
|
| 109 | - |
|
| 110 | - if (empty($installedApps)) { |
|
| 111 | - return new DataResponse([ |
|
| 112 | - 'missing' => [], |
|
| 113 | - 'available' => [], |
|
| 114 | - ]); |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - $this->appFetcher->setVersion($newVersion, 'future-apps.json', false); |
|
| 118 | - |
|
| 119 | - // Apps available on the app store for that version |
|
| 120 | - $availableApps = array_map(static function (array $app) { |
|
| 121 | - return $app['id']; |
|
| 122 | - }, $this->appFetcher->get()); |
|
| 123 | - |
|
| 124 | - if (empty($availableApps)) { |
|
| 125 | - return new DataResponse([ |
|
| 126 | - 'appstore_disabled' => false, |
|
| 127 | - 'already_on_latest' => false, |
|
| 128 | - ], Http::STATUS_NOT_FOUND); |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - $this->language = $this->l10nFactory->getUserLanguage($this->userSession->getUser()); |
|
| 132 | - |
|
| 133 | - $missing = array_diff($installedApps, $availableApps); |
|
| 134 | - $missing = array_map([$this, 'getAppDetails'], $missing); |
|
| 135 | - sort($missing); |
|
| 136 | - |
|
| 137 | - $available = array_intersect($installedApps, $availableApps); |
|
| 138 | - $available = array_map([$this, 'getAppDetails'], $available); |
|
| 139 | - sort($available); |
|
| 140 | - |
|
| 141 | - return new DataResponse([ |
|
| 142 | - 'missing' => $missing, |
|
| 143 | - 'available' => $available, |
|
| 144 | - ]); |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - /** |
|
| 148 | - * Get translated app name |
|
| 149 | - * |
|
| 150 | - * @param string $appId |
|
| 151 | - * @return string[] |
|
| 152 | - */ |
|
| 153 | - protected function getAppDetails(string $appId): array { |
|
| 154 | - $app = $this->appManager->getAppInfo($appId, false, $this->language); |
|
| 155 | - return [ |
|
| 156 | - 'appId' => $appId, |
|
| 157 | - 'appName' => $app['name'] ?? $appId, |
|
| 158 | - ]; |
|
| 159 | - } |
|
| 42 | + /** @var IConfig */ |
|
| 43 | + protected $config; |
|
| 44 | + |
|
| 45 | + /** @var IAppManager */ |
|
| 46 | + protected $appManager; |
|
| 47 | + |
|
| 48 | + /** @var AppFetcher */ |
|
| 49 | + protected $appFetcher; |
|
| 50 | + |
|
| 51 | + /** @var IFactory */ |
|
| 52 | + protected $l10nFactory; |
|
| 53 | + |
|
| 54 | + /** @var IUserSession */ |
|
| 55 | + protected $userSession; |
|
| 56 | + |
|
| 57 | + /** @var string */ |
|
| 58 | + protected $language; |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * List of apps that were in the appstore but are now shipped and don't have |
|
| 62 | + * a compatible update available. |
|
| 63 | + * |
|
| 64 | + * @var array<string, int> |
|
| 65 | + */ |
|
| 66 | + protected array $appsShippedInFutureVersion = [ |
|
| 67 | + 'bruteforcesettings' => 25, |
|
| 68 | + 'suspicious_login' => 25, |
|
| 69 | + 'twofactor_totp' => 25, |
|
| 70 | + ]; |
|
| 71 | + |
|
| 72 | + public function __construct(string $appName, |
|
| 73 | + IRequest $request, |
|
| 74 | + IConfig $config, |
|
| 75 | + IAppManager $appManager, |
|
| 76 | + AppFetcher $appFetcher, |
|
| 77 | + IFactory $l10nFactory, |
|
| 78 | + IUserSession $userSession) { |
|
| 79 | + parent::__construct($appName, $request); |
|
| 80 | + |
|
| 81 | + $this->config = $config; |
|
| 82 | + $this->appManager = $appManager; |
|
| 83 | + $this->appFetcher = $appFetcher; |
|
| 84 | + $this->l10nFactory = $l10nFactory; |
|
| 85 | + $this->userSession = $userSession; |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + /** |
|
| 89 | + * @param string $newVersion |
|
| 90 | + * @return DataResponse |
|
| 91 | + */ |
|
| 92 | + public function getAppList(string $newVersion): DataResponse { |
|
| 93 | + if (!$this->config->getSystemValue('appstoreenabled', true)) { |
|
| 94 | + return new DataResponse([ |
|
| 95 | + 'appstore_disabled' => true, |
|
| 96 | + ], Http::STATUS_NOT_FOUND); |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + // Get list of installed custom apps |
|
| 100 | + $installedApps = $this->appManager->getInstalledApps(); |
|
| 101 | + $installedApps = array_filter($installedApps, function ($app) { |
|
| 102 | + try { |
|
| 103 | + $this->appManager->getAppPath($app); |
|
| 104 | + } catch (AppPathNotFoundException $e) { |
|
| 105 | + return false; |
|
| 106 | + } |
|
| 107 | + return !$this->appManager->isShipped($app) && !isset($this->appsShippedInFutureVersion[$app]); |
|
| 108 | + }); |
|
| 109 | + |
|
| 110 | + if (empty($installedApps)) { |
|
| 111 | + return new DataResponse([ |
|
| 112 | + 'missing' => [], |
|
| 113 | + 'available' => [], |
|
| 114 | + ]); |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + $this->appFetcher->setVersion($newVersion, 'future-apps.json', false); |
|
| 118 | + |
|
| 119 | + // Apps available on the app store for that version |
|
| 120 | + $availableApps = array_map(static function (array $app) { |
|
| 121 | + return $app['id']; |
|
| 122 | + }, $this->appFetcher->get()); |
|
| 123 | + |
|
| 124 | + if (empty($availableApps)) { |
|
| 125 | + return new DataResponse([ |
|
| 126 | + 'appstore_disabled' => false, |
|
| 127 | + 'already_on_latest' => false, |
|
| 128 | + ], Http::STATUS_NOT_FOUND); |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + $this->language = $this->l10nFactory->getUserLanguage($this->userSession->getUser()); |
|
| 132 | + |
|
| 133 | + $missing = array_diff($installedApps, $availableApps); |
|
| 134 | + $missing = array_map([$this, 'getAppDetails'], $missing); |
|
| 135 | + sort($missing); |
|
| 136 | + |
|
| 137 | + $available = array_intersect($installedApps, $availableApps); |
|
| 138 | + $available = array_map([$this, 'getAppDetails'], $available); |
|
| 139 | + sort($available); |
|
| 140 | + |
|
| 141 | + return new DataResponse([ |
|
| 142 | + 'missing' => $missing, |
|
| 143 | + 'available' => $available, |
|
| 144 | + ]); |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + /** |
|
| 148 | + * Get translated app name |
|
| 149 | + * |
|
| 150 | + * @param string $appId |
|
| 151 | + * @return string[] |
|
| 152 | + */ |
|
| 153 | + protected function getAppDetails(string $appId): array { |
|
| 154 | + $app = $this->appManager->getAppInfo($appId, false, $this->language); |
|
| 155 | + return [ |
|
| 156 | + 'appId' => $appId, |
|
| 157 | + 'appName' => $app['name'] ?? $appId, |
|
| 158 | + ]; |
|
| 159 | + } |
|
| 160 | 160 | } |