Stanza.io plugins XEP-0012: Last Activity

598 views Asked by At

I want to implement XEP-0012 when by default its not supported by Stanza.io.

Im following how to create plugin from its documentation but its not working.

Heres my code (last.js):

'use strict';

module.exports = function (client) {

    client.disco.addFeature('jabber:iq:last');

    client.on('jabber:iq:last', function (iq) {
        client.sendIq(iq.resultReply({
            last: new Date().getTime()
        }));
    });

    client.getLastActivity = function (jid, cb) {
        return this.sendIq({
            to: jid,
            type: 'get',
            last: true
        }, cb);
    };

};

and then i use it with: client.use(require('last'))

but when i call the function its returning an error bad request <bad-request xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/></error>

what am i missing?

thanks

2

There are 2 answers

0
Sakshi Nagpal On

We basically need to create a custom stanza and send it as iq

this.client.use(this.setCustomMessage.bind(this));
setCustomMessage(client, stanzas) {
const lastActivity = stanzas.define({
    name: 'lastActivity',
    element: 'query',
    namespace: 'jabber:iq:last',
    fields: {
    seconds: stanzas.utils.attribute('seconds')
    }
});
stanzas.withIQ(iq => {
            stanzas.extend(iq, lastActivity);
        });
}

and to get the lastActivity of a user having jid as id

getLastActivity(userId, cb) {
        return this.client.sendIq({
            to: 'userId',
            type: 'get',
            id: 'last',
            query: true
        }, cb);
    }
}

you can get the result of last Activity in two ways: Either getting the response from promise

this.getLastActivity(userId).then(data => {
    if (data && data.query && data.query.seconds) {
    // Math.round(new Date())-(parseInt(data.query.seconds, 10)*1000) 
       will give last activity 
    }
});

or

client.on('iq', data => {
    if (data.query && data.query.seconds) {
    // Math.round(new Date())-(parseInt(data.query.seconds, 10)*1000) 
       will give last activity 
    }
});
0
Israt Jahan Simu On
module.exports = function (client, stanzas) {

// 1. Create and register our custom `mystanza` stanza type

const helpers = stanzas.utils;

const IqLastActivity = stanzas.define({
    name: 'query',
    element: 'query',
    namespace: 'jabber:iq:last',
    fields: {
        seconds: helpers.attribute('seconds')
    }
});

stanzas.withIq((Iq) => {
    stanzas.extend(Iq, IqLastActivity);
});

// eslint-disable-next-line no-param-reassign
client.lastActivityByIq = (data) => {
    client.sendIq(data);
};
client.on('iq', (iq) => {
    if (iq.query) {
        client.emit('iq:last', iq);
    }
});

client.on('iq:last', (iq) => {
       console.log('iq:last', iq);
});
};

add this code to your client file

import IqLastActivity from './PathToFile/iqLastActivity';

client.use(IqLastActivity);