Keyword/phrase filtering (#7905)
* Add keyword filtering GET|POST /api/v1/filters GET|PUT|DELETE /api/v1/filters/:id - Irreversible filters can drop toots from home or notifications - Other filters can hide toots through the client app - Filters use a phrase valid in particular contexts, expiration * Make sure expired filters don't get applied client-side * Add missing API methods * Remove "regex filter" from column settings * Add tests * Add test for FeedManager * Add CustomFilter test * Add UI for managing filters * Add streaming API event to allow syncing filters * Fix testslocal
parent
fbee9b5ac8
commit
cdb101340a
38 changed files with 530 additions and 72 deletions
@ -0,0 +1,48 @@ |
|||||||
|
# frozen_string_literal: true |
||||||
|
|
||||||
|
class Api::V1::FiltersController < Api::BaseController |
||||||
|
before_action -> { doorkeeper_authorize! :read }, only: [:index, :show] |
||||||
|
before_action -> { doorkeeper_authorize! :write }, except: [:index, :show] |
||||||
|
before_action :require_user! |
||||||
|
before_action :set_filters, only: :index |
||||||
|
before_action :set_filter, only: [:show, :update, :destroy] |
||||||
|
|
||||||
|
respond_to :json |
||||||
|
|
||||||
|
def index |
||||||
|
render json: @filters, each_serializer: REST::FilterSerializer |
||||||
|
end |
||||||
|
|
||||||
|
def create |
||||||
|
@filter = current_account.custom_filters.create!(resource_params) |
||||||
|
render json: @filter, serializer: REST::FilterSerializer |
||||||
|
end |
||||||
|
|
||||||
|
def show |
||||||
|
render json: @filter, serializer: REST::FilterSerializer |
||||||
|
end |
||||||
|
|
||||||
|
def update |
||||||
|
@filter.update!(resource_params) |
||||||
|
render json: @filter, serializer: REST::FilterSerializer |
||||||
|
end |
||||||
|
|
||||||
|
def destroy |
||||||
|
@filter.destroy! |
||||||
|
render_empty |
||||||
|
end |
||||||
|
|
||||||
|
private |
||||||
|
|
||||||
|
def set_filters |
||||||
|
@filters = current_account.custom_filters |
||||||
|
end |
||||||
|
|
||||||
|
def set_filter |
||||||
|
@filter = current_account.custom_filters.find(params[:id]) |
||||||
|
end |
||||||
|
|
||||||
|
def resource_params |
||||||
|
params.permit(:phrase, :expires_at, :irreversible, context: []) |
||||||
|
end |
||||||
|
end |
@ -0,0 +1,57 @@ |
|||||||
|
# frozen_string_literal: true |
||||||
|
|
||||||
|
class FiltersController < ApplicationController |
||||||
|
include Authorization |
||||||
|
|
||||||
|
layout 'admin' |
||||||
|
|
||||||
|
before_action :set_filters, only: :index |
||||||
|
before_action :set_filter, only: [:edit, :update, :destroy] |
||||||
|
|
||||||
|
def index |
||||||
|
@filters = current_account.custom_filters |
||||||
|
end |
||||||
|
|
||||||
|
def new |
||||||
|
@filter = current_account.custom_filters.build |
||||||
|
end |
||||||
|
|
||||||
|
def create |
||||||
|
@filter = current_account.custom_filters.build(resource_params) |
||||||
|
|
||||||
|
if @filter.save |
||||||
|
redirect_to filters_path |
||||||
|
else |
||||||
|
render action: :new |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
def edit; end |
||||||
|
|
||||||
|
def update |
||||||
|
if @filter.update(resource_params) |
||||||
|
redirect_to filters_path |
||||||
|
else |
||||||
|
render action: :edit |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
def destroy |
||||||
|
@filter.destroy |
||||||
|
redirect_to filters_path |
||||||
|
end |
||||||
|
|
||||||
|
private |
||||||
|
|
||||||
|
def set_filters |
||||||
|
@filters = current_account.custom_filters |
||||||
|
end |
||||||
|
|
||||||
|
def set_filter |
||||||
|
@filter = current_account.custom_filters.find(params[:id]) |
||||||
|
end |
||||||
|
|
||||||
|
def resource_params |
||||||
|
params.require(:custom_filter).permit(:phrase, :expires_in, :irreversible, context: []) |
||||||
|
end |
||||||
|
end |
@ -0,0 +1,26 @@ |
|||||||
|
import api from '../api'; |
||||||
|
|
||||||
|
export const FILTERS_FETCH_REQUEST = 'FILTERS_FETCH_REQUEST'; |
||||||
|
export const FILTERS_FETCH_SUCCESS = 'FILTERS_FETCH_SUCCESS'; |
||||||
|
export const FILTERS_FETCH_FAIL = 'FILTERS_FETCH_FAIL'; |
||||||
|
|
||||||
|
export const fetchFilters = () => (dispatch, getState) => { |
||||||
|
dispatch({ |
||||||
|
type: FILTERS_FETCH_REQUEST, |
||||||
|
skipLoading: true, |
||||||
|
}); |
||||||
|
|
||||||
|
api(getState) |
||||||
|
.get('/api/v1/filters') |
||||||
|
.then(({ data }) => dispatch({ |
||||||
|
type: FILTERS_FETCH_SUCCESS, |
||||||
|
filters: data, |
||||||
|
skipLoading: true, |
||||||
|
})) |
||||||
|
.catch(err => dispatch({ |
||||||
|
type: FILTERS_FETCH_FAIL, |
||||||
|
err, |
||||||
|
skipLoading: true, |
||||||
|
skipAlert: true, |
||||||
|
})); |
||||||
|
}; |
@ -0,0 +1,11 @@ |
|||||||
|
import { FILTERS_FETCH_SUCCESS } from '../actions/filters'; |
||||||
|
import { List as ImmutableList, fromJS } from 'immutable'; |
||||||
|
|
||||||
|
export default function filters(state = ImmutableList(), action) { |
||||||
|
switch(action.type) { |
||||||
|
case FILTERS_FETCH_SUCCESS: |
||||||
|
return fromJS(action.filters); |
||||||
|
default: |
||||||
|
return state; |
||||||
|
} |
||||||
|
}; |
@ -0,0 +1,24 @@ |
|||||||
|
# frozen_string_literal: true |
||||||
|
|
||||||
|
module Expireable |
||||||
|
extend ActiveSupport::Concern |
||||||
|
|
||||||
|
included do |
||||||
|
scope :expired, -> { where.not(expires_at: nil).where('expires_at < ?', Time.now.utc) } |
||||||
|
|
||||||
|
attr_reader :expires_in |
||||||
|
|
||||||
|
def expires_in=(interval) |
||||||
|
self.expires_at = interval.to_i.seconds.from_now unless interval.blank? |
||||||
|
@expires_in = interval |
||||||
|
end |
||||||
|
|
||||||
|
def expire! |
||||||
|
touch(:expires_at) |
||||||
|
end |
||||||
|
|
||||||
|
def expired? |
||||||
|
!expires_at.nil? && expires_at < Time.now.utc |
||||||
|
end |
||||||
|
end |
||||||
|
end |
@ -0,0 +1,55 @@ |
|||||||
|
# frozen_string_literal: true |
||||||
|
# == Schema Information |
||||||
|
# |
||||||
|
# Table name: custom_filters |
||||||
|
# |
||||||
|
# id :bigint(8) not null, primary key |
||||||
|
# account_id :bigint(8) |
||||||
|
# expires_at :datetime |
||||||
|
# phrase :text default(""), not null |
||||||
|
# context :string default([]), not null, is an Array |
||||||
|
# irreversible :boolean default(FALSE), not null |
||||||
|
# created_at :datetime not null |
||||||
|
# updated_at :datetime not null |
||||||
|
# |
||||||
|
|
||||||
|
class CustomFilter < ApplicationRecord |
||||||
|
VALID_CONTEXTS = %w( |
||||||
|
home |
||||||
|
notifications |
||||||
|
public |
||||||
|
thread |
||||||
|
).freeze |
||||||
|
|
||||||
|
include Expireable |
||||||
|
|
||||||
|
belongs_to :account |
||||||
|
|
||||||
|
validates :phrase, :context, presence: true |
||||||
|
validate :context_must_be_valid |
||||||
|
validate :irreversible_must_be_within_context |
||||||
|
|
||||||
|
scope :active_irreversible, -> { where(irreversible: true).where(Arel.sql('expires_at IS NULL OR expires_at > NOW()')) } |
||||||
|
|
||||||
|
before_validation :clean_up_contexts |
||||||
|
after_commit :remove_cache |
||||||
|
|
||||||
|
private |
||||||
|
|
||||||
|
def clean_up_contexts |
||||||
|
self.context = Array(context).map(&:strip).map(&:presence).compact |
||||||
|
end |
||||||
|
|
||||||
|
def remove_cache |
||||||
|
Rails.cache.delete("filters:#{account_id}") |
||||||
|
Redis.current.publish("timeline:#{account_id}", Oj.dump(event: :filters_changed)) |
||||||
|
end |
||||||
|
|
||||||
|
def context_must_be_valid |
||||||
|
errors.add(:context, I18n.t('filters.errors.invalid_context')) if context.empty? || context.any? { |c| !VALID_CONTEXTS.include?(c) } |
||||||
|
end |
||||||
|
|
||||||
|
def irreversible_must_be_within_context |
||||||
|
errors.add(:irreversible, I18n.t('filters.errors.invalid_irreversible')) if irreversible? && !context.include?('home') && !context.include?('notifications') |
||||||
|
end |
||||||
|
end |
@ -0,0 +1,5 @@ |
|||||||
|
# frozen_string_literal: true |
||||||
|
|
||||||
|
class REST::FilterSerializer < ActiveModel::Serializer |
||||||
|
attributes :id, :phrase, :context, :expires_at |
||||||
|
end |
@ -0,0 +1,11 @@ |
|||||||
|
.fields-group |
||||||
|
= f.input :phrase, as: :string, wrapper: :with_block_label |
||||||
|
|
||||||
|
.fields-group |
||||||
|
= f.input :context, wrapper: :with_block_label, collection: CustomFilter::VALID_CONTEXTS, as: :check_boxes, collection_wrapper_tag: 'ul', item_wrapper_tag: 'li', label_method: lambda { |context| I18n.t("filters.contexts.#{context}") }, include_blank: false |
||||||
|
|
||||||
|
.fields-group |
||||||
|
= f.input :irreversible, wrapper: :with_label |
||||||
|
|
||||||
|
.fields-group |
||||||
|
= f.input :expires_in, wrapper: :with_label, collection: [30.minutes, 1.hour, 6.hours, 12.hours, 1.day, 1.week].map(&:to_i), label_method: lambda { |i| I18n.t("invites.expires_in.#{i}") }, prompt: I18n.t('invites.expires_in_prompt') |
@ -0,0 +1,8 @@ |
|||||||
|
- content_for :page_title do |
||||||
|
= t('filters.edit.title') |
||||||
|
|
||||||
|
= simple_form_for @filter, url: filter_path(@filter), method: :put do |f| |
||||||
|
= render 'fields', f: f |
||||||
|
|
||||||
|
.actions |
||||||
|
= f.button :button, t('generic.save_changes'), type: :submit |
@ -0,0 +1,20 @@ |
|||||||
|
- content_for :page_title do |
||||||
|
= t('filters.index.title') |
||||||
|
|
||||||
|
.table-wrapper |
||||||
|
%table.table |
||||||
|
%thead |
||||||
|
%tr |
||||||
|
%th= t('simple_form.labels.defaults.phrase') |
||||||
|
%th= t('simple_form.labels.defaults.context') |
||||||
|
%th |
||||||
|
%tbody |
||||||
|
- @filters.each do |filter| |
||||||
|
%tr |
||||||
|
%td= filter.phrase |
||||||
|
%td= filter.context.map { |context| I18n.t("filters.contexts.#{context}") }.join(', ') |
||||||
|
%td |
||||||
|
= table_link_to 'pencil', t('filters.edit.title'), edit_filter_path(filter) |
||||||
|
= table_link_to 'times', t('filters.index.delete'), filter_path(filter), method: :delete |
||||||
|
|
||||||
|
= link_to t('filters.new.title'), new_filter_path, class: 'button' |
@ -0,0 +1,8 @@ |
|||||||
|
- content_for :page_title do |
||||||
|
= t('filters.new.title') |
||||||
|
|
||||||
|
= simple_form_for @filter, url: filters_path do |f| |
||||||
|
= render 'fields', f: f |
||||||
|
|
||||||
|
.actions |
||||||
|
= f.button :button, t('filters.new.title'), type: :submit |
@ -0,0 +1,13 @@ |
|||||||
|
class CreateCustomFilters < ActiveRecord::Migration[5.2] |
||||||
|
def change |
||||||
|
create_table :custom_filters do |t| |
||||||
|
t.belongs_to :account, foreign_key: { on_delete: :cascade } |
||||||
|
t.datetime :expires_at |
||||||
|
t.text :phrase, null: false, default: '' |
||||||
|
t.string :context, array: true, null: false, default: [] |
||||||
|
t.boolean :irreversible, null: false, default: false |
||||||
|
|
||||||
|
t.timestamps |
||||||
|
end |
||||||
|
end |
||||||
|
end |
@ -0,0 +1,81 @@ |
|||||||
|
require 'rails_helper' |
||||||
|
|
||||||
|
RSpec.describe Api::V1::FiltersController, type: :controller do |
||||||
|
render_views |
||||||
|
|
||||||
|
let(:user) { Fabricate(:user) } |
||||||
|
let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: 'read write') } |
||||||
|
|
||||||
|
before do |
||||||
|
allow(controller).to receive(:doorkeeper_token) { token } |
||||||
|
end |
||||||
|
|
||||||
|
describe 'GET #index' do |
||||||
|
let!(:filter) { Fabricate(:custom_filter, account: user.account) } |
||||||
|
|
||||||
|
it 'returns http success' do |
||||||
|
get :index |
||||||
|
expect(response).to have_http_status(200) |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
describe 'POST #create' do |
||||||
|
before do |
||||||
|
post :create, params: { phrase: 'magic', context: %w(home), irreversible: true } |
||||||
|
end |
||||||
|
|
||||||
|
it 'returns http success' do |
||||||
|
expect(response).to have_http_status(200) |
||||||
|
end |
||||||
|
|
||||||
|
it 'creates a filter' do |
||||||
|
filter = user.account.custom_filters.first |
||||||
|
expect(filter).to_not be_nil |
||||||
|
expect(filter.phrase).to eq 'magic' |
||||||
|
expect(filter.context).to eq %w(home) |
||||||
|
expect(filter.irreversible?).to be true |
||||||
|
expect(filter.expires_at).to be_nil |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
describe 'GET #show' do |
||||||
|
let(:filter) { Fabricate(:custom_filter, account: user.account) } |
||||||
|
|
||||||
|
it 'returns http success' do |
||||||
|
get :show, params: { id: filter.id } |
||||||
|
expect(response).to have_http_status(200) |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
describe 'PUT #update' do |
||||||
|
let(:filter) { Fabricate(:custom_filter, account: user.account) } |
||||||
|
|
||||||
|
before do |
||||||
|
put :update, params: { id: filter.id, phrase: 'updated' } |
||||||
|
end |
||||||
|
|
||||||
|
it 'returns http success' do |
||||||
|
expect(response).to have_http_status(200) |
||||||
|
end |
||||||
|
|
||||||
|
it 'updates the filter' do |
||||||
|
expect(filter.reload.phrase).to eq 'updated' |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
describe 'DELETE #destroy' do |
||||||
|
let(:filter) { Fabricate(:custom_filter, account: user.account) } |
||||||
|
|
||||||
|
before do |
||||||
|
delete :destroy, params: { id: filter.id } |
||||||
|
end |
||||||
|
|
||||||
|
it 'returns http success' do |
||||||
|
expect(response).to have_http_status(200) |
||||||
|
end |
||||||
|
|
||||||
|
it 'removes the filter' do |
||||||
|
expect { filter.reload }.to raise_error ActiveRecord::RecordNotFound |
||||||
|
end |
||||||
|
end |
||||||
|
end |
@ -0,0 +1,6 @@ |
|||||||
|
Fabricator(:custom_filter) do |
||||||
|
account |
||||||
|
expires_at nil |
||||||
|
phrase 'discourse' |
||||||
|
context %w(home notifications) |
||||||
|
end |
@ -0,0 +1,5 @@ |
|||||||
|
require 'rails_helper' |
||||||
|
|
||||||
|
RSpec.describe CustomFilter, type: :model do |
||||||
|
|
||||||
|
end |
Loading…
Reference in new issue