Watch
1
0
Fork
You've already forked mautrix-whatsapp
0
mirror of https://github.com/mautrix/whatsapp.git synced 2026-11-15 10:36:05 -05:00

Compare commits

..
5 changed files with 22 additions and 17 deletions

View file

@ -96,6 +96,16 @@ func (wa *WhatsAppClient) historySyncLoop(ctx context.Context) {
} }
if resetTimer { if resetTimer {
timerPending.Store(true) timerPending.Store(true)
wa.historySyncPortalLock.Lock()
wa.historySyncPortalGen++
if meta := wa.UserLogin.Metadata.(*waid.UserLoginMetadata); !meta.HistorySyncPortalsNeedCreating {
meta.HistorySyncPortalsNeedCreating = true
err := wa.UserLogin.Save(ctx)
if err != nil {
wa.UserLogin.Log.Err(err).Msg("Failed to save user login history sync portals need creating flag")
}
}
wa.historySyncPortalLock.Unlock()
} }
if timerPending.Load() { if timerPending.Load() {
dispatchTimer.Reset(wa.Main.Config.HistorySync.DispatchWait) dispatchTimer.Reset(wa.Main.Config.HistorySync.DispatchWait)
@ -405,8 +415,6 @@ func (wa *WhatsAppClient) handleWAHistorySync(
return nil return nil
} }
const historySyncQueuedTimeout = 10 * time.Minute
func (wa *WhatsAppClient) createPortalsFromHistorySync(ctx context.Context) { func (wa *WhatsAppClient) createPortalsFromHistorySync(ctx context.Context) {
log := wa.UserLogin.Log.With(). log := wa.UserLogin.Log.With().
Str("action", "create portals from history sync"). Str("action", "create portals from history sync").
@ -419,6 +427,9 @@ func (wa *WhatsAppClient) createPortalsFromHistorySync(ctx context.Context) {
} }
}() }()
ctx = log.WithContext(ctx) ctx = log.WithContext(ctx)
wa.historySyncPortalLock.Lock()
gen := wa.historySyncPortalGen
wa.historySyncPortalLock.Unlock()
limit := wa.Main.Config.HistorySync.MaxInitialConversations limit := wa.Main.Config.HistorySync.MaxInitialConversations
loginTS := wa.UserLogin.Metadata.(*waid.UserLoginMetadata).LoggedInAt loginTS := wa.UserLogin.Metadata.(*waid.UserLoginMetadata).LoggedInAt
conversations, err := wa.Main.DB.Conversation.GetRecent(ctx, wa.UserLogin.ID, limit, loginTS) conversations, err := wa.Main.DB.Conversation.GetRecent(ctx, wa.UserLogin.ID, limit, loginTS)
@ -450,9 +461,6 @@ func (wa *WhatsAppClient) createPortalsFromHistorySync(ctx context.Context) {
// We don't currently support new PSAs, so don't bother backfilling them either // We don't currently support new PSAs, so don't bother backfilling them either
wg.Done() wg.Done()
continue continue
} else if queuedAt, ok := wa.historySyncQueued.Get(conv.ChatJID); ok && time.Since(queuedAt) < historySyncQueuedTimeout {
wg.Done()
continue
} }
// TODO can the chat info fetch be avoided entirely? // TODO can the chat info fetch be avoided entirely?
select { select {
@ -493,7 +501,6 @@ func (wa *WhatsAppClient) createPortalsFromHistorySync(ctx context.Context) {
wg.Done() wg.Done()
continue continue
} }
wa.historySyncQueued.Set(conv.ChatJID, time.Now())
res := wa.UserLogin.QueueRemoteEvent(&simplevent.ChatResync{ res := wa.UserLogin.QueueRemoteEvent(&simplevent.ChatResync{
EventMeta: simplevent.EventMeta{ EventMeta: simplevent.EventMeta{
Type: bridgev2.RemoteEventChatResync, Type: bridgev2.RemoteEventChatResync,
@ -509,7 +516,6 @@ func (wa *WhatsAppClient) createPortalsFromHistorySync(ctx context.Context) {
if err != nil { if err != nil {
zerolog.Ctx(ctx).Err(err).Msg("Failed to mark conversation as bridged") zerolog.Ctx(ctx).Err(err).Msg("Failed to mark conversation as bridged")
} }
wa.historySyncQueued.Delete(conv.ChatJID)
wg.Done() wg.Done()
}, },
}, },
@ -517,7 +523,6 @@ func (wa *WhatsAppClient) createPortalsFromHistorySync(ctx context.Context) {
LatestMessageTS: conv.LastMessageTimestamp, LatestMessageTS: conv.LastMessageTimestamp,
}) })
if !res.Success { if !res.Success {
wa.historySyncQueued.Delete(conv.ChatJID)
log.Debug().Msg("Cancelling history sync portal creation loop") log.Debug().Msg("Cancelling history sync portal creation loop")
return return
} }
@ -525,7 +530,9 @@ func (wa *WhatsAppClient) createPortalsFromHistorySync(ctx context.Context) {
log.Info().Int("conversation_count", len(conversations)).Msg("Finished creating portals from history sync") log.Info().Int("conversation_count", len(conversations)).Msg("Finished creating portals from history sync")
go func() { go func() {
wg.Wait() wg.Wait()
if wa.historySyncQueued.Len() > 0 { wa.historySyncPortalLock.Lock()
defer wa.historySyncPortalLock.Unlock()
if wa.historySyncPortalGen != gen {
return return
} }
wa.UserLogin.Metadata.(*waid.UserLoginMetadata).HistorySyncPortalsNeedCreating = false wa.UserLogin.Metadata.(*waid.UserLoginMetadata).HistorySyncPortalsNeedCreating = false

View file

@ -50,7 +50,6 @@ func (wa *WhatsAppConnector) LoadUserLogin(ctx context.Context, login *bridgev2.
MC: noopMCInstance, MC: noopMCInstance,
historySyncWakeup: make(chan struct{}, 1), historySyncWakeup: make(chan struct{}, 1),
historySyncQueued: exsync.NewMap[types.JID, time.Time](),
resyncQueue: make(map[types.JID]resyncQueueItem), resyncQueue: make(map[types.JID]resyncQueueItem),
directMediaRetries: make(map[networkid.MessageID]*directMediaRetry), directMediaRetries: make(map[networkid.MessageID]*directMediaRetry),
mediaRetryLock: semaphore.NewWeighted(wa.Config.HistorySync.MediaRequests.MaxAsyncHandle), mediaRetryLock: semaphore.NewWeighted(wa.Config.HistorySync.MediaRequests.MaxAsyncHandle),
@ -115,7 +114,6 @@ type WhatsAppClient struct {
MC mClient MC mClient
historySyncWakeup chan struct{} historySyncWakeup chan struct{}
historySyncQueued *exsync.Map[types.JID, time.Time]
stopLoops atomic.Pointer[context.CancelFunc] stopLoops atomic.Pointer[context.CancelFunc]
resyncQueue map[types.JID]resyncQueueItem resyncQueue map[types.JID]resyncQueueItem
resyncQueueLock sync.Mutex resyncQueueLock sync.Mutex
@ -130,6 +128,9 @@ type WhatsAppClient struct {
lastPresence types.Presence lastPresence types.Presence
blocklistLock sync.Mutex blocklistLock sync.Mutex
historySyncPortalLock sync.Mutex
historySyncPortalGen uint64
disableNewsletter bool disableNewsletter bool
appStateRecoveryLock sync.Mutex appStateRecoveryLock sync.Mutex

View file

@ -1,4 +1,4 @@
-- v0 -> v12 (compatible with v3+): Latest revision -- v0 -> v11 (compatible with v3+): Latest revision
CREATE TABLE whatsapp_poll_option_id ( CREATE TABLE whatsapp_poll_option_id (
bridge_id TEXT NOT NULL, bridge_id TEXT NOT NULL,
@ -50,8 +50,6 @@ CREATE TABLE whatsapp_history_sync_message (
REFERENCES whatsapp_history_sync_conversation (bridge_id, user_login_id, chat_jid) ON UPDATE CASCADE ON DELETE CASCADE REFERENCES whatsapp_history_sync_conversation (bridge_id, user_login_id, chat_jid) ON UPDATE CASCADE ON DELETE CASCADE
); );
CREATE INDEX whatsapp_history_sync_message_chat_ts_idx ON whatsapp_history_sync_message (bridge_id, user_login_id, chat_jid, timestamp);
CREATE TABLE whatsapp_media_backfill_request ( CREATE TABLE whatsapp_media_backfill_request (
bridge_id TEXT NOT NULL, bridge_id TEXT NOT NULL,
user_login_id TEXT NOT NULL, user_login_id TEXT NOT NULL,

View file

@ -1,2 +0,0 @@
-- v12 (compatible with v3+): Add index for history sync message timestamp lookups
CREATE INDEX whatsapp_history_sync_message_chat_ts_idx ON whatsapp_history_sync_message (bridge_id, user_login_id, chat_jid, timestamp);

View file

@ -76,7 +76,8 @@ func (mc *MessageConverter) getBasicUserInfo(ctx context.Context, user types.JID
var pnJID types.JID var pnJID types.JID
if user.Server == types.DefaultUserServer { if user.Server == types.DefaultUserServer {
pnJID = user pnJID = user
} else if cli := getClient(ctx); user.Server == types.HiddenUserServer && cli != nil { } else if user.Server == types.HiddenUserServer {
cli := getClient(ctx)
if user.User == cli.Store.GetLID().User { if user.User == cli.Store.GetLID().User {
pnJID = cli.Store.GetJID() pnJID = cli.Store.GetJID()
} else { } else {