stackernews / stacker.news

Internet communities that pay you Bitcoin
https://stacker.news
MIT License
423 stars 106 forks source link

@remindme bot support #1159

Closed SatsAllDay closed 4 months ago

SatsAllDay commented 4 months ago

Description

Closes #510

This PR adds support for a @remindme bot, similar to the @delete bot.

It uses the same directive pattern, meaning you mention @remindme followed by a known syntax to enable the functionality. The pattern is as follows:

@remindme in <num> <units>

where <num> is a positive integer with no separators, and <units> is one of second, minute, hours, day, week, month, year, or their plural counterparts. Some examples:

  1. @remindme in 1 day
  2. @remindme in 2 minutes
  3. @remindme in 4 months
  4. etc.

When you include this in either a post or a comment, a reminder is scheduled and you are given feedback in the UI that tells you when you will be reminded of this item, localized to your browser's timezone/locale.

If you have a mention of @remindme but the reminder was not scheduled, a toast message is shown to help guide you to the correct syntax for using the bot.

If you delete an item that has a @remindme bot mention, the reminder is canceled (deleted). Note: if the reminder has already occurred, it is not deleted, since we can't "unremind" you. That means it will still show up in your notification history.

If you edit an item that has a @remindme bot mention, the existing reminder is canceled (deleted). If the updated text contains another @remindme bot mention, the new reminder is scheduled accordingly. Similar to deleting an item with a @remindme mention, if the reminder has already occurred before you edit the containing item, the reminder is not deleted, so it will still show up in your notification history.

Reminders are sent via notifications in your notifications page, as well as via push notification, if you have that enabled. The reminder links you to the item in which you mentioned @remindme.

So, for example, if someone else makes a post and you want to be reminded of it, you would reply via comment to the post with @remindme in 1 week. 1 week later, you will be notified of your comment, and therefore the parent item implicitly.

Technical overview:

  1. New Reminder model added to the DB. It has 3 interesting fields:
    1. itemId
    2. userId
    3. remindAt
  2. Updated item API resolver for create/update/delete item, to parse for @remindme mentions and extract the configuration, and create the Reminder instances, and also schedule the reminder jobs.
  3. New worker job type reminder - scheduled via the item API resolver, and handled via a new worker module
  4. Updates to all UI forms to surface feedback via toasts, for either a successful reminder scheduled, or a failed attempt to schedule reminder.
  5. Updates to the user API resolver to include reminder notifications in hasNewNotes
  6. Updates to the notifications API resolver to include reminders that have occurred (i.e. remindAt in the past) in the notifications listing
  7. Updates to the notifications UI to render reminder notifications appropriately

    Screenshots

    Successful reminder scheduled toast

    Successful reminder scheduled toast

Reminders in notifications page

Reminders in notifications page

Additional Context

  1. We could get away with removing userId from the Reminder model, since with the implementation, the userId in the Reminder is always the owner of the item corresponding to itemId, so we could just look up the user id when needed. I originally modeled it with both userId and itemId since that seemed natural, but it might be overkill.

    This approach does future-proof us if we ever decide to allow you to be directly reminded of someone else's items, though. So maybe it's fine? Open to input here.

Checklist

Are your changes backwards compatible? Please answer below: Yes. New DB model but it is backwards compatible.

Did you QA this? Could we deploy this straight to production? Please answer below: Yes.

For frontend changes: Tested on mobile? Please answer below: No new patterns implemented, so I didn't test it on mobile.

Did you introduce any new environment variables? If so, call them out explicitly here: No.

coderabbitai[bot] commented 4 months ago
Walkthrough ## Walkthrough The changes introduce a reminder functionality across various components and modules. Users can now set reminders on items, which will notify them at a scheduled time. This involves adding new resolver functions, type definitions, and database schema changes to support reminders. Additionally, notification handling is updated to include reminders, and UI components are modified to display reminder-related messages. ## Changes | Files/Modules | Change Summary | |----------------------------------------------------|--------------------------------------------------------------------------------------------------------------| | `api/resolvers/item.js`, `lib/item.js` | Added functions to handle reminders and modified existing functions to integrate reminder logic. | | `api/resolvers/notifications.js`, `user.js` | Added queries and logic for fetching and handling reminders. | | `api/typeDefs/item.js`, `notifications.js` | Added new fields and types for reminders in GraphQL type definitions. | | `components/...-form.js`, `components/reply.js` | Updated GraphQL queries and mutations to include `reminderScheduledAt` and changed toast messages. | | `components/notifications.js` | Added a new function and updated component to handle reminder notifications. | | `fragments/notifications.js` | Added fields for reminders in the `NOTIFICATIONS` GraphQL query. | | `lib/form.js`, `lib/item.js`, `lib/webPush.js` | Added functions and patterns to handle reminders and renamed toast functions. | | `prisma/migrations/.../migration.sql`, `schema.prisma` | Added new `Reminder` table and updated schema for reminders. | | `worker/index.js`, `worker/reminder.js` | Added job processing logic for sending reminder notifications. | ## Assessment against linked issues | Objective (Issue #510) | Addressed | Explanation | |----------------------------------------------------------------|-----------|-------------| | Implement a reminder bot that attaches to posts or comments | ✅ | | | Allow users to schedule reminders for various time intervals | ✅ | | | Notify users via messages in their notifications | ✅ | | | Attach the reminder as a reply to the original message | ✅ | | | Implement a cost mechanism for setting reminders | ❌ | The cost mechanism for setting reminders is not addressed in the changes. | Overall, the changes effectively implement the core reminder functionality as requested in the linked issue, but the cost mechanism for setting reminders is not included and needs to be addressed separately.

Recent Review Details **Configuration used: CodeRabbit UI** **Review profile: CHILL**
Commits Files that changed from the base of the PR and between 1946cf4e41615e34476693342efbd0d9a5f63137 and b884baa4b073c20c929ee58eca6bab67048ca77a.
Files selected for processing (1) * api/resolvers/item.js (6 hunks)
Additional comments not posted (1)
api/resolvers/item.js (1)
`1369-1370`: Ensure reminders are not created for anonymous users.
Verification successful --- The search results indicate that there are indeed checks for anonymous users (`ANON_USER_ID`) in various parts of the codebase, including the `api/resolvers/item.js` file. Specifically, there are lines that mention not supporting reminders for anonymous users and disallowing anonymous users from using reminders. Here are the relevant lines from `api/resolvers/item.js`: - `api/resolvers/item.js: // don't support reminders for ANON` - `api/resolvers/item.js: // disallow anon to use reminder` - `api/resolvers/item.js: if (!me || me.id === ANON_USER_ID) {` These lines suggest that the code does include logic to prevent reminders from being created for anonymous users. - : The codebase includes checks to ensure that reminders are not created for anonymous users.
Scripts Executed The following scripts were executed for the analysis: --- Script: ```shell #!/bin/bash # Description: Verify that reminders are not created for anonymous users. # Test: Search for checks against anonymous user IDs in reminder creation logic. ast-grep --lang javascript --pattern $'function createReminderAndJob($_, $_) { $$$ if ($_ === ANON_USER_ID) { $$$ } $$$ }' ``` Length of output: 135 --- --- Script: ```shell #!/bin/bash # Description: Locate the implementation of the createReminderAndJob function and inspect its logic. # Find the implementation of the createReminderAndJob function ast-grep --lang javascript --pattern 'function createReminderAndJob($_, $_) { $$$ }' # Search for any constants or variables that might represent anonymous users rg -i 'anon' --type js ``` Length of output: 10465
--- 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 - [X](https://twitter.com/intent/tweet?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A&url=https%3A//coderabbit.ai) - [Mastodon](https://mastodon.social/share?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A%20https%3A%2F%2Fcoderabbit.ai) - [Reddit](https://www.reddit.com/submit?title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&text=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code.%20Check%20it%20out%3A%20https%3A//coderabbit.ai) - [LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fcoderabbit.ai&mini=true&title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&summary=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code)
Tips ### Chat There are 3 ways to chat with [CodeRabbit](https://coderabbit.ai): - Review comments: Directly reply to a review comment made by CodeRabbit. Example: - `I pushed a fix in commit .` - `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 generate interesting stats about this repository and render them as a table.` - `@coderabbitai show all the console.log statements in this repository.` - `@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.` 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 as PR comments) - `@coderabbitai pause` to pause the reviews on a PR. - `@coderabbitai resume` to resume the paused reviews. - `@coderabbitai review` to trigger a review. This is useful when automatic reviews are disabled for the repository. - `@coderabbitai resolve` resolve all the CodeRabbit review comments. - `@coderabbitai help` to get help. Additionally, you can add `@coderabbitai ignore` anywhere in the PR description to prevent this PR from being reviewed. ### CodeRabbit Configration 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](https://docs.coderabbit.ai/guides/configure-coderabbit) 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](https://coderabbit.ai/docs) for detailed information on how to use CodeRabbit. - Join our [Discord Community](https://discord.com/invite/GsXnASn26c) to get help, request features, and share feedback. - Follow us on [X/Twitter](https://twitter.com/coderabbitai) for updates and announcements.
SatsAllDay commented 4 months ago

Awesome! Thank you for the feedback, I'll work on it as soon as I can

huumn commented 4 months ago

FYI I've made the changes myself which I just need to test now.