API for blocking and unblocking

This commit is contained in:
Eugen Rochko 2016-10-03 18:17:06 +02:00
parent 2c9e672ee2
commit 7b9a4af311
10 changed files with 106 additions and 8 deletions

View file

@ -26,7 +26,7 @@ const StatusContent = React.createClass({
} else { } else {
link.setAttribute('target', '_blank'); link.setAttribute('target', '_blank');
link.setAttribute('rel', 'noopener'); link.setAttribute('rel', 'noopener');
link.addEventListener('click', this.onNormalClick); link.addEventListener('click', this.onNormalClick.bind(this));
} }
} }
}, },

View file

@ -40,11 +40,15 @@ const Mastodon = React.createClass({
if (typeof App !== 'undefined') { if (typeof App !== 'undefined') {
App.timeline = App.cable.subscriptions.create("TimelineChannel", { App.timeline = App.cable.subscriptions.create("TimelineChannel", {
connected: function() {}, connected () {
disconnected: function() {}, },
received: function(data) { disconnected () {
},
received (data) {
switch(data.type) { switch(data.type) {
case 'update': case 'update':
return store.dispatch(updateTimeline(data.timeline, JSON.parse(data.message))); return store.dispatch(updateTimeline(data.timeline, JSON.parse(data.message)));
@ -53,6 +57,8 @@ const Mastodon = React.createClass({
case 'merge': case 'merge':
case 'unmerge': case 'unmerge':
return store.dispatch(refreshTimeline('home')); return store.dispatch(refreshTimeline('home'));
case 'block':
return store.dispatch(refreshTimeline('mentions'));
} }
} }
}); });

View file

@ -24,13 +24,25 @@ class Api::V1::AccountsController < ApiController
end end
def follow def follow
@follow = FollowService.new.call(current_user.account, @account.acct) FollowService.new.call(current_user.account, @account.acct)
set_relationship
render action: :relationship
end
def block
BlockService.new.call(current_user.account, @account)
set_relationship set_relationship
render action: :relationship render action: :relationship
end end
def unfollow def unfollow
@unfollow = UnfollowService.new.call(current_user.account, @account) UnfollowService.new.call(current_user.account, @account)
set_relationship
render action: :relationship
end
def unblock
UnblockService.new.call(current_user.account, @account)
set_relationship set_relationship
render action: :relationship render action: :relationship
end end

View file

@ -0,0 +1,25 @@
class BlockService < BaseService
def call(account, target_account)
return if account.id == target_account.id
UnfollowService.new.call(account, target_account) if account.following?(target_account)
account.block!(target_account)
clear_mentions(account, target_account)
end
private
def clear_mentions(account, target_account)
timeline_key = FeedManager.instance.key(:mentions, account.id)
target_account.statuses.select('id').find_each do |status|
redis.zrem(timeline_key, status.id)
end
FeedManager.instance.broadcast(account.id, type: 'block', id: target_account.id)
end
def redis
$redis
end
end

View file

@ -0,0 +1,5 @@
class UnblockService < BaseService
def call(account, target_account)
account.unblock!(target_account) if account.blocking?(target_account)
end
end

View file

@ -13,7 +13,7 @@ class UnfollowService < BaseService
def unmerge_from_timeline(from_account, into_account) def unmerge_from_timeline(from_account, into_account)
timeline_key = FeedManager.instance.key(:home, into_account.id) timeline_key = FeedManager.instance.key(:home, into_account.id)
from_account.statuses.find_each do |status| from_account.statuses.select('id').find_each do |status|
redis.zrem(timeline_key, status.id) redis.zrem(timeline_key, status.id)
end end

View file

@ -74,6 +74,8 @@ Rails.application.routes.draw do
post :follow post :follow
post :unfollow post :unfollow
post :block
post :unblock
end end
end end
end end

View file

@ -79,6 +79,44 @@ RSpec.describe Api::V1::AccountsController, type: :controller do
end end
end end
describe 'POST #block' do
let(:other_account) { Fabricate(:user, email: 'bob@example.com', account: Fabricate(:account, username: 'bob')).account }
before do
user.account.follow!(other_account)
post :block, params: { id: other_account.id }
end
it 'returns http success' do
expect(response).to have_http_status(:success)
end
it 'removes the following relation between user and target user' do
expect(user.account.following?(other_account)).to be false
end
it 'creates a blocking relation' do
expect(user.account.blocking?(other_account)).to be true
end
end
describe 'POST #unblock' do
let(:other_account) { Fabricate(:user, email: 'bob@example.com', account: Fabricate(:account, username: 'bob')).account }
before do
user.account.block!(other_account)
post :unblock, params: { id: other_account.id }
end
it 'returns http success' do
expect(response).to have_http_status(:success)
end
it 'removes the blocking relation between user and target user' do
expect(user.account.blocking?(other_account)).to be false
end
end
describe 'GET #relationships' do describe 'GET #relationships' do
let(:simon) { Fabricate(:user, email: 'simon@example.com', account: Fabricate(:account, username: 'simon')).account } let(:simon) { Fabricate(:user, email: 'simon@example.com', account: Fabricate(:account, username: 'simon')).account }
let(:lewis) { Fabricate(:user, email: 'lewis@example.com', account: Fabricate(:account, username: 'lewis')).account } let(:lewis) { Fabricate(:user, email: 'lewis@example.com', account: Fabricate(:account, username: 'lewis')).account }

View file

@ -0,0 +1,5 @@
require 'rails_helper'
RSpec.describe BlockService do
subject { BlockService.new }
end

View file

@ -0,0 +1,5 @@
require 'rails_helper'
RSpec.describe UnblockService do
subject { UnblockService.new }
end