Skip to content

Commit

Permalink
base (#1231)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rohan-cp authored Oct 25, 2023
1 parent c355c6d commit f33c4a9
Showing 1 changed file with 45 additions and 1 deletion.
46 changes: 45 additions & 1 deletion service/notifications/notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,20 @@ func (p *pushLimiter) tryAdmire(ctx context.Context, sendingUserID persist.DBID,
}
}

func (p *pushLimiter) tryAdmireToken(ctx context.Context, sendingUserID persist.DBID, receivingUserID persist.DBID, tokenID persist.DBID) error {
key := fmt.Sprintf("%s:%s:%s", sendingUserID.String(), receivingUserID.String(), tokenID.String())
if p.isActionAllowed(ctx, p.admires, key) {
return nil
}

return errRateLimited{
limiterName: p.admires.Name(),
senderID: sendingUserID,
receiverID: receivingUserID,
tokenID: tokenID,
}
}

func (p *pushLimiter) tryFollow(ctx context.Context, sendingUserID persist.DBID, receivingUserID persist.DBID) error {
key := fmt.Sprintf("%s:%s", sendingUserID, receivingUserID)
if p.isActionAllowed(ctx, p.follows, key) {
Expand Down Expand Up @@ -548,6 +562,24 @@ func createPushMessage(ctx context.Context, notif db.Notification, queries *db.Q
return message, nil
}

if notif.Action == persist.ActionAdmiredToken {
admirer, err := queries.GetUserById(ctx, notif.Data.AdmirerIDs[0])
if err != nil {
return task.PushNotificationMessage{}, err
}

if err = limiter.tryAdmireToken(ctx, admirer.ID, notif.OwnerID, notif.TokenID); err != nil {
return task.PushNotificationMessage{}, err
}

if !admirer.Username.Valid {
return task.PushNotificationMessage{}, fmt.Errorf("user with ID=%s has no username", admirer.ID)
}

message.Body = fmt.Sprintf("%s admired your token", admirer.Username.String)
return message, nil
}

if notif.Action == persist.ActionCommentedOnFeedEvent || notif.Action == persist.ActionCommentedOnPost {
comment, err := queries.GetCommentByCommentID(ctx, notif.CommentID)
if err != nil {
Expand Down Expand Up @@ -940,6 +972,8 @@ func actionSupportsPushNotifications(action persist.Action) bool {
return true
case persist.ActionAdmiredPost:
return true
case persist.ActionAdmiredToken:
return true
case persist.ActionUserPostedYourWork:
return true
default:
Expand Down Expand Up @@ -1020,7 +1054,7 @@ func updateAndPublishNotif(ctx context.Context, notif db.Notification, mostRecen
var amount = notif.Amount
resultData := mostRecentNotif.Data.Concat(notif.Data)
switch notif.Action {
case persist.ActionAdmiredFeedEvent, persist.ActionAdmiredPost:
case persist.ActionAdmiredFeedEvent, persist.ActionAdmiredPost, persist.ActionAdmiredToken:
amount = int32(len(resultData.AdmirerIDs))
case persist.ActionViewedGallery:
amount = int32(len(resultData.AuthedViewerIDs) + len(resultData.UnauthedViewerIDs))
Expand Down Expand Up @@ -1100,6 +1134,15 @@ func addNotification(ctx context.Context, notif db.Notification, queries *db.Que
EventIds: notif.EventIds,
Post: sql.NullString{String: notif.PostID.String(), Valid: true},
})
case persist.ActionAdmiredToken:
return queries.CreateAdmireNotification(ctx, db.CreateAdmireNotificationParams{
ID: id,
OwnerID: notif.OwnerID,
Action: notif.Action,
Data: notif.Data,
EventIds: notif.EventIds,
Token: sql.NullString{String: notif.TokenID.String(), Valid: true},
})
case persist.ActionCommentedOnPost:
return queries.CreateCommentNotification(ctx, db.CreateCommentNotificationParams{
ID: id,
Expand Down Expand Up @@ -1213,6 +1256,7 @@ type errRateLimited struct {
senderID persist.DBID
receiverID persist.DBID
feedEventID persist.DBID
tokenID persist.DBID
}

func (e errRateLimited) Error() string {
Expand Down

0 comments on commit f33c4a9

Please sign in to comment.