Browse Source

Try to fix false positive send error on whatsapp mobile

Tulir Asokan 4 years ago
parent
commit
80b534323f
4 changed files with 13 additions and 9 deletions
  1. 1 1
      go.mod
  2. 2 0
      go.sum
  3. 2 1
      main.go
  4. 8 7
      portal.go

+ 1 - 1
go.mod

@@ -15,4 +15,4 @@ require (
 	maunium.net/go/mautrix v0.9.12
 	maunium.net/go/mautrix v0.9.12
 )
 )
 
 
-replace github.com/Rhymen/go-whatsapp => github.com/tulir/go-whatsapp v0.5.3
+replace github.com/Rhymen/go-whatsapp => github.com/tulir/go-whatsapp v0.5.4

+ 2 - 0
go.sum

@@ -470,6 +470,8 @@ github.com/tulir/go-whatsapp v0.5.2 h1:CJcFSAoD/vXybamd9Hfa67Otg11G4EfoIgciQakun
 github.com/tulir/go-whatsapp v0.5.2/go.mod h1:7J3IIL3bEQiBJGtiZst1N4PgXHlWIartdVQLe6lcx9A=
 github.com/tulir/go-whatsapp v0.5.2/go.mod h1:7J3IIL3bEQiBJGtiZst1N4PgXHlWIartdVQLe6lcx9A=
 github.com/tulir/go-whatsapp v0.5.3 h1:e3bPLCmbBF146l8tnswqxnVLtGCWOEruJSR4luySh6I=
 github.com/tulir/go-whatsapp v0.5.3 h1:e3bPLCmbBF146l8tnswqxnVLtGCWOEruJSR4luySh6I=
 github.com/tulir/go-whatsapp v0.5.3/go.mod h1:7J3IIL3bEQiBJGtiZst1N4PgXHlWIartdVQLe6lcx9A=
 github.com/tulir/go-whatsapp v0.5.3/go.mod h1:7J3IIL3bEQiBJGtiZst1N4PgXHlWIartdVQLe6lcx9A=
+github.com/tulir/go-whatsapp v0.5.4 h1:S99KYBzuoZ+3mfUDknxiKQEvotKQnzxrPhgoL+o8b/k=
+github.com/tulir/go-whatsapp v0.5.4/go.mod h1:7J3IIL3bEQiBJGtiZst1N4PgXHlWIartdVQLe6lcx9A=
 github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
 github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
 github.com/urfave/cli v1.22.1 h1:+mkCCcOFKPnCmVYVcURKps1Xe+3zP90gSYGNfRkjoIY=
 github.com/urfave/cli v1.22.1 h1:+mkCCcOFKPnCmVYVcURKps1Xe+3zP90gSYGNfRkjoIY=
 github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
 github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=

+ 2 - 1
main.go

@@ -17,6 +17,7 @@
 package main
 package main
 
 
 import (
 import (
+	"errors"
 	"fmt"
 	"fmt"
 	"os"
 	"os"
 	"os/signal"
 	"os/signal"
@@ -196,7 +197,7 @@ func (bridge *Bridge) ensureConnection() {
 	for {
 	for {
 		resp, err := bridge.Bot.Whoami()
 		resp, err := bridge.Bot.Whoami()
 		if err != nil {
 		if err != nil {
-			if httpErr, ok := err.(mautrix.HTTPError); ok && httpErr.RespError != nil && httpErr.RespError.ErrCode == "M_UNKNOWN_ACCESS_TOKEN" {
+			if errors.Is(err, mautrix.MUnknownToken) {
 				bridge.Log.Fatalln("Access token invalid. Is the registration installed in your homeserver correctly?")
 				bridge.Log.Fatalln("Access token invalid. Is the registration installed in your homeserver correctly?")
 				os.Exit(16)
 				os.Exit(16)
 			}
 			}

+ 8 - 7
portal.go

@@ -205,8 +205,8 @@ func (portal *Portal) syncDoublePuppetDetailsAfterCreate(source *User) {
 		return
 		return
 	}
 	}
 	source.syncChatDoublePuppetDetails(doublePuppet, Chat{
 	source.syncChatDoublePuppetDetails(doublePuppet, Chat{
-		Chat:    chat,
-		Portal:  portal,
+		Chat:   chat,
+		Portal: portal,
 	}, true)
 	}, true)
 }
 }
 
 
@@ -2003,7 +2003,7 @@ func (portal *Portal) convertMatrixMessage(sender *User, evt *event.Event) (*waP
 	}
 	}
 
 
 	ts := uint64(evt.Timestamp / 1000)
 	ts := uint64(evt.Timestamp / 1000)
-	status := waProto.WebMessageInfo_ERROR
+	status := waProto.WebMessageInfo_PENDING
 	fromMe := true
 	fromMe := true
 	info := &waProto.WebMessageInfo{
 	info := &waProto.WebMessageInfo{
 		Key: &waProto.MessageKey{
 		Key: &waProto.MessageKey{
@@ -2011,9 +2011,10 @@ func (portal *Portal) convertMatrixMessage(sender *User, evt *event.Event) (*waP
 			Id:        makeMessageID(),
 			Id:        makeMessageID(),
 			RemoteJid: &portal.Key.JID,
 			RemoteJid: &portal.Key.JID,
 		},
 		},
-		MessageTimestamp: &ts,
-		Message:          &waProto.Message{},
-		Status:           &status,
+		MessageTimestamp:    &ts,
+		MessageC2STimestamp: &ts,
+		Message:             &waProto.Message{},
+		Status:              &status,
 	}
 	}
 	ctxInfo := &waProto.ContextInfo{}
 	ctxInfo := &waProto.ContextInfo{}
 	replyToID := content.GetReplyTo()
 	replyToID := content.GetReplyTo()
@@ -2187,11 +2188,11 @@ func (portal *Portal) HandleMatrixMessage(sender *User, evt *event.Event) {
 		return
 		return
 	}
 	}
 	dbMsg := portal.markHandled(sender, info, evt.ID, false)
 	dbMsg := portal.markHandled(sender, info, evt.ID, false)
-	portal.log.Debugln("Sending event", evt.ID, "to WhatsApp", info.Key.GetId())
 	portal.sendRaw(sender, evt, info, dbMsg)
 	portal.sendRaw(sender, evt, info, dbMsg)
 }
 }
 
 
 func (portal *Portal) sendRaw(sender *User, evt *event.Event, info *waProto.WebMessageInfo, dbMsg *database.Message) {
 func (portal *Portal) sendRaw(sender *User, evt *event.Event, info *waProto.WebMessageInfo, dbMsg *database.Message) {
+	portal.log.Debugln("Sending event", evt.ID, "to WhatsApp", info.Key.GetId())
 	errChan := make(chan error, 1)
 	errChan := make(chan error, 1)
 	go sender.Conn.SendRaw(info, errChan)
 	go sender.Conn.SendRaw(info, errChan)