fix xmpp reading of message extensions

This commit is contained in:
Martin/Geno 2019-06-06 22:54:45 +02:00
parent b72998ae4b
commit e1585612fa
No known key found for this signature in database
GPG Key ID: 9D7D3C6BFF600C6A
2 changed files with 46 additions and 28 deletions

View File

@ -19,29 +19,38 @@ func (a *Account) Send(to string, msg xmpp.Message) error {
return nil return nil
} }
func (a *Account) sending(to string, msg xmpp.Message) (o3.Message, error) { func (a *Account) sending(to string, msg xmpp.Message) (o3.Message, error) {
logger := log.WithFields(map[string]interface{}{
"from": a.XMPP.String(),
"to": to,
})
chatState := false chatState := false
chatStateComposing := false chatStateComposing := false
msgStateID := "" msgStateID := ""
msgStateRead := false msgStateRead := false
for _, el := range msg.Extensions { for _, el := range msg.Extensions {
switch ex := el.(type) { switch ex := el.(type) {
case xmpp.StateActive:
case *xmpp.StateActive:
chatState = true chatState = true
case xmpp.StateComposing: case *xmpp.StateComposing:
chatState = true chatState = true
chatStateComposing = true chatStateComposing = true
case xmpp.StateGone: case *xmpp.StateGone:
chatState = true chatState = true
case xmpp.StateInactive: case *xmpp.StateInactive:
chatState = true chatState = true
case xmpp.StatePaused: case *xmpp.StatePaused:
chatState = true chatState = true
case xmpp.ReceiptReceived:
case *xmpp.ReceiptReceived:
msgStateID = ex.ID msgStateID = ex.ID
case xmpp.MarkReceived: case *xmpp.MarkReceived:
msgStateID = ex.ID msgStateID = ex.ID
case xmpp.MarkDisplayed:
case *xmpp.MarkDisplayed:
msgStateRead = true msgStateRead = true
msgStateID = ex.ID msgStateID = ex.ID
} }
@ -60,18 +69,22 @@ func (a *Account) sending(to string, msg xmpp.Message) (o3.Message, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
log.WithFields(map[string]interface{}{ logger.WithFields(map[string]interface{}{
"tid": to,
"msg_id": id, "msg_id": id,
"type": msgType, "type": msgType,
}).Debug("update status of threema message") }).Debug("update status of threema message")
return drm, nil return drm, nil
} }
if chatStateComposing {
return nil, nil
}
if chatState { if chatState {
return nil, nil tnm := o3.TypingNotificationMessage{}
if chatStateComposing {
tnm.OnOff = 0x1
}
logger.WithFields(map[string]interface{}{
"state": chatStateComposing,
}).Debug("send typing")
return tnm, nil
} }
} }
@ -82,5 +95,10 @@ func (a *Account) sending(to string, msg xmpp.Message) (o3.Message, error) {
} }
a.deliveredMSG[msg3.ID()] = msg.Id a.deliveredMSG[msg3.ID()] = msg.Id
a.readedMSG[msg3.ID()] = msg.Id a.readedMSG[msg3.ID()] = msg.Id
logger.WithFields(map[string]interface{}{
"x_id": msg.Id,
"t_id": msg3.ID(),
"text": msg.Body,
}).Debug("send text")
return msg3, nil return msg3, nil
} }

View File

@ -34,7 +34,7 @@ func TestAccountSend(t *testing.T) {
err := a.Send("a", xmpp.Message{ err := a.Send("a", xmpp.Message{
PacketAttrs: xmpp.PacketAttrs{From: "a@example.org"}, PacketAttrs: xmpp.PacketAttrs{From: "a@example.org"},
Extensions: []xmpp.MsgExtension{ Extensions: []xmpp.MsgExtension{
xmpp.ReceiptReceived{ID: "blub"}, &xmpp.ReceiptReceived{ID: "blub"},
}, },
}) })
assert.Error(err) assert.Error(err)
@ -51,7 +51,7 @@ func TestAccountSendingDeliviery(t *testing.T) {
msg, err := a.sending("a", xmpp.Message{ msg, err := a.sending("a", xmpp.Message{
PacketAttrs: xmpp.PacketAttrs{From: "a@example.org"}, PacketAttrs: xmpp.PacketAttrs{From: "a@example.org"},
Extensions: []xmpp.MsgExtension{ Extensions: []xmpp.MsgExtension{
xmpp.ReceiptReceived{ID: "blub"}, &xmpp.ReceiptReceived{ID: "blub"},
}, },
}) })
assert.Error(err) assert.Error(err)
@ -61,7 +61,7 @@ func TestAccountSendingDeliviery(t *testing.T) {
msg, err = a.sending("a", xmpp.Message{ msg, err = a.sending("a", xmpp.Message{
PacketAttrs: xmpp.PacketAttrs{From: "a@example.org"}, PacketAttrs: xmpp.PacketAttrs{From: "a@example.org"},
Extensions: []xmpp.MsgExtension{ Extensions: []xmpp.MsgExtension{
xmpp.MarkReceived{ID: "3"}, &xmpp.MarkReceived{ID: "3"},
}, },
}) })
assert.NoError(err) assert.NoError(err)
@ -73,7 +73,7 @@ func TestAccountSendingDeliviery(t *testing.T) {
msg, err = a.sending("a", xmpp.Message{ msg, err = a.sending("a", xmpp.Message{
PacketAttrs: xmpp.PacketAttrs{From: "a@example.org"}, PacketAttrs: xmpp.PacketAttrs{From: "a@example.org"},
Extensions: []xmpp.MsgExtension{ Extensions: []xmpp.MsgExtension{
xmpp.MarkDisplayed{ID: "5"}, &xmpp.MarkDisplayed{ID: "5"},
}, },
}) })
assert.NoError(err) assert.NoError(err)
@ -94,7 +94,7 @@ func TestSendTyping(t *testing.T) {
msg, err := a.sending("a", xmpp.Message{ msg, err := a.sending("a", xmpp.Message{
PacketAttrs: xmpp.PacketAttrs{From: "a@example.org"}, PacketAttrs: xmpp.PacketAttrs{From: "a@example.org"},
Extensions: []xmpp.MsgExtension{ Extensions: []xmpp.MsgExtension{
xmpp.StateComposing{}, &xmpp.StateComposing{},
}, },
}) })
assert.NoError(err) assert.NoError(err)
@ -104,10 +104,10 @@ func TestSendTyping(t *testing.T) {
msg, err = a.sending("a", xmpp.Message{ msg, err = a.sending("a", xmpp.Message{
PacketAttrs: xmpp.PacketAttrs{From: "a@example.org"}, PacketAttrs: xmpp.PacketAttrs{From: "a@example.org"},
Extensions: []xmpp.MsgExtension{ Extensions: []xmpp.MsgExtension{
xmpp.StateActive{}, &xmpp.StateActive{},
xmpp.StateGone{}, &xmpp.StateGone{},
xmpp.StateInactive{}, &xmpp.StateInactive{},
xmpp.StatePaused{}, &xmpp.StatePaused{},
}, },
}) })
assert.NoError(err) assert.NoError(err)
@ -117,11 +117,11 @@ func TestSendTyping(t *testing.T) {
msg, err = a.sending("a", xmpp.Message{ msg, err = a.sending("a", xmpp.Message{
PacketAttrs: xmpp.PacketAttrs{From: "a@example.org"}, PacketAttrs: xmpp.PacketAttrs{From: "a@example.org"},
Extensions: []xmpp.MsgExtension{ Extensions: []xmpp.MsgExtension{
xmpp.StateActive{}, &xmpp.StateActive{},
xmpp.StateComposing{}, &xmpp.StateComposing{},
xmpp.StateGone{}, &xmpp.StateGone{},
xmpp.StateInactive{}, &xmpp.StateInactive{},
xmpp.StatePaused{}, &xmpp.StatePaused{},
}, },
Body: "hi", Body: "hi",
}) })