Skip to content

Commit

Permalink
Remove the legacy FCM client (no longer used) (#4429)
Browse files Browse the repository at this point in the history
  • Loading branch information
nono authored Jun 25, 2024
2 parents cfda87e + 19fb9ad commit 34ca69d
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 184 deletions.
1 change: 0 additions & 1 deletion model/account/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ type AccountType struct {
Secret interface{} `json:"secret,omitempty"`

// For sending notifications via Firebase Cloud Messaging
AndroidAPIKey string `json:"android_api_key"`
FCMCredentials json.RawMessage `json:"fcm_credentials"`
}

Expand Down
6 changes: 0 additions & 6 deletions pkg/config/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,6 @@ type Notifications struct {

FCMCredentialsFile string

AndroidAPIKey string
FCMServer string

IOSCertificateKeyPath string
IOSCertificatePassword string
IOSKeyID string
Expand Down Expand Up @@ -859,9 +856,6 @@ func UseViper(v *viper.Viper) error {

FCMCredentialsFile: v.GetString("notifications.fcm_credentials_file"),

FCMServer: v.GetString("notifications.fcm_server"),
AndroidAPIKey: v.GetString("notifications.android_api_key"),

IOSCertificateKeyPath: v.GetString("notifications.ios_certificate_key_path"),
IOSCertificatePassword: v.GetString("notifications.ios_certificate_password"),
IOSKeyID: v.GetString("notifications.ios_key_id"),
Expand Down
2 changes: 0 additions & 2 deletions pkg/config/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ func TestConfigUnmarshal(t *testing.T) {
// Notifications
assert.EqualValues(t, Notifications{
Development: true,
FCMServer: "http://some-server",
AndroidAPIKey: "some-api-key",
IOSCertificateKeyPath: "cert-key-path",
IOSCertificatePassword: "cert-password",
IOSKeyID: "key-id",
Expand Down
2 changes: 0 additions & 2 deletions pkg/config/config/testdata/full_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,6 @@ flagship:

notifications:
development: true
fcm_server: http://some-server
android_api_key: some-api-key
ios_certificate_key_path: cert-key-path
ios_certificate_password: cert-password
ios_key_id: key-id
Expand Down
145 changes: 27 additions & 118 deletions worker/push/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,16 @@ import (
"github.com/cozy/cozy-stack/pkg/mail"
"google.golang.org/api/option"

fcm "github.com/appleboy/go-fcm"

apns "github.com/sideshow/apns2"
apns_cert "github.com/sideshow/apns2/certificate"
apns_payload "github.com/sideshow/apns2/payload"
apns_token "github.com/sideshow/apns2/token"
)

var (
fcmClient *messaging.Client
legacyFCMClient *fcm.Client
iosClient *apns.Client
huaweiClient *huawei.Client
fcmClient *messaging.Client
iosClient *apns.Client
huaweiClient *huawei.Client
)

func init() {
Expand Down Expand Up @@ -75,19 +72,6 @@ func Init() (err error) {
Infof("Initialized FCM client with credentials file")
}

if conf.AndroidAPIKey != "" {
if conf.FCMServer != "" {
legacyFCMClient, err = fcm.NewClient(conf.AndroidAPIKey, fcm.WithEndpoint(conf.FCMServer))
} else {
legacyFCMClient, err = fcm.NewClient(conf.AndroidAPIKey)
}
logger.WithNamespace("push").Infof("Initialized FCM client with Android API Key")
if err != nil {
logger.WithNamespace("push").Warnf("%s", err)
return
}
}

if conf.IOSCertificateKeyPath != "" {
var authKey *ecdsa.PrivateKey
var certificateKey tls.Certificate
Expand Down Expand Up @@ -241,7 +225,8 @@ func pushToFirebase(ctx *job.TaskContext, c *oauth.Client, msg *center.PushMessa
client := getFirebaseClient(slug, ctx.Instance.ContextName)

if client == nil {
return pushToLegacyFirebase(ctx, c, msg)
ctx.Logger().Warn("Could not send android notification: not configured")
return nil
}

var priority string
Expand Down Expand Up @@ -289,67 +274,6 @@ func pushToFirebase(ctx *job.TaskContext, c *oauth.Client, msg *center.PushMessa
return nil
}

func pushToLegacyFirebase(ctx *job.TaskContext, c *oauth.Client, msg *center.PushMessage) error {
slug := msg.Slug()
if c.Flagship {
slug = ""
}

client := getLegacyFirebaseClient(slug, ctx.Instance.ContextName)

if client == nil {
ctx.Logger().Warn("Could not send android notification: not configured")
return nil
}

var priority string
if msg.Priority == "high" {
priority = "high"
}

var hashedSource []byte
if msg.Collapsible {
hashedSource = hashSource(msg.Source)
} else {
hashedSource = hashSource(msg.Source + msg.NotificationID)
}

notification := &fcm.Message{
To: c.NotificationDeviceToken,
Priority: priority,
ContentAvailable: true,
Notification: &fcm.Notification{
Sound: msg.Sound,
Title: msg.Title,
Body: msg.Message,
},
Data: prepareLegacyAndroidData(msg, hashedSource),
}

if msg.Collapsible {
notification.CollapseKey = hex.EncodeToString(hashedSource)
}

res, err := client.Send(notification)
if err != nil {
ctx.Logger().Warnf("Error during fcm send: %s", err)
return err
}
if res.Failure == 0 {
return nil
}

for _, result := range res.Results {
if result.Unregistered() {
_ = c.Delete(ctx.Instance)
}
if err = result.Error; err != nil {
return err
}
}
return nil
}

func prepareAndroidData(msg *center.PushMessage, hashedSource []byte) map[string]string {
// notID should be an integer, we take the first 32bits of the hashed source
// value.
Expand All @@ -371,27 +295,6 @@ func prepareAndroidData(msg *center.PushMessage, hashedSource []byte) map[string
return data
}

func prepareLegacyAndroidData(msg *center.PushMessage, hashedSource []byte) map[string]interface{} {
// notID should be an integer, we take the first 32bits of the hashed source
// value.
notID := int32(binary.BigEndian.Uint32(hashedSource[:4]))
if notID < 0 {
notID = -notID
}

data := map[string]interface{}{
// Fields required by phonegap-plugin-push
// see: https://github.com/phonegap/phonegap-plugin-push/blob/master/docs/PAYLOAD.md#android-behaviour
"notId": notID,
"title": msg.Title,
"body": msg.Message,
}
for k, v := range msg.Data {
data[k] = v
}
return data
}

func getFirebaseClient(slug, contextName string) *messaging.Client {
if slug == "" {
return fcmClient
Expand All @@ -413,21 +316,6 @@ func getFirebaseClient(slug, contextName string) *messaging.Client {
return fcmClient
}

func getLegacyFirebaseClient(slug, contextName string) *fcm.Client {
if slug == "" {
return legacyFCMClient
}
typ, err := account.TypeInfo(slug, contextName)
if err == nil && typ.AndroidAPIKey != "" {
client, err := fcm.NewClient(typ.AndroidAPIKey)
if err != nil {
return nil
}
return client
}
return legacyFCMClient
}

func pushToAPNS(ctx *job.TaskContext, c *oauth.Client, msg *center.PushMessage) error {
if iosClient == nil {
ctx.Logger().Warn("Could not send iOS notification: not configured")
Expand Down Expand Up @@ -482,7 +370,7 @@ func pushToHuawei(ctx *job.TaskContext, c *oauth.Client, msg *center.PushMessage
} else {
hashedSource = hashSource(msg.Source + msg.NotificationID)
}
data := prepareLegacyAndroidData(msg, hashedSource)
data := prepareHuaweiData(msg, hashedSource)

notification := huawei.NewNotification(msg.Title, msg.Message, c.NotificationDeviceToken, data)
ctx.Logger().Infof("Huawei Push Kit send: %#v", notification)
Expand All @@ -502,6 +390,27 @@ func hashSource(source string) []byte {
return h.Sum(nil)
}

func prepareHuaweiData(msg *center.PushMessage, hashedSource []byte) map[string]interface{} {
// notID should be an integer, we take the first 32bits of the hashed source
// value.
notID := int32(binary.BigEndian.Uint32(hashedSource[:4]))
if notID < 0 {
notID = -notID
}

data := map[string]interface{}{
// Fields required by phonegap-plugin-push
// see: https://github.com/phonegap/phonegap-plugin-push/blob/master/docs/PAYLOAD.md#android-behaviour
"notId": notID,
"title": msg.Title,
"body": msg.Message,
}
for k, v := range msg.Data {
data[k] = v
}
return data
}

func sendFallbackMail(inst *instance.Instance, email *mail.Options) {
if inst == nil || email == nil {
return
Expand Down
55 changes: 0 additions & 55 deletions worker/push/push_test.go

This file was deleted.

0 comments on commit 34ca69d

Please sign in to comment.