Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added post delete mutation for posts in the admin-x-activitypub app #22330

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

mike182uk
Copy link
Member

refs AP-806

Added post delete mutation for posts in the admin-x-activitypub app

Copy link
Contributor

coderabbitai bot commented Mar 3, 2025

Warning

Rate limit exceeded

@mike182uk has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 18 minutes and 57 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between 11cd1a5 and 7f85b92.

📒 Files selected for processing (4)
  • apps/admin-x-activitypub/src/api/activitypub.ts (4 hunks)
  • apps/admin-x-activitypub/src/components/feed/FeedItem.tsx (4 hunks)
  • apps/admin-x-activitypub/src/hooks/use-activity-pub-queries.ts (1 hunks)
  • apps/admin-x-activitypub/src/utils/posts.ts (1 hunks)

Walkthrough

This update adds deletion capabilities to the ActivityPub implementation. A new boolean property, authoredByMe, is introduced in the Post interface to indicate if a post belongs to the current user. The ActivityPubAPI class now supports the DELETE HTTP method in its fetchJSON function and includes a dedicated delete method that constructs the appropriate URL and handles a 204 No Content response. In the FeedItem component, a deletion feature is integrated with a user confirmation flow using the new useDeletePostMutationForUser hook, which leverages React Query’s mutation functionality to handle state updates optimistically. Additionally, the posts mapping utility has been updated to include an authored property based on the new flag from the post object.

Possibly related PRs

Suggested labels

browser-tests

Suggested reviewers

  • djordjevlais

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@mike182uk mike182uk force-pushed the mike-ap-806-wire-up-the-delete-buttons-to-the-deleteid-api branch from a4716b4 to 11cd1a5 Compare March 3, 2025 16:27
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Nitpick comments (3)
apps/admin-x-activitypub/src/hooks/use-activity-pub-queries.ts (1)

819-882: Comprehensive implementation with room for improvement in cache management

The implementation is solid, handling optimistic updates and error recovery for feed and inbox. However, despite defining query keys for outboxQueryKey, likedQueryKey, and profilePostsQueryKey, the code doesn't update these caches optimistically, which could lead to stale data.

Consider updating all defined caches to maintain consistent state across the application. Here's a suggestion for extending the optimistic updates:

onMutate: (id) => {
    // Find the post in the feed query cache
    const previousFeed = queryClient.getQueryData<{pages: {posts: Activity[]}[]}[]>(feedQueryKey);
    queryClient.setQueryData(feedQueryKey, (current?: {pages: {posts: Activity[]}[]}) => {
        if (!current) {
            return current;
        }

        return {
            ...current,
            pages: current.pages.map((page: {posts: Activity[]}) => {
                return {
                    ...page,
                    posts: page.posts.filter((item: Activity) => item.id !== id)
                };
            })
        };
    });

    // Find the post in the inbox query cache
    const previousInbox = queryClient.getQueryData<{pages: {posts: Activity[]}[]}[]>(inboxQueryKey);
    queryClient.setQueryData(inboxQueryKey, (current?: {pages: {posts: Activity[]}[]}) => {
        if (!current) {
            return current;
        }

        return {
            ...current,
            pages: current.pages.map((page: {posts: Activity[]}) => {
                return {
                    ...page,
                    posts: page.posts.filter((item: Activity) => item.id !== id)
                };
            })
        };
    });

+   // Also update outbox, liked, and profile posts caches
+   const previousOutbox = queryClient.getQueryData<{pages: {data: Activity[]}[]}[]>(outboxQueryKey);
+   queryClient.setQueryData(outboxQueryKey, (current?: {pages: {data: Activity[]}[]}) => {
+       if (!current) {
+           return current;
+       }
+
+       return {
+           ...current,
+           pages: current.pages.map((page: {data: Activity[]}) => {
+               return {
+                   ...page,
+                   data: page.data.filter((item: Activity) => item.id !== id)
+               };
+           })
+       };
+   });
+
+   // Similar updates for likedQueryKey and profilePostsQueryKey
+   
    return {
        previousFeed,
        previousInbox,
+       previousOutbox,
+       // Add other previous states
    };
},
onError: (_err, _variables, context) => {
    queryClient.setQueryData(feedQueryKey, context?.previousFeed);
    queryClient.setQueryData(inboxQueryKey, context?.previousInbox);
+   queryClient.setQueryData(outboxQueryKey, context?.previousOutbox);
+   // Restore other caches
}
apps/admin-x-activitypub/src/components/feed/FeedItem.tsx (2)

218-224: Improve user confirmation UX for delete operation

The current confirmation dialog is minimal and only shows the post ID, which isn't user-friendly. Consider showing a more descriptive message that includes some content from the post.

const handleDelete = (postId: string) => {
-   const confirm = window.confirm(`Delete post\n\n${postId}\n\n?`);
+   // Get a readable post identifier (title or first few characters of content)
+   const postIdentifier = object.name || 
+       (object.content ? stripHtml(object.content).substring(0, 50) + '...' : 'this post');
+   const confirm = window.confirm(`Are you sure you want to delete "${postIdentifier}"?\n\nThis action cannot be undone.`);

    if (confirm) {
        deletePostMutation.mutate(postId);
    }
};

259-265: Remove TODO comment

The TODO comment about deleting posts can now be removed since you've implemented the functionality.

- // TODO: If this is your own Note/Article, you should be able to delete it
- // menuItems.push({
- //     id: 'delete',
- //     label: 'Delete',
- //     destructive: true,
- //     onClick: handleDelete
- // });
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 453cf69 and 11cd1a5.

📒 Files selected for processing (4)
  • apps/admin-x-activitypub/src/api/activitypub.ts (4 hunks)
  • apps/admin-x-activitypub/src/components/feed/FeedItem.tsx (4 hunks)
  • apps/admin-x-activitypub/src/hooks/use-activity-pub-queries.ts (1 hunks)
  • apps/admin-x-activitypub/src/utils/posts.ts (1 hunks)
🔇 Additional comments (7)
apps/admin-x-activitypub/src/utils/posts.ts (1)

107-107: LGTM! Appropriate handling of author status.

The explicit comparison === true ensures that the property is boolean, avoiding potential issues with falsy values. This change properly supports the post deletion feature being implemented.

apps/admin-x-activitypub/src/components/feed/FeedItem.tsx (2)

195-195: LGTM! Good initialization of the delete mutation hook.

The hook is properly initialized with 'index' as the handle parameter.


251-257: LGTM! Appropriate conditional rendering of delete option

The delete menu item is correctly displayed only when the post is authored by the current user, ensuring proper access control.

apps/admin-x-activitypub/src/api/activitypub.ts (4)

104-104: LGTM! Clear interface extension.

Adding the authoredByMe boolean property to the Post interface is a clean way to track post ownership.


137-137: Type update correctly expands HTTP method support

The update to include 'DELETE' as a valid method type is appropriate and follows TypeScript best practices.


151-155: Proper handling of HTTP 204 No Content responses

The implementation correctly handles 204 status code responses by returning null, which is appropriate for successful deletion operations that don't return content.


307-310: Well-implemented delete method

The delete method follows the established pattern in the class, using the appropriate URL structure and HTTP method. The implementation is clean and consistent with the codebase's style.

Comment on lines 837 to 838
const previousFeed = queryClient.getQueryData<{pages: {posts: Activity[]}[]}[]>(feedQueryKey);
queryClient.setQueryData(feedQueryKey, (current?: {pages: {posts: Activity[]}[]}) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Fix type assertion in getQueryData

The type assertion <{pages: {posts: Activity[]}[]}[]> doesn't match the structure returned by the query. This could lead to runtime type errors.

- const previousFeed = queryClient.getQueryData<{pages: {posts: Activity[]}[]}[]>(feedQueryKey);
+ const previousFeed = queryClient.getQueryData<{pages: {posts: Activity[]}[]}>(feedQueryKey);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const previousFeed = queryClient.getQueryData<{pages: {posts: Activity[]}[]}[]>(feedQueryKey);
queryClient.setQueryData(feedQueryKey, (current?: {pages: {posts: Activity[]}[]}) => {
const previousFeed = queryClient.getQueryData<{pages: {posts: Activity[]}[]}>(feedQueryKey);
queryClient.setQueryData(feedQueryKey, (current?: {pages: {posts: Activity[]}[]}) => {

Comment on lines 855 to 856
const previousInbox = queryClient.getQueryData<{pages: {posts: Activity[]}[]}[]>(inboxQueryKey);
queryClient.setQueryData(inboxQueryKey, (current?: {pages: {posts: Activity[]}[]}) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Fix type assertion in getQueryData

Same issue as previous comment - the type assertion doesn't match the structure returned by the query.

- const previousInbox = queryClient.getQueryData<{pages: {posts: Activity[]}[]}[]>(inboxQueryKey);
+ const previousInbox = queryClient.getQueryData<{pages: {posts: Activity[]}[]}>(inboxQueryKey);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const previousInbox = queryClient.getQueryData<{pages: {posts: Activity[]}[]}[]>(inboxQueryKey);
queryClient.setQueryData(inboxQueryKey, (current?: {pages: {posts: Activity[]}[]}) => {
const previousInbox = queryClient.getQueryData<{pages: {posts: Activity[]}[]}>(inboxQueryKey);
queryClient.setQueryData(inboxQueryKey, (current?: {pages: {posts: Activity[]}[]}) => {

@mike182uk mike182uk force-pushed the mike-ap-806-wire-up-the-delete-buttons-to-the-deleteid-api branch from 11cd1a5 to 038d842 Compare March 3, 2025 16:33
@mike182uk mike182uk force-pushed the mike-ap-806-wire-up-the-delete-buttons-to-the-deleteid-api branch from 038d842 to 7f85b92 Compare March 3, 2025 16:36
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