Skip to content

Commit

Permalink
Use OCSResponse instead of JSONResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
jvillafanez committed Mar 26, 2018
1 parent 20bfa16 commit 4a208ae
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 39 deletions.
26 changes: 13 additions & 13 deletions js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@
// Initial call to the notification endpoint
var self = this;
this.fetchDescendentV2(
OC.generateUrl('apps/notifications/api/v2/notifications?limit=3'),
OC.generateUrl('apps/notifications/api/v2/notifications?limit=3&format=json'),
function(result, textStatus, jqxhr) {
self.updateLastKnowId(jqxhr.getResponseHeader('OC-Last-Notification'));
if (result.data.length > 0) {
self.updateLastShownId(result.data[0].notification_id);
if (result.ocs.data.notifications.length > 0) {
self.updateLastShownId(result.ocs.data.notifications[0].notification_id);
}
});

Expand Down Expand Up @@ -178,13 +178,13 @@
type: 'GET'
}).done(function(result, textStatus, jqxhr) {
// Fill Array
$.each(result.data, function(index) {
var n = new self.Notif(result.data[index]);
$.each(result.ocs.data.notifications, function(index) {
var n = new self.Notif(result.ocs.data.notifications[index]);
self.notifications[n.getId()] = n;
self.appendToUI(n);
});
if (typeof result.next !== 'undefined') {
self.addShowMoreNotificationsButton(result.next);
if (typeof result.ocs.data.next !== 'undefined') {
self.addShowMoreNotificationsButton(result.ocs.data.next);
}
// Check if we have any, and notify the UI
if (self.numNotifications() !== 0) {
Expand Down Expand Up @@ -221,13 +221,13 @@
type: 'GET'
}).done(function(result, textStatus, jqxhr){
// Fill Array
$.each(result.data, function(index) {
var n = new self.Notif(result.data[index]);
$.each(result.ocs.data.notifications, function(index) {
var n = new self.Notif(result.ocs.data.notifications[index]);
self.notifications[n.getId()] = n;
self.addToUI(n);
});
if (typeof result.next !== 'undefined') {
self.addShowNewerNotificationsButton(result.next);
if (typeof result.ocs.data.next !== 'undefined') {
self.addShowNewerNotificationsButton(result.ocs.data.next);
}
// Check if we have any, and notify the UI
if (self.numNotifications() !== 0) {
Expand Down Expand Up @@ -255,7 +255,7 @@
pollingV2: function() {
var self = this;
var request = $.ajax({
url: OC.generateUrl('apps/notifications/api/v2/tracker/notifications/polling'),
url: OC.generateUrl('apps/notifications/api/v2/tracker/notifications/polling?format=json'),
type: 'GET'
}).done(function(result, textStatus, jqxhr){
var lastNotificationId = jqxhr.getResponseHeader('OC-Last-Notification');
Expand Down Expand Up @@ -310,7 +310,7 @@
// if we don't know the id shown, rely on the stored one
lastShownId = this.getLastShownId();
}
var targetUrl = OC.generateUrl('apps/notifications/api/v2/notifications?id=' + lastShownId + '&fetch=asc&limit=3');
var targetUrl = OC.generateUrl('apps/notifications/api/v2/notifications?id=' + lastShownId + '&fetch=asc&limit=3&format=json');
this.addShowNewerNotificationsButton(targetUrl);
},

Expand Down
57 changes: 31 additions & 26 deletions lib/Controller/EndpointV2Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\Http\OCSResponse;
use OCP\IConfig;
use OCP\IRequest;
use OCP\IUserSession;
Expand Down Expand Up @@ -67,11 +67,12 @@ public function __construct(Handler $handler, IManager $manager, IUserSession $u
* @param int $id the id of the notification
* @param string $fetch the fetch order
* @param int $limit the limit for the number of notifications to be returned
* @param string $format the format of the result: 'json' or 'xml'
*/
public function listNotifications($id = null, $fetch = 'desc', $limit = self::ENFORCED_LIST_LIMIT) {
public function listNotifications($id = null, $fetch = 'desc', $limit = self::ENFORCED_LIST_LIMIT, $format = 'json') {
$userObject = $this->userSession->getUser();
if ($userObject === null) {
return new JSONResponse(null, Http::STATUS_FORBIDDEN);
return new OCSResponse($format, Http::STATUS_FORBIDDEN, null, [], null, null, true);
}
$userid = $userObject->getUID();

Expand Down Expand Up @@ -118,39 +119,41 @@ public function listNotifications($id = null, $fetch = 'desc', $limit = self::EN
'id' => $data[count($data) - 1]['notification_id'],
'fetch' => strtolower($order),
'limit' => $maxResults,
'format' => $format,
]);

$jsonResponse = new JSONResponse([
'data' => $data,
$ocsResponse = new OCSResponse($format, Http::STATUS_OK, null, [
'notifications' => $data,
'next' => $url,
]);
], null, null, true);
} else {
$jsonResponse = new JSONResponse([
'data' => $data,
]);
$ocsResponse = new OCSResponse($format, Http::STATUS_OK, null, [
'notifications' => $data,
], null, null, true);
}
$jsonResponse->addHeader('OC-Last-Notification', $maxId);
return $jsonResponse;
$ocsResponse->addHeader('OC-Last-Notification', $maxId);
return $ocsResponse;
}

/**
* @NoAdminRequired
* @NoCSRFRequired
*
* @param int $id
* @param string $format either 'json' or 'xml'
* @return Result
*/
public function getNotification($id) {
public function getNotification($id, $format = 'json') {
$userObject = $this->userSession->getUser();
if ($userObject === null) {
return new JSONResponse(null, Http::STATUS_FORBIDDEN);
return new OCSResponse($format, Http::STATUS_FORBIDDEN, null, [], null, null, true);
}
$userid = $userObject->getUID();

$notification = $this->handler->getById($id, $userid);

if (!($notification instanceof INotification)) {
return new JSONResponse(null, HTTP::STATUS_NOT_FOUND);
return new OCSResponse($format, Http::STATUS_NOT_FOUND, null, [], null, null, true);
}

$language = $this->config->getUserValue($userid, 'core', 'lang', null);
Expand All @@ -159,39 +162,43 @@ public function getNotification($id) {
$notification = $this->manager->prepare($notification, $language);
} catch (\InvalidArgumentException $e) {
// The app was disabled
return new JSONResponse(null, HTTP::STATUS_NOT_FOUND);
return new OCSResponse($format, Http::STATUS_NOT_FOUND, null, [], null, null, true);
}

return new JSONResponse($this->notificationToArray($id, $notification));
return new OCSResponse($format, Http::STATUS_OK, null,
$this->notificationToArray($id, $notificationToArray), null, null, true);
}

/**
* @NoAdminRequired
* @NoCSRFRequired
*
* @param int $id
* @param string $format either 'json' or 'xml'
* @return Result
*/
public function deleteNotification($id) {
public function deleteNotification($id, $format = 'json') {
$userObject = $this->userSession->getUser();
if ($userObject === null) {
return new JSONResponse(null, Http::STATUS_FORBIDDEN);
return new OCSResponse($format, Http::STATUS_FORBIDDEN, null, [], null, null, true);
}
$userid = $userObject->getUID();

$this->handler->deleteById($id, $userid);
return new JSONResponse();
return new OCSResponse($format, Http::STATUS_OK, null, [], null, null, true);
}


/**
* @NoAdminRequired
* @NoCSRFRequired
* @param string $format either 'json' or 'xml'
*/
public function getLastNotificationId() {
public function getLastNotificationId($format = 'json') {
$userObject = $this->userSession->getUser();
if ($userObject === null) {
return new JSONResponse(null, Http::STATUS_FORBIDDEN);
return new OCSResponse($format, Http::STATUS_FORBIDDEN, null, [], null, null, true);
}
$userid = $userObject->getUID();

Expand All @@ -201,11 +208,9 @@ public function getLastNotificationId() {
$maxId = -1;
}

$jsonResponse = new JSONResponse([
'id' => $maxId,
]);
$jsonResponse->addHeader('OC-Last-Notification', $maxId);
return $jsonResponse;
$ocsResponse = new OCSResponse($format, Http::STATUS_OK, null, ['id' => $maxId], null, null, true);
$ocsResponse->addHeader('OC-Last-Notification', $maxId);
return $ocsResponse;
}

/**
Expand Down

0 comments on commit 4a208ae

Please sign in to comment.