I am having some issues with setting custom user properties. Basically
when a user's property is set on the server, under some circumstances,
the property is not reflected correctly on the client.

Am describing below, the details of my situation. I have also
described the workaround to this problem, but would appreciate if
someone could help with a neater way out.

====== details ======
I have implemented a custom server command to set some custom user
properties, as follows:

---- server-side code -----
Ape.registerCmd('setuserdata', true, function(params, infos) {
        Ape.log('received userdata for '+infos.user.getProperty('name'));
        if (!$defined(params.userdata)) return ['111','user data not
provided'];

        infos.user.setProperty('userdata', params.userdata);

        var chan = Ape.getChannelByPubid(params.pipe);

        if (chan) {
                chan.pipe.sendRaw('userdata', {'userdata': params.userdata},
{'from': infos.user.pipe});
        } else {
                return ['109', 'UNKNOWN_PIPE'];
        }

        return 1;
});

Also, to ensure that the custom data is set during login, have done
the following changes to nickname.js:
Ape.registerHookCmd("connect", function(params, cmd) {
        if (!$defined(params) || !$defined(params.name)) return 0;

        if (userlist.has(params.name.toLowerCase())) return ["007",
"NICK_USED"];
        if (params.name.length > 16 || params.name.test('[^a-zA-Z0-9]', 'i'))
return ["006", "BAD_NICK"];

        cmd.user.setProperty('name', params.name);

        // set custom user data
        if ($defined(params.userdata)) {
                cmd.user.setProperty('userdata', params.userdata);
        }
        return 1;
});

---- client-side code -----
On the client, for changing the currently logged in user's data have a
function like this:
function setUserData(data) {
     pipe.request.send('setuserdata', {'userdata':data}); // where
pipe is retrieved from the channel... code not shown
}

for catching 'userJoin', have something like:
pipe.addEvent('userJoin', function(user, pipe) {
        log(user.properties.userdata,pipe.properties.name,pipe.properties);
});

and for catching the userdata event from other users:
ape.onRaw('userdata', function(raw,pipe) {
        
log(raw.data.from.properties.name,raw.data.userdata,pipe.properties.name);
});

----- test case -----
Now suppose we have the following sequence of events:
1 - user A joins channel C1 and initializes userdata={'p1':'v1'}
2 - user B joins channel C1 and initializes userdata={'p1':'v1'}
3- user A gets a 'userJoin' notification for user B, with
userdata={'p1':'v1'}
4- user B updates userdata by calling setUserData({'p1':'v2'})
5- user A gets event "userdata" and receives userdata{'p1':'v2'}
6- user B leaves channel C1
7- user A gets 'userLeft' event
8- user B joins channel C1
9- user A gets 'userJoin' notification for user B with
userdata={'p1':'v1'}

As you can see, in step 9, A should have actually received userdata =
{'p1':'v2'} as set in step 5 and not {'p1':'v1'}

Observation:
in firebug, I noticed a users hash in ape core. On viewing
ape.users.get(userB.pubid), I saw that the properties still reflect
userdata:{'p1':'v1'}

Workaround:
ape.onRaw('userdata', function(raw,pipe) {
        apeuser.properties.userdata=raw.data.userdata; // workaround:
just extend the hash
        
log(raw.data.from.properties.name,raw.data.userdata,pipe.properties.name);
});

-- 
You received this message because you are subscribed to the Google
Groups "APE Project" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/ape-project?hl=en
---
APE Project (Ajax Push Engine)
Official website : http://www.ape-project.org/
Git Hub : http://github.com/APE-Project/

Reply via email to