Clean up and fix some things

This commit is contained in:
Tulir Asokan 2021-10-29 16:50:29 +03:00
parent 4695ce450f
commit 3d384a6db7
6 changed files with 17 additions and 35 deletions

View file

@ -33,7 +33,6 @@ import (
type BridgeStateEvent string
const (
StateStarting BridgeStateEvent = "STARTING"
StateUnconfigured BridgeStateEvent = "UNCONFIGURED"
StateRunning BridgeStateEvent = "RUNNING"
StateConnecting BridgeStateEvent = "CONNECTING"
@ -51,20 +50,14 @@ const (
WANotLoggedIn BridgeErrorCode = "wa-logged-out"
WANotConnected BridgeErrorCode = "wa-not-connected"
WAConnecting BridgeErrorCode = "wa-connecting"
WATimeout BridgeErrorCode = "wa-timeout"
WAServerTimeout BridgeErrorCode = "wa-server-timeout"
WAPingFalse BridgeErrorCode = "wa-ping-false"
WAPingError BridgeErrorCode = "wa-ping-error"
)
var bridgeHumanErrors = map[BridgeErrorCode]string{
WANotLoggedIn: "You're not logged into WhatsApp",
WANotConnected: "You're not connected to WhatsApp",
WAConnecting: "Trying to reconnect to WhatsApp. Please make sure WhatsApp is running on your phone and connected to the internet.",
WATimeout: "WhatsApp on your phone is not responding. Please make sure it is running and connected to the internet.",
WAServerTimeout: "The WhatsApp web servers are not responding. The bridge will try to reconnect.",
WAPingFalse: "WhatsApp returned an error, reconnecting. Please make sure WhatsApp is running on your phone and connected to the internet.",
WAPingError: "WhatsApp returned an unknown error",
}
type BridgeState struct {
@ -89,7 +82,7 @@ type GlobalBridgeState struct {
func (pong BridgeState) fill(user *User) BridgeState {
if user != nil {
pong.UserID = user.MXID
pong.RemoteID = user.JID.String()
pong.RemoteID = fmt.Sprintf("%s_a%d_d%d", user.JID.User, user.JID.Agent, user.JID.Device)
pong.RemoteName = fmt.Sprintf("+%s", user.JID.User)
}

2
go.mod
View file

@ -8,7 +8,7 @@ require (
github.com/mattn/go-sqlite3 v1.14.9
github.com/prometheus/client_golang v1.11.0
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e
go.mau.fi/whatsmeow v0.0.0-20211028182103-c52e72e7c66f
go.mau.fi/whatsmeow v0.0.0-20211029134926-9c2dba8b03e0
golang.org/x/image v0.0.0-20210628002857-a66eb6448b8d
google.golang.org/protobuf v1.27.1
gopkg.in/yaml.v2 v2.4.0

4
go.sum
View file

@ -139,8 +139,8 @@ github.com/tidwall/sjson v1.2.3 h1:5+deguEhHSEjmuICXZ21uSSsXotWMA0orU783+Z7Cp8=
github.com/tidwall/sjson v1.2.3/go.mod h1:5WdjKx3AQMvCJ4RG6/2UYT7dLrGvJUV1x4jdTAyGvZs=
go.mau.fi/libsignal v0.0.0-20211024113310-f9fc6a1855f2 h1:xpQTMgJGGaF+c8jV/LA/FVXAPJxZbSAGeflOc+Ly6uQ=
go.mau.fi/libsignal v0.0.0-20211024113310-f9fc6a1855f2/go.mod h1:3XlVlwOfp8f9Wri+C1D4ORqgUsN4ZvunJOoPjQMBhos=
go.mau.fi/whatsmeow v0.0.0-20211028182103-c52e72e7c66f h1:/2pIpCW3g2D99UbPJWFwUwKFb1bxTycaueDsYqWWxNE=
go.mau.fi/whatsmeow v0.0.0-20211028182103-c52e72e7c66f/go.mod h1:ODEmmqeUn9eBDQHFc1S902YA3YFLtmaBujYRRFl53jI=
go.mau.fi/whatsmeow v0.0.0-20211029134926-9c2dba8b03e0 h1:czbbBkOdIld6u9ljeV2cvL6vTdDLgshfLStyDgmfhlc=
go.mau.fi/whatsmeow v0.0.0-20211029134926-9c2dba8b03e0/go.mod h1:ODEmmqeUn9eBDQHFc1S902YA3YFLtmaBujYRRFl53jI=
golang.org/x/crypto v0.0.0-20170930174604-9419663f5a44/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=

View file

@ -322,7 +322,6 @@ func (bridge *Bridge) Start() {
os.Exit(19)
}
}
bridge.sendGlobalBridgeState(BridgeState{StateEvent: StateStarting}.fill(nil))
if bridge.Provisioning != nil {
bridge.Log.Debugln("Initializing provisioning API")
bridge.Provisioning.Init()

View file

@ -1357,13 +1357,6 @@ func (portal *Portal) CreateMatrixRoom(user *User) error {
portal.FirstEventID = firstEventResp.EventID
portal.Update()
}
//user.CreateUserPortal(database.PortalKeyWithMeta{PortalKey: portal.Key, InCommunity: inCommunity})
//err = portal.FillInitialHistory(user)
//if err != nil {
// portal.log.Errorln("Failed to fill history:", err)
//}
return nil
}

29
user.go
View file

@ -227,6 +227,7 @@ func (user *User) Login(ctx context.Context) (<-chan whatsmeow.QRChannelItem, er
newSession := user.bridge.WAContainer.NewDevice()
newSession.Log = &waLogger{user.log.Sub("Session")}
user.Client = whatsmeow.NewClient(newSession, &waLogger{user.log.Sub("Client")})
user.Client.AddEventHandler(user.HandleEvent)
qrChan, err := user.Client.GetQRChannel(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get QR channel: %w", err)
@ -385,7 +386,7 @@ func (user *User) handleHistorySync(evt *waProto.HistorySync) {
func (user *User) HandleEvent(event interface{}) {
switch v := event.(type) {
case *events.LoggedOut:
go user.handleLoggedOut()
go user.handleLoggedOut(v.OnConnect)
user.bridge.Metrics.TrackConnectionState(user.JID, false)
user.bridge.Metrics.TrackLoginState(user.JID, false)
case *events.Connected:
@ -414,10 +415,10 @@ func (user *User) HandleEvent(event interface{}) {
user.log.Warnln("Failed to send presence after push name update:", err)
}
case *events.PairSuccess:
user.Session = user.Client.Store
user.JID = v.ID
user.addToJIDMap()
user.Update()
user.Session = user.Client.Store
case *events.ConnectFailure, *events.StreamError:
go user.sendBridgeState(BridgeState{StateEvent: StateUnknownError})
user.bridge.Metrics.TrackConnectionState(user.JID, false)
@ -445,7 +446,7 @@ func (user *User) HandleEvent(event interface{}) {
case *events.HistorySync:
user.historySyncs <- v
case *events.Mute:
portal := user.bridge.GetPortalByJID(user.PortalKey(v.JID))
portal := user.GetPortalByJID(v.JID)
if portal != nil {
var mutedUntil time.Time
if v.Action.GetMuted() {
@ -454,12 +455,12 @@ func (user *User) HandleEvent(event interface{}) {
go user.updateChatMute(nil, portal, mutedUntil)
}
case *events.Archive:
portal := user.bridge.GetPortalByJID(user.PortalKey(v.JID))
portal := user.GetPortalByJID(v.JID)
if portal != nil {
go user.updateChatTag(nil, portal, user.bridge.Config.Bridge.ArchiveTag, v.Action.GetArchived())
}
case *events.Pin:
portal := user.bridge.GetPortalByJID(user.PortalKey(v.JID))
portal := user.GetPortalByJID(v.JID)
if portal != nil {
go user.updateChatTag(nil, portal, user.bridge.Config.Bridge.PinnedTag, v.Action.GetPinned())
}
@ -619,19 +620,19 @@ func (user *User) UpdateDirectChats(chats map[id.UserID][]id.RoomID) {
}
}
func (user *User) handleLoggedOut() {
func (user *User) handleLoggedOut(onConnect bool) {
user.JID = types.EmptyJID
user.Update()
user.sendMarkdownBridgeAlert("Connecting to WhatsApp failed as the device was logged out. Please link the bridge to your phone again.")
if onConnect {
user.sendMarkdownBridgeAlert("Connecting to WhatsApp failed as the device was logged out. Please link the bridge to your phone again.")
} else {
user.sendMarkdownBridgeAlert("You were logged out from another device. Please link the bridge to your phone again.")
}
user.sendBridgeState(BridgeState{StateEvent: StateBadCredentials, Error: WANotLoggedIn})
}
func (user *User) PortalKey(jid types.JID) database.PortalKey {
return database.NewPortalKey(jid, user.JID)
}
func (user *User) GetPortalByJID(jid types.JID) *Portal {
return user.bridge.GetPortalByJID(user.PortalKey(jid))
return user.bridge.GetPortalByJID(database.NewPortalKey(jid, user.JID))
}
func (user *User) syncPuppet(jid types.JID) {
@ -782,7 +783,3 @@ func (user *User) handlePictureUpdate(evt *events.Picture) {
}
}
}
func (user *User) NeedsRelaybot(portal *Portal) bool {
return !user.HasSession() // || !user.IsInPortal(portal.Key)
}