1
0

WhatsNewController.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. public function __construct(
  39. string $appName,
  40. IRequest $request,
  41. CapabilitiesManager $capabilitiesManager,
  42. private IUserSession $userSession,
  43. IUserManager $userManager,
  44. Manager $keyManager,
  45. private IConfig $config,
  46. private ChangesCheck $whatsNewService,
  47. private IFactory $langFactory,
  48. private Defaults $defaults,
  49. ) {
  50. parent::__construct($appName, $request, $capabilitiesManager, $userSession, $userManager, $keyManager);
  51. }
  52. /**
  53. * @NoAdminRequired
  54. */
  55. public function get():DataResponse {
  56. $user = $this->userSession->getUser();
  57. if ($user === null) {
  58. throw new \RuntimeException("Acting user cannot be resolved");
  59. }
  60. $lastRead = $this->config->getUserValue($user->getUID(), 'core', 'whatsNewLastRead', 0);
  61. $currentVersion = $this->whatsNewService->normalizeVersion($this->config->getSystemValue('version'));
  62. if (version_compare($lastRead, $currentVersion, '>=')) {
  63. return new DataResponse([], Http::STATUS_NO_CONTENT);
  64. }
  65. try {
  66. $iterator = $this->langFactory->getLanguageIterator();
  67. $whatsNew = $this->whatsNewService->getChangesForVersion($currentVersion);
  68. $resultData = [
  69. 'changelogURL' => $whatsNew['changelogURL'],
  70. 'product' => $this->defaults->getProductName(),
  71. 'version' => $currentVersion,
  72. ];
  73. do {
  74. $lang = $iterator->current();
  75. if (isset($whatsNew['whatsNew'][$lang])) {
  76. $resultData['whatsNew'] = $whatsNew['whatsNew'][$lang];
  77. break;
  78. }
  79. $iterator->next();
  80. } while ($lang !== 'en' && $iterator->valid());
  81. return new DataResponse($resultData);
  82. } catch (DoesNotExistException $e) {
  83. return new DataResponse([], Http::STATUS_NO_CONTENT);
  84. }
  85. }
  86. /**
  87. * @NoAdminRequired
  88. *
  89. * @throws \OCP\PreConditionNotMetException
  90. * @throws DoesNotExistException
  91. */
  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. }