#37: Use public database interface; fixes graupel on ownCloud 9
[grauphel.git] / controller / apicontroller.php
index ee16f80..23a47e7 100644 (file)
@@ -16,6 +16,7 @@ namespace OCA\Grauphel\Controller;
 use \OCP\AppFramework\Controller;
 use \OCP\AppFramework\Http\JSONResponse;
 
 use \OCP\AppFramework\Controller;
 use \OCP\AppFramework\Http\JSONResponse;
 
+use \OCA\Grauphel\Lib\Client;
 use \OCA\Grauphel\Lib\NoteStorage;
 use \OCA\Grauphel\Lib\OAuth;
 use \OCA\Grauphel\Lib\OAuthException;
 use \OCA\Grauphel\Lib\NoteStorage;
 use \OCA\Grauphel\Lib\OAuth;
 use \OCA\Grauphel\Lib\OAuthException;
@@ -105,10 +106,10 @@ class ApiController extends Controller
             'api-version' => '1.0',
         );
 
             'api-version' => '1.0',
         );
 
-        $client = $this->getClient();
+        $cl = new Client();
+        $client = $cl->getClient();
         if ($client !== false) {
         if ($client !== false) {
-            $data['oauth_authorize_url'] .= '?client='
-                . urlencode($this->getNiceClientName($client));
+            $data['oauth_authorize_url'] .= '?client=' . urlencode($client);
         }
 
         if ($authenticated) {
         }
 
         if ($authenticated) {
@@ -118,7 +119,7 @@ class ApiController extends Controller
                         'grauphel.api.user', array('username' => $username)
                     )
                 ),
                         'grauphel.api.user', array('username' => $username)
                     )
                 ),
-                'href' => null,//FIXME
+                'href' => null,
             );
         }
 
             );
         }
 
@@ -166,7 +167,9 @@ class ApiController extends Controller
                         'grauphel.api.notes', array('username' => $username)
                     )
                 ),
                         'grauphel.api.notes', array('username' => $username)
                     )
                 ),
-                'href'    => null,
+                'href'    => $this->deps->urlGen->getAbsoluteURL(
+                    $this->deps->urlGen->linkToRoute('grauphel.gui.index')
+                ),
             ),
             'latest-sync-revision' => $syncdata->latestSyncRevision,
             'current-sync-guid'    => $syncdata->currentSyncGuid,
             ),
             'latest-sync-revision' => $syncdata->latestSyncRevision,
             'current-sync-guid'    => $syncdata->currentSyncGuid,
@@ -213,7 +216,7 @@ class ApiController extends Controller
             )
         );
         $syncdata = $this->notes->loadSyncData();
             )
         );
         $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;
@@ -255,8 +258,8 @@ class ApiController extends Controller
             return;
         }
 
             return;
         }
 
-        //note that we have more data in $arPut than just our JSON
-        // request object merges it with other data
+        //Note that we have more data in $arPut than just our JSON.
+        // The request object merges it with other data.
         $arPut = $this->request->put;
 
         //structural validation
         $arPut = $this->request->put;
 
         //structural validation
@@ -284,23 +287,31 @@ class ApiController extends Controller
         }
 
         //update
         }
 
         //update
-        ++$syncdata->latestSyncRevision;
-        foreach ($arPut['note-changes'] as $noteUpdate) {
-            //owncloud converts object to array, so we reverse
-            $noteUpdate = (object) $noteUpdate;
-
-            $note = $this->notes->load($noteUpdate->guid);
-            if (isset($noteUpdate->command) && $noteUpdate->command == 'delete') {
-                $this->notes->delete($noteUpdate->guid);
-            } else {
-                $this->notes->update(
-                    $note, $noteUpdate, $syncdata->latestSyncRevision
-                );
-                $this->notes->save($note);
+        $db = \OC::$server->getDatabaseConnection();
+        $db->beginTransaction();
+        try {
+            ++$syncdata->latestSyncRevision;
+            foreach ($arPut['note-changes'] as $noteUpdate) {
+                //owncloud converts object to array, so we reverse
+                $noteUpdate = (object) $noteUpdate;
+
+                $note = $this->notes->load($noteUpdate->guid);
+                if (isset($noteUpdate->command) && $noteUpdate->command == 'delete') {
+                    $this->notes->delete($noteUpdate->guid);
+                } else {
+                    $this->notes->update(
+                        $note, $noteUpdate, $syncdata->latestSyncRevision
+                    );
+                    $this->notes->save($note);
+                }
             }
             }
-        }
 
 
-        $this->notes->saveSyncData($syncdata);
+            $this->notes->saveSyncData($syncdata);
+            $db->commit();
+        } catch (\DatabaseException $e) {
+            $db->rollBack();
+            throw $e;
+        }
     }
 
     /**
     }
 
     /**
@@ -333,31 +344,6 @@ class ApiController extends Controller
         return new JSONResponse($note);
     }
 
         return new JSONResponse($note);
     }
 
-    protected function getClient()
-    {
-        if (isset($_SERVER['HTTP_X_TOMBOY_CLIENT'])) {
-            $client = $_SERVER['HTTP_X_TOMBOY_CLIENT'];
-            $doublepos = strpos($client, ', org.tomdroid');
-            if ($doublepos !== false) {
-                //https://bugs.launchpad.net/tomdroid/+bug/1375436
-                //X-Tomboy-Client header is sent twice
-                $client = substr($client, 0, $doublepos);
-            }
-            return $client;
-        }
-
-        return false;
-    }
-
-    protected function getNiceClientName($client)
-    {
-        if (substr($client, 0, 12) == 'org.tomdroid') {
-            //org.tomdroid v0.7.5, build 14, Android v4.4.2, innotek GmbH/VirtualBox
-            return 'Tomdroid';
-        }
-        return $client;
-    }
-
     /**
      * Checks if the given user is authorized (by oauth token or normal login)
      *
     /**
      * Checks if the given user is authorized (by oauth token or normal login)
      *