fix: s3 delivery after connect#264
Conversation
Summary of ChangesHello @jeffersonfelixdev, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request resolves a critical bug preventing S3 media delivery from functioning immediately for newly created users. Previously, S3 settings were not correctly loaded into the in-memory user cache by the authentication middleware, requiring a full container restart to activate S3 functionality. The implemented solution modifies the database query within the authentication process to include the necessary S3 enablement and media delivery preferences, ensuring that the user cache is populated with accurate information from the first authenticated request. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request correctly addresses a bug where S3 media delivery settings were not being applied for new users until a container restart. The root cause was accurately identified as the authentication middleware's cache not being populated with S3-related fields. The fix, which involves updating the SQL query in authalice and adding the new fields to the cached Values struct, is appropriate and well-implemented. The fix is well-explained in the description. I've included a couple of suggestions to improve code maintainability.
| log.Info().Msg("Looking for user information in DB") | ||
| // Checks DB from matching user and store user values in context | ||
| rows, err := s.db.Query("SELECT id,name,webhook,jid,events,proxy_url,qrcode,history,hmac_key IS NOT NULL AND length(hmac_key) > 0 FROM users WHERE token=$1 LIMIT 1", token) | ||
| rows, err := s.db.Query("SELECT id,name,webhook,jid,events,proxy_url,qrcode,history,hmac_key IS NOT NULL AND length(hmac_key) > 0,CASE WHEN s3_enabled THEN 'true' ELSE 'false' END,COALESCE(media_delivery, 'base64') FROM users WHERE token=$1 LIMIT 1", token) |
There was a problem hiding this comment.
This SQL query has become quite long, which can harm readability and maintainability. It's a good practice to extract complex queries into named constants. This improves readability and separates SQL from Go logic.
For example, you could define a constant at the package level:
const authaliceUserQuery = `
SELECT
id, name, webhook, jid, events, proxy_url, qrcode, history,
hmac_key IS NOT NULL AND length(hmac_key) > 0,
CASE WHEN s3_enabled THEN 'true' ELSE 'false' END,
COALESCE(media_delivery, 'base64')
FROM users
WHERE token=$1
LIMIT 1`Then, you can use this constant in your query call, making the code cleaner.
| rows, err := s.db.Query("SELECT id,name,webhook,jid,events,proxy_url,qrcode,history,hmac_key IS NOT NULL AND length(hmac_key) > 0,CASE WHEN s3_enabled THEN 'true' ELSE 'false' END,COALESCE(media_delivery, 'base64') FROM users WHERE token=$1 LIMIT 1", token) | |
| rows, err := s.db.Query(authaliceUserQuery, token) |
| v := Values{map[string]string{ | ||
| "Id": txtid, | ||
| "Name": name, | ||
| "Jid": jid, | ||
| "Webhook": webhook, | ||
| "Token": token, | ||
| "Proxy": proxy_url, | ||
| "Events": events, | ||
| "Qrcode": qrcode, | ||
| "History": historyStr, | ||
| "HasHmac": strconv.FormatBool(hasHmac), | ||
| "Id": txtid, | ||
| "Name": name, | ||
| "Jid": jid, | ||
| "Webhook": webhook, | ||
| "Token": token, | ||
| "Proxy": proxy_url, | ||
| "Events": events, | ||
| "Qrcode": qrcode, | ||
| "History": historyStr, | ||
| "HasHmac": strconv.FormatBool(hasHmac), | ||
| "S3Enabled": s3Enabled, | ||
| "MediaDelivery": mediaDelivery, | ||
| }} |
There was a problem hiding this comment.
To improve readability and long-term maintainability, it's a good practice to keep the keys in map literals sorted alphabetically. This makes it easier for developers to find specific keys when reading or modifying the code.
| v := Values{map[string]string{ | |
| "Id": txtid, | |
| "Name": name, | |
| "Jid": jid, | |
| "Webhook": webhook, | |
| "Token": token, | |
| "Proxy": proxy_url, | |
| "Events": events, | |
| "Qrcode": qrcode, | |
| "History": historyStr, | |
| "HasHmac": strconv.FormatBool(hasHmac), | |
| "Id": txtid, | |
| "Name": name, | |
| "Jid": jid, | |
| "Webhook": webhook, | |
| "Token": token, | |
| "Proxy": proxy_url, | |
| "Events": events, | |
| "Qrcode": qrcode, | |
| "History": historyStr, | |
| "HasHmac": strconv.FormatBool(hasHmac), | |
| "S3Enabled": s3Enabled, | |
| "MediaDelivery": mediaDelivery, | |
| }} | |
| v := Values{map[string]string{ | |
| "Events": events, | |
| "HasHmac": strconv.FormatBool(hasHmac), | |
| "History": historyStr, | |
| "Id": txtid, | |
| "Jid": jid, | |
| "MediaDelivery": mediaDelivery, | |
| "Name": name, | |
| "Proxy": proxy_url, | |
| "Qrcode": qrcode, | |
| "S3Enabled": s3Enabled, | |
| "Token": token, | |
| "Webhook": webhook, | |
| }} |
|
After more tests I figured out that S3 client was failing before upload media. I added lazy client initialization to ensure that client is connected before upload to S3. Now is working. |
Fix: S3 media delivery not applied until container restart
Description
When creating a user via
POST /admin/userswithmediaDelivery: "s3"and a valid S3 configuration (confirmed by/session/s3/test), the service did not upload message media to S3 after the user authorized the account. The/session/statusendpoint correctly showed S3 as enabled withmedia_delivery: "s3", but incoming messages with attachments (images, audio, documents, video) were not sent to S3. Calling/session/disconnectand/session/connectdid not fix the behaviour; only a full container restart did.Root cause
The authentication middleware (
authalice) populates the in-memory user cache (userinfocache) when a request is made with a token that is not yet cached. It loads the user from the database and builds aValuesobject to store in the cache.The middleware’s SQL query did not include
s3_enabledormedia_delivery. So when a newly created user (with S3 configured) made their first authenticated request (e.g./session/connectto authorize the session), the cache was filled withoutS3EnabledandMediaDelivery. The message handler inwmiau.gouses the cache first: when the user is in cache it readsS3EnabledandMediaDeliveryfrom there. With those keys missing (empty string), the conditions3Config.Enabled == "true"failed and S3 uploads were never triggered.On container restart,
connectOnStartup()runs and uses a different query that does includes3_enabledandmedia_delivery, so the cache was populated correctly and S3 worked until the next user created and authorized without a restart.Solution
Update the authentication middleware in
handlers.goso that when loading the user from the database it:s3_enabled(as'true'/'false') andCOALESCE(media_delivery, 'base64').S3EnabledandMediaDeliveryto theValuesstruct written touserinfocache.With this change, the first authenticated request after user creation (e.g. connect + QR auth) fills the cache with the correct S3 settings from the database, and the message handler uses S3 for media delivery without requiring a container restart.
Changes
authalice): includes3_enabledandmedia_deliveryin the DB query and in the cached userValues.This PR resolves #230.