NotesService: Put bookmarks from Notes into a separate folder

Signed-off-by: Marcel Klehr <mklehr@gmx.net>
This commit is contained in:
Marcel Klehr 2022-03-19 19:13:26 +01:00
parent aea7333507
commit 68bf20517d
3 changed files with 73 additions and 18 deletions

View File

@ -78,7 +78,7 @@ class FolderMapper extends QBMapper {
*
* @return Entity
*/
public function findRootFolder(string $userId): Entity {
public function findRootFolder(string $userId): Folder {
$qb = $this->db->getQueryBuilder();
$qb
->select(array_map(static function ($col) {

View File

@ -93,6 +93,18 @@ class FolderService {
$this->eventDispatcher = $eventDispatcher;
}
public function getRootFolder(string $userId) : Folder {
return $this->folderMapper->findRootFolder($userId);
}
/**
* @throws DoesNotExistException
* @throws MultipleObjectsReturnedException
*/
public function findById(int $id) : Folder {
return $this->folderMapper->find($id);
}
/**
* @param $title
* @param $parentFolderId

View File

@ -7,6 +7,8 @@
namespace OCA\Bookmarks\Service;
use OCA\Bookmarks\Db\Bookmark;
use OCA\Bookmarks\Db\Folder;
use OCA\Bookmarks\Exception\AlreadyExistsError;
use OCA\Bookmarks\Exception\UnsupportedOperation;
use OCA\Bookmarks\Exception\UrlParseError;
@ -16,6 +18,9 @@ use OCA\Notes\Service\NotesService as OriginalNotesService;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
use OCP\Collaboration\Resources\IManager;
use OCP\Collaboration\Resources\ResourceException;
use OCP\IConfig;
use OCP\IL10N;
use OCP\IUser;
use OCP\IUserSession;
use Psr\Log\LoggerInterface;
@ -36,15 +41,25 @@ class NotesService {
*/
private $session;
/**
* @var LoggerInterface
* @var IConfig
*/
private $logger;
private $config;
/**
* @var FolderService
*/
private $folders;
/**
* @var IL10N
*/
private $l;
public function __construct(BookmarkService $bookmarks, IManager $resourceManager, IUserSession $session, LoggerInterface $logger) {
public function __construct(BookmarkService $bookmarks, IManager $resourceManager, IUserSession $session, IConfig $config, FolderService $folders, IL10N $l) {
$this->bookmarks = $bookmarks;
$this->resourceManager = $resourceManager;
$this->session = $session;
$this->logger = $logger;
$this->config = $config;
$this->folders = $folders;
$this->l = $l;
}
/**
@ -52,6 +67,10 @@ class NotesService {
*/
public function extractBookmarksFromNotes(IUser $user) {
$notes = $this->getNotes($user);
$notesBookmarksFolder = $this->getOrCreateNotesBookmarksFolder($user);
if ($notesBookmarksFolder === null) {
return;
}
foreach ($notes as $note) {
$noteContent = $note->getContent();
if (preg_match_all(self::REGEX_URL, $noteContent, $matches) === false) {
@ -60,27 +79,51 @@ class NotesService {
foreach ($matches[0] as $url) {
try {
$this->bookmarks->findByUrl($user->getUID(), $url);
continue; // If the bookmark exists already, do nothing.
$bookmark = $this->bookmarks->findByUrl($user->getUID(), $url);
$this->bookmarks->addToFolder($notesBookmarksFolder->getId(), $bookmark->getId());
} catch (UrlParseError $e) {
continue;
} catch (DoesNotExistException $e) {
// noop
try {
$bookmark = $this->bookmarks->create($user->getUID(), $url, null, null, null, [$notesBookmarksFolder->getId()]);
} catch (AlreadyExistsError|UnsupportedOperation|UrlParseError|UserLimitExceededError|DoesNotExistException|MultipleObjectsReturnedException $e) {
continue;
}
}
try {
$bookmark = $this->bookmarks->create($user->getUID(), $url);
} catch (AlreadyExistsError|UnsupportedOperation|UrlParseError|UserLimitExceededError|DoesNotExistException|MultipleObjectsReturnedException $e) {
continue;
}
$bookmarkResource = $this->resourceManager->createResource('bookmarks', (string)$bookmark->getId());
$noteResource = $this->resourceManager->createResource('file', (string)$note->getId());
$collection = $this->resourceManager->newCollection($note->getTitle());
$collection->addResource($bookmarkResource);
$collection->addResource($noteResource);
$this->linkBookmarkWithNote($user, $bookmark, $note);
}
}
}
public function getOrCreateNotesBookmarksFolder(IUser $user) : ?Folder {
$folderId = $this->config->getAppValue('bookmarks', 'notesIntegration.folderId', '');
try {
if ($folderId === '') {
$folder = $this->folders->create($this->l->t('Links from Notes'), $this->folders->getRootFolder($user->getUID())->getId());
$this->config->setAppValue('bookmarks', 'notesIntegration.folderId', (string)$folder->getId());
return $folder;
} else {
return $this->folders->findById((int)$folderId);
}
} catch (UnsupportedOperation|DoesNotExistException|MultipleObjectsReturnedException $e) {
return null;
}
}
public function linkBookmarkWithNote(IUser $user, Bookmark $bookmark, Note $note) : void {
try {
$this->resourceManager->getResourceForUser('bookmarks', (string)$bookmark->getId(), $user);
return;
} catch (ResourceException $e) {
// noop
}
$bookmarkResource = $this->resourceManager->createResource('bookmarks', (string)$bookmark->getId());
$noteResource = $this->resourceManager->createResource('file', (string)$note->getId());
$collection = $this->resourceManager->newCollection($note->getTitle());
$collection->addResource($bookmarkResource);
$collection->addResource($noteResource);
}
/**
* @param string $userId
* @return Note[]