Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix users receiving friend request notifications when added by users who have them actively blocked. #1105

Merged
merged 4 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading