You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
3.3 KiB

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