Skip to content

Commit

Permalink
share logic
Browse files Browse the repository at this point in the history
  • Loading branch information
benny-conn committed Oct 16, 2023
1 parent b393b27 commit 90df168
Show file tree
Hide file tree
Showing 3 changed files with 272 additions and 233 deletions.
157 changes: 8 additions & 149 deletions emails/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/bsm/redislock"
"github.com/mikeydub/go-gallery/service/auth"
"github.com/mikeydub/go-gallery/service/notifications"

"cloud.google.com/go/pubsub"
"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -105,17 +106,10 @@ func sendVerificationEmail(dataloaders *dataloader.Loaders, queries *coredb.Quer
}
}

type notificationEmailDynamicTemplateData struct {
Actor string `json:"actor"`
Action string `json:"action"`
CollectionName string `json:"collectionName"`
CollectionID persist.DBID `json:"collectionId"`
PreviewText string `json:"previewText"`
}
type notificationsEmailDynamicTemplateData struct {
Notifications []notificationEmailDynamicTemplateData `json:"notifications"`
Username string `json:"username"`
UnsubscribeToken string `json:"unsubscribeToken"`
Notifications []notifications.UserFacingNotificationData `json:"notifications"`
Username string `json:"username"`
UnsubscribeToken string `json:"unsubscribeToken"`
}

func adminSendNotificationEmail(queries *coredb.Queries, s *sendgrid.Client) gin.HandlerFunc {
Expand Down Expand Up @@ -205,17 +199,18 @@ func sendNotificationEmailToUser(c context.Context, u coredb.PiiUserView, emailR
}

data := notificationsEmailDynamicTemplateData{
Notifications: make([]notificationEmailDynamicTemplateData, 0, resultLimit),
Notifications: make([]notifications.UserFacingNotificationData, 0, resultLimit),
Username: u.Username.String,
UnsubscribeToken: j,
}
notifTemplates := make(chan notificationEmailDynamicTemplateData)
notifTemplates := make(chan notifications.UserFacingNotificationData)
errChan := make(chan error)

for _, n := range notifs {
notif := n
go func() {
notifTemplate, err := notifToTemplateData(c, queries, notif)
// notifTemplate, err := notifToTemplateData(c, queries, notif)
notifTemplate, err := notifications.NotificationToUserFacingData(c, queries, notif)
if err != nil {
errChan <- err
return
Expand Down Expand Up @@ -277,142 +272,6 @@ outer:
return &rest.Response{StatusCode: 200, Body: "not sending real emails", Headers: map[string][]string{}}, nil
}

func notifToTemplateData(ctx context.Context, queries *coredb.Queries, n coredb.Notification) (notificationEmailDynamicTemplateData, error) {

switch n.Action {
case persist.ActionAdmiredFeedEvent:
feedEvent, err := queries.GetFeedEventByID(ctx, n.FeedEventID)
if err != nil {
return notificationEmailDynamicTemplateData{}, fmt.Errorf("failed to get feed event for admire %s: %w", n.FeedEventID, err)
}
collection, _ := queries.GetCollectionById(ctx, feedEvent.Data.CollectionID)
data := notificationEmailDynamicTemplateData{}
if collection.ID != "" && collection.Name.String != "" {
data.CollectionID = collection.ID
data.CollectionName = collection.Name.String
data.Action = "admired your additions to"
} else {
data.Action = "admired your gallery update"
}
if len(n.Data.AdmirerIDs) > 1 {
data.Actor = fmt.Sprintf("%d collectors", len(n.Data.AdmirerIDs))
} else {
actorUser, err := queries.GetUserById(ctx, n.Data.AdmirerIDs[0])
if err != nil {
return notificationEmailDynamicTemplateData{}, err
}
data.Actor = actorUser.Username.String
}
return data, nil
case persist.ActionUserFollowedUsers:
if len(n.Data.FollowerIDs) > 1 {
return notificationEmailDynamicTemplateData{
Actor: fmt.Sprintf("%d users", len(n.Data.FollowerIDs)),
Action: "followed you",
}, nil
}
if len(n.Data.FollowerIDs) == 1 {
userActor, err := queries.GetUserById(ctx, n.Data.FollowerIDs[0])
if err != nil {
return notificationEmailDynamicTemplateData{}, fmt.Errorf("failed to get user for follower %s: %w", n.Data.FollowerIDs[0], err)
}
action := "followed you"
if n.Data.FollowedBack {
action = "followed you back"
}
return notificationEmailDynamicTemplateData{
Actor: userActor.Username.String,
Action: action,
}, nil
}
return notificationEmailDynamicTemplateData{}, fmt.Errorf("no follower ids")
case persist.ActionCommentedOnFeedEvent:
comment, err := queries.GetCommentByCommentID(ctx, n.CommentID)
if err != nil {
return notificationEmailDynamicTemplateData{}, fmt.Errorf("failed to get comment for comment %s: %w", n.CommentID, err)
}
userActor, err := queries.GetUserById(ctx, comment.ActorID)
if err != nil {
return notificationEmailDynamicTemplateData{}, fmt.Errorf("failed to get user for comment actor %s: %w", comment.ActorID, err)
}
feedEvent, err := queries.GetFeedEventByID(ctx, n.FeedEventID)
if err != nil {
return notificationEmailDynamicTemplateData{}, fmt.Errorf("failed to get feed event for comment %s: %w", n.FeedEventID, err)
}
collection, _ := queries.GetCollectionById(ctx, feedEvent.Data.CollectionID)
if collection.ID != "" {
return notificationEmailDynamicTemplateData{
Actor: userActor.Username.String,
Action: "commented on your additions to",
CollectionName: collection.Name.String,
CollectionID: collection.ID,
PreviewText: util.TruncateWithEllipsis(comment.Comment, 20),
}, nil
}
return notificationEmailDynamicTemplateData{
Actor: userActor.Username.String,
Action: "commented on your gallery update",
PreviewText: util.TruncateWithEllipsis(comment.Comment, 20),
}, nil
case persist.ActionViewedGallery:
if len(n.Data.AuthedViewerIDs)+len(n.Data.UnauthedViewerIDs) > 1 {
return notificationEmailDynamicTemplateData{
Actor: fmt.Sprintf("%d collectors", len(n.Data.AuthedViewerIDs)+len(n.Data.UnauthedViewerIDs)),
Action: "viewed your gallery",
}, nil
}
if len(n.Data.AuthedViewerIDs) == 1 {
userActor, err := queries.GetUserById(ctx, n.Data.AuthedViewerIDs[0])
if err != nil {
return notificationEmailDynamicTemplateData{}, fmt.Errorf("failed to get user for viewer %s: %w", n.Data.AuthedViewerIDs[0], err)
}
return notificationEmailDynamicTemplateData{
Actor: userActor.Username.String,
Action: "viewed your gallery",
}, nil
}
if len(n.Data.UnauthedViewerIDs) == 1 {
return notificationEmailDynamicTemplateData{
Actor: "Someone",
Action: "viewed your gallery",
}, nil
}

return notificationEmailDynamicTemplateData{}, fmt.Errorf("no viewer ids")

case persist.ActionAdmiredPost:
data := notificationEmailDynamicTemplateData{}
if len(n.Data.AdmirerIDs) > 1 {
data.Actor = fmt.Sprintf("%d collectors", len(n.Data.AdmirerIDs))
} else {
actorUser, err := queries.GetUserById(ctx, n.Data.AdmirerIDs[0])
if err != nil {
return notificationEmailDynamicTemplateData{}, err
}
data.Actor = actorUser.Username.String
}
data.Action = "admired your post"
return data, nil

case persist.ActionCommentedOnPost:
comment, err := queries.GetCommentByCommentID(ctx, n.CommentID)
if err != nil {
return notificationEmailDynamicTemplateData{}, fmt.Errorf("failed to get comment for comment %s: %w", n.CommentID, err)
}
userActor, err := queries.GetUserById(ctx, comment.ActorID)
if err != nil {
return notificationEmailDynamicTemplateData{}, fmt.Errorf("failed to get user for comment actor %s: %w", comment.ActorID, err)
}
return notificationEmailDynamicTemplateData{
Actor: userActor.Username.String,
Action: "commented on your post",
PreviewText: util.TruncateWithEllipsis(comment.Comment, 20),
}, nil
default:
return notificationEmailDynamicTemplateData{}, fmt.Errorf("unknown action %s", n.Action)
}
}

func runForUsersWithNotificationsOnForEmailType(ctx context.Context, emailType persist.EmailType, queries *coredb.Queries, fn func(u coredb.PiiUserView) error) error {
errGroup := new(errgroup.Group)
var lastID persist.DBID
Expand Down
9 changes: 5 additions & 4 deletions emails/t__unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"github.com/mikeydub/go-gallery/db/gen/coredb"
"github.com/mikeydub/go-gallery/service/notifications"
)

func TestNotificationTemplating_Success(t *testing.T) {
Expand All @@ -17,28 +18,28 @@ func TestNotificationTemplating_Success(t *testing.T) {
q := coredb.New(pgx)

t.Run("creates a template for admire notifications", func(t *testing.T) {
data, err := notifToTemplateData(ctx, q, admireNotif)
data, err := notifications.NotificationToUserFacingData(ctx, q, admireNotif)
a.NoError(err)
a.Equal(testUser2.Username.String, data.Actor)
a.Equal(data.CollectionID, testGallery.Collections[0])
})

t.Run("creates a template for follow notifications", func(t *testing.T) {
data, err := notifToTemplateData(ctx, q, followNotif)
data, err := notifications.NotificationToUserFacingData(ctx, q, followNotif)
a.NoError(err)
a.Equal(testUser2.Username.String, data.Actor)
})

t.Run("creates a template for comment notifications", func(t *testing.T) {
data, err := notifToTemplateData(ctx, q, commentNotif)
data, err := notifications.NotificationToUserFacingData(ctx, q, commentNotif)
a.NoError(err)
a.Equal(testUser2.Username.String, data.Actor)
a.Equal(data.CollectionID, testGallery.Collections[0])
a.Equal(data.PreviewText, comment.Comment)
})

t.Run("creates a template for view notifications", func(t *testing.T) {
data, err := notifToTemplateData(ctx, q, viewNotif)
data, err := notifications.NotificationToUserFacingData(ctx, q, viewNotif)
a.NoError(err)
a.Equal(testUser2.Username.String, data.Actor)
})
Expand Down
Loading

0 comments on commit 90df168

Please sign in to comment.