dbmeta: move types to waid package

This commit is contained in:
Tulir Asokan 2024-09-16 15:38:39 +03:00
parent 33a995413a
commit 2395e9b0f5
10 changed files with 63 additions and 57 deletions

View file

@ -205,7 +205,7 @@ func legacyProvLogin(w http.ResponseWriter, r *http.Request) {
// TODO delete old logins
_ = conn.WriteJSON(map[string]any{
"success": true,
"jid": waid.ParseUserLoginID(step.CompleteParams.UserLoginID, step.CompleteParams.UserLogin.Metadata.(*connector.UserLoginMetadata).WADeviceID).String(),
"jid": waid.ParseUserLoginID(step.CompleteParams.UserLoginID, step.CompleteParams.UserLogin.Metadata.(*waid.UserLoginMetadata).WADeviceID).String(),
"platform": step.CompleteParams.UserLogin.Client.(*connector.WhatsAppClient).Device.Platform,
"phone": step.CompleteParams.UserLogin.RemoteProfile.Phone,
})

View file

@ -54,7 +54,7 @@ func (wa *WhatsAppClient) GetChatInfo(ctx context.Context, portal *bridgev2.Port
func updateDisappearingTimerSetAt(ts int64) bridgev2.ExtraUpdater[*bridgev2.Portal] {
return func(_ context.Context, portal *bridgev2.Portal) bool {
meta := portal.Metadata.(*PortalMetadata)
meta := portal.Metadata.(*waid.PortalMetadata)
if meta.DisappearingTimerSetAt != ts {
meta.DisappearingTimerSetAt = ts
return true

View file

@ -23,7 +23,7 @@ func (wa *WhatsAppConnector) LoadUserLogin(_ context.Context, login *bridgev2.Us
}
login.Client = w
loginMetadata := login.Metadata.(*UserLoginMetadata)
loginMetadata := login.Metadata.(*waid.UserLoginMetadata)
if loginMetadata.WADeviceID == 0 {
return nil
}

View file

@ -1,61 +1,27 @@
package connector
import (
"go.mau.fi/util/jsontime"
"go.mau.fi/whatsmeow/types"
"maunium.net/go/mautrix/bridgev2/database"
"maunium.net/go/mautrix-whatsapp/pkg/waid"
)
func (wa *WhatsAppConnector) GetDBMetaTypes() database.MetaTypes {
return database.MetaTypes{
Ghost: func() any {
return &GhostMetadata{}
return &waid.GhostMetadata{}
},
Message: func() any {
return &MessageMetadata{}
return &waid.MessageMetadata{}
},
Reaction: func() any {
return &ReactionMetadata{}
return &waid.ReactionMetadata{}
},
Portal: func() any {
return &PortalMetadata{}
return &waid.PortalMetadata{}
},
UserLogin: func() any {
return &UserLoginMetadata{}
return &waid.UserLoginMetadata{}
},
}
}
type UserLoginMetadata struct {
WADeviceID uint16 `json:"wa_device_id"`
PhoneLastSeen jsontime.Unix `json:"phone_last_seen"`
PhoneLastPinged jsontime.Unix `json:"phone_last_pinged"`
Timezone string `json:"timezone"`
}
type MessageErrorType string
const (
MsgNoError MessageErrorType = ""
MsgErrDecryptionFailed MessageErrorType = "decryption_failed"
MsgErrMediaNotFound MessageErrorType = "media_not_found"
)
type MessageMetadata struct {
SenderDeviceID uint16 `json:"sender_device_id,omitempty"`
Error MessageErrorType `json:"error,omitempty"`
BroadcastListJID *types.JID `json:"broadcast_list_jid,omitempty"`
}
type ReactionMetadata struct {
SenderDeviceID uint16 `json:"sender_device_id,omitempty"`
}
type PortalMetadata struct {
DisappearingTimerSetAt int64 `json:"disappearing_timer_set_at,omitempty"`
}
type GhostMetadata struct {
AvatarFetchAttempted bool `json:"avatar_fetch_attempted,omitempty"`
LastSync jsontime.Unix `json:"last_sync,omitempty"`
}

View file

@ -141,7 +141,7 @@ func (evt *WAMessageEvent) GetReactionEmoji() (string, networkid.EmojiID) {
}
func (evt *WAMessageEvent) GetReactionDBMetadata() any {
return &ReactionMetadata{
return &waid.ReactionMetadata{
SenderDeviceID: evt.Info.Sender.Device,
}
}
@ -168,7 +168,7 @@ func (evt *WAMessageEvent) GetType() bridgev2.RemoteEventType {
}
func (evt *WAMessageEvent) HandleExisting(ctx context.Context, portal *bridgev2.Portal, intent bridgev2.MatrixAPI, existing []*database.Message) (bridgev2.UpsertResult, error) {
if existing[0].Metadata.(*MessageMetadata).Error == MsgErrDecryptionFailed {
if existing[0].Metadata.(*waid.MessageMetadata).Error == waid.MsgErrDecryptionFailed {
zerolog.Ctx(ctx).Debug().Stringer("existing_mxid", existing[0].MXID).Msg("Ignoring duplicate message")
evt.isUndecryptableUpsertSubEvent = true
return bridgev2.UpsertResult{SubEvents: []bridgev2.RemoteEvent{
@ -182,11 +182,11 @@ func (evt *WAMessageEvent) HandleExisting(ctx context.Context, portal *bridgev2.
func (evt *WAMessageEvent) ConvertMessage(ctx context.Context, portal *bridgev2.Portal, intent bridgev2.MatrixAPI) (*bridgev2.ConvertedMessage, error) {
converted := evt.wa.Main.MsgConv.ToMatrix(ctx, portal, evt.wa.Client, intent, evt.Message)
for _, part := range converted.Parts {
part.DBMetadata = &MessageMetadata{
part.DBMetadata = &waid.MessageMetadata{
SenderDeviceID: evt.Info.Sender.Device,
}
if evt.Info.IsIncomingBroadcast() {
part.DBMetadata.(*MessageMetadata).BroadcastListJID = &evt.Info.Chat
part.DBMetadata.(*waid.MessageMetadata).BroadcastListJID = &evt.Info.Chat
if part.Extra == nil {
part.Extra = map[string]any{}
}
@ -261,9 +261,9 @@ func (evt *WAUndecryptableMessage) ConvertMessage(ctx context.Context, portal *b
Type: event.EventMessage,
Content: &undecryptableMessageContent,
Extra: extra,
DBMetadata: &MessageMetadata{
DBMetadata: &waid.MessageMetadata{
SenderDeviceID: evt.Info.Sender.Device,
Error: MsgErrDecryptionFailed,
Error: waid.MsgErrDecryptionFailed,
BroadcastListJID: broadcastListJID,
},
}},

View file

@ -49,7 +49,7 @@ func (wa *WhatsAppClient) HandleMatrixMessage(ctx context.Context, msg *bridgev2
ID: wrappedMsgID,
SenderID: waid.MakeUserID(wa.JID),
Timestamp: resp.Timestamp,
Metadata: &MessageMetadata{
Metadata: &waid.MessageMetadata{
SenderDeviceID: wa.JID.Device,
},
},
@ -86,7 +86,7 @@ func (wa *WhatsAppClient) HandleMatrixReaction(ctx context.Context, msg *bridgev
resp, err := wa.Client.SendMessage(ctx, portalJID, reactionMsg)
zerolog.Ctx(ctx).Trace().Any("response", resp).Msg("WhatsApp reaction response")
return &database.Reaction{
Metadata: &ReactionMetadata{
Metadata: &waid.ReactionMetadata{
SenderDeviceID: wa.JID.Device,
},
}, err

View file

@ -163,7 +163,7 @@ func (wa *WhatsAppClient) handleWAEvent(rawEvt any) {
case *events.OfflineSyncCompleted:
if !wa.PhoneRecentlySeen(true) {
log.Info().
Time("phone_last_seen", wa.UserLogin.Metadata.(*UserLoginMetadata).PhoneLastSeen.Time).
Time("phone_last_seen", wa.UserLogin.Metadata.(*waid.UserLoginMetadata).PhoneLastSeen.Time).
Msg("Offline sync completed, but phone last seen date is still old - sending phone offline bridge status")
wa.UserLogin.BridgeState.Send(status.BridgeState{StateEvent: status.StateTransientDisconnect, Error: WAPhoneOffline})
} else {
@ -319,7 +319,7 @@ func (wa *WhatsAppClient) handleWALogout(reason events.ConnectFailureReason, onC
wa.Client.Disconnect()
wa.Client = nil
wa.JID = types.EmptyJID
wa.UserLogin.Metadata.(*UserLoginMetadata).WADeviceID = 0
wa.UserLogin.Metadata.(*waid.UserLoginMetadata).WADeviceID = 0
wa.UserLogin.BridgeState.Send(status.BridgeState{
StateEvent: status.StateBadCredentials,
Error: errorCode,

View file

@ -290,7 +290,7 @@ func (wl *WALogin) Wait(ctx context.Context) (*bridgev2.LoginStep, error) {
Phone: "+" + wl.LoginSuccess.ID.User,
Name: wl.LoginSuccess.BusinessName,
},
Metadata: &UserLoginMetadata{
Metadata: &waid.UserLoginMetadata{
WADeviceID: wl.LoginSuccess.ID.Device,
},
}, &bridgev2.NewLoginParams{

View file

@ -20,7 +20,7 @@ func (wa *WhatsAppClient) GetUserInfo(ctx context.Context, ghost *bridgev2.Ghost
if err != nil {
return nil, err
}
fetchAvatar := !ghost.Metadata.(*GhostMetadata).AvatarFetchAttempted
fetchAvatar := !ghost.Metadata.(*waid.GhostMetadata).AvatarFetchAttempted
return wa.contactToUserInfo(ctx, jid, contact, fetchAvatar), nil
}
@ -51,7 +51,7 @@ func (wa *WhatsAppClient) contactToUserInfo(ctx context.Context, jid types.JID,
}
func markAvatarFetchAttempted(_ context.Context, ghost *bridgev2.Ghost) bool {
meta := ghost.Metadata.(*GhostMetadata)
meta := ghost.Metadata.(*waid.GhostMetadata)
if !meta.AvatarFetchAttempted {
meta.AvatarFetchAttempted = true
return true

40
pkg/waid/dbmeta.go Normal file
View file

@ -0,0 +1,40 @@
package waid
import (
"go.mau.fi/util/jsontime"
"go.mau.fi/whatsmeow/types"
)
type UserLoginMetadata struct {
WADeviceID uint16 `json:"wa_device_id"`
PhoneLastSeen jsontime.Unix `json:"phone_last_seen"`
PhoneLastPinged jsontime.Unix `json:"phone_last_pinged"`
Timezone string `json:"timezone"`
}
type MessageErrorType string
const (
MsgNoError MessageErrorType = ""
MsgErrDecryptionFailed MessageErrorType = "decryption_failed"
MsgErrMediaNotFound MessageErrorType = "media_not_found"
)
type MessageMetadata struct {
SenderDeviceID uint16 `json:"sender_device_id,omitempty"`
Error MessageErrorType `json:"error,omitempty"`
BroadcastListJID *types.JID `json:"broadcast_list_jid,omitempty"`
}
type ReactionMetadata struct {
SenderDeviceID uint16 `json:"sender_device_id,omitempty"`
}
type PortalMetadata struct {
DisappearingTimerSetAt int64 `json:"disappearing_timer_set_at,omitempty"`
}
type GhostMetadata struct {
AvatarFetchAttempted bool `json:"avatar_fetch_attempted,omitempty"`
LastSync jsontime.Unix `json:"last_sync,omitempty"`
}