threema refactory
This commit is contained in:
parent
39cd55266a
commit
b535a6f685
|
@ -19,13 +19,14 @@ func (a *Account) receiver(out chan<- stanza.Packet) {
|
|||
out <- xMSG
|
||||
continue
|
||||
}
|
||||
sender := receivedMessage.Msg.Sender().String()
|
||||
header := receivedMessage.Msg.Header()
|
||||
sender := header.Sender.String()
|
||||
if string(a.TID) == sender {
|
||||
continue
|
||||
}
|
||||
if p, err := a.receiving(receivedMessage.Msg); err != nil {
|
||||
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
|
||||
} else if p != nil {
|
||||
out <- p
|
||||
|
@ -40,19 +41,20 @@ func requestExtensions(xMSG *stanza.Message) {
|
|||
}
|
||||
|
||||
func (a *Account) receiving(receivedMessage o3.Message) (stanza.Packet, error) {
|
||||
header := receivedMessage.Header()
|
||||
sender := header.Sender.String()
|
||||
logger := log.WithFields(map[string]interface{}{
|
||||
"from": receivedMessage.Sender().String(),
|
||||
"from": header.Sender.String(),
|
||||
"to": a.XMPP.String(),
|
||||
})
|
||||
switch msg := receivedMessage.(type) {
|
||||
case o3.TextMessage:
|
||||
sender := msg.Sender().String()
|
||||
xMSG := stanza.NewMessage(stanza.Attrs{Type: stanza.MessageTypeChat, From: sender, To: a.XMPP.String(), Id: strconv.FormatUint(msg.ID(), 10)})
|
||||
xMSG.Body = msg.Text()
|
||||
xMSG := stanza.NewMessage(stanza.Attrs{Type: stanza.MessageTypeChat, From: sender, To: a.XMPP.String(), Id: strconv.FormatUint(header.ID, 10)})
|
||||
xMSG.Body = msg.Body
|
||||
requestExtensions(&xMSG)
|
||||
logger.WithField("text", xMSG.Body).Debug("send text")
|
||||
return xMSG, nil
|
||||
|
||||
/*
|
||||
case o3.AudioMessage:
|
||||
if a.threema.httpUploadPath == "" {
|
||||
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)
|
||||
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 {
|
||||
logger.Warnf("unable to create data from message: %s", 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)
|
||||
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 {
|
||||
logger.Warnf("unable to create data from message: %s", err)
|
||||
return nil, err
|
||||
|
@ -90,27 +92,26 @@ func (a *Account) receiving(receivedMessage o3.Message) (stanza.Packet, error) {
|
|||
requestExtensions(&xMSG)
|
||||
logger.WithField("url", xMSG.Body).Debug("send image")
|
||||
return xMSG, nil
|
||||
|
||||
*/
|
||||
case o3.DeliveryReceiptMessage:
|
||||
msgID := msg.MsgID()
|
||||
xMSG := stanza.NewMessage(stanza.Attrs{Type: stanza.MessageTypeChat, From: msg.Sender().String(), To: a.XMPP.String()})
|
||||
xMSG := stanza.NewMessage(stanza.Attrs{Type: stanza.MessageTypeChat, From: sender, To: a.XMPP.String()})
|
||||
state := ""
|
||||
|
||||
if msg.Status() == o3.MSGDELIVERED {
|
||||
if msg.Status == o3.MSGDELIVERED {
|
||||
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.MarkReceived{ID: id})
|
||||
delete(a.deliveredMSG, msgID)
|
||||
delete(a.deliveredMSG, msg.MessageID)
|
||||
} else {
|
||||
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"
|
||||
if id, ok := a.readedMSG[msgID]; ok {
|
||||
if id, ok := a.readedMSG[msg.MessageID]; ok {
|
||||
xMSG.Extensions = append(xMSG.Extensions, stanza.MarkDisplayed{ID: id})
|
||||
delete(a.readedMSG, msgID)
|
||||
delete(a.readedMSG, msg.MessageID)
|
||||
} else {
|
||||
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
|
||||
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 {
|
||||
logger.Debug("composing")
|
||||
xMSG.Extensions = append(xMSG.Extensions, stanza.StateComposing{})
|
||||
|
|
|
@ -8,12 +8,12 @@ import (
|
|||
"gosrc.io/xmpp/stanza"
|
||||
)
|
||||
|
||||
const threemaID = "87654321"
|
||||
const threemaFromID = "87654321"
|
||||
|
||||
var threemaIDByte o3.IDString
|
||||
var threemaFromIDByte o3.IDString
|
||||
|
||||
func init() {
|
||||
threemaIDByte = o3.NewIDString(threemaID)
|
||||
threemaFromIDByte = o3.NewIDString(threemaFromID)
|
||||
}
|
||||
|
||||
func createDummyAccount() Account {
|
||||
|
@ -21,8 +21,8 @@ func createDummyAccount() Account {
|
|||
deliveredMSG: make(map[uint64]string),
|
||||
readedMSG: make(map[uint64]string),
|
||||
}
|
||||
a.TID = make([]byte, len(threemaIDByte))
|
||||
copy(a.TID, threemaIDByte[:])
|
||||
a.TID = make([]byte, len(threemaFromIDByte))
|
||||
copy(a.TID, threemaFromIDByte[:])
|
||||
|
||||
return a
|
||||
}
|
||||
|
@ -45,35 +45,33 @@ func TestReceiveText(t *testing.T) {
|
|||
a := createDummyAccount()
|
||||
|
||||
// receiving text
|
||||
session := o3.SessionContext{
|
||||
ID: o3.ThreemaID{
|
||||
ID: o3.NewIDString("12345678"),
|
||||
Nick: o3.NewPubNick("user"),
|
||||
txtMsg := o3.TextMessage{
|
||||
MessageHeader: &o3.MessageHeader{
|
||||
Sender: threemaFromIDByte,
|
||||
Recipient: o3.NewIDString("12345678"),
|
||||
},
|
||||
Body: "Oojoh0Ah",
|
||||
}
|
||||
txtMsg, err := o3.NewTextMessage(&session, threemaID, "Oojoh0Ah")
|
||||
assert.NoError(err)
|
||||
p, err := a.receiving(txtMsg)
|
||||
assert.NoError(err)
|
||||
assert.NotNil(p)
|
||||
xMSG, ok := p.(stanza.Message)
|
||||
assert.True(ok)
|
||||
assert.Equal("Oojoh0Ah", xMSG.Body)
|
||||
}
|
||||
|
||||
/*
|
||||
func TestReceiveAudio(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
a := createDummyAccount()
|
||||
a.threema = &Threema{}
|
||||
|
||||
/* receiving image
|
||||
session := o3.SessionContext{
|
||||
ID: o3.ThreemaID{
|
||||
ID: o3.NewIDString("12345678"),
|
||||
Nick: o3.NewPubNick("user"),
|
||||
dataMsg := o3.ImageMessage{
|
||||
MessageHeader: &o3.MessageHeader{
|
||||
Sender: threemaFromIDByte,
|
||||
},
|
||||
}*/
|
||||
dataMsg := o3.AudioMessage{}
|
||||
}
|
||||
_, err := a.receiving(dataMsg)
|
||||
assert.Error(err)
|
||||
|
||||
|
@ -88,22 +86,26 @@ func TestReceiveImage(t *testing.T) {
|
|||
a := createDummyAccount()
|
||||
a.threema = &Threema{}
|
||||
|
||||
/* receiving image
|
||||
session := o3.SessionContext{
|
||||
ID: o3.ThreemaID{
|
||||
ID: o3.NewIDString("12345678"),
|
||||
Nick: o3.NewPubNick("user"),
|
||||
// receiving image
|
||||
dataMsg := o3.ImageMessage{
|
||||
MessageHeader: &o3.MessageHeader{
|
||||
Sender: threemaFromIDByte,
|
||||
},
|
||||
}*/
|
||||
imgMsg := o3.ImageMessage{}
|
||||
_, err := a.receiving(imgMsg)
|
||||
}
|
||||
_, err := a.receiving(dataMsg)
|
||||
assert.Error(err)
|
||||
|
||||
a.threema.httpUploadPath = "/tmp"
|
||||
imgMsg = o3.ImageMessage{}
|
||||
_, err = a.receiving(imgMsg)
|
||||
dataMsg := o3.ImageMessage{
|
||||
MessageHeader: &o3.MessageHeader{
|
||||
Sender: threemaFromIDByte,
|
||||
},
|
||||
}
|
||||
dataMsg = o3.ImageMessage{}
|
||||
_, err = a.receiving(dataMsg)
|
||||
assert.Error(err)
|
||||
}
|
||||
*/
|
||||
|
||||
func TestReceiveDeliveryReceipt(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
@ -111,20 +113,20 @@ func TestReceiveDeliveryReceipt(t *testing.T) {
|
|||
a := createDummyAccount()
|
||||
|
||||
// receiving delivered
|
||||
session := o3.SessionContext{
|
||||
ID: o3.ThreemaID{
|
||||
ID: o3.NewIDString("12345678"),
|
||||
Nick: o3.NewPubNick("user"),
|
||||
},
|
||||
}
|
||||
msgID := o3.NewMsgID()
|
||||
a.deliveredMSG[msgID] = "im4aeseeh1IbaQui"
|
||||
a.readedMSG[msgID] = "im4aeseeh1IbaQui"
|
||||
|
||||
drm, err := o3.NewDeliveryReceiptMessage(&session, threemaID, msgID, o3.MSGDELIVERED)
|
||||
assert.NoError(err)
|
||||
drm := o3.DeliveryReceiptMessage{
|
||||
MessageHeader: &o3.MessageHeader{
|
||||
Sender: threemaFromIDByte,
|
||||
},
|
||||
Status: o3.MSGDELIVERED,
|
||||
MessageID: msgID,
|
||||
}
|
||||
p, err := a.receiving(drm)
|
||||
assert.NoError(err)
|
||||
assert.NotNil(p)
|
||||
xMSG, ok := p.(stanza.Message)
|
||||
assert.True(ok)
|
||||
rr := xMSG.Extensions[0].(stanza.ReceiptReceived)
|
||||
|
@ -136,10 +138,16 @@ func TestReceiveDeliveryReceipt(t *testing.T) {
|
|||
assert.Nil(p)
|
||||
|
||||
// receiving readed
|
||||
drm, err = o3.NewDeliveryReceiptMessage(&session, threemaID, msgID, o3.MSGREAD)
|
||||
assert.NoError(err)
|
||||
drm = o3.DeliveryReceiptMessage{
|
||||
MessageHeader: &o3.MessageHeader{
|
||||
Sender: threemaFromIDByte,
|
||||
},
|
||||
MessageID: msgID,
|
||||
Status: o3.MSGREAD,
|
||||
}
|
||||
p, err = a.receiving(drm)
|
||||
assert.NoError(err)
|
||||
assert.NotNil(p)
|
||||
xMSG, ok = p.(stanza.Message)
|
||||
assert.True(ok)
|
||||
cmdd := xMSG.Extensions[0].(stanza.MarkDisplayed)
|
||||
|
@ -156,17 +164,27 @@ func TestReceiveTyping(t *testing.T) {
|
|||
a := createDummyAccount()
|
||||
|
||||
// receiving inactive
|
||||
tnm := o3.TypingNotificationMessage{}
|
||||
tnm := o3.TypingNotificationMessage{
|
||||
MessageHeader: &o3.MessageHeader{
|
||||
Sender: threemaFromIDByte,
|
||||
},
|
||||
}
|
||||
p, err := a.receiving(tnm)
|
||||
assert.NotNil(p)
|
||||
assert.NoError(err)
|
||||
xMSG, ok := p.(stanza.Message)
|
||||
assert.True(ok)
|
||||
assert.IsType(stanza.StateInactive{}, xMSG.Extensions[0])
|
||||
|
||||
// receiving composing
|
||||
tnm = o3.TypingNotificationMessage{}
|
||||
tnm.OnOff = 0x1
|
||||
tnm = o3.TypingNotificationMessage{
|
||||
MessageHeader: &o3.MessageHeader{
|
||||
Sender: threemaFromIDByte,
|
||||
},
|
||||
OnOff: 0x1,
|
||||
}
|
||||
p, err = a.receiving(tnm)
|
||||
assert.NotNil(p)
|
||||
assert.NoError(err)
|
||||
xMSG, ok = p.(stanza.Message)
|
||||
assert.True(ok)
|
||||
|
|
|
@ -23,6 +23,8 @@ func (a *Account) sending(to string, msg stanza.Message) (o3.Message, error) {
|
|||
"from": a.XMPP.String(),
|
||||
"to": to,
|
||||
})
|
||||
msg3To := o3.NewIDString(to)
|
||||
msg3From := o3.NewIDString(string(a.AccountThreema.TID))
|
||||
|
||||
chatState := false
|
||||
chatStateComposing := false
|
||||
|
@ -61,22 +63,29 @@ func (a *Account) sending(to string, msg stanza.Message) (o3.Message, error) {
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
msgType := o3.MSGDELIVERED
|
||||
if msgStateRead {
|
||||
msgType = o3.MSGREAD
|
||||
drm := o3.DeliveryReceiptMessage{
|
||||
MessageHeader: &o3.MessageHeader{
|
||||
Sender: msg3From,
|
||||
ID: id,
|
||||
Recipient: msg3To,
|
||||
},
|
||||
Status: o3.MSGDELIVERED,
|
||||
}
|
||||
drm, err := o3.NewDeliveryReceiptMessage(&a.Session, to, id, msgType)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
if msgStateRead {
|
||||
drm.Status = o3.MSGREAD
|
||||
}
|
||||
logger.WithFields(map[string]interface{}{
|
||||
"msg_id": id,
|
||||
"type": msgType,
|
||||
"type": drm.Status,
|
||||
}).Debug("update status of threema message")
|
||||
return drm, nil
|
||||
}
|
||||
if chatState {
|
||||
tnm := o3.TypingNotificationMessage{}
|
||||
tnm := o3.TypingNotificationMessage{
|
||||
MessageHeader: &o3.MessageHeader{
|
||||
Sender: o3.NewIDString(string(a.AccountThreema.TID)),
|
||||
},
|
||||
}
|
||||
if chatStateComposing {
|
||||
tnm.OnOff = 0x1
|
||||
}
|
||||
|
@ -86,17 +95,22 @@ func (a *Account) sending(to string, msg stanza.Message) (o3.Message, error) {
|
|||
return nil, nil
|
||||
}
|
||||
}
|
||||
msg3ID := o3.NewMsgID()
|
||||
|
||||
// send text message
|
||||
msg3, err := o3.NewTextMessage(&a.Session, to, msg.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
msg3 := o3.TextMessage{
|
||||
MessageHeader: &o3.MessageHeader{
|
||||
Sender: o3.NewIDString(string(a.AccountThreema.TID)),
|
||||
ID: msg3ID,
|
||||
Recipient: msg3To,
|
||||
},
|
||||
Body: msg.Body,
|
||||
}
|
||||
a.deliveredMSG[msg3.ID()] = msg.Id
|
||||
a.readedMSG[msg3.ID()] = msg.Id
|
||||
a.deliveredMSG[msg3ID] = msg.Id
|
||||
a.readedMSG[msg3ID] = msg.Id
|
||||
logger.WithFields(map[string]interface{}{
|
||||
"x_id": msg.Id,
|
||||
"t_id": msg3.ID(),
|
||||
"t_id": msg3ID,
|
||||
"text": msg.Body,
|
||||
}).Debug("send text")
|
||||
return msg3, nil
|
||||
|
|
|
@ -28,7 +28,7 @@ func TestAccountSend(t *testing.T) {
|
|||
p := <-send
|
||||
assert.NotNil(p)
|
||||
msg := p.(o3.TextMessage)
|
||||
assert.Contains(msg.Text(), "ohz8kai0ohNgohth")
|
||||
assert.Contains(msg.Body, "ohz8kai0ohNgohth")
|
||||
|
||||
// test error
|
||||
err := a.Send("a", stanza.Message{
|
||||
|
@ -67,7 +67,7 @@ func TestAccountSendingDeliviery(t *testing.T) {
|
|||
assert.NoError(err)
|
||||
drm, ok := msg.(o3.DeliveryReceiptMessage)
|
||||
assert.True(ok)
|
||||
assert.Equal(o3.MSGDELIVERED, drm.Status())
|
||||
assert.Equal(o3.MSGDELIVERED, drm.Status)
|
||||
|
||||
// test read
|
||||
msg, err = a.sending("a", stanza.Message{
|
||||
|
@ -79,7 +79,7 @@ func TestAccountSendingDeliviery(t *testing.T) {
|
|||
assert.NoError(err)
|
||||
drm, ok = msg.(o3.DeliveryReceiptMessage)
|
||||
assert.True(ok)
|
||||
assert.Equal(o3.MSGREAD, drm.Status())
|
||||
assert.Equal(o3.MSGREAD, drm.Status)
|
||||
}
|
||||
func TestSendTyping(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
@ -128,5 +128,5 @@ func TestSendTyping(t *testing.T) {
|
|||
assert.NoError(err)
|
||||
assert.NotNil(msg)
|
||||
o3msg := msg.(o3.TextMessage)
|
||||
assert.Contains(o3msg.Text(), "hi")
|
||||
assert.Contains(o3msg.Body, "hi")
|
||||
}
|
||||
|
|
|
@ -1 +1 @@
|
|||
Subproject commit 16684c5acfd9abe4d27d52478c7a6c6d5113d29c
|
||||
Subproject commit 0c0e14982ac6e2886901faef423ed731911b3b39
|
Reference in New Issue