fix send message to threema
This commit is contained in:
parent
5f09a187ce
commit
0bd92ec5c3
|
@ -19,56 +19,60 @@ 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 err != nil {
|
||||
return nil, err
|
||||
if msg.Body == "" {
|
||||
if msgStateID != "" {
|
||||
id, err := strconv.ParseUint(msgStateID, 10, 64)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
msgType := o3.MSGDELIVERED
|
||||
if msgStateRead {
|
||||
msgType = o3.MSGREAD
|
||||
}
|
||||
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 readed {
|
||||
msgType = o3.MSGREAD
|
||||
if chatStateComposing {
|
||||
return nil, nil
|
||||
}
|
||||
drm, err := o3.NewDeliveryReceiptMessage(&a.Session, to, id, msgType)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
if chatState {
|
||||
return nil, nil
|
||||
}
|
||||
log.WithFields(map[string]interface{}{
|
||||
"tid": to,
|
||||
"msg_id": id,
|
||||
"type": msgType,
|
||||
}).Debug("update status of threema message")
|
||||
return drm, nil
|
||||
}
|
||||
|
||||
// send text message
|
||||
|
|
|
@ -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