Skip to content

Commit

Permalink
Adding default blocklist support for anonymous users
Browse files Browse the repository at this point in the history
  • Loading branch information
Soblow authored and Lugrim committed Nov 5, 2023
1 parent c1f0532 commit d97ada8
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 3 deletions.
3 changes: 3 additions & 0 deletions server/config.yaml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ default_rank: regular
# default blocklisted tags (space separated)
default_tag_blocklist: ''

# Apply blocklist for anonymous viewers too
default_tag_blocklist_for_anonymous: yes

privileges:
'users:create:self': anonymous # Registration permission
'users:create:any': administrator
Expand Down
1 change: 1 addition & 0 deletions server/szurubooru/api/info_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def get_info(ctx: rest.Context, _params: Dict[str, str] = {}) -> rest.Response:
"tagCategoryNameRegex": config.config["tag_category_name_regex"],
"defaultUserRank": config.config["default_rank"],
"defaultTagBlocklist": config.config["default_tag_blocklist"],
"defaultTagBlocklistForAnonymous": config.config["default_tag_blocklist_for_anonymous"],
"enableSafety": config.config["enable_safety"],
"contactEmail": config.config["contact_email"],
"canSendMails": bool(config.config["smtp"]["host"]),
Expand Down
14 changes: 11 additions & 3 deletions server/szurubooru/search/configs/post_search_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import sqlalchemy as sa

from szurubooru import db, errors, model
from szurubooru import config, db, errors, model
from szurubooru.func import util
from szurubooru.search import criteria, tokens, parser
from szurubooru.search.configs import util as search_util
Expand Down Expand Up @@ -178,9 +178,17 @@ def on_search_query_parsed(self, search_query: SearchQuery) -> SaQuery:
new_special_tokens.append(token)
search_query.special_tokens = new_special_tokens

if self.user and self.user.blocklist:
blocklist_to_use = ""

if self.user:
if self.user.blocklist:
blocklist_to_use = self.user.blocklist
elif (self.user.rank == model.User.RANK_ANONYMOUS) and config.config["default_tag_blocklist_for_anonymous"]:
blocklist_to_use = config.config["default_tag_blocklist"]

if len(blocklist_to_use) > 0:
# TODO Sort an already parsed and checked version instead?
blocklist_query = parser.Parser().parse(self.user.blocklist)
blocklist_query = parser.Parser().parse(blocklist_to_use)
search_query_orig_list = [e.criterion.original_text for e in search_query.anonymous_tokens]
for t in blocklist_query.anonymous_tokens:
if t.criterion.original_text in search_query_orig_list:
Expand Down
2 changes: 2 additions & 0 deletions server/szurubooru/tests/api/test_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def test_info_api(
"tag_category_name_regex": "4",
"default_rank": "5",
"default_tag_blocklist": "testTag",
"default_tag_blocklist_for_anonymous": True,
"privileges": {
"test_key1": "test_value1",
"test_key2": "test_value2",
Expand All @@ -50,6 +51,7 @@ def test_info_api(
"tagCategoryNameRegex": "4",
"defaultUserRank": "5",
"defaultTagBlocklist": "testTag",
"defaultTagBlocklistForAnonymous": True,
"privileges": {
"testKey1": "test_value1",
"testKey2": "test_value2",
Expand Down
69 changes: 69 additions & 0 deletions server/szurubooru/tests/api/test_post_blocklist.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,72 @@ def test_blocklist(user_factory, post_factory, context_factory, config_injector,
"results": ["serialized post 15", "serialized post 14", "serialized post 13"],
}


def test_blocklist_no_anonymous(user_factory, post_factory, context_factory, config_injector, tag_factory):
tag1 = tag_factory(names=['tag1'])
post1 = post_factory(id=21, tags=[tag1])
post2 = post_factory(id=22, tags=[tag1])
post3 = post_factory(id=23)
config_injector({
"default_tag_blocklist": "tag1",
"default_tag_blocklist_for_anonymous": False,
"privileges": {
"posts:list": model.User.RANK_ANONYMOUS,
}
})
db.session.add_all([post1, post2, post3])
db.session.flush()
with patch("szurubooru.func.posts.serialize_post"):
posts.serialize_post.side_effect = (
lambda post, *_args, **_kwargs: "serialized post %d" % post.post_id
)
result = api.post_api.get_posts(
context_factory(
params={"query": "", "offset": 0},
user=user_factory(rank=model.User.RANK_ANONYMOUS),
)
)
assert result == {
"query": "",
"offset": 0,
"limit": 100,
"total": 3,
"results": ["serialized post 23", "serialized post 22", "serialized post 21"],
}


def test_blocklist_anonymous(user_factory, post_factory, context_factory, config_injector, tag_factory):
tag1 = tag_factory(names=['tag1'])
tag2 = tag_factory(names=['tag2'])
tag3 = tag_factory(names=['tag3'])
post1 = post_factory(id=31, tags=[tag1, tag2])
post2 = post_factory(id=32, tags=[tag1])
post3 = post_factory(id=33, tags=[tag2])
post4 = post_factory(id=34, tags=[tag3])
post5 = post_factory(id=35)
config_injector({
"default_tag_blocklist": "tag3",
"default_tag_blocklist_for_anonymous": True,
"privileges": {
"posts:list": model.User.RANK_ANONYMOUS,
}
})
db.session.add_all([post1, post2, post3, post4, post5])
db.session.flush()
with patch("szurubooru.func.posts.serialize_post"):
posts.serialize_post.side_effect = (
lambda post, *_args, **_kwargs: "serialized post %d" % post.post_id
)
result = api.post_api.get_posts(
context_factory(
params={"query": "", "offset": 0},
user=user_factory(rank=model.User.RANK_ANONYMOUS),
)
)
assert result == {
"query": "",
"offset": 0,
"limit": 100,
"total": 4,
"results": ["serialized post 35", "serialized post 33", "serialized post 32", "serialized post 31"],
}

0 comments on commit d97ada8

Please sign in to comment.