No need to continue the loop if we've found what we're looking for.
add a filter for user and media id
Don't query for permission value every iteration
Grant view permission the the user is a member of any of the feeds
move viewable permission logic

Move it to the media object.