From 0bf09010d1eab30354ccf1e7b1d911e8cf8e2c52 Mon Sep 17 00:00:00 2001 From: Deimos Date: Thu, 5 Sep 2019 14:21:12 -0600 Subject: [PATCH] On home page, only include subgroups if subscribed Previously, subgroups were always included in topic queries. This meant that, for example, it was impossible to subscribe to ~tildes without always also having ~tildes.official topics in your home page as well. This change makes it so that the home page now only includes topics from groups that the user is actually subscribed to. When visiting a group individually it still includes the subgroups. As part of deploying this, I'll want to automatically add subscriptions for users that were subscribed to ~tildes but not ~tildes.official so they don't see a behavior change (and that's the only current subgroup). --- tildes/tildes/models/topic/topic_query.py | 17 +++++++++++------ tildes/tildes/views/topic.py | 6 ++++-- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/tildes/tildes/models/topic/topic_query.py b/tildes/tildes/models/topic/topic_query.py index d2f2f60..b3dc293 100644 --- a/tildes/tildes/models/topic/topic_query.py +++ b/tildes/tildes/models/topic/topic_query.py @@ -137,14 +137,19 @@ class TopicQuery(PaginatedQuery): return self - def inside_groups(self, groups: Sequence[Group]) -> "TopicQuery": + def inside_groups( + self, groups: Sequence[Group], include_subgroups: bool = True + ) -> "TopicQuery": """Restrict the topics to inside specific groups (generative).""" - query_paths = [group.path for group in groups] - subgroup_subquery = self.request.db_session.query(Group.group_id).filter( - Group.path.descendant_of(query_paths) - ) + if include_subgroups: + query_paths = [group.path for group in groups] + group_ids = self.request.db_session.query(Group.group_id).filter( + Group.path.descendant_of(query_paths) + ) + else: + group_ids = [group.group_id for group in groups] - return self.filter(Topic.group_id.in_(subgroup_subquery)) # type: ignore + return self.filter(Topic.group_id.in_(group_ids)) # type: ignore def inside_time_period(self, period: SimpleHoursPeriod) -> "TopicQuery": """Restrict the topics to inside a time period (generative).""" diff --git a/tildes/tildes/views/topic.py b/tildes/tildes/views/topic.py index d41c6fe..b6b96a6 100644 --- a/tildes/tildes/views/topic.py +++ b/tildes/tildes/views/topic.py @@ -146,7 +146,9 @@ def get_group_topics( ) -> dict: """Get a listing of topics in the group.""" # pylint: disable=too-many-arguments, too-many-branches, too-many-locals - if request.matched_route.name == "home": + is_home_page = request.matched_route.name == "home" + + if is_home_page: # on the home page, include topics from the user's subscribed groups # (or all groups, if logged-out) if request.user: @@ -171,7 +173,7 @@ def get_group_topics( query = ( request.query(Topic) .join_all_relationships() - .inside_groups(groups) + .inside_groups(groups, include_subgroups=not is_home_page) .apply_sort_option(order) )