Fix for checking of child element

Sorry for the big cut and paste but this seemed the easiest way......

@dodo @schnouki see report_bugs@topics.buddycloud.org

lloyd@evilprofessor.co.uk
When posting to a channel I get a complaint from the server of:

<iq id="1" to="twinkle@my-server/client" from="channels.my-server" type="error"><error code="-1" type="CANCEL"><text xml:lang="en" xmlns="urn:ietf:params:xml:ns:xmpp-stanzas">Object has no method 'is'</text></error></iq>

It appears this is happening in normalize.js:23. The input stanza looks like this:

<iq from="twinkle@my-server/client" to="channels.my-server" type="set" id="1" xmlns="jabber:client">
    <pubsub xmlns="http://jabber.org/protocol/pubsub">
        <publish node="/user/twinkle@my-server/posts">
            <item>
                <entry xmlns="http://www.w3.org/2005/Atom" xmlns:thr="http://purl.org/syndication/thread/1.0">
                    <content>post</content>
                    <author>
                        <name>twinkle@my-server</name>
                    </author>
                    <updated>2012-08-08T14:00:30.740Z</updated>
                </entry>
            </item>
        </publish>
    </pubsub>
</iq>


This is on version 0.3.4. I'm wondering what is going on with this as running via the webclient is working just fine (in fact I used the actual stanza it sends as a template).

3 hours agoschnouki@pouet.im
Wow, that's weird. And bad. Do you have something in the server log about that error?
2 hours agododo@buddycloud.org
i think the bug is somewhere in here: https://github.com/buddycloud/buddycloud-server/blob/master/src/local/operations.coffee#L506-525

actually this is really bad code style, where even coffeescript cannot help. why are there two variables called item?

specially line 517, which item is meant? is supposed to be the inner item, which is atm returned at that position, or should be the outer item return instead?

@schnouki (and everybody else), any guesses?

2 hours agoschnouki@pouet.im
Wow, that's ugly.
On line 517 item is the inner one (oldItem in the next callback), which can be null if publishing a new item (instead of updating an old one).
2 hours agolloyd@evilprofessor.co.uk
What I don't understand is why this works when going via the webclient but the smack client I'm using (from the guys who bring you openfire) is causing this error - seeing as the stanza is almost copied verbatim.
2 hours agododo@buddycloud.org
lloyd, can you diff both stanzas?
2 hours agoschnouki@pouet.im
And post relevant parts of the server log?
an hour agolloyd@evilprofessor.co.uk
I've copied the relevant parts of the buddycloud logs into here --> https://gist.github.com/3302529
54 minutes agoschnouki@pouet.im
So the problem is here: https://github.com/buddycloud/buddycloud-server/blob/master/src/normalize.coffee#L104. That's a bug: the ltx doc (https://github.com/astro/ltx#element-attributes) says that children is "an Array of Strings and Elements". Here there's a string, as showed in your log (line 39). But it's treated as an Element with an 'is' method.

Could you please try adding "typeof child != 'string' and" in the tests:

    alreadyPresent = req.item.el.children.some (child) ->
        typeof child != 'string' and
        child.is('link') and
        child.attrs.rel is 'self' and
        child.attrs.href is link

(although it would be nicer to test if it's an instance of Element)