Chatwoot integration: `updateMessageSourceID` overwrites `created_at` with `NOW()` in the DB session timezone, shifting sent messages by the server's UTC offset (messages appear out of order)
メンテナーはふだん 4 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 2/5
- 見積もり時間
- 1〜3時間
- 初心者へのやさしさ
- 76/100
- issue の種類
- バグ
- 明瞭さ
- 明確に書かれている
- 活発さ
- 活発
- 技術スタック
- postgresql, typescript
調査の方向性
src/api/integrations/chatbot/chatwoot/utils/chatwoot-import-helper.tsのupdateMessageSourceIDから始め、libs/postgres.client.tsのPostgreSQLクライアント設定と関連するテストを調べてください。Chatwootメッセージのcreated_atとupdated_atを更新処理がどのように扱うか確認してください。データベースセッションのタイムゾーンがUTCでない場合に、送信メッセージがRailsによって割り当てられたタイムスタンプを保持し、時系列順に表示されれば完了です。
索引モデルが issue の本文から書いたものです。
説明
Summary
When a message is sent from Chatwoot through Evolution API, updateMessageSourceID runs a direct UPDATE on the Chatwoot database that sets created_at = NOW(). If the Chatwoot Postgres server timezone is not UTC, the stored value is shifted by the server's UTC offset. Chatwoot orders messages by created_at, so agent replies show up above the customer's newer messages.
This looks like the same root cause as the "UTC-3 on created_at" symptom listed in #2014 / #2316, but there it is described only for imports. The send path is affected too, on every outgoing message.
Where
src/api/integrations/chatbot/chatwoot/utils/chatwoot-import-helper.ts (current main, ~line 570):
public updateMessageSourceID(messageId: string | number, sourceId: string) {
const pgClient = postgresClient.getChatwootConnection();
const sql = `UPDATE messages SET source_id = $1, status = 0, created_at = NOW(), updated_at = NOW() WHERE id = $2;`;
return pgClient.query(sql, [`WAID:${sourceId}`, messageId]);
}
The pool in libs/postgres.client.ts is created from CHATWOOT_IMPORT_DATABASE_CONNECTION_URI with no timezone setting, so it inherits the server's timezone. Rails forces SET SESSION timezone TO 'UTC' on its own connections, but this pool does not.
messages.created_at / updated_at are timestamp without time zone, which Chatwoot reads as UTC. NOW() in a non-UTC session writes local wall-clock time into them.
Environment
- Evolution API v2.3.7 (also checked the source on
main) - Chatwoot self-hosted, Postgres 16 with
timezone = 'America/Sao_Paulo'(UTC-3) - Integration through
CHATWOOT_IMPORT_DATABASE_CONNECTION_URI(direct DB access)
Reproduce
- Run Chatwoot on a Postgres server whose timezone is not UTC.
- Connect an instance with the Chatwoot integration and the DB connection URI set.
- Send a reply from the Chatwoot UI.
- Query that message:
created_atis about 3 hours earlier than the real send time (here UTC-3). Itsupdated_at - created_atis about 3h once Chatwoot later touches the row (delivered/read). - Open the conversation: the reply is placed above customer messages that arrived before it.
Messages sent from the phone are not affected, because they are created by Rails with correct timestamps.
Suggested fix
Do not overwrite created_at in this UPDATE; Rails already set it correctly when the message was created (this is also what Chatwoot maintainers ask for in chatwoot/chatwoot#8779, where created_at is described as Rails-managed):
UPDATE messages SET source_id = $1, status = 0, updated_at = NOW() AT TIME ZONE 'UTC' WHERE id = $2;
The import paths that use NOW() or to_timestamp(...) into timestamp without time zone columns depend on the session timezone in the same way and could use AT TIME ZONE 'UTC' too. I have not tested these on upstream, only the send path described above.
Workaround
Force UTC on the Evolution connection by adding the options parameter to the connection URI:
CHATWOOT_IMPORT_DATABASE_CONNECTION_URI=postgresql://user:pass@host:5432/chatwoot?sslmode=disable&options=-c%20timezone%3DUTC
node-postgres forwards options in the startup packet, so sessions opened by Evolution run in UTC. We verified with SHOW timezone from inside the container: without options the session was in the server timezone, with it UTC. New messages were stored with the correct time afterwards. Existing rows stay shifted and need a one-off repair.
Related
- #2316 (closed as duplicate), #2014 (tracking umbrella)
- 主要言語
- TypeScript
- スター
- 10.2k
- フォーク
- 7.5k
- 平均マージ
- 6日 19時間
- マージ済み PR(30日)
- 4
環境構築
- Dockerfile または Docker Compose ファイルあり
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
evolution-foundation/evolution-api のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
evolution-foundation/evolution-api#2796 ·
メンテナーはふだん 4 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
evolution-foundation/evolution-api#2795 ·
メンテナーはふだん 4 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
evolution-foundation/evolution-api#2723 ·
メンテナーはふだん 4 日以内に返信
-
難易度 1/5 1〜3時間 初心者へのやさしさ 88/100
evolution-foundation/evolution-api#2704 ·
メンテナーはふだん 4 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
evolution-foundation/evolution-api#2702 · コメント 3 件 · リアクション 3 件 ·
メンテナーはふだん 4 日以内に返信
evolution-foundation/evolution-api の issue をすべて見る
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
abhigyanpatwari/GitNexus#4124 ·
メンテナーはふだん 1 日以内に返信
-
good first issue
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
laaielichter/Godotwright#250 ·
メンテナーはふだん 1 日以内に返信
-
An Antigravity tab can quote an old Assist message as the owner's words in a resumed conversationオープン
難易度 2/5 1〜3時間 初心者へのやさしさ 62/100
メンテナーはふだん 1 日以内に返信
-
Retry an empty final answer when Gemini reports MALFORMED_RESPONSE (gemini-4-hydrogen)対応中かも @waleedkadous が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 62/100
iaser-ai/ansari#444 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
[Bug]: Manager UI logo fails to load — broken external URL対応中かも @kavindu-udara が今日担当しました。 オープンbug
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100