WhatsNewController.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OC\Core\Controller;
  7. use OC\CapabilitiesManager;
  8. use OC\Security\IdentityProof\Manager;
  9. use OC\Updater\ChangesCheck;
  10. use OCP\AppFramework\Db\DoesNotExistException;
  11. use OCP\AppFramework\Http;
  12. use OCP\AppFramework\Http\Attribute\ApiRoute;
  13. use OCP\AppFramework\Http\DataResponse;
  14. use OCP\Defaults;
  15. use OCP\IConfig;
  16. use OCP\IRequest;
  17. use OCP\IUserManager;
  18. use OCP\IUserSession;
  19. use OCP\L10N\IFactory;
  20. class WhatsNewController extends OCSController {
  21. public function __construct(
  22. string $appName,
  23. IRequest $request,
  24. CapabilitiesManager $capabilitiesManager,
  25. private IUserSession $userSession,
  26. IUserManager $userManager,
  27. Manager $keyManager,
  28. private IConfig $config,
  29. private ChangesCheck $whatsNewService,
  30. private IFactory $langFactory,
  31. private Defaults $defaults,
  32. ) {
  33. parent::__construct($appName, $request, $capabilitiesManager, $userSession, $userManager, $keyManager);
  34. }
  35. /**
  36. * @NoAdminRequired
  37. *
  38. * Get the changes
  39. *
  40. * @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{}>
  41. *
  42. * 200: Changes returned
  43. * 204: No changes
  44. */
  45. #[ApiRoute(verb: 'GET', url: '/whatsnew', root: '/core')]
  46. public function get():DataResponse {
  47. $user = $this->userSession->getUser();
  48. if ($user === null) {
  49. throw new \RuntimeException("Acting user cannot be resolved");
  50. }
  51. $lastRead = $this->config->getUserValue($user->getUID(), 'core', 'whatsNewLastRead', 0);
  52. $currentVersion = $this->whatsNewService->normalizeVersion($this->config->getSystemValue('version'));
  53. if (version_compare($lastRead, $currentVersion, '>=')) {
  54. return new DataResponse([], Http::STATUS_NO_CONTENT);
  55. }
  56. try {
  57. $iterator = $this->langFactory->getLanguageIterator();
  58. $whatsNew = $this->whatsNewService->getChangesForVersion($currentVersion);
  59. $resultData = [
  60. 'changelogURL' => $whatsNew['changelogURL'],
  61. 'product' => $this->defaults->getProductName(),
  62. 'version' => $currentVersion,
  63. ];
  64. do {
  65. $lang = $iterator->current();
  66. if (isset($whatsNew['whatsNew'][$lang])) {
  67. $resultData['whatsNew'] = $whatsNew['whatsNew'][$lang];
  68. break;
  69. }
  70. $iterator->next();
  71. } while ($lang !== 'en' && $iterator->valid());
  72. return new DataResponse($resultData);
  73. } catch (DoesNotExistException $e) {
  74. return new DataResponse([], Http::STATUS_NO_CONTENT);
  75. }
  76. }
  77. /**
  78. * @NoAdminRequired
  79. *
  80. * Dismiss the changes
  81. *
  82. * @param string $version Version to dismiss the changes for
  83. *
  84. * @return DataResponse<Http::STATUS_OK, array<empty>, array{}>
  85. * @throws \OCP\PreConditionNotMetException
  86. * @throws DoesNotExistException
  87. *
  88. * 200: Changes dismissed
  89. */
  90. #[ApiRoute(verb: 'POST', url: '/whatsnew', root: '/core')]
  91. public function dismiss(string $version):DataResponse {
  92. $user = $this->userSession->getUser();
  93. if ($user === null) {
  94. throw new \RuntimeException("Acting user cannot be resolved");
  95. }
  96. $version = $this->whatsNewService->normalizeVersion($version);
  97. // checks whether it's a valid version, throws an Exception otherwise
  98. $this->whatsNewService->getChangesForVersion($version);
  99. $this->config->setUserValue($user->getUID(), 'core', 'whatsNewLastRead', $version);
  100. return new DataResponse();
  101. }
  102. }