Skip to content

Conversation

@cxyfer
Copy link
Owner

@cxyfer cxyfer commented Jul 4, 2025

Summary

  • Added new /recent slash command to view any LeetCode user's recent accepted submissions
  • Implemented paginated display showing one problem per page with navigation controls
  • Added lazy loading to improve performance by fetching problem details only when needed

Features

  • Paginated Interface: Display submissions one at a time with previous/next navigation buttons
  • Lazy Loading: Fetch problem details only when viewing specific pages, reducing initial load time
  • Submission Caching: 5-minute cache to avoid re-fetching when navigating between pages
  • Clean UI: Navigation buttons at edges without text labels, English timestamps and page indicators
  • Flexible Limit: Support up to 50 recent submissions with customizable limit parameter

Changes

  • Modified leetcode.py:
    • Made fetch_recent_ac_submissions async with aiohttp
    • Return basic submission info without fetching full problem details
  • Updated cogs/slash_commands_cog.py:
    • Added /recent command with username and limit parameters
    • Implemented helper methods for creating embeds and views
    • Added _get_submission_details for lazy loading problem information
  • Enhanced cogs/interaction_handler_cog.py:
    • Added navigation button handlers for previous/next actions
    • Implemented submission caching with TTL
    • Support lazy loading when switching pages

Test Plan

  • Test /recent command with valid username
  • Test navigation between multiple submissions
  • Verify lazy loading works correctly
  • Check cache expiration after 5 minutes
  • Test with different submission limits
  • Verify error handling for invalid usernames

🤖 Generated with Claude Code

- New slash command to display recent accepted submissions for any LeetCode user
- Paginated interface showing one problem per page with navigation buttons
- Lazy loading implementation to fetch problem details only when needed
- 5-minute cache for submissions to improve performance
- Navigation buttons placed at edges without text labels for clean UI
- English language for timestamps and page indicators
- Supports up to 50 recent submissions with customizable limit

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@cxyfer cxyfer requested a review from Copilot July 4, 2025 16:32
Copilot

This comment was marked as outdated.

cxyfer and others added 2 commits July 5, 2025 00:35
- Wrap tags with || spoiler markers to hide potential hints
- Consistent with tag display in other commands

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
- Fix typo in daily_cn command description (remove trailing '1')
- Move time import to module level to avoid repeated imports
- Store original limit in cache to maintain consistent pagination when re-fetching
- Update docstring to accurately reflect basic submission info return
- Remove test_recent function from production code

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@cxyfer cxyfer requested a review from Copilot July 4, 2025 16:39
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds a new /recent slash command to fetch and display a LeetCode user's recent accepted submissions with pagination, lazy loading of problem details, and a short-term cache.

  • Converted fetch_recent_ac_submissions to async and return only basic info for lazy loading.
  • Introduced /recent command in SlashCommandsCog with paginated embeds and caching.
  • Added navigation handlers and a TTL-based submissions cache in InteractionHandlerCog.

Reviewed Changes

Copilot reviewed 3 out of 13 changed files in this pull request and generated 2 comments.

File Description
leetcode.py Made fetch_recent_ac_submissions async, added domain check, and built basic submission info
cogs/slash_commands_cog.py Added /recent command, helper methods for embeds/views, and caching logic
cogs/interaction_handler_cog.py Implemented prev/next button handlers, cache lookup/refresh, and page navigation
Comments suppressed due to low confidence (4)

leetcode.py:621

  • The method now returns early for non-.com domains but this limitation is not documented in the docstring; consider updating the docstring to mention domain restriction.
        if self.domain != "com":

cogs/slash_commands_cog.py:345

  • New pagination and caching behaviors were added but there are no corresponding tests; consider adding unit tests for the /recent command, page navigation, and cache expiration logic.
    @app_commands.command(name="recent", description="查看 LeetCode 使用者的近期解題紀錄 (僅限 LCUS)")

cogs/slash_commands_cog.py:392

  • self.logger is used here but there's no evidence that self.logger is initialized in this cog; you may need to define it in __init__ or use self.bot.logger to avoid an AttributeError.
            self.logger.info(f"Sent user submissions for {username} to {interaction.user.name}")

cogs/slash_commands_cog.py:472

  • [nitpick] The constant LEETCODE_DISCRIPTION_BUTTON_PREFIX appears to have a typo ('DISCRIPTION' vs 'DESCRIPTION'); consider renaming for clarity.
            custom_id=f"{self.bot.LEETCODE_DISCRIPTION_BUTTON_PREFIX}{submission['id']}_com",

Comment on lines +267 to +283
cached_data = self.submissions_cache.get(cache_key)

# Check if cache is valid (5 minutes)
if cached_data and (time.time() - cached_data[1]) < 300:
submissions = cached_data[0]
else:
# Fetch submissions again
await interaction.response.defer(ephemeral=True)
# Use the original limit if available, otherwise default to 50
original_limit = cached_data[2] if cached_data and len(cached_data) > 2 else 50
submissions = await self.bot.lcus.fetch_recent_ac_submissions(username, original_limit)
if not submissions:
await interaction.followup.send(f"找不到使用者 **{username}** 的解題紀錄。", ephemeral=True)
return
# Update cache with limit
self.submissions_cache[cache_key] = (submissions, time.time(), original_limit)

Copy link

Copilot AI Jul 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Accessing cached_data by index makes the code harder to read; consider unpacking the tuple as (submissions, timestamp, original_limit) = cached_data for clarity.

Suggested change
cached_data = self.submissions_cache.get(cache_key)
# Check if cache is valid (5 minutes)
if cached_data and (time.time() - cached_data[1]) < 300:
submissions = cached_data[0]
else:
# Fetch submissions again
await interaction.response.defer(ephemeral=True)
# Use the original limit if available, otherwise default to 50
original_limit = cached_data[2] if cached_data and len(cached_data) > 2 else 50
submissions = await self.bot.lcus.fetch_recent_ac_submissions(username, original_limit)
if not submissions:
await interaction.followup.send(f"找不到使用者 **{username}** 的解題紀錄。", ephemeral=True)
return
# Update cache with limit
self.submissions_cache[cache_key] = (submissions, time.time(), original_limit)
cached_data = self.submissions_cache.get(cache_key)
# Unpack cached data
if cached_data:
submissions, timestamp, original_limit = cached_data
else:
submissions, timestamp, original_limit = None, None, 50
# Check if cache is valid (5 minutes)
if submissions and (time.time() - timestamp) < 300:
pass # Use cached submissions
else:
# Fetch submissions again
await interaction.response.defer(ephemeral=True)
submissions = await self.bot.lcus.fetch_recent_ac_submissions(username, original_limit)
if not submissions:
await interaction.followup.send(f"找不到使用者 **{username}** 的解題紀錄。", ephemeral=True)
return
# Update cache with limit
self.submissions_cache[cache_key] = (submissions, time.time(), original_limit)

Copilot uses AI. Check for mistakes.
tags_str = ", ".join([f"||`{tag}`||" for tag in submission['tags'][:5]]) # Limit tags to avoid too long
embed.add_field(name="🏷️ Tags", value=tags_str, inline=False)

embed.set_author(name=f"{username}'s Recent Submissions", icon_url="https://leetcode.com/static/images/LeetCode_logo.png")
Copy link

Copilot AI Jul 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The author name is in English while command messages are in Chinese; consider localizing all UI strings consistently based on user language.

Suggested change
embed.set_author(name=f"{username}'s Recent Submissions", icon_url="https://leetcode.com/static/images/LeetCode_logo.png")
localized_author_name = self._translate("Recent Submissions", username)
embed.set_author(name=localized_author_name, icon_url="https://leetcode.com/static/images/LeetCode_logo.png")

Copilot uses AI. Check for mistakes.
@cxyfer cxyfer merged commit d68b394 into main Jul 4, 2025
@cxyfer cxyfer deleted the feat branch July 4, 2025 16:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant