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,44 +19,41 @@ func (a *Account) Send(to string, msg xmpp.Message) error {
return nil
}
func (a *Account) sending(to string, msg xmpp.Message) (o3.Message, error) {
// handle delivered / readed
msgID := ""
readed := false
composing := false
state := false
chatState := false
chatStateComposing := false
msgStateID := ""
msgStateRead := false
for _, el := range msg.Extensions {
switch ex := el.(type) {
case xmpp.StateComposing:
composing = true
state = true
case xmpp.StateInactive:
state = true
case xmpp.StateActive:
state = true
chatState = true
case xmpp.StateComposing:
chatState = true
chatStateComposing = true
case xmpp.StateGone:
state = true
chatState = true
case xmpp.StateInactive:
chatState = true
case xmpp.StatePaused:
chatState = true
case xmpp.ReceiptReceived:
msgID = ex.ID
msgStateID = ex.ID
case xmpp.MarkReceived:
msgID = ex.ID
msgStateID = ex.ID
case xmpp.MarkDisplayed:
readed = true
msgID = ex.ID
msgStateRead = true
msgStateID = ex.ID
}
}
if composing {
return nil, nil
}
if state && msg.Body == "" {
return nil, nil
}
if msgID != "" {
id, err := strconv.ParseUint(msgID, 10, 64)
if msg.Body == "" {
if msgStateID != "" {
id, err := strconv.ParseUint(msgStateID, 10, 64)
if err != nil {
return nil, err
}
msgType := o3.MSGDELIVERED
if readed {
if msgStateRead {
msgType = o3.MSGREAD
}
drm, err := o3.NewDeliveryReceiptMessage(&a.Session, to, id, msgType)
@ -70,6 +67,13 @@ func (a *Account) sending(to string, msg xmpp.Message) (o3.Message, error) {
}).Debug("update status of threema message")
return drm, nil
}
if chatStateComposing {
return nil, nil
}
if chatState {
return nil, nil
}
}
// send text message
msg3, err := o3.NewTextMessage(&a.Session, to, msg.Body)

View File

@ -81,3 +81,52 @@ func TestAccountSendingDeliviery(t *testing.T) {
assert.True(ok)
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