Partitioning a friend request system?

Hello, I am trying to build a friend request system. The tricky part is that certain user info is available only to friends.

Currently I have multiple objects (with mostly duplicated data):

  • User - accessed only by currently logged in user (partition: user=user.id)
  • PublicUser - accessed by all users (partition: user=all-the-users)
  • FriendUser - accessed only by friends (partition: I have no clue how to properly partition it)

I also have an object(collection) Friendship. There I store:

  • receiver_id
  • requester_id
  • accepted

Is there a proper and more scalable way of building this? I want to avoid having an array of friend ids under the main user object.

Thank you all!

HI @dimo, welcome to the community forum!

For your partition key, I’d suggest creating a String attribute called partition. User partition as the partion key. partition will be set to <key>=<value>, and <key> can be different for different collections.

For the User collection, docs would include partition: "user=878275838475834".

For the PublicUser collection, docs would include partition: "manyUsers=all-the-users".

For the FriendUser collection, docs would include partition: "friendlyUser=878275838475834".

Your sync permissions can then call a Realm function that will make different checks, depending on the “type” of key and the value:

  • If key == "user" then check that this is the correct user asking (by comparing with the value)
  • If key == "manyUsers" && value == "all-the-users" then return true
  • If key == "friendlyUser" then check if the current user is allowed to access data for the user represented by value (e.g. by checking that user’s data to see if the current user has been added as a friend).
2 Likes

Hey @Andrew_Morgan ! Thanks for the quick reply!

This will work! I think I got it from a different perspective, I was trying to have a value of “friends-only” and got super confused how I am going to check this. Your suggestion solves everything!

Thanks for your time!

@dimo glad that this works for you!

For future projects, I’ve just published an article on Realm provisioning strategies: https://www.mongodb.com/how-to/realm-partitioning-strategies/

2 Likes

Amazing! Thanks again @Andrew_Morgan!

I am facing another problem because of the partition strategy. It’s related to accepting, declining and canceling the friend request but I will open another thread because it’s Swift related.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.