Add auto tagger for posts
This commit is contained in:
31
app/commands/auto_tagging/tag.rb
Normal file
31
app/commands/auto_tagging/tag.rb
Normal file
@@ -0,0 +1,31 @@
|
||||
module Adamantium
|
||||
module Commands
|
||||
module AutoTagging
|
||||
class Tag
|
||||
include Dry::Monads[:result]
|
||||
include Deps["repos.post_repo", "repos.auto_tagging_repo"]
|
||||
|
||||
def call(auto_tag_id: nil)
|
||||
auto_taggings = if auto_tag_id
|
||||
auto_tagging_repo.find(auto_tag_id)
|
||||
else
|
||||
auto_tagging_repo.all
|
||||
end
|
||||
|
||||
auto_taggings.each do |auto_tagging|
|
||||
posts = auto_tagging.title_only? ?
|
||||
post_repo.by_title(title_contains: auto_tagging.title_contains) :
|
||||
post_repo.by_content(body_contains: auto_tagging.body_contains)
|
||||
|
||||
posts.each do |post|
|
||||
post_repo.auto_tag_post(post_id: post.id,
|
||||
tag_id: auto_tagging.tag_id)
|
||||
end
|
||||
end
|
||||
|
||||
Success()
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
@@ -10,6 +10,7 @@ module Adamantium
|
||||
syndicate: "commands.posts.syndicate",
|
||||
send_to_dayone: "syndication.dayone",
|
||||
send_webmentions: "commands.posts.send_webmentions",
|
||||
auto_tag: "commands.auto_tagging.tag",
|
||||
]
|
||||
|
||||
include Dry::Monads[:result]
|
||||
@@ -20,6 +21,8 @@ module Adamantium
|
||||
|
||||
syndicate.call(created_post.id, post)
|
||||
|
||||
auto_tag.call()
|
||||
|
||||
# decorated_post = Decorators::Posts::Decorator.new(created_post)
|
||||
|
||||
# send_webmentions.call(post_content: attrs[:content], post_url: decorated_post.permalink)
|
||||
|
13
app/entities/auto_tagging.rb
Normal file
13
app/entities/auto_tagging.rb
Normal file
@@ -0,0 +1,13 @@
|
||||
module Adamantium
|
||||
module Entities
|
||||
class AutoTagging < Dry::Struct
|
||||
attribute? :title_contains, Types::Optional::String
|
||||
attribute? :body_contains, Types::Optional::String
|
||||
attribute :tag_id, Types::Coercible::Integer
|
||||
|
||||
def title_only?
|
||||
!title_contains.empty?
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
18
app/repos/auto_tagging_repo.rb
Normal file
18
app/repos/auto_tagging_repo.rb
Normal file
@@ -0,0 +1,18 @@
|
||||
module Adamantium
|
||||
module Repos
|
||||
class AutoTaggingRepo < Adamantium::Repo[:auto_taggings]
|
||||
def find(id)
|
||||
auto_taggings
|
||||
.where(id: id)
|
||||
.map_to(Adamantium::Entities::AutoTagging)
|
||||
.to_a
|
||||
end
|
||||
|
||||
def all
|
||||
auto_taggings
|
||||
.map_to(Adamantium::Entities::AutoTagging)
|
||||
.to_a
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
@@ -47,6 +47,38 @@ module Adamantium
|
||||
end
|
||||
end
|
||||
|
||||
def auto_tag_post(post_id:, tag_id:)
|
||||
|
||||
return if posts
|
||||
.post_tags
|
||||
.where(
|
||||
post_id: post_id,
|
||||
tag_id: tag_id
|
||||
).count > 0
|
||||
|
||||
posts
|
||||
.post_tags
|
||||
.changeset(:create, {
|
||||
post_id: post_id,
|
||||
tag_id: tag_id
|
||||
})
|
||||
.commit
|
||||
end
|
||||
|
||||
def by_title(title_contains:)
|
||||
posts
|
||||
.where(post_type: "post")
|
||||
.published
|
||||
.where(Sequel.ilike(:name, "%#{title_contains}%")).to_a
|
||||
end
|
||||
|
||||
def by_content(body_contains:)
|
||||
posts
|
||||
.where(post_type: "post")
|
||||
.published
|
||||
.where(Sequel.ilike(:content, "%#{body_contains}%")).to_a
|
||||
end
|
||||
|
||||
def remove_tag(post_id:, tag:)
|
||||
tag = posts.tags.where(label: tag).one
|
||||
|
||||
|
Reference in New Issue
Block a user