add image+audio support + refactory group

This commit is contained in:
Martin/Geno 2019-09-09 13:27:06 +02:00 committed by genofire
parent 836efe59a4
commit eef58dcfba
3 changed files with 58 additions and 62 deletions

View File

@ -58,65 +58,57 @@ func (a *Account) receiving(receivedMessage o3.Message) (stanza.Packet, error) {
}) })
sender = strings.ToLower(sender) sender = strings.ToLower(sender)
switch msg := receivedMessage.(type) { 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: 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)}) 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 xMSG.Body = msg.Body
requestExtensions(&xMSG)
logger.WithFields(map[string]interface{}{ logger.WithFields(map[string]interface{}{
"from_x": xMSG.From, "from_x": xMSG.From,
"id": xMSG.Id, "id": xMSG.Id,
"text": xMSG.Body, "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 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: case *o3.ImageMessage:
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")
} }
data, err := msg.GetImageData(a.Session) data, err := msg.GetData(a.ThreemaID)
if err != nil { if err != nil {
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(sender.String(), header.ID, "jpg", data) xMSG, err := a.FileToXMPP(sender, 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
} }
xMSG.Type = "chat" requestExtensions(&xMSG)
requestExtensions(&xMSG) logger.WithField("url", xMSG.Body).Debug("recv image")
logger.WithField("url", xMSG.Body).Debug("recv image") return xMSG, nil
return xMSG, nil
*/
case *o3.DeliveryReceiptMessage: case *o3.DeliveryReceiptMessage:
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()})
state := "" state := ""

View File

@ -60,14 +60,13 @@ func TestReceiveText(t *testing.T) {
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{}
dataMsg := o3.ImageMessage{ dataMsg := o3.AudioMessage{
MessageHeader: &o3.MessageHeader{ MessageHeader: &o3.MessageHeader{
Sender: threemaFromIDByte, Sender: threemaFromIDByte,
}, },
@ -76,7 +75,11 @@ func TestReceiveAudio(t *testing.T) {
assert.Error(err) assert.Error(err)
a.threema.httpUploadPath = "/tmp" a.threema.httpUploadPath = "/tmp"
dataMsg = o3.AudioMessage{} dataMsg = o3.AudioMessage{
MessageHeader: &o3.MessageHeader{
Sender: threemaFromIDByte,
},
}
_, err = a.receiving(dataMsg) _, err = a.receiving(dataMsg)
assert.Error(err) assert.Error(err)
} }
@ -96,16 +99,19 @@ func TestReceiveImage(t *testing.T) {
assert.Error(err) assert.Error(err)
a.threema.httpUploadPath = "/tmp" a.threema.httpUploadPath = "/tmp"
dataMsg := o3.ImageMessage{ dataMsg = o3.ImageMessage{
MessageHeader: &o3.MessageHeader{
Sender: threemaFromIDByte,
},
}
dataMsg = o3.ImageMessage{
MessageHeader: &o3.MessageHeader{ MessageHeader: &o3.MessageHeader{
Sender: threemaFromIDByte, Sender: threemaFromIDByte,
}, },
} }
dataMsg = o3.ImageMessage{}
_, err = a.receiving(dataMsg) _, 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)

View File

@ -113,8 +113,9 @@ func (a *Account) sending(to string, msg stanza.Message) (o3.Message, error) {
// send text message // send text message
msg3 := &o3.TextMessage{ msg3 := &o3.TextMessage{
MessageHeader: header, GroupMessageHeader: groupHeader,
Body: msg.Body, MessageHeader: header,
Body: msg.Body,
} }
logger = logger.WithFields(map[string]interface{}{ logger = logger.WithFields(map[string]interface{}{
"x_id": msg.Id, "x_id": msg.Id,
@ -124,10 +125,7 @@ func (a *Account) sending(to string, msg stanza.Message) (o3.Message, error) {
if groupHeader != nil { if groupHeader != nil {
logger.Debug("send grouptext") logger.Debug("send grouptext")
// TODO iterate of all occupants // TODO iterate of all occupants
return &o3.GroupTextMessage{ return msg3, nil
GroupMessageHeader: groupHeader,
TextMessage: msg3,
}, nil
} }
a.deliveredMSG[msg3ID] = msg.Id a.deliveredMSG[msg3ID] = msg.Id
a.readedMSG[msg3ID] = msg.Id a.readedMSG[msg3ID] = msg.Id