This commit is contained in:
Pascal Precht 2022-06-08 21:40:02 +02:00
parent 49ca3e1c36
commit 1b2ba6eb95
No known key found for this signature in database
GPG Key ID: 3017C135E0731E5A
4 changed files with 39 additions and 8 deletions

View File

@ -6,9 +6,13 @@ type BookmarkDto* = object
name*: string name*: string
url*: string url*: string
imageUrl*: string imageUrl*: string
removed*: bool
deletedAt*: int
proc toBookmarkDto*(jsonObj: JsonNode): BookmarkDto = proc toBookmarkDto*(jsonObj: JsonNode): BookmarkDto =
result = BookmarkDto() result = BookmarkDto()
discard jsonObj.getProp("name", result.name) discard jsonObj.getProp("name", result.name)
discard jsonObj.getProp("url", result.url) discard jsonObj.getProp("url", result.url)
discard jsonObj.getProp("imageUrl", result.imageUrl) discard jsonObj.getProp("imageUrl", result.imageUrl)
discard jsonObj.getProp("removed", result.removed)
discard jsonObj.getProp("deletedAt", result.deletedAt)

View File

@ -1,8 +1,9 @@
import Tables, json, sequtils, strformat, chronicles import Tables, json, sequtils, strformat, chronicles, strutils
import result import result
include ../../common/json_utils include ../../common/json_utils
import ./dto/bookmark as bookmark_dto import ./dto/bookmark as bookmark_dto
import ../../../backend/backend import ../../../backend/backend
import ../../../backend/browser
export bookmark_dto export bookmark_dto
@ -26,6 +27,7 @@ proc init*(self: Service) =
try: try:
let response = backend.getBookmarks() let response = backend.getBookmarks()
for bookmark in response.result.getElems().mapIt(it.toBookmarkDto()): for bookmark in response.result.getElems().mapIt(it.toBookmarkDto()):
if not bookmark.removed:
self.bookmarks[bookmark.url] = bookmark self.bookmarks[bookmark.url] = bookmark
except Exception as e: except Exception as e:
@ -37,12 +39,15 @@ proc getBookmarks*(self: Service): seq[BookmarkDto] =
proc storeBookmark*(self: Service, url, name: string): R = proc storeBookmark*(self: Service, url, name: string): R =
try: try:
let response = backend.storeBookmark(backend.Bookmark(name: name, url: url)).result if not url.isEmptyOrWhitespace:
self.bookmarks[url] = BookmarkDto() let response = browser.addBookmark(backend.Bookmark(name: name, url: url)).result
self.bookmarks[url].url = url self.bookmarks[url] = BookmarkDto()
self.bookmarks[url].name = name self.bookmarks[url].url = url
discard response.getProp("imageUrl", self.bookmarks[url].imageUrl) self.bookmarks[url].name = name
result.ok self.bookmarks[url] 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: except Exception as e:
let errDescription = e.msg let errDescription = e.msg
error "error: ", errDescription error "error: ", errDescription
@ -52,7 +57,7 @@ proc deleteBookmark*(self: Service, url: string): bool =
try: try:
if not self.bookmarks.hasKey(url): if not self.bookmarks.hasKey(url):
return return
discard backend.deleteBookmark(url) discard browser.removeBookmark(url).result
self.bookmarks.del(url) self.bookmarks.del(url)
except Exception as e: except Exception as e:
let errDescription = e.msg let errDescription = e.msg

View File

@ -16,6 +16,9 @@ type
Bookmark* = ref object of RootObj Bookmark* = ref object of RootObj
name* {.serializedFieldName("name").}: string name* {.serializedFieldName("name").}: string
url* {.serializedFieldName("url").}: string url* {.serializedFieldName("url").}: string
imageUrl* {.serializedFieldName("imageUrl").}: string
removed* {.serializedFieldName("removed").}: bool
deletedAt* {.serializedFieldName("deletedAt").}: int
Permission* = ref object of RootObj Permission* = ref object of RootObj
dapp* {.serializedFieldName("dapp").}: string dapp* {.serializedFieldName("dapp").}: string

19
src/backend/browser.nim Normal file
View File

@ -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])