Skip to content
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
2 changes: 1 addition & 1 deletion controller/settingscontroller.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ public function set() {
* @NoAdminRequired
*/
public function get() {
return new JSONResponse($this->service->get());
return new JSONResponse($this->service->getAll());
}
}
4 changes: 4 additions & 0 deletions css/notes.css
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@
opacity: 1 !important;
}

#app-settings-content > div + div {
padding-top: 2ex;
}

.note-search span {
background-position: 0 center;
background-origin: content-box;
Expand Down
6 changes: 6 additions & 0 deletions js/app/controllers/notessettingscontroller.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ app.controller('NotesSettingsController',
function($scope, Restangular, $document) {
'use strict';

$scope.extensions = ['.txt', '.md'];

Restangular.one('settings').get().then(function(settings) {
if(angular.isObject(settings)) {
$scope.settings = settings;
Expand All @@ -17,4 +19,8 @@ app.controller('NotesSettingsController',
window.location.reload(true);
});
});

$document.on('change', '#fileSuffix', function() {
$scope.settings.put();
});
});
2 changes: 1 addition & 1 deletion js/public/app.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion js/public/app.min.js.map

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions service/notesservice.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public function create ($userId) {
// check new note exists already and we need to number it
// pass -1 because no file has id -1 and that will ensure
// to only return filenames that dont yet exist
$path = $this->generateFileName($folder, $title, "txt", -1);
$path = $this->generateFileName($folder, $title, $this->settings->get('fileSuffix'), -1);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why the change for fileSuffix instead of extension in all files?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is your comment about the naming? I chose fileSuffix because the value includes the dot. I wanted to have the dot included, because it's more generic and I think that displaying .md to the user is more helpful than displaying md.

$file = $folder->newFile($path);

return $this->getNote($file, $folder);
Expand Down Expand Up @@ -160,7 +160,7 @@ public function update ($id, $content, $userId, $category=null, $mtime=0) {
// this can fail if access rights are not sufficient or category name is illegal
try {
$currentFilePath = $this->root->getFullPath($file->getPath());
$fileExtension = pathinfo($file->getName(), PATHINFO_EXTENSION);
$fileSuffix = '.' . pathinfo($file->getName(), PATHINFO_EXTENSION);

// detect (new) folder path based on category name
if($category===null) {
Expand All @@ -178,7 +178,7 @@ public function update ($id, $content, $userId, $category=null, $mtime=0) {
}

// assemble new file path
$newFilePath = $basePath . '/' . $this->generateFileName($folder, $title, $fileExtension, $id);
$newFilePath = $basePath . '/' . $this->generateFileName($folder, $title, $fileSuffix, $id);

// if the current path is not the new path, the file has to be renamed
if($currentFilePath !== $newFilePath) {
Expand Down Expand Up @@ -344,15 +344,15 @@ private function getOrCreateFolder($path) {
*
* @param Folder $folder a folder to the notes directory
* @param string $title the filename which should be used
* @param string $extension the extension which should be used
* @param string $suffix the suffix (incl. dot) which should be used
* @param int $id the id of the note for which the title should be generated
* used to see if the file itself has the title and not a different file for
* checking for filename collisions
* @return string the resolved filename to prevent overwriting different
* files with the same title
*/
private function generateFileName (Folder $folder, $title, $extension, $id) {
$path = $title . '.' . $extension;
private function generateFileName (Folder $folder, $title, $suffix, $id) {
$path = $title . $suffix;

// if file does not exist, that name has not been taken. Similar we don't
// need to handle file collisions if it is the filename did not change
Expand All @@ -368,7 +368,7 @@ private function generateFileName (Folder $folder, $title, $extension, $id) {
} else {
$newTitle = $title . ' (2)';
}
return $this->generateFileName($folder, $newTitle, $extension, $id);
return $this->generateFileName($folder, $newTitle, $suffix, $id);
}
}

Expand Down
43 changes: 35 additions & 8 deletions service/settingsservice.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ class SettingsService
private $root;

/* Default values */
private $settings = [
private $defaults = [
"notesPath" => "Notes",
"fileSuffix" => ".txt",

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please make this value globally changeable. this is the most important part of the issue your are fixing here.
maybe even configuring the notesPath globally would be nice.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR is about a per user setting. I think that we should do a system-wide (admin) setting in a separate PR (if it is really needed).

];

public function __construct(
Expand All @@ -33,16 +34,42 @@ public function __construct(
* @throws \OCP\PreConditionNotMetException
*/
public function set($settings) {
foreach($this->settings as $name => $value) {
$this->settings[$name] = isset($settings[$name]) ? $settings[$name] : $value;
// remove illegal, empty and default settings
foreach($settings as $name => $value) {
if(!array_key_exists($name, $this->defaults)
|| empty($value)
|| $value === $this->defaults[$name]
) {
unset($settings[$name]);
}
}

$this->config->setUserValue($this->uid, 'notes', 'settings', json_encode($this->settings));
$this->config->setUserValue($this->uid, 'notes', 'settings', json_encode($settings));
}

public function get($name = null, $default = null) {
public function getAll() {
$settings = json_decode($this->config->getUserValue($this->uid, 'notes', 'settings'));
if(!$settings || !is_object($settings)) $settings = $this->settings;
return $name ? (property_exists($settings, $name) ? $settings->{$name} : $default) : $settings;
if(is_object($settings)) {
// use default for empty settings
foreach($this->defaults as $name => $defaultValue) {
if(!property_exists($settings, $name) || empty($settings->{$name})) {
$settings->{$name} = $defaultValue;
}
}
} else {
$settings = (object)$this->defaults;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i personally would prefer to have a real settings class here and call the constructor with the defaults array. typecasting here makes me have a bad feeling but as i don't know the coding standard in this project and in nextcloud i assume you can ignore this. for projects where i approve the PR i would probably request a change.

in gerneral i would use symfonys option resolver for handling all the settings https://symfony.com/doc/current/components/options_resolver.html

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are not using symfony in this app, yet. Personally, I don't want to use another library just for this small thing. But please feel free to provide a PR with your proposed implementation.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

symfony is not only a full stack framework, its components can easily used in many projects but as i wrote, nothing i really thought you would change. i only try to be constructive.

}
return $settings;
}

/**
* @throws \OCP\PreConditionNotMetException
*/
public function get($name) {
$settings = $this->getAll();
if(property_exists($settings, $name)) {
return $settings->{$name};
} else {
throw new \OCP\PreConditionNotMetException('Setting '.$name.' not found.');
}
}
}
Loading