1
0

WhatsNewController.php 3.8 KB

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