Notification Lifecycle Functionality and Key Exchange Handling
Description:
This task involves implementing a function to process notifications and setting up a cron job to handle cases where the contact_key is unavailable. The function will manage the end-to-end notification lifecycle, including user verification, notification sending, and status logging. The cron job will ensure notifications with the WAITING_KEY_EXCHANGE status are periodically rechecked and processed when the contact_key becomes available.
Acceptance Criteria:
1. Notification Processing Function
Create a function named sendNotification to handle the following:
-
Input Parameters:
pubkey: User's public key.event: The event type (e.g.,bounty_assigned).content: Notification content.alias: Alias of the user
-
Steps to Implement:
-
Verify User on v2 Bot:
-
Call the
/contact/{pubkey}endpoint to check if the user exists. -
If
contact_keyis empty:- Add the user to the bot's contact list using the
/add_contactendpoint. - Re-call
/contact/{pubkey}to verify thecontact_key.
- Add the user to the bot's contact list using the
-
If the
contact_keyis still empty, log the notification in the database with the statusWAITING_KEY_EXCHANGEand exit the function
-
-
Send the Notification:
- If the
contact_keyis available, call the/sendendpoint with the appropriate payload:{ dest: string; // User's pubkey amt_msat: 0; // For text messages content: string; // Notification content is_tribe: false; // For direct messages wait: true; // Synchronous behavior }
- If the
-
Log the Result:
- Update the database with the response from the
/sendendpoint. - Store the final status (
COMPLETE,FAILED, orPENDING) along with relevant metadata.
- Update the database with the response from the
-
2. Cron Job for Key Exchange Handling
Implement a cron job to periodically process notifications with the status WAITING_KEY_EXCHANGE:
- Behavior:
- Query the database for all notifications with the status
WAITING_KEY_EXCHANGE. - For each record:
- Re-call the
/contact/{pubkey}endpoint to check if thecontact_keyis now available. - If the
contact_keyis available:- Call the function in charge of sending notifications to send the notification.
- Update the database with the response from the
/sendendpoint.
- If the
contact_keyis still unavailable increment retry by 1 and leave the record for the next cron job iteration.
- Re-call the
- Query the database for all notifications with the status
Database Schema:
Ensure the database table notifications supports the following:
id(Primary Key)event(Event type, e.g.,bounty_assigned)pubkeycontentretriesstatus(PENDING,COMPLETE,FAILED,WAITING_KEY_EXCHANGE)uuid(Unique identifier for the notification)created_at(Timestamp for record creation)
Additional Notes:
- The
sendNotificationfunction and cron job should be modular and reusable for future enhancements. - Ensure robust error handling for API calls and database interactions.
This Ticket depends on this
@humansinstitute please assign me
@humansinstitute please assign me