forked from glitch-soc/mastodon
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
283 additions
and
3 deletions.
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
app/controllers/admin/push_subscription_blocks_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# frozen_string_literal: true | ||
|
||
module Admin | ||
class PushSubscriptionBlocksController < BaseController | ||
before_action :set_push_subscription_block, except: [:index, :new, :create] | ||
|
||
def index | ||
authorize :push_subscription_block, :update? | ||
@push_subscription_blocks = PushSubscriptionBlock.all | ||
end | ||
|
||
def new | ||
authorize :push_subscription_block, :update? | ||
@push_subscription_block = PushSubscriptionBlock.new | ||
end | ||
|
||
def create | ||
authorize :push_subscription_block, :update? | ||
|
||
@push_subscription_block = PushSubscriptionBlock.new(resource_params) | ||
|
||
if @push_subscription_block.save | ||
@push_subscription_block.enable! | ||
redirect_to admin_push_subscription_blocks_path | ||
else | ||
render action: :new | ||
end | ||
end | ||
|
||
def edit | ||
authorize :push_subscription_block, :update? | ||
end | ||
|
||
def update | ||
authorize :push_subscription_block, :update? | ||
|
||
if @push_subscription_block.update(resource_params) | ||
redirect_to admin_push_subscription_blocks_path | ||
else | ||
render action: :edit | ||
end | ||
end | ||
|
||
def destroy | ||
authorize :push_subscription_block, :update? | ||
@push_subscription_block.destroy | ||
redirect_to admin_push_subscription_blocks_path | ||
end | ||
|
||
def enable | ||
authorize :push_subscription_block, :update? | ||
@push_subscription_block.enable! | ||
redirect_to admin_push_subscription_blocks_path | ||
end | ||
|
||
def disable | ||
authorize :push_subscription_block, :update? | ||
@push_subscription_block.disable! | ||
redirect_to admin_push_subscription_blocks_path | ||
end | ||
|
||
private | ||
|
||
def set_push_subscription_block | ||
@push_subscription_block = PushSubscriptionBlock.find(params[:id]) | ||
end | ||
|
||
def resource_params | ||
params.require(:push_subscription_block).permit(:name, :endpoint) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# frozen_string_literal: true | ||
# == Schema Information | ||
# | ||
# Table name: push_subscription_blocks | ||
# | ||
# id :bigint(8) not null, primary key | ||
# name :string default(""), not null | ||
# endpoint :string not null | ||
# enable :boolean default(TRUE), not null | ||
# created_at :datetime not null | ||
# updated_at :datetime not null | ||
# | ||
class PushSubscriptionBlock < ApplicationRecord | ||
CACHE_KEY = 'push_subscription_blocks' | ||
|
||
validates :endpoint, presence: true, uniqueness: true, url: true, if: :will_save_change_to_endpoint? | ||
|
||
after_commit :reset_cache | ||
|
||
def enable! | ||
update!(enable: true) | ||
end | ||
|
||
def disable! | ||
update!(enable: false) | ||
end | ||
|
||
class << self | ||
def allow?(url) | ||
!deny?(url) | ||
end | ||
|
||
def deny?(url) | ||
blocks = Rails.cache.fetch(CACHE_KEY) { Regexp.union(PushSubscriptionBlock.where(enable: true).pluck(:endpoint).map { |pattern| Regexp.new("^#{Regexp.escape(pattern)}", Regexp::IGNORECASE) }) } | ||
blocks.match?(url) | ||
end | ||
end | ||
|
||
private | ||
|
||
def reset_cache | ||
Rails.cache.delete(CACHE_KEY) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# frozen_string_literal: true | ||
|
||
class PushSubscriptionBlockPolicy < ApplicationPolicy | ||
def update? | ||
admin? | ||
end | ||
end |
24 changes: 24 additions & 0 deletions
24
app/views/admin/push_subscription_blocks/_push_subscription_block.html.haml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
%tr | ||
%td.nowrap | ||
%samp= push_subscription_block.name | ||
%td.wrap | ||
%samp= push_subscription_block.endpoint | ||
%td.nowrap | ||
- if push_subscription_block.enable? | ||
%span.positive-hint | ||
= fa_icon('check') | ||
= ' ' | ||
= t 'admin.push_subscription_blocks.enabled' | ||
- else | ||
%span.negative-hint | ||
= fa_icon('times') | ||
= ' ' | ||
= t 'admin.push_subscription_blocks.disabled' | ||
%td.nowrap | ||
- if push_subscription_block.enable? | ||
= table_link_to 'power-off', t('admin.push_subscription_blocks.disable'), disable_admin_push_subscription_block_path(push_subscription_block), method: :post, data: { confirm: t('admin.accounts.are_you_sure') } | ||
- else | ||
= table_link_to 'power-off', t('admin.push_subscription_blocks.enable'), enable_admin_push_subscription_block_path(push_subscription_block), method: :post, data: { confirm: t('admin.accounts.are_you_sure') } | ||
|
||
= table_link_to 'pencil', t('admin.push_subscription_blocks.edit.title'), edit_admin_push_subscription_block_path(push_subscription_block) | ||
= table_link_to 'times', t('admin.push_subscription_blocks.delete'), admin_push_subscription_block_path(push_subscription_block), method: :delete, data: { confirm: t('admin.accounts.are_you_sure') } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
- content_for :page_title do | ||
= t('admin.push_subscription_blocks.edit.title') | ||
|
||
= simple_form_for @push_subscription_block, url: admin_push_subscription_block_path(@push_subscription_block), method: :put do |f| | ||
= render 'shared/error_messages', object: @push_subscription_block | ||
|
||
%p.hint= t('admin.push_subscription_blocks.enable_hint') | ||
|
||
.field-group | ||
= f.input :name, as: :string, wrapper: :with_block_label | ||
|
||
.field-group | ||
= f.input :endpoint, as: :string, wrapper: :with_block_label | ||
|
||
.actions | ||
= f.button :button, t('admin.push_subscription_blocks.save'), type: :submit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
- content_for :page_title do | ||
= t('admin.push_subscription_blocks.title') | ||
|
||
.simple_form | ||
%p.hint= t('admin.push_subscription_blocks.description_html') | ||
= link_to @push_subscription_blocks.empty? ? t('admin.push_subscription_blocks.setup') : t('admin.push_subscription_blocks.add_new'), new_admin_push_subscription_block_path, class: 'block-button' | ||
|
||
- unless @push_subscription_blocks.empty? | ||
%hr.spacer | ||
|
||
.table-wrapper | ||
%table.table | ||
%thead | ||
%tr | ||
%th= t('admin.push_subscription_blocks.name') | ||
%th= t('admin.push_subscription_blocks.endpoint') | ||
%th= t('admin.push_subscription_blocks.enable') | ||
%th | ||
%tbody | ||
= render @push_subscription_blocks | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
- content_for :page_title do | ||
= t('admin.push_subscription_blocks.add_new') | ||
|
||
= simple_form_for @push_subscription_block, url: admin_push_subscription_blocks_path do |f| | ||
= render 'shared/error_messages', object: @push_subscription_block | ||
|
||
%p.hint= t('admin.push_subscription_blocks.enable_hint') | ||
|
||
.field-group | ||
= f.input :name, as: :string, wrapper: :with_block_label | ||
|
||
.field-group | ||
= f.input :endpoint, as: :string, wrapper: :with_block_label | ||
|
||
.actions | ||
= f.button :button, t('admin.push_subscription_blocks.save'), type: :submit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
db/migrate/20221215211405_create_push_subscription_blocks.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class CreatePushSubscriptionBlocks < ActiveRecord::Migration[6.1] | ||
def change | ||
create_table :push_subscription_blocks do |t| | ||
t.string :name, null: false, default: '' | ||
t.string :endpoint, null: false | ||
t.boolean :enable, null: false, default: true | ||
|
||
t.timestamps | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Fabricator(:push_subscription_block) do | ||
name 'tootle' | ||
endpoint 'https://tootleformastodon.appspot.com/api/v1/notifications/callback/' | ||
enable true | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe PushSubscriptionBlock, type: :model do | ||
pending "add some examples to (or delete) #{__FILE__}" | ||
end |