Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
['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']],
],
Expand Down
18 changes: 18 additions & 0 deletions css/styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,24 @@
}
}

.dismiss-all {
display: flex;
opacity: 0.5;
padding: 10px;
margin-left: auto;
margin-right: auto;

&:hover,
.icon-close {
opacity: 1;
cursor: pointer;
}

.icon-close {
margin-right: 5px;
}
}

/* Menu arrow */
&:after {
right: 101px;
Expand Down
8 changes: 8 additions & 0 deletions docs/ocs-endpoint-v2.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
Expand Down Expand Up @@ -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.
17 changes: 16 additions & 1 deletion js-src/components/root.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
</div>
<div class="notification-container">
<div class="notification-wrapper" v-if="notifications.length">
<notification v-for="(n, index) in notifications" v-bind="n" :key="n.notification_id" @remove="onRemove" ></notification>
<notification v-for="n in notifications" v-bind="n" :key="n.notification_id" @remove="onRemove" ></notification>
<div class="dismiss-all" v-if="notifications.length > 2" @click="onDismissAll">
<span class="icon icon-close svg" :title="t('notifications', 'Dismiss all notifications')"></span> {{ t('notifications', 'Dismiss all notifications') }}
</div>
</div>
<div class="emptycontent" v-else>
<h2>{{ t('notifications', 'No notifications') }}</h2>
Expand Down Expand Up @@ -54,6 +57,18 @@
},

methods: {
onDismissAll: function() {
$.ajax({
url: OC.linkToOCS('apps/notifications/api/v2', 2) + 'notifications',
type: 'DELETE',
success: function () {
this.notifications = [];
}.bind(this),
error: function () {
OC.Notification.showTemporary(t('notifications', 'Failed to dismiss all notifications'));
}
});
},
onRemove: function(index) {
this.notifications.splice(index, 1);
}
Expand Down
1 change: 1 addition & 0 deletions lib/Capabilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public function getCapabilities() {
'list',
'get',
'delete',
'delete-all',
'icons',
'rich-strings',
],
Expand Down
10 changes: 10 additions & 0 deletions lib/Controller/EndpointController.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,16 @@ public function deleteNotification($id = 0) {
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
*
Expand Down
15 changes: 15 additions & 0 deletions lib/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,21 @@ public function delete(INotification $notification) {
$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
*
Expand Down
10 changes: 10 additions & 0 deletions tests/Integration/features/bootstrap/FeatureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,16 @@ public function deleteNotification($toDelete, $api) {
$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]*)$/
*
Expand Down
9 changes: 9 additions & 0 deletions tests/Integration/features/delete-notifications-v1.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 9 additions & 0 deletions tests/Integration/features/delete-notifications-v2.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions tests/Unit/AppInfo/RoutesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@
*/
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']);
}
}
1 change: 1 addition & 0 deletions tests/Unit/CapabilitiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public function testGetCapabilities() {
'list',
'get',
'delete',
'delete-all',
'icons',
'rich-strings',
],
Expand Down
17 changes: 17 additions & 0 deletions tests/Unit/Controller/EndpointControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,23 @@ public function testDeleteNotificationNoId() {
$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', [], []],
Expand Down
4 changes: 0 additions & 4 deletions tests/Unit/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,4 @@
// 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();