Auto-delete clients when FB returns UNREGISTERED
This commit is contained in:
@@ -12,6 +12,6 @@ func NewDummy() NotificationClient {
|
||||
return &DummyConnector{}
|
||||
}
|
||||
|
||||
func (d DummyConnector) SendNotification(ctx context.Context, user models.User, client models.Client, channel models.Channel, msg models.Message) (string, error) {
|
||||
return "%DUMMY%", nil
|
||||
func (d DummyConnector) SendNotification(ctx context.Context, user models.User, client models.Client, channel models.Channel, msg models.Message) (string, string, error) {
|
||||
return "%DUMMY%", "", nil
|
||||
}
|
||||
|
@@ -53,7 +53,7 @@ type Notification struct {
|
||||
Priority int
|
||||
}
|
||||
|
||||
func (fb FirebaseConnector) SendNotification(ctx context.Context, user models.User, client models.Client, channel models.Channel, msg models.Message) (string, error) {
|
||||
func (fb FirebaseConnector) SendNotification(ctx context.Context, user models.User, client models.Client, channel models.Channel, msg models.Message) (string, string, error) {
|
||||
|
||||
uri := "https://fcm.googleapis.com/v1/projects/" + fb.fbProject + "/messages:send"
|
||||
|
||||
@@ -100,18 +100,18 @@ func (fb FirebaseConnector) SendNotification(ctx context.Context, user models.Us
|
||||
|
||||
bytesBody, err := json.Marshal(gin.H{"message": jsonBody})
|
||||
if err != nil {
|
||||
return "", err
|
||||
return "", "", err
|
||||
}
|
||||
|
||||
request, err := http.NewRequestWithContext(ctx, "POST", uri, bytes.NewBuffer(bytesBody))
|
||||
if err != nil {
|
||||
return "", err
|
||||
return "", "", err
|
||||
}
|
||||
|
||||
tok, err := fb.auth.Token(ctx)
|
||||
if err != nil {
|
||||
log.Err(err).Msg("Refreshing FB token failed")
|
||||
return "", err
|
||||
return "", "", err
|
||||
}
|
||||
|
||||
request.Header.Set("Authorization", "Bearer "+tok)
|
||||
@@ -120,31 +120,53 @@ func (fb FirebaseConnector) SendNotification(ctx context.Context, user models.Us
|
||||
|
||||
response, err := fb.client.Do(request)
|
||||
if err != nil {
|
||||
return "", err
|
||||
return "", "", err
|
||||
}
|
||||
defer func() { _ = response.Body.Close() }()
|
||||
|
||||
if response.StatusCode < 200 || response.StatusCode >= 300 {
|
||||
if bstr, err := io.ReadAll(response.Body); err == nil {
|
||||
return "", errors.New(fmt.Sprintf("FCM-Request returned %d: %s", response.StatusCode, string(bstr)))
|
||||
|
||||
var errRespBody struct {
|
||||
Error struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Status string `json:"status"`
|
||||
Details []struct {
|
||||
AtType string `json:"@type"`
|
||||
ECode string `json:"errorCode"`
|
||||
} `json:"details"`
|
||||
} `json:"error"`
|
||||
}
|
||||
|
||||
if err := json.Unmarshal(bstr, &errRespBody); err == nil {
|
||||
for _, v := range errRespBody.Error.Details {
|
||||
return "", v.ECode, errors.New(fmt.Sprintf("FCM-Request returned %d [UNREGISTERED]: %s", response.StatusCode, string(bstr)))
|
||||
}
|
||||
}
|
||||
|
||||
return "", "", errors.New(fmt.Sprintf("FCM-Request returned %d: %s", response.StatusCode, string(bstr)))
|
||||
|
||||
} else {
|
||||
return "", errors.New(fmt.Sprintf("FCM-Request returned %d", response.StatusCode))
|
||||
|
||||
return "", "", errors.New(fmt.Sprintf("FCM-Request returned %d", response.StatusCode))
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
respBodyBin, err := io.ReadAll(response.Body)
|
||||
if err != nil {
|
||||
return "", err
|
||||
return "", "", err
|
||||
}
|
||||
|
||||
var respBody struct {
|
||||
Name string `json:"name"`
|
||||
}
|
||||
if err := json.Unmarshal(respBodyBin, &respBody); err != nil {
|
||||
return "", err
|
||||
return "", "", err
|
||||
}
|
||||
|
||||
log.Info().Msg(fmt.Sprintf("Sucessfully pushed notification %s", msg.MessageID))
|
||||
|
||||
return respBody.Name, nil
|
||||
return respBody.Name, "", nil
|
||||
}
|
||||
|
@@ -6,5 +6,5 @@ import (
|
||||
)
|
||||
|
||||
type NotificationClient interface {
|
||||
SendNotification(ctx context.Context, user models.User, client models.Client, channel models.Channel, msg models.Message) (string, error)
|
||||
SendNotification(ctx context.Context, user models.User, client models.Client, channel models.Channel, msg models.Message) (string, string, error)
|
||||
}
|
||||
|
@@ -24,10 +24,10 @@ func (d *TestSink) Last() SinkData {
|
||||
return d.Data[len(d.Data)-1]
|
||||
}
|
||||
|
||||
func (d *TestSink) SendNotification(ctx context.Context, user models.User, client models.Client, channel models.Channel, msg models.Message) (string, error) {
|
||||
func (d *TestSink) SendNotification(ctx context.Context, user models.User, client models.Client, channel models.Channel, msg models.Message) (string, string, error) {
|
||||
id, err := langext.NewHexUUID()
|
||||
if err != nil {
|
||||
return "", err
|
||||
return "", "", err
|
||||
}
|
||||
|
||||
key := "TestSink[" + id + "]"
|
||||
@@ -37,5 +37,5 @@ func (d *TestSink) SendNotification(ctx context.Context, user models.User, clien
|
||||
Client: client,
|
||||
})
|
||||
|
||||
return key, nil
|
||||
return key, "", nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user