WhatsNewController.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OC\Core\Controller;
  25. use OC\CapabilitiesManager;
  26. use OC\Security\IdentityProof\Manager;
  27. use OC\Updater\ChangesCheck;
  28. use OCP\AppFramework\Db\DoesNotExistException;
  29. use OCP\AppFramework\Http;
  30. use OCP\AppFramework\Http\DataResponse;
  31. use OCP\Defaults;
  32. use OCP\IConfig;
  33. use OCP\IRequest;
  34. use OCP\IUserManager;
  35. use OCP\IUserSession;
  36. use OCP\L10N\IFactory;
  37. class WhatsNewController extends OCSController {
  38. /** @var IConfig */
  39. protected $config;
  40. /** @var IUserSession */
  41. private $userSession;
  42. /** @var ChangesCheck */
  43. private $whatsNewService;
  44. /** @var IFactory */
  45. private $langFactory;
  46. /** @var Defaults */
  47. private $defaults;
  48. public function __construct(
  49. string $appName,
  50. IRequest $request,
  51. CapabilitiesManager $capabilitiesManager,
  52. IUserSession $userSession,
  53. IUserManager $userManager,
  54. Manager $keyManager,
  55. IConfig $config,
  56. ChangesCheck $whatsNewService,
  57. IFactory $langFactory,
  58. Defaults $defaults
  59. ) {
  60. parent::__construct($appName, $request, $capabilitiesManager, $userSession, $userManager, $keyManager);
  61. $this->config = $config;
  62. $this->userSession = $userSession;
  63. $this->whatsNewService = $whatsNewService;
  64. $this->langFactory = $langFactory;
  65. $this->defaults = $defaults;
  66. }
  67. /**
  68. * @NoAdminRequired
  69. */
  70. public function get():DataResponse {
  71. $user = $this->userSession->getUser();
  72. if ($user === null) {
  73. throw new \RuntimeException("Acting user cannot be resolved");
  74. }
  75. $lastRead = $this->config->getUserValue($user->getUID(), 'core', 'whatsNewLastRead', 0);
  76. $currentVersion = $this->whatsNewService->normalizeVersion($this->config->getSystemValue('version'));
  77. if (version_compare($lastRead, $currentVersion, '>=')) {
  78. return new DataResponse([], Http::STATUS_NO_CONTENT);
  79. }
  80. try {
  81. $iterator = $this->langFactory->getLanguageIterator();
  82. $whatsNew = $this->whatsNewService->getChangesForVersion($currentVersion);
  83. $resultData = [
  84. 'changelogURL' => $whatsNew['changelogURL'],
  85. 'product' => $this->defaults->getProductName(),
  86. 'version' => $currentVersion,
  87. ];
  88. do {
  89. $lang = $iterator->current();
  90. if (isset($whatsNew['whatsNew'][$lang])) {
  91. $resultData['whatsNew'] = $whatsNew['whatsNew'][$lang];
  92. break;
  93. }
  94. $iterator->next();
  95. } while ($lang !== 'en' && $iterator->valid());
  96. return new DataResponse($resultData);
  97. } catch (DoesNotExistException $e) {
  98. return new DataResponse([], Http::STATUS_NO_CONTENT);
  99. }
  100. }
  101. /**
  102. * @NoAdminRequired
  103. *
  104. * @throws \OCP\PreConditionNotMetException
  105. * @throws DoesNotExistException
  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. }