client: make store access safer

This commit is contained in:
Tulir Asokan 2025-03-06 15:53:29 +02:00
parent 587b1c1cfd
commit 9ec53dbbef
7 changed files with 19 additions and 9 deletions

2
go.mod
View file

@ -12,7 +12,7 @@ require (
github.com/tidwall/gjson v1.18.0
go.mau.fi/util v0.8.5
go.mau.fi/webp v0.2.0
go.mau.fi/whatsmeow v0.0.0-20250305175604-af3dc0346412
go.mau.fi/whatsmeow v0.0.0-20250306135213-ae5c492c5067
golang.org/x/image v0.24.0
golang.org/x/net v0.35.0
golang.org/x/sync v0.11.0

4
go.sum
View file

@ -70,8 +70,8 @@ go.mau.fi/util v0.8.5 h1:PwCAAtcfK0XxZ4sdErJyfBMkTEWoQU33aB7QqDDzQRI=
go.mau.fi/util v0.8.5/go.mod h1:Ycug9mrbztlahHPEJ6H5r8Nu/xqZaWbE5vPHVWmfz6M=
go.mau.fi/webp v0.2.0 h1:QVMenHw7JDb4vall5sV75JNBQj9Hw4u8AKbi1QetHvg=
go.mau.fi/webp v0.2.0/go.mod h1:VSg9MyODn12Mb5pyG0NIyNFhujrmoFSsZBs8syOZD1Q=
go.mau.fi/whatsmeow v0.0.0-20250305175604-af3dc0346412 h1:AM+t3vKEho3zTDOW2g6KvxB7iGNPwp0SFZpmx4slVVU=
go.mau.fi/whatsmeow v0.0.0-20250305175604-af3dc0346412/go.mod h1:6hRrUtDWI2wTRClOd6m17GwrFE2a8/p5R4pjJsIVn+U=
go.mau.fi/whatsmeow v0.0.0-20250306135213-ae5c492c5067 h1:ScfoS96E8XvYp/FWHK+iN2TCifR5gTbkkltQ9bYDCRQ=
go.mau.fi/whatsmeow v0.0.0-20250306135213-ae5c492c5067/go.mod h1:6hRrUtDWI2wTRClOd6m17GwrFE2a8/p5R4pjJsIVn+U=
go.mau.fi/zeroconfig v0.1.3 h1:As9wYDKmktjmNZW5i1vn8zvJlmGKHeVxHVIBMXsm4kM=
go.mau.fi/zeroconfig v0.1.3/go.mod h1:NcSJkf180JT+1IId76PcMuLTNa1CzsFFZ0nBygIQM70=
golang.org/x/crypto v0.33.0 h1:IOBPskki6Lysi0lo9qQvbxiQ+FvsCC/YWOecCHAixus=

View file

@ -87,7 +87,7 @@ func updateDisappearingTimerSetAt(ts int64) bridgev2.ExtraUpdater[*bridgev2.Port
}
func (wa *WhatsAppClient) applyChatSettings(ctx context.Context, chatID types.JID, info *bridgev2.ChatInfo) {
chat, err := wa.Client.Store.ChatSettings.GetChatSettings(chatID)
chat, err := wa.GetStore().ChatSettings.GetChatSettings(chatID)
if err != nil {
zerolog.Ctx(ctx).Warn().Err(err).Msg("Failed to get chat settings")
return

View file

@ -280,6 +280,16 @@ func (wa *WhatsAppClient) startLoops() {
}
}
func (wa *WhatsAppClient) GetStore() *store.Device {
if cli := wa.Client; cli != nil {
if currentStore := cli.Store; currentStore != nil {
return currentStore
}
}
wa.UserLogin.Log.Warn().Caller(1).Msg("Returning noop device in GetStore")
return store.NoopDevice
}
func (wa *WhatsAppClient) Disconnect() {
if stopHistorySyncLoop := wa.stopLoops.Swap(nil); stopHistorySyncLoop != nil {
(*stopHistorySyncLoop)()

View file

@ -125,7 +125,7 @@ func (wa *WhatsAppClient) handleWAEvent(rawEvt any) {
go wa.handleWAPictureUpdate(evt)
case *events.AppStateSyncComplete:
if len(wa.Client.Store.PushName) > 0 && evt.Name == appstate.WAPatchCriticalBlock {
if len(wa.GetStore().PushName) > 0 && evt.Name == appstate.WAPatchCriticalBlock {
err := wa.Client.SendPresence(types.PresenceUnavailable)
if err != nil {
log.Warn().Err(err).Msg("Failed to send presence after app state sync")
@ -142,7 +142,7 @@ func (wa *WhatsAppClient) handleWAEvent(rawEvt any) {
if err != nil {
log.Warn().Err(err).Msg("Failed to send presence after push name update")
}
_, _, err = wa.Client.Store.Contacts.PutPushName(wa.JID.ToNonAD(), evt.Action.GetName())
_, _, err = wa.GetStore().Contacts.PutPushName(wa.JID.ToNonAD(), evt.Action.GetName())
if err != nil {
log.Err(err).Msg("Failed to update push name in store")
}
@ -157,7 +157,7 @@ func (wa *WhatsAppClient) handleWAEvent(rawEvt any) {
case *events.Connected:
log.Debug().Msg("Connected to WhatsApp socket")
wa.UserLogin.BridgeState.Send(status.BridgeState{StateEvent: status.StateConnected})
if len(wa.Client.Store.PushName) > 0 {
if len(wa.GetStore().PushName) > 0 {
go func() {
err := wa.Client.SendPresence(types.PresenceUnavailable)
if err != nil {

View file

@ -133,7 +133,7 @@ func (wa *WhatsAppClient) getContactList(ctx context.Context, filter string) ([]
if !wa.IsLoggedIn() {
return nil, mautrix.MForbidden.WithMessage("You must be logged in to list contacts")
}
contacts, err := wa.Client.Store.Contacts.GetAllContacts()
contacts, err := wa.GetStore().Contacts.GetAllContacts()
if err != nil {
return nil, err
}

View file

@ -169,7 +169,7 @@ func (wa *WhatsAppClient) GetUserInfo(ctx context.Context, ghost *bridgev2.Ghost
}
func (wa *WhatsAppClient) getUserInfo(ctx context.Context, jid types.JID, fetchAvatar bool) (*bridgev2.UserInfo, error) {
contact, err := wa.Client.Store.Contacts.GetContact(jid)
contact, err := wa.GetStore().Contacts.GetContact(jid)
if err != nil {
return nil, err
}