pull/326/head
Philipp Heckel 2022-06-15 20:51:42 -04:00
parent ebbc2838ba
commit 0ff8e968ca
1 changed files with 35 additions and 11 deletions

View File

@ -15,16 +15,27 @@ const (
matrixPushKeyHeader = "X-Matrix-Pushkey" matrixPushKeyHeader = "X-Matrix-Pushkey"
) )
type matrixMessage struct { // matrixRequest represents a Matrix message, as it is sent to a Push Gateway (as per
Notification *matrixNotification `json:"notification"` // this spec: https://spec.matrix.org/v1.2/push-gateway-api/).
} //
// From the message, we only require the "pushkey", as it represents our target topic URL.
type matrixNotification struct { // A message may look like this (excerpt):
Devices []*matrixDevice `json:"devices"` // {
} // "notification": {
// "devices": [
type matrixDevice struct { // {
// "pushkey": "https://ntfy.sh/upDAHJKFFDFD?up=1",
// ...
// }
// ]
// }
// }
type matrixRequest struct {
Notification *struct {
Devices []*struct {
PushKey string `json:"pushkey"` PushKey string `json:"pushkey"`
} `json:"devices"`
} `json:"notification"`
} }
type matrixResponse struct { type matrixResponse struct {
@ -43,6 +54,19 @@ func (e errMatrix) Error() string {
return fmt.Sprintf("message with push key %s rejected", e.pushKey) return fmt.Sprintf("message with push key %s rejected", e.pushKey)
} }
// newRequestFromMatrixJSON reads the request body as a Matrix JSON message, parses the "pushkey", and creates a new
// HTTP request that looks like a normal ntfy request from it.
//
// It basically converts a Matrix push gatewqy request:
//
// POST /_matrix/push/v1/notify HTTP/1.1
// { "notification": { "devices": [ { "pushkey": "https://ntfy.sh/upDAHJKFFDFD?up=1", ... } ] } }
//
// to a ntfy request, looking like this:
//
// POST /upDAHJKFFDFD?up=1 HTTP/1.1
// { "notification": { "devices": [ { "pushkey": "https://ntfy.sh/upDAHJKFFDFD?up=1", ... } ] } }
//
func newRequestFromMatrixJSON(r *http.Request, baseURL string, messageLimit int) (*http.Request, error) { func newRequestFromMatrixJSON(r *http.Request, baseURL string, messageLimit int) (*http.Request, error) {
if baseURL == "" { if baseURL == "" {
return nil, errHTTPInternalErrorMissingBaseURL return nil, errHTTPInternalErrorMissingBaseURL
@ -52,7 +76,7 @@ func newRequestFromMatrixJSON(r *http.Request, baseURL string, messageLimit int)
return nil, err return nil, err
} }
defer r.Body.Close() defer r.Body.Close()
var m matrixMessage var m matrixRequest
if err := json.NewDecoder(body).Decode(&m); err != nil { if err := json.NewDecoder(body).Decode(&m); err != nil {
return nil, errHTTPBadRequestMatrixMessageInvalid return nil, errHTTPBadRequestMatrixMessageInvalid
} else if m.Notification == nil || len(m.Notification.Devices) == 0 || m.Notification.Devices[0].PushKey == "" { } else if m.Notification == nil || len(m.Notification.Devices) == 0 || m.Notification.Devices[0].PushKey == "" {