1
0

WhatsNewController.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. use OCP\ServerVersion;
  22. class WhatsNewController extends OCSController {
  23. public function __construct(
  24. string $appName,
  25. IRequest $request,
  26. CapabilitiesManager $capabilitiesManager,
  27. private IUserSession $userSession,
  28. IUserManager $userManager,
  29. Manager $keyManager,
  30. ServerVersion $serverVersion,
  31. private IConfig $config,
  32. private ChangesCheck $whatsNewService,
  33. private IFactory $langFactory,
  34. private Defaults $defaults,
  35. ) {
  36. parent::__construct($appName, $request, $capabilitiesManager, $userSession, $userManager, $keyManager, $serverVersion);
  37. }
  38. /**
  39. * Get the changes
  40. *
  41. * @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{}>
  42. *
  43. * 200: Changes returned
  44. * 204: No changes
  45. */
  46. #[NoAdminRequired]
  47. #[ApiRoute(verb: 'GET', url: '/whatsnew', root: '/core')]
  48. public function get():DataResponse {
  49. $user = $this->userSession->getUser();
  50. if ($user === null) {
  51. throw new \RuntimeException('Acting user cannot be resolved');
  52. }
  53. $lastRead = $this->config->getUserValue($user->getUID(), 'core', 'whatsNewLastRead', 0);
  54. $currentVersion = $this->whatsNewService->normalizeVersion($this->config->getSystemValue('version'));
  55. if (version_compare($lastRead, $currentVersion, '>=')) {
  56. return new DataResponse([], Http::STATUS_NO_CONTENT);
  57. }
  58. try {
  59. $iterator = $this->langFactory->getLanguageIterator();
  60. $whatsNew = $this->whatsNewService->getChangesForVersion($currentVersion);
  61. $resultData = [
  62. 'changelogURL' => $whatsNew['changelogURL'],
  63. 'product' => $this->defaults->getProductName(),
  64. 'version' => $currentVersion,
  65. ];
  66. do {
  67. $lang = $iterator->current();
  68. if (isset($whatsNew['whatsNew'][$lang])) {
  69. $resultData['whatsNew'] = $whatsNew['whatsNew'][$lang];
  70. break;
  71. }
  72. $iterator->next();
  73. } while ($lang !== 'en' && $iterator->valid());
  74. return new DataResponse($resultData);
  75. } catch (DoesNotExistException $e) {
  76. return new DataResponse([], Http::STATUS_NO_CONTENT);
  77. }
  78. }
  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. #[NoAdminRequired]
  91. #[ApiRoute(verb: 'POST', url: '/whatsnew', root: '/core')]
  92. public function dismiss(string $version):DataResponse {
  93. $user = $this->userSession->getUser();
  94. if ($user === null) {
  95. throw new \RuntimeException('Acting user cannot be resolved');
  96. }
  97. $version = $this->whatsNewService->normalizeVersion($version);
  98. // checks whether it's a valid version, throws an Exception otherwise
  99. $this->whatsNewService->getChangesForVersion($version);
  100. $this->config->setUserValue($user->getUID(), 'core', 'whatsNewLastRead', $version);
  101. return new DataResponse();
  102. }
  103. }