fix send message to threema
This commit is contained in:
parent
5f09a187ce
commit
0bd92ec5c3
|
@ -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)
|
||||
|
|
|
@ -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")
|
||||
}
|
||||
|
|
|
@ -1 +1 @@
|
|||
Subproject commit 1df9d46212f2d8cac64a52cc0c3779d3b774a8d6
|
||||
Subproject commit f78a014b96d53478e43b9adadd7bd3b5f11b0416
|
Reference in New Issue