From cd37bde4ef0747a11c1221e937027fe17f2894fe Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Fri, 22 Aug 2014 21:11:20 +0200 Subject: [PATCH 1/1] link api from web interface, make single note fetching work, redo note storage username --- controller/apicontroller.php | 53 ++++++++++++++----------- controller/guicontroller.php | 16 +++++--- lib/notestorage.php | 77 ++++++++++++++++++------------------ templates/appnavigation.php | 2 +- templates/index.php | 6 ++- 5 files changed, 85 insertions(+), 69 deletions(-) diff --git a/controller/apicontroller.php b/controller/apicontroller.php index a4361ef..691d8ab 100644 --- a/controller/apicontroller.php +++ b/controller/apicontroller.php @@ -148,7 +148,7 @@ class ApiController extends Controller ) ) ); - $syncdata = $this->notes->loadSyncData($username); + $syncdata = $this->notes->loadSyncData(); $data = array( 'user-name' => $username, @@ -185,8 +185,8 @@ class ApiController extends Controller ) ) ); - $syncdata = $this->notes->loadSyncData($username); - return $this->fetchNotes($username, $syncdata); + $syncdata = $this->notes->loadSyncData(); + return $this->fetchNotes($syncdata); } /** @@ -206,17 +206,17 @@ class ApiController extends Controller ) ) ); - $syncdata = $this->notes->loadSyncData($username); + $syncdata = $this->notes->loadSyncData(); $res = $this->handleNoteSave($username, $syncdata); if ($res instanceof \OCP\AppFramework\Http\Response) { return $res; } - return $this->fetchNotes($username, $syncdata); + return $this->fetchNotes($syncdata); } - protected function fetchNotes($username, $syncdata) + protected function fetchNotes($syncdata) { $since = null; if (isset($_GET['since'])) { @@ -224,9 +224,9 @@ class ApiController extends Controller } if (isset($_GET['include_notes']) && $_GET['include_notes']) { - $notes = $this->notes->loadNotesFull($username, $since); + $notes = $this->notes->loadNotesFull($since); } else { - $notes = $this->notes->loadNotesOverview($username, $since); + $notes = $this->notes->loadNotesOverview($since); } //work around bug https://bugzilla.gnome.org/show_bug.cgi?id=734313 @@ -283,18 +283,18 @@ class ApiController extends Controller //owncloud converts object to array, so we reverse $noteUpdate = (object) $noteUpdate; - $note = $this->notes->load($username, $noteUpdate->guid); + $note = $this->notes->load($noteUpdate->guid); if (isset($noteUpdate->command) && $noteUpdate->command == 'delete') { - $this->notes->delete($username, $noteUpdate->guid); + $this->notes->delete($noteUpdate->guid); } else { $this->notes->update( $note, $noteUpdate, $syncdata->latestSyncRevision ); - $this->notes->save($username, $note); + $this->notes->save($note); } } - $this->notes->saveSyncData($username, $syncdata); + $this->notes->saveSyncData($syncdata); } /** @@ -304,17 +304,19 @@ class ApiController extends Controller * @NoCSRFRequired * @PublicPage */ - public function note() + public function note($username, $guid) { - //FIXME - $deps = Dependencies::get(); - $username = $deps->urlGen->loadUsername(); - $guid = $deps->urlGen->loadGuid(); - $oauth = new \OAuth(); - $oauth->setDeps($deps); - $oauth->verifyOAuthUser($username, $deps->urlGen->note($username, $guid)); + $this->verifyUser( + $username, + $this->deps->urlGen->getAbsoluteURL( + $this->deps->urlGen->linkToRoute( + 'grauphel.api.note', + array('username' => $username, 'guid' => $guid) + ) + ) + ); - $note = $this->notes->load($username, $guid, false); + $note = $this->notes->load($guid, false); if ($note === null) { header('HTTP/1.0 404 Not Found'); header('Content-type: text/plain'); @@ -322,8 +324,7 @@ class ApiController extends Controller exit(1); } - $data = array('note' => array($note)); - $deps->renderer->sendJson($data); + return new JSONResponse($note); } /** @@ -335,13 +336,17 @@ class ApiController extends Controller */ protected function verifyUser($username, $curUrl) { - if ($this->user !== null && $this->user->getUID() == $username) { + if ($this->user !== null && $this->user->getUid() == $username) { + $this->notes->setUsername($username); return true; } $oauth = new OAuth(); $oauth->setDeps($this->deps); $oauth->verifyOAuthUser($username, $curUrl); + + $this->notes->setUsername($username); + return true; } } ?> diff --git a/controller/guicontroller.php b/controller/guicontroller.php index 2c05750..b92d374 100644 --- a/controller/guicontroller.php +++ b/controller/guicontroller.php @@ -59,7 +59,12 @@ class GuiController extends Controller $this->checkDeps(); $res = new TemplateResponse('grauphel', 'index'); - $res->setParams(array('apiurl' => $this->getApiUrl())); + $res->setParams( + array( + 'apiroot' => $this->getApiRootUrl(), + 'apiurl' => $this->urlGen->linkToRoute('grauphel.api.index') + ) + ); $this->addNavigation($res); $this->addStats($res); return $res; @@ -68,7 +73,7 @@ class GuiController extends Controller protected function addNavigation(TemplateResponse $res) { $nav = new \OCP\Template('grauphel', 'appnavigation', ''); - $nav->assign('apiurl', $this->getApiUrl()); + $nav->assign('apiroot', $this->getApiRootUrl()); $params = $res->getParams(); $params['appNavigation'] = $nav; @@ -83,11 +88,12 @@ class GuiController extends Controller $username = $this->user->getUid(); $notes = new \OCA\Grauphel\Lib\NoteStorage($this->urlGen); + $notes->setUsername($username); $tokens = new \OCA\Grauphel\Lib\TokenStorage(); $nav = new \OCP\Template('grauphel', 'indexStats', ''); - $nav->assign('notes', count($notes->loadNotesOverview($username))); - $nav->assign('syncrev', $notes->loadSyncData($username)->latestSyncRevision); + $nav->assign('notes', count($notes->loadNotesOverview())); + $nav->assign('syncrev', $notes->loadSyncData()->latestSyncRevision); $nav->assign('tokens', count($tokens->loadForUser($username, 'access'))); $params = $res->getParams(); @@ -102,7 +108,7 @@ class GuiController extends Controller } } - protected function getApiUrl() + protected function getApiRootUrl() { //we need to remove the trailing / for tomdroid and conboy return rtrim( diff --git a/lib/notestorage.php b/lib/notestorage.php index 63e1516..f3a904d 100644 --- a/lib/notestorage.php +++ b/lib/notestorage.php @@ -27,26 +27,35 @@ namespace OCA\Grauphel\Lib; class NoteStorage { protected $urlGen; + protected $username; public function __construct($urlGen) { - $this->urlGen = $urlGen; + $this->urlGen = $urlGen; } + + public function setUsername($username) + { + $this->username = $username; + } + /** * Create a new sync data object for fresh users. * Used by loadSyncData() * - * @param string $username User name - * * @return SyncData New synchronization statistics */ - protected function getNewSyncData($username) + protected function getNewSyncData() { $syncdata = new SyncData(); - $syncdata->initNew($username); + $syncdata->initNew($this->username); return $syncdata; } + public function getTags() + { + } + /** * Updates the given $note object with data from $noteUpdate. * Sets the last-sync-revision to $syncRevision @@ -107,21 +116,19 @@ class NoteStorage * Loads synchronization data for the given user. * Creates fresh sync data if there are none for the user. * - * @param string $username User name - * * @return SyncData Synchronization statistics (revision, sync guid) */ - public function loadSyncData($username) + public function loadSyncData() { $row = \OC_DB::executeAudited( 'SELECT * FROM `*PREFIX*grauphel_syncdata`' . ' WHERE `syncdata_user` = ?', - array($username) + array($this->username) )->fetchRow(); if ($row === false) { - $syncdata = $this->getNewSyncData($username); - $this->saveSyncData($username, $syncdata); + $syncdata = $this->getNewSyncData($this->username); + $this->saveSyncData($this->username, $syncdata); } else { $syncdata = new SyncData(); $syncdata->latestSyncRevision = (int) $row['syncdata_latest_sync_revision']; @@ -134,17 +141,16 @@ class NoteStorage /** * Save synchronization data for the given user. * - * @param string $username User name * @param SyncData $syncdata Synchronization data object * * @return void */ - public function saveSyncData($username, SyncData $syncdata) + public function saveSyncData(SyncData $syncdata) { $row = \OC_DB::executeAudited( 'SELECT * FROM `*PREFIX*grauphel_syncdata`' . ' WHERE `syncdata_user` = ?', - array($username) + array($this->username) )->fetchRow(); if ($row === false) { @@ -153,7 +159,7 @@ class NoteStorage . '(`syncdata_user`, `syncdata_latest_sync_revision`, `syncdata_current_sync_guid`)' . ' VALUES(?, ?, ?)'; $params = array( - $username, + $this->username, $syncdata->latestSyncRevision, $syncdata->currentSyncGuid ); @@ -167,7 +173,7 @@ class NoteStorage . ' `' . implode('` = ?, `', array_keys($data)) . '` = ?' . ' WHERE `syncdata_user` = ?'; $params = array_values($data); - $params[] = $username; + $params[] = $this->username; } \OC_DB::executeAudited($sql, $params); } @@ -175,18 +181,17 @@ class NoteStorage /** * Load a note from the storage. * - * @param string $username User name * @param string $guid Note identifier * @param boolean $createNew Create a new note if it does not exist * * @return object Note object, NULL if !$createNew and note does not exist */ - public function load($username, $guid, $createNew = true) + public function load($guid, $createNew = true) { $row = \OC_DB::executeAudited( 'SELECT * FROM `*PREFIX*grauphel_notes`' . ' WHERE `note_user` = ? AND `note_guid` = ?', - array($username, $guid) + array($this->username, $guid) )->fetchRow(); if ($row === false) { @@ -216,23 +221,22 @@ class NoteStorage /** * Save a note into storage. * - * @param string $username User name - * @param object $note Note to save + * @param object $note Note to save * * @return void */ - public function save($username, $note) + public function save($note) { $row = \OC_DB::executeAudited( 'SELECT * FROM `*PREFIX*grauphel_notes`' . ' WHERE `note_user` = ? AND `note_guid` = ?', - array($username, $note->guid) + array($this->username, $note->guid) )->fetchRow(); $data = $this->rowFromNote($note); if ($row === false) { //INSERT - $data['note_user'] = $username; + $data['note_user'] = $this->username; $sql = 'INSERT INTO `*PREFIX*grauphel_notes`' . ' (`' . implode('`, `', array_keys($data)) . '`)' . ' VALUES(' . implode(', ', array_fill(0, count($data), '?')) . ')'; @@ -243,7 +247,7 @@ class NoteStorage . '`' . implode('` = ?, `', array_keys($data)) . '` = ?' . ' WHERE `note_user` = ? AND `note_guid` = ?'; $params = array_values($data); - $params[] = $username; + $params[] = $this->username; $params[] = $note->guid; } \OC_DB::executeAudited($sql, $params); @@ -252,17 +256,16 @@ class NoteStorage /** * Delete a note from storage. * - * @param string $username User name - * @param object $guid ID of the note + * @param object $guid ID of the note * * @return void */ - public function delete($username, $guid) + public function delete($guid) { \OC_DB::executeAudited( 'DELETE FROM `*PREFIX*grauphel_notes`' . ' WHERE `note_user` = ? AND `note_guid` = ?', - array($username, $guid) + array($this->username, $guid) ); } @@ -270,18 +273,17 @@ class NoteStorage * Load notes for the given user in short form. * Optionally only those changed after $since revision * - * @param string $username User name - * @param integer $since Revision number after which the notes changed + * @param integer $since Revision number after which the notes changed * * @return array Array of short note objects */ - public function loadNotesOverview($username, $since = null) + public function loadNotesOverview($since = null) { $result = \OC_DB::executeAudited( 'SELECT `note_guid`, `note_title`, `note_last_sync_revision`' . ' FROM `*PREFIX*grauphel_notes`' . ' WHERE note_user = ?', - array($username) + array($this->username) ); $notes = array(); @@ -296,7 +298,7 @@ class NoteStorage $this->urlGen->linkToRoute( 'grauphel.api.note', array( - 'username' => $username, + 'username' => $this->username, 'guid' => $row['note_guid'] ) ) @@ -314,17 +316,16 @@ class NoteStorage * Load notes for the given user in full form. * Optionally only those changed after $since revision * - * @param string $username User name - * @param integer $since Revision number after which the notes changed + * @param integer $since Revision number after which the notes changed * * @return array Array of full note objects */ - public function loadNotesFull($username, $since = null) + public function loadNotesFull($since = null) { $result = \OC_DB::executeAudited( 'SELECT * FROM `*PREFIX*grauphel_notes`' . ' WHERE note_user = ?', - array($username) + array($this->username) ); $notes = array(); diff --git a/templates/appnavigation.php b/templates/appnavigation.php index 19854f1..78b4358 100644 --- a/templates/appnavigation.php +++ b/templates/appnavigation.php @@ -12,7 +12,7 @@ diff --git a/templates/index.php b/templates/index.php index e63df37..bc51f4b 100644 --- a/templates/index.php +++ b/templates/index.php @@ -38,7 +38,11 @@

Use the following sync server URL with tomboy/conboy/tomdroid:

-
+
+

+ You may also explore the API yourself at + api/1.0. +

printPage(); ?> -- 2.30.2