aboutsummaryrefslogtreecommitdiffstats
path: root/modules/webhook/matrix.go
diff options
context:
space:
mode:
Diffstat (limited to 'modules/webhook/matrix.go')
-rw-r--r--modules/webhook/matrix.go21
1 files changed, 20 insertions, 1 deletions
diff --git a/modules/webhook/matrix.go b/modules/webhook/matrix.go
index 68c65623f7..d6309000a8 100644
--- a/modules/webhook/matrix.go
+++ b/modules/webhook/matrix.go
@@ -5,6 +5,7 @@
package webhook
import (
+ "crypto/sha1"
"encoding/json"
"errors"
"fmt"
@@ -291,7 +292,14 @@ func getMatrixHookRequest(t *models.HookTask) (*http.Request, error) {
}
t.PayloadContent = string(payload)
- req, err := http.NewRequest("POST", t.URL, strings.NewReader(string(payload)))
+ txnID, err := getMatrixTxnID(payload)
+ if err != nil {
+ return nil, fmt.Errorf("getMatrixHookRequest: unable to hash payload: %+v", err)
+ }
+
+ t.URL = fmt.Sprintf("%s/%s", t.URL, txnID)
+
+ req, err := http.NewRequest(t.HTTPMethod, t.URL, strings.NewReader(string(payload)))
if err != nil {
return nil, err
}
@@ -301,3 +309,14 @@ func getMatrixHookRequest(t *models.HookTask) (*http.Request, error) {
return req, nil
}
+
+// getMatrixTxnID creates a txnID based on the payload to ensure idempotency
+func getMatrixTxnID(payload []byte) (string, error) {
+ h := sha1.New()
+ _, err := h.Write(payload)
+ if err != nil {
+ return "", err
+ }
+
+ return fmt.Sprintf("%x", h.Sum(nil)), nil
+}