diff --git a/doc/notification.md b/doc/notification.md new file mode 100644 index 00000000000..e2d941ec02b --- /dev/null +++ b/doc/notification.md @@ -0,0 +1,38 @@ +## Notification API +[Back to the navigation](index.md) + +Listing notifications and marking them as read. +Wraps [GitHub Notification API](https://developer.github.com/v3/activity/notifications/). + +### List notifications + +```php +$issues = $client->api('notification')->all(); +``` + +Returns an array of unread notifications. + +### Include already read notifications, including participating, or since a certain date + +```php +$includingRead = true; +$participating = true; +$since = new DateTime('1970/01/01'); +$issues = $client->api('notification')->all($includingRead, $participating, $since); +``` + +Returns an array of all notifications + +### Mark notifications as read + +```php +$client->api('notification')->markRead(); +``` + +or up until a certain date + +```php +$client->api('notification')->markRead(new DateTime('2015/01/01')); +``` + +Marks all notifications as read up until the current date, unless a date is given diff --git a/lib/Github/Api/Notification.php b/lib/Github/Api/Notification.php new file mode 100644 index 00000000000..75831137df3 --- /dev/null +++ b/lib/Github/Api/Notification.php @@ -0,0 +1,60 @@ + + */ +class Notification extends AbstractApi +{ + /** + * Get a listing of notifications + * + * @link https://developer.github.com/v3/activity/notifications/ + * + * @param bool $includingRead + * @param bool $participating + * @param DateTime|null $since + * + * @return array array of notifications + */ + public function all($includingRead = false, $participating = false, DateTime $since = null) + { + $parameters = array( + 'all' => $includingRead, + 'participating' => $participating + ); + + if($since !== null) { + $parameters['since'] = $since->format(DateTime::ISO8601); + } + + return $this->get('notifications', $parameters); + } + + /** + * Marks all notifications as read from the current date + * Optionally give DateTime to mark as read before that date + * + * @link https://developer.github.com/v3/activity/notifications/#mark-as-read + * + * @param DateTime|null $since + */ + public function markRead(DateTime $since = null) + { + $parameters = array(); + + if($since !== null) { + $parameters['last_read_at'] = $since->format(DateTime::ISO8601); + } + + $this->put('notifications', $parameters); + } +} diff --git a/lib/Github/Client.php b/lib/Github/Client.php index c1779663858..64c4741c8f8 100644 --- a/lib/Github/Client.php +++ b/lib/Github/Client.php @@ -22,6 +22,8 @@ * @method Api\Issue issue() * @method Api\Issue issues() * @method Api\Markdown markdown() + * @method Api\Notification notification() + * @method Api\Notification notifications() * @method Api\Organization organization() * @method Api\Organization organizations() * @method Api\PullRequest pr() @@ -143,6 +145,11 @@ public function api($name) $api = new Api\Markdown($this); break; + case 'notification': + case 'notifications': + $api = new Api\Notification($this); + break; + case 'organization': case 'organizations': $api = new Api\Organization($this); diff --git a/test/Github/Tests/Api/NotificationTest.php b/test/Github/Tests/Api/NotificationTest.php new file mode 100644 index 00000000000..a212530fffd --- /dev/null +++ b/test/Github/Tests/Api/NotificationTest.php @@ -0,0 +1,104 @@ + false, + 'participating' => false, + ); + + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('get') + ->with('notifications', $parameters); + + $api->all(); + } + + /** + * @test + */ + public function shouldGetNotificationsSince() + { + $since = new DateTime('now'); + + $parameters = array( + 'all' => false, + 'participating' => false, + 'since' => $since->format(DateTime::ISO8601), + ); + + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('get') + ->with('notifications', $parameters); + + $api->all(false, false, $since); + } + + /** + * @test + */ + public function shouldGetNotificationsIncludingAndParticipating() + { + $parameters = array( + 'all' => true, + 'participating' => true, + ); + + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('get') + ->with('notifications', $parameters); + + $api->all(true, true); + } + + /** + * @test + */ + public function shouldMarkNotificationsAsRead() + { + $parameters = array(); + + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('put') + ->with('notifications', $parameters); + + $api->markRead(); + } + + /** + * @test + */ + public function shouldMarkNotificationsAsReadForGivenDate() + { + $since = new DateTime('now'); + + $parameters = array( + 'last_read_at' => $since->format(DateTime::ISO8601), + ); + + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('put') + ->with('notifications', $parameters); + + $api->markRead($since); + } + + protected function getApiClass() + { + return 'Github\Api\Notification'; + } +}