Account domain blocks (#2381)
* Add <ostatus:conversation /> tag to Atom input/output Only uses ref attribute (not href) because href would be the alternate link that's always included also. Creates new conversation for every non-reply status. Carries over conversation for every reply. Keeps remote URIs verbatim, generates local URIs on the fly like the rest of them. * Conversation muting - prevents notifications that reference a conversation (including replies, favourites, reblogs) from being created. API endpoints /api/v1/statuses/:id/mute and /api/v1/statuses/:id/unmute Currently no way to tell when a status/conversation is muted, so the web UI only has a "disable notifications" button, doesn't work as a toggle * Display "Dismiss notifications" on all statuses in notifications column, not just own * Add "muted" as a boolean attribute on statuses JSON For now always false on contained reblogs, since it's only relevant for statuses returned from the notifications endpoint, which are not nested Remove "Disable notifications" from detailed status view, since it's only relevant in the notifications column * Up max class length * Remove pending test for conversation mute * Add tests, clean up * Rename to "mute conversation" and "unmute conversation" * Raise validation error when trying to mute/unmute status without conversation * Adding account domain blocks that filter notifications and public timelines * Add tests for domain blocks in notifications, public timelines Filter reblogs of blocked domains from home * Add API for listing and creating account domain blocks * API for creating/deleting domain blocks, tests for Status#ancestors and Status#descendants, filter domain blocks from them * Filter domains in streaming API * Update account_domain_block_spec.rbmaster
parent
8ec8410651
commit
620d0d8029
@ -0,0 +1,38 @@ |
|||||||
|
# frozen_string_literal: true |
||||||
|
|
||||||
|
class Api::V1::DomainBlocksController < ApiController |
||||||
|
before_action -> { doorkeeper_authorize! :follow } |
||||||
|
before_action :require_user! |
||||||
|
|
||||||
|
respond_to :json |
||||||
|
|
||||||
|
def show |
||||||
|
@blocks = AccountDomainBlock.where(account: current_account).paginate_by_max_id(limit_param(100), params[:max_id], params[:since_id]) |
||||||
|
|
||||||
|
next_path = api_v1_domain_blocks_url(pagination_params(max_id: @blocks.last.id)) if @blocks.size == limit_param(100) |
||||||
|
prev_path = api_v1_domain_blocks_url(pagination_params(since_id: @blocks.first.id)) unless @blocks.empty? |
||||||
|
|
||||||
|
set_pagination_headers(next_path, prev_path) |
||||||
|
render json: @blocks.map(&:domain) |
||||||
|
end |
||||||
|
|
||||||
|
def create |
||||||
|
current_account.block_domain!(domain_block_params[:domain]) |
||||||
|
render_empty |
||||||
|
end |
||||||
|
|
||||||
|
def destroy |
||||||
|
current_account.unblock_domain!(domain_block_params[:domain]) |
||||||
|
render_empty |
||||||
|
end |
||||||
|
|
||||||
|
private |
||||||
|
|
||||||
|
def pagination_params(core_params) |
||||||
|
params.permit(:limit).merge(core_params) |
||||||
|
end |
||||||
|
|
||||||
|
def domain_block_params |
||||||
|
params.permit(:domain) |
||||||
|
end |
||||||
|
end |
@ -0,0 +1,26 @@ |
|||||||
|
# frozen_string_literal: true |
||||||
|
# == Schema Information |
||||||
|
# |
||||||
|
# Table name: account_domain_blocks |
||||||
|
# |
||||||
|
# id :integer not null, primary key |
||||||
|
# account_id :integer |
||||||
|
# domain :string |
||||||
|
# created_at :datetime not null |
||||||
|
# updated_at :datetime not null |
||||||
|
# |
||||||
|
|
||||||
|
class AccountDomainBlock < ApplicationRecord |
||||||
|
include Paginable |
||||||
|
|
||||||
|
belongs_to :account, required: true |
||||||
|
|
||||||
|
after_create :remove_blocking_cache |
||||||
|
after_destroy :remove_blocking_cache |
||||||
|
|
||||||
|
private |
||||||
|
|
||||||
|
def remove_blocking_cache |
||||||
|
Rails.cache.delete("exclude_domains_for:#{account_id}") |
||||||
|
end |
||||||
|
end |
@ -0,0 +1,127 @@ |
|||||||
|
# frozen_string_literal: true |
||||||
|
|
||||||
|
module AccountInteractions |
||||||
|
extend ActiveSupport::Concern |
||||||
|
|
||||||
|
class_methods do |
||||||
|
def following_map(target_account_ids, account_id) |
||||||
|
follow_mapping(Follow.where(target_account_id: target_account_ids, account_id: account_id), :target_account_id) |
||||||
|
end |
||||||
|
|
||||||
|
def followed_by_map(target_account_ids, account_id) |
||||||
|
follow_mapping(Follow.where(account_id: target_account_ids, target_account_id: account_id), :account_id) |
||||||
|
end |
||||||
|
|
||||||
|
def blocking_map(target_account_ids, account_id) |
||||||
|
follow_mapping(Block.where(target_account_id: target_account_ids, account_id: account_id), :target_account_id) |
||||||
|
end |
||||||
|
|
||||||
|
def muting_map(target_account_ids, account_id) |
||||||
|
follow_mapping(Mute.where(target_account_id: target_account_ids, account_id: account_id), :target_account_id) |
||||||
|
end |
||||||
|
|
||||||
|
def requested_map(target_account_ids, account_id) |
||||||
|
follow_mapping(FollowRequest.where(target_account_id: target_account_ids, account_id: account_id), :target_account_id) |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
included do |
||||||
|
# Follow relations |
||||||
|
has_many :follow_requests, dependent: :destroy |
||||||
|
|
||||||
|
has_many :active_relationships, class_name: 'Follow', foreign_key: 'account_id', dependent: :destroy |
||||||
|
has_many :passive_relationships, class_name: 'Follow', foreign_key: 'target_account_id', dependent: :destroy |
||||||
|
|
||||||
|
has_many :following, -> { order('follows.id desc') }, through: :active_relationships, source: :target_account |
||||||
|
has_many :followers, -> { order('follows.id desc') }, through: :passive_relationships, source: :account |
||||||
|
|
||||||
|
# Block relationships |
||||||
|
has_many :block_relationships, class_name: 'Block', foreign_key: 'account_id', dependent: :destroy |
||||||
|
has_many :blocking, -> { order('blocks.id desc') }, through: :block_relationships, source: :target_account |
||||||
|
has_many :blocked_by_relationships, class_name: 'Block', foreign_key: :target_account_id, dependent: :destroy |
||||||
|
has_many :blocked_by, -> { order('blocks.id desc') }, through: :blocked_by_relationships, source: :account |
||||||
|
|
||||||
|
# Mute relationships |
||||||
|
has_many :mute_relationships, class_name: 'Mute', foreign_key: 'account_id', dependent: :destroy |
||||||
|
has_many :muting, -> { order('mutes.id desc') }, through: :mute_relationships, source: :target_account |
||||||
|
has_many :conversation_mutes, dependent: :destroy |
||||||
|
has_many :domain_blocks, class_name: 'AccountDomainBlock', dependent: :destroy |
||||||
|
|
||||||
|
def follow!(other_account) |
||||||
|
active_relationships.find_or_create_by!(target_account: other_account) |
||||||
|
end |
||||||
|
|
||||||
|
def block!(other_account) |
||||||
|
block_relationships.find_or_create_by!(target_account: other_account) |
||||||
|
end |
||||||
|
|
||||||
|
def mute!(other_account) |
||||||
|
mute_relationships.find_or_create_by!(target_account: other_account) |
||||||
|
end |
||||||
|
|
||||||
|
def mute_conversation!(conversation) |
||||||
|
conversation_mutes.find_or_create_by!(conversation: conversation) |
||||||
|
end |
||||||
|
|
||||||
|
def block_domain!(other_domain) |
||||||
|
domain_blocks.find_or_create_by!(domain: other_domain) |
||||||
|
end |
||||||
|
|
||||||
|
def unfollow!(other_account) |
||||||
|
follow = active_relationships.find_by(target_account: other_account) |
||||||
|
follow&.destroy |
||||||
|
end |
||||||
|
|
||||||
|
def unblock!(other_account) |
||||||
|
block = block_relationships.find_by(target_account: other_account) |
||||||
|
block&.destroy |
||||||
|
end |
||||||
|
|
||||||
|
def unmute!(other_account) |
||||||
|
mute = mute_relationships.find_by(target_account: other_account) |
||||||
|
mute&.destroy |
||||||
|
end |
||||||
|
|
||||||
|
def unmute_conversation!(conversation) |
||||||
|
mute = conversation_mutes.find_by(conversation: conversation) |
||||||
|
mute&.destroy! |
||||||
|
end |
||||||
|
|
||||||
|
def unblock_domain!(other_domain) |
||||||
|
block = domain_blocks.find_by(domain: other_domain) |
||||||
|
block&.destroy |
||||||
|
end |
||||||
|
|
||||||
|
def following?(other_account) |
||||||
|
active_relationships.where(target_account: other_account).exists? |
||||||
|
end |
||||||
|
|
||||||
|
def blocking?(other_account) |
||||||
|
block_relationships.where(target_account: other_account).exists? |
||||||
|
end |
||||||
|
|
||||||
|
def domain_blocking?(other_domain) |
||||||
|
domain_blocks.where(domain: other_domain).exists? |
||||||
|
end |
||||||
|
|
||||||
|
def muting?(other_account) |
||||||
|
mute_relationships.where(target_account: other_account).exists? |
||||||
|
end |
||||||
|
|
||||||
|
def muting_conversation?(conversation) |
||||||
|
conversation_mutes.where(conversation: conversation).exists? |
||||||
|
end |
||||||
|
|
||||||
|
def requested?(other_account) |
||||||
|
follow_requests.where(target_account: other_account).exists? |
||||||
|
end |
||||||
|
|
||||||
|
def favourited?(status) |
||||||
|
status.proper.favourites.where(account: self).exists? |
||||||
|
end |
||||||
|
|
||||||
|
def reblogged?(status) |
||||||
|
status.proper.reblogs.where(account: self).exists? |
||||||
|
end |
||||||
|
end |
||||||
|
end |
@ -0,0 +1,12 @@ |
|||||||
|
class CreateAccountDomainBlocks < ActiveRecord::Migration[5.0] |
||||||
|
def change |
||||||
|
create_table :account_domain_blocks do |t| |
||||||
|
t.integer :account_id |
||||||
|
t.string :domain |
||||||
|
|
||||||
|
t.timestamps |
||||||
|
end |
||||||
|
|
||||||
|
add_index :account_domain_blocks, [:account_id, :domain], unique: true |
||||||
|
end |
||||||
|
end |
@ -0,0 +1,55 @@ |
|||||||
|
require 'rails_helper' |
||||||
|
|
||||||
|
RSpec.describe Api::V1::DomainBlocksController, type: :controller do |
||||||
|
render_views |
||||||
|
|
||||||
|
let(:user) { Fabricate(:user, account: Fabricate(:account, username: 'alice')) } |
||||||
|
let(:token) { double acceptable?: true, resource_owner_id: user.id } |
||||||
|
|
||||||
|
before do |
||||||
|
user.account.block_domain!('example.com') |
||||||
|
allow(controller).to receive(:doorkeeper_token) { token } |
||||||
|
end |
||||||
|
|
||||||
|
describe 'GET #show' do |
||||||
|
before do |
||||||
|
get :show |
||||||
|
end |
||||||
|
|
||||||
|
it 'returns http success' do |
||||||
|
expect(response).to have_http_status(:success) |
||||||
|
end |
||||||
|
|
||||||
|
it 'returns blocked domains' do |
||||||
|
expect(body_as_json.first).to eq 'example.com' |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
describe 'POST #create' do |
||||||
|
before do |
||||||
|
post :create, params: { domain: 'example.org' } |
||||||
|
end |
||||||
|
|
||||||
|
it 'returns http success' do |
||||||
|
expect(response).to have_http_status(:success) |
||||||
|
end |
||||||
|
|
||||||
|
it 'creates a domain block' do |
||||||
|
expect(user.account.domain_blocking?('example.org')).to be true |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
describe 'DELETE #destroy' do |
||||||
|
before do |
||||||
|
delete :destroy, params: { domain: 'example.com' } |
||||||
|
end |
||||||
|
|
||||||
|
it 'returns http success' do |
||||||
|
expect(response).to have_http_status(:success) |
||||||
|
end |
||||||
|
|
||||||
|
it 'deletes a domain block' do |
||||||
|
expect(user.account.domain_blocking?('example.com')).to be false |
||||||
|
end |
||||||
|
end |
||||||
|
end |
@ -0,0 +1,4 @@ |
|||||||
|
Fabricator(:account_domain_block) do |
||||||
|
account_id 1 |
||||||
|
domain "MyString" |
||||||
|
end |
@ -0,0 +1,5 @@ |
|||||||
|
require 'rails_helper' |
||||||
|
|
||||||
|
RSpec.describe AccountDomainBlock, type: :model do |
||||||
|
|
||||||
|
end |
Loading…
Reference in new issue