Skip to content

feat: implement alwaysOnline presence flag (was a no-op)#91

Open
michelspinelli wants to merge 1 commit into
evolution-foundation:mainfrom
michelspinelli:feat/always-online-presence-flag
Open

feat: implement alwaysOnline presence flag (was a no-op)#91
michelspinelli wants to merge 1 commit into
evolution-foundation:mainfrom
michelspinelli:feat/always-online-presence-flag

Conversation

@michelspinelli

@michelspinelli michelspinelli commented Jun 24, 2026

Copy link
Copy Markdown

Problem

Numbers connected through Evolution Go stop receiving push notifications on the phone. This happens regardless of the alwaysOnline advanced setting — even with alwaysOnline=false for every instance.

Root cause

The alwaysOnline setting exists on the instance model, the REST API and Swagger (the CHANGELOG even notes alwaysOnline (still to be implemented)), but it was never wired into the presence logic.

In pkg/whatsmeow/service/whatsmeow.go, on the events.Connected handler, the client:

  1. calls SendPresence(types.PresenceAvailable) unconditionally on every (re)connect, and
  2. starts schedulePresenceUpdates, which every 1–3h toggles UnavailableAvailable.

Since whatsmeow reconnects frequently, the linked device is kept permanently "available". WhatsApp then delivers messages to that active companion session and suppresses push notifications on the primary phone.

Fix

Gate the connect-time presence (and the periodic schedulePresenceUpdates job) behind Instance.AlwaysOnline:

  • alwaysOnline == true → previous behavior (mark available + start the periodic job).
  • alwaysOnline == false → send PresenceUnavailable once, so the phone keeps receiving push notifications.

This makes the existing panel/API toggle actually work, per-instance, with no breaking change to current setups (the flag defaults to false in the model — operators who relied on always-online can simply enable it).

Testing

Built and deployed on a production VPS (upgraded from 0.6.1 to 0.7.1 + this patch) across 16 instances. After the change:

  • logs show Marked self as unavailable (alwaysOnline=false) on connect instead of Marked self as available;
  • sessions persisted (no re-pairing);
  • inbound/outbound messages and webhooks unaffected;
  • push notifications confirmed back on the users' phones.

🤖 Generated with Claude Code

Summary by Sourcery

Respect the instance alwaysOnline flag when setting WhatsApp presence on connect so linked devices are not kept permanently available by default.

Bug Fixes:

  • Fix presence handling so instances with alwaysOnline disabled are marked unavailable instead of always being set to available on connect, restoring push notifications to primary phones.

Enhancements:

  • Add conditional scheduling of periodic presence updates and improved logging for available/unavailable presence changes based on the alwaysOnline flag.

The `alwaysOnline` advanced setting exists on the instance model, the REST
API and Swagger, but it was never wired into the presence logic. On every
connect the device was marked `PresenceAvailable` unconditionally and the
periodic `schedulePresenceUpdates` job kept re-asserting it, so the linked
device stayed permanently "available". WhatsApp then delivers messages to
that active session and suppresses push notifications on the user's phone.

This gates the connect-time presence (and the periodic job) behind
`Instance.AlwaysOnline`. When the flag is false we send `PresenceUnavailable`
once so the phone keeps receiving push notifications; when true the previous
always-online behavior is preserved.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@sourcery-ai

sourcery-ai Bot commented Jun 24, 2026

Copy link
Copy Markdown

Reviewer's Guide

Wires the existing alwaysOnline instance flag into the WhatsApp presence handling so that presence updates and periodic presence scheduling are conditional on the flag, sending available only when alwaysOnline is true and explicitly sending unavailable when it is false, with additional logging around these behaviors.

Sequence diagram for alwaysOnline-controlled presence handling on connect

sequenceDiagram
    participant WhatsAppServer
    participant MyClient
    participant WAClient
    participant schedulePresenceUpdates
    participant Logger

    WhatsAppServer->>MyClient: events.Connected
    MyClient->>MyClient: myEventHandler
    alt Instance.AlwaysOnline == true
        MyClient->>schedulePresenceUpdates: schedulePresenceUpdates(mycli)
        MyClient->>WAClient: SendPresence(context, PresenceAvailable)
        alt SendPresence succeeds
            MyClient->>Logger: LogWarn(Marked self as available)
        else SendPresence fails
            MyClient->>Logger: LogWarn(Failed to send available presence)
        end
    else Instance.AlwaysOnline == false
        MyClient->>WAClient: SendPresence(context, PresenceUnavailable)
        alt SendPresence succeeds
            MyClient->>Logger: LogInfo(Marked self as unavailable)
        else SendPresence fails
            MyClient->>Logger: LogWarn(Failed to send unavailable presence)
        end
    end
Loading

File-Level Changes

Change Details Files
Gate connect-time and periodic presence behavior on the Instance.AlwaysOnline flag and adjust logging accordingly.
  • Wrap presence handling in events.Connected with a conditional on Instance.AlwaysOnline to decide presence state and whether to start schedulePresenceUpdates
  • When alwaysOnline is true, continue to schedule periodic presence updates and send PresenceAvailable, logging success or failure via a warning-level log
  • When alwaysOnline is false, send PresenceUnavailable once on connect and skip scheduling periodic presence updates, logging success at info level and failures at warning level
  • Refactor presence sending to use a shared err variable within the new conditional branches
pkg/whatsmeow/service/whatsmeow.go

Possibly linked issues

  • #(not provided): PR makes presence respect alwaysOnline flag, preventing unconditional PresenceAvailable and fixing permanent online behavior described.
  • #0: PR wires alwaysOnline into presence, sending PresenceUnavailable when false, which fixes lost phone notifications described.

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hey - I've found 1 issue, and left some high level feedback:

  • Consider using a non-Warn log level (e.g. Info/Debug) for the normal "Marked self as available" path so that only actual failures log as warnings, and align the log levels between the available/unavailable branches.
  • Instead of calling SendPresence with context.Background(), consider threading through a request-scoped context or adding a timeout context to avoid potential hangs if the presence call blocks.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Consider using a non-Warn log level (e.g. Info/Debug) for the normal "Marked self as available" path so that only actual failures log as warnings, and align the log levels between the available/unavailable branches.
- Instead of calling SendPresence with context.Background(), consider threading through a request-scoped context or adding a timeout context to avoid potential hangs if the presence call blocks.

## Individual Comments

### Comment 1
<location path="pkg/whatsmeow/service/whatsmeow.go" line_range="917-930" />
<code_context>
+				if err != nil {
+					mycli.loggerWrapper.GetLogger(mycli.userID).LogWarn("[%s] Failed to send available presence %v", mycli.userID, err)
+				} else {
+					mycli.loggerWrapper.GetLogger(mycli.userID).LogWarn("[%s] Marked self as available", mycli.userID)
+				}
 			} else {
-				mycli.loggerWrapper.GetLogger(mycli.userID).LogWarn("[%s] Marked self as available", mycli.userID)
+				err = mycli.WAClient.SendPresence(context.Background(), types.PresenceUnavailable)
+				if err != nil {
+					mycli.loggerWrapper.GetLogger(mycli.userID).LogWarn("[%s] Failed to send unavailable presence %v", mycli.userID, err)
+				} else {
+					mycli.loggerWrapper.GetLogger(mycli.userID).LogInfo("[%s] Marked self as unavailable (alwaysOnline=false)", mycli.userID)
+				}
 			}
</code_context>
<issue_to_address>
**suggestion:** Logging levels for successful presence updates are inconsistent and may be noisy.

In the `AlwaysOnline` branch, a successful `SendPresence` logs at `Warn`, while the `alwaysOnline=false` success logs at `Info`. Using `Warn` for an expected success will add noise and obscure real problems. Please use a non-warning level for the success case here, and keep both branches consistent unless there’s a strong reason not to.

```suggestion
				err = mycli.WAClient.SendPresence(context.Background(), types.PresenceAvailable)
				if err != nil {
					mycli.loggerWrapper.GetLogger(mycli.userID).LogWarn("[%s] Failed to send available presence %v", mycli.userID, err)
				} else {
					mycli.loggerWrapper.GetLogger(mycli.userID).LogInfo("[%s] Marked self as available (alwaysOnline=true)", mycli.userID)
				}
			} else {
				err = mycli.WAClient.SendPresence(context.Background(), types.PresenceUnavailable)
				if err != nil {
					mycli.loggerWrapper.GetLogger(mycli.userID).LogWarn("[%s] Failed to send unavailable presence %v", mycli.userID, err)
				} else {
					mycli.loggerWrapper.GetLogger(mycli.userID).LogInfo("[%s] Marked self as unavailable (alwaysOnline=false)", mycli.userID)
				}
			}
```
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment on lines +917 to 930
err = mycli.WAClient.SendPresence(context.Background(), types.PresenceAvailable)
if err != nil {
mycli.loggerWrapper.GetLogger(mycli.userID).LogWarn("[%s] Failed to send available presence %v", mycli.userID, err)
} else {
mycli.loggerWrapper.GetLogger(mycli.userID).LogWarn("[%s] Marked self as available", mycli.userID)
}
} else {
mycli.loggerWrapper.GetLogger(mycli.userID).LogWarn("[%s] Marked self as available", mycli.userID)
err = mycli.WAClient.SendPresence(context.Background(), types.PresenceUnavailable)
if err != nil {
mycli.loggerWrapper.GetLogger(mycli.userID).LogWarn("[%s] Failed to send unavailable presence %v", mycli.userID, err)
} else {
mycli.loggerWrapper.GetLogger(mycli.userID).LogInfo("[%s] Marked self as unavailable (alwaysOnline=false)", mycli.userID)
}
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

suggestion: Logging levels for successful presence updates are inconsistent and may be noisy.

In the AlwaysOnline branch, a successful SendPresence logs at Warn, while the alwaysOnline=false success logs at Info. Using Warn for an expected success will add noise and obscure real problems. Please use a non-warning level for the success case here, and keep both branches consistent unless there’s a strong reason not to.

Suggested change
err = mycli.WAClient.SendPresence(context.Background(), types.PresenceAvailable)
if err != nil {
mycli.loggerWrapper.GetLogger(mycli.userID).LogWarn("[%s] Failed to send available presence %v", mycli.userID, err)
} else {
mycli.loggerWrapper.GetLogger(mycli.userID).LogWarn("[%s] Marked self as available", mycli.userID)
}
} else {
mycli.loggerWrapper.GetLogger(mycli.userID).LogWarn("[%s] Marked self as available", mycli.userID)
err = mycli.WAClient.SendPresence(context.Background(), types.PresenceUnavailable)
if err != nil {
mycli.loggerWrapper.GetLogger(mycli.userID).LogWarn("[%s] Failed to send unavailable presence %v", mycli.userID, err)
} else {
mycli.loggerWrapper.GetLogger(mycli.userID).LogInfo("[%s] Marked self as unavailable (alwaysOnline=false)", mycli.userID)
}
}
err = mycli.WAClient.SendPresence(context.Background(), types.PresenceAvailable)
if err != nil {
mycli.loggerWrapper.GetLogger(mycli.userID).LogWarn("[%s] Failed to send available presence %v", mycli.userID, err)
} else {
mycli.loggerWrapper.GetLogger(mycli.userID).LogInfo("[%s] Marked self as available (alwaysOnline=true)", mycli.userID)
}
} else {
err = mycli.WAClient.SendPresence(context.Background(), types.PresenceUnavailable)
if err != nil {
mycli.loggerWrapper.GetLogger(mycli.userID).LogWarn("[%s] Failed to send unavailable presence %v", mycli.userID, err)
} else {
mycli.loggerWrapper.GetLogger(mycli.userID).LogInfo("[%s] Marked self as unavailable (alwaysOnline=false)", mycli.userID)
}
}

@NeritonDias

Copy link
Copy Markdown

🔴 👎

A implementação do flag alwaysOnline ficou limpa e aditiva, mas o PR está apontando para main. Aqui o fluxo de contribuição é via develop — PR que não mira a develop não entra. Reabre contra develop que eu reavalio.

@dpaes

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.

2 participants