link api from web interface, make single note fetching work, redo note storage username
authorChristian Weiske <cweiske@cweiske.de>
Fri, 22 Aug 2014 19:11:20 +0000 (21:11 +0200)
committerChristian Weiske <cweiske@cweiske.de>
Fri, 22 Aug 2014 19:11:20 +0000 (21:11 +0200)
controller/apicontroller.php
controller/guicontroller.php
lib/notestorage.php
templates/appnavigation.php
templates/index.php

index a4361ef2d1af62f7dfd5b4794b6c06a9aa155c31..691d8abbe0cdaf99b3586c922c31194f2a2fc460 100644 (file)
@@ -148,7 +148,7 @@ class ApiController extends Controller
                 )
             )
         );
                 )
             )
         );
-        $syncdata = $this->notes->loadSyncData($username);
+        $syncdata = $this->notes->loadSyncData();
 
         $data = array(
             'user-name'  => $username,
 
         $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;
         }
 
         
         $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'])) {
     {
         $since = null;
         if (isset($_GET['since'])) {
@@ -224,9 +224,9 @@ class ApiController extends Controller
         }
 
         if (isset($_GET['include_notes']) && $_GET['include_notes']) {
         }
 
         if (isset($_GET['include_notes']) && $_GET['include_notes']) {
-            $notes = $this->notes->loadNotesFull($username, $since);
+            $notes = $this->notes->loadNotesFull($since);
         } else {
         } else {
-            $notes = $this->notes->loadNotesOverview($username, $since);
+            $notes = $this->notes->loadNotesOverview($since);
         }
 
         //work around bug https://bugzilla.gnome.org/show_bug.cgi?id=734313
         }
 
         //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;
 
             //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') {
             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
                 );
             } 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
      */
      * @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');
         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);
         }
 
             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)
     {
      */
     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);
             return true;
         }
 
         $oauth = new OAuth();
         $oauth->setDeps($this->deps);
         $oauth->verifyOAuthUser($username, $curUrl);
+
+        $this->notes->setUsername($username);
+        return true;
     }
 }
 ?>
     }
 }
 ?>
index 2c05750b0dc746dc3fa8902610060e26e5c7e0e1..b92d374e73fb0623d242f63e0f43aef9e06b31e8 100644 (file)
@@ -59,7 +59,12 @@ class GuiController extends Controller
         $this->checkDeps();
 
         $res = new TemplateResponse('grauphel', 'index');
         $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;
         $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', '');
     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;
 
         $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);
 
         $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', '');
         $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();
         $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(
     {
         //we need to remove the trailing / for tomdroid and conboy
         return rtrim(
index 63e1516f896c9a72af8d096adb06b66986c9895d..f3a904de996c9a303efe6cefe94ceb7cc623df03 100644 (file)
@@ -27,26 +27,35 @@ namespace OCA\Grauphel\Lib;
 class NoteStorage
 {
     protected $urlGen;
 class NoteStorage
 {
     protected $urlGen;
+    protected $username;
 
     public function __construct($urlGen)
     {
 
     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()
      *
     /**
      * Create a new sync data object for fresh users.
      * Used by loadSyncData()
      *
-     * @param string $username User name
-     *
      * @return SyncData New synchronization statistics
      */
      * @return SyncData New synchronization statistics
      */
-    protected function getNewSyncData($username)
+    protected function getNewSyncData()
     {
         $syncdata = new SyncData();
     {
         $syncdata = new SyncData();
-        $syncdata->initNew($username);
+        $syncdata->initNew($this->username);
         return $syncdata;
     }
 
         return $syncdata;
     }
 
+    public function getTags()
+    {
+    }
+
     /**
      * Updates the given $note object with data from $noteUpdate.
      * Sets the last-sync-revision to $syncRevision
     /**
      * 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.
      *
      * 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)
      */
      * @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` = ?',
     {
         $row = \OC_DB::executeAudited(
             'SELECT * FROM `*PREFIX*grauphel_syncdata`'
             . ' WHERE `syncdata_user` = ?',
-            array($username)
+            array($this->username)
         )->fetchRow();
 
         if ($row === false) {
         )->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'];
         } 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.
      *
     /**
      * Save synchronization data for the given user.
      *
-     * @param string   $username User name
      * @param SyncData $syncdata Synchronization data object
      *
      * @return void
      */
      * @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` = ?',
     {
         $row = \OC_DB::executeAudited(
             'SELECT * FROM `*PREFIX*grauphel_syncdata`'
             . ' WHERE `syncdata_user` = ?',
-            array($username)
+            array($this->username)
         )->fetchRow();
 
         if ($row === false) {
         )->fetchRow();
 
         if ($row === false) {
@@ -153,7 +159,7 @@ class NoteStorage
                 . '(`syncdata_user`, `syncdata_latest_sync_revision`, `syncdata_current_sync_guid`)'
                 . ' VALUES(?, ?, ?)';
             $params = array(
                 . '(`syncdata_user`, `syncdata_latest_sync_revision`, `syncdata_current_sync_guid`)'
                 . ' VALUES(?, ?, ?)';
             $params = array(
-                $username,
+                $this->username,
                 $syncdata->latestSyncRevision,
                 $syncdata->currentSyncGuid
             );
                 $syncdata->latestSyncRevision,
                 $syncdata->currentSyncGuid
             );
@@ -167,7 +173,7 @@ class NoteStorage
                 . ' `' . implode('` = ?, `', array_keys($data)) . '` = ?'
                 . ' WHERE `syncdata_user` = ?';
             $params = array_values($data);
                 . ' `' . implode('` = ?, `', array_keys($data)) . '` = ?'
                 . ' WHERE `syncdata_user` = ?';
             $params = array_values($data);
-            $params[] = $username;
+            $params[] = $this->username;
         }
         \OC_DB::executeAudited($sql, $params);
     }
         }
         \OC_DB::executeAudited($sql, $params);
     }
@@ -175,18 +181,17 @@ class NoteStorage
     /**
      * Load a note from the storage.
      *
     /**
      * 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
      */
      * @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` = ?',
     {
         $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) {
         )->fetchRow();
 
         if ($row === false) {
@@ -216,23 +221,22 @@ class NoteStorage
     /**
      * Save a note into storage.
      *
     /**
      * Save a note into storage.
      *
-     * @param string $username User name
-     * @param object $note     Note to save
+     * @param object $note Note to save
      *
      * @return void
      */
      *
      * @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` = ?',
     {
         $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
         )->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), '?')) . ')';
             $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);
                 . '`' . 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);
             $params[] = $note->guid;
         }
         \OC_DB::executeAudited($sql, $params);
@@ -252,17 +256,16 @@ class NoteStorage
     /**
      * Delete a note from storage.
      *
     /**
      * 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
      */
      *
      * @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` = ?',
     {
         \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
      *
      * 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
      */
      *
      * @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 = ?',
     {
         $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();
         );
 
         $notes = array();
@@ -296,7 +298,7 @@ class NoteStorage
                         $this->urlGen->linkToRoute(
                             'grauphel.api.note',
                             array(
                         $this->urlGen->linkToRoute(
                             'grauphel.api.note',
                             array(
-                                'username' => $username,
+                                'username' => $this->username,
                                 'guid' => $row['note_guid']
                             )
                         )
                                 '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
      *
      * 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
      */
      *
      * @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 = ?',
     {
         $result = \OC_DB::executeAudited(
             'SELECT * FROM `*PREFIX*grauphel_notes`'
             . ' WHERE note_user = ?',
-            array($username)
+            array($this->username)
         );
 
         $notes = array();
         );
 
         $notes = array();
index 19854f1bda304c71f30e987353ccc277ee48f5c3..78b4358bbaf6d467ff392388b03295a9ebdab119 100644 (file)
@@ -12,7 +12,7 @@
     <div id="app-settings-content" style="display: none;">
       <h2><?php p($l->t('Tomboy note server'));?></h2>
       <em><?php print_unescaped($l->t('Use the following sync server URL with tomboy/conboy/tomdroid:')); ?></em>
     <div id="app-settings-content" style="display: none;">
       <h2><?php p($l->t('Tomboy note server'));?></h2>
       <em><?php print_unescaped($l->t('Use the following sync server URL with tomboy/conboy/tomdroid:')); ?></em>
-      <div><input id="resturl" type="text" readonly="readonly" value="<?php p($_['apiurl']); ?>" /></div>
+      <div><input id="resturl" type="text" readonly="readonly" value="<?php p($_['apiroot']); ?>" /></div>
     </div>
   </div>
 </div>
     </div>
   </div>
 </div>
index e63df374b6ca8ed51c223f4e6d1b6b81a47cf441..bc51f4b8639ea6b4374dbbd16b48e769f45b58dd 100644 (file)
     <p>
       Use the following sync server URL with tomboy/conboy/tomdroid:
     </p>
     <p>
       Use the following sync server URL with tomboy/conboy/tomdroid:
     </p>
-    <pre><?php p($_['apiurl']); ?></pre>
+    <pre><?php p($_['apiroot']); ?></pre>
+    <p>
+      You may also explore the API yourself at
+      <a style="text-decoration: underline" href="<?php p($_['apiurl']); ?>">api/1.0</a>.
+    </p>
   </div>
 
   <?php isset($_['stats']) && $_['stats']->printPage(); ?>
   </div>
 
   <?php isset($_['stats']) && $_['stats']->printPage(); ?>