Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Also show notes inside subdirectories #6

Merged
merged 6 commits into from
Jan 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion db/note.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace OCA\Notes\Db;

use OCP\Files\File;
use OCP\Files\Folder;
use OCP\AppFramework\Db\Entity;

/**
Expand All @@ -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()
Expand All @@ -32,6 +35,7 @@ class Note extends Entity {

public $modified;
public $title;
public $category;
public $content;
public $favorite = false;

Expand All @@ -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)]);
Expand Down
50 changes: 36 additions & 14 deletions service/notesservice.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace OCA\Notes\Service;

use OCP\Files\FileInfo;
use OCP\IL10N;
use OCP\Files\IRootFolder;
use OCP\Files\Folder;
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
}


Expand All @@ -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);
Expand All @@ -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) {
Expand All @@ -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));
}


Expand Down Expand Up @@ -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
*
Expand Down
3 changes: 3 additions & 0 deletions tests/unit/controller/NotesApiControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]
Expand Down Expand Up @@ -138,6 +140,7 @@ public function testGetHide(){

$this->assertEquals(json_encode([
'modified' => 123,
'category' => null,
'favorite' => false,
'id' => 3,
]), json_encode($response->getData()));
Expand Down
46 changes: 45 additions & 1 deletion tests/unit/db/NoteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
7 changes: 5 additions & 2 deletions tests/unit/service/NotesServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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')
Expand Down