diff --git a/db/note.php b/db/note.php index 2da98c143..f277ea061 100644 --- a/db/note.php +++ b/db/note.php @@ -12,6 +12,7 @@ namespace OCA\Notes\Db; use OCP\Files\File; +use OCP\Files\Folder; use OCP\AppFramework\Db\Entity; /** @@ -22,6 +23,8 @@ * @method void setModified(integer $value) * @method string getTitle() * @method void setTitle(string $value) + * @method string getCategory() + * @method void setCategory(string $value) * @method string getContent() * @method void setContent(string $value) * @method boolean getFavorite() @@ -32,6 +35,7 @@ class Note extends Entity { public $modified; public $title; + public $category; public $content; public $favorite = false; @@ -44,12 +48,14 @@ public function __construct() { * @param File $file * @return static */ - public static function fromFile(File $file, $tags=[]){ + public static function fromFile(File $file, Folder $notesFolder, $tags=[]){ $note = new static(); $note->setId($file->getId()); $note->setContent($file->getContent()); $note->setModified($file->getMTime()); $note->setTitle(pathinfo($file->getName(),PATHINFO_FILENAME)); // remove extension + $subdir = substr(dirname($file->getPath()), strlen($notesFolder->getPath())+1); + $note->setCategory($subdir ? $subdir : null); if(is_array($tags) && in_array(\OC\Tags::TAG_FAVORITE, $tags)) { $note->setFavorite(true); //unset($tags[array_search(\OC\Tags::TAG_FAVORITE, $tags)]); diff --git a/service/notesservice.php b/service/notesservice.php index 125ec2f2c..b14006b14 100644 --- a/service/notesservice.php +++ b/service/notesservice.php @@ -11,6 +11,7 @@ namespace OCA\Notes\Service; +use OCP\Files\FileInfo; use OCP\IL10N; use OCP\Files\IRootFolder; use OCP\Files\Folder; @@ -42,13 +43,11 @@ public function __construct (IRootFolder $root, IL10N $l10n) { * @return array with all notes in the current directory */ public function getAll ($userId){ - $folder = $this->getFolderForUser($userId); - $files = $folder->getDirectoryListing(); + $notesFolder = $this->getFolderForUser($userId); + $notes = $this->gatherNoteFiles($notesFolder); $filesById = []; - foreach($files as $file) { - if($this->isNote($file)) { - $filesById[$file->getId()] = $file; - } + foreach($notes as $note) { + $filesById[$note->getId()] = $note; } $tagger = \OC::$server->getTagManager()->load('files'); if($tagger===null) { @@ -59,7 +58,7 @@ public function getAll ($userId){ $notes = []; foreach($filesById as $id=>$file) { - $notes[] = Note::fromFile($file, array_key_exists($id, $tags) ? $tags[$id] : []); + $notes[] = Note::fromFile($file, $notesFolder, array_key_exists($id, $tags) ? $tags[$id] : []); } return $notes; @@ -75,7 +74,7 @@ public function getAll ($userId){ */ public function get ($id, $userId) { $folder = $this->getFolderForUser($userId); - return Note::fromFile($this->getFileById($folder, $id), $this->getTags($id)); + return Note::fromFile($this->getFileById($folder, $id), $folder, $this->getTags($id)); } private function getTags ($id) { @@ -104,7 +103,7 @@ public function create ($userId) { $path = $this->generateFileName($folder, $title, "txt", -1); $file = $folder->newFile($path); - return Note::fromFile($file); + return Note::fromFile($file, $folder); } @@ -119,8 +118,9 @@ public function create ($userId) { * @return \OCA\Notes\Db\Note the updated note */ public function update ($id, $content, $userId, $mtime=0) { - $folder = $this->getFolderForUser($userId); - $file = $this->getFileById($folder, $id); + $notesFolder = $this->getFolderForUser($userId); + $file = $this->getFileById($notesFolder, $id); + $folder = $file->getParent(); // generate content from the first line of the title $splitContent = preg_split("/\R/", $content, 2); @@ -140,9 +140,9 @@ public function update ($id, $content, $userId, $mtime=0) { // generate filename if there were collisions $currentFilePath = $file->getPath(); - $basePath = '/' . $userId . '/files/Notes/'; + $basePath = pathinfo($file->getPath(), PATHINFO_DIRNAME); $fileExtension = pathinfo($file->getName(), PATHINFO_EXTENSION); - $newFilePath = $basePath . $this->generateFileName($folder, $title, $fileExtension, $id); + $newFilePath = $basePath . '/' . $this->generateFileName($folder, $title, $fileExtension, $id); // if the current path is not the new path, the file has to be renamed if($currentFilePath !== $newFilePath) { @@ -155,7 +155,7 @@ public function update ($id, $content, $userId, $mtime=0) { $file->touch($mtime); } - return Note::fromFile($file, $this->getTags($id)); + return Note::fromFile($file, $notesFolder, $this->getTags($id)); } @@ -262,6 +262,28 @@ private function generateFileName (Folder $folder, $title, $extension, $id) { } } + + /** + * gather note files in given directory and all subdirectories + * @param Folder $folder + * @return array + */ + private function gatherNoteFiles ($folder) { + $notes = []; + $nodes = $folder->getDirectoryListing(); + foreach($nodes as $node) { + if($node->getType() === FileInfo::TYPE_FOLDER) { + $notes = array_merge($notes, $this->gatherNoteFiles($node)); + continue; + } + if($this->isNote($node)) { + $notes[] = $node; + } + } + return $notes; + } + + /** * test if file is a note * diff --git a/tests/unit/controller/NotesApiControllerTest.php b/tests/unit/controller/NotesApiControllerTest.php index 13a205f6c..3ce8cab6d 100644 --- a/tests/unit/controller/NotesApiControllerTest.php +++ b/tests/unit/controller/NotesApiControllerTest.php @@ -88,11 +88,13 @@ public function testGetAllHide(){ $this->assertEquals(json_encode([ [ 'modified' => 123, + 'category' => null, 'favorite' => false, 'id' => 3, ], [ 'modified' => 111, + 'category' => null, 'favorite' => false, 'id' => 4, ] @@ -138,6 +140,7 @@ public function testGetHide(){ $this->assertEquals(json_encode([ 'modified' => 123, + 'category' => null, 'favorite' => false, 'id' => 3, ]), json_encode($response->getData())); diff --git a/tests/unit/db/NoteTest.php b/tests/unit/db/NoteTest.php index 6352531c4..bc17c91fd 100644 --- a/tests/unit/db/NoteTest.php +++ b/tests/unit/db/NoteTest.php @@ -31,11 +31,55 @@ public function testFromFile(){ $file->expects($this->any()) ->method('getName') ->will($this->returnValue('file.txt')); + $file->expects($this->any()) + ->method('getPath') + ->will($this->returnValue('/john/files/Notes/file.txt')); + + $notesFolder = $this->getMockBuilder('OCP\Files\Folder')->getMock(); + $notesFolder->expects($this->any()) + ->method('getPath') + ->will($this->returnValue('/john/files/Notes')); + + + $note = Note::fromFile($file, $notesFolder); + + $this->assertEquals(3, $note->getId()); + $this->assertEquals(323, $note->getModified()); + $this->assertEquals(null, $note->getCategory()); + $this->assertEquals('file', $note->getTitle()); + $this->assertEquals('content', $note->getContent()); + } + + + public function testFromFileInSubdir(){ + $file = $this->getMockBuilder('OCP\Files\File')->getMock(); + $file->expects($this->any()) + ->method('getId') + ->will($this->returnValue(3)); + $file->expects($this->any()) + ->method('getContent') + ->will($this->returnValue('content')); + $file->expects($this->any()) + ->method('getMTime') + ->will($this->returnValue(323)); + $file->expects($this->any()) + ->method('getName') + ->will($this->returnValue('file.txt')); + $file->expects($this->any()) + ->method('getPath') + ->will($this->returnValue('/john/files/Notes/mycategory/file.txt')); + + $notesFolder = $this->getMockBuilder('OCP\Files\Folder')->getMock(); + $notesFolder->expects($this->any()) + ->method('getPath') + ->will($this->returnValue('/john/files/Notes')); + - $note = Note::fromFile($file); + $note = Note::fromFile($file, $notesFolder); $this->assertEquals(3, $note->getId()); $this->assertEquals(323, $note->getModified()); + $this->assertEquals('mycategory', $note->getCategory()); $this->assertEquals('file', $note->getTitle()); $this->assertEquals('content', $note->getContent()); } diff --git a/tests/unit/service/NotesServiceTest.php b/tests/unit/service/NotesServiceTest.php index 072ee797d..f9b63cf06 100644 --- a/tests/unit/service/NotesServiceTest.php +++ b/tests/unit/service/NotesServiceTest.php @@ -35,7 +35,7 @@ public function setUp(){ $this->service = new NotesService($this->root, $this->l10n); } - private function createNode($name, $type, $mime, $mtime=0, $content='', $id=0, $path='/') { + private function createNode($name, $type, $mime, $mtime=0, $content='', $id=0, $subdir='') { if ($type === 'folder') { $iface = 'OCP\Files\Folder'; } else { @@ -60,7 +60,10 @@ private function createNode($name, $type, $mime, $mtime=0, $content='', $id=0, $ ->will($this->returnValue($id)); $node->expects($this->any()) ->method('getPath') - ->will($this->returnValue($path)); + ->will($this->returnValue('/' . $this->userId . '/files/Notes/'.$subdir.($subdir ? '/' : '').$name)); + $node->expects($this->any()) + ->method('getParent') + ->will($this->returnValue($this->userFolder)); if ($type === 'file') { $node->expects($this->any()) ->method('getContent')