Skip to content

Instantly share code, notes, and snippets.

@knixeur
Last active August 29, 2015 14:14
Show Gist options
  • Save knixeur/376078bd6b702255a7e7 to your computer and use it in GitHub Desktop.
Save knixeur/376078bd6b702255a7e7 to your computer and use it in GitHub Desktop.
A simple plugin that let's you open chats or join rooms when converse is initialized
function converseAutoJoinChatPlugin( converse ) {
var jids = [ '[email protected]' ];
converse.on('ready', function() {
var _transform = function (jid) {
var chatbox = converse.chatboxes.get(jid);
if (!chatbox) {
var roster_item = converse.roster.get(jid);
if (roster_item === undefined) {
// Assume MUC
converse.chatboxes.create({
'id': jid,
'jid': jid,
'name': Strophe.unescapeNode(Strophe.getNodeFromJid(jid)),
'nick': Strophe.unescapeNode(Strophe.getNodeFromJid(converse.jid)),
'chatroom': true,
'box_id' : b64_sha1(jid)
});
}
converse.chatboxes.create({
'id': jid,
'jid': jid,
'fullname': _.isEmpty(roster_item.get('fullname'))? jid: roster_item.get('fullname'),
'image_type': roster_item.get('image_type'),
'image': roster_item.get('image'),
'url': roster_item.get('url')
});
}
};
if (typeof jids === "string") {
_transform(jids);
}
_.map(jids, _transform);
});
};
converse.plugins.add('converseAutoJoinChatPlugin', converseAutoJoinChatPlugin);
@gowthaman-murugan
Copy link

Help me anything wrong below code because "rosterItems" always undefined

 function converseGetRosterItems(converse) {
        converse.on('roster', function (item) {
             var rosterItems = converse.roster.get('userA@localhost');
              console.log("......item", rosterItems); ======>undefined
        });
 }
 converse.plugins.add('converseGetRosterItems', converseGetRosterItems);

   converse.initialize({
        allow_otr: true,
        auto_list_rooms: false,
        auto_subscribe: false,
        bosh_service_url: 'http://localhost:5280/http-bind/',  
        debug: true ,
        hide_muc_server: false,
        i18n: locales['en'], // Refer to ./locale/locales.js to see which locales are supported
        prebind: false,
        show_controlbox_by_default: true,
        xhr_user_search: false,
        keepalive : true
    }); 


        converseGetRosterItems(converse);

@knixeur
Copy link
Author

knixeur commented Mar 9, 2015

@gowthaman-i2i my guess is that the contact was not yet initialized so it's not on converse.roster model yet, if you look at the source code, the 'roster' events is called BEFORE it's processed by the rosterHandler method which creates the users in the model if necessary.
Cheers,
Guillermo

@metalaureate
Copy link

@gbonvehi Thank you for this example. I am about to make an unreasonable request, but would you be able to give me some guidance on how to modify your script to create a plugin that opens/joins MUC rooms when a browser event outside converse is triggered? I use converse in the context of a kind of game, where people "win" access to a reserved MUC. The user is made a member of the room server-side; I just need to automate the equivalent of entering the MUC name in the room field and pressing Join.

Your example works for SUCs but am unable to get it to work for MUCs that I can open the manual way. JC says I need to call the .join() method of the ChatRoomView.

@knixeur
Copy link
Author

knixeur commented Apr 16, 2015

@metalaureate
Sure, I'll make a simple (untested) example here to get you started

@knixeur
Copy link
Author

knixeur commented Apr 16, 2015

@rui-wang-codebase
Copy link

@gbonvehi could you please help me? I am new to javascript and could not get the plugin working. is there any instruction on how to use that?

Here is my webpage, I actually didn't use the plugin, and directly try to use it as a function. (I'm still not very clear about the plugin mechanism. After many errors trying, I chose this way).

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>converse.js</title>
    <meta http-equiv="content-type" content="text/html;charset=utf-8" />
    <link rel="stylesheet" media="screen" href="converse.css">
    <script data-main="main" src="require.js"></script>
    <script src="converse.js"></script>
</head>
<body>
    <div id="conversejs"></div>
<script>
    require(['converse'], function (converse) {
        converse.initialize({
            allow_otr: true,
            auto_list_rooms: false,
            auto_subscribe: false,
            bosh_service_url: 'http://127.0.0.1:5280/http-bind/',
            debug: true,
            show_controlbox_by_default: true,
            hide_muc_server: false,
            i18n: locales['en'],
            prebind: false,
            show_controlbox_by_default: true,
            xhr_user_search: false,
        });
        var jids = [ '[email protected]' ];
        converse.listen.on('ready', function(event) {
                    var _transform = function (jid) {
                        var chatbox = converse.chatboxes.get(jid);
                        if (!chatbox) {
                            var roster_item = converse.roster.get(jid);
                            if (roster_item === undefined) {
                                // Assume MUC
                                converse.chatboxes.create({
                                    'id': jid,
                                    'jid': jid,
                                    'name': Strophe.unescapeNode(Strophe.getNodeFromJid(jid)),
                                    'nick': Strophe.unescapeNode(Strophe.getNodeFromJid(converse.jid)),
                                    'chatroom': true,
                                    'box_id' : b64_sha1(jid)
                                });
                            }
                            converse.chatboxes.create({
                                'id': jid,
                                'jid': jid,
                                'fullname': _.isEmpty(roster_item.get('fullname'))? jid: roster_item.get('fullname'),
                                'image_type': roster_item.get('image_type'),
                                'image': roster_item.get('image'),
                                'url': roster_item.get('url')
                            });
                        }
                    };
                    if (typeof jids === "string") {
                        _transform(jids);
                    }
                    _.map(jids, _transform);
        });
        console.log(converse);
     });
</script>
</body>
</html>

and it has an error: "ERROR: User connection callback caused an exception: TypeError: converse.chatboxes is undefined”

but I look at converse.js, that should have been created by the converse.initialize function...:-(

any input will be greatly appreciated.

@knixeur
Copy link
Author

knixeur commented Jul 1, 2015

@rui-wang-codebase
Sorry for the late response, Gist doesn't ping users when they're mentioned here, take a look here conversejs/converse.js#156 (comment) the API was changed a bit.
Cheers,
Guillermo

@themaverik
Copy link

@gbonvehi, thanks for the plugin
I want to develop a plugin for filtering messages using the 'message' event. can you help me with this...thanks in advance

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment