Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable26] feat(API): Add an endpoint to check for existance of notification ids #1509

Merged
merged 7 commits into from
Apr 12, 2023
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
5 changes: 5 additions & 0 deletions .github/workflows/phpunit-mysql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@ jobs:
working-directory: apps/${{ env.APP_NAME }}
run: composer run test:integration

- name: Print logs
if: always()
run: |
cat data/nextcloud.log

- name: Skipped
# Fail the action when neither unit nor integration tests ran
if: steps.check_phpunit.outcome == 'failure' && steps.check_integration.outcome == 'failure'
Expand Down
5 changes: 5 additions & 0 deletions .github/workflows/phpunit-oci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ jobs:
working-directory: apps/${{ env.APP_NAME }}
run: composer run test:integration

- name: Print logs
if: always()
run: |
cat data/nextcloud.log

- name: Skipped
# Fail the action when neither unit nor integration tests ran
if: steps.check_phpunit.outcome == 'failure' && steps.check_integration.outcome == 'failure'
Expand Down
5 changes: 5 additions & 0 deletions .github/workflows/phpunit-pgsql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ jobs:
working-directory: apps/${{ env.APP_NAME }}
run: composer run test:integration

- name: Print logs
if: always()
run: |
cat data/nextcloud.log

- name: Skipped
# Fail the action when neither unit nor integration tests ran
if: steps.check_phpunit.outcome == 'failure' && steps.check_integration.outcome == 'failure'
Expand Down
5 changes: 5 additions & 0 deletions .github/workflows/phpunit-sqlite.yml
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ jobs:
working-directory: apps/${{ env.APP_NAME }}
run: composer run test:integration

- name: Print logs
if: always()
run: |
cat data/nextcloud.log

- name: Skipped
# Fail the action when neither unit nor integration tests ran
if: steps.check_phpunit.outcome == 'failure' && steps.check_integration.outcome == 'failure'
Expand Down
5 changes: 4 additions & 1 deletion appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
declare(strict_types=1);

/**
* @copyright Copyright (c) 2023, Joas Schilling <[email protected]>
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
* @author Joas Schilling <[email protected]>
*
* @copyright Copyright (c) 2016, ownCloud, Inc.
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
Expand All @@ -26,6 +28,7 @@
'ocs' => [
['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#confirmIdsForUser', 'url' => '/api/{apiVersion}/notifications/exists', 'verb' => 'POST', 'requirements' => ['apiVersion' => 'v(1|2)']],
['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']],
Expand Down
9 changes: 9 additions & 0 deletions docs/ocs-endpoint-v2.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,12 @@ In order to delete a notification, you can send a DELETE request against `/ocs/v
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.


## Check existance of notifications for a user

In order to check whether a set of notification ids (max. 200 items per request) still exist for a user,
a client can send a POST request against `/ocs/v2.php/apps/notifications/api/v2/notifications/exists` with
the integer list provided as `ids` field on the POST body.

**Note:** This endpoint was added for Nextcloud 27, so check for the `exists` capability first.
3 changes: 2 additions & 1 deletion lib/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
declare(strict_types=1);

/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
* @author Joas Schilling <[email protected]>
*
* @copyright Copyright (c) 2016, ownCloud, Inc.
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
Expand Down
4 changes: 3 additions & 1 deletion lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

declare(strict_types=1);
/**
* @copyright Copyright (c) 2023, Joas Schilling <[email protected]>
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
* @author Joas Schilling <[email protected]>
*
* @copyright Copyright (c) 2016, ownCloud, Inc.
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
Expand Down
6 changes: 4 additions & 2 deletions lib/BackgroundJob/GenerateUserSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

declare(strict_types=1);
/**
* @copyright Copyright (c) 2021 Joas Schilling <[email protected]>
* @copyright Copyright (c) 2021, Joas Schilling <[email protected]>
*
* @license GNU AGPL version 3 or any later version
* @author Joas Schilling <[email protected]>
*
* @license AGPL-3.0-or-later
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
Expand Down
6 changes: 4 additions & 2 deletions lib/BackgroundJob/SendNotificationMails.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

declare(strict_types=1);
/**
* @copyright Copyright (c) 2021 Julien Barnoin <[email protected]>
* @copyright Copyright (c) 2021, Julien Barnoin <[email protected]>
*
* @license GNU AGPL version 3 or any later version
* @author Julien Barnoin <[email protected]>
*
* @license AGPL-3.0-or-later
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
Expand Down
4 changes: 3 additions & 1 deletion lib/Capabilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
declare(strict_types=1);

/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
* @author Joas Schilling <[email protected]>
*
* @copyright Copyright (c) 2016, ownCloud, Inc.
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
Expand Down Expand Up @@ -50,6 +51,7 @@ public function getCapabilities(): array {
'rich-strings',
'action-web',
'user-status',
'exists',
],
'push' => [
'devices',
Expand Down
4 changes: 2 additions & 2 deletions lib/Command/Generate.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
declare(strict_types=1);

/**
* @copyright Copyright (c) 2017 Joas Schilling <[email protected]>
* @copyright Copyright (c) 2017, Joas Schilling <[email protected]>
*
* @author Joas Schilling <[email protected]>
*
* @license GNU AGPL version 3 or any later version
* @license AGPL-3.0-or-later
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
Expand Down
4 changes: 2 additions & 2 deletions lib/Command/TestPush.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
declare(strict_types=1);

/**
* @copyright Copyright (c) 2017 Joas Schilling <[email protected]>
* @copyright Copyright (c) 2017, Joas Schilling <[email protected]>
*
* @author Joas Schilling <[email protected]>
*
* @license GNU AGPL version 3 or any later version
* @license AGPL-3.0-or-later
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
Expand Down
4 changes: 2 additions & 2 deletions lib/Controller/APIController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
declare(strict_types=1);

/**
* @copyright Copyright (c) 2017 Joas Schilling <[email protected]>
* @copyright Copyright (c) 2017, Joas Schilling <[email protected]>
*
* @author Joas Schilling <[email protected]>
*
* @license GNU AGPL version 3 or any later version
* @license AGPL-3.0-or-later
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
Expand Down
33 changes: 32 additions & 1 deletion lib/Controller/EndpointController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
declare(strict_types=1);

/**
* @copyright Copyright (c) 2023, Joas Schilling <[email protected]>
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
* @author Joas Schilling <[email protected]>
*
* @copyright Copyright (c) 2016, ownCloud, Inc.
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
Expand Down Expand Up @@ -169,6 +171,35 @@ public function getNotification(string $apiVersion, int $id): DataResponse {
return new DataResponse($this->notificationToArray($id, $notification, $apiVersion, $hasActiveTalkDesktop));
}

/**
* @NoAdminRequired
*
* @param string $apiVersion
* @param int[] $ids
* @return DataResponse
*/
public function confirmIdsForUser(string $apiVersion, array $ids): DataResponse {
if (!$this->manager->hasNotifiers()) {
return new DataResponse([], Http::STATUS_OK);
}

if (empty($ids)) {
return new DataResponse([], Http::STATUS_OK);
}

if (count($ids) > 200) {
return new DataResponse([], Http::STATUS_BAD_REQUEST);
}

$ids = array_unique(array_filter(array_map(
static fn ($id) => is_numeric($id) ? (int) $id : 0,
$ids
)));

$existingIds = $this->handler->confirmIdsForUser($this->getCurrentUser(), $ids);
return new DataResponse($existingIds, Http::STATUS_OK);
}

/**
* @NoAdminRequired
*
Expand Down
6 changes: 4 additions & 2 deletions lib/Controller/PushController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
declare(strict_types=1);

/**
* @copyright Copyright (c) 2017 Joas Schilling <[email protected]>
* @copyright Copyright (c) 2017, Joas Schilling <[email protected]>
*
* @license GNU AGPL version 3 or any later version
* @author Joas Schilling <[email protected]>
*
* @license AGPL-3.0-or-later
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
Expand Down
6 changes: 4 additions & 2 deletions lib/Controller/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
declare(strict_types=1);

/**
* @copyright Copyright (c) 2021 Julien Barnoin <[email protected]>
* @copyright Copyright (c) 2021, Julien Barnoin <[email protected]>
*
* @license GNU AGPL version 3 or any later version
* @author Julien Barnoin <[email protected]>
*
* @license AGPL-3.0-or-later
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
Expand Down
2 changes: 1 addition & 1 deletion lib/Exceptions/NotificationNotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*
* @author Joas Schilling <[email protected]>
*
* @license GNU AGPL version 3 or any later version
* @license AGPL-3.0-or-later
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
Expand Down
6 changes: 4 additions & 2 deletions lib/FakeUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

declare(strict_types=1);
/**
* @copyright Copyright (c) 2022 Joas Schilling <[email protected]>
* @copyright Copyright (c) 2022, Joas Schilling <[email protected]>
*
* @license GNU AGPL version 3 or any later version
* @author Joas Schilling <[email protected]>
*
* @license AGPL-3.0-or-later
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
Expand Down
28 changes: 27 additions & 1 deletion lib/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
declare(strict_types=1);

/**
* @copyright Copyright (c) 2023, Joas Schilling <[email protected]>
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
* @author Joas Schilling <[email protected]>
*
* @copyright Copyright (c) 2016, ownCloud, Inc.
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
Expand Down Expand Up @@ -217,6 +219,30 @@ public function getById(int $id, string $user): INotification {
}
}

/**
* Confirm that the notification ids still exist for the user
*
* @param string $user
* @param int[] $ids
* @return int[]
*/
public function confirmIdsForUser(string $user, array $ids): array {
$query = $this->connection->getQueryBuilder();
$query->select('notification_id')
->from('notifications')
->where($query->expr()->in('notification_id', $query->createNamedParameter($ids, IQueryBuilder::PARAM_INT_ARRAY)))
->andWhere($query->expr()->eq('user', $query->createNamedParameter($user)));
$result = $query->executeQuery();

$existing = [];
while ($row = $result->fetch()) {
$existing[] = (int) $row['notification_id'];
}
$result->closeCursor();

return $existing;
}

/**
* Get the notifications after (and excluding) the given id
*
Expand Down
4 changes: 2 additions & 2 deletions lib/Listener/BeforeTemplateRenderedListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
declare(strict_types=1);

/**
* @copyright Copyright (c) 2021 Joas Schilling <[email protected]>
* @copyright Copyright (c) 2021, Joas Schilling <[email protected]>
*
* @author Joas Schilling <[email protected]>
*
* @license GNU AGPL version 3 or any later version
* @license AGPL-3.0-or-later
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
Expand Down
4 changes: 2 additions & 2 deletions lib/Listener/PostLoginListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
declare(strict_types=1);

/**
* @copyright Copyright (c) 2022 Nikita Toponen <[email protected]>
* @copyright Copyright (c) 2022, Nikita Toponen <[email protected]>
*
* @author Nikita Toponen <[email protected]>
*
* @license GNU AGPL version 3 or any later version
* @license AGPL-3.0-or-later
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
Expand Down
4 changes: 2 additions & 2 deletions lib/Listener/UserCreatedListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

declare(strict_types=1);
/**
* @copyright Copyright (c) 2022 Nikita Toponen <[email protected]>
* @copyright Copyright (c) 2022, Nikita Toponen <[email protected]>
*
* @author Nikita Toponen <[email protected]>
*
* @license GNU AGPL version 3 or any later version
* @license AGPL-3.0-or-later
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
Expand Down
4 changes: 2 additions & 2 deletions lib/Listener/UserDeletedListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
declare(strict_types=1);

/**
* @copyright Copyright (c) 2020 Daniel Kesselberg <[email protected]>
* @copyright Copyright (c) 2020, Daniel Kesselberg <[email protected]>
*
* @author Daniel Kesselberg <[email protected]>
*
* @license GNU AGPL version 3 or any later version
* @license AGPL-3.0-or-later
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
Expand Down
Loading