mirror of
https://github.com/JanGross/quicknotes.git
synced 2025-12-01 15:47:17 +01:00
Move main code to a new Service..
This commit is contained in:
@@ -23,117 +23,41 @@
|
|||||||
namespace OCA\QuickNotes\Controller;
|
namespace OCA\QuickNotes\Controller;
|
||||||
|
|
||||||
use OCP\AppFramework\Http;
|
use OCP\AppFramework\Http;
|
||||||
use OCP\AppFramework\Http\DataResponse;
|
|
||||||
use OCP\AppFramework\Http\JSONResponse;
|
use OCP\AppFramework\Http\JSONResponse;
|
||||||
use OCP\AppFramework\Controller;
|
use OCP\AppFramework\Controller;
|
||||||
|
|
||||||
use OCP\IRequest;
|
use OCP\IRequest;
|
||||||
|
|
||||||
use OCA\QuickNotes\Db\Attach;
|
use OCA\QuickNotes\Service\NoteService;
|
||||||
use OCA\QuickNotes\Db\AttachMapper;
|
|
||||||
use OCA\QuickNotes\Db\Color;
|
|
||||||
use OCA\QuickNotes\Db\ColorMapper;
|
|
||||||
use OCA\QuickNotes\Db\Note;
|
|
||||||
use OCA\QuickNotes\Db\NoteMapper;
|
|
||||||
use OCA\QuickNotes\Db\NoteTag;
|
|
||||||
use OCA\QuickNotes\Db\NoteTagMapper;
|
|
||||||
use OCA\QuickNotes\Db\NoteShare;
|
|
||||||
use OCA\QuickNotes\Db\NoteShareMapper;
|
|
||||||
use OCA\QuickNotes\Db\Tag;
|
|
||||||
use OCA\QuickNotes\Db\TagMapper;
|
|
||||||
|
|
||||||
use OCA\QuickNotes\Service\FileService;
|
|
||||||
|
|
||||||
|
|
||||||
class NoteController extends Controller {
|
class NoteController extends Controller {
|
||||||
|
|
||||||
private $notemapper;
|
private $noteService;
|
||||||
private $notetagmapper;
|
|
||||||
private $colormapper;
|
|
||||||
private $notesharemapper;
|
|
||||||
private $attachMapper;
|
|
||||||
private $tagmapper;
|
|
||||||
private $fileService;
|
|
||||||
private $userId;
|
private $userId;
|
||||||
|
|
||||||
public function __construct($AppName,
|
public function __construct($AppName,
|
||||||
IRequest $request,
|
IRequest $request,
|
||||||
NoteMapper $notemapper,
|
NoteService $noteService,
|
||||||
NoteTagMapper $notetagmapper,
|
$userId)
|
||||||
NoteShareMapper $notesharemapper,
|
|
||||||
ColorMapper $colormapper,
|
|
||||||
AttachMapper $attachMapper,
|
|
||||||
TagMapper $tagmapper,
|
|
||||||
FileService $fileService,
|
|
||||||
$UserId)
|
|
||||||
{
|
{
|
||||||
parent::__construct($AppName, $request);
|
parent::__construct($AppName, $request);
|
||||||
|
|
||||||
$this->notemapper = $notemapper;
|
$this->noteService = $noteService;
|
||||||
$this->notetagmapper = $notetagmapper;
|
$this->userId = $userId;
|
||||||
$this->colormapper = $colormapper;
|
|
||||||
$this->notesharemapper = $notesharemapper;
|
|
||||||
$this->attachMapper = $attachMapper;
|
|
||||||
$this->tagmapper = $tagmapper;
|
|
||||||
$this->fileService = $fileService;
|
|
||||||
$this->userId = $UserId;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @NoAdminRequired
|
* @NoAdminRequired
|
||||||
*/
|
*/
|
||||||
public function index() {
|
public function index(): JSONResponse {
|
||||||
$notes = $this->notemapper->findAll($this->userId);
|
$notes = $this->noteService->getAll($this->userId);
|
||||||
foreach($notes as $note) {
|
$etag = md5(json_encode($notes));
|
||||||
$note->setIsShared(false);
|
|
||||||
$sharedWith = $this->notesharemapper->getSharesForNote($note->getId());
|
|
||||||
if(count($sharedWith) > 0) {
|
|
||||||
$shareList = array();
|
|
||||||
foreach($sharedWith as $share) {
|
|
||||||
$shareList[] = $share->getSharedUser();
|
|
||||||
}
|
|
||||||
$note->setSharedWith(implode(", ", $shareList));
|
|
||||||
} else {
|
|
||||||
$note->setSharedWith(null);
|
|
||||||
}
|
|
||||||
$note->setTags($this->tagmapper->getTagsForNote($this->userId, $note->getId()));
|
|
||||||
}
|
|
||||||
$shareEntries = $this->notesharemapper->findForUser($this->userId);
|
|
||||||
$shares = array();
|
|
||||||
foreach($shareEntries as $entry) {
|
|
||||||
try {
|
|
||||||
//find is only to check if current user is owner
|
|
||||||
$this->notemapper->find($entry->getNoteId(), $this->userId);
|
|
||||||
//user is owner, nothing to do
|
|
||||||
} catch(\OCP\AppFramework\Db\DoesNotExistException $e) {
|
|
||||||
$share = $this->notemapper->findById($entry->getNoteId());
|
|
||||||
$share->setIsShared(true);
|
|
||||||
$shares[] = $share;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
$notes = array_merge($notes, $shares);
|
|
||||||
|
|
||||||
// Insert true color to response
|
$response = new JSONResponse($notes);
|
||||||
foreach ($notes as $note) {
|
$response->setETag($etag);
|
||||||
$note->setColor($this->colormapper->find($note->getColorId())->getColor());
|
|
||||||
}
|
|
||||||
|
|
||||||
// Insert true color to response
|
return $response;
|
||||||
foreach ($notes as $note) {
|
|
||||||
$note->setIsPinned($note->getPinned() ? true : false);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Insert true attachts to response
|
|
||||||
foreach ($notes as $note) {
|
|
||||||
$attachts = $this->attachMapper->findFromNote($this->userId, $note->getId());
|
|
||||||
foreach ($attachts as $attach) {
|
|
||||||
$attach->setPreviewUrl($this->fileService->getPreviewUrl($attach->getFileId(), 512));
|
|
||||||
$attach->setRedirectUrl($this->fileService->getRedirectToFileUrl($attach->getFileId()));
|
|
||||||
}
|
|
||||||
$note->setAttachts($attachts);
|
|
||||||
}
|
|
||||||
|
|
||||||
return new DataResponse($notes);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -141,13 +65,18 @@ class NoteController extends Controller {
|
|||||||
*
|
*
|
||||||
* @param int $id
|
* @param int $id
|
||||||
*/
|
*/
|
||||||
public function show($id) {
|
public function show($id): JSONResponse {
|
||||||
// TODO: Implement.
|
$note = $this->noteService->get($this->userId, $id);
|
||||||
try {
|
if (is_null($note)) {
|
||||||
return new DataResponse($this->notemapper->find($id, $this->userId));
|
return new JSONResponse([], Http::STATUS_NOT_FOUND);
|
||||||
} catch(Exception $e) {
|
|
||||||
return new DataResponse([], Http::STATUS_NOT_FOUND);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$etag = md5(json_encode($note));
|
||||||
|
|
||||||
|
$response = new JSONResponse($note);
|
||||||
|
$response->setETag($etag);
|
||||||
|
|
||||||
|
return $response;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -155,35 +84,17 @@ class NoteController extends Controller {
|
|||||||
*
|
*
|
||||||
* @param string $title
|
* @param string $title
|
||||||
* @param string $content
|
* @param string $content
|
||||||
|
* @param string $color
|
||||||
*/
|
*/
|
||||||
public function create($title, $content, $color = "#F7EB96") {
|
public function create($title, $content, $color = "#F7EB96") {
|
||||||
// Get color or append it
|
$note = $this->noteService->create($this->userId, $title, $content, $color);
|
||||||
if ($this->colormapper->colorExists($color)) {
|
|
||||||
$hcolor = $this->colormapper->findByColor($color);
|
|
||||||
} else {
|
|
||||||
$hcolor = new Color();
|
|
||||||
$hcolor->setColor($color);
|
|
||||||
$hcolor = $this->colormapper->insert($hcolor);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create note and insert it
|
$etag = md5(json_encode($note));
|
||||||
$note = new Note();
|
|
||||||
|
|
||||||
$note->setTitle($title);
|
$response = new JSONResponse($note);
|
||||||
$note->setContent($content);
|
$response->setETag($etag);
|
||||||
$note->setTimestamp(time());
|
|
||||||
$note->setColorId($hcolor->id);
|
|
||||||
$note->setUserId($this->userId);
|
|
||||||
|
|
||||||
$newNote = $this->notemapper->insert($note);
|
return $response;
|
||||||
|
|
||||||
// Insert true color pin and tags to response
|
|
||||||
$newNote->setColor($hcolor->getColor());
|
|
||||||
$newNote->setIsPinned(false);
|
|
||||||
$newNote->setTags([]);
|
|
||||||
$newNote->setAttachts([]);
|
|
||||||
|
|
||||||
return new DataResponse($newNote);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -193,128 +104,22 @@ class NoteController extends Controller {
|
|||||||
* @param string $title
|
* @param string $title
|
||||||
* @param string $content
|
* @param string $content
|
||||||
* @param array $attachts
|
* @param array $attachts
|
||||||
* @param boolean $pinned
|
* @param bool $pinned
|
||||||
* @param array $tags
|
* @param array $tags
|
||||||
* @param string $color
|
* @param string $color
|
||||||
*/
|
*/
|
||||||
public function update($id, $title, $content, $attachts, $pinned, $tags, $color = "#F7EB96") {
|
public function update(int $id, string $title, string $content, array $attachts, bool $pinned, array $tags, string $color = "#F7EB96"): JSONResponse {
|
||||||
// Get current Note and Color.
|
$note = $this->noteService->update($this->userId, $id, $title, $content, $attachts, $pinned, $tags, $color);
|
||||||
try {
|
if (is_null($note)) {
|
||||||
$note = $this->notemapper->find($id, $this->userId);
|
return new JSONResponse([], Http::STATUS_NOT_FOUND);
|
||||||
} catch(Exception $e) {
|
|
||||||
return new DataResponse([], Http::STATUS_NOT_FOUND);
|
|
||||||
}
|
|
||||||
$oldcolorid = $note->getColorId();
|
|
||||||
|
|
||||||
// Get new Color or append it.
|
|
||||||
if ($this->colormapper->colorExists($color)) {
|
|
||||||
$hcolor = $this->colormapper->findByColor($color);
|
|
||||||
} else {
|
|
||||||
$hcolor = new Color();
|
|
||||||
$hcolor->setColor($color);
|
|
||||||
$hcolor = $this->colormapper->insert($hcolor);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete old attachts
|
$etag = md5(json_encode($note));
|
||||||
$dbAttachts = $this->attachMapper->findFromNote($this->userId, $id);
|
|
||||||
foreach ($dbAttachts as $dbAttach) {
|
|
||||||
$delete = true;
|
|
||||||
foreach ($attachts as $attach) {
|
|
||||||
if ($dbAttach->getFileId() === $attach['file_id']) {
|
|
||||||
$delete = false;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if ($delete) {
|
|
||||||
$this->attachMapper->delete($dbAttach);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add new attachts
|
$response = new JSONResponse($note);
|
||||||
foreach ($attachts as $attach) {
|
$response->setETag($etag);
|
||||||
if (!$this->attachMapper->fileAttachExists($this->userId, $id, $attach['file_id'])) {
|
|
||||||
$hAttach = new Attach();
|
|
||||||
$hAttach->setUserId($this->userId);
|
|
||||||
$hAttach->setNoteId($id);
|
|
||||||
$hAttach->setFileId($attach['file_id']);
|
|
||||||
$hAttach->setCreatedAt(time());
|
|
||||||
$this->attachMapper->insert($hAttach);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Delete old tag relations
|
return $response;
|
||||||
$dbTags = $this->tagmapper->getTagsForNote($this->userId, $id);
|
|
||||||
foreach ($dbTags as $dbTag) {
|
|
||||||
$delete = true;
|
|
||||||
foreach ($tags as $tag) {
|
|
||||||
if ($dbTag->getId() === $tag['id']) {
|
|
||||||
$delete = false;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if ($delete) {
|
|
||||||
$hnotetag = $this->notetagmapper->findNoteTag($this->userId, $id, $dbTag->getId());
|
|
||||||
$this->notetagmapper->delete($hnotetag);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add new tags and update relations.
|
|
||||||
foreach ($tags as $tag) {
|
|
||||||
if (!$this->tagmapper->tagExists($this->userId, $tag['name'])) {
|
|
||||||
$htag = new Tag();
|
|
||||||
$htag->setName($tag['name']);
|
|
||||||
$htag->setUserId($this->userId);
|
|
||||||
$htag = $this->tagmapper->insert($htag);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
$htag = $this->tagmapper->getTag($this->userId, $tag['name']);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!$this->notetagmapper->noteTagExists($this->userId, $id, $htag->getId())) {
|
|
||||||
$noteTag = new NoteTag();
|
|
||||||
$noteTag->setNoteId($id);
|
|
||||||
$noteTag->setTagId($htag->getId());
|
|
||||||
$noteTag->setUserId($this->userId);
|
|
||||||
$this->notetagmapper->insert($noteTag);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set new info on Note
|
|
||||||
$note->setTitle($title);
|
|
||||||
$note->setContent($content);
|
|
||||||
$note->setPinned($pinned ? 1 : 0);
|
|
||||||
$note->setTimestamp(time());
|
|
||||||
$note->setColorId($hcolor->id);
|
|
||||||
|
|
||||||
// Update note.
|
|
||||||
$newnote = $this->notemapper->update($note);
|
|
||||||
|
|
||||||
// Insert true color and pin to response
|
|
||||||
$newnote->setColor($hcolor->getColor());
|
|
||||||
$newnote->setIsPinned($note->getPinned() ? true : false);
|
|
||||||
|
|
||||||
// Fill new tags
|
|
||||||
$newnote->setTags($this->tagmapper->getTagsForNote($this->userId, $newnote->getId()));
|
|
||||||
|
|
||||||
// Fill attachts to response
|
|
||||||
$attachts = $this->attachMapper->findFromNote($this->userId, $newnote->getId());
|
|
||||||
foreach ($attachts as $attach) {
|
|
||||||
$attach->setPreviewUrl($this->fileService->getPreviewUrl($attach->getFileId(), 512));
|
|
||||||
$attach->setRedirectUrl($this->fileService->getRedirectToFileUrl($attach->getFileId()));
|
|
||||||
}
|
|
||||||
$newnote->setAttachts($attachts);
|
|
||||||
|
|
||||||
// Remove old color if necessary
|
|
||||||
if (($oldcolorid !== $hcolor->getId()) &&
|
|
||||||
(!$this->notemapper->colorIdCount($oldcolorid))) {
|
|
||||||
$oldcolor = $this->colormapper->find($oldcolorid);
|
|
||||||
$this->colormapper->delete($oldcolor);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Purge orphan tags.
|
|
||||||
$this->tagmapper->dropOld();
|
|
||||||
|
|
||||||
return new DataResponse($newnote);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -322,121 +127,8 @@ class NoteController extends Controller {
|
|||||||
*
|
*
|
||||||
* @param int $id
|
* @param int $id
|
||||||
*/
|
*/
|
||||||
public function destroy($id) {
|
public function destroy(int $id): JSONResponse {
|
||||||
// Get Note and Color
|
$this->noteService->destroy($this->userId, $id);
|
||||||
try {
|
return new JSONResponse([]);
|
||||||
$note = $this->notemapper->find($id, $this->userId);
|
|
||||||
} catch(Exception $e) {
|
|
||||||
return new DataResponse([], Http::STATUS_NOT_FOUND);
|
|
||||||
}
|
|
||||||
$oldcolorid = $note->getColorId();
|
|
||||||
|
|
||||||
$this->notesharemapper->deleteByNoteId($note->getId());
|
|
||||||
|
|
||||||
// Delete note.
|
|
||||||
$this->notemapper->delete($note);
|
|
||||||
|
|
||||||
// Delete Color if necessary
|
|
||||||
if (!$this->notemapper->colorIdCount($oldcolorid)) {
|
|
||||||
$oldcolor = $this->colormapper->find($oldcolorid);
|
|
||||||
$this->colormapper->delete($oldcolor);
|
|
||||||
}
|
|
||||||
|
|
||||||
$attachts = $this->attachMapper->findFromNote($this->userId, $id);
|
|
||||||
foreach ($attachts as $attach) {
|
|
||||||
$this->attachMapper->delete($attach);
|
|
||||||
}
|
|
||||||
|
|
||||||
return new DataResponse($note);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @NoAdminRequired
|
|
||||||
*/
|
|
||||||
public function getUserGroupsAndUsersWithShare($noteId) {
|
|
||||||
$userMgr = \OC::$server->getUserManager();
|
|
||||||
$grpMgr = \OC::$server->getGroupManager();
|
|
||||||
$users = array();
|
|
||||||
$groups = array();
|
|
||||||
if($grpMgr->isAdmin($this->userId)) {
|
|
||||||
$igroups = $grpMgr->search("");
|
|
||||||
$iusers = $userMgr->search("");
|
|
||||||
foreach($igroups as $g) {
|
|
||||||
$groups[] = $g->getGID();
|
|
||||||
}
|
|
||||||
foreach($iusers as $u) {
|
|
||||||
$users[] = $u->getUID();
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
$igroups = $grpMgr->getUserGroups($userMgr->get($this->userId));
|
|
||||||
foreach($igroups as $g) {
|
|
||||||
$iusers = $g->getUsers();
|
|
||||||
foreach($iusers as $u) {
|
|
||||||
$users[] = $u->getUID();
|
|
||||||
}
|
|
||||||
$groups[] = $g->getGID();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$users = array_unique($users);
|
|
||||||
if(($i = array_search($this->userId, $users)) !== false) {
|
|
||||||
unset($users[$i]);
|
|
||||||
}
|
|
||||||
$pos_users = array();
|
|
||||||
$pos_groups = array();
|
|
||||||
$shares = $this->notesharemapper->getSharesForNote($noteId);
|
|
||||||
foreach($shares as $s) {
|
|
||||||
$shareType = $s->getSharedUser();
|
|
||||||
if(strlen($shareType) !== 0) {
|
|
||||||
if(($i = array_search($shareType, $users)) !== false) {
|
|
||||||
unset($users[$i]);
|
|
||||||
$pos_users[] = $shareType;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
$shareType = $s->getSharedGroup();
|
|
||||||
if(($i = array_search($shareType, $groups)) !== false) {
|
|
||||||
unset($groups[$i]);
|
|
||||||
$pos_groups[] = $shareType;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
$params = array('groups' => $groups, 'users' => $users, 'posGroups' => $pos_groups, 'posUsers' => $pos_users);
|
|
||||||
return new JSONResponse($params);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @NoAdminRequired
|
|
||||||
*/
|
|
||||||
public function addGroupShare($groupId, $noteId) {
|
|
||||||
$share = new NoteShare();
|
|
||||||
$share->setSharedGroup($groupId);
|
|
||||||
$share->setNoteId($noteId);
|
|
||||||
$this->notesharemapper->insert($share);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @NoAdminRequired
|
|
||||||
*/
|
|
||||||
public function removeGroupShare($groupId, $noteId) {
|
|
||||||
$share = $this->notesharemapper->findByNoteAndGroup($noteId, $groupId);
|
|
||||||
$this->notesharemapper->delete($share);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @NoAdminRequired
|
|
||||||
*/
|
|
||||||
public function addUserShare($userId, $noteId) {
|
|
||||||
$share = new NoteShare();
|
|
||||||
$share->setSharedUser($userId);
|
|
||||||
$share->setNoteId($noteId);
|
|
||||||
$this->notesharemapper->insert($share);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @NoAdminRequired
|
|
||||||
*/
|
|
||||||
public function removeUserShare($userId, $noteId) {
|
|
||||||
$share = $this->notesharemapper->findByNoteAndUser($noteId, $userId);
|
|
||||||
$this->notesharemapper->delete($share);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
434
lib/Service/NoteService.php
Normal file
434
lib/Service/NoteService.php
Normal file
@@ -0,0 +1,434 @@
|
|||||||
|
<?php
|
||||||
|
/*
|
||||||
|
* @copyright 2016-2020 Matias De lellis <mati86dl@gmail.com>
|
||||||
|
*
|
||||||
|
* @author 2016 Matias De lellis <mati86dl@gmail.com>
|
||||||
|
*
|
||||||
|
* @license GNU AGPL version 3 or any later version
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Affero General Public License as
|
||||||
|
* published by the Free Software Foundation, either version 3 of the
|
||||||
|
* License, or (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU Affero General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Affero General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace OCA\QuickNotes\Service;
|
||||||
|
|
||||||
|
use OCA\QuickNotes\Db\Attach;
|
||||||
|
use OCA\QuickNotes\Db\AttachMapper;
|
||||||
|
|
||||||
|
use OCA\QuickNotes\Db\Color;
|
||||||
|
use OCA\QuickNotes\Db\ColorMapper;
|
||||||
|
|
||||||
|
use OCA\QuickNotes\Db\Note;
|
||||||
|
use OCA\QuickNotes\Db\NoteMapper;
|
||||||
|
|
||||||
|
use OCA\QuickNotes\Db\NoteTag;
|
||||||
|
use OCA\QuickNotes\Db\NoteTagMapper;
|
||||||
|
|
||||||
|
use OCA\QuickNotes\Db\NoteShare;
|
||||||
|
use OCA\QuickNotes\Db\NoteShareMapper;
|
||||||
|
|
||||||
|
use OCA\QuickNotes\Db\Tag;
|
||||||
|
use OCA\QuickNotes\Db\TagMapper;
|
||||||
|
|
||||||
|
use OCA\QuickNotes\Service\FileService;
|
||||||
|
|
||||||
|
|
||||||
|
class NoteService {
|
||||||
|
|
||||||
|
private $notemapper;
|
||||||
|
private $notetagmapper;
|
||||||
|
private $colormapper;
|
||||||
|
private $notesharemapper;
|
||||||
|
private $attachMapper;
|
||||||
|
private $tagmapper;
|
||||||
|
private $fileService;
|
||||||
|
|
||||||
|
public function __construct(NoteMapper $notemapper,
|
||||||
|
NoteTagMapper $notetagmapper,
|
||||||
|
NoteShareMapper $notesharemapper,
|
||||||
|
ColorMapper $colormapper,
|
||||||
|
AttachMapper $attachMapper,
|
||||||
|
TagMapper $tagmapper,
|
||||||
|
FileService $fileService)
|
||||||
|
{
|
||||||
|
$this->notemapper = $notemapper;
|
||||||
|
$this->notetagmapper = $notetagmapper;
|
||||||
|
$this->colormapper = $colormapper;
|
||||||
|
$this->notesharemapper = $notesharemapper;
|
||||||
|
$this->attachMapper = $attachMapper;
|
||||||
|
$this->tagmapper = $tagmapper;
|
||||||
|
$this->fileService = $fileService;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @NoAdminRequired
|
||||||
|
*/
|
||||||
|
public function getAll(string $userId): array {
|
||||||
|
$notes = $this->notemapper->findAll($userId);
|
||||||
|
foreach($notes as $note) {
|
||||||
|
$note->setIsShared(false);
|
||||||
|
$sharedWith = $this->notesharemapper->getSharesForNote($note->getId());
|
||||||
|
if(count($sharedWith) > 0) {
|
||||||
|
$shareList = array();
|
||||||
|
foreach($sharedWith as $share) {
|
||||||
|
$shareList[] = $share->getSharedUser();
|
||||||
|
}
|
||||||
|
$note->setSharedWith(implode(", ", $shareList));
|
||||||
|
} else {
|
||||||
|
$note->setSharedWith(null);
|
||||||
|
}
|
||||||
|
$note->setTags($this->tagmapper->getTagsForNote($userId, $note->getId()));
|
||||||
|
}
|
||||||
|
$shareEntries = $this->notesharemapper->findForUser($userId);
|
||||||
|
$shares = array();
|
||||||
|
foreach($shareEntries as $entry) {
|
||||||
|
try {
|
||||||
|
//find is only to check if current user is owner
|
||||||
|
$this->notemapper->find($entry->getNoteId(), $userId);
|
||||||
|
//user is owner, nothing to do
|
||||||
|
} catch(\OCP\AppFramework\Db\DoesNotExistException $e) {
|
||||||
|
$share = $this->notemapper->findById($entry->getNoteId());
|
||||||
|
$share->setIsShared(true);
|
||||||
|
$shares[] = $share;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$notes = array_merge($notes, $shares);
|
||||||
|
|
||||||
|
foreach ($notes as $note) {
|
||||||
|
$note->setTitle(strip_tags($note->getTitle()));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Insert true color to response
|
||||||
|
foreach ($notes as $note) {
|
||||||
|
$note->setColor($this->colormapper->find($note->getColorId())->getColor());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Insert true color to response
|
||||||
|
foreach ($notes as $note) {
|
||||||
|
$note->setIsPinned($note->getPinned() ? true : false);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Insert true attachts to response
|
||||||
|
foreach ($notes as $note) {
|
||||||
|
$attachts = $this->attachMapper->findFromNote($userId, $note->getId());
|
||||||
|
foreach ($attachts as $attach) {
|
||||||
|
$attach->setPreviewUrl($this->fileService->getPreviewUrl($attach->getFileId(), 512));
|
||||||
|
$attach->setRedirectUrl($this->fileService->getRedirectToFileUrl($attach->getFileId()));
|
||||||
|
}
|
||||||
|
$note->setAttachts($attachts);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $notes;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $userId
|
||||||
|
* @param int $id
|
||||||
|
*/
|
||||||
|
public function get(string $userId, int $id): ?Note {
|
||||||
|
try {
|
||||||
|
return $this->notemapper->find($id, $userId);
|
||||||
|
} catch(Exception $e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $userId
|
||||||
|
* @param string $title
|
||||||
|
* @param string $content
|
||||||
|
* @param string $color
|
||||||
|
*/
|
||||||
|
public function create(string $userId, string $title, string $content, string $color = "#F7EB96"): Note {
|
||||||
|
// Get color or append it
|
||||||
|
if ($this->colormapper->colorExists($color)) {
|
||||||
|
$hcolor = $this->colormapper->findByColor($color);
|
||||||
|
} else {
|
||||||
|
$hcolor = new Color();
|
||||||
|
$hcolor->setColor($color);
|
||||||
|
$hcolor = $this->colormapper->insert($hcolor);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create note and insert it
|
||||||
|
$note = new Note();
|
||||||
|
|
||||||
|
$note->setTitle($title);
|
||||||
|
$note->setContent($content);
|
||||||
|
$note->setTimestamp(time());
|
||||||
|
$note->setColorId($hcolor->id);
|
||||||
|
$note->setUserId($userId);
|
||||||
|
|
||||||
|
$newNote = $this->notemapper->insert($note);
|
||||||
|
|
||||||
|
// Insert true color pin and tags to response
|
||||||
|
$newNote->setColor($hcolor->getColor());
|
||||||
|
$newNote->setIsPinned(false);
|
||||||
|
$newNote->setTags([]);
|
||||||
|
$newNote->setAttachts([]);
|
||||||
|
|
||||||
|
return $newNote;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string userId
|
||||||
|
* @param int $id
|
||||||
|
* @param string $title
|
||||||
|
* @param string $content
|
||||||
|
* @param array $attachts
|
||||||
|
* @param bool $pinned
|
||||||
|
* @param array $tags
|
||||||
|
* @param string $color
|
||||||
|
*/
|
||||||
|
public function update(string $userId,
|
||||||
|
int $id,
|
||||||
|
string $title,
|
||||||
|
string $content,
|
||||||
|
array $attachts,
|
||||||
|
bool $pinned,
|
||||||
|
array $tags,
|
||||||
|
string $color): Note
|
||||||
|
{
|
||||||
|
// Get current Note and Color.
|
||||||
|
try {
|
||||||
|
$note = $this->notemapper->find($id, $userId);
|
||||||
|
} catch(Exception $e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
$oldcolorid = $note->getColorId();
|
||||||
|
|
||||||
|
// Get new Color or append it.
|
||||||
|
if ($this->colormapper->colorExists($color)) {
|
||||||
|
$hcolor = $this->colormapper->findByColor($color);
|
||||||
|
} else {
|
||||||
|
$hcolor = new Color();
|
||||||
|
$hcolor->setColor($color);
|
||||||
|
$hcolor = $this->colormapper->insert($hcolor);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete old attachts
|
||||||
|
$dbAttachts = $this->attachMapper->findFromNote($userId, $id);
|
||||||
|
foreach ($dbAttachts as $dbAttach) {
|
||||||
|
$delete = true;
|
||||||
|
foreach ($attachts as $attach) {
|
||||||
|
if ($dbAttach->getFileId() === $attach['file_id']) {
|
||||||
|
$delete = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ($delete) {
|
||||||
|
$this->attachMapper->delete($dbAttach);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add new attachts
|
||||||
|
foreach ($attachts as $attach) {
|
||||||
|
if (!$this->attachMapper->fileAttachExists($userId, $id, $attach['file_id'])) {
|
||||||
|
$hAttach = new Attach();
|
||||||
|
$hAttach->setUserId($userId);
|
||||||
|
$hAttach->setNoteId($id);
|
||||||
|
$hAttach->setFileId($attach['file_id']);
|
||||||
|
$hAttach->setCreatedAt(time());
|
||||||
|
$this->attachMapper->insert($hAttach);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete old tag relations
|
||||||
|
$dbTags = $this->tagmapper->getTagsForNote($userId, $id);
|
||||||
|
foreach ($dbTags as $dbTag) {
|
||||||
|
$delete = true;
|
||||||
|
foreach ($tags as $tag) {
|
||||||
|
if ($dbTag->getId() === $tag['id']) {
|
||||||
|
$delete = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ($delete) {
|
||||||
|
$hnotetag = $this->notetagmapper->findNoteTag($userId, $id, $dbTag->getId());
|
||||||
|
$this->notetagmapper->delete($hnotetag);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add new tags and update relations.
|
||||||
|
foreach ($tags as $tag) {
|
||||||
|
if (!$this->tagmapper->tagExists($userId, $tag['name'])) {
|
||||||
|
$htag = new Tag();
|
||||||
|
$htag->setName($tag['name']);
|
||||||
|
$htag->setUserId($userId);
|
||||||
|
$htag = $this->tagmapper->insert($htag);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$htag = $this->tagmapper->getTag($userId, $tag['name']);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!$this->notetagmapper->noteTagExists($userId, $id, $htag->getId())) {
|
||||||
|
$noteTag = new NoteTag();
|
||||||
|
$noteTag->setNoteId($id);
|
||||||
|
$noteTag->setTagId($htag->getId());
|
||||||
|
$noteTag->setUserId($userId);
|
||||||
|
$this->notetagmapper->insert($noteTag);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set new info on Note
|
||||||
|
$note->setTitle($title);
|
||||||
|
$note->setContent($content);
|
||||||
|
$note->setPinned($pinned ? 1 : 0);
|
||||||
|
$note->setTimestamp(time());
|
||||||
|
$note->setColorId($hcolor->id);
|
||||||
|
|
||||||
|
// Update note.
|
||||||
|
$newnote = $this->notemapper->update($note);
|
||||||
|
|
||||||
|
// Insert true color and pin to response
|
||||||
|
$newnote->setColor($hcolor->getColor());
|
||||||
|
$newnote->setIsPinned($note->getPinned() ? true : false);
|
||||||
|
|
||||||
|
// Fill new tags
|
||||||
|
$newnote->setTags($this->tagmapper->getTagsForNote($userId, $newnote->getId()));
|
||||||
|
|
||||||
|
// Fill attachts to response
|
||||||
|
$attachts = $this->attachMapper->findFromNote($userId, $newnote->getId());
|
||||||
|
foreach ($attachts as $attach) {
|
||||||
|
$attach->setPreviewUrl($this->fileService->getPreviewUrl($attach->getFileId(), 512));
|
||||||
|
$attach->setRedirectUrl($this->fileService->getRedirectToFileUrl($attach->getFileId()));
|
||||||
|
}
|
||||||
|
$newnote->setAttachts($attachts);
|
||||||
|
|
||||||
|
// Remove old color if necessary
|
||||||
|
if (($oldcolorid !== $hcolor->getId()) &&
|
||||||
|
(!$this->notemapper->colorIdCount($oldcolorid))) {
|
||||||
|
$oldcolor = $this->colormapper->find($oldcolorid);
|
||||||
|
$this->colormapper->delete($oldcolor);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Purge orphan tags.
|
||||||
|
$this->tagmapper->dropOld();
|
||||||
|
|
||||||
|
return $newnote;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $userId
|
||||||
|
* @param int $id
|
||||||
|
*/
|
||||||
|
public function destroy($userId, $id) {
|
||||||
|
// Get Note and Color
|
||||||
|
try {
|
||||||
|
$note = $this->notemapper->find($id, $userId);
|
||||||
|
} catch(Exception $e) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
$oldcolorid = $note->getColorId();
|
||||||
|
|
||||||
|
$this->notesharemapper->deleteByNoteId($note->getId());
|
||||||
|
|
||||||
|
// Delete note.
|
||||||
|
$this->notemapper->delete($note);
|
||||||
|
|
||||||
|
// Delete Color if necessary
|
||||||
|
if (!$this->notemapper->colorIdCount($oldcolorid)) {
|
||||||
|
$oldcolor = $this->colormapper->find($oldcolorid);
|
||||||
|
$this->colormapper->delete($oldcolor);
|
||||||
|
}
|
||||||
|
|
||||||
|
$attachts = $this->attachMapper->findFromNote($userId, $id);
|
||||||
|
foreach ($attachts as $attach) {
|
||||||
|
$this->attachMapper->delete($attach);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @NoAdminRequired
|
||||||
|
*/
|
||||||
|
public function getUserGroupsAndUsersWithShare($userId, $noteId) {
|
||||||
|
$userMgr = \OC::$server->getUserManager();
|
||||||
|
$grpMgr = \OC::$server->getGroupManager();
|
||||||
|
$users = array();
|
||||||
|
$groups = array();
|
||||||
|
if($grpMgr->isAdmin($userId)) {
|
||||||
|
$igroups = $grpMgr->search("");
|
||||||
|
$iusers = $userMgr->search("");
|
||||||
|
foreach($igroups as $g) {
|
||||||
|
$groups[] = $g->getGID();
|
||||||
|
}
|
||||||
|
foreach($iusers as $u) {
|
||||||
|
$users[] = $u->getUID();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$igroups = $grpMgr->getUserGroups($userMgr->get($userId));
|
||||||
|
foreach($igroups as $g) {
|
||||||
|
$iusers = $g->getUsers();
|
||||||
|
foreach($iusers as $u) {
|
||||||
|
$users[] = $u->getUID();
|
||||||
|
}
|
||||||
|
$groups[] = $g->getGID();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$users = array_unique($users);
|
||||||
|
if(($i = array_search($userId, $users)) !== false) {
|
||||||
|
unset($users[$i]);
|
||||||
|
}
|
||||||
|
$pos_users = array();
|
||||||
|
$pos_groups = array();
|
||||||
|
$shares = $this->notesharemapper->getSharesForNote($noteId);
|
||||||
|
foreach($shares as $s) {
|
||||||
|
$shareType = $s->getSharedUser();
|
||||||
|
if(strlen($shareType) !== 0) {
|
||||||
|
if(($i = array_search($shareType, $users)) !== false) {
|
||||||
|
unset($users[$i]);
|
||||||
|
$pos_users[] = $shareType;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$shareType = $s->getSharedGroup();
|
||||||
|
if(($i = array_search($shareType, $groups)) !== false) {
|
||||||
|
unset($groups[$i]);
|
||||||
|
$pos_groups[] = $shareType;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ['groups' => $groups, 'users' => $users, 'posGroups' => $pos_groups, 'posUsers' => $pos_users];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*/
|
||||||
|
public function addGroupShare($groupId, $noteId) {
|
||||||
|
$share = new NoteShare();
|
||||||
|
$share->setSharedGroup($groupId);
|
||||||
|
$share->setNoteId($noteId);
|
||||||
|
$this->notesharemapper->insert($share);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*/
|
||||||
|
public function removeGroupShare($groupId, $noteId) {
|
||||||
|
$share = $this->notesharemapper->findByNoteAndGroup($noteId, $groupId);
|
||||||
|
$this->notesharemapper->delete($share);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*/
|
||||||
|
public function addUserShare($userId, $noteId) {
|
||||||
|
$share = new NoteShare();
|
||||||
|
$share->setSharedUser($userId);
|
||||||
|
$share->setNoteId($noteId);
|
||||||
|
$this->notesharemapper->insert($share);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*/
|
||||||
|
public function removeUserShare($userId, $noteId) {
|
||||||
|
$share = $this->notesharemapper->findByNoteAndUser($noteId, $userId);
|
||||||
|
$this->notesharemapper->delete($share);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user