BackgroundJob.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. *
  7. * @license AGPL-3.0
  8. *
  9. * This code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License, version 3,
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>
  20. *
  21. */
  22. namespace OCA\UpdateNotification\Notification;
  23. use OC\BackgroundJob\TimedJob;
  24. use OC\Installer;
  25. use OC\Updater\VersionCheck;
  26. use OCP\App\IAppManager;
  27. use OCP\Http\Client\IClientService;
  28. use OCP\IConfig;
  29. use OCP\IGroup;
  30. use OCP\IGroupManager;
  31. use OCP\Notification\IManager;
  32. class BackgroundJob extends TimedJob {
  33. /** @var IConfig */
  34. protected $config;
  35. /** @var IManager */
  36. protected $notificationManager;
  37. /** @var IGroupManager */
  38. protected $groupManager;
  39. /** @var IAppManager */
  40. protected $appManager;
  41. /** @var IClientService */
  42. protected $client;
  43. /** @var string[] */
  44. protected $users;
  45. /**
  46. * NotificationBackgroundJob constructor.
  47. *
  48. * @param IConfig $config
  49. * @param IManager $notificationManager
  50. * @param IGroupManager $groupManager
  51. * @param IAppManager $appManager
  52. * @param IClientService $client
  53. */
  54. public function __construct(IConfig $config, IManager $notificationManager, IGroupManager $groupManager, IAppManager $appManager, IClientService $client) {
  55. // Run once a day
  56. $this->setInterval(60 * 60 * 24);
  57. $this->config = $config;
  58. $this->notificationManager = $notificationManager;
  59. $this->groupManager = $groupManager;
  60. $this->appManager = $appManager;
  61. $this->client = $client;
  62. }
  63. protected function run($argument) {
  64. $this->checkCoreUpdate();
  65. $this->checkAppUpdates();
  66. }
  67. /**
  68. * Check for ownCloud update
  69. */
  70. protected function checkCoreUpdate() {
  71. if (in_array($this->getChannel(), ['daily', 'git'])) {
  72. // "These aren't the update channels you're looking for." - Ben Obi-Wan Kenobi
  73. return;
  74. }
  75. $updater = $this->createVersionCheck();
  76. $status = $updater->check();
  77. if (isset($status['version'])) {
  78. $this->createNotifications('core', $status['version'], $status['versionstring']);
  79. }
  80. }
  81. /**
  82. * Check all installed apps for updates
  83. */
  84. protected function checkAppUpdates() {
  85. $apps = $this->appManager->getInstalledApps();
  86. foreach ($apps as $app) {
  87. $update = $this->isUpdateAvailable($app);
  88. if ($update !== false) {
  89. $this->createNotifications($app, $update);
  90. }
  91. }
  92. }
  93. /**
  94. * Create notifications for this app version
  95. *
  96. * @param string $app
  97. * @param string $version
  98. * @param string $visibleVersion
  99. */
  100. protected function createNotifications($app, $version, $visibleVersion = '') {
  101. $lastNotification = $this->config->getAppValue('updatenotification', $app, false);
  102. if ($lastNotification === $version) {
  103. // We already notified about this update
  104. return;
  105. } else if ($lastNotification !== false) {
  106. // Delete old updates
  107. $this->deleteOutdatedNotifications($app, $lastNotification);
  108. }
  109. $notification = $this->notificationManager->createNotification();
  110. $notification->setApp('updatenotification')
  111. ->setDateTime(new \DateTime())
  112. ->setObject($app, $version);
  113. if ($visibleVersion !== '') {
  114. $notification->setSubject('update_available', ['version' => $visibleVersion]);
  115. } else {
  116. $notification->setSubject('update_available');
  117. }
  118. foreach ($this->getUsersToNotify() as $uid) {
  119. $notification->setUser($uid);
  120. $this->notificationManager->notify($notification);
  121. }
  122. $this->config->setAppValue('updatenotification', $app, $version);
  123. }
  124. /**
  125. * @return string[]
  126. */
  127. protected function getUsersToNotify() {
  128. if ($this->users !== null) {
  129. return $this->users;
  130. }
  131. $notifyGroups = json_decode($this->config->getAppValue('updatenotification', 'notify_groups', '["admin"]'), true);
  132. $this->users = [];
  133. foreach ($notifyGroups as $group) {
  134. $groupToNotify = $this->groupManager->get($group);
  135. if ($groupToNotify instanceof IGroup) {
  136. foreach ($groupToNotify->getUsers() as $user) {
  137. $this->users[$user->getUID()] = true;
  138. }
  139. }
  140. }
  141. $this->users = array_keys($this->users);
  142. return $this->users;
  143. }
  144. /**
  145. * Delete notifications for old updates
  146. *
  147. * @param string $app
  148. * @param string $version
  149. */
  150. protected function deleteOutdatedNotifications($app, $version) {
  151. $notification = $this->notificationManager->createNotification();
  152. $notification->setApp('updatenotification')
  153. ->setObject($app, $version);
  154. $this->notificationManager->markProcessed($notification);
  155. }
  156. /**
  157. * @return VersionCheck
  158. */
  159. protected function createVersionCheck() {
  160. return new VersionCheck(
  161. $this->client,
  162. $this->config
  163. );
  164. }
  165. /**
  166. * @return string
  167. */
  168. protected function getChannel() {
  169. return \OC_Util::getChannel();
  170. }
  171. /**
  172. * @param string $app
  173. * @return string|false
  174. */
  175. protected function isUpdateAvailable($app) {
  176. return Installer::isUpdateAvailable($app, \OC::$server->getAppFetcher());
  177. }
  178. }