fix send message to threema

This commit is contained in:
Martin/Geno 2019-06-06 22:05:33 +02:00
parent 5f09a187ce
commit 0bd92ec5c3
No known key found for this signature in database
GPG Key ID: 9D7D3C6BFF600C6A
3 changed files with 92 additions and 39 deletions

View File

@ -19,56 +19,60 @@ 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) {
// handle delivered / readed chatState := false
msgID := "" chatStateComposing := false
readed := false msgStateID := ""
composing := false msgStateRead := false
state := false
for _, el := range msg.Extensions { for _, el := range msg.Extensions {
switch ex := el.(type) { switch ex := el.(type) {
case xmpp.StateComposing:
composing = true
state = true
case xmpp.StateInactive:
state = true
case xmpp.StateActive: case xmpp.StateActive:
state = true chatState = true
case xmpp.StateComposing:
chatState = true
chatStateComposing = true
case xmpp.StateGone: case xmpp.StateGone:
state = true chatState = true
case xmpp.StateInactive:
chatState = true
case xmpp.StatePaused:
chatState = true
case xmpp.ReceiptReceived: case xmpp.ReceiptReceived:
msgID = ex.ID msgStateID = ex.ID
case xmpp.MarkReceived: case xmpp.MarkReceived:
msgID = ex.ID msgStateID = ex.ID
case xmpp.MarkDisplayed: case xmpp.MarkDisplayed:
readed = true msgStateRead = true
msgID = ex.ID msgStateID = ex.ID
} }
} }
if composing { if msg.Body == "" {
return nil, nil if msgStateID != "" {
} id, err := strconv.ParseUint(msgStateID, 10, 64)
if state && msg.Body == "" { if err != nil {
return nil, nil return nil, err
} }
if msgID != "" { msgType := o3.MSGDELIVERED
id, err := strconv.ParseUint(msgID, 10, 64) if msgStateRead {
if err != nil { msgType = o3.MSGREAD
return nil, err }
drm, err := o3.NewDeliveryReceiptMessage(&a.Session, to, id, msgType)
if err != nil {
return nil, err
}
log.WithFields(map[string]interface{}{
"tid": to,
"msg_id": id,
"type": msgType,
}).Debug("update status of threema message")
return drm, nil
} }
msgType := o3.MSGDELIVERED if chatStateComposing {
if readed { return nil, nil
msgType = o3.MSGREAD
} }
drm, err := o3.NewDeliveryReceiptMessage(&a.Session, to, id, msgType) if chatState {
if err != nil { return nil, nil
return nil, err
} }
log.WithFields(map[string]interface{}{
"tid": to,
"msg_id": id,
"type": msgType,
}).Debug("update status of threema message")
return drm, nil
} }
// send text message // send text message

View File

@ -81,3 +81,52 @@ func TestAccountSendingDeliviery(t *testing.T) {
assert.True(ok) assert.True(ok)
assert.Equal(o3.MSGREAD, drm.Status()) assert.Equal(o3.MSGREAD, drm.Status())
} }
func TestSendTyping(t *testing.T) {
assert := assert.New(t)
a := Account{
Session: o3.NewSessionContext(o3.ThreemaID{ID: o3.NewIDString("43218765")}),
deliveredMSG: make(map[uint64]string),
readedMSG: make(map[uint64]string),
}
// skip typing messae
msg, err := a.sending("a", xmpp.Message{
PacketAttrs: xmpp.PacketAttrs{From: "a@example.org"},
Extensions: []xmpp.MsgExtension{
xmpp.StateComposing{},
},
})
assert.NoError(err)
assert.Nil(msg)
// skip gone
msg, err = a.sending("a", xmpp.Message{
PacketAttrs: xmpp.PacketAttrs{From: "a@example.org"},
Extensions: []xmpp.MsgExtension{
xmpp.StateActive{},
xmpp.StateGone{},
xmpp.StateInactive{},
xmpp.StatePaused{},
},
})
assert.NoError(err)
assert.Nil(msg)
// skip gone
msg, err = a.sending("a", xmpp.Message{
PacketAttrs: xmpp.PacketAttrs{From: "a@example.org"},
Extensions: []xmpp.MsgExtension{
xmpp.StateActive{},
xmpp.StateComposing{},
xmpp.StateGone{},
xmpp.StateInactive{},
xmpp.StatePaused{},
},
Body: "hi",
})
assert.NoError(err)
assert.NotNil(msg)
o3msg := msg.(o3.TextMessage)
assert.Contains(o3msg.Text(), "hi")
}

2
vendor/gosrc.io/xmpp generated vendored

@ -1 +1 @@
Subproject commit 1df9d46212f2d8cac64a52cc0c3779d3b774a8d6 Subproject commit f78a014b96d53478e43b9adadd7bd3b5f11b0416