Skip to content

Commit

Permalink
Wip
Browse files Browse the repository at this point in the history
  • Loading branch information
kmycode committed Feb 21, 2024
1 parent 07440ff commit 8aefddd
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 0 deletions.
2 changes: 2 additions & 0 deletions app/lib/activitypub/activity/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,8 @@ def process_poll
def poll_vote?
return false if replied_to_status.nil? || replied_to_status.preloadable_poll.nil? || !replied_to_status.local? || !replied_to_status.preloadable_poll.options.include?(@object['name'])

return true unless check_invalid_reaction_for_ng_rule! @account, uri: @json['id'], reaction_type: 'vote', recipient: replied_to_status.account

poll_vote! unless replied_to_status.preloadable_poll.expired?

true
Expand Down
2 changes: 2 additions & 0 deletions app/lib/activitypub/activity/follow.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
class ActivityPub::Activity::Follow < ActivityPub::Activity
include Payloadable
include FollowHelper
include NgRuleHelper

def perform
return request_follow_for_friend if friend_follow?

target_account = account_from_uri(object_uri)

return if target_account.nil? || !target_account.local? || delete_arrived_first?(@json['id'])
return unless check_invalid_reaction_for_ng_rule! @account, uri: @json['id'], reaction_type: 'follow', recipient: target_account

# Update id of already-existing follow requests
existing_follow_request = ::FollowRequest.find_by(account: @account, target_account: target_account) || PendingFollowRequest.find_by(account: @account, target_account: target_account)
Expand Down
4 changes: 4 additions & 0 deletions app/lib/activitypub/activity/like.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class ActivityPub::Activity::Like < ActivityPub::Activity
include Redisable
include Lockable
include JsonLdHelper
include NgRuleHelper

def perform
@original_status = status_from_uri(object_uri)
Expand All @@ -25,6 +26,7 @@ def reject_favourite?

def process_favourite
return if @account.favourited?(@original_status)
return unless check_invalid_reaction_for_ng_rule! @account, uri: @json['id'], reaction_type: 'favourite', recipient: @original_status.account

favourite = @original_status.favourites.create!(account: @account, uri: @json['id'])

Expand All @@ -43,6 +45,8 @@ def process_emoji_reaction
return if emoji.nil?
end

return unless check_invalid_reaction_for_ng_rule! @account, uri: @json['id'], reaction_type: 'emoji_reaction', emoji_reaction_name: emoji&.shortcode || shortcode, emoji_reaction_origin_domain: emoji&.domain, recipient: @original_status.account

reaction = nil

with_redis_lock("emoji_reaction:#{@original_status.id}") do
Expand Down
26 changes: 26 additions & 0 deletions spec/lib/activitypub/activity/create_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1560,6 +1560,32 @@ def activity_for_object(json)
expect(vote.uri).to eq object_json[:id]
expect(poll.reload.cached_tallies).to eq [1, 0]
end

context 'when ng rule is existing' do
let(:custom_before) { true }

context 'when ng rule is match' do
before do
Fabricate(:ng_rule, account_domain: 'example.com', reaction_action: :reject, reaction_type: ['vote'])
subject.perform
end

it 'does not create a reblog by sender of status' do
expect(poll.votes.first).to be_nil
end
end

context 'when ng rule is not match' do
before do
Fabricate(:ng_rule, account_domain: 'foo.bar', reaction_action: :reject, reaction_type: ['vote'])
subject.perform
end

it 'creates a reblog by sender of status' do
expect(poll.votes.first).to_not be_nil
end
end
end
end

context 'when a vote to an expired local poll' do
Expand Down
27 changes: 27 additions & 0 deletions spec/lib/activitypub/activity/follow_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,33 @@
expect(sender.requested?(recipient)).to be false
end
end

context 'when ng rule is existing' do
context 'when ng rule is match' do
before do
Fabricate(:ng_rule, account_domain: 'example.com', reaction_action: :reject, reaction_type: ['follow'])
stub_request(:post, 'https://example.com/inbox').to_return(status: 200, body: '', headers: {})
subject.perform
end

it 'does not create a reblog by sender of status' do
expect(sender.following?(recipient)).to be false
expect(sender.requested?(recipient)).to be false
end
end

context 'when ng rule is not match' do
before do
Fabricate(:ng_rule, account_domain: 'foo.bar', reaction_action: :reject, reaction_type: ['follow'])
subject.perform
end

it 'creates a reblog by sender of status' do
expect(sender.following?(recipient)).to be true
expect(sender.requested?(recipient)).to be false
end
end
end
end

context 'when a follow relationship already exists' do
Expand Down
50 changes: 50 additions & 0 deletions spec/lib/activitypub/activity/like_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,32 @@
end
end

context 'when ng rule is existing' do
subject { described_class.new(json, sender) }

context 'when ng rule is match' do
before do
Fabricate(:ng_rule, account_domain: 'example.com', reaction_action: :reject, reaction_type: ['favourite'])
subject.perform
end

it 'does not create a reblog by sender of status' do
expect(sender.favourited?(status)).to be false
end
end

context 'when ng rule is not match' do
before do
Fabricate(:ng_rule, account_domain: 'foo.bar', reaction_action: :reject, reaction_type: ['favourite'])
subject.perform
end

it 'creates a reblog by sender of status' do
expect(sender.favourited?(status)).to be true
end
end
end

describe '#perform when receive emoji reaction' do
subject do
described_class.new(json, sender).perform
Expand Down Expand Up @@ -592,6 +618,30 @@
end
end
end

context 'when ng rule is existing' do
let(:content) { '😀' }

context 'when ng rule is match' do
before do
Fabricate(:ng_rule, account_domain: 'example.com', reaction_action: :reject, reaction_type: ['emoji_reaction'])
end

it 'does not create a reblog by sender of status' do
expect(subject.count).to eq 0
end
end

context 'when ng rule is not match' do
before do
Fabricate(:ng_rule, account_domain: 'foo.bar', reaction_action: :reject, reaction_type: ['emoji_reaction'])
end

it 'creates a reblog by sender of status' do
expect(subject.count).to eq 1
end
end
end
end

describe '#perform when rejecting favourite domain block' do
Expand Down

0 comments on commit 8aefddd

Please sign in to comment.