librelist archives

« back to archive

Decoding object returned from listen.on('messageSend',msg)

Decoding object returned from listen.on('messageSend',msg)

From:
Simon Hill
Date:
2015-03-10 @ 18:08
I need to listen on messageSend event to check if the recipient is offline
and if offline, re-route the message to a different channel.

converse.listen.on('messageSend', function (msg) {
    $log.info(msg);

});

messageSendWhen a message will be sent out.converse.on('messageSend',
function (messageText) { ...});

However, 'msg' is returns a gigantic object and I don't know which way is
up.

How can I extract the recipient, the message, and if you don't mind, can
you suggest how I look up to find out if recipient is online? (I assume I
read the chat_status of their contacts object).

Thanks !
Si

Re: [conversejs] Decoding object returned from listen.on('messageSend', msg)

From:
Jc Brand
Date:
2015-03-11 @ 11:57
On Tue, Mar 10, 2015 at 11:08:12AM -0700, Simon Hill wrote:
>    I need to listen on messageSend event to check if the recipient is offline
>    and if offline, re-route the message to a different channel.
> 
>  converse.listen.on('messageSend', function (msg) {
>      $[1]log.info(msg);
> 
>  });
> 
>              When a
>              message
>  messageSend will be  converse.on('messageSend', function (messageText) { ...});
>              sent
>              out.
> 
>    However, 'msg' is returns a gigantic object and I do n't know which way is
>    up.
>    How can I extract the recipient, the message, and if you don't mind, can
>    you suggest how I look up to find out if recipient is online? (I assume I
>    read the chat_status of their contacts object).

The object returned is a Backbone Model with the following attributes:

* chat_state
* delayed
* fullname
* message
* msgid
* sender
* time

So you can get the message text via msg.get('message')

To get the recipient, you call the "converse.contacts.get" API method which will
return an object with all attributes of that contact.

Writing this now, I see that it's probably confusing when Backbone models are
in some cases returned and in other cases normal objects are returned.

This is partly because I try to hide complexity and also internal data and
methods but also because I personally never use the API.

Because I don't use it, I don't always encounter such inconsistencies.

Re: [conversejs] Decoding object returned from listen.on('messageSend', msg)

From:
Simon Hill
Date:
2015-03-11 @ 15:38
Sorry, may be I misunderstood, but

msg.get('message')

is undefined

I've never really understood Backbone. There's not 'get' method on the
object returned, that I can see.

Could you clarify?

Thanks!

On Wed, Mar 11, 2015 at 4:57 AM, JC Brand <lists@opkode.com> wrote:

> On Tue, Mar 10, 2015 at 11:08:12AM -0700, Simon Hill wrote:
> >    I need to listen on messageSend event to check if the recipient is
> offline
> >    and if offline, re-route the message to a different channel.
> >
> >  converse.listen.on('messageSend', function (msg) {
> >      $[1]log.info(msg);
> >
> >  });
> >
> >              When a
> >              message
> >  messageSend will be
> converse.on('messageSend', function (messageText) { ...});
> >              sent
> >              out.
> >
> >    However, 'msg' is returns a gigantic object and I do n't know which
> way is
> >    up.
> >    How can I extract the recipient, the message, and if you don't mind,
> can
> >    you suggest how I look up to find out if recipient is online? (I
> assume I
> >    read the chat_status of their contacts object).
>
> The object returned is a Backbone Model with the following attributes:
>
> * chat_state
> * delayed
> * fullname
> * message
> * msgid
> * sender
> * time
>
> So you can get the message text via msg.get('message')
>
> To get the recipient, you call the "converse.contacts.get" API method
> which will
> return an object with all attributes of that contact.
>
> Writing this now, I see that it's probably confusing when Backbone models
> are
> in some cases returned and in other cases normal objects are returned.
>
> This is partly because I try to hide complexity and also internal data and
> methods but also because I personally never use the API.
>
> Because I don't use it, I don't always encounter such inconsistencies.
>

Re: [conversejs] Decoding object returned from listen.on('messageSend', msg)

From:
Jc Brand
Date:
2015-03-13 @ 23:00

On 11 Maart 2015 4:38:21 nm. CET, Simon Hill <simonhill303@gmail.com> wrote:
>Sorry, may be I misunderstood, but
>
>msg.get('message')
>
>is undefined
>
>I've never really understood Backbone. There's not 'get' method on the
>object returned, that I can see.
>
>Could you clarify?

Then maybe I was wrong to think it was a backbone object.

Just play around with it and inspect it in the browser console. I'm sure 
you'll figure it out.


>On Wed, Mar 11, 2015 at 4:57 AM, JC Brand <lists@opkode.com> wrote:
>
>> On Tue, Mar 10, 2015 at 11:08:12AM -0700, Simon Hill wrote:
>> >    I need to listen on messageSend event to check if the recipient
>is
>> offline
>> >    and if offline, re-route the message to a different channel.
>> >
>> >  converse.listen.on('messageSend', function (msg) {
>> >      $[1]log.info(msg);
>> >
>> >  });
>> >
>> >              When a
>> >              message
>> >  messageSend will be
>> converse.on('messageSend', function (messageText) { ...});
>> >              sent
>> >              out.
>> >
>> >    However, 'msg' is returns a gigantic object and I do n't know
>which
>> way is
>> >    up.
>> >    How can I extract the recipient, the message, and if you don't
>mind,
>> can
>> >    you suggest how I look up to find out if recipient is online? (I
>> assume I
>> >    read the chat_status of their contacts object).
>>
>> The object returned is a Backbone Model with the following
>attributes:
>>
>> * chat_state
>> * delayed
>> * fullname
>> * message
>> * msgid
>> * sender
>> * time
>>
>> So you can get the message text via msg.get('message')
>>
>> To get the recipient, you call the "converse.contacts.get" API method
>> which will
>> return an object with all attributes of that contact.
>>
>> Writing this now, I see that it's probably confusing when Backbone
>models
>> are
>> in some cases returned and in other cases normal objects are
>returned.
>>
>> This is partly because I try to hide complexity and also internal
>data and
>> methods but also because I personally never use the API.
>>
>> Because I don't use it, I don't always encounter such
>inconsistencies.
>>

-- 
Diese Nachricht wurde von meinem Android-Mobiltelefon mit K-9 Mail gesendet.