[TEST] refactory component threema sending
This commit is contained in:
		
							parent
							
								
									ee53a32ace
								
							
						
					
					
						commit
						211e0abed0
					
				| 
						 | 
					@ -12,7 +12,7 @@ func (c *Config) receiver() {
 | 
				
			||||||
			log.WithField("type", c.Type).Panicf("connection closed%s", err)
 | 
								log.WithField("type", c.Type).Panicf("connection closed%s", err)
 | 
				
			||||||
			return
 | 
								return
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		p, back := c.receive(packet)
 | 
							p, back := c.receiving(packet)
 | 
				
			||||||
		if p == nil {
 | 
							if p == nil {
 | 
				
			||||||
			continue
 | 
								continue
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
| 
						 | 
					@ -24,7 +24,7 @@ func (c *Config) receiver() {
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (c *Config) receive(packet xmpp.Packet) (xmpp.Packet, bool) {
 | 
					func (c *Config) receiving(packet xmpp.Packet) (xmpp.Packet, bool) {
 | 
				
			||||||
	logger := log.WithField("type", c.Type)
 | 
						logger := log.WithField("type", c.Type)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	switch p := packet.(type) {
 | 
						switch p := packet.(type) {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -13,20 +13,20 @@ func TestReceive(t *testing.T) {
 | 
				
			||||||
	c := Config{Host: "example.org", Type: "monkeyservice"}
 | 
						c := Config{Host: "example.org", Type: "monkeyservice"}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// ignoring packet
 | 
						// ignoring packet
 | 
				
			||||||
	p, back := c.receive(xmpp.Handshake{})
 | 
						p, _ := c.receiving(xmpp.Handshake{})
 | 
				
			||||||
	assert.Nil(p)
 | 
						assert.Nil(p)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// receive presence
 | 
						// receive presence
 | 
				
			||||||
	p, back = c.receive(xmpp.Presence{})
 | 
						p, _ = c.receiving(xmpp.Presence{})
 | 
				
			||||||
	assert.Nil(p)
 | 
						assert.Nil(p)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// message
 | 
						// message
 | 
				
			||||||
	p, back = c.receive(xmpp.Message{})
 | 
						p, back := c.receiving(xmpp.Message{})
 | 
				
			||||||
	assert.False(back)
 | 
						assert.False(back)
 | 
				
			||||||
	assert.NotNil(p)
 | 
						assert.NotNil(p)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// unsupported iq
 | 
						// unsupported iq
 | 
				
			||||||
	p, back = c.receive(xmpp.IQ{Payload: []xmpp.IQPayload{
 | 
						p, back = c.receiving(xmpp.IQ{Payload: []xmpp.IQPayload{
 | 
				
			||||||
		&xmpp.Err{},
 | 
							&xmpp.Err{},
 | 
				
			||||||
	}})
 | 
						}})
 | 
				
			||||||
	assert.True(back)
 | 
						assert.True(back)
 | 
				
			||||||
| 
						 | 
					@ -36,7 +36,7 @@ func TestReceive(t *testing.T) {
 | 
				
			||||||
	assert.Equal("feature-not-implemented", iq.Error.Reason)
 | 
						assert.Equal("feature-not-implemented", iq.Error.Reason)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// iq disco info
 | 
						// iq disco info
 | 
				
			||||||
	p, back = c.receive(xmpp.IQ{
 | 
						p, back = c.receiving(xmpp.IQ{
 | 
				
			||||||
		PacketAttrs: xmpp.PacketAttrs{Type: "get"},
 | 
							PacketAttrs: xmpp.PacketAttrs{Type: "get"},
 | 
				
			||||||
		Payload: []xmpp.IQPayload{
 | 
							Payload: []xmpp.IQPayload{
 | 
				
			||||||
			&xmpp.DiscoInfo{},
 | 
								&xmpp.DiscoInfo{},
 | 
				
			||||||
| 
						 | 
					@ -50,7 +50,7 @@ func TestReceive(t *testing.T) {
 | 
				
			||||||
	assert.Equal("monkeyservice", dinfo.Identity.Name)
 | 
						assert.Equal("monkeyservice", dinfo.Identity.Name)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// iq disco items
 | 
						// iq disco items
 | 
				
			||||||
	p, back = c.receive(xmpp.IQ{
 | 
						p, back = c.receiving(xmpp.IQ{
 | 
				
			||||||
		PacketAttrs: xmpp.PacketAttrs{Type: "get"},
 | 
							PacketAttrs: xmpp.PacketAttrs{Type: "get"},
 | 
				
			||||||
		Payload: []xmpp.IQPayload{
 | 
							Payload: []xmpp.IQPayload{
 | 
				
			||||||
			&xmpp.DiscoItems{},
 | 
								&xmpp.DiscoItems{},
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -7,13 +7,13 @@ import (
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (c *Config) sender(packets chan xmpp.Packet) {
 | 
					func (c *Config) sender(packets chan xmpp.Packet) {
 | 
				
			||||||
	for packet := range packets {
 | 
						for packet := range packets {
 | 
				
			||||||
		if p := c.send(packet); p != nil {
 | 
							if p := c.sending(packet); p != nil {
 | 
				
			||||||
			c.xmpp.Send(p)
 | 
								c.xmpp.Send(p)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (c *Config) send(packet xmpp.Packet) xmpp.Packet {
 | 
					func (c *Config) sending(packet xmpp.Packet) xmpp.Packet {
 | 
				
			||||||
	logger := log.WithField("type", c.Type)
 | 
						logger := log.WithField("type", c.Type)
 | 
				
			||||||
	switch p := packet.(type) {
 | 
						switch p := packet.(type) {
 | 
				
			||||||
	case xmpp.Message:
 | 
						case xmpp.Message:
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -13,17 +13,17 @@ func TestSend(t *testing.T) {
 | 
				
			||||||
	c := Config{Host: "example.org"}
 | 
						c := Config{Host: "example.org"}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// ignoring packet
 | 
						// ignoring packet
 | 
				
			||||||
	p := c.send(xmpp.IQ{})
 | 
						p := c.sending(xmpp.IQ{})
 | 
				
			||||||
	assert.Nil(p)
 | 
						assert.Nil(p)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// send by component host
 | 
						// send by component host
 | 
				
			||||||
	p = c.send(xmpp.Message{})
 | 
						p = c.sending(xmpp.Message{})
 | 
				
			||||||
	assert.NotNil(p)
 | 
						assert.NotNil(p)
 | 
				
			||||||
	msg := p.(xmpp.Message)
 | 
						msg := p.(xmpp.Message)
 | 
				
			||||||
	assert.Equal("example.org", msg.PacketAttrs.From)
 | 
						assert.Equal("example.org", msg.PacketAttrs.From)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// send by a user of component
 | 
						// send by a user of component
 | 
				
			||||||
	p = c.send(xmpp.Message{PacketAttrs: xmpp.PacketAttrs{From: "threemaid"}})
 | 
						p = c.sending(xmpp.Message{PacketAttrs: xmpp.PacketAttrs{From: "threemaid"}})
 | 
				
			||||||
	assert.NotNil(p)
 | 
						assert.NotNil(p)
 | 
				
			||||||
	msg = p.(xmpp.Message)
 | 
						msg = p.(xmpp.Message)
 | 
				
			||||||
	assert.Equal("threemaid@example.org", msg.PacketAttrs.From)
 | 
						assert.Equal("threemaid@example.org", msg.PacketAttrs.From)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -14,7 +14,7 @@ type Account struct {
 | 
				
			||||||
	models.AccountThreema
 | 
						models.AccountThreema
 | 
				
			||||||
	Session      o3.SessionContext
 | 
						Session      o3.SessionContext
 | 
				
			||||||
	send         chan<- o3.Message
 | 
						send         chan<- o3.Message
 | 
				
			||||||
	recieve      <-chan o3.ReceivedMsg
 | 
						receive      <-chan o3.ReceivedMsg
 | 
				
			||||||
	deliveredMSG map[uint64]string
 | 
						deliveredMSG map[uint64]string
 | 
				
			||||||
	readedMSG    map[uint64]string
 | 
						readedMSG    map[uint64]string
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -45,7 +45,7 @@ func (t *Threema) getAccount(jid *models.JID) (*Account, error) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	a := &Account{AccountThreema: account}
 | 
						a := &Account{AccountThreema: account}
 | 
				
			||||||
	a.Session = o3.NewSessionContext(tid)
 | 
						a.Session = o3.NewSessionContext(tid)
 | 
				
			||||||
	a.send, a.recieve, err = a.Session.Run()
 | 
						a.send, a.receive, err = a.Session.Run()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return nil, err
 | 
							return nil, err
 | 
				
			||||||
| 
						 | 
					@ -55,7 +55,7 @@ func (t *Threema) getAccount(jid *models.JID) (*Account, error) {
 | 
				
			||||||
	a.deliveredMSG = make(map[uint64]string)
 | 
						a.deliveredMSG = make(map[uint64]string)
 | 
				
			||||||
	a.readedMSG = make(map[uint64]string)
 | 
						a.readedMSG = make(map[uint64]string)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	go a.reciever(t.out)
 | 
						go a.receiver(t.out)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	t.accountJID[jid.String()] = a
 | 
						t.accountJID[jid.String()] = a
 | 
				
			||||||
	return a, nil
 | 
						return a, nil
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -55,7 +55,7 @@ func (t *Threema) send(packet xmpp.Packet) xmpp.Packet {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if to.IsDomain() {
 | 
							if to.IsDomain() {
 | 
				
			||||||
			if from == nil {
 | 
								if from == nil {
 | 
				
			||||||
				log.Warn("recieve message without sender")
 | 
									log.Warn("receive message without sender")
 | 
				
			||||||
				return nil
 | 
									return nil
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			msg := xmpp.NewMessage("chat", "", from.String(), "", "en")
 | 
								msg := xmpp.NewMessage("chat", "", from.String(), "", "en")
 | 
				
			||||||
| 
						 | 
					@ -66,7 +66,7 @@ func (t *Threema) send(packet xmpp.Packet) xmpp.Packet {
 | 
				
			||||||
		account, err := t.getAccount(from)
 | 
							account, err := t.getAccount(from)
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			msg := xmpp.NewMessage("chat", "", from.String(), "", "en")
 | 
								msg := xmpp.NewMessage("chat", "", from.String(), "", "en")
 | 
				
			||||||
			msg.Body = "It was not possible to send, becouse we have no account for you.\nPlease generate one, by sending `generate` to this gateway"
 | 
								msg.Body = "It was not possible to send, because we have no account for you.\nPlease generate one, by sending `generate` to this gateway"
 | 
				
			||||||
			return msg
 | 
								return msg
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -77,7 +77,7 @@ func (t *Threema) send(packet xmpp.Packet) xmpp.Packet {
 | 
				
			||||||
			return msg
 | 
								return msg
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	default:
 | 
						default:
 | 
				
			||||||
		log.Warnf("unkown package: %v", p)
 | 
							log.Warnf("unknown package: %v", p)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return nil
 | 
						return nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -8,14 +8,14 @@ import (
 | 
				
			||||||
	"gosrc.io/xmpp"
 | 
						"gosrc.io/xmpp"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (a *Account) reciever(out chan<- xmpp.Packet) {
 | 
					func (a *Account) receiver(out chan<- xmpp.Packet) {
 | 
				
			||||||
	for receivedMessage := range a.recieve {
 | 
						for receivedMessage := range a.receive {
 | 
				
			||||||
		if p := a.handle(receivedMessage); p != nil {
 | 
							if p := a.receiving(receivedMessage); p != nil {
 | 
				
			||||||
			out <- p
 | 
								out <- p
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
func (a *Account) handle(receivedMessage o3.ReceivedMsg) xmpp.Packet {
 | 
					func (a *Account) receiving(receivedMessage o3.ReceivedMsg) xmpp.Packet {
 | 
				
			||||||
	if receivedMessage.Err != nil {
 | 
						if receivedMessage.Err != nil {
 | 
				
			||||||
		log.Warnf("Error Receiving Message: %s\n", receivedMessage.Err)
 | 
							log.Warnf("Error Receiving Message: %s\n", receivedMessage.Err)
 | 
				
			||||||
		return nil
 | 
							return nil
 | 
				
			||||||
| 
						 | 
					@ -28,28 +28,28 @@ func createDummyAccount() Account {
 | 
				
			||||||
	return a
 | 
						return a
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func TestRecieve(t *testing.T) {
 | 
					func TestReceive(t *testing.T) {
 | 
				
			||||||
	assert := assert.New(t)
 | 
						assert := assert.New(t)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	a := createDummyAccount()
 | 
						a := createDummyAccount()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// handle/skip error
 | 
						// receiving/skip error
 | 
				
			||||||
	p := a.handle(o3.ReceivedMsg{
 | 
						p := a.receiving(o3.ReceivedMsg{
 | 
				
			||||||
		Err: errors.New("dummy"),
 | 
							Err: errors.New("dummy"),
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
	assert.Nil(p)
 | 
						assert.Nil(p)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// nothing to handle
 | 
						// nothing to receiving
 | 
				
			||||||
	p = a.handle(o3.ReceivedMsg{})
 | 
						p = a.receiving(o3.ReceivedMsg{})
 | 
				
			||||||
	assert.Nil(p)
 | 
						assert.Nil(p)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func TestRecieveText(t *testing.T) {
 | 
					func TestReceiveText(t *testing.T) {
 | 
				
			||||||
	assert := assert.New(t)
 | 
						assert := assert.New(t)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	a := createDummyAccount()
 | 
						a := createDummyAccount()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// handle text
 | 
						// receiving text
 | 
				
			||||||
	session := o3.SessionContext{
 | 
						session := o3.SessionContext{
 | 
				
			||||||
		ID: o3.ThreemaID{
 | 
							ID: o3.ThreemaID{
 | 
				
			||||||
			ID:   o3.NewIDString("12345678"),
 | 
								ID:   o3.NewIDString("12345678"),
 | 
				
			||||||
| 
						 | 
					@ -58,14 +58,14 @@ func TestRecieveText(t *testing.T) {
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	txtMsg, err := o3.NewTextMessage(&session, threemaID, "Oojoh0Ah")
 | 
						txtMsg, err := o3.NewTextMessage(&session, threemaID, "Oojoh0Ah")
 | 
				
			||||||
	assert.NoError(err)
 | 
						assert.NoError(err)
 | 
				
			||||||
	p := a.handle(o3.ReceivedMsg{
 | 
						p := a.receiving(o3.ReceivedMsg{
 | 
				
			||||||
		Msg: txtMsg,
 | 
							Msg: txtMsg,
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
	xMSG, ok := p.(xmpp.Message)
 | 
						xMSG, ok := p.(xmpp.Message)
 | 
				
			||||||
	assert.True(ok)
 | 
						assert.True(ok)
 | 
				
			||||||
	assert.Equal("Oojoh0Ah", xMSG.Body)
 | 
						assert.Equal("Oojoh0Ah", xMSG.Body)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// handle/skip text to own id
 | 
						// receiving/skip text to own id
 | 
				
			||||||
	session = o3.SessionContext{
 | 
						session = o3.SessionContext{
 | 
				
			||||||
		ID: o3.ThreemaID{
 | 
							ID: o3.ThreemaID{
 | 
				
			||||||
			ID:   threemaIDByte,
 | 
								ID:   threemaIDByte,
 | 
				
			||||||
| 
						 | 
					@ -74,18 +74,18 @@ func TestRecieveText(t *testing.T) {
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	txtMsg, err = o3.NewTextMessage(&session, threemaID, "Aesh8shu")
 | 
						txtMsg, err = o3.NewTextMessage(&session, threemaID, "Aesh8shu")
 | 
				
			||||||
	assert.NoError(err)
 | 
						assert.NoError(err)
 | 
				
			||||||
	p = a.handle(o3.ReceivedMsg{
 | 
						p = a.receiving(o3.ReceivedMsg{
 | 
				
			||||||
		Msg: txtMsg,
 | 
							Msg: txtMsg,
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
	assert.Nil(p)
 | 
						assert.Nil(p)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func TestRecieveDeliveryReceipt(t *testing.T) {
 | 
					func TestReceiveDeliveryReceipt(t *testing.T) {
 | 
				
			||||||
	assert := assert.New(t)
 | 
						assert := assert.New(t)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	a := createDummyAccount()
 | 
						a := createDummyAccount()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// handle delivered
 | 
						// receiving delivered
 | 
				
			||||||
	session := o3.SessionContext{
 | 
						session := o3.SessionContext{
 | 
				
			||||||
		ID: o3.ThreemaID{
 | 
							ID: o3.ThreemaID{
 | 
				
			||||||
			ID:   o3.NewIDString("12345678"),
 | 
								ID:   o3.NewIDString("12345678"),
 | 
				
			||||||
| 
						 | 
					@ -98,7 +98,7 @@ func TestRecieveDeliveryReceipt(t *testing.T) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	drm, err := o3.NewDeliveryReceiptMessage(&session, threemaID, msgID, o3.MSGDELIVERED)
 | 
						drm, err := o3.NewDeliveryReceiptMessage(&session, threemaID, msgID, o3.MSGDELIVERED)
 | 
				
			||||||
	assert.NoError(err)
 | 
						assert.NoError(err)
 | 
				
			||||||
	p := a.handle(o3.ReceivedMsg{
 | 
						p := a.receiving(o3.ReceivedMsg{
 | 
				
			||||||
		Msg: drm,
 | 
							Msg: drm,
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
	xMSG, ok := p.(xmpp.Message)
 | 
						xMSG, ok := p.(xmpp.Message)
 | 
				
			||||||
| 
						 | 
					@ -106,16 +106,16 @@ func TestRecieveDeliveryReceipt(t *testing.T) {
 | 
				
			||||||
	rr := xMSG.Extensions[0].(xmpp.ReceiptReceived)
 | 
						rr := xMSG.Extensions[0].(xmpp.ReceiptReceived)
 | 
				
			||||||
	assert.Equal("im4aeseeh1IbaQui", rr.Id)
 | 
						assert.Equal("im4aeseeh1IbaQui", rr.Id)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// handle delivered -> not in cache
 | 
						// receiving delivered -> not in cache
 | 
				
			||||||
	p = a.handle(o3.ReceivedMsg{
 | 
						p = a.receiving(o3.ReceivedMsg{
 | 
				
			||||||
		Msg: drm,
 | 
							Msg: drm,
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
	assert.Nil(p)
 | 
						assert.Nil(p)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// handle readed
 | 
						// receiving readed
 | 
				
			||||||
	drm, err = o3.NewDeliveryReceiptMessage(&session, threemaID, msgID, o3.MSGREAD)
 | 
						drm, err = o3.NewDeliveryReceiptMessage(&session, threemaID, msgID, o3.MSGREAD)
 | 
				
			||||||
	assert.NoError(err)
 | 
						assert.NoError(err)
 | 
				
			||||||
	p = a.handle(o3.ReceivedMsg{
 | 
						p = a.receiving(o3.ReceivedMsg{
 | 
				
			||||||
		Msg: drm,
 | 
							Msg: drm,
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
	xMSG, ok = p.(xmpp.Message)
 | 
						xMSG, ok = p.(xmpp.Message)
 | 
				
			||||||
| 
						 | 
					@ -123,8 +123,8 @@ func TestRecieveDeliveryReceipt(t *testing.T) {
 | 
				
			||||||
	cmdd := xMSG.Extensions[0].(xmpp.ChatMarkerDisplayed)
 | 
						cmdd := xMSG.Extensions[0].(xmpp.ChatMarkerDisplayed)
 | 
				
			||||||
	assert.Equal("im4aeseeh1IbaQui", cmdd.Id)
 | 
						assert.Equal("im4aeseeh1IbaQui", cmdd.Id)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// handle delivered -> not in cache
 | 
						// receiving delivered -> not in cache
 | 
				
			||||||
	p = a.handle(o3.ReceivedMsg{
 | 
						p = a.receiving(o3.ReceivedMsg{
 | 
				
			||||||
		Msg: drm,
 | 
							Msg: drm,
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
	assert.Nil(p)
 | 
						assert.Nil(p)
 | 
				
			||||||
| 
						 | 
					@ -9,6 +9,17 @@ import (
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (a *Account) Send(to string, msg xmpp.Message) error {
 | 
					func (a *Account) Send(to string, msg xmpp.Message) error {
 | 
				
			||||||
 | 
						m, err := a.sending(to, msg)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if m != nil {
 | 
				
			||||||
 | 
							a.send <- m
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					func (a *Account) sending(to string, msg xmpp.Message) (o3.Message, error) {
 | 
				
			||||||
 | 
						// handle delivered / readed
 | 
				
			||||||
	msgID := ""
 | 
						msgID := ""
 | 
				
			||||||
	readed := false
 | 
						readed := false
 | 
				
			||||||
	for _, el := range msg.Extensions {
 | 
						for _, el := range msg.Extensions {
 | 
				
			||||||
| 
						 | 
					@ -25,7 +36,7 @@ func (a *Account) Send(to string, msg xmpp.Message) error {
 | 
				
			||||||
	if msgID != "" {
 | 
						if msgID != "" {
 | 
				
			||||||
		id, err := strconv.ParseUint(msgID, 10, 64)
 | 
							id, err := strconv.ParseUint(msgID, 10, 64)
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			return err
 | 
								return nil, err
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		msgType := o3.MSGDELIVERED
 | 
							msgType := o3.MSGDELIVERED
 | 
				
			||||||
		if readed {
 | 
							if readed {
 | 
				
			||||||
| 
						 | 
					@ -33,23 +44,22 @@ func (a *Account) Send(to string, msg xmpp.Message) error {
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		drm, err := o3.NewDeliveryReceiptMessage(&a.Session, to, id, msgType)
 | 
							drm, err := o3.NewDeliveryReceiptMessage(&a.Session, to, id, msgType)
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			return err
 | 
								return nil, err
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		a.send <- drm
 | 
					 | 
				
			||||||
		log.WithFields(map[string]interface{}{
 | 
							log.WithFields(map[string]interface{}{
 | 
				
			||||||
			"tid":    to,
 | 
								"tid":    to,
 | 
				
			||||||
			"msg_id": id,
 | 
								"msg_id": id,
 | 
				
			||||||
			"type":   msgType,
 | 
								"type":   msgType,
 | 
				
			||||||
		}).Debug("update status of threema message")
 | 
							}).Debug("update status of threema message")
 | 
				
			||||||
		return nil
 | 
							return drm, nil
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// send text message
 | 
				
			||||||
	msg3, err := o3.NewTextMessage(&a.Session, to, msg.Body)
 | 
						msg3, err := o3.NewTextMessage(&a.Session, to, msg.Body)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return err
 | 
							return nil, err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	a.deliveredMSG[msg3.ID()] = msg.Id
 | 
						a.deliveredMSG[msg3.ID()] = msg.Id
 | 
				
			||||||
	a.readedMSG[msg3.ID()] = msg.Id
 | 
						a.readedMSG[msg3.ID()] = msg.Id
 | 
				
			||||||
	a.send <- msg3
 | 
						return msg3, nil
 | 
				
			||||||
	return nil
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,83 @@
 | 
				
			||||||
 | 
					package threema
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"testing"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"github.com/o3ma/o3"
 | 
				
			||||||
 | 
						"github.com/stretchr/testify/assert"
 | 
				
			||||||
 | 
						"gosrc.io/xmpp"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestAccountSend(t *testing.T) {
 | 
				
			||||||
 | 
						assert := assert.New(t)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						send := make(chan o3.Message)
 | 
				
			||||||
 | 
						a := Account{
 | 
				
			||||||
 | 
							Session:      o3.NewSessionContext(o3.ThreemaID{ID: o3.NewIDString("43218765")}),
 | 
				
			||||||
 | 
							send:         send,
 | 
				
			||||||
 | 
							deliveredMSG: make(map[uint64]string),
 | 
				
			||||||
 | 
							readedMSG:    make(map[uint64]string),
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						go func() {
 | 
				
			||||||
 | 
							a.Send("a", xmpp.Message{
 | 
				
			||||||
 | 
								PacketAttrs: xmpp.PacketAttrs{From: "a@example.org"},
 | 
				
			||||||
 | 
								Body:        "ohz8kai0ohNgohth",
 | 
				
			||||||
 | 
							})
 | 
				
			||||||
 | 
						}()
 | 
				
			||||||
 | 
						p := <-send
 | 
				
			||||||
 | 
						assert.NotNil(p)
 | 
				
			||||||
 | 
						msg := p.(o3.TextMessage)
 | 
				
			||||||
 | 
						assert.Contains(msg.Text(), "ohz8kai0ohNgohth")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// test error
 | 
				
			||||||
 | 
						err := a.Send("a", xmpp.Message{
 | 
				
			||||||
 | 
							PacketAttrs: xmpp.PacketAttrs{From: "a@example.org"},
 | 
				
			||||||
 | 
							Extensions: []xmpp.MsgExtension{
 | 
				
			||||||
 | 
								&xmpp.ReceiptReceived{Id: "blub"},
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
						assert.Error(err)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestAccountSendingDeliviery(t *testing.T) {
 | 
				
			||||||
 | 
						assert := assert.New(t)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						a := Account{
 | 
				
			||||||
 | 
							Session: o3.NewSessionContext(o3.ThreemaID{ID: o3.NewIDString("43218765")}),
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// test error - threema send only int ids
 | 
				
			||||||
 | 
						msg, err := a.sending("a", xmpp.Message{
 | 
				
			||||||
 | 
							PacketAttrs: xmpp.PacketAttrs{From: "a@example.org"},
 | 
				
			||||||
 | 
							Extensions: []xmpp.MsgExtension{
 | 
				
			||||||
 | 
								&xmpp.ReceiptReceived{Id: "blub"},
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
						assert.Error(err)
 | 
				
			||||||
 | 
						assert.Nil(msg)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// test delivered
 | 
				
			||||||
 | 
						msg, err = a.sending("a", xmpp.Message{
 | 
				
			||||||
 | 
							PacketAttrs: xmpp.PacketAttrs{From: "a@example.org"},
 | 
				
			||||||
 | 
							Extensions: []xmpp.MsgExtension{
 | 
				
			||||||
 | 
								&xmpp.ChatMarkerReceived{Id: "3"},
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
						assert.NoError(err)
 | 
				
			||||||
 | 
						drm, ok := msg.(o3.DeliveryReceiptMessage)
 | 
				
			||||||
 | 
						assert.True(ok)
 | 
				
			||||||
 | 
						assert.Equal(o3.MSGDELIVERED, drm.Status())
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// test read
 | 
				
			||||||
 | 
						msg, err = a.sending("a", xmpp.Message{
 | 
				
			||||||
 | 
							PacketAttrs: xmpp.PacketAttrs{From: "a@example.org"},
 | 
				
			||||||
 | 
							Extensions: []xmpp.MsgExtension{
 | 
				
			||||||
 | 
								&xmpp.ChatMarkerDisplayed{Id: "5"},
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
						assert.NoError(err)
 | 
				
			||||||
 | 
						drm, ok = msg.(o3.DeliveryReceiptMessage)
 | 
				
			||||||
 | 
						assert.True(ok)
 | 
				
			||||||
 | 
						assert.Equal(o3.MSGREAD, drm.Status())
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Reference in New Issue