4

Currently in my app I store different forum posts. Users can add new messages and create new posts. Other users - while displaying the content - can filter it so that they will not see the content uploaded by specific users that they blocked earlier.

Each user is represented as a combination of device_id and display_name.

When users are fetching content, they do a post query and include an array of previously blocked users:

"blockedUsers": (
{
    "device_id" = "XXX";
    "display_name" = XXX;
},
{
    "device_id" = "YYY";
    "display_name" = YYY;
}
)

Currently there's a possibility of adding content with some registered display_name or anonymously.

Right now I support only filtering users with some display_name:

if(blockedUsers) {
    var excludedUsernames = [];

    for(var i in blockedUsers) {
         excludedUsernames.push(blockedUsers[i]['display_name']);
    }

    query.$and.push({ 'display_name': { $nin: excludedUsernames } });
}

That works fine - when user is registered and has some display_name (that is different than anonymous) - I don't care about his device_id, all I care is to block him by his display_name.

But, when some anonymous users has been blocked before:

"blockedUsers": (
{
    "device_id" = "XXX";
    "display_name" = "anonymous"; //represents anonymous user
},
{
    "device_id" = "YYY";
    "display_name" = "anonymous"; //represents anonymous user
}
)

I need to exclude his posts by device_id and the display_name == anonymous. In that case when there's some device_id but contains display_name != anonymous - I don't want to block that.

I don't know how to update my query so that it covers the requirement from above, could you help me with that? Thanks!

2
  • I think your post hasn't been answered the first time you posted it because it's really hard to follow what you're asking. For the sake of clarification, using pseudo code, am I correct that what you're trying to solve is a query like: !((displayName IN arrayOfBlockedNames) OR (displayName == 'anonymous' AND device_id IN arrayOfBlockedDevices)). Am I correct? Commented Dec 12, 2016 at 15:41
  • Paul, yes, you are correct Commented Dec 12, 2016 at 23:58

1 Answer 1

2
+50

If I understood well:

  • If user has a banned device_id, then block him
  • If user has a banned display_name, then block him

In that case, it does not really matter if he is anonymous or not.


let excludedUsernames, excludedDevices;

blockedUsers.forEach((e) => {
    excludedUsernames.puhs({ e["display_name"] });
    excludedDevices.push({ e["device_id"] });
});

query.$and.push({ 'display_name' : { $nin: excludedUsernames } });
query.$and.push({ 'device_id'    : { $nin: excludedDevices   } });

EDIT

query.$or.push({
    $and: [
        { 'device_id'    : { $nin: excludedDevices }},
        { 'display_name' : "anonymous" }
    ]
});
query.$or.push({ 'display_name' : { $nin: excludedUsernames } });
Sign up to request clarification or add additional context in comments.

5 Comments

yes, almost - one more thing - user can access my app with same device_id, but different display_names. Then I need to block a device_id only when display_name == anonymous.
Edited my answer.
thanks for your help! I had to modify the code slightly, now it looks like this: excludedUsernames.push({display_name:e['display_name']}); excludedDevices.push({device_id:e['device_id']}); (before it was raising an error), however now I'm getting another error in line query.$or.push({ $and: [... - it says: TypeError: Cannot read property 'push' of undefined
That is because your object query has no property $or. Where you first initialized it, you must have specified a $and property. Change it to $or.
thank you for your help, I found your answer really helpful! Currently I struggle with different problem, maybe you will be able to help me, I posted a question here stackoverflow.com/questions/41212600/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.