threema refactory

This commit is contained in:
Martin/Geno 2019-08-30 17:28:21 +02:00 committed by genofire
parent 39cd55266a
commit b535a6f685
5 changed files with 146 additions and 113 deletions

View File

@ -19,13 +19,14 @@ func (a *Account) receiver(out chan<- stanza.Packet) {
out <- xMSG out <- xMSG
continue continue
} }
sender := receivedMessage.Msg.Sender().String() header := receivedMessage.Msg.Header()
sender := header.Sender.String()
if string(a.TID) == sender { if string(a.TID) == sender {
continue continue
} }
if p, err := a.receiving(receivedMessage.Msg); err != nil { if p, err := a.receiving(receivedMessage.Msg); err != nil {
xMSG := stanza.NewMessage(stanza.Attrs{Type: stanza.MessageTypeChat, From: sender, To: a.XMPP.String()}) xMSG := stanza.NewMessage(stanza.Attrs{Type: stanza.MessageTypeChat, From: sender, To: a.XMPP.String()})
xMSG.Body = fmt.Sprintf("error on decoding message: %s\n%v", err, receivedMessage.Msg.Serialize()) xMSG.Body = fmt.Sprintf("error on decoding message: %s\n%v", err, receivedMessage.Msg)
out <- xMSG out <- xMSG
} else if p != nil { } else if p != nil {
out <- p out <- p
@ -40,19 +41,20 @@ func requestExtensions(xMSG *stanza.Message) {
} }
func (a *Account) receiving(receivedMessage o3.Message) (stanza.Packet, error) { func (a *Account) receiving(receivedMessage o3.Message) (stanza.Packet, error) {
header := receivedMessage.Header()
sender := header.Sender.String()
logger := log.WithFields(map[string]interface{}{ logger := log.WithFields(map[string]interface{}{
"from": receivedMessage.Sender().String(), "from": header.Sender.String(),
"to": a.XMPP.String(), "to": a.XMPP.String(),
}) })
switch msg := receivedMessage.(type) { switch msg := receivedMessage.(type) {
case o3.TextMessage: case o3.TextMessage:
sender := msg.Sender().String() xMSG := stanza.NewMessage(stanza.Attrs{Type: stanza.MessageTypeChat, From: sender, To: a.XMPP.String(), Id: strconv.FormatUint(header.ID, 10)})
xMSG := stanza.NewMessage(stanza.Attrs{Type: stanza.MessageTypeChat, From: sender, To: a.XMPP.String(), Id: strconv.FormatUint(msg.ID(), 10)}) xMSG.Body = msg.Body
xMSG.Body = msg.Text()
requestExtensions(&xMSG) requestExtensions(&xMSG)
logger.WithField("text", xMSG.Body).Debug("send text") logger.WithField("text", xMSG.Body).Debug("send text")
return xMSG, nil return xMSG, nil
/*
case o3.AudioMessage: case o3.AudioMessage:
if a.threema.httpUploadPath == "" { if a.threema.httpUploadPath == "" {
return nil, errors.New("no place to store files at transport configurated") return nil, errors.New("no place to store files at transport configurated")
@ -62,7 +64,7 @@ func (a *Account) receiving(receivedMessage o3.Message) (stanza.Packet, error) {
logger.Warnf("unable to read data from message: %s", err) logger.Warnf("unable to read data from message: %s", err)
return nil, err return nil, err
} }
xMSG, err := a.FileToXMPP(msg.Sender().String(), msg.ID(), "mp3", data) xMSG, err := a.FileToXMPP(sender.String(), header.ID, "mp3", data)
if err != nil { if err != nil {
logger.Warnf("unable to create data from message: %s", err) logger.Warnf("unable to create data from message: %s", err)
return nil, err return nil, err
@ -81,7 +83,7 @@ func (a *Account) receiving(receivedMessage o3.Message) (stanza.Packet, error) {
logger.Warnf("unable to read data from message: %s", err) logger.Warnf("unable to read data from message: %s", err)
return nil, err return nil, err
} }
xMSG, err := a.FileToXMPP(msg.Sender().String(), msg.ID(), "jpg", data) xMSG, err := a.FileToXMPP(sender.String(), header.ID, "jpg", data)
if err != nil { if err != nil {
logger.Warnf("unable to create data from message: %s", err) logger.Warnf("unable to create data from message: %s", err)
return nil, err return nil, err
@ -90,27 +92,26 @@ func (a *Account) receiving(receivedMessage o3.Message) (stanza.Packet, error) {
requestExtensions(&xMSG) requestExtensions(&xMSG)
logger.WithField("url", xMSG.Body).Debug("send image") logger.WithField("url", xMSG.Body).Debug("send image")
return xMSG, nil return xMSG, nil
*/
case o3.DeliveryReceiptMessage: case o3.DeliveryReceiptMessage:
msgID := msg.MsgID() xMSG := stanza.NewMessage(stanza.Attrs{Type: stanza.MessageTypeChat, From: sender, To: a.XMPP.String()})
xMSG := stanza.NewMessage(stanza.Attrs{Type: stanza.MessageTypeChat, From: msg.Sender().String(), To: a.XMPP.String()})
state := "" state := ""
if msg.Status() == o3.MSGDELIVERED { if msg.Status == o3.MSGDELIVERED {
state = "delivered" state = "delivered"
if id, ok := a.deliveredMSG[msgID]; ok { if id, ok := a.deliveredMSG[msg.MessageID]; ok {
xMSG.Extensions = append(xMSG.Extensions, stanza.ReceiptReceived{ID: id}) xMSG.Extensions = append(xMSG.Extensions, stanza.ReceiptReceived{ID: id})
xMSG.Extensions = append(xMSG.Extensions, stanza.MarkReceived{ID: id}) xMSG.Extensions = append(xMSG.Extensions, stanza.MarkReceived{ID: id})
delete(a.deliveredMSG, msgID) delete(a.deliveredMSG, msg.MessageID)
} else { } else {
logger.Warnf("found not id in cache to announce received on xmpp side") logger.Warnf("found not id in cache to announce received on xmpp side")
} }
} }
if msg.Status() == o3.MSGREAD { if msg.Status == o3.MSGREAD {
state = "displayed" state = "displayed"
if id, ok := a.readedMSG[msgID]; ok { if id, ok := a.readedMSG[msg.MessageID]; ok {
xMSG.Extensions = append(xMSG.Extensions, stanza.MarkDisplayed{ID: id}) xMSG.Extensions = append(xMSG.Extensions, stanza.MarkDisplayed{ID: id})
delete(a.readedMSG, msgID) delete(a.readedMSG, msg.MessageID)
} else { } else {
logger.Warnf("found not id in cache to announce readed on xmpp side") logger.Warnf("found not id in cache to announce readed on xmpp side")
} }
@ -122,7 +123,7 @@ func (a *Account) receiving(receivedMessage o3.Message) (stanza.Packet, error) {
} }
return nil, nil return nil, nil
case o3.TypingNotificationMessage: case o3.TypingNotificationMessage:
xMSG := stanza.NewMessage(stanza.Attrs{Type: stanza.MessageTypeChat, From: msg.Sender().String(), To: a.XMPP.String(), Id: strconv.FormatUint(msg.ID(), 10)}) xMSG := stanza.NewMessage(stanza.Attrs{Type: stanza.MessageTypeChat, From: sender, To: a.XMPP.String(), Id: strconv.FormatUint(header.ID, 10)})
if msg.OnOff != 0 { if msg.OnOff != 0 {
logger.Debug("composing") logger.Debug("composing")
xMSG.Extensions = append(xMSG.Extensions, stanza.StateComposing{}) xMSG.Extensions = append(xMSG.Extensions, stanza.StateComposing{})

View File

@ -8,12 +8,12 @@ import (
"gosrc.io/xmpp/stanza" "gosrc.io/xmpp/stanza"
) )
const threemaID = "87654321" const threemaFromID = "87654321"
var threemaIDByte o3.IDString var threemaFromIDByte o3.IDString
func init() { func init() {
threemaIDByte = o3.NewIDString(threemaID) threemaFromIDByte = o3.NewIDString(threemaFromID)
} }
func createDummyAccount() Account { func createDummyAccount() Account {
@ -21,8 +21,8 @@ func createDummyAccount() Account {
deliveredMSG: make(map[uint64]string), deliveredMSG: make(map[uint64]string),
readedMSG: make(map[uint64]string), readedMSG: make(map[uint64]string),
} }
a.TID = make([]byte, len(threemaIDByte)) a.TID = make([]byte, len(threemaFromIDByte))
copy(a.TID, threemaIDByte[:]) copy(a.TID, threemaFromIDByte[:])
return a return a
} }
@ -45,35 +45,33 @@ func TestReceiveText(t *testing.T) {
a := createDummyAccount() a := createDummyAccount()
// receiving text // receiving text
session := o3.SessionContext{ txtMsg := o3.TextMessage{
ID: o3.ThreemaID{ MessageHeader: &o3.MessageHeader{
ID: o3.NewIDString("12345678"), Sender: threemaFromIDByte,
Nick: o3.NewPubNick("user"), Recipient: o3.NewIDString("12345678"),
}, },
Body: "Oojoh0Ah",
} }
txtMsg, err := o3.NewTextMessage(&session, threemaID, "Oojoh0Ah")
assert.NoError(err)
p, err := a.receiving(txtMsg) p, err := a.receiving(txtMsg)
assert.NoError(err) assert.NoError(err)
assert.NotNil(p)
xMSG, ok := p.(stanza.Message) xMSG, ok := p.(stanza.Message)
assert.True(ok) assert.True(ok)
assert.Equal("Oojoh0Ah", xMSG.Body) assert.Equal("Oojoh0Ah", xMSG.Body)
} }
/*
func TestReceiveAudio(t *testing.T) { func TestReceiveAudio(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
a := createDummyAccount() a := createDummyAccount()
a.threema = &Threema{} a.threema = &Threema{}
/* receiving image dataMsg := o3.ImageMessage{
session := o3.SessionContext{ MessageHeader: &o3.MessageHeader{
ID: o3.ThreemaID{ Sender: threemaFromIDByte,
ID: o3.NewIDString("12345678"),
Nick: o3.NewPubNick("user"),
}, },
}*/ }
dataMsg := o3.AudioMessage{}
_, err := a.receiving(dataMsg) _, err := a.receiving(dataMsg)
assert.Error(err) assert.Error(err)
@ -88,22 +86,26 @@ func TestReceiveImage(t *testing.T) {
a := createDummyAccount() a := createDummyAccount()
a.threema = &Threema{} a.threema = &Threema{}
/* receiving image // receiving image
session := o3.SessionContext{ dataMsg := o3.ImageMessage{
ID: o3.ThreemaID{ MessageHeader: &o3.MessageHeader{
ID: o3.NewIDString("12345678"), Sender: threemaFromIDByte,
Nick: o3.NewPubNick("user"),
}, },
}*/ }
imgMsg := o3.ImageMessage{} _, err := a.receiving(dataMsg)
_, err := a.receiving(imgMsg)
assert.Error(err) assert.Error(err)
a.threema.httpUploadPath = "/tmp" a.threema.httpUploadPath = "/tmp"
imgMsg = o3.ImageMessage{} dataMsg := o3.ImageMessage{
_, err = a.receiving(imgMsg) MessageHeader: &o3.MessageHeader{
Sender: threemaFromIDByte,
},
}
dataMsg = o3.ImageMessage{}
_, err = a.receiving(dataMsg)
assert.Error(err) assert.Error(err)
} }
*/
func TestReceiveDeliveryReceipt(t *testing.T) { func TestReceiveDeliveryReceipt(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
@ -111,20 +113,20 @@ func TestReceiveDeliveryReceipt(t *testing.T) {
a := createDummyAccount() a := createDummyAccount()
// receiving delivered // receiving delivered
session := o3.SessionContext{
ID: o3.ThreemaID{
ID: o3.NewIDString("12345678"),
Nick: o3.NewPubNick("user"),
},
}
msgID := o3.NewMsgID() msgID := o3.NewMsgID()
a.deliveredMSG[msgID] = "im4aeseeh1IbaQui" a.deliveredMSG[msgID] = "im4aeseeh1IbaQui"
a.readedMSG[msgID] = "im4aeseeh1IbaQui" a.readedMSG[msgID] = "im4aeseeh1IbaQui"
drm, err := o3.NewDeliveryReceiptMessage(&session, threemaID, msgID, o3.MSGDELIVERED) drm := o3.DeliveryReceiptMessage{
assert.NoError(err) MessageHeader: &o3.MessageHeader{
Sender: threemaFromIDByte,
},
Status: o3.MSGDELIVERED,
MessageID: msgID,
}
p, err := a.receiving(drm) p, err := a.receiving(drm)
assert.NoError(err) assert.NoError(err)
assert.NotNil(p)
xMSG, ok := p.(stanza.Message) xMSG, ok := p.(stanza.Message)
assert.True(ok) assert.True(ok)
rr := xMSG.Extensions[0].(stanza.ReceiptReceived) rr := xMSG.Extensions[0].(stanza.ReceiptReceived)
@ -136,10 +138,16 @@ func TestReceiveDeliveryReceipt(t *testing.T) {
assert.Nil(p) assert.Nil(p)
// receiving readed // receiving readed
drm, err = o3.NewDeliveryReceiptMessage(&session, threemaID, msgID, o3.MSGREAD) drm = o3.DeliveryReceiptMessage{
assert.NoError(err) MessageHeader: &o3.MessageHeader{
Sender: threemaFromIDByte,
},
MessageID: msgID,
Status: o3.MSGREAD,
}
p, err = a.receiving(drm) p, err = a.receiving(drm)
assert.NoError(err) assert.NoError(err)
assert.NotNil(p)
xMSG, ok = p.(stanza.Message) xMSG, ok = p.(stanza.Message)
assert.True(ok) assert.True(ok)
cmdd := xMSG.Extensions[0].(stanza.MarkDisplayed) cmdd := xMSG.Extensions[0].(stanza.MarkDisplayed)
@ -156,17 +164,27 @@ func TestReceiveTyping(t *testing.T) {
a := createDummyAccount() a := createDummyAccount()
// receiving inactive // receiving inactive
tnm := o3.TypingNotificationMessage{} tnm := o3.TypingNotificationMessage{
MessageHeader: &o3.MessageHeader{
Sender: threemaFromIDByte,
},
}
p, err := a.receiving(tnm) p, err := a.receiving(tnm)
assert.NotNil(p)
assert.NoError(err) assert.NoError(err)
xMSG, ok := p.(stanza.Message) xMSG, ok := p.(stanza.Message)
assert.True(ok) assert.True(ok)
assert.IsType(stanza.StateInactive{}, xMSG.Extensions[0]) assert.IsType(stanza.StateInactive{}, xMSG.Extensions[0])
// receiving composing // receiving composing
tnm = o3.TypingNotificationMessage{} tnm = o3.TypingNotificationMessage{
tnm.OnOff = 0x1 MessageHeader: &o3.MessageHeader{
Sender: threemaFromIDByte,
},
OnOff: 0x1,
}
p, err = a.receiving(tnm) p, err = a.receiving(tnm)
assert.NotNil(p)
assert.NoError(err) assert.NoError(err)
xMSG, ok = p.(stanza.Message) xMSG, ok = p.(stanza.Message)
assert.True(ok) assert.True(ok)

View File

@ -23,6 +23,8 @@ func (a *Account) sending(to string, msg stanza.Message) (o3.Message, error) {
"from": a.XMPP.String(), "from": a.XMPP.String(),
"to": to, "to": to,
}) })
msg3To := o3.NewIDString(to)
msg3From := o3.NewIDString(string(a.AccountThreema.TID))
chatState := false chatState := false
chatStateComposing := false chatStateComposing := false
@ -61,22 +63,29 @@ func (a *Account) sending(to string, msg stanza.Message) (o3.Message, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
msgType := o3.MSGDELIVERED drm := o3.DeliveryReceiptMessage{
if msgStateRead { MessageHeader: &o3.MessageHeader{
msgType = o3.MSGREAD Sender: msg3From,
ID: id,
Recipient: msg3To,
},
Status: o3.MSGDELIVERED,
} }
drm, err := o3.NewDeliveryReceiptMessage(&a.Session, to, id, msgType) if msgStateRead {
if err != nil { drm.Status = o3.MSGREAD
return nil, err
} }
logger.WithFields(map[string]interface{}{ logger.WithFields(map[string]interface{}{
"msg_id": id, "msg_id": id,
"type": msgType, "type": drm.Status,
}).Debug("update status of threema message") }).Debug("update status of threema message")
return drm, nil return drm, nil
} }
if chatState { if chatState {
tnm := o3.TypingNotificationMessage{} tnm := o3.TypingNotificationMessage{
MessageHeader: &o3.MessageHeader{
Sender: o3.NewIDString(string(a.AccountThreema.TID)),
},
}
if chatStateComposing { if chatStateComposing {
tnm.OnOff = 0x1 tnm.OnOff = 0x1
} }
@ -86,17 +95,22 @@ func (a *Account) sending(to string, msg stanza.Message) (o3.Message, error) {
return nil, nil return nil, nil
} }
} }
msg3ID := o3.NewMsgID()
// send text message // send text message
msg3, err := o3.NewTextMessage(&a.Session, to, msg.Body) msg3 := o3.TextMessage{
if err != nil { MessageHeader: &o3.MessageHeader{
return nil, err Sender: o3.NewIDString(string(a.AccountThreema.TID)),
ID: msg3ID,
Recipient: msg3To,
},
Body: msg.Body,
} }
a.deliveredMSG[msg3.ID()] = msg.Id a.deliveredMSG[msg3ID] = msg.Id
a.readedMSG[msg3.ID()] = msg.Id a.readedMSG[msg3ID] = msg.Id
logger.WithFields(map[string]interface{}{ logger.WithFields(map[string]interface{}{
"x_id": msg.Id, "x_id": msg.Id,
"t_id": msg3.ID(), "t_id": msg3ID,
"text": msg.Body, "text": msg.Body,
}).Debug("send text") }).Debug("send text")
return msg3, nil return msg3, nil

View File

@ -28,7 +28,7 @@ func TestAccountSend(t *testing.T) {
p := <-send p := <-send
assert.NotNil(p) assert.NotNil(p)
msg := p.(o3.TextMessage) msg := p.(o3.TextMessage)
assert.Contains(msg.Text(), "ohz8kai0ohNgohth") assert.Contains(msg.Body, "ohz8kai0ohNgohth")
// test error // test error
err := a.Send("a", stanza.Message{ err := a.Send("a", stanza.Message{
@ -67,7 +67,7 @@ func TestAccountSendingDeliviery(t *testing.T) {
assert.NoError(err) assert.NoError(err)
drm, ok := msg.(o3.DeliveryReceiptMessage) drm, ok := msg.(o3.DeliveryReceiptMessage)
assert.True(ok) assert.True(ok)
assert.Equal(o3.MSGDELIVERED, drm.Status()) assert.Equal(o3.MSGDELIVERED, drm.Status)
// test read // test read
msg, err = a.sending("a", stanza.Message{ msg, err = a.sending("a", stanza.Message{
@ -79,7 +79,7 @@ func TestAccountSendingDeliviery(t *testing.T) {
assert.NoError(err) assert.NoError(err)
drm, ok = msg.(o3.DeliveryReceiptMessage) drm, ok = msg.(o3.DeliveryReceiptMessage)
assert.True(ok) assert.True(ok)
assert.Equal(o3.MSGREAD, drm.Status()) assert.Equal(o3.MSGREAD, drm.Status)
} }
func TestSendTyping(t *testing.T) { func TestSendTyping(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
@ -128,5 +128,5 @@ func TestSendTyping(t *testing.T) {
assert.NoError(err) assert.NoError(err)
assert.NotNil(msg) assert.NotNil(msg)
o3msg := msg.(o3.TextMessage) o3msg := msg.(o3.TextMessage)
assert.Contains(o3msg.Text(), "hi") assert.Contains(o3msg.Body, "hi")
} }

2
vendor/github.com/o3ma/o3 generated vendored

@ -1 +1 @@
Subproject commit 16684c5acfd9abe4d27d52478c7a6c6d5113d29c Subproject commit 0c0e14982ac6e2886901faef423ed731911b3b39