WhatsNewController.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2018 Arthur Schiwon <blizzz@arthur-schiwon.de>
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Kate Döen <kate.doeen@nextcloud.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 OC\Core\Controller;
  26. use OC\CapabilitiesManager;
  27. use OC\Security\IdentityProof\Manager;
  28. use OC\Updater\ChangesCheck;
  29. use OCP\AppFramework\Db\DoesNotExistException;
  30. use OCP\AppFramework\Http;
  31. use OCP\AppFramework\Http\Attribute\ApiRoute;
  32. use OCP\AppFramework\Http\DataResponse;
  33. use OCP\Defaults;
  34. use OCP\IConfig;
  35. use OCP\IRequest;
  36. use OCP\IUserManager;
  37. use OCP\IUserSession;
  38. use OCP\L10N\IFactory;
  39. class WhatsNewController extends OCSController {
  40. public function __construct(
  41. string $appName,
  42. IRequest $request,
  43. CapabilitiesManager $capabilitiesManager,
  44. private IUserSession $userSession,
  45. IUserManager $userManager,
  46. Manager $keyManager,
  47. private IConfig $config,
  48. private ChangesCheck $whatsNewService,
  49. private IFactory $langFactory,
  50. private Defaults $defaults,
  51. ) {
  52. parent::__construct($appName, $request, $capabilitiesManager, $userSession, $userManager, $keyManager);
  53. }
  54. /**
  55. * @NoAdminRequired
  56. *
  57. * Get the changes
  58. *
  59. * @return DataResponse<Http::STATUS_OK, array{changelogURL: string, product: string, version: string, whatsNew?: array{regular: string[], admin: string[]}}, array{}>|DataResponse<Http::STATUS_NO_CONTENT, array<empty>, array{}>
  60. *
  61. * 200: Changes returned
  62. * 204: No changes
  63. */
  64. #[ApiRoute(verb: 'GET', url: '/whatsnew', root: '/core')]
  65. public function get():DataResponse {
  66. $user = $this->userSession->getUser();
  67. if ($user === null) {
  68. throw new \RuntimeException("Acting user cannot be resolved");
  69. }
  70. $lastRead = $this->config->getUserValue($user->getUID(), 'core', 'whatsNewLastRead', 0);
  71. $currentVersion = $this->whatsNewService->normalizeVersion($this->config->getSystemValue('version'));
  72. if (version_compare($lastRead, $currentVersion, '>=')) {
  73. return new DataResponse([], Http::STATUS_NO_CONTENT);
  74. }
  75. try {
  76. $iterator = $this->langFactory->getLanguageIterator();
  77. $whatsNew = $this->whatsNewService->getChangesForVersion($currentVersion);
  78. $resultData = [
  79. 'changelogURL' => $whatsNew['changelogURL'],
  80. 'product' => $this->defaults->getProductName(),
  81. 'version' => $currentVersion,
  82. ];
  83. do {
  84. $lang = $iterator->current();
  85. if (isset($whatsNew['whatsNew'][$lang])) {
  86. $resultData['whatsNew'] = $whatsNew['whatsNew'][$lang];
  87. break;
  88. }
  89. $iterator->next();
  90. } while ($lang !== 'en' && $iterator->valid());
  91. return new DataResponse($resultData);
  92. } catch (DoesNotExistException $e) {
  93. return new DataResponse([], Http::STATUS_NO_CONTENT);
  94. }
  95. }
  96. /**
  97. * @NoAdminRequired
  98. *
  99. * Dismiss the changes
  100. *
  101. * @param string $version Version to dismiss the changes for
  102. *
  103. * @return DataResponse<Http::STATUS_OK, array<empty>, array{}>
  104. * @throws \OCP\PreConditionNotMetException
  105. * @throws DoesNotExistException
  106. *
  107. * 200: Changes dismissed
  108. */
  109. #[ApiRoute(verb: 'POST', url: '/whatsnew', root: '/core')]
  110. public function dismiss(string $version):DataResponse {
  111. $user = $this->userSession->getUser();
  112. if ($user === null) {
  113. throw new \RuntimeException("Acting user cannot be resolved");
  114. }
  115. $version = $this->whatsNewService->normalizeVersion($version);
  116. // checks whether it's a valid version, throws an Exception otherwise
  117. $this->whatsNewService->getChangesForVersion($version);
  118. $this->config->setUserValue($user->getUID(), 'core', 'whatsNewLastRead', $version);
  119. return new DataResponse();
  120. }
  121. }