in chat-client/main.py [0:0]
def openInitialDialog(request_json):
"""
Opens the initial subscription dialog. If a category tag (e.g., "All App Mod Products")
is present in the saved subscriptions (which also includes individual members),
only the category tag itself is marked selected in the UI, suppressing the selection
of the individual members of that category (even if they are in the saved list).
Other explicitly saved items remain selected.
Handles overrides ("All Products", "All Blogs").
"""
try:
# Default empty sets
products_subscribed_set = set()
categories_subscribed_set = set()
doc_exists = False
# Fetch subscriptions
space_name = request_json["chat"]["appCommandPayload"]["space"]["name"].replace(
"/", "_"
)
subscriptions_ref = DB.collection("product_space_subscriptions")
product_doc_ref = subscriptions_ref.document(space_name)
products_doc = product_doc_ref.get()
doc_exists = products_doc.exists
if doc_exists:
doc_data = products_doc.to_dict()
products_subscribed_set = set(doc_data.get("products_subscribed", []))
categories_subscribed_set = set(doc_data.get("categories_subscribed", []))
# --- Determine Overrides ---
all_products_override = "All Products" in products_subscribed_set
all_blogs_override = "All Blogs" in categories_subscribed_set
# --- Generate Product Dialog Items ---
notes = []
all_possible_products = getattr(client_utils, "google_cloud_products", [])
if all_products_override:
# Handle global override - only "All Products" is selected
for product in all_possible_products:
is_selected = product == "All Products"
notes.append(
{"text": product, "value": product, "selected": is_selected}
)
elif doc_exists:
# Identify active category tags and the individual products they cover
active_product_tags = {
tag for tag in CATEGORY_MAP if tag in products_subscribed_set
}
products_covered_by_active_tags = set()
if active_product_tags:
for tag in active_product_tags:
# Get members ONLY (exclude the tag)
products_covered_by_active_tags.update(
get_members_only(tag, CATEGORY_MAP)
)
# Determine selection state for each possible product
for product in all_possible_products:
is_selected = False # Default
# Rule 1: Is it an active category tag?
if product in active_product_tags:
is_selected = True
# Rule 2: Is it in the subscribed list AND NOT covered by an active tag?
elif (
product in products_subscribed_set
and product not in products_covered_by_active_tags
):
is_selected = True
notes.append(
{"text": product, "value": product, "selected": is_selected}
)
else:
# No document exists and no override, nothing is selected
for product in all_possible_products:
notes.append({"text": product, "value": product, "selected": False})
# --- Generate Blog Dialog Items (Similar Logic) ---
blogs = []
all_possible_categories = getattr(client_utils, "categories", [])
if all_blogs_override:
# Handle global override - only "All Blogs" is selected
for category in all_possible_categories:
is_selected = category == "All Blogs"
blogs.append(
{"text": category, "value": category, "selected": is_selected}
)
elif doc_exists:
# Identify active category tags and the individual blogs they cover
active_blog_tags = {
tag for tag in BLOG_CATEGORY_MAP if tag in categories_subscribed_set
}
blogs_covered_by_active_tags = set()
if active_blog_tags:
for tag in active_blog_tags:
# Get members ONLY (exclude the tag)
blogs_covered_by_active_tags.update(
get_members_only(tag, BLOG_CATEGORY_MAP)
)
# Determine selection state for each possible category
for category in all_possible_categories:
is_selected = False # Default
# Rule 1: Is it an active category tag?
if category in active_blog_tags:
is_selected = True
# Rule 2: Is it in the subscribed list AND NOT covered by an active tag?
elif (
category in categories_subscribed_set
and category not in blogs_covered_by_active_tags
):
is_selected = True
blogs.append(
{"text": category, "value": category, "selected": is_selected}
)
else:
# No document exists and no override, nothing is selected
for category in all_possible_categories:
blogs.append({"text": category, "value": category, "selected": False})
return client_utils.retrieve_dialog_response(notes, blogs)
except Exception as e:
# Add proper error handling/logging
space_name_for_error = "unknown"
try:
# Attempt to get space name for logging, but don't fail if request structure is unexpected
space_name_for_error = (
request_json.get("chat", {})
.get("appCommandPayload", {})
.get("space", {})
.get("name", "unknown")
.replace("/", "_")
)
except Exception:
pass # Ignore errors just trying to get the name for logging
print(f"Error opening initial dialog for space {space_name_for_error}: {e}")
# Return an error response or a default dialog
notes = [
{"text": p, "value": p, "selected": False}
for p in getattr(client_utils, "google_cloud_products", [])
]
blogs = [
{"text": c, "value": c, "selected": False}
for c in getattr(client_utils, "categories", [])
]
return client_utils.retrieve_dialog_response(
notes, blogs, error="Failed to load subscriptions."
)