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
 | 
						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 == "" {
 | 
					 | 
				
			||||||
		return nil, nil
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	if msgID != "" {
 | 
					 | 
				
			||||||
		id, err := strconv.ParseUint(msgID, 10, 64)
 | 
					 | 
				
			||||||
			if err != nil {
 | 
								if err != nil {
 | 
				
			||||||
				return nil, err
 | 
									return nil, err
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			msgType := o3.MSGDELIVERED
 | 
								msgType := o3.MSGDELIVERED
 | 
				
			||||||
		if readed {
 | 
								if msgStateRead {
 | 
				
			||||||
				msgType = o3.MSGREAD
 | 
									msgType = o3.MSGREAD
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			drm, err := o3.NewDeliveryReceiptMessage(&a.Session, to, id, msgType)
 | 
								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")
 | 
								}).Debug("update status of threema message")
 | 
				
			||||||
			return drm, nil
 | 
								return drm, nil
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
							if chatStateComposing {
 | 
				
			||||||
 | 
								return nil, nil
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							if chatState {
 | 
				
			||||||
 | 
								return nil, nil
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// send text message
 | 
						// send text message
 | 
				
			||||||
	msg3, err := o3.NewTextMessage(&a.Session, to, msg.Body)
 | 
						msg3, err := o3.NewTextMessage(&a.Session, to, msg.Body)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -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")
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1 +1 @@
 | 
				
			||||||
Subproject commit 1df9d46212f2d8cac64a52cc0c3779d3b774a8d6
 | 
					Subproject commit f78a014b96d53478e43b9adadd7bd3b5f11b0416
 | 
				
			||||||
		Reference in New Issue