mirror of
https://github.com/mautrix/whatsapp.git
synced 2026-11-09 15:49:18 -05:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
945474343f |
||
|
|
5c8c1fc835 |
||
|
|
d9b0daca89 |
||
|
|
ac29fa5272 |
||
|
|
d574a67a2d |
||
|
|
5640ef311b |
||
|
|
81f64557ea | ||
|
|
03859bb3db |
||
|
|
1d024590d2 |
||
|
|
5ba69ab4ee |
||
|
|
429320b373 |
||
|
|
78c5ee3b20 |
||
|
|
e097ba38de |
||
|
|
4be45ddbea |
||
|
|
96ce0799be |
||
|
|
07d8e9417b |
||
|
|
3a5d6448a8 |
||
|
|
9f82246069 | ||
|
|
5dd621fdf4 |
||
|
|
de02399ff8 |
||
|
|
ae8dcdec4b | ||
|
|
678d4ebb4f | ||
|
|
1f30d5516c | ||
|
|
e8f1d2b3f6 |
||
|
|
123ac4f340 |
||
|
|
33357ced5a |
||
|
|
ab1fae7181 |
||
|
|
7a0b4b5808 |
||
|
|
88f5fc738c |
||
|
|
51b5c5e5fb |
||
|
|
021504afb0 |
||
|
|
76c59bd05d |
||
|
|
bb09b7584c |
||
|
|
870414b015 |
||
|
|
57e055c3ad |
||
|
|
322485c5a9 |
||
|
|
960b4ed000 | ||
|
|
2759bc5ea2 | ||
|
|
1678681af7 |
||
|
|
56a19a1d10 |
||
|
|
8f1937b259 |
28 changed files with 650 additions and 134 deletions
1
.github/workflows/go.yml
vendored
1
.github/workflows/go.yml
vendored
|
|
@ -30,6 +30,7 @@ jobs:
|
|||
run: |
|
||||
go install golang.org/x/tools/cmd/goimports@latest
|
||||
go install honnef.co/go/tools/cmd/staticcheck@latest
|
||||
go install github.com/beeper/pre-commit-go/bridgelint@latest
|
||||
export PATH="$HOME/go/bin:$PATH"
|
||||
|
||||
- name: Run pre-commit
|
||||
|
|
|
|||
|
|
@ -21,9 +21,12 @@ repos:
|
|||
- id: go-mod-tidy
|
||||
|
||||
- repo: https://github.com/beeper/pre-commit-go
|
||||
rev: v0.4.2
|
||||
rev: v0.5.0
|
||||
hooks:
|
||||
- id: zerolog-ban-msgf
|
||||
- id: zerolog-use-stringer
|
||||
- id: prevent-literal-http-methods
|
||||
- id: zerolog-ban-global-log
|
||||
- id: bridgelint
|
||||
language: system
|
||||
verbose: true
|
||||
|
|
|
|||
4
go.mod
4
go.mod
|
|
@ -14,13 +14,13 @@ require (
|
|||
github.com/yuin/goldmark v1.8.6
|
||||
go.mau.fi/util v0.11.0
|
||||
go.mau.fi/webp v0.3.0
|
||||
go.mau.fi/whatsmeow v0.0.0-20261016130820-0b0a1cdac3e2
|
||||
go.mau.fi/whatsmeow v0.0.0-20261026125351-1fbae988179a
|
||||
golang.org/x/image v0.47.0
|
||||
golang.org/x/net v0.61.0
|
||||
golang.org/x/sync v0.24.0
|
||||
google.golang.org/protobuf v1.36.12
|
||||
gopkg.in/yaml.v3 v3.0.1
|
||||
maunium.net/go/mautrix v0.32.0
|
||||
maunium.net/go/mautrix v0.32.1-0.20261105141340-842796acf445
|
||||
)
|
||||
|
||||
require (
|
||||
|
|
|
|||
8
go.sum
8
go.sum
|
|
@ -71,8 +71,8 @@ go.mau.fi/util v0.11.0 h1:JNEpkVRxfTdJUC0DIGKiRlLKzl4G/qLX/eVzoP7QuwY=
|
|||
go.mau.fi/util v0.11.0/go.mod h1:04AEFVsc4zNdIkDC1gkdqbg/nZFolHY50pyXh2DSy7s=
|
||||
go.mau.fi/webp v0.3.0 h1:gVHQZtz21Ziwj+CDuklbX9mqpsnDIFKxs/BJyV7iZzA=
|
||||
go.mau.fi/webp v0.3.0/go.mod h1:rlZFTev+dYxhvk+XNBP/5GcTt4gXmzAB4DU0aGUYIQo=
|
||||
go.mau.fi/whatsmeow v0.0.0-20261016130820-0b0a1cdac3e2 h1:+EhsWM5VeE5Vu3KRlk3PmYAY0lEYkbhI5W4mTmM9X/Q=
|
||||
go.mau.fi/whatsmeow v0.0.0-20261016130820-0b0a1cdac3e2/go.mod h1:iY4lm61nTLWsNiIE1EmHUZwhKGBeoV4dhnVE2uMtNoQ=
|
||||
go.mau.fi/whatsmeow v0.0.0-20261026125351-1fbae988179a h1:ZyCV/mWfRkutxs5R0K3v+oyTbF4huprSlPZg/LEFQ7k=
|
||||
go.mau.fi/whatsmeow v0.0.0-20261026125351-1fbae988179a/go.mod h1:iY4lm61nTLWsNiIE1EmHUZwhKGBeoV4dhnVE2uMtNoQ=
|
||||
go.mau.fi/zeroconfig v0.2.0 h1:e/OGEERqVRRKlgaro7E6bh8xXiKFSXB3eNNIud7FUjU=
|
||||
go.mau.fi/zeroconfig v0.2.0/go.mod h1:J0Vn0prHNOm493oZoQ84kq83ZaNCYZnq+noI1b1eN8w=
|
||||
golang.org/x/crypto v0.58.0 h1:COkYLr4k7nDI4r6QtMML2AfzeIswWHAJthLS6K/M/54=
|
||||
|
|
@ -103,5 +103,5 @@ gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
|||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
maunium.net/go/mauflag v1.0.0 h1:YiaRc0tEI3toYtJMRIfjP+jklH45uDHtT80nUamyD4M=
|
||||
maunium.net/go/mauflag v1.0.0/go.mod h1:nLivPOpTpHnpzEh8jEdSL9UqO9+/KBJFmNRlwKfkPeA=
|
||||
maunium.net/go/mautrix v0.32.0 h1:xuAZzzyWce0pFkBhV/MCLFiJdVs2rFkY3Eep7hNng4I=
|
||||
maunium.net/go/mautrix v0.32.0/go.mod h1:tZSwPzw+/0pa0dxGufkh0uJqM84BNxB6e2I0+S0UyjU=
|
||||
maunium.net/go/mautrix v0.32.1-0.20261105141340-842796acf445 h1:ZI9Y2YEmjqQDloOCiciHeynTRYjUwK0ZZbNcRH/5NOc=
|
||||
maunium.net/go/mautrix v0.32.1-0.20261105141340-842796acf445/go.mod h1:tZSwPzw+/0pa0dxGufkh0uJqM84BNxB6e2I0+S0UyjU=
|
||||
|
|
|
|||
|
|
@ -142,8 +142,16 @@ func (wa *WhatsAppClient) downloadAndSaveWAHistorySyncData(ctx context.Context,
|
|||
Str("peer_data_request_session_id", evt.GetPeerDataRequestSessionID()).
|
||||
Msg("Downloading history sync")
|
||||
blob, err := wa.Client.DownloadHistorySync(log.WithContext(ctx), evt, true)
|
||||
if err != nil {
|
||||
if errors.Is(err, whatsmeow.ErrMediaDownloadFailedWith403) || errors.Is(err, whatsmeow.ErrMediaDownloadFailedWith404) || errors.Is(err, whatsmeow.ErrMediaDownloadFailedWith410) {
|
||||
log.Err(err).Msg("History sync blob is no longer available, dropping notification")
|
||||
if err = wa.Main.DB.HSNotif.Delete(ctx, rowid); err != nil {
|
||||
log.Err(err).Msg("Failed to delete queued history sync notification")
|
||||
errored = true
|
||||
}
|
||||
return
|
||||
} else if err != nil {
|
||||
log.Err(err).Msg("Failed to download history sync")
|
||||
errored = true
|
||||
return
|
||||
}
|
||||
if blob.GetSyncType() == waHistorySync.HistorySync_ON_DEMAND {
|
||||
|
|
@ -171,6 +179,7 @@ func (wa *WhatsAppClient) downloadAndSaveWAHistorySyncData(ctx context.Context,
|
|||
})
|
||||
if err != nil {
|
||||
log.Err(err).Msg("Failed to store history sync notification data")
|
||||
errored = true
|
||||
} else {
|
||||
resetTimer = blob.GetSyncType() == waHistorySync.HistorySync_INITIAL_BOOTSTRAP ||
|
||||
blob.GetSyncType() == waHistorySync.HistorySync_RECENT ||
|
||||
|
|
@ -246,10 +255,17 @@ func (wa *WhatsAppClient) handleWAHistorySync(
|
|||
} else if jid.Server == types.BroadcastServer {
|
||||
log.Debug().Stringer("chat_jid", jid).Msg("Skipping broadcast list in history sync")
|
||||
continue
|
||||
} else if jid == types.PSAJID {
|
||||
// New messages in the official updates chat aren't bridged because they use a different path,
|
||||
// so drop history as well. This should be removed if the PSA chat gets properly bridged in the future.
|
||||
log.Debug().Stringer("chat_jid", jid).Msg("Skipping PSA in history sync")
|
||||
continue
|
||||
} else {
|
||||
totalMessageCount += len(conv.GetMessages())
|
||||
}
|
||||
if jid.Server == types.DefaultUserServer {
|
||||
if jid == types.MetaAIJID {
|
||||
jid = types.NewMetaAIJID
|
||||
} else if jid.Server == types.DefaultUserServer {
|
||||
lid, err := wa.GetStore().LIDs.GetLIDForPN(ctx, jid)
|
||||
if err != nil {
|
||||
log.Err(err).Stringer("pn", jid).Msg("Failed to get LID for phone number in history sync")
|
||||
|
|
@ -282,6 +298,9 @@ func (wa *WhatsAppClient) handleWAHistorySync(
|
|||
Int("msg_index", i).
|
||||
Str("msg_id", rawMsg.GetMessage().GetKey().GetID()).
|
||||
Uint64("msg_time_seconds", rawMsg.GetMessage().GetMessageTimestamp()).
|
||||
Stringer("stub_type", rawMsg.GetMessage().GetMessageStubType()).
|
||||
Str("message_type", getMessageType(rawMsg.GetMessage().GetMessage())).
|
||||
Bool("from_me", rawMsg.GetMessage().GetKey().GetFromMe()).
|
||||
Msg("Dropping historical message due to parse error")
|
||||
log.Trace().
|
||||
Any("web_message_info", rawMsg.GetMessage()).
|
||||
|
|
@ -435,7 +454,7 @@ func (wa *WhatsAppClient) createPortalsFromHistorySync(ctx context.Context) {
|
|||
return
|
||||
}
|
||||
wrappedInfo, err := wa.getChatInfo(ctx, conv.ChatJID, conv, true)
|
||||
if errors.Is(err, whatsmeow.ErrNotInGroup) || errors.Is(err, whatsmeow.ErrGroupNotFound) {
|
||||
if errors.Is(err, whatsmeow.ErrNotInGroup) || errors.Is(err, whatsmeow.ErrGroupNotFound) || errors.Is(err, ErrNewsletterNotFound) {
|
||||
log.Debug().Err(err).Stringer("chat_jid", conv.ChatJID).
|
||||
Msg("Skipping creating room because the user is not a participant")
|
||||
//err = wa.Main.DB.Message.DeleteAllInChat(ctx, wa.UserLogin.ID, conv.ChatJID)
|
||||
|
|
@ -586,16 +605,36 @@ func (wa *WhatsAppClient) FetchMessages(ctx context.Context, params bridgev2.Fet
|
|||
}
|
||||
}
|
||||
}
|
||||
resp, err := wa.convertHistorySyncMessages(ctx, params.Portal, portalJID, messages, true)
|
||||
resp, err := wa.convertHistorySyncMessages(ctx, params.Portal, portalJID, messages, true, !params.Forward && !hasMore)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to convert messages: %w", err)
|
||||
}
|
||||
// TODO might be better to always set true here and let GetBetween return an empty result to mark as false?
|
||||
resp.HasMore = hasMore
|
||||
resp.Forward = params.Forward
|
||||
resp.MarkRead = markRead
|
||||
if params.Forward && !markRead && conv != nil && !ptr.Val(conv.MarkedAsUnread) {
|
||||
resp.ReadUpTo = historySyncReadUpTo(resp.Messages, int(ptr.Val(conv.UnreadCount)))
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func historySyncReadUpTo(messages []*bridgev2.BackfillMessage, unreadCount int) time.Time {
|
||||
if unreadCount <= 0 {
|
||||
return time.Time{}
|
||||
}
|
||||
for i := len(messages) - 1; i >= 0; i-- {
|
||||
if messages[i].Sender.IsFromMe {
|
||||
continue
|
||||
}
|
||||
unreadCount--
|
||||
if unreadCount == 0 {
|
||||
return messages[i].Timestamp.Add(-time.Nanosecond)
|
||||
}
|
||||
}
|
||||
return time.Time{}
|
||||
}
|
||||
|
||||
func (wa *WhatsAppClient) deleteHistorySyncMessages(ctx context.Context, portalJID types.JID, newestTS, oldestTS uint64) {
|
||||
var err error
|
||||
var rows int64
|
||||
|
|
@ -628,6 +667,7 @@ func (wa *WhatsAppClient) convertHistorySyncMessages(
|
|||
portalJID types.JID,
|
||||
messages []*waWeb.WebMessageInfo,
|
||||
explodeOnError bool,
|
||||
deleteEverything bool,
|
||||
) (*bridgev2.FetchMessagesResponse, error) {
|
||||
cli := wa.Client
|
||||
if cli == nil {
|
||||
|
|
@ -664,6 +704,9 @@ func (wa *WhatsAppClient) convertHistorySyncMessages(
|
|||
if !wa.ensureAltJIDs(ctx, &evt.Info.MessageSource, false) {
|
||||
return nil, fmt.Errorf("failed to ensure alt JIDs for message %s", evt.Info.ID)
|
||||
}
|
||||
if evt.Info.IsFromMe {
|
||||
wa.addPreviousOwnLID(ctx, evt.Info.Sender)
|
||||
}
|
||||
isViewOnce := evt.IsViewOnce || evt.IsViewOnceV2 || evt.IsViewOnceV2Extension
|
||||
converted, mediaReq := wa.convertHistorySyncMessage(
|
||||
ctx, cli, portal, &evt.Info, evt.Message, evt.RawMessage, isViewOnce, msg.Reactions,
|
||||
|
|
@ -683,9 +726,11 @@ func (wa *WhatsAppClient) convertHistorySyncMessages(
|
|||
Messages: convertedMessages,
|
||||
Cursor: networkid.PaginationCursor(strconv.FormatUint(oldestTS, 10)),
|
||||
CompleteCallback: func() {
|
||||
// TODO this only deletes after backfilling. If there's no need for backfill after a relogin,
|
||||
// the messages will be stuck in the database
|
||||
wa.deleteHistorySyncMessages(ctx, portalJID, newestTS, oldestTS)
|
||||
if deleteEverything {
|
||||
wa.deleteHistorySyncMessages(ctx, portalJID, 1<<62, oldestTS)
|
||||
} else {
|
||||
wa.deleteHistorySyncMessages(ctx, portalJID, newestTS, oldestTS)
|
||||
}
|
||||
if len(mediaRequests) > 0 {
|
||||
go func(ctx context.Context) {
|
||||
for _, req := range mediaRequests {
|
||||
|
|
@ -766,10 +811,12 @@ type onDemandHistorySyncRequest struct {
|
|||
SentAt time.Time
|
||||
}
|
||||
|
||||
const onDemandHistorySyncRequestTimeout = 1 * time.Hour
|
||||
|
||||
func (wa *WhatsAppClient) startOnDemandHistorySyncRequest(ctx context.Context, portalJID types.JID, msgID types.MessageID) bool {
|
||||
wa.onDemandHistorySyncLock.Lock()
|
||||
defer wa.onDemandHistorySyncLock.Unlock()
|
||||
if req, inFlight := wa.onDemandHistorySyncRequests[portalJID]; inFlight {
|
||||
if req, inFlight := wa.onDemandHistorySyncRequests[portalJID]; inFlight && time.Since(req.SentAt) < onDemandHistorySyncRequestTimeout {
|
||||
zerolog.Ctx(ctx).Debug().
|
||||
Str("in_flight_request_msg_id", req.ID).
|
||||
Stringer("portal_jid", portalJID).
|
||||
|
|
@ -803,7 +850,9 @@ func (wa *WhatsAppClient) handleOnDemandHistorySync(ctx context.Context, blob *w
|
|||
zerolog.Ctx(ctx).Err(err).Str("jid", conv.GetID()).Msg("Failed to parse portal JID")
|
||||
continue
|
||||
}
|
||||
if portalJID.Server == types.DefaultUserServer {
|
||||
if portalJID == types.MetaAIJID {
|
||||
portalJID = types.NewMetaAIJID
|
||||
} else if portalJID.Server == types.DefaultUserServer {
|
||||
lid, err := wa.GetStore().LIDs.GetLIDForPN(ctx, portalJID)
|
||||
if err != nil {
|
||||
zerolog.Ctx(ctx).Err(err).Stringer("lid", portalJID).Msg("Failed to get LID for phone number in on-demand history sync")
|
||||
|
|
@ -844,7 +893,7 @@ func (wa *WhatsAppClient) handleOnDemandHistorySync(ctx context.Context, blob *w
|
|||
Int("message_count", len(messages)).
|
||||
Stringer("end_of_history_type", conv.GetEndOfHistoryTransferType()).
|
||||
Msg("Converting messages to bridge from on-demand history sync")
|
||||
resp, err := wa.convertHistorySyncMessages(ctx, portal, portalJID, messages, false)
|
||||
resp, err := wa.convertHistorySyncMessages(ctx, portal, portalJID, messages, false, false)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -31,6 +31,7 @@ func (wa *WhatsAppClient) GetChatInfo(ctx context.Context, portal *bridgev2.Port
|
|||
}
|
||||
|
||||
var ErrBroadcastList = errors.New("broadcast list bridging is currently not supported")
|
||||
var ErrNewsletterNotFound = errors.New("newsletter doesn't exist")
|
||||
|
||||
func (wa *WhatsAppClient) getChatInfo(ctx context.Context, portalJID types.JID, conv *wadb.Conversation, isNew bool) (wrapped *bridgev2.ChatInfo, err error) {
|
||||
switch portalJID.Server {
|
||||
|
|
@ -56,6 +57,8 @@ func (wa *WhatsAppClient) getChatInfo(ctx context.Context, portalJID types.JID,
|
|||
info, err := wa.Client.GetNewsletterInfo(ctx, portalJID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else if info == nil || info.ID.IsEmpty() {
|
||||
return nil, ErrNewsletterNotFound
|
||||
}
|
||||
wrapped = wa.wrapNewsletterInfo(ctx, info)
|
||||
default:
|
||||
|
|
|
|||
|
|
@ -55,6 +55,7 @@ func (wa *WhatsAppConnector) LoadUserLogin(ctx context.Context, login *bridgev2.
|
|||
mediaRetryLock: semaphore.NewWeighted(wa.Config.HistorySync.MediaRequests.MaxAsyncHandle),
|
||||
pushNamesSynced: exsync.NewEvent(),
|
||||
appStateFullSyncAttempted: make(map[appstate.WAPatchName]time.Time),
|
||||
previousOwnLIDs: exsync.NewSetWithItems(login.Metadata.(*waid.UserLoginMetadata).PreviousOwnLIDs),
|
||||
|
||||
onDemandHistorySyncRequests: make(map[types.JID]onDemandHistorySyncRequest),
|
||||
|
||||
|
|
@ -84,6 +85,7 @@ func (wa *WhatsAppConnector) LoadUserLogin(ctx context.Context, login *bridgev2.
|
|||
w.Client.ManualHistorySyncDownload = true
|
||||
w.Client.SendReportingTokens = true
|
||||
w.Client.AutomaticMessageRerequestFromPhone = true
|
||||
w.Client.UnifiedSessionInterval = 24 * time.Hour
|
||||
w.Client.GetMessageForRetry = w.trackNotFoundRetry
|
||||
w.Client.PreRetryCallback = w.trackFoundRetry
|
||||
w.Client.BackgroundEventCtx = w.UserLogin.Log.WithContext(wa.Bridge.BackgroundCtx)
|
||||
|
|
@ -137,6 +139,9 @@ type WhatsAppClient struct {
|
|||
|
||||
onDemandHistorySyncLock sync.Mutex
|
||||
onDemandHistorySyncRequests map[types.JID]onDemandHistorySyncRequest
|
||||
|
||||
previousOwnLIDs *exsync.Set[types.JID]
|
||||
previousOwnLIDsLock sync.Mutex
|
||||
}
|
||||
|
||||
var (
|
||||
|
|
@ -205,7 +210,26 @@ func (wa *WhatsAppClient) IsThisUser(_ context.Context, userID networkid.UserID)
|
|||
|
||||
func (wa *WhatsAppClient) IsOwnJID(jid types.JID) bool {
|
||||
return (jid.Server == types.DefaultUserServer && jid.User == wa.JID.User) ||
|
||||
(jid.Server == types.HiddenUserServer && jid.User == wa.GetLID().User)
|
||||
(jid.Server == types.HiddenUserServer && jid.User == wa.GetLID().User) ||
|
||||
wa.previousOwnLIDs.Has(jid.ToNonAD())
|
||||
}
|
||||
|
||||
func (wa *WhatsAppClient) addPreviousOwnLID(ctx context.Context, jid types.JID) {
|
||||
if jid.Server != types.HiddenUserServer || wa.IsOwnJID(jid) {
|
||||
return
|
||||
}
|
||||
jid = jid.ToNonAD()
|
||||
wa.previousOwnLIDsLock.Lock()
|
||||
defer wa.previousOwnLIDsLock.Unlock()
|
||||
if !wa.previousOwnLIDs.Add(jid) {
|
||||
return
|
||||
}
|
||||
zerolog.Ctx(ctx).Info().Stringer("lid", jid).Msg("Found previous own LID in history sync")
|
||||
wa.UserLogin.Metadata.(*waid.UserLoginMetadata).PreviousOwnLIDs = wa.previousOwnLIDs.AsList()
|
||||
err := wa.UserLogin.Save(ctx)
|
||||
if err != nil {
|
||||
zerolog.Ctx(ctx).Err(err).Msg("Failed to save user login after adding previous own LID")
|
||||
}
|
||||
}
|
||||
|
||||
func (wa *WhatsAppClient) GetLID() types.JID {
|
||||
|
|
|
|||
|
|
@ -148,13 +148,7 @@ func fnSync(ce *commands.Event) {
|
|||
case "appstate":
|
||||
names := appstate.AllPatchNames[:]
|
||||
if len(ce.Args) > 1 {
|
||||
names = exslices.CastFuncFilter(ce.Args[1:], func(name string) (appstate.WAPatchName, bool) {
|
||||
if !slices.Contains(appstate.AllPatchNames[:], appstate.WAPatchName(name)) {
|
||||
ce.Reply("Invalid app state name `%s`", name)
|
||||
return "", false
|
||||
}
|
||||
return appstate.WAPatchName(name), true
|
||||
})
|
||||
names = parseAppStateNames(ce, ce.Args[1:])
|
||||
}
|
||||
for _, name := range names {
|
||||
err := wa.Client.FetchAppState(ce.Ctx, name, true, false)
|
||||
|
|
@ -174,6 +168,50 @@ func fnSync(ce *commands.Event) {
|
|||
}
|
||||
}
|
||||
|
||||
func parseAppStateNames(ce *commands.Event, args []string) []appstate.WAPatchName {
|
||||
return exslices.CastFuncFilter(args, func(name string) (appstate.WAPatchName, bool) {
|
||||
if !slices.Contains(appstate.AllPatchNames[:], appstate.WAPatchName(name)) {
|
||||
ce.Reply("Invalid app state name `%s`", name)
|
||||
return "", false
|
||||
}
|
||||
return appstate.WAPatchName(name), true
|
||||
})
|
||||
}
|
||||
|
||||
var cmdResetAppState = &commands.FullHandler{
|
||||
Func: fnResetAppState,
|
||||
Name: "reset-appstate",
|
||||
Help: commands.HelpMeta{
|
||||
Section: commands.HelpSectionAdmin,
|
||||
Description: "Ask your phone to reset broken app state. This logs out all linked devices.",
|
||||
Args: "<names...>",
|
||||
},
|
||||
RequiresLogin: true,
|
||||
}
|
||||
|
||||
func fnResetAppState(ce *commands.Event) {
|
||||
login := ce.User.GetDefaultLogin()
|
||||
if login == nil {
|
||||
ce.Reply("Login not found")
|
||||
return
|
||||
}
|
||||
if len(ce.Args) == 0 {
|
||||
ce.Reply("Usage: `$cmdprefix reset-appstate <names...>`\n\nThis logs out all linked devices, including the bridge. You'll have to log in again afterwards.")
|
||||
return
|
||||
}
|
||||
names := parseAppStateNames(ce, ce.Args)
|
||||
if len(names) != len(ce.Args) {
|
||||
return
|
||||
}
|
||||
wa := login.Client.(*WhatsAppClient)
|
||||
_, err := wa.Client.SendPeerMessage(ce.Ctx, whatsmeow.BuildFatalAppStateExceptionNotification(names...))
|
||||
if err != nil {
|
||||
ce.Reply("Failed to send app state reset request: %v", err)
|
||||
return
|
||||
}
|
||||
ce.Reply("Sent app state reset request. Your phone should log out all linked devices shortly, after which you can log in again.")
|
||||
}
|
||||
|
||||
var cmdInviteLink = &commands.FullHandler{
|
||||
Func: fnInviteLink,
|
||||
Name: "invite-link",
|
||||
|
|
|
|||
|
|
@ -110,7 +110,7 @@ func (wa *WhatsAppConnector) Init(bridge *bridgev2.Bridge) {
|
|||
wa.DB = wadb.New(bridge.ID, bridge.DB.Database, bridge.Log.With().Str("db_section", "whatsapp").Logger())
|
||||
wa.MsgConv.DB = wa.DB
|
||||
wa.Bridge.Commands.(*commands.Processor).AddHandlers(
|
||||
cmdAccept, cmdSync, cmdInviteLink, cmdResolveLink, cmdJoin,
|
||||
cmdAccept, cmdSync, cmdResetAppState, cmdInviteLink, cmdResolveLink, cmdJoin,
|
||||
)
|
||||
wa.mediaEditCache = make(MediaEditCache)
|
||||
wa.unmigratedDMs = exsync.NewSet[networkid.PortalKey]()
|
||||
|
|
@ -164,11 +164,19 @@ func (wa *WhatsAppConnector) Start(ctx context.Context) error {
|
|||
if err != nil {
|
||||
return fmt.Errorf("failed to migrate to LID DMs: %w", err)
|
||||
}
|
||||
err = wa.migrateMetaAIPortals(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to migrate Meta AI portals: %w", err)
|
||||
}
|
||||
go func() {
|
||||
err = wa.syncMismatchingGhosts(wa.Bridge.BackgroundCtx)
|
||||
if err != nil {
|
||||
wa.Bridge.Log.Err(err).Msg("Failed to sync mismatching ghosts")
|
||||
}
|
||||
err = wa.removeRejoinedPNGhosts(wa.Bridge.BackgroundCtx)
|
||||
if err != nil {
|
||||
wa.Bridge.Log.Err(err).Msg("Failed to remove rejoined PN ghosts")
|
||||
}
|
||||
}()
|
||||
return nil
|
||||
}
|
||||
|
|
@ -235,7 +243,16 @@ func (wa *WhatsAppConnector) GenerateTransactionID(_ id.UserID, _ id.RoomID, _ e
|
|||
}
|
||||
|
||||
func (wa *WhatsAppConnector) ResetHTTPTransport() {
|
||||
// No-op for now, whatsmeow doesn't use the shared transport config yet
|
||||
for _, login := range wa.Bridge.GetAllCachedUserLogins() {
|
||||
client := login.Client.(*WhatsAppClient).Client
|
||||
if client == nil {
|
||||
continue
|
||||
}
|
||||
err := wa.updateProxy(login.Log.WithContext(wa.Bridge.BackgroundCtx), client, false)
|
||||
if err != nil {
|
||||
login.Log.Err(err).Msg("Failed to recreate HTTP clients")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (wa *WhatsAppConnector) ResetNetworkConnections() {
|
||||
|
|
|
|||
|
|
@ -164,7 +164,19 @@ func (wa *WhatsAppConnector) downloadStickerDirectMedia(ctx context.Context, par
|
|||
return nil, fmt.Errorf("no WhatsApp client found on login %s", parsedID.UserLogin)
|
||||
}
|
||||
sticker, err := wa.MsgConv.GetCachedSticker(ctx, waClient.Client, parsedID.Sticker.PackID, parsedID.Sticker.FileHash)
|
||||
if err != nil {
|
||||
var httpErr whatsmeow.DownloadHTTPError
|
||||
if errors.As(err, &httpErr) {
|
||||
switch httpErr.StatusCode {
|
||||
case http.StatusNotFound, http.StatusGone:
|
||||
return nil, mautrix.MNotFound.WithMessage("Sticker pack not found")
|
||||
case http.StatusUnauthorized, http.StatusForbidden:
|
||||
return nil, mautrix.MForbidden.WithMessage("WhatsApp denied access to the sticker pack")
|
||||
case http.StatusTooManyRequests:
|
||||
return nil, mautrix.MLimitExceeded.WithMessage("Rate limited by WhatsApp while fetching the sticker pack").WithCanRetry(true)
|
||||
default:
|
||||
return nil, mautrix.MUnknown.WithMessage("WhatsApp returned HTTP %d while fetching the sticker pack", httpErr.StatusCode).WithStatus(http.StatusBadGateway).WithCanRetry(true)
|
||||
}
|
||||
} else if err != nil {
|
||||
return nil, err
|
||||
} else if sticker == nil {
|
||||
return nil, mautrix.MNotFound.WithMessage("Sticker not found in pack")
|
||||
|
|
|
|||
|
|
@ -21,11 +21,16 @@ import (
|
|||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"go.mau.fi/whatsmeow"
|
||||
"maunium.net/go/mautrix"
|
||||
"maunium.net/go/mautrix/bridgev2"
|
||||
"maunium.net/go/mautrix/event"
|
||||
|
||||
"go.mau.fi/mautrix-whatsapp/pkg/waid"
|
||||
)
|
||||
|
||||
// wrapConversionError turns a failure to convert a Matrix event into a message status. Nothing has been
|
||||
|
|
@ -85,6 +90,50 @@ func wrapWhatsAppError(err error) error {
|
|||
}
|
||||
}
|
||||
|
||||
// wrapSendError turns an error from sending a message to WhatsApp into a message status.
|
||||
func (wa *WhatsAppClient) wrapSendError(err error) error {
|
||||
switch sendErrorCode(err) {
|
||||
case 403:
|
||||
return bridgev2.WrapErrorInStatus(err).
|
||||
WithMessage("you don't have permission to do that on WhatsApp").
|
||||
WithErrorReason(event.MessageStatusNoPermission).
|
||||
WithStatus(event.MessageStatusFail).
|
||||
WithIsCertain(true).
|
||||
WithSendNotice(true)
|
||||
case 463:
|
||||
msg := "WhatsApp has temporarily restricted your account from messaging new contacts"
|
||||
if until := wa.UserLogin.Metadata.(*waid.UserLoginMetadata).ReachoutTimelockUntil; until.After(time.Now()) {
|
||||
msg = fmt.Sprintf("WhatsApp has restricted your account from messaging new contacts until %s", until.UTC().Format("Jan 2, 2006 at 3:04 PM MST"))
|
||||
}
|
||||
return bridgev2.WrapErrorInStatus(err).
|
||||
WithMessage(msg).
|
||||
WithErrorReason(event.MessageStatusNoPermission).
|
||||
WithStatus(event.MessageStatusFail).
|
||||
WithIsCertain(true).
|
||||
WithSendNotice(true)
|
||||
case 421, 479:
|
||||
return bridgev2.WrapErrorInStatus(err).
|
||||
WithMessage("WhatsApp rejected the message").
|
||||
WithErrorReason(event.MessageStatusNetworkError).
|
||||
WithStatus(event.MessageStatusFail).
|
||||
WithIsCertain(true).
|
||||
WithSendNotice(true)
|
||||
default:
|
||||
return wrapWhatsAppError(err)
|
||||
}
|
||||
}
|
||||
|
||||
// sendErrorCode returns the code WhatsApp rejected a message with, which whatsmeow only includes in the
|
||||
// error text.
|
||||
func sendErrorCode(err error) int {
|
||||
if !errors.Is(err, whatsmeow.ErrServerReturnedError) {
|
||||
return 0
|
||||
}
|
||||
_, codeStr, _ := strings.Cut(err.Error(), whatsmeow.ErrServerReturnedError.Error()+" ")
|
||||
code, _ := strconv.Atoi(codeStr)
|
||||
return code
|
||||
}
|
||||
|
||||
// wrapWhatsAppRespError turns an error from a whatsmeow request made during login or provisioning into
|
||||
// an HTTP error, keeping the original error in the chain. Clients retry 429 and 502-504 automatically,
|
||||
// which isn't wanted for provisioning requests, so those statuses aren't used.
|
||||
|
|
|
|||
|
|
@ -474,7 +474,9 @@ func (evt *WAMediaRetry) getRealSender() types.JID {
|
|||
|
||||
func (evt *WAMediaRetry) GetSender() bridgev2.EventSender {
|
||||
realSender := pickLID(evt.getRealSender(), evt.senderLID)
|
||||
return evt.wa.makeEventSender(evt.wa.Main.Bridge.BackgroundCtx, realSender)
|
||||
sender := evt.wa.makeEventSender(evt.wa.Main.Bridge.BackgroundCtx, realSender)
|
||||
sender.ForceEditOrigSender = true
|
||||
return sender
|
||||
}
|
||||
|
||||
func (evt *WAMediaRetry) GetTargetMessage() networkid.MessageID {
|
||||
|
|
|
|||
|
|
@ -107,6 +107,8 @@ func (wa *WhatsAppClient) HandleMatrixMessage(ctx context.Context, msg *bridgev2
|
|||
var ErrBroadcastSendDisabled = bridgev2.WrapErrorInStatus(errors.New("sending status messages is disabled")).WithErrorAsMessage().WithIsCertain(true).WithSendNotice(true).WithErrorReason(event.MessageStatusUnsupported).WithStatus(event.MessageStatusFail)
|
||||
var ErrBroadcastReactionUnsupported = bridgev2.WrapErrorInStatus(errors.New("reacting to status messages is not currently supported")).WithErrorAsMessage().WithIsCertain(true).WithSendNotice(true).WithErrorReason(event.MessageStatusUnsupported).WithStatus(event.MessageStatusFail)
|
||||
var ErrNotCommunityAdmin = bridgev2.WrapErrorInStatus(errors.New("only community admins can send announcements")).WithErrorAsMessage().WithIsCertain(true).WithSendNotice(true).WithErrorReason(event.MessageStatusNoPermission).WithStatus(event.MessageStatusFail)
|
||||
var ErrEmptyGroupName = bridgev2.WrapErrorInStatus(errors.New("WhatsApp group names can't be empty")).WithErrorAsMessage().WithIsCertain(true).WithSendNotice(true).WithErrorReason(event.MessageStatusUnsupported).WithStatus(event.MessageStatusFail)
|
||||
var ErrAddBlockedByPrivacy = bridgev2.WrapErrorInStatus(errors.New("this user's privacy settings don't allow adding them to groups")).WithErrorAsMessage().WithIsCertain(true).WithSendNotice(true).WithErrorReason(event.MessageStatusNoPermission).WithStatus(event.MessageStatusFail)
|
||||
|
||||
func (wa *WhatsAppClient) handleConvertedMatrixMessage(ctx context.Context, msg *bridgev2.MatrixMessage, waMsg *waE2E.Message, req *whatsmeow.SendRequestExtra) (*bridgev2.MatrixMessageResponse, error) {
|
||||
if req == nil {
|
||||
|
|
@ -139,7 +141,7 @@ func (wa *WhatsAppClient) handleConvertedMatrixMessage(ctx context.Context, msg
|
|||
if errors.Is(err, whatsmeow.ErrNoPermissionInGroup) {
|
||||
return nil, ErrNotCommunityAdmin
|
||||
}
|
||||
return nil, wrapWhatsAppError(err)
|
||||
return nil, wa.wrapSendError(err)
|
||||
}
|
||||
var pickedMessageID networkid.MessageID
|
||||
if resp.Sender == wa.GetLID() {
|
||||
|
|
@ -218,7 +220,7 @@ func (wa *WhatsAppClient) HandleMatrixReaction(ctx context.Context, msg *bridgev
|
|||
Metadata: &waid.ReactionMetadata{
|
||||
SenderDeviceID: wa.JID.Device,
|
||||
},
|
||||
}, wrapWhatsAppError(err)
|
||||
}, wa.wrapSendError(err)
|
||||
}
|
||||
|
||||
func (wa *WhatsAppClient) HandleMatrixReactionRemove(ctx context.Context, msg *bridgev2.MatrixReactionRemove) (retErr error) {
|
||||
|
|
@ -248,7 +250,7 @@ func (wa *WhatsAppClient) HandleMatrixReactionRemove(ctx context.Context, msg *b
|
|||
defer wa.mcTrack(msg, time.Now(), &retErr)
|
||||
resp, err := wa.Client.SendMessage(ctx, portalJID, reactionMsg, extra)
|
||||
zerolog.Ctx(ctx).Trace().Any("response", resp).Msg("WhatsApp reaction response")
|
||||
return wrapWhatsAppError(err)
|
||||
return wa.wrapSendError(err)
|
||||
}
|
||||
|
||||
func (wa *WhatsAppClient) HandleMatrixEdit(ctx context.Context, edit *bridgev2.MatrixEdit) (retErr error) {
|
||||
|
|
@ -288,7 +290,7 @@ func (wa *WhatsAppClient) HandleMatrixEdit(ctx context.Context, edit *bridgev2.M
|
|||
ID: editID,
|
||||
})
|
||||
log.Trace().Any("response", resp).Msg("WhatsApp edit response")
|
||||
return wrapWhatsAppError(err)
|
||||
return wa.wrapSendError(err)
|
||||
}
|
||||
|
||||
func (wa *WhatsAppClient) HandleMatrixMessageRemove(ctx context.Context, msg *bridgev2.MatrixMessageRemove) (retErr error) {
|
||||
|
|
@ -313,11 +315,11 @@ func (wa *WhatsAppClient) HandleMatrixMessageRemove(ctx context.Context, msg *br
|
|||
|
||||
resp, err := wa.Client.SendMessage(ctx, portalJID, revokeMessage, extra)
|
||||
log.Trace().Any("response", resp).Msg("WhatsApp delete response")
|
||||
return wrapWhatsAppError(err)
|
||||
return wa.wrapSendError(err)
|
||||
}
|
||||
|
||||
func (wa *WhatsAppClient) HandleMatrixReadReceipt(ctx context.Context, receipt *bridgev2.MatrixReadReceipt) (retErr error) {
|
||||
if !receipt.ReadUpTo.After(receipt.LastRead) {
|
||||
if receipt.Receipt.IsThread() || !receipt.ReadUpTo.After(receipt.LastRead) {
|
||||
return nil
|
||||
}
|
||||
if receipt.LastRead.IsZero() {
|
||||
|
|
@ -483,6 +485,8 @@ func (wa *WhatsAppClient) HandleMatrixMembership(ctx context.Context, msg *bridg
|
|||
return nil, wrapWhatsAppError(err)
|
||||
} else if len(resp) == 0 {
|
||||
return nil, fmt.Errorf("no response for participant change")
|
||||
} else if resp[0].Error == 403 && action == whatsmeow.ParticipantChangeAdd {
|
||||
return nil, ErrAddBlockedByPrivacy
|
||||
} else if resp[0].Error != 0 {
|
||||
return nil, fmt.Errorf("failed to change participant: code %d", resp[0].Error)
|
||||
}
|
||||
|
|
@ -501,6 +505,8 @@ func (wa *WhatsAppClient) HandleMatrixRoomName(ctx context.Context, msg *bridgev
|
|||
|
||||
if msg.Portal.RoomType == database.RoomTypeDM {
|
||||
return false, fmt.Errorf("cannot set room name for DM")
|
||||
} else if msg.Content.Name == "" {
|
||||
return false, ErrEmptyGroupName
|
||||
}
|
||||
|
||||
defer wa.mcTrack(msg, time.Now(), &retErr)
|
||||
|
|
|
|||
|
|
@ -237,7 +237,11 @@ func (wa *WhatsAppClient) handleWAEvent(rawEvt any) (success bool) {
|
|||
})
|
||||
wa.notifyOfflineSyncWaiter(fmt.Errorf("stream error: %s", message))
|
||||
case *events.StreamReplaced:
|
||||
wa.UserLogin.BridgeState.Send(status.BridgeState{StateEvent: status.StateUnknownError, Error: WAStreamReplaced})
|
||||
wa.UserLogin.BridgeState.Send(status.BridgeState{
|
||||
StateEvent: status.StateUnknownError,
|
||||
Error: WAStreamReplaced,
|
||||
BlockAutoReconnect: true,
|
||||
})
|
||||
wa.notifyOfflineSyncWaiter(fmt.Errorf("stream replaced"))
|
||||
case *events.KeepAliveTimeout:
|
||||
wa.UserLogin.BridgeState.Send(status.BridgeState{StateEvent: status.StateTransientDisconnect, Error: WAKeepaliveTimeout})
|
||||
|
|
@ -253,7 +257,11 @@ func (wa *WhatsAppClient) handleWAEvent(rawEvt any) (success bool) {
|
|||
wa.notifyOfflineSyncWaiter(fmt.Errorf("connection failure: %s (%s)", evt.Reason, evt.Message))
|
||||
case *events.ClientOutdated:
|
||||
wa.UserLogin.Log.Error().Msg("Got a client outdated connect failure. The bridge is likely out of date, please update immediately.")
|
||||
wa.UserLogin.BridgeState.Send(status.BridgeState{StateEvent: status.StateUnknownError, Error: WAClientOutdated})
|
||||
wa.UserLogin.BridgeState.Send(status.BridgeState{
|
||||
StateEvent: status.StateUnknownError,
|
||||
Error: WAClientOutdated,
|
||||
BlockAutoReconnect: true,
|
||||
})
|
||||
wa.notifyOfflineSyncWaiter(fmt.Errorf("client outdated"))
|
||||
case *events.TemporaryBan:
|
||||
wa.UserLogin.BridgeState.Send(status.BridgeState{
|
||||
|
|
@ -366,6 +374,14 @@ func (wa *WhatsAppClient) handleWAMessage(ctx context.Context, evt *events.Messa
|
|||
if assocType := messageAssoc.GetAssociationType(); assocType == waE2E.MessageAssociation_HD_IMAGE_DUAL_UPLOAD || assocType == waE2E.MessageAssociation_HD_VIDEO_DUAL_UPLOAD {
|
||||
parentKey := messageAssoc.GetParentMessageKey()
|
||||
protocolMsg, shouldHideEdit := makeHDMediaReplacementEdit(evt.Message, parentKey)
|
||||
if protocolMsg.GetEditedMessage() == nil {
|
||||
wa.UserLogin.Log.Debug().
|
||||
Str("message_id", evt.Info.ID).
|
||||
Str("parent_id", parentKey.GetID()).
|
||||
Stringer("assoc_type", assocType).
|
||||
Msg("Ignoring HD replacement message without replacement content")
|
||||
return
|
||||
}
|
||||
dontRenderEdited = shouldHideEdit
|
||||
wa.UserLogin.Log.Debug().
|
||||
Str("message_id", evt.Info.ID).
|
||||
|
|
@ -514,6 +530,10 @@ func (wa *WhatsAppClient) handleWAReceipt(ctx context.Context, evt *events.Recei
|
|||
} else if evt.Chat.Server == types.NewsletterServer {
|
||||
messageSender = evt.Chat
|
||||
}
|
||||
var messageSenderAlt types.JID
|
||||
if evt.MessageSender.IsEmpty() && evt.Chat.Server == types.BotServer {
|
||||
messageSenderAlt = wa.JID
|
||||
}
|
||||
var chatAlt types.JID
|
||||
if evt.Chat.Server == types.DefaultUserServer {
|
||||
chatLID, _ := wa.GetStore().LIDs.GetLIDForPN(ctx, evt.Chat)
|
||||
|
|
@ -527,6 +547,9 @@ func (wa *WhatsAppClient) handleWAReceipt(ctx context.Context, evt *events.Recei
|
|||
if !chatAlt.IsEmpty() {
|
||||
targets = append(targets, waid.MakeMessageID(chatAlt, messageSender, id))
|
||||
}
|
||||
if !messageSenderAlt.IsEmpty() {
|
||||
targets = append(targets, waid.MakeMessageID(evt.Chat, messageSenderAlt, id))
|
||||
}
|
||||
}
|
||||
senderLID := evt.Sender
|
||||
if senderLID.Server == types.DefaultUserServer && !evt.SenderAlt.IsEmpty() {
|
||||
|
|
@ -955,7 +978,9 @@ func (wa *WhatsAppClient) handleWAAppStateSyncComplete(ctx context.Context, evt
|
|||
defer wa.appStateRecoveryLock.Unlock()
|
||||
meta := wa.UserLogin.Metadata.(*waid.UserLoginMetadata)
|
||||
if ts, exists := meta.AppStateRecoveryAttempted[evt.Name]; exists {
|
||||
delete(wa.appStateFullSyncAttempted, evt.Name)
|
||||
if !evt.Recovery {
|
||||
delete(wa.appStateFullSyncAttempted, evt.Name)
|
||||
}
|
||||
delete(meta.AppStateRecoveryAttempted, evt.Name)
|
||||
err := wa.UserLogin.Save(ctx)
|
||||
if err != nil {
|
||||
|
|
@ -993,7 +1018,7 @@ func (wa *WhatsAppClient) handleWAAppStateSyncError(ctx context.Context, evt *ev
|
|||
return
|
||||
}
|
||||
if !evt.FullSync {
|
||||
if !lastFullSync.IsZero() {
|
||||
if !lastFullSync.IsZero() && time.Since(lastFullSync) < 48*time.Hour {
|
||||
log.Debug().
|
||||
Err(evt.Error).
|
||||
Time("last_full_sync_attempt", lastFullSync).
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@ package connector
|
|||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
|
|
@ -25,9 +26,11 @@ import (
|
|||
"go.mau.fi/util/dbutil"
|
||||
"go.mau.fi/whatsmeow/types"
|
||||
|
||||
"maunium.net/go/mautrix"
|
||||
"maunium.net/go/mautrix/bridgev2"
|
||||
"maunium.net/go/mautrix/bridgev2/networkid"
|
||||
"maunium.net/go/mautrix/event"
|
||||
"maunium.net/go/mautrix/id"
|
||||
|
||||
"go.mau.fi/mautrix-whatsapp/pkg/waid"
|
||||
)
|
||||
|
|
@ -47,6 +50,8 @@ func (wa *WhatsAppClient) FindAltTargetMessage(ctx context.Context, targetMsg ne
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else if parsed.Chat == types.NewMetaAIJID {
|
||||
altChat = types.MetaAIJID
|
||||
}
|
||||
if !altSender.IsEmpty() {
|
||||
altSenderID := *parsed
|
||||
|
|
@ -58,6 +63,8 @@ func (wa *WhatsAppClient) FindAltTargetMessage(ctx context.Context, targetMsg ne
|
|||
altChatID.Chat = altChat
|
||||
if altSender.Server == types.DefaultUserServer {
|
||||
altChatID.Sender = altSender
|
||||
} else if parsed.Sender == types.NewMetaAIJID {
|
||||
altChatID.Sender = types.MetaAIJID
|
||||
}
|
||||
alts = append(alts, altChatID.String())
|
||||
}
|
||||
|
|
@ -211,6 +218,30 @@ func (wa *WhatsAppConnector) migrateToLIDDMs(ctx context.Context) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (wa *WhatsAppConnector) migrateMetaAIPortals(ctx context.Context) error {
|
||||
if wa.Bridge.Background || wa.Bridge.DB.KV.Get(ctx, "whatsapp_meta_ai_portals_migrated") == "true" {
|
||||
return nil
|
||||
}
|
||||
log := zerolog.Ctx(ctx).With().Str("action", "migrate meta ai portals").Logger()
|
||||
const findMetaAIPortals = "SELECT id, receiver FROM portal WHERE bridge_id=$1 AND id=$2"
|
||||
portalKeys, err := scanPortalKey.NewRowIter(wa.Bridge.DB.Query(ctx, findMetaAIPortals, wa.Bridge.ID, waid.MakePortalID(types.MetaAIJID))).AsList()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get Meta AI portals: %w", err)
|
||||
}
|
||||
for _, key := range portalKeys {
|
||||
res, err := wa.reIDPhoneDMToLID(ctx, types.MetaAIJID, types.NewMetaAIJID, key.Receiver)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to re-ID Meta AI portal for %s: %w", key.Receiver, err)
|
||||
}
|
||||
log.Info().
|
||||
Str("receiver", string(key.Receiver)).
|
||||
Stringer("result", res).
|
||||
Msg("Re-ID'd Meta AI phone number portal")
|
||||
}
|
||||
wa.Bridge.DB.KV.Set(ctx, "whatsapp_meta_ai_portals_migrated", "true")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (wa *WhatsAppConnector) syncMismatchingGhosts(ctx context.Context) error {
|
||||
if wa.Bridge.Background || wa.Bridge.DB.KV.Get(ctx, "whatsapp_lid_avatars_resynced") == "true" {
|
||||
return nil
|
||||
|
|
@ -267,3 +298,84 @@ func (wa *WhatsAppConnector) syncMismatchingGhosts(ctx context.Context) error {
|
|||
wa.Bridge.DB.KV.Set(ctx, "whatsapp_lid_avatars_resynced", "true")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (wa *WhatsAppConnector) removeRejoinedPNGhosts(ctx context.Context) error {
|
||||
if wa.Bridge.Background || wa.Bridge.DB.KV.Get(ctx, "whatsapp_lid_dm_pn_ghosts_removed_v2") == "true" {
|
||||
return nil
|
||||
}
|
||||
stateGetter, ok := wa.Bridge.Matrix.(bridgev2.MatrixConnectorWithArbitraryRoomState)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
log := zerolog.Ctx(ctx).With().Str("action", "remove rejoined pn ghosts").Logger()
|
||||
const findLIDPortals = "SELECT id, mxid FROM portal WHERE bridge_id=$1 AND room_type='dm' AND id LIKE '%@lid' AND mxid IS NOT NULL"
|
||||
lidPortals, err := scanLIDPortal.NewRowIter(wa.Bridge.DB.Query(ctx, findLIDPortals, wa.Bridge.ID)).AsList()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get LID portals: %w", err)
|
||||
}
|
||||
var removed, failed int
|
||||
for _, portal := range lidPortals {
|
||||
lid, err := waid.ParsePortalID(portal.ID)
|
||||
if err != nil || lid.Server != types.HiddenUserServer {
|
||||
continue
|
||||
}
|
||||
pn, err := wa.DeviceStore.LIDMap.GetPNForLID(ctx, lid)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get PN for LID portal %s: %w", portal.ID, err)
|
||||
} else if pn.IsEmpty() {
|
||||
continue
|
||||
}
|
||||
pnUserID := waid.MakeUserID(pn)
|
||||
memberEvt, err := stateGetter.GetStateEvent(ctx, portal.MXID, event.StateMember, wa.Bridge.Matrix.FormatGhostMXID(pnUserID).String())
|
||||
if errors.Is(err, mautrix.MNotFound) || errors.Is(err, mautrix.MForbidden) {
|
||||
continue
|
||||
} else if err != nil {
|
||||
log.Warn().Err(err).Stringer("room_id", portal.MXID).Msg("Failed to get PN ghost membership")
|
||||
failed++
|
||||
continue
|
||||
} else if memberEvt == nil || memberEvt.Content.AsMember().Membership != event.MembershipJoin {
|
||||
continue
|
||||
}
|
||||
pnGhost, err := wa.Bridge.GetGhostByID(ctx, pnUserID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get PN ghost for %s: %w", pn, err)
|
||||
}
|
||||
_, err = pnGhost.Intent.SendState(ctx, portal.MXID, event.StateMember, pnGhost.Intent.GetMXID().String(), &event.Content{
|
||||
Parsed: &event.MemberEventContent{Membership: event.MembershipLeave, Reason: "Migrating to LIDs"},
|
||||
Raw: map[string]any{"com.beeper.exclude_from_timeline": true},
|
||||
}, time.Time{})
|
||||
if err != nil {
|
||||
log.Err(err).Stringer("room_id", portal.MXID).Stringer("pn", pn).Msg("Failed to remove PN ghost from LID DM")
|
||||
if !errors.Is(err, mautrix.MForbidden) && !errors.Is(err, mautrix.MNotFound) {
|
||||
failed++
|
||||
}
|
||||
continue
|
||||
}
|
||||
log.Debug().
|
||||
Stringer("room_id", portal.MXID).
|
||||
Stringer("pn", pn).
|
||||
Stringer("join_sender", memberEvt.Sender).
|
||||
Time("join_ts", time.UnixMilli(memberEvt.Timestamp)).
|
||||
Msg("Removed PN ghost from LID DM")
|
||||
removed++
|
||||
}
|
||||
log.Info().
|
||||
Int("removed_ghosts", removed).
|
||||
Int("failed", failed).
|
||||
Int("total_lid_portals", len(lidPortals)).
|
||||
Msg("Finished removing rejoined PN ghosts from LID DMs")
|
||||
if failed == 0 && ctx.Err() == nil {
|
||||
wa.Bridge.DB.KV.Set(ctx, "whatsapp_lid_dm_pn_ghosts_removed_v2", "true")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type lidPortal struct {
|
||||
ID networkid.PortalID
|
||||
MXID id.RoomID
|
||||
}
|
||||
|
||||
var scanLIDPortal = dbutil.ConvertRowFn[lidPortal](func(row dbutil.Scannable) (portal lidPortal, err error) {
|
||||
err = row.Scan(&portal.ID, &portal.MXID)
|
||||
return
|
||||
})
|
||||
|
|
|
|||
|
|
@ -137,6 +137,7 @@ var (
|
|||
_ bridgev2.LoginProcessDisplayAndWait = (*WALogin)(nil)
|
||||
_ bridgev2.LoginProcessUserInput = (*WALogin)(nil)
|
||||
_ bridgev2.LoginProcessWithOverride = (*WALogin)(nil)
|
||||
_ bridgev2.LoginProcessWithParams = (*WALogin)(nil)
|
||||
_ bridgev2.LoginProcessWebAuthn = (*WALogin)(nil)
|
||||
)
|
||||
|
||||
|
|
@ -144,6 +145,14 @@ const LoginConnectWait = 30 * time.Second
|
|||
const LoginPairPhoneWait = 30 * time.Second
|
||||
|
||||
func (wl *WALogin) Start(ctx context.Context) (*bridgev2.LoginStep, error) {
|
||||
return wl.StartWithParams(ctx, bridgev2.LoginStartParams{})
|
||||
}
|
||||
|
||||
func (wl *WALogin) StartWithOverride(ctx context.Context, old *bridgev2.UserLogin) (*bridgev2.LoginStep, error) {
|
||||
return wl.StartWithParams(ctx, bridgev2.LoginStartParams{Override: old})
|
||||
}
|
||||
|
||||
func (wl *WALogin) StartWithParams(ctx context.Context, params bridgev2.LoginStartParams) (*bridgev2.LoginStep, error) {
|
||||
wl.Main.firstClientConnectOnce.Do(wl.Main.onFirstClientConnect)
|
||||
device := wl.Main.DeviceStore.NewDevice()
|
||||
wl.Client = whatsmeow.NewClient(device, waLog.Zerolog(wl.Log))
|
||||
|
|
@ -153,8 +162,20 @@ func (wl *WALogin) Start(ctx context.Context) (*bridgev2.LoginStep, error) {
|
|||
if err := wl.Main.updateProxy(ctx, wl.Client, true); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if params.WebSocketHTTP != nil {
|
||||
wl.Client.SetPreLoginHTTPClient(&http.Client{Transport: params.WebSocketHTTP})
|
||||
}
|
||||
|
||||
if wl.PhoneCode {
|
||||
if params.Override != nil {
|
||||
phoneNumber := fmt.Sprintf("+%s", params.Override.ID)
|
||||
wl.Log.Debug().
|
||||
Str("phone_number", phoneNumber).
|
||||
Msg("Auto-submitting phone number for relogin")
|
||||
return wl.SubmitUserInput(ctx, map[string]string{
|
||||
"phone_number": phoneNumber,
|
||||
})
|
||||
}
|
||||
return &bridgev2.LoginStep{
|
||||
Type: bridgev2.LoginStepTypeUserInput,
|
||||
StepID: LoginStepIDPhoneNumber,
|
||||
|
|
@ -184,20 +205,6 @@ func (wl *WALogin) Start(ctx context.Context) (*bridgev2.LoginStep, error) {
|
|||
return makeQRStep(wl.QRs[0]), nil
|
||||
}
|
||||
|
||||
func (wl *WALogin) StartWithOverride(ctx context.Context, old *bridgev2.UserLogin) (*bridgev2.LoginStep, error) {
|
||||
step, err := wl.Start(ctx)
|
||||
if err == nil && step != nil && old != nil && step.StepID == LoginStepIDPhoneNumber {
|
||||
phoneNumber := fmt.Sprintf("+%s", old.ID)
|
||||
wl.Log.Debug().
|
||||
Str("phone_number", phoneNumber).
|
||||
Msg("Auto-submitting phone number for relogin")
|
||||
return wl.SubmitUserInput(ctx, map[string]string{
|
||||
"phone_number": phoneNumber,
|
||||
})
|
||||
}
|
||||
return step, err
|
||||
}
|
||||
|
||||
func (wl *WALogin) SubmitUserInput(ctx context.Context, input map[string]string) (*bridgev2.LoginStep, error) {
|
||||
err := wl.Client.Connect()
|
||||
if err != nil {
|
||||
|
|
@ -281,6 +288,7 @@ func (wl *WALogin) handleEvent(rawEvt any) {
|
|||
wl.QRs[i] = strings.Replace(code, evt.OldSecret, evt.NewSecret, 1)
|
||||
}
|
||||
wl.ADVRotate.Set()
|
||||
return
|
||||
case *events.QRScannedWithoutMultidevice:
|
||||
wl.Log.Error().Msg("QR code scanned without multidevice enabled")
|
||||
wl.LoginError = ErrLoginMultideviceNotEnabled
|
||||
|
|
@ -316,6 +324,7 @@ func (wl *WALogin) handleEvent(rawEvt any) {
|
|||
wl.LoginError = ErrUnexpectedDisconnect
|
||||
case *events.ManualLoginReconnect:
|
||||
wl.Received515.Set()
|
||||
return
|
||||
default:
|
||||
wl.Log.Warn().Type("event_type", evt).Msg("Got unexpected event")
|
||||
return
|
||||
|
|
@ -364,9 +373,14 @@ func (wl *WALogin) Wait(ctx context.Context) (*bridgev2.LoginStep, error) {
|
|||
case <-wl.PasskeyRequest.GetChan():
|
||||
return wl.makePasskeyStep()
|
||||
case <-wl.ADVRotate.GetChan():
|
||||
wl.Log.Debug().Msg("ADV secret was rotated, returning new QR immediately")
|
||||
wl.ADVRotate.Clear()
|
||||
return makeQRStep(wl.QRs[nextIndex]), nil
|
||||
if currentIndex < 0 || currentIndex >= len(wl.QRs) {
|
||||
wl.Log.Debug().Msg("ADV secret was rotated, but no QRs are left to return")
|
||||
wl.Cancel()
|
||||
return nil, ErrLoginTimeout
|
||||
}
|
||||
wl.Log.Debug().Msg("ADV secret was rotated, returning current QR immediately")
|
||||
return makeQRStep(wl.QRs[currentIndex]), nil
|
||||
case <-wl.LoginComplete.GetChan():
|
||||
// continue
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@ import (
|
|||
"context"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"runtime/debug"
|
||||
"time"
|
||||
|
|
@ -57,9 +56,9 @@ func (wa *WhatsAppClient) resyncWASARootSecrets(ctx context.Context) {
|
|||
success := true
|
||||
if err := wa.Client.FetchAppState(ctx, appstate.WAPatchRegularHigh, true, false); err != nil {
|
||||
log.Err(err).Msg("Failed to resync WASA root secrets")
|
||||
// Mark the sync as completed on mismatching LTHash errors, because those can get permanently stuck
|
||||
// (retrying won't help). In case it's not stuck, a single call should be enough to request a resync.
|
||||
if !errors.Is(err, appstate.ErrMismatchingLTHash) || ctx.Err() != nil {
|
||||
// Mark the sync as completed on errors too, because some get permanently stuck (mismatching LTHash,
|
||||
// expired patch blobs), and a failed full sync already requests app state recovery from the phone.
|
||||
if ctx.Err() != nil {
|
||||
return
|
||||
}
|
||||
success = false
|
||||
|
|
|
|||
|
|
@ -54,23 +54,38 @@ func (wa *WhatsAppConnector) getProxy(ctx context.Context, reason string) (strin
|
|||
}
|
||||
|
||||
func (wa *WhatsAppConnector) updateProxy(ctx context.Context, client *whatsmeow.Client, isLogin bool) error {
|
||||
if wa.Config.ProxyOnlyLogin && !isLogin {
|
||||
return nil
|
||||
var proxy string
|
||||
if !wa.Config.ProxyOnlyLogin || isLogin {
|
||||
reason := "connect"
|
||||
if isLogin {
|
||||
reason = "login"
|
||||
}
|
||||
var err error
|
||||
proxy, err = wa.getProxy(ctx, reason)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get proxy address: %w", err)
|
||||
}
|
||||
}
|
||||
reason := "connect"
|
||||
if isLogin {
|
||||
reason = "login"
|
||||
settings := wa.Bridge.GetHTTPClientSettings().WithGlobalTimeout(0)
|
||||
if settings.HTTPProxy == nil {
|
||||
settings.HTTPProxy = http.ProxyFromEnvironment
|
||||
}
|
||||
if proxy, err := wa.getProxy(ctx, reason); err != nil {
|
||||
return fmt.Errorf("failed to get proxy address: %w", err)
|
||||
} else if proxy == "" {
|
||||
return nil
|
||||
} else if err = client.SetProxyAddress(proxy, whatsmeow.SetProxyOptions{
|
||||
OnlyLogin: wa.Config.ProxyOnlyLogin,
|
||||
NoMedia: wa.Config.ProxyOnlyLogin,
|
||||
}); err != nil {
|
||||
return fmt.Errorf("failed to set proxy address: %w", err)
|
||||
proxySettings := settings
|
||||
if proxy != "" {
|
||||
var err error
|
||||
proxySettings, err = settings.WithProxy(proxy)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to set proxy address: %w", err)
|
||||
}
|
||||
}
|
||||
client.SetPreLoginHTTPClient(proxySettings.Compile())
|
||||
if wa.Config.ProxyOnlyLogin {
|
||||
proxySettings = settings
|
||||
}
|
||||
client.SetWebsocketHTTPClient(proxySettings.Compile())
|
||||
client.SetMediaHTTPClient(proxySettings.WithResponseHeaderTimeout(0).Compile())
|
||||
if proxy != "" {
|
||||
zerolog.Ctx(ctx).Debug().Msg("Enabled proxy")
|
||||
}
|
||||
zerolog.Ctx(ctx).Debug().Msg("Enabled proxy")
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -29,12 +29,15 @@ var ResyncLoopInterval = 4 * time.Hour
|
|||
var ResyncJitterSeconds = 3600
|
||||
|
||||
func (wa *WhatsAppClient) EnqueueGhostResync(ghost *bridgev2.Ghost) {
|
||||
jid := waid.ParseUserID(ghost.ID)
|
||||
if jid.Server == types.BotServer {
|
||||
return
|
||||
}
|
||||
lastSync := ghost.Metadata.(*waid.GhostMetadata).LastSync.Time
|
||||
if lastSync.Add(ResyncMinInterval).After(time.Now()) {
|
||||
if ghost.AvatarID != "" && lastSync.Add(ResyncMinInterval).After(time.Now()) {
|
||||
return
|
||||
}
|
||||
wa.resyncQueueLock.Lock()
|
||||
jid := waid.ParseUserID(ghost.ID)
|
||||
if _, exists := wa.resyncQueue[jid]; !exists {
|
||||
wa.resyncQueue[jid] = resyncQueueItem{ghost: ghost}
|
||||
nextResyncIn := time.Until(wa.nextResync).String()
|
||||
|
|
@ -116,14 +119,17 @@ func (wa *WhatsAppClient) doGhostResync(ctx context.Context, queue map[types.JID
|
|||
var ghostJIDs []types.JID
|
||||
var ghosts []*bridgev2.Ghost
|
||||
var portals []*bridgev2.Portal
|
||||
selectedGhosts := make(map[types.JID]*bridgev2.Ghost)
|
||||
for jid, item := range queue {
|
||||
var lastSync time.Time
|
||||
unknownAvatar := false
|
||||
if item.ghost != nil {
|
||||
lastSync = item.ghost.Metadata.(*waid.GhostMetadata).LastSync.Time
|
||||
unknownAvatar = item.ghost.AvatarID == ""
|
||||
} else if item.portal != nil {
|
||||
lastSync = item.portal.Metadata.(*waid.PortalMetadata).LastSync.Time
|
||||
}
|
||||
if lastSync.Add(ResyncMinInterval).After(time.Now()) {
|
||||
if !unknownAvatar && lastSync.Add(ResyncMinInterval).After(time.Now()) {
|
||||
log.Debug().
|
||||
Stringer("jid", jid).
|
||||
Time("last_sync", lastSync).
|
||||
|
|
@ -131,12 +137,23 @@ func (wa *WhatsAppClient) doGhostResync(ctx context.Context, queue map[types.JID
|
|||
continue
|
||||
}
|
||||
if item.ghost != nil {
|
||||
ghosts = append(ghosts, item.ghost)
|
||||
ghostJIDs = append(ghostJIDs, jid)
|
||||
selectedGhosts[jid] = item.ghost
|
||||
} else if item.portal != nil {
|
||||
portals = append(portals, item.portal)
|
||||
}
|
||||
}
|
||||
for jid, ghost := range selectedGhosts {
|
||||
if jid.Server == types.DefaultUserServer {
|
||||
lid, err := wa.GetStore().LIDs.GetLIDForPN(ctx, jid)
|
||||
if err != nil {
|
||||
log.Warn().Err(err).Stringer("jid", jid).Msg("Failed to get LID for background sync")
|
||||
} else if _, lidSelected := selectedGhosts[lid]; lidSelected {
|
||||
continue
|
||||
}
|
||||
}
|
||||
ghosts = append(ghosts, ghost)
|
||||
ghostJIDs = append(ghostJIDs, jid)
|
||||
}
|
||||
for _, portal := range portals {
|
||||
wa.UserLogin.QueueRemoteEvent(&simplevent.ChatResync{
|
||||
EventMeta: simplevent.EventMeta{
|
||||
|
|
@ -165,7 +182,7 @@ func (wa *WhatsAppClient) doGhostResync(ctx context.Context, queue map[types.JID
|
|||
log.Warn().Stringer("jid", jid).Msg("Didn't get info for puppet in background sync")
|
||||
continue
|
||||
}
|
||||
userInfo, err := wa.getUserInfo(ctx, jid, info.PictureID, info.PictureID != "" && string(ghost.AvatarID) != info.PictureID)
|
||||
userInfo, err := wa.getUserInfo(ctx, jid, info.PictureID, ghost.AvatarID == "" || (info.PictureID != "" && string(ghost.AvatarID) != info.PictureID))
|
||||
if err != nil {
|
||||
log.Err(err).Stringer("jid", jid).Msg("Failed to get user info for puppet in background sync")
|
||||
continue
|
||||
|
|
@ -472,16 +489,19 @@ func (wa *WhatsAppClient) syncAltGhostWithInfo(ctx context.Context, jid types.JI
|
|||
}
|
||||
|
||||
func makeInfoFromGhost(ghost *bridgev2.Ghost) *bridgev2.UserInfo {
|
||||
return &bridgev2.UserInfo{
|
||||
Identifiers: ghost.Identifiers,
|
||||
Name: &ghost.Name,
|
||||
Avatar: &bridgev2.Avatar{
|
||||
ID: ghost.AvatarID,
|
||||
Remove: ghost.AvatarID == "" || ghost.AvatarMXC == "",
|
||||
MXC: ghost.AvatarMXC,
|
||||
Hash: ghost.AvatarHash,
|
||||
},
|
||||
info := &bridgev2.UserInfo{
|
||||
Identifiers: ghost.Identifiers,
|
||||
Name: &ghost.Name,
|
||||
IsBot: &ghost.IsBot,
|
||||
ExtraProfile: ghost.ExtraProfile,
|
||||
}
|
||||
if ghost.AvatarID != "" && ghost.AvatarID != "unauthorized" {
|
||||
info.Avatar = &bridgev2.Avatar{
|
||||
ID: ghost.AvatarID,
|
||||
Remove: ghost.AvatarMXC == "",
|
||||
MXC: ghost.AvatarMXC,
|
||||
Hash: ghost.AvatarHash,
|
||||
}
|
||||
}
|
||||
return info
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
-- v0 -> v10 (compatible with v3+): Latest revision
|
||||
-- v0 -> v11 (compatible with v3+): Latest revision
|
||||
|
||||
CREATE TABLE whatsapp_poll_option_id (
|
||||
bridge_id TEXT NOT NULL,
|
||||
|
|
|
|||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Reference in a new issue