Is the user a friend?

Hi,

is there a method to find out if a user is our friend without retrieving the friends list ?

thanks

@Philippe There’s no function in the server framework for this feature because it would undermine the privacy of the user in the game. For example the option for user A to check whether random user B has user X on their friend list would be invasive. You can fetch the current user’s friend list and loop over it to check whether they have user B as a friend.

ok thanks for the quick response. i will do that.

Bye

I don’t k now if this solution is the best approach but you can use RPC call, passing the player user-id as payload to be check.

func checkIfPlayerIsFriend(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, payload string) (string, error) {
	userID, ok := ctx.Value(runtime.RUNTIME_CTX_USER_ID).(string)
	if !ok {
		return "", errNoUserIdFound
	}
	userIDToBeChecked := payload //validate userIDToBeChecked make sure it's a valid user-id (caution : be aware of SQL injection ..


	query := `
SELECT state
FROM user_edge
WHERE (destination_id =$1 AND source_id = $2 AND state = $3)
OR (destination_id =$2 AND source_id = $1 AND state = $3)`

	ee, err := db.Exec(query,userID,userIDToBeChecked,0) //  $3 = 0 (as code 0 --> Users are friends with each other.)
	if err != nil {
		return "",errInternalError
	}

	rowAffectedCount,_ :=ee.RowsAffected()
	if rowAffectedCount == 0{
		return "N",nil //not friends
	}
	return "Y",nil // yay, player is a friend (nakama)
}

You can even modify the code to check what state you have with the player.

@Mohammed You can of course drop down to raw SQL to get the information you want from the social graph. I would recommend against it for the reasons above.

@novabyte but on the code above the check will be between the rpc caller and the another user id on the playload.

Code above is expecting one userid only, the other user is will be taken from. Context value

So you cant check two random players

Or am I missing something about how RPC works. Is it possible to impersonate another player?

thanks for these solutions.
@Mohammed I used the novabyte solution because if one day, the database model changes, the code will have to be modified but I keep this method if I have other problems. Thanks

@Philippe I agree with you, I actually forget about " the database model changes" :sweat: