Просмотр исходного кода

Add option to send read receipt on confirmed delivery to WhatsApp

Tulir Asokan 5 лет назад
Родитель
Сommit
c6b4f34c28
3 измененных файлов с 19 добавлено и 0 удалено
  1. 3 0
      config/bridge.go
  2. 4 0
      example-config.yaml
  3. 12 0
      portal.go

+ 3 - 0
config/bridge.go

@@ -37,6 +37,7 @@ type BridgeConfig struct {
 
 	ConnectionTimeout     int  `yaml:"connection_timeout"`
 	FetchMessageOnTimeout bool `yaml:"fetch_message_on_timeout"`
+	DeliveryReceipts      bool `yaml:"delivery_receipts"`
 	LoginQRRegenCount     int  `yaml:"login_qr_regen_count"`
 	MaxConnectionAttempts int  `yaml:"max_connection_attempts"`
 	ConnectionRetryDelay  int  `yaml:"connection_retry_delay"`
@@ -81,6 +82,8 @@ type BridgeConfig struct {
 
 func (bc *BridgeConfig) setDefaults() {
 	bc.ConnectionTimeout = 20
+	bc.FetchMessageOnTimeout = false
+	bc.DeliveryReceipts = false
 	bc.LoginQRRegenCount = 2
 	bc.MaxConnectionAttempts = 3
 	bc.ConnectionRetryDelay = -1

+ 4 - 0
example-config.yaml

@@ -77,6 +77,10 @@ bridge:
     # to see if it was actually bridged? Use this if you have problems with sends timing out but actually
     # succeeding.
     fetch_message_on_timeout: false
+    # Whether or not the bridge should send a read receipt from the bridge bot when a message has been
+    # sent to WhatsApp. If fetch_message_on_timeout is enabled, a successful post-timeout fetch will
+    # trigger a read receipt too.
+    delivery_receipts: false
     # Number of times to regenerate QR code when logging in.
     # The regenerated QR code is sent as an edit and essentially multiplies the login timeout (20 seconds)
     login_qr_regen_count: 2

+ 12 - 0
portal.go

@@ -1436,6 +1436,15 @@ func (portal *Portal) sendErrorMessage(sendErr error) id.EventID {
 	return resp.EventID
 }
 
+func (portal *Portal) sendDeliveryReceipt(eventID id.EventID) {
+	if portal.bridge.Config.Bridge.DeliveryReceipts {
+		err := portal.bridge.Bot.MarkRead(portal.MXID, eventID)
+		if err != nil {
+			portal.log.Debugfln("Failed to send delivery receipt for %s: %v", eventID, err)
+		}
+	}
+}
+
 var timeout = errors.New("message sending timed out")
 
 func (portal *Portal) HandleMatrixMessage(sender *User, evt *event.Event) {
@@ -1459,6 +1468,7 @@ func (portal *Portal) HandleMatrixMessage(sender *User, evt *event.Event) {
 	case <-time.After(time.Duration(portal.bridge.Config.Bridge.ConnectionTimeout) * time.Second):
 		if portal.bridge.Config.Bridge.FetchMessageOnTimeout && portal.wasMessageSent(sender, info.Key.GetId()) {
 			portal.log.Debugln("Matrix event %s was bridged, but response didn't arrive within timeout")
+			portal.sendDeliveryReceipt(evt.ID)
 		} else {
 			portal.log.Warnfln("Response when bridging Matrix event %s is taking long to arrive", evt.ID)
 			errorEventID = portal.sendErrorMessage(timeout)
@@ -1470,6 +1480,7 @@ func (portal *Portal) HandleMatrixMessage(sender *User, evt *event.Event) {
 		portal.sendErrorMessage(err)
 	} else {
 		portal.log.Debugfln("Handled Matrix event %s", evt.ID)
+		portal.sendDeliveryReceipt(evt.ID)
 	}
 	if errorEventID != "" {
 		_, err = portal.MainIntent().RedactEvent(portal.MXID, errorEventID)
@@ -1526,6 +1537,7 @@ func (portal *Portal) HandleMatrixRedaction(sender *User, evt *event.Event) {
 		portal.log.Errorfln("Error handling Matrix redaction %s: %v", evt.ID, err)
 	} else {
 		portal.log.Debugln("Handled Matrix redaction %s of %s", evt.ID, evt.Redacts)
+		portal.sendDeliveryReceipt(evt.ID)
 	}
 }