Added LinkBlock functionality in admin

• Added:
- LinkBlock functionality in admin
This commit is contained in:
mgabdev
2020-12-08 23:19:10 -05:00
parent 95d326936b
commit c18991f174
16 changed files with 190 additions and 2 deletions

View File

@@ -0,0 +1,47 @@
# frozen_string_literal: true
module Admin
class LinkBlocksController < BaseController
before_action :set_link_block, only: [:show, :destroy]
def index
authorize :link_block, :index?
@link_blocks = LinkBlock.page(params[:page])
end
def new
authorize :link_block, :create?
@link_block = LinkBlock.new
end
def create
authorize :link_block, :create?
@link_block = LinkBlock.new(resource_params)
if @link_block.save
log_action :create, @link_block
redirect_to admin_link_blocks_path, notice: I18n.t('admin.link_blocks.created_msg')
else
render :new
end
end
def destroy
authorize @link_block, :destroy?
@link_block.destroy!
log_action :destroy, @link_block
redirect_to admin_link_blocks_path, notice: I18n.t('admin.link_blocks.destroyed_msg')
end
private
def set_link_block
@link_block = LinkBlock.find(params[:id])
end
def resource_params
params.require(:link_block).permit(:link)
end
end
end

View File

@@ -22,6 +22,12 @@ class TagManager
uri.normalized_host
end
def normalize_link(link)
return if link.nil?
uri = Addressable::URI.parse(link)
return "#{uri.normalized_host}#{uri.normalized_path}"
end
def same_acct?(canonical, needle)
return true if canonical.casecmp(needle).zero?
username, domain = needle.split('@')

View File

@@ -0,0 +1,15 @@
# frozen_string_literal: true
module LinkNormalizable
extend ActiveSupport::Concern
included do
before_validation :normalize_link
end
private
def normalize_link
self.link = TagManager.instance.normalize_link(link&.strip)
end
end

26
app/models/link_block.rb Normal file
View File

@@ -0,0 +1,26 @@
# frozen_string_literal: true
# == Schema Information
#
# Table name: link_blocks
#
# id :bigint(8) not null, primary key
# link :string default(""), not null
# created_at :datetime not null
# updated_at :datetime not null
#
class LinkBlock < ApplicationRecord
include LinkNormalizable
validates :link, presence: true, uniqueness: true
def self.block?(text)
return false if text.nil?
return false if text.length < 1
urls = text.scan(FetchLinkCardService::URL_PATTERN).map { |array| Addressable::URI.parse(array[0]).normalize }
url = urls.first
link_for_fetch = TagManager.instance.normalize_link(url)
where(link: link_for_fetch).exists?
end
end

View File

@@ -0,0 +1,15 @@
# frozen_string_literal: true
class LinkBlockPolicy < ApplicationPolicy
def index?
admin?
end
def create?
admin?
end
def destroy?
admin?
end
end

View File

@@ -25,6 +25,7 @@ class EditStatusService < BaseService
return idempotency_duplicate if idempotency_given? && idempotency_duplicate?
validate_links!
validate_media!
preprocess_attributes!
revision_text = prepare_revision_text
@@ -89,6 +90,10 @@ class EditStatusService < BaseService
raise GabSocial::ValidationError, I18n.t('media_attachments.validations.images_and_video') if @media.size > 1 && hasVideoOrGif
end
def validate_links!
raise GabSocial::NotPermittedError if LinkBlock.block?(@text)
end
def language_from_option(str)
ISO_639.find(str)&.alpha2
end

View File

@@ -34,6 +34,7 @@ class PostStatusService < BaseService
return idempotency_duplicate if idempotency_given? && idempotency_duplicate?
validate_links!
validate_media!
validate_group!
preprocess_attributes!
@@ -98,7 +99,7 @@ class PostStatusService < BaseService
end
def postprocess_status!
LinkCrawlWorker.perform_async(@status.id) unless @status.spoiler_text?
LinkCrawlWorker.perform_async(@status.id)
DistributionWorker.perform_async(@status.id)
# Pubsubhubbub::DistributionWorker.perform_async(@status.stream_entry.id)
# ActivityPub::DistributionWorker.perform_async(@status.id)
@@ -127,6 +128,10 @@ class PostStatusService < BaseService
raise GabSocial::ValidationError, I18n.t('media_attachments.validations.images_and_video') if @media.size > 1 && hasVideoOrGif
end
def validate_links!
raise GabSocial::NotPermittedError if LinkBlock.block?(@text)
end
def language_from_option(str)
ISO_639.find(str)&.alpha2
end

View File

@@ -0,0 +1,5 @@
%tr
%td
%samp= link_block.link
%td
= table_link_to 'trash', t('admin.link_blocks.delete'), admin_link_block_path(link_block), method: :delete

View File

@@ -0,0 +1,14 @@
- content_for :page_title do
= t('admin.link_blocks.title')
.table-wrapper
%table.table
%thead
%tr
%th= t('admin.link_blocks.link')
%th
%tbody
= render @link_blocks
= paginate @link_blocks
= link_to t('admin.link_blocks.add_new'), new_admin_link_block_path, class: 'button'

View File

@@ -0,0 +1,11 @@
- content_for :page_title do
= t('.title')
= simple_form_for @link_block, url: admin_link_blocks_path do |f|
= render 'shared/error_messages', object: @link_block
.fields-group
= f.input :link, wrapper: :with_label, label: t('admin.link_blocks.link')
.actions
= f.button :button, t('.create'), type: :submit