1
0

WhatsNewController.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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\DataResponse;
  32. use OCP\Defaults;
  33. use OCP\IConfig;
  34. use OCP\IRequest;
  35. use OCP\IUserManager;
  36. use OCP\IUserSession;
  37. use OCP\L10N\IFactory;
  38. class WhatsNewController extends OCSController {
  39. public function __construct(
  40. string $appName,
  41. IRequest $request,
  42. CapabilitiesManager $capabilitiesManager,
  43. private IUserSession $userSession,
  44. IUserManager $userManager,
  45. Manager $keyManager,
  46. private IConfig $config,
  47. private ChangesCheck $whatsNewService,
  48. private IFactory $langFactory,
  49. private Defaults $defaults,
  50. ) {
  51. parent::__construct($appName, $request, $capabilitiesManager, $userSession, $userManager, $keyManager);
  52. }
  53. /**
  54. * @NoAdminRequired
  55. *
  56. * Get the changes
  57. *
  58. * @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{}>
  59. *
  60. * 200: Changes returned
  61. * 204: No changes
  62. */
  63. public function get():DataResponse {
  64. $user = $this->userSession->getUser();
  65. if ($user === null) {
  66. throw new \RuntimeException("Acting user cannot be resolved");
  67. }
  68. $lastRead = $this->config->getUserValue($user->getUID(), 'core', 'whatsNewLastRead', 0);
  69. $currentVersion = $this->whatsNewService->normalizeVersion($this->config->getSystemValue('version'));
  70. if (version_compare($lastRead, $currentVersion, '>=')) {
  71. return new DataResponse([], Http::STATUS_NO_CONTENT);
  72. }
  73. try {
  74. $iterator = $this->langFactory->getLanguageIterator();
  75. $whatsNew = $this->whatsNewService->getChangesForVersion($currentVersion);
  76. $resultData = [
  77. 'changelogURL' => $whatsNew['changelogURL'],
  78. 'product' => $this->defaults->getProductName(),
  79. 'version' => $currentVersion,
  80. ];
  81. do {
  82. $lang = $iterator->current();
  83. if (isset($whatsNew['whatsNew'][$lang])) {
  84. $resultData['whatsNew'] = $whatsNew['whatsNew'][$lang];
  85. break;
  86. }
  87. $iterator->next();
  88. } while ($lang !== 'en' && $iterator->valid());
  89. return new DataResponse($resultData);
  90. } catch (DoesNotExistException $e) {
  91. return new DataResponse([], Http::STATUS_NO_CONTENT);
  92. }
  93. }
  94. /**
  95. * @NoAdminRequired
  96. *
  97. * Dismiss the changes
  98. *
  99. * @param string $version Version to dismiss the changes for
  100. *
  101. * @return DataResponse<Http::STATUS_OK, array<empty>, array{}>
  102. * @throws \OCP\PreConditionNotMetException
  103. * @throws DoesNotExistException
  104. *
  105. * 200: Changes dismissed
  106. */
  107. public function dismiss(string $version):DataResponse {
  108. $user = $this->userSession->getUser();
  109. if ($user === null) {
  110. throw new \RuntimeException("Acting user cannot be resolved");
  111. }
  112. $version = $this->whatsNewService->normalizeVersion($version);
  113. // checks whether it's a valid version, throws an Exception otherwise
  114. $this->whatsNewService->getChangesForVersion($version);
  115. $this->config->setUserValue($user->getUID(), 'core', 'whatsNewLastRead', $version);
  116. return new DataResponse();
  117. }
  118. }