Skip to content

Commit

Permalink
Add instance category while checking new updates
Browse files Browse the repository at this point in the history
Signed-off-by: Benjamin Gaussorgues <benjamin.gaussorgues@nextcloud.com>
  • Loading branch information
Altahrim committed Jul 10, 2023
1 parent 0d78334 commit 299c1b8
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 59 deletions.
46 changes: 13 additions & 33 deletions apps/updatenotification/lib/Notification/BackgroundJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,49 +35,28 @@
use OCP\IConfig;
use OCP\IGroup;
use OCP\IGroupManager;
use OCP\IUserManager;
use OCP\Notification\IManager;

class BackgroundJob extends TimedJob {
protected $connectionNotifications = [3, 7, 14, 30];

/** @var IConfig */
protected $config;

/** @var IManager */
protected $notificationManager;

/** @var IGroupManager */
protected $groupManager;

/** @var IAppManager */
protected $appManager;

/** @var IClientService */
protected $client;

/** @var Installer */
protected $installer;

/** @var string[] */
protected $users;

public function __construct(ITimeFactory $timeFactory,
IConfig $config,
IManager $notificationManager,
IGroupManager $groupManager,
IAppManager $appManager,
IClientService $client,
Installer $installer) {
public function __construct(
ITimeFactory $timeFactory,
protected IConfig $config,
protected IManager $notificationManager,
protected IUserManager $userManager,
protected IGroupManager $groupManager,
protected IAppManager $appManager,
protected IClientService $client,
protected Installer $installer
) {
parent::__construct($timeFactory);
// Run once a day
$this->setInterval(60 * 60 * 24);

$this->config = $config;
$this->notificationManager = $notificationManager;
$this->groupManager = $groupManager;
$this->appManager = $appManager;
$this->client = $client;
$this->installer = $installer;
}

protected function run($argument) {
Expand Down Expand Up @@ -264,7 +243,8 @@ protected function deleteOutdatedNotifications($app, $version) {
protected function createVersionCheck(): VersionCheck {
return new VersionCheck(
$this->client,
$this->config
$this->config,
$this->userManager,
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
use OCP\IGroup;
use OCP\IGroupManager;
use OCP\IUser;
use OCP\IUserManager;
use OCP\Notification\IManager;
use OCP\Notification\INotification;
use PHPUnit\Framework\MockObject\MockObject;
Expand All @@ -63,6 +64,7 @@ protected function setUp(): void {

$this->config = $this->createMock(IConfig::class);
$this->notificationManager = $this->createMock(IManager::class);
$this->userManager = $this->createMock(IUserManager::class);
$this->groupManager = $this->createMock(IGroupManager::class);
$this->appManager = $this->createMock(IAppManager::class);
$this->client = $this->createMock(IClientService::class);
Expand All @@ -80,6 +82,7 @@ protected function getJob(array $methods = []) {
$this->timeFactory,
$this->config,
$this->notificationManager,
$this->userManager,
$this->groupManager,
$this->appManager,
$this->client,
Expand Down
43 changes: 29 additions & 14 deletions lib/private/Updater/VersionCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,15 @@

use OCP\Http\Client\IClientService;
use OCP\IConfig;
use OCP\IUserManager;
use OCP\Util;

class VersionCheck {
/** @var IClientService */
private $clientService;

/** @var IConfig */
private $config;

/**
* @param IClientService $clientService
* @param IConfig $config
*/
public function __construct(IClientService $clientService,
IConfig $config) {
$this->clientService = $clientService;
$this->config = $config;
public function __construct(
private IClientService $clientService,
private IConfig $config,
private IUserManager $userManager,
) {
}


Expand Down Expand Up @@ -81,6 +73,8 @@ public function check() {
$version['php_major'] = PHP_MAJOR_VERSION;
$version['php_minor'] = PHP_MINOR_VERSION;
$version['php_release'] = PHP_RELEASE_VERSION;
$version['category'] = $this->computeCategory();
$version['isSubscriber'] = strlen($this->config->getAppValue('support', 'subscription_key', '0')) > 0;
$versionString = implode('x', $version);

//fetch xml data from updater
Expand Down Expand Up @@ -130,4 +124,25 @@ protected function getUrlContent($url) {
$response = $client->get($url);
return $response->getBody();
}

private function computeCategory() {
$categoryBoundaries = [
100,
500,
1000,
5000,
10000,
100000,
1000000,
];

$nbUsers = $this->userManager->countSeenUsers();
foreach ($categoryBoundaries as $categoryId => $boundary) {
if ($nbUsers <= $boundary) {
return $categoryId;
}
}

return $categoryId + 1;
}
}
35 changes: 23 additions & 12 deletions tests/lib/Updater/VersionCheckTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use OC\Updater\VersionCheck;
use OCP\Http\Client\IClientService;
use OCP\IConfig;
use OCP\IUserManager;
use OCP\Util;

class VersionCheckTest extends \Test\TestCase {
Expand All @@ -44,7 +45,7 @@ protected function setUp(): void {

$this->updater = $this->getMockBuilder(VersionCheck::class)
->setMethods(['getUrlContent'])
->setConstructorArgs([$clientService, $this->config])
->setConstructorArgs([$clientService, $this->config, $this->createMock(IUserManager::class)])
->getMock();
}

Expand All @@ -53,7 +54,7 @@ protected function setUp(): void {
* @return string
*/
private function buildUpdateUrl($baseUrl) {
return $baseUrl . '?version='.implode('x', Util::getVersion()).'xinstalledatxlastupdatedatx'.\OC_Util::getChannel().'xxx'.PHP_MAJOR_VERSION.'x'.PHP_MINOR_VERSION.'x'.PHP_RELEASE_VERSION;
return $baseUrl . '?version='.implode('x', Util::getVersion()).'xinstalledatxlastupdatedatx'.\OC_Util::getChannel().'xxx'.PHP_MAJOR_VERSION.'x'.PHP_MINOR_VERSION.'x'.PHP_RELEASE_VERSION.'x0x1';
}

public function testCheckInCache() {
Expand Down Expand Up @@ -102,19 +103,21 @@ public function testCheckWithoutUpdateUrl() {
->with('has_internet_connection', true)
->willReturn(true);
$this->config
->expects($this->exactly(4))
->expects($this->exactly(5))
->method('getAppValue')
->withConsecutive(
['core', 'lastupdatedat'],
['core', 'installedat'],
['core', 'installedat'],
['core', 'lastupdatedat'],
['support', 'subscription_key', '0'],
)
->willReturnOnConsecutiveCalls(
'0',
'installedat',
'installedat',
'lastupdatedat'
'lastupdatedat',
'subscription_key',
);
$this->config
->expects($this->once())
Expand Down Expand Up @@ -154,19 +157,21 @@ public function testCheckWithInvalidXml() {
->with('has_internet_connection', true)
->willReturn(true);
$this->config
->expects($this->exactly(4))
->expects($this->exactly(5))
->method('getAppValue')
->withConsecutive(
['core', 'lastupdatedat'],
['core', 'installedat'],
['core', 'installedat'],
['core', 'lastupdatedat'],
['support', 'subscription_key', '0'],
)
->willReturnOnConsecutiveCalls(
'0',
'installedat',
'installedat',
'lastupdatedat'
'lastupdatedat',
'subscription_key'
);
$this->config
->expects($this->once())
Expand Down Expand Up @@ -208,19 +213,21 @@ public function testCheckWithEmptyValidXmlResponse() {
->with('has_internet_connection', true)
->willReturn(true);
$this->config
->expects($this->exactly(4))
->expects($this->exactly(5))
->method('getAppValue')
->withConsecutive(
['core', 'lastupdatedat'],
['core', 'installedat'],
['core', 'installedat'],
['core', 'lastupdatedat'],
['support', 'subscription_key', '0'],
)
->willReturnOnConsecutiveCalls(
'0',
'installedat',
'installedat',
'lastupdatedat'
'lastupdatedat',
'subscription_key',
);
$this->config
->expects($this->once())
Expand Down Expand Up @@ -261,19 +268,21 @@ public function testCheckWithEmptyInvalidXmlResponse() {
->with('has_internet_connection', true)
->willReturn(true);
$this->config
->expects($this->exactly(4))
->expects($this->exactly(5))
->method('getAppValue')
->withConsecutive(
['core', 'lastupdatedat'],
['core', 'installedat'],
['core', 'installedat'],
['core', 'lastupdatedat'],
['support', 'subscription_key', '0'],
)
->willReturnOnConsecutiveCalls(
'0',
'installedat',
'installedat',
'lastupdatedat'
'lastupdatedat',
'subscription_key',
);
$this->config
->expects($this->once())
Expand Down Expand Up @@ -315,19 +324,21 @@ public function testCheckWithMissingAttributeXmlResponse() {
->with('has_internet_connection', true)
->willReturn(true);
$this->config
->expects($this->exactly(4))
->expects($this->exactly(5))
->method('getAppValue')
->withConsecutive(
['core', 'lastupdatedat'],
['core', 'installedat'],
['core', 'installedat'],
['core', 'lastupdatedat'],
['support', 'subscription_key', '0'],
)
->willReturnOnConsecutiveCalls(
'0',
'installedat',
'installedat',
'lastupdatedat'
'lastupdatedat',
'subscription_key'
);
$this->config
->expects($this->once())
Expand Down

0 comments on commit 299c1b8

Please sign in to comment.