Sorry, I actually don't think you can solve this with queries only. I think you need to do it like this:
- Get a list of all PhotoJourney objects where photo matches the current photo.
- Create a list of user objects by retrieving the
photoSentTo
object in allPhotoJourney
objects (excludeUsers) - Query for all users (allUsers)
- As you now have two lists of user objects, filter out the objects from allUsers that exist in excludeUsers
The resulting list is the list you can use for getting a random user.
This is quite a heavy operation if you have lots of users. You should create a cloud code function that does this job on the server and only returns the random user object.
As an alternative, you could create a new field on the Photo object (or create a join table for it) that contains an array of pointers to all the users that have received the Photo. Then, when querying for users, you can exclude users that exist in this list. Or the array could contain only the objectIDs, and you can use that as a constraint in your User query:
userQuery.notContainedIn("objectId", photo.get("sentToUsers")); // Array of objectIds of receivers