mirror of
https://github.com/42wim/matterbridge.git
synced 2024-11-21 18:22:00 -08:00
Refactor login logic (whatsapp)
This commit is contained in:
parent
69c74be7bb
commit
320c996a21
@ -74,11 +74,14 @@ func (b *Bwhatsapp) Connect() error {
|
|||||||
b.wc = whatsmeow.NewClient(device, waLog.Stdout("Client", "INFO", true))
|
b.wc = whatsmeow.NewClient(device, waLog.Stdout("Client", "INFO", true))
|
||||||
b.wc.AddEventHandler(b.eventHandler)
|
b.wc.AddEventHandler(b.eventHandler)
|
||||||
|
|
||||||
// No ID stored, new login
|
firstlogin := false
|
||||||
qrChan, err := b.wc.GetQRChannel(context.Background())
|
var qrChan <-chan whatsmeow.QRChannelItem
|
||||||
// This error means that we're already logged in, so ignore it.
|
if b.wc.Store.ID == nil {
|
||||||
if err != nil && !errors.Is(err, whatsmeow.ErrQRStoreContainsID) {
|
firstlogin = true
|
||||||
return errors.New("failed to to get QR channel:" + err.Error())
|
qrChan, err = b.wc.GetQRChannel(context.Background())
|
||||||
|
if err != nil && !errors.Is(err, whatsmeow.ErrQRStoreContainsID) {
|
||||||
|
return errors.New("failed to to get QR channel:" + err.Error())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
err = b.wc.Connect()
|
err = b.wc.Connect()
|
||||||
@ -86,12 +89,25 @@ func (b *Bwhatsapp) Connect() error {
|
|||||||
return errors.New("failed to connect to WhatsApp: " + err.Error())
|
return errors.New("failed to connect to WhatsApp: " + err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
for evt := range qrChan {
|
if b.wc.Store.ID == nil {
|
||||||
if evt.Event == "code" {
|
for evt := range qrChan {
|
||||||
// Render the QR code here
|
if evt.Event == "code" {
|
||||||
qrterminal.GenerateHalfBlock(evt.Code, qrterminal.L, os.Stdout)
|
qrterminal.GenerateHalfBlock(evt.Code, qrterminal.L, os.Stdout)
|
||||||
} else {
|
} else {
|
||||||
b.Log.Infof("QR channel result: %s", evt.Event)
|
b.Log.Infof("QR channel result: %s", evt.Event)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// disconnect and reconnect on our first login/pairing
|
||||||
|
// for some reason the GetJoinedGroups in JoinChannel doesn't work on first login
|
||||||
|
if firstlogin {
|
||||||
|
b.wc.Disconnect()
|
||||||
|
time.Sleep(time.Second)
|
||||||
|
|
||||||
|
err = b.wc.Connect()
|
||||||
|
if err != nil {
|
||||||
|
return errors.New("failed to connect to WhatsApp: " + err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -113,7 +129,6 @@ func (b *Bwhatsapp) Connect() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// get user avatar asynchronously
|
// get user avatar asynchronously
|
||||||
// go func() {
|
|
||||||
b.Log.Info("Getting user avatars..")
|
b.Log.Info("Getting user avatars..")
|
||||||
|
|
||||||
for jid := range b.users {
|
for jid := range b.users {
|
||||||
@ -122,13 +137,14 @@ func (b *Bwhatsapp) Connect() error {
|
|||||||
b.Log.Warnf("Could not get profile photo of %s: %v", jid, err)
|
b.Log.Warnf("Could not get profile photo of %s: %v", jid, err)
|
||||||
} else {
|
} else {
|
||||||
b.Lock()
|
b.Lock()
|
||||||
b.userAvatars[jid] = info.URL
|
if info != nil {
|
||||||
|
b.userAvatars[jid] = info.URL
|
||||||
|
}
|
||||||
b.Unlock()
|
b.Unlock()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
b.Log.Info("Finished getting avatars..")
|
b.Log.Info("Finished getting avatars..")
|
||||||
// }()
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user