Add an endpoint to delete all notifications

Signed-off-by: Joas Schilling <coding@schilljs.com>
This commit is contained in:
Joas Schilling 2018-01-29 16:53:55 +01:00
Родитель 508c1f6853
Коммит 5ce598d420
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 7076EA9751AACDDA
12 изменённых файлов: 83 добавлений и 6 удалений

Просмотреть файл

@ -24,6 +24,7 @@ return [
['name' => 'Endpoint#listNotifications', 'url' => '/api/{apiVersion}/notifications', 'verb' => 'GET', 'requirements' => ['apiVersion' => 'v(1|2)']],
['name' => 'Endpoint#getNotification', 'url' => '/api/{apiVersion}/notifications/{id}', 'verb' => 'GET', 'requirements' => ['apiVersion' => 'v(1|2)', 'id' => '\d+']],
['name' => 'Endpoint#deleteNotification', 'url' => '/api/{apiVersion}/notifications/{id}', 'verb' => 'DELETE', 'requirements' => ['apiVersion' => 'v(1|2)', 'id' => '\d+']],
['name' => 'Endpoint#deleteAllNotifications', 'url' => '/api/{apiVersion}/notifications', 'verb' => 'DELETE', 'requirements' => ['apiVersion' => 'v(1|2)']],
['name' => 'Push#registerDevice', 'url' => '/api/{apiVersion}/push', 'verb' => 'POST', 'requirements' => ['apiVersion' => 'v2']],
['name' => 'Push#removeDevice', 'url' => '/api/{apiVersion}/push', 'verb' => 'DELETE', 'requirements' => ['apiVersion' => 'v2']],
],

Просмотреть файл

@ -23,6 +23,7 @@ In order to find out if notifications is installed/enabled on the server you can
"list",
"get",
"delete",
"delete-all",
"icons",
"rich-strings"
]
@ -150,3 +151,10 @@ In order to get a single notification, you can send a GET request against `/ocs/
In order to delete a notification, you can send a DELETE request against `/ocs/v2.php/apps/notifications/api/v2/notifications/{id}`
## Deleting all notifications for a user
In order to delete all notifications, you can send a DELETE request against `/ocs/v2.php/apps/notifications/api/v2/notifications`
**Note:** This endpoint was added for Nextcloud 14, so check for the `delete-all` capability first.

Просмотреть файл

@ -42,6 +42,7 @@ class Capabilities implements ICapability {
'list',
'get',
'delete',
'delete-all',
'icons',
'rich-strings',
],

Просмотреть файл

@ -157,6 +157,16 @@ class EndpointController extends OCSController {
return new DataResponse();
}
/**
* @NoAdminRequired
*
* @return DataResponse
*/
public function deleteAllNotifications(): DataResponse {
$this->handler->deleteByUser($this->getCurrentUser());
return new DataResponse();
}
/**
* Get an Etag for the notification ids
*

Просмотреть файл

@ -91,6 +91,21 @@ class Handler {
$sql->execute();
}
/**
* Delete the notification of a given user
*
* @param string $user
*/
public function deleteByUser(string $user) {
$notification = $this->manager->createNotification();
try {
$notification->setUser($user);
} catch (\InvalidArgumentException $e) {
return;
}
$this->delete($notification);
}
/**
* Delete the notification matching the given id
*

Просмотреть файл

@ -214,6 +214,16 @@ class FeatureContext implements Context, SnippetAcceptingContext {
$this->sendingTo('DELETE', '/apps/notifications/api/' . $api . '/notifications/' . $this->deletedNotification);
}
/**
* @Then /^delete all notifications on (v\d+)$/
*
* @param string $api
*/
public function deleteAllNotification($api) {
PHPUnit_Framework_Assert::assertNotEmpty($this->notificationIds);
$this->sendingTo('DELETE', '/apps/notifications/api/' . $api . '/notifications');
}
/**
* @Then /^status code is ([0-9]*)$/
*

Просмотреть файл

@ -40,3 +40,12 @@ Feature: delete-notifications
And delete last notification on v1
And status code is 200
And user "test1" has 2 notifications on v1 missing the last one
Scenario: Delete all notifications
Given user "test1" has notifications
Given user "test1" has notifications
Given user "test1" has notifications
Then user "test1" has 3 notifications on v2
And delete all notifications on v2
And status code is 200
And user "test1" has 0 notifications on v2

Просмотреть файл

@ -40,3 +40,12 @@ Feature: delete-notifications
And delete last notification on v2
And status code is 200
And user "test1" has 2 notifications on v2 missing the last one
Scenario: Delete all notifications
Given user "test1" has notifications
Given user "test1" has notifications
Given user "test1" has notifications
Then user "test1" has 3 notifications on v1
And delete all notifications on v1
And status code is 200
And user "test1" has 0 notifications on v1

Просмотреть файл

@ -32,11 +32,11 @@ use OCA\Notifications\Tests\Unit\TestCase;
*/
class RoutesTest extends TestCase {
public function testRoutes() {
$routes = include(__DIR__ . '/../../../appinfo/routes.php');
$routes = include __DIR__ . '/../../../appinfo/routes.php';
$this->assertInternalType('array', $routes);
$this->assertCount(1, $routes);
$this->assertArrayHasKey('ocs', $routes);
$this->assertInternalType('array', $routes['ocs']);
$this->assertCount(5, $routes['ocs']);
$this->assertCount(6, $routes['ocs']);
}
}

Просмотреть файл

@ -35,6 +35,7 @@ class CapabilitiesTest extends TestCase {
'list',
'get',
'delete',
'delete-all',
'icons',
'rich-strings',
],

Просмотреть файл

@ -400,6 +400,23 @@ class EndpointControllerTest extends TestCase {
$this->assertSame(Http::STATUS_NOT_FOUND, $response->getStatus());
}
/**
* @dataProvider dataDeleteNotification
* @param int $_
* @param string $username
*/
public function testDeleteAllNotifications($_, $username) {
$controller = $this->getController([], $username);
$this->handler->expects($this->once())
->method('deleteByUser')
->with($username);
$response = $controller->deleteAllNotifications();
$this->assertInstanceOf(DataResponse::class, $response);
$this->assertSame(Http::STATUS_OK, $response->getStatus());
}
public function dataNotificationToArray() {
return [
['v1', 42, 'app1', 'user1', 1234, 'type1', 42, 'subject1', '', [], 'message1', 'richMessage 1', ['richMessage param'], 'link1', 'icon1', [], []],

Просмотреть файл

@ -32,8 +32,4 @@ require_once __DIR__ . '/../../../../lib/base.php';
// Fix for "Autoload path not allowed: .../notifications/tests/testcase.php"
\OC_App::loadApp('notifications');
if(!class_exists('PHPUnit_Framework_TestCase')) {
require_once('PHPUnit/Autoload.php');
}
OC_Hook::clear();