Verified Commit 8a86a507 authored by WorldTeacher's avatar WorldTeacher
Browse files

fix(shelf-sync): store metadata from books in db on sync

parent 90140285
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -25,5 +25,5 @@ translate: ## Update translation files
generate_version: ## Generate version file
	sh ./scripts/generate-version.sh

changelog-pregen: ## Pre-generate next changelog file
	bash ./scripts/changelog-pregen.sh
changelog-pregen: ## Pre-generate changelog (CHG_ARGS=--docs for docs changelog)
	bash ./scripts/changelog-pregen.sh $(CHG_ARGS)
+17 −5
Original line number Diff line number Diff line
@@ -533,7 +533,7 @@ function M.new(deps)
                local ok_partial = pcall(function()
                    for _, nb in ipairs(new_books) do
                        self.db:saveBookCache(nb.filepath, nb.hash, nb.book_id,
                            nb.title, nb.author, nb.isbn10, nb.isbn13)
                            nb.title, nb.author, nb.isbn10, nb.isbn13, nb.server_pagecount)
                    end
                end)
                if ok_partial then self.db:commit() else self.db:rollback() end
@@ -659,9 +659,17 @@ function M.new(deps)
            local cap_book_id  = book_id
            local cap_filepath = filepath
            local cap_title    = book.title
            local cap_author   = book.author
            local cap_isbn10   = book.isbn10
            local cap_isbn13   = book.isbn13
            local cap_author   = book.author or book.authors
            local cap_isbn10   = book.isbn10 or (type(book.metadata) == "table" and book.metadata.isbn10) or nil
            local cap_isbn13   = book.isbn13 or (type(book.metadata) == "table" and book.metadata.isbn13) or nil
            local cap_server_pagecount = tonumber(book.pagecount)
                or tonumber(book.server_pagecount)
                or (type(book.metadata) == "table" and (tonumber(book.metadata.pagecount) or tonumber(book.metadata.pageCount)))
            if cap_server_pagecount and cap_server_pagecount > 0 then
                cap_server_pagecount = math.floor(cap_server_pagecount + 0.5)
            else
                cap_server_pagecount = nil
            end

            local book_completed, book_result = runSubprocess(function()
                -- Child: check / download / hash one book.
@@ -717,6 +725,7 @@ function M.new(deps)
                            filepath = cap_filepath, hash = book_result.hash,
                            book_id  = cap_book_id,  title = cap_title,
                            author   = cap_author,   isbn10 = cap_isbn10, isbn13 = cap_isbn13,
                            server_pagecount = cap_server_pagecount,
                        })
                    elseif book_result.status == "skipped_uncached" then
                        skipped = skipped + 1
@@ -724,6 +733,7 @@ function M.new(deps)
                            filepath = cap_filepath, hash = book_result.hash,
                            book_id  = cap_book_id,  title = cap_title,
                            author   = cap_author,   isbn10 = cap_isbn10, isbn13 = cap_isbn13,
                            server_pagecount = cap_server_pagecount,
                        })
                    elseif book_result.status == "skipped" then
                        skipped = skipped + 1
@@ -746,6 +756,7 @@ function M.new(deps)
                    filepath = cap_filepath, hash = book_result.hash,
                    book_id  = cap_book_id,  title = cap_title,
                    author   = cap_author,   isbn10 = cap_isbn10, isbn13 = cap_isbn13,
                    server_pagecount = cap_server_pagecount,
                })
            elseif book_result.status == "skipped_uncached" then
                skipped = skipped + 1
@@ -753,6 +764,7 @@ function M.new(deps)
                    filepath = cap_filepath, hash = book_result.hash,
                    book_id  = cap_book_id,  title = cap_title,
                    author   = cap_author,   isbn10 = cap_isbn10, isbn13 = cap_isbn13,
                    server_pagecount = cap_server_pagecount,
                })
            elseif book_result.status == "skipped" then
                skipped = skipped + 1
@@ -773,7 +785,7 @@ function M.new(deps)
        local ok_commit, err_commit = pcall(function()
            for _, nb in ipairs(new_books) do
                self.db:saveBookCache(nb.filepath, nb.hash, nb.book_id,
                    nb.title, nb.author, nb.isbn10, nb.isbn13)
                    nb.title, nb.author, nb.isbn10, nb.isbn13, nb.server_pagecount)
            end

            if delete_removed then
+26 −4
Original line number Diff line number Diff line
@@ -745,7 +745,7 @@ describe("BookloreSync:syncFromBookloreShelf", function()
    assert.is_false(p.sync_in_progress)
  end)

  it("downloads a missing book and queues a DB write", function()
  it("downloads a missing book and queues a DB write with metadata", function()
    -- File does NOT exist on disk initially
    local filepath = "/tmp/test-dl/NewBook_7.epub"
    package.preload["libs/libkoreader-lfs"] = function()
@@ -754,7 +754,17 @@ describe("BookloreSync:syncFromBookloreShelf", function()
    end
    package.preload["booklore_api_client"] = function()
      return make_api_stub({
        books       = { { id = 7, title = "NewBook", extension = "epub" } },
        books       = {
          {
            id = 7,
            title = "NewBook",
            authors = "Jane Doe",
            isbn10 = "1234567890",
            isbn13 = "9781234567897",
            pagecount = 321,
            extension = "epub",
          },
        },
        download_ok = true,
      })
    end
@@ -764,8 +774,16 @@ describe("BookloreSync:syncFromBookloreShelf", function()

    local p = make_plugin()
    local saved = {}
    p.db.saveBookCache = function(_, fp, hash, bid, title, author, isbn10, isbn13)
      table.insert(saved, { fp = fp, bid = bid, title = title })
    p.db.saveBookCache = function(_, fp, hash, bid, title, author, isbn10, isbn13, server_pagecount)
      table.insert(saved, {
        fp = fp,
        bid = bid,
        title = title,
        author = author,
        isbn10 = isbn10,
        isbn13 = isbn13,
        server_pagecount = server_pagecount,
      })
    end

    p:syncFromBookloreShelf(true)
@@ -773,6 +791,10 @@ describe("BookloreSync:syncFromBookloreShelf", function()
    assert.are.equal(1, #saved)
    assert.are.equal(7, saved[1].bid)
    assert.are.equal("NewBook", saved[1].title)
    assert.are.equal("Jane Doe", saved[1].author)
    assert.are.equal("1234567890", saved[1].isbn10)
    assert.are.equal("9781234567897", saved[1].isbn13)
    assert.are.equal(321, saved[1].server_pagecount)
    assert.is_false(p.sync_in_progress)
  end)