Skip to content

Commit

Permalink
Disable push notifications in local environments
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-bouvier committed Dec 31, 2019
1 parent a336b96 commit 01d80cd
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 18 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ vi config.json

Attributes `google_email` and `google_password` refer to the credentials of your Google account. These credentials are used to send emails. `mongo_password` refers to the MongoDB password. `env` refers to the environment type and should be set to `prod`, `dev` or `local`. Finally, `port` refers to the API port.

In a `local` environment:

* Cookies are not secure ;
* Push notifications are not sent.

The FCM HTTP v1 API requires some credentials to send push notifications. The `service-account.json` file can be downloaded from the Firebase Cloud Messaging dashboard, and should be copied at the root of this directory. This way, it will be included in the Docker container.

The authentication mechanism relies on JWT tokens and thus needs a secret key. Please generate the following keys at the root of this directory:
Expand Down
6 changes: 6 additions & 0 deletions mail.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/smtp"
)

// SendEmail sends an email to the given recipient.
func SendEmail(to string, subject string, body string) {
from := config.GoogleEmail
pass := config.GooglePassword
Expand All @@ -26,6 +27,7 @@ func SendEmail(to string, subject string, body string) {
smtp.SendMail("smtp.gmail.com:587", smtp.PlainAuth("", from, pass, "smtp.gmail.com"), from, []string{to}, []byte(msg))
}

// SendAssociationEmailSubscription sends a subscription email containing the credentials.
func SendAssociationEmailSubscription(email string, password string) error {
data := struct {
Email string
Expand All @@ -43,6 +45,8 @@ func SendAssociationEmailSubscription(email string, password string) error {
return err
}

// SendAssociationEmailForCommentOnEvent sends an email indicating
// a new comment has been added on an event
func SendAssociationEmailForCommentOnEvent(email string, event Event, comment Comment, user User) error {
data := struct {
EventName string
Expand All @@ -66,6 +70,8 @@ func SendAssociationEmailForCommentOnEvent(email string, event Event, comment Co
return err
}

// SendAssociationEmailForCommentOnPost sends an email indicating
// a new comment has been added on a post
func SendAssociationEmailForCommentOnPost(email string, post Post, comment Comment, user User) error {
data := struct {
PostName string
Expand Down
48 changes: 30 additions & 18 deletions notificationManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,30 +87,37 @@ func buildTopicsStringForPromotions(prefix string, promotions []string) string {
return topics
}

// TriggerNotificationForUserFromPost will send a Notification as well as a FCM push
// notification to the given user
// TriggerNotificationForUserFromPost sends a notification and a push
// notification to the given user.
// Push notifications are not sent in a local environment.
func TriggerNotificationForUserFromPost(sender bson.ObjectId, receiver bson.ObjectId, content bson.ObjectId, message string, comment Comment, tagType string) {
notification := Notification{Sender: sender, Content: content, Message: message, Comment: comment, Type: tagType}
user := getNotificationUserForUser(receiver)

sendNotificationToUsers(notification, []NotificationUser{user})

sendPushNotificationToDevice(GetUser(sender).Username, message, content.Hex(), ".activities.PostActivity", user.Token)
if config.Environment != "local" {
sendPushNotificationToDevice(GetUser(sender).Username, message, content.Hex(), ".activities.PostActivity", user.Token)
}
}

// TriggerNotificationForUserFromEvent will send a Notification as well as a FCM push
// notification to the given user
// TriggerNotificationForUserFromEvent sends a notification and a push
// notification to the given user.
// Push notifications are not sent in a local environment.
func TriggerNotificationForUserFromEvent(sender bson.ObjectId, receiver bson.ObjectId, content bson.ObjectId, message string, comment Comment, tagType string) {
notification := Notification{Sender: sender, Content: content, Message: message, Comment: comment, Type: tagType}
user := getNotificationUserForUser(receiver)

sendNotificationToUsers(notification, []NotificationUser{user})

sendPushNotificationToDevice(GetUser(sender).Username, message, content.Hex(), ".activities.EventActivity", user.Token)
if config.Environment != "local" {
sendPushNotificationToDevice(GetUser(sender).Username, message, content.Hex(), ".activities.EventActivity", user.Token)
}
}

// TriggerNotificationForEvent will send a Notification as well as a FCM push
// notification to users targeted by the Event platform and promotion
// TriggerNotificationForEvent sends a notification and a push
// notification to users targeted by the promotion.
// Push notifications are not sent in a local environment.
func TriggerNotificationForEvent(event Event, sender bson.ObjectId, content bson.ObjectId, message string) {
notification := Notification{Sender: sender, Content: content, Message: message, Type: "event"}

Expand Down Expand Up @@ -139,15 +146,18 @@ func TriggerNotificationForEvent(event Event, sender bson.ObjectId, content bson

sendNotificationToUsers(notification, users)

for _, promotion := range event.Promotions {
var topics = fmt.Sprintf(`%s && 'events-%s' in topics`, platforms, promotion)
sendPushNotificationToTopics(event.Name, message, content.Hex(), ".activities.EventActivity", topics)
if config.Environment != "local" {
for _, promotion := range event.Promotions {
var topics = fmt.Sprintf(`%s && 'events-%s' in topics`, platforms, promotion)
sendPushNotificationToTopics(event.Name, message, content.Hex(), ".activities.EventActivity", topics)
}
sendPushNotificationToTopics(event.Name, message, content.Hex(), ".activities.EventActivity", fmt.Sprintf(`%s && 'events-unknown-class' in topics`, platforms))
}
sendPushNotificationToTopics(event.Name, message, content.Hex(), ".activities.EventActivity", fmt.Sprintf(`%s && 'events-unknown-class' in topics`, platforms))
}

// TriggerNotificationForPost will send a Notification as well as a FCM push
// notification to users targeted by the Post platform and promotion
// TriggerNotificationForPost sends a notification and a push
// notification to users targeted by the promotion.
// Push notifications are not sent in a local environment.
func TriggerNotificationForPost(post Post, sender bson.ObjectId, content bson.ObjectId, message string) {
notification := Notification{Sender: sender, Content: content, Message: message, Type: "post"}

Expand Down Expand Up @@ -176,11 +186,13 @@ func TriggerNotificationForPost(post Post, sender bson.ObjectId, content bson.Ob

sendNotificationToUsers(notification, users)

for _, promotion := range post.Promotions {
var topics = fmt.Sprintf(`%s && 'posts-%s' in topics`, platforms, promotion)
sendPushNotificationToTopics(post.Title, message, content.Hex(), ".activities.PostActivity", topics)
if config.Environment != "local" {
for _, promotion := range post.Promotions {
var topics = fmt.Sprintf(`%s && 'posts-%s' in topics`, platforms, promotion)
sendPushNotificationToTopics(post.Title, message, content.Hex(), ".activities.PostActivity", topics)
}
sendPushNotificationToTopics(post.Title, message, content.Hex(), ".activities.PostActivity", fmt.Sprintf(`%s && 'posts-unknown-class' in topics`, platforms))
}
sendPushNotificationToTopics(post.Title, message, content.Hex(), ".activities.PostActivity", fmt.Sprintf(`%s && 'posts-unknown-class' in topics`, platforms))
}

func sendNotificationToUsers(notification Notification, users []NotificationUser) {
Expand Down

0 comments on commit 01d80cd

Please sign in to comment.