This repository has been archived by the owner on Feb 23, 2023. It is now read-only.
forked from matrix-org/dendrite
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds PushGatewayClient and plumbing in Pushserver's room consumer.
* Push rules are "always notify". * Only event_id_only push format is allowed.
- Loading branch information
Showing
12 changed files
with
606 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package pushgateway | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/opentracing/opentracing-go" | ||
) | ||
|
||
type httpClient struct { | ||
hc *http.Client | ||
} | ||
|
||
// NewHTTPClient creates a new Push Gateway client that uses an | ||
// *http.Client. | ||
func NewHTTPClient(hc *http.Client) Client { | ||
return &httpClient{hc: hc} | ||
} | ||
|
||
func (h *httpClient) Notify(ctx context.Context, url string, req *NotifyRequest, resp *NotifyResponse) error { | ||
span, ctx := opentracing.StartSpanFromContext(ctx, "Notify") | ||
defer span.Finish() | ||
|
||
body, err := json.Marshal(req) | ||
if err != nil { | ||
return err | ||
} | ||
hreq, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(body)) | ||
if err != nil { | ||
return err | ||
} | ||
hreq.Header.Set("Content-Type", "application/json") | ||
|
||
hresp, err := h.hc.Do(hreq) | ||
if err != nil { | ||
return err | ||
} | ||
defer hresp.Body.Close() | ||
|
||
if hresp.StatusCode == http.StatusOK { | ||
return json.NewDecoder(hresp.Body).Decode(resp) | ||
} | ||
|
||
var errorBody struct { | ||
Message string `json:"message"` | ||
} | ||
if err := json.NewDecoder(hresp.Body).Decode(&errorBody); err == nil { | ||
return fmt.Errorf("push gateway: %d from %s: %s", hresp.StatusCode, url, errorBody.Message) | ||
} | ||
return fmt.Errorf("push gateway: %d from %s", hresp.StatusCode, url) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package pushgateway | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
) | ||
|
||
// A Client is how interactions iwth a Push Gateway is done. | ||
type Client interface { | ||
// Notify sends a notification to the gateway at the given URL. | ||
Notify(ctx context.Context, url string, req *NotifyRequest, resp *NotifyResponse) error | ||
} | ||
|
||
type NotifyRequest struct { | ||
Notification Notification `json:"notification"` // Required | ||
} | ||
|
||
type NotifyResponse struct { | ||
// Rejected is the list of device push keys that were rejected | ||
// during the push. The caller should remove the push keys so they | ||
// are not used again. | ||
Rejected []string `json:"rejected"` // Required | ||
} | ||
|
||
type Notification struct { | ||
Content json.RawMessage `json:"content,omitempty"` | ||
Counts *Counts `json:"counts,omitempty"` | ||
Devices []*Device `json:"devices"` // Required | ||
EventID string `json:"event_id,omitempty"` | ||
Prio Prio `json:"prio,omitempty"` | ||
RoomAlias string `json:"room_alias,omitempty"` | ||
RoomID string `json:"room_id,omitempty"` | ||
RoomName string `json:"room_name,omitempty"` | ||
Sender string `json:"sender,omitempty"` | ||
SenderDisplayName string `json:"sender_display_name,omitempty"` | ||
Type string `json:"type,omitempty"` | ||
UserIsTarget bool `json:"user_is_target,omitempty"` | ||
} | ||
|
||
type Counts struct { | ||
MissedCalls int `json:"missed_calls,omitempty"` | ||
Unread int `json:"unread,omitempty"` | ||
} | ||
|
||
type Device struct { | ||
AppID string `json:"app_id"` // Required | ||
Data map[string]interface{} `json:"data,omitempty"` | ||
PushKey string `json:"pushkey"` // Required | ||
PushKeyTS int64 `json:"pushkey_ts,omitempty"` | ||
Tweaks map[string]interface{} `json:"tweaks,omitempty"` | ||
} | ||
|
||
type Prio string | ||
|
||
const ( | ||
HighPrio Prio = "high" | ||
LowPrio Prio = "low" | ||
) |
Oops, something went wrong.