From eef58dcfbac6fdd368b1ba02eb4e91bb8ed10716 Mon Sep 17 00:00:00 2001 From: Martin/Geno Date: Mon, 9 Sep 2019 13:27:06 +0200 Subject: [PATCH] add image+audio support + refactory group --- component/threema/receive.go | 92 ++++++++++++++----------------- component/threema/receive_test.go | 18 ++++-- component/threema/send.go | 10 ++-- 3 files changed, 58 insertions(+), 62 deletions(-) diff --git a/component/threema/receive.go b/component/threema/receive.go index 637d9b2..7ba463d 100644 --- a/component/threema/receive.go +++ b/component/threema/receive.go @@ -58,65 +58,57 @@ func (a *Account) receiving(receivedMessage o3.Message) (stanza.Packet, error) { }) sender = strings.ToLower(sender) switch msg := receivedMessage.(type) { - case *o3.GroupTextMessage: - xMSG := stanza.NewMessage(stanza.Attrs{Type: stanza.MessageTypeGroupchat, From: jidFromThreemaGroup(sender, msg.GroupMessageHeader), To: a.XMPP.String(), Id: strconv.FormatUint(header.ID, 10)}) - xMSG.Body = msg.Body - requestExtensions(&xMSG) - logger.WithFields(map[string]interface{}{ - "from_x": xMSG.From, - "id": xMSG.Id, - "text": xMSG.Body, - }).Debug("recv grouptext") - return xMSG, nil case *o3.TextMessage: + dbText := "recv text" xMSG := stanza.NewMessage(stanza.Attrs{Type: stanza.MessageTypeChat, From: sender, To: a.XMPP.String(), Id: strconv.FormatUint(header.ID, 10)}) + if msg.GroupMessageHeader != nil { + xMSG = stanza.NewMessage(stanza.Attrs{Type: stanza.MessageTypeGroupchat, From: jidFromThreemaGroup(sender, msg.GroupMessageHeader), To: a.XMPP.String(), Id: strconv.FormatUint(header.ID, 10)}) + dbText = "recv grouptext" + } else { + requestExtensions(&xMSG) + } xMSG.Body = msg.Body - requestExtensions(&xMSG) logger.WithFields(map[string]interface{}{ "from_x": xMSG.From, "id": xMSG.Id, "text": xMSG.Body, - }).Debug("recv text") + }).Debug(dbText) + return xMSG, nil + case *o3.AudioMessage: + if a.threema.httpUploadPath == "" { + return nil, errors.New("no place to store files at transport configurated") + } + data, err := msg.GetData() + if err != nil { + logger.Warnf("unable to read data from message: %s", err) + return nil, err + } + xMSG, err := a.FileToXMPP(sender, header.ID, "mp3", data) + if err != nil { + logger.Warnf("unable to create data from message: %s", err) + return nil, err + } + requestExtensions(&xMSG) + logger.WithField("url", xMSG.Body).Debug("recv audio") return xMSG, nil - /* - case o3.AudioMessage: - if a.threema.httpUploadPath == "" { - return nil, errors.New("no place to store files at transport configurated") - } - data, err := msg.GetAudioData(a.Session) - if err != nil { - logger.Warnf("unable to read data from message: %s", err) - return nil, err - } - 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 - } - xMSG.Type = "chat" - requestExtensions(&xMSG) - logger.WithField("url", xMSG.Body).Debug("recv audio") - return xMSG, nil - case o3.ImageMessage: - if a.threema.httpUploadPath == "" { - return nil, errors.New("no place to store files at transport configurated") - } - data, err := msg.GetImageData(a.Session) - if err != nil { - logger.Warnf("unable to read data from message: %s", err) - return nil, err - } - 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 - } - xMSG.Type = "chat" - requestExtensions(&xMSG) - logger.WithField("url", xMSG.Body).Debug("recv image") - return xMSG, nil - */ + case *o3.ImageMessage: + if a.threema.httpUploadPath == "" { + return nil, errors.New("no place to store files at transport configurated") + } + data, err := msg.GetData(a.ThreemaID) + if err != nil { + logger.Warnf("unable to read data from message: %s", err) + return nil, err + } + xMSG, err := a.FileToXMPP(sender, header.ID, "jpg", data) + if err != nil { + logger.Warnf("unable to create data from message: %s", err) + return nil, err + } + requestExtensions(&xMSG) + logger.WithField("url", xMSG.Body).Debug("recv image") + return xMSG, nil case *o3.DeliveryReceiptMessage: xMSG := stanza.NewMessage(stanza.Attrs{Type: stanza.MessageTypeChat, From: sender, To: a.XMPP.String()}) state := "" diff --git a/component/threema/receive_test.go b/component/threema/receive_test.go index 378b374..4f66265 100644 --- a/component/threema/receive_test.go +++ b/component/threema/receive_test.go @@ -60,14 +60,13 @@ func TestReceiveText(t *testing.T) { assert.Equal("Oojoh0Ah", xMSG.Body) } -/* func TestReceiveAudio(t *testing.T) { assert := assert.New(t) a := createDummyAccount() a.threema = &Threema{} - dataMsg := o3.ImageMessage{ + dataMsg := o3.AudioMessage{ MessageHeader: &o3.MessageHeader{ Sender: threemaFromIDByte, }, @@ -76,7 +75,11 @@ func TestReceiveAudio(t *testing.T) { assert.Error(err) a.threema.httpUploadPath = "/tmp" - dataMsg = o3.AudioMessage{} + dataMsg = o3.AudioMessage{ + MessageHeader: &o3.MessageHeader{ + Sender: threemaFromIDByte, + }, + } _, err = a.receiving(dataMsg) assert.Error(err) } @@ -96,16 +99,19 @@ func TestReceiveImage(t *testing.T) { assert.Error(err) a.threema.httpUploadPath = "/tmp" - dataMsg := o3.ImageMessage{ + dataMsg = o3.ImageMessage{ + MessageHeader: &o3.MessageHeader{ + Sender: threemaFromIDByte, + }, + } + 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) diff --git a/component/threema/send.go b/component/threema/send.go index 54743d0..05c2de3 100644 --- a/component/threema/send.go +++ b/component/threema/send.go @@ -113,8 +113,9 @@ func (a *Account) sending(to string, msg stanza.Message) (o3.Message, error) { // send text message msg3 := &o3.TextMessage{ - MessageHeader: header, - Body: msg.Body, + GroupMessageHeader: groupHeader, + MessageHeader: header, + Body: msg.Body, } logger = logger.WithFields(map[string]interface{}{ "x_id": msg.Id, @@ -124,10 +125,7 @@ func (a *Account) sending(to string, msg stanza.Message) (o3.Message, error) { if groupHeader != nil { logger.Debug("send grouptext") // TODO iterate of all occupants - return &o3.GroupTextMessage{ - GroupMessageHeader: groupHeader, - TextMessage: msg3, - }, nil + return msg3, nil } a.deliveredMSG[msg3ID] = msg.Id a.readedMSG[msg3ID] = msg.Id