Skip to content

Commit

Permalink
Fix users receiving friend request notifications when added by users …
Browse files Browse the repository at this point in the history
…who have them actively blocked. (#1105)
  • Loading branch information
Luke Gehorsam authored Oct 17, 2023
1 parent 378dab6 commit 08c63d2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ The format is based on [keep a changelog](http://keepachangelog.com) and this pr
- Fix storage index listing results sometimes being returned with incorrect order.
- Fixes calculation of leaderboard and tournament times for rare types of CRON expressions that don't execute at a fixed interval.
- Improved how start and end times are calculated for tournaments occuring in the future.
- Fix users receiving friend request notifications when added by users who have blocked them.

### [3.17.1] - 2023-08-23
### Added
Expand Down
28 changes: 14 additions & 14 deletions server/core_friend.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,20 @@ func AddFriends(ctx context.Context, logger *zap.Logger, db *sql.DB, messageRout
notificationToSend = make(map[string]bool)

for id := range uniqueFriendIDs {
// Check to see if user has already blocked friend, if so, don't add friend or send notification.
var blockState int
err := tx.QueryRowContext(ctx, "SELECT state FROM user_edge WHERE source_id = $1 AND destination_id = $2 AND state = 3", userID, id).Scan(&blockState)
// ignore if the error is sql.ErrNoRows as means block was not found - continue as intended.
if err != nil && err != sql.ErrNoRows {
// genuine DB error was found.
logger.Debug("Failed to check edge state.", zap.Error(err), zap.String("user", userID.String()), zap.String("friend", id))
return err
} else if err == nil {
// the block was found, don't add friend or send notification.
logger.Info("Ignoring previously blocked friend. Delete friend first before attempting to add.", zap.String("user", userID.String()), zap.String("friend", id))
continue
}

isFriendAccept, addFriendErr := addFriend(ctx, logger, tx, userID, id)
if addFriendErr == nil {
notificationToSend[id] = isFriendAccept
Expand Down Expand Up @@ -273,20 +287,6 @@ func AddFriends(ctx context.Context, logger *zap.Logger, db *sql.DB, messageRout

// Returns "true" if accepting an invite, otherwise false
func addFriend(ctx context.Context, logger *zap.Logger, tx *sql.Tx, userID uuid.UUID, friendID string) (bool, error) {
// Check to see if user has already blocked friend, if so ignore.
var blockState int
err := tx.QueryRowContext(ctx, "SELECT state FROM user_edge WHERE source_id = $1 AND destination_id = $2 AND state = 3", userID, friendID).Scan(&blockState)
// ignore if the error is sql.ErrNoRows as means block was not found - continue as intended.
if err != nil && err != sql.ErrNoRows {
// genuine DB error was found.
logger.Debug("Failed to check edge state.", zap.Error(err), zap.String("user", userID.String()), zap.String("friend", friendID))
return false, err
} else if err == nil {
// the block was found, return early.
logger.Info("Ignoring previously blocked friend. Delete friend first before attempting to add.", zap.String("user", userID.String()), zap.String("friend", friendID))
return false, nil
}

// Mark an invite as accepted, if one was in place.
res, err := tx.ExecContext(ctx, `
UPDATE user_edge SET state = 0, update_time = now()
Expand Down

0 comments on commit 08c63d2

Please sign in to comment.