Saving and changing friend metadata

Hi,

I’m trying to use new Nakama feature to save friend metadata to mark them as favorite. The only method to actually change friend metadata seems to be Client.AddFriendsAsync() and providing metadata parameter, which I do, but server does not seem to save it, because I’m getting empty friend metadata back when I call it with NakamaClient.ListFriendsAsync()

I’m using Nakama 3.26 and latest Unity SDK. Thanks for advices.

Method to save metadata:

private void UpdateFriendMetadata(string friendId, string keyToWrite, object valueToWrite)
{
    Dictionary<string, string> metadata = new()
    {
       { keyToWrite, valueToWrite.ToString().ToLower() }
    };

    string metadataString = Nakama.TinyJson.JsonWriter.ToJson(metadata);

    AddFriend(friendId, metadataString);
}

Method for adding friend:

public async void AddFriend(string userID, string metadata = "")
{
    string[] usernames =
    {
       userID
    };

    await NakamaClient.AddFriendsAsync(CurrentSession, usernames, usernames: Array.Empty<string>(), metadata: metadata);
}

Hello @MichalKrela

First off, I’d advise you to upgrade to latest Nakama v3.29.0

Then, I’d just like to clarify that the metadata can be different in each direction of the friends graph edge, so if A calls AddFriend to add B, it would add the metadata from A→B (returned when A lists friends) and when B calls AddFriend to accept, it could set it’s own metadata field from B→A (returned when B lists his friends).

However, to update the metadata at a later stage, you’d have to use the FriendMetadataUpdate function, probably wrapped in a custom RPC. You should be mindful that whatever is passed to metadata in this function, will overwrite whatever was there, so if you need to do incremental updates, you should read the value and modify it before you call the function to update it.

If you’re still seeing issues, please report back, it may be a bug.

Hope this helps.

Hi @sesposito,

thanks for you answer, I did exactly what you suggested and it is working fine for our needs, I’m glad you implemented this feature, thank you!