WhatsNewController.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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\Attribute\NoAdminRequired;
  14. use OCP\AppFramework\Http\DataResponse;
  15. use OCP\Defaults;
  16. use OCP\IConfig;
  17. use OCP\IRequest;
  18. use OCP\IUserManager;
  19. use OCP\IUserSession;
  20. use OCP\L10N\IFactory;
  21. class WhatsNewController extends OCSController {
  22. public function __construct(
  23. string $appName,
  24. IRequest $request,
  25. CapabilitiesManager $capabilitiesManager,
  26. private IUserSession $userSession,
  27. IUserManager $userManager,
  28. Manager $keyManager,
  29. private IConfig $config,
  30. private ChangesCheck $whatsNewService,
  31. private IFactory $langFactory,
  32. private Defaults $defaults,
  33. ) {
  34. parent::__construct($appName, $request, $capabilitiesManager, $userSession, $userManager, $keyManager);
  35. }
  36. /**
  37. * Get the changes
  38. *
  39. * @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{}>
  40. *
  41. * 200: Changes returned
  42. * 204: No changes
  43. */
  44. #[NoAdminRequired]
  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. * Dismiss the changes
  79. *
  80. * @param string $version Version to dismiss the changes for
  81. *
  82. * @return DataResponse<Http::STATUS_OK, array<empty>, array{}>
  83. * @throws \OCP\PreConditionNotMetException
  84. * @throws DoesNotExistException
  85. *
  86. * 200: Changes dismissed
  87. */
  88. #[NoAdminRequired]
  89. #[ApiRoute(verb: 'POST', url: '/whatsnew', root: '/core')]
  90. public function dismiss(string $version):DataResponse {
  91. $user = $this->userSession->getUser();
  92. if ($user === null) {
  93. throw new \RuntimeException("Acting user cannot be resolved");
  94. }
  95. $version = $this->whatsNewService->normalizeVersion($version);
  96. // checks whether it's a valid version, throws an Exception otherwise
  97. $this->whatsNewService->getChangesForVersion($version);
  98. $this->config->setUserValue($user->getUID(), 'core', 'whatsNewLastRead', $version);
  99. return new DataResponse();
  100. }
  101. }