From 2fb807632c4f8953482d68abd11d6f59bd5793cc Mon Sep 17 00:00:00 2001 From: Kalissaac Date: Tue, 19 Jul 2022 10:42:28 -0700 Subject: [PATCH] style(alerting): Add comments and rename character bytes constant --- alerting/provider/matrix/matrix.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/alerting/provider/matrix/matrix.go b/alerting/provider/matrix/matrix.go index 38be741a..0c234243 100644 --- a/alerting/provider/matrix/matrix.go +++ b/alerting/provider/matrix/matrix.go @@ -66,6 +66,7 @@ func (provider *AlertProvider) Send(endpoint *core.Endpoint, alert *alert.Alert, if config.ServerURL == "" { config.ServerURL = defaultHomeserverURL } + // The Matrix endpoint requires a unique transaction ID for each event sent txnId := randStringBytes(24) request, err := http.NewRequest( http.MethodPut, @@ -173,14 +174,16 @@ func (provider *AlertProvider) getConfigForGroup(group string) MatrixProviderCon } } -const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" - func randStringBytes(n int) string { + // All the compatible characters to use in a transaction ID + const availableCharacterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" + b := make([]byte, n) rand.Seed(time.Now().UnixNano()) for i := range b { - b[i] = letterBytes[rand.Intn(len(letterBytes))] + b[i] = availableCharacterBytes[rand.Intn(len(availableCharacterBytes))] } + return string(b) }