5
0
mirror of https://github.com/cwinfo/matterbridge.git synced 2024-11-26 04:21:38 +00:00

Refactor image downloads (whatsapp)

This commit is contained in:
Wim 2020-11-29 15:16:26 +01:00
parent 2c20f72a9c
commit 95625f6871

View File

@ -139,72 +139,57 @@ func (b *Bwhatsapp) HandleTextMessage(message whatsapp.TextMessage) {
// HandleImageMessage sent from WhatsApp, relay it to the brige // HandleImageMessage sent from WhatsApp, relay it to the brige
func (b *Bwhatsapp) HandleImageMessage(message whatsapp.ImageMessage) { func (b *Bwhatsapp) HandleImageMessage(message whatsapp.ImageMessage) {
if message.Info.FromMe { // || !strings.Contains(strings.ToLower(message.Text), "@echo") { if message.Info.FromMe || message.Info.Timestamp < b.startedAt {
return return
} }
// whatsapp sends last messages to show context , cut them
if message.Info.Timestamp < b.startedAt {
return
}
messageTime := time.Unix(int64(message.Info.Timestamp), 0) // TODO check how behaves between timezones
groupJID := message.Info.RemoteJid
senderJID := message.Info.SenderJid senderJID := message.Info.SenderJid
if len(senderJID) == 0 { if len(message.Info.SenderJid) == 0 && message.Info.Source != nil && message.Info.Source.Participant != nil {
// TODO workaround till https://github.com/Rhymen/go-whatsapp/issues/86 resolved senderJID = *message.Info.Source.Participant
if message.Info.Source != nil && message.Info.Source.Participant != nil {
senderJID = *message.Info.Source.Participant
}
} }
// translate sender's Jid to the nicest username we can get senderName := b.getSenderName(message.Info.SenderJid)
senderName := b.getSenderName(senderJID)
if senderName == "" { if senderName == "" {
senderName = "Someone" // don't expose telephone number senderName = "Someone" // don't expose telephone number
} }
b.Log.Debugf("<= Sending message from %s on %s to gateway", senderJID, b.Account)
rmsg := config.Message{ rmsg := config.Message{
UserID: senderJID, UserID: senderJID,
Username: senderName, Username: senderName,
Timestamp: messageTime, Channel: message.Info.RemoteJid,
Channel: groupJID, Account: b.Account,
Account: b.Account, Protocol: b.Protocol,
Protocol: b.Protocol, Extra: make(map[string][]interface{}),
Extra: make(map[string][]interface{}), ID: message.Info.Id,
// ParentID: TODO, // TODO handle thread replies // map from Info.QuotedMessageID string
ID: message.Info.Id,
} }
if avatarURL, exists := b.userAvatars[senderJID]; exists { if avatarURL, exists := b.userAvatars[senderJID]; exists {
rmsg.Avatar = avatarURL rmsg.Avatar = avatarURL
} }
// Download and unencrypt content
data, err := message.Download()
if err != nil {
b.Log.Errorf("%v", err)
return
}
// Get file extension by mimetype
fileExt, err := mime.ExtensionsByType(message.Type) fileExt, err := mime.ExtensionsByType(message.Type)
if err != nil { if err != nil {
b.Log.Errorf("%v", err) b.Log.Errorf("Mimetype detection error: %s", err)
return return
} }
filename := fmt.Sprintf("%v%v", message.Info.Id, fileExt[0]) filename := fmt.Sprintf("%v%v", message.Info.Id, fileExt[0])
b.Log.Debugf("<= Image downloaded and unencrypted") b.Log.Debugf("Trying to download %s with type %s", filename, message.Type)
data, err := message.Download()
if err != nil {
b.Log.Errorf("Download image failed: %s", err)
return
}
// Move file to bridge storage // Move file to bridge storage
helper.HandleDownloadData(b.Log, &rmsg, filename, message.Caption, "", &data, b.General) helper.HandleDownloadData(b.Log, &rmsg, filename, message.Caption, "", &data, b.General)
b.Log.Debugf("<= Image Message is %#v", rmsg) b.Log.Debugf("<= Sending message from %s on %s to gateway", senderJID, b.Account)
b.Log.Debugf("<= Message is %#v", rmsg)
b.Remote <- rmsg b.Remote <- rmsg
} }
@ -322,10 +307,3 @@ func (b *Bwhatsapp) HandleAudioMessage(message whatsapp.AudioMessage) {
b.Remote <- rmsg b.Remote <- rmsg
} }
//
//func (b *Bwhatsapp) HandleJsonMessage(message string) {
// fmt.Println(message) // TODO implement
//}
// TODO HandleRawMessage
// TODO HandleAudioMessage