server_matrix.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package server
  2. import (
  3. "encoding/json"
  4. "heckel.io/ntfy/log"
  5. "io"
  6. "net/http"
  7. )
  8. const (
  9. matrixPushkeyHeader = "X-Matrix-Pushkey"
  10. )
  11. type matrixMessage struct {
  12. Notification *matrixNotification `json:"notification"`
  13. }
  14. type matrixNotification struct {
  15. Devices []*matrixDevice `json:"devices"`
  16. }
  17. type matrixDevice struct {
  18. PushKey string `json:"pushkey"`
  19. }
  20. type matrixResponse struct {
  21. Rejected []string `json:"rejected"`
  22. }
  23. func handleMatrixDiscovery(w http.ResponseWriter) error {
  24. w.Header().Set("Content-Type", "application/json")
  25. _, err := io.WriteString(w, `{"unifiedpush":{"gateway":"matrix"}}`+"\n")
  26. return err
  27. }
  28. func writeMatrixError(w http.ResponseWriter, pushKey string, err error) error {
  29. log.Debug("Matrix message with push key %s rejected: %s", pushKey, err.Error())
  30. response := &matrixResponse{
  31. Rejected: []string{pushKey},
  32. }
  33. w.Header().Set("Content-Type", "application/json")
  34. if err := json.NewEncoder(w).Encode(response); err != nil {
  35. return err
  36. }
  37. return nil
  38. }
  39. func writeMatrixSuccess(w http.ResponseWriter) error {
  40. response := &matrixResponse{
  41. Rejected: make([]string, 0),
  42. }
  43. if err := json.NewEncoder(w).Encode(response); err != nil {
  44. return err
  45. }
  46. return nil
  47. }