From f33c4a9cd6b1063f6c78f6e44c4efe1c7722275a Mon Sep 17 00:00:00 2001 From: Rohan-cp Date: Wed, 25 Oct 2023 23:20:45 +0530 Subject: [PATCH] base (#1231) --- service/notifications/notifications.go | 46 +++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/service/notifications/notifications.go b/service/notifications/notifications.go index e0b43c7fe..a19998123 100644 --- a/service/notifications/notifications.go +++ b/service/notifications/notifications.go @@ -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) { @@ -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 { @@ -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: @@ -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)) @@ -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, @@ -1213,6 +1256,7 @@ type errRateLimited struct { senderID persist.DBID receiverID persist.DBID feedEventID persist.DBID + tokenID persist.DBID } func (e errRateLimited) Error() string {