From 1b2ba6eb954c4dbcb38ae388d7bb0a1a332048de Mon Sep 17 00:00:00 2001 From: Pascal Precht <445106+PascalPrecht@users.noreply.github.com> Date: Wed, 8 Jun 2022 21:40:02 +0200 Subject: [PATCH] WIP --- .../service/bookmarks/dto/bookmark.nim | 4 ++++ src/app_service/service/bookmarks/service.nim | 21 ++++++++++++------- src/backend/backend.nim | 3 +++ src/backend/browser.nim | 19 +++++++++++++++++ 4 files changed, 39 insertions(+), 8 deletions(-) create mode 100644 src/backend/browser.nim diff --git a/src/app_service/service/bookmarks/dto/bookmark.nim b/src/app_service/service/bookmarks/dto/bookmark.nim index 8d7c962f7d..fcc0ee385f 100644 --- a/src/app_service/service/bookmarks/dto/bookmark.nim +++ b/src/app_service/service/bookmarks/dto/bookmark.nim @@ -6,9 +6,13 @@ type BookmarkDto* = object name*: string url*: string imageUrl*: string + removed*: bool + deletedAt*: int proc toBookmarkDto*(jsonObj: JsonNode): BookmarkDto = result = BookmarkDto() discard jsonObj.getProp("name", result.name) discard jsonObj.getProp("url", result.url) discard jsonObj.getProp("imageUrl", result.imageUrl) + discard jsonObj.getProp("removed", result.removed) + discard jsonObj.getProp("deletedAt", result.deletedAt) diff --git a/src/app_service/service/bookmarks/service.nim b/src/app_service/service/bookmarks/service.nim index add9a14d31..2f22df7ffc 100644 --- a/src/app_service/service/bookmarks/service.nim +++ b/src/app_service/service/bookmarks/service.nim @@ -1,8 +1,9 @@ -import Tables, json, sequtils, strformat, chronicles +import Tables, json, sequtils, strformat, chronicles, strutils import result include ../../common/json_utils import ./dto/bookmark as bookmark_dto import ../../../backend/backend +import ../../../backend/browser export bookmark_dto @@ -26,6 +27,7 @@ proc init*(self: Service) = try: let response = backend.getBookmarks() for bookmark in response.result.getElems().mapIt(it.toBookmarkDto()): + if not bookmark.removed: self.bookmarks[bookmark.url] = bookmark except Exception as e: @@ -37,12 +39,15 @@ proc getBookmarks*(self: Service): seq[BookmarkDto] = proc storeBookmark*(self: Service, url, name: string): R = try: - let response = backend.storeBookmark(backend.Bookmark(name: name, url: url)).result - self.bookmarks[url] = BookmarkDto() - self.bookmarks[url].url = url - self.bookmarks[url].name = name - discard response.getProp("imageUrl", self.bookmarks[url].imageUrl) - result.ok self.bookmarks[url] + if not url.isEmptyOrWhitespace: + let response = browser.addBookmark(backend.Bookmark(name: name, url: url)).result + self.bookmarks[url] = BookmarkDto() + self.bookmarks[url].url = url + self.bookmarks[url].name = name + discard response.getProp("imageUrl", self.bookmarks[url].imageUrl) + discard response.getProp("removed", self.bookmarks[url].removed) + discard response.getProp("deletedAt", self.bookmarks[url].deletedAt) + result.ok self.bookmarks[url] except Exception as e: let errDescription = e.msg error "error: ", errDescription @@ -52,7 +57,7 @@ proc deleteBookmark*(self: Service, url: string): bool = try: if not self.bookmarks.hasKey(url): return - discard backend.deleteBookmark(url) + discard browser.removeBookmark(url).result self.bookmarks.del(url) except Exception as e: let errDescription = e.msg diff --git a/src/backend/backend.nim b/src/backend/backend.nim index 232e92c195..1fa850b7e8 100644 --- a/src/backend/backend.nim +++ b/src/backend/backend.nim @@ -16,6 +16,9 @@ type Bookmark* = ref object of RootObj name* {.serializedFieldName("name").}: string url* {.serializedFieldName("url").}: string + imageUrl* {.serializedFieldName("imageUrl").}: string + removed* {.serializedFieldName("removed").}: bool + deletedAt* {.serializedFieldName("deletedAt").}: int Permission* = ref object of RootObj dapp* {.serializedFieldName("dapp").}: string diff --git a/src/backend/browser.nim b/src/backend/browser.nim new file mode 100644 index 0000000000..a837fcb772 --- /dev/null +++ b/src/backend/browser.nim @@ -0,0 +1,19 @@ +import json, strutils +import core, utils +import response_type +import ./backend + +export response_type + + +proc addBookmark*(bookmark: backend.Bookmark): RpcResponse[JsonNode] {.raises: [Exception].} = + result = callPrivateRPC("addBookmark".prefix, %*[{ + "url": bookmark.url, + "name": bookmark.name, + "imageUrl": bookmark.imageUrl, + "removed": bookmark.removed, + "deletedAt": bookmark.deletedAt + }]) + +proc removeBookmark*(url: string): RpcResponse[JsonNode] {.raises: [Exception].} = + result = callPrivateRPC("removeBookmark".prefix, %*[url])