Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
La Quadrature du Net
mamot
Commits
22a8801d
Commit
22a8801d
authored
Oct 09, 2016
by
Eugen Rochko
Browse files
Adding domain blocks
parent
52d7f862
Changes
13
Hide whitespace changes
Inline
Side-by-side
app/controllers/api/subscriptions_controller.rb
View file @
22a8801d
...
...
@@ -13,8 +13,9 @@ class Api::SubscriptionsController < ApiController
def
update
body
=
request
.
body
.
read
subscription
=
@account
.
subscription
(
api_subscription_url
(
@account
.
id
))
if
@account
.
subscription
(
api_subscription_url
(
@account
.
id
))
.
verify
(
body
,
request
.
headers
[
'HTTP_X_HUB_SIGNATURE'
])
if
subscription
.
verify
(
body
,
request
.
headers
[
'HTTP_X_HUB_SIGNATURE'
])
ProcessFeedService
.
new
.
call
(
body
,
@account
)
head
201
else
...
...
app/models/account.rb
View file @
22a8801d
...
...
@@ -24,10 +24,10 @@ class Account < ApplicationRecord
validates
:note
,
length:
{
maximum:
124
},
if:
'local?'
# Timelines
has_many
:stream_entries
,
inverse_of: :account
has_many
:statuses
,
inverse_of: :account
has_many
:favourites
,
inverse_of: :account
has_many
:mentions
,
inverse_of: :account
has_many
:stream_entries
,
inverse_of: :account
,
dependent: :destroy
has_many
:statuses
,
inverse_of: :account
,
dependent: :destroy
has_many
:favourites
,
inverse_of: :account
,
dependent: :destroy
has_many
:mentions
,
inverse_of: :account
,
dependent: :destroy
# Follow relations
has_many
:active_relationships
,
class_name:
'Follow'
,
foreign_key:
'account_id'
,
dependent: :destroy
...
...
app/models/domain_block.rb
0 → 100644
View file @
22a8801d
class
DomainBlock
<
ApplicationRecord
validates
:domain
,
presence:
true
,
uniqueness:
true
def
self
.
blocked?
(
domain
)
where
(
domain:
domain
).
exists?
end
end
app/services/block_domain_service.rb
0 → 100644
View file @
22a8801d
class
BlockDomainService
<
BaseService
def
call
(
domain
)
block
=
DomainBlock
.
find_or_create_by!
(
domain:
domain
)
Account
.
where
(
domain:
domain
).
find_each
do
|
account
|
if
account
.
subscribed?
account
.
subscription
(
''
).
unsubscribe
end
account
.
destroy!
end
end
end
app/services/follow_remote_account_service.rb
View file @
22a8801d
...
...
@@ -8,6 +8,7 @@ class FollowRemoteAccountService < BaseService
username
,
domain
=
uri
.
split
(
'@'
)
return
Account
.
find_local
(
username
)
if
TagManager
.
instance
.
local_domain?
(
domain
)
return
nil
if
DomainBlock
.
blocked?
(
domain
)
account
=
Account
.
find_remote
(
username
,
domain
)
...
...
app/services/process_interaction_service.rb
View file @
22a8801d
...
...
@@ -13,6 +13,8 @@ class ProcessInteractionService < BaseService
domain
=
Addressable
::
URI
.
parse
(
url
).
host
account
=
Account
.
find_by
(
username:
username
,
domain:
domain
)
return
if
DomainBlock
.
blocked?
(
domain
)
if
account
.
nil?
account
=
follow_remote_account_service
.
call
(
"
#{
username
}
@
#{
domain
}
"
)
end
...
...
db/migrate/20161009120834_create_domain_blocks.rb
0 → 100644
View file @
22a8801d
class
CreateDomainBlocks
<
ActiveRecord
::
Migration
[
5.0
]
def
change
create_table
:domain_blocks
do
|
t
|
t
.
string
:domain
,
null:
false
,
default:
''
t
.
timestamps
end
add_index
:domain_blocks
,
:domain
,
unique:
true
end
end
db/schema.rb
View file @
22a8801d
...
...
@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord
::
Schema
.
define
(
version:
2016100
6213403
)
do
ActiveRecord
::
Schema
.
define
(
version:
2016100
9120834
)
do
# These are extensions that must be enabled in order to support this database
enable_extension
"plpgsql"
...
...
@@ -51,6 +51,13 @@ ActiveRecord::Schema.define(version: 20161006213403) do
t
.
index
[
"account_id"
,
"target_account_id"
],
name:
"index_blocks_on_account_id_and_target_account_id"
,
unique:
true
,
using: :btree
end
create_table
"domain_blocks"
,
force: :cascade
do
|
t
|
t
.
string
"domain"
,
default:
""
,
null:
false
t
.
datetime
"created_at"
,
null:
false
t
.
datetime
"updated_at"
,
null:
false
t
.
index
[
"domain"
],
name:
"index_domain_blocks_on_domain"
,
unique:
true
,
using: :btree
end
create_table
"favourites"
,
force: :cascade
do
|
t
|
t
.
integer
"account_id"
,
null:
false
t
.
integer
"status_id"
,
null:
false
...
...
spec/fabricators/domain_block_fabricator.rb
0 → 100644
View file @
22a8801d
Fabricator
(
:domain_block
)
do
domain
"MyString"
end
spec/fabricators/media_attachment_fabricator.rb
View file @
22a8801d
Fabricator
(
:media_attachment
)
do
status_id
1
file
""
remote_url
"MyString"
account_id
1
end
spec/models/domain_block_spec.rb
0 → 100644
View file @
22a8801d
require
'rails_helper'
RSpec
.
describe
DomainBlock
,
type: :model
do
pending
"add some examples to (or delete)
#{
__FILE__
}
"
end
spec/rails_helper.rb
View file @
22a8801d
...
...
@@ -30,3 +30,7 @@ end
def
request_fixture
(
name
)
File
.
read
(
File
.
join
(
Rails
.
root
,
'spec'
,
'fixtures'
,
'requests'
,
name
))
end
def
attachment_fixture
(
name
)
File
.
open
(
File
.
join
(
Rails
.
root
,
'spec'
,
'fixtures'
,
'files'
,
name
))
end
spec/services/block_domain_service_spec.rb
0 → 100644
View file @
22a8801d
require
'rails_helper'
RSpec
.
describe
BlockDomainService
do
let
(
:bad_account
)
{
Fabricate
(
:account
,
username:
'badguy666'
,
domain:
'evil.org'
)
}
let
(
:bad_status1
)
{
Fabricate
(
:status
,
account:
bad_account
,
text:
'You suck'
)
}
let
(
:bad_status2
)
{
Fabricate
(
:status
,
account:
bad_account
,
text:
'Hahaha'
)
}
let
(
:bad_attachment
)
{
Fabricate
(
:media_attachment
,
account:
bad_account
,
status:
bad_status2
,
file:
attachment_fixture
(
'attachment.jpg'
))
}
subject
{
BlockDomainService
.
new
}
before
do
bad_account
bad_status1
bad_status2
bad_attachment
subject
.
call
(
'evil.org'
)
end
it
'creates a domain block'
do
expect
(
DomainBlock
.
blocked?
(
'evil.org'
)).
to
be
true
end
it
'removes remote accounts from that domain'
do
expect
(
Account
.
find_remote
(
'badguy666'
,
'evil.org'
)).
to
be_nil
end
it
'removes the remote accounts\'s statuses and media attachments'
do
expect
{
bad_status1
.
reload
}.
to
raise_exception
ActiveRecord
::
RecordNotFound
expect
{
bad_status2
.
reload
}.
to
raise_exception
ActiveRecord
::
RecordNotFound
expect
{
bad_attachment
.
reload
}.
to
raise_exception
ActiveRecord
::
RecordNotFound
end
end
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment