diff --git a/weasyl/search.py b/weasyl/search.py index fe4460a96..396620546 100644 --- a/weasyl/search.py +++ b/weasyl/search.py @@ -180,8 +180,9 @@ def _find_without_media(userid, rating, limit, elif within == "friend": # Search within friends content statement_from.append( - "INNER JOIN frienduser ON (frienduser.userid, frienduser.otherid) = (%(userid)s, content.userid)" - " OR (frienduser.userid, frienduser.otherid) = (content.userid, %(userid)s)") + "INNER JOIN frienduser ON ((frienduser.userid, frienduser.otherid) = (%(userid)s, content.userid)" + " OR (frienduser.userid, frienduser.otherid) = (content.userid, %(userid)s))" + " AND frienduser.settings !~ 'p'") elif within == "follow": # Search within following content statement_from.append( diff --git a/weasyl/test/test_search.py b/weasyl/test/test_search.py index 591e1bf20..54a23d386 100644 --- a/weasyl/test/test_search.py +++ b/weasyl/test/test_search.py @@ -179,3 +179,41 @@ def test_user_search_ordering(db): results = search.select_users("user") assert [user["title"] for user in results] == ["user_aa", "user_Ab", "user_ba", "user_Bb"] + + +def test_search_within_friends(db): + def _select(userid: int): + results, _, _ = search.select( + search=search.Query.parse("ferret", "submit"), + userid=userid, rating=ratings.GENERAL.code, limit=100, + cat=None, subcat=None, within="friend", backid=None, nextid=None, + ) + + return results + + config = CharSettings({}, {}, {}) + config_pending = CharSettings({"pending"}, {}, {}) + + user1_id = db_utils.create_user("user1", username="user1", config=config) + user2_id = db_utils.create_user("user2", username="user2", config=config) + + tag_id = db_utils.create_tag("ferret") + + submission1_id = db_utils.create_submission(user1_id, rating=ratings.GENERAL.code) + db_utils.create_submission_tag(tag_id, submission1_id) + + submission2_id = db_utils.create_submission(user2_id, rating=ratings.GENERAL.code) + db_utils.create_submission_tag(tag_id, submission2_id) + + assert len(_select(user1_id)) == 0 + assert len(_select(user2_id)) == 0 + + db_utils.create_friendship(user1_id, user2_id, config_pending) + + assert len(_select(user1_id)) == 0 + assert len(_select(user2_id)) == 0 + + db_utils.create_friendship(user2_id, user1_id, config) + + assert len(_select(user1_id)) == 1 + assert len(_select(user1_id)) == 1