Add bookmark admin
This commit is contained in:
20
slices/admin/actions/bookmarks/cache.rb
Normal file
20
slices/admin/actions/bookmarks/cache.rb
Normal file
@@ -0,0 +1,20 @@
|
||||
module Admin
|
||||
module Actions
|
||||
module Bookmarks
|
||||
class Cache < Action
|
||||
|
||||
include Deps["commands.bookmarks.cache"]
|
||||
|
||||
def handle(req, res)
|
||||
bookmark_id = req.params[:id]
|
||||
|
||||
if cache.(bookmark_id: bookmark_id).success?
|
||||
res.body = "Success"
|
||||
else
|
||||
res.body = "Failed"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
17
slices/admin/actions/bookmarks/delete.rb
Normal file
17
slices/admin/actions/bookmarks/delete.rb
Normal file
@@ -0,0 +1,17 @@
|
||||
module Admin
|
||||
module Actions
|
||||
module Bookmarks
|
||||
class Delete < Action
|
||||
|
||||
include Deps["repos.bookmark_repo", "repos.post_tag_repo"]
|
||||
|
||||
def handle(req, res)
|
||||
bookmark_id = req.params[:id]
|
||||
|
||||
post_tag_repo.delete_by_post_id(post_id: bookmark_id)
|
||||
bookmark_repo.delete(id: bookmark_id)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
14
slices/admin/actions/bookmarks/index.rb
Normal file
14
slices/admin/actions/bookmarks/index.rb
Normal file
@@ -0,0 +1,14 @@
|
||||
module Admin
|
||||
module Actions
|
||||
module Bookmarks
|
||||
class Index < Action
|
||||
|
||||
include Deps["views.bookmarks.index"]
|
||||
|
||||
def handle(req, res)
|
||||
res.render index
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
25
slices/admin/commands/bookmarks/cache.rb
Normal file
25
slices/admin/commands/bookmarks/cache.rb
Normal file
@@ -0,0 +1,25 @@
|
||||
require "readability"
|
||||
require "down"
|
||||
|
||||
module Admin
|
||||
module Commands
|
||||
module Bookmarks
|
||||
class Cache
|
||||
include Dry::Monads[:result]
|
||||
include Deps["repos.bookmark_repo"]
|
||||
|
||||
def call(bookmark_id: )
|
||||
bookmark = bookmark_repo.fetch(id: bookmark_id)
|
||||
bookmark.url
|
||||
|
||||
tempfile = Down.download(bookmark.url)
|
||||
content = Readability::Document.new(tempfile.read, tags: %w[div p h1 h2 h3 h4 h5 h6]).content
|
||||
|
||||
bookmark_repo.update(id: bookmark_id, cached_content: content)
|
||||
|
||||
Success()
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
23
slices/admin/repos/bookmark_repo.rb
Normal file
23
slices/admin/repos/bookmark_repo.rb
Normal file
@@ -0,0 +1,23 @@
|
||||
module Admin
|
||||
module Repos
|
||||
class BookmarkRepo < Adamantium::Repo[:posts]
|
||||
def list
|
||||
posts
|
||||
.where(post_type: "bookmark")
|
||||
.to_a
|
||||
end
|
||||
|
||||
def fetch(id:)
|
||||
posts.where(id: id).one
|
||||
end
|
||||
|
||||
def delete(id:)
|
||||
posts.where(id: id).delete
|
||||
end
|
||||
|
||||
def update(id:, cached_content:)
|
||||
posts.where(id: id).update(cached_content: cached_content)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
@@ -5,6 +5,10 @@ module Admin
|
||||
def delete(tag_id:)
|
||||
post_tags.where(tag_id: tag_id).delete
|
||||
end
|
||||
|
||||
def delete_by_post_id(post_id:)
|
||||
post_tags.where(post_id: post_id).delete
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
31
slices/admin/templates/bookmarks/index.html.slim
Normal file
31
slices/admin/templates/bookmarks/index.html.slim
Normal file
@@ -0,0 +1,31 @@
|
||||
div class="mb-12 prose dark:prose-invert max-w-prose mx-auto text-gray-800 dark:text-gray-200"
|
||||
h1 Admin // Bookmarks
|
||||
|
||||
div class="max-w-prose mx-auto"
|
||||
table class="prose dark:prose-invert table-auto prose-a:text-blue-600 prose-a:no-underline"
|
||||
thead
|
||||
th Details
|
||||
th Date
|
||||
th Actions
|
||||
tbody
|
||||
- bookmarks.each do |bookmark|
|
||||
tr id="bookmark-#{bookmark.id}"
|
||||
td
|
||||
div
|
||||
= bookmark.name
|
||||
a class="no-underline" href=bookmark.url
|
||||
small class="text-gray-400 dark:text-gray-600" = bookmark.url
|
||||
div
|
||||
- if bookmark.cached_content
|
||||
a href="/bookmark/#{bookmark.slug}" View cached version
|
||||
- else
|
||||
button hx-post="/admin/bookmarks/cache/#{bookmark.id}" No cached content, cache now?
|
||||
td
|
||||
= bookmark.published_at&.strftime("%d %b %Y")
|
||||
td
|
||||
button hx-delete="/admin/bookmarks/#{bookmark.id}" hx-target="#bookmark-#{bookmark.id}" delete
|
||||
|
||||
div class="max-w-screen-md mx-auto border-t-4 border-solid border-gray-400 dark:border-gray-600"
|
||||
|
||||
|
||||
|
@@ -7,6 +7,8 @@ div class="max-w-prose mx-auto prose dark:prose-invert"
|
||||
a href="/admin/tags" Tags
|
||||
li
|
||||
a href="/admin/tags/auto_tagging" Auto tagging
|
||||
li
|
||||
a href="/admin/bookmarks" Bookmarks
|
||||
|
||||
div class="max-w-screen-md mx-auto border-t-4 border-solid border-gray-400 dark:border-gray-600"
|
||||
|
||||
|
@@ -47,6 +47,8 @@ html
|
||||
a class="p-1 rounded text-gray-400 hover:bg-red-100 hover:text-red-400 dark:hover:bg-red-200 #{link_active?('about') ? 'text-red-600 dark:text-red-400' : ''}" href="/admin/tags" Tags
|
||||
span class="text-gray-400 dark:text-gray-600"
|
||||
= "/"
|
||||
a class="p-1 rounded text-gray-400 hover:bg-red-100 hover:text-red-400 dark:hover:bg-red-200 #{link_active?('about') ? 'text-red-600 dark:text-red-400' : ''}" href="/admin/bookmarks" Bookmarks
|
||||
|
||||
== yield
|
||||
div class="px-4 max-w-screen-md mx-auto pb-10"
|
||||
p class="float-left text-gray-200 dark:text-gray-600" © 2023 Daniel Nitsikopoulos. All rights reserved.
|
||||
|
14
slices/admin/views/bookmarks/index.rb
Normal file
14
slices/admin/views/bookmarks/index.rb
Normal file
@@ -0,0 +1,14 @@
|
||||
module Admin
|
||||
module Views
|
||||
module Bookmarks
|
||||
class Index < Admin::View
|
||||
|
||||
include Deps["repos.bookmark_repo"]
|
||||
|
||||
expose :bookmarks do
|
||||
bookmark_repo.list
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Reference in New Issue
Block a user