// Check to see if user has already blocked friend, if so ignore.
rows,err:=tx.QueryContext(ctx,"SELECT state FROM user_edge WHERE source_id = $1 AND destination_id = $2 AND state = 3",userID,friendID)
iferr!=nil{
iferr==sql.ErrNoRows{
logger.Info("Ignoring previously blocked friend. Delete friend first before attempting to add.",zap.String("user",userID.String()),zap.String("friend",friendID))
returnfalse,sql.ErrNoRows
}
varblockStateint
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.
iferr!=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))
returnfalse,err
}elseiferr==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))
returnfalse,nil
}
// We don't need the result, it only matters if there was one.
rows.Close()
// Mark an invite as accepted, if one was in place.
res,err:=tx.ExecContext(ctx,`
UPDATE user_edge SET state = 0, update_time = now()
WHERE (source_id = $1 AND destination_id = $2 AND state = 2)
OR (source_id = $2 AND destination_id = $1 AND state = 1)
WHERE (source_id = $1 AND destination_id = $2 AND state = 1)
OR (source_id = $2 AND destination_id = $1 AND state = 2)
`,friendID,userID)
iferr!=nil{
logger.Debug("Failed to update user state.",zap.Error(err),zap.String("user",userID.String()),zap.String("friend",friendID))
@@ -238,8 +240,8 @@ OR (source_id = $2 AND destination_id = $1 AND state = 1)
INSERT INTO user_edge (source_id, destination_id, state, position, update_time)