More attachment robust support
This commit is contained in:
parent
10ca97310e
commit
dba9b05c88
15
ROADMAP.md
15
ROADMAP.md
@ -2,7 +2,7 @@
|
||||
* Matrix → GroupMe
|
||||
* [ ] Message content
|
||||
* [x] Plain text
|
||||
* [ ] Formatted messages
|
||||
* [ ] Formatted messages<sup>3</sup>
|
||||
* [ ] Media/files
|
||||
* [ ] Replies
|
||||
* [ ] Message redactions
|
||||
@ -23,13 +23,12 @@
|
||||
* GroupMe → Matrix
|
||||
* [ ] Message content
|
||||
* [x] Plain text
|
||||
* [ ] Formatted messages
|
||||
* [ ] Media/files
|
||||
* [x] Media/files
|
||||
* [x] Images
|
||||
* [x] Videos
|
||||
* [x] Random Files
|
||||
* [ ] Location messages
|
||||
* [ ] Contact messages
|
||||
* [x] Location messages<sup>1</sup>
|
||||
* [ ] Polls<sup>3</sup>
|
||||
* [ ] Replies
|
||||
* [ ] Chat types
|
||||
* [ ] Private chat
|
||||
@ -38,6 +37,10 @@
|
||||
* [ ] Presence
|
||||
* [ ] Typing notifications
|
||||
* [ ] Read receipts
|
||||
* [ ] Calendar things
|
||||
* [ ] Events created
|
||||
* [ ] Events modified
|
||||
* [ ] Going/Not
|
||||
* [ ] Admin/superadmin status
|
||||
* [ ] Membership actions
|
||||
* [ ] Invite
|
||||
@ -64,6 +67,6 @@
|
||||
* [ ] Option to use own Matrix account for messages sent from WhatsApp mobile/other web clients
|
||||
* [ ] Shared group chat portals
|
||||
|
||||
<sup>[1]</sup> May involve reverse-engineering the WhatsApp Web API and/or editing go-whatsapp
|
||||
<sup>[1]</sup> Basic feature works. Improvements are TODO.
|
||||
<sup>[2]</sup> May already work
|
||||
<sup>[3]</sup> May not be possible
|
||||
|
@ -78,7 +78,7 @@ type Message struct {
|
||||
|
||||
Chat PortalKey `gorm:"primaryKey;embedded;embeddedPrefix:chat_"`
|
||||
JID types.GroupMeID `gorm:"primaryKey"`
|
||||
MXID id.EventID `gorm:"unique;notNull"`
|
||||
MXID id.EventID `gorm:"primaryKey;unique;notNull"`
|
||||
Sender types.GroupMeID `gorm:"notNull"`
|
||||
Timestamp uint64 `gorm:"notNull;default:0"`
|
||||
Content *groupmeExt.Message `gorm:"type:TEXT;notNull"`
|
||||
|
89
portal.go
89
portal.go
@ -28,12 +28,14 @@ import (
|
||||
"image/jpeg"
|
||||
"image/png"
|
||||
"io/ioutil"
|
||||
"math"
|
||||
"math/rand"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
@ -1260,13 +1262,13 @@ func (portal *Portal) sendMessageDirect(intent *appservice.IntentAPI, eventType
|
||||
}
|
||||
}
|
||||
|
||||
func (portal *Portal) handleAttachment(intent *appservice.IntentAPI, attachment *groupme.Attachment, source *User, message *groupme.Message) (err error, sendText bool) {
|
||||
func (portal *Portal) handleAttachment(intent *appservice.IntentAPI, attachment *groupme.Attachment, source *User, message *groupme.Message) (msg *event.MessageEventContent, sendText bool, err error) {
|
||||
sendText = true
|
||||
switch attachment.Type {
|
||||
case "image":
|
||||
imgData, mime, err := groupmeExt.DownloadImage(attachment.URL)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to load media info: %w", err), true
|
||||
return nil, true, fmt.Errorf("failed to load media info: %w", err)
|
||||
}
|
||||
|
||||
var width, height int
|
||||
@ -1285,7 +1287,7 @@ func (portal *Portal) handleAttachment(intent *appservice.IntentAPI, attachment
|
||||
} else {
|
||||
err = fmt.Errorf("failed to upload media: %w", err)
|
||||
}
|
||||
return err, true
|
||||
return nil, true, err
|
||||
}
|
||||
attachmentUrl, _ := url.Parse(attachment.URL)
|
||||
urlParts := strings.Split(attachmentUrl.Path, ".")
|
||||
@ -1315,15 +1317,8 @@ func (portal *Portal) handleAttachment(intent *appservice.IntentAPI, attachment
|
||||
}
|
||||
//TODO thumbnail since groupme supports it anyway
|
||||
content.MsgType = event.MsgImage
|
||||
_, _ = intent.UserTyping(portal.MXID, false, 0)
|
||||
|
||||
eventType := event.EventMessage
|
||||
|
||||
_, err = portal.sendMessage(intent, eventType, content, message.CreatedAt.ToTime().Unix())
|
||||
if err != nil {
|
||||
portal.log.Errorfln("Failed to handle message %s: %v", "TODOID", err)
|
||||
return nil, true
|
||||
}
|
||||
return content, true, nil
|
||||
case "video":
|
||||
vidContents, mime := groupmeExt.DownloadVideo(attachment.VideoPreviewURL, attachment.URL, source.Token)
|
||||
if mime == "" {
|
||||
@ -1340,7 +1335,7 @@ func (portal *Portal) handleAttachment(intent *appservice.IntentAPI, attachment
|
||||
} else {
|
||||
err = fmt.Errorf("failed to upload media: %w", err)
|
||||
}
|
||||
return err, true
|
||||
return nil, true, err
|
||||
}
|
||||
|
||||
text := strings.Split(attachment.URL, "/")
|
||||
@ -1361,18 +1356,9 @@ func (portal *Portal) handleAttachment(intent *appservice.IntentAPI, attachment
|
||||
content.URL = uploaded.ContentURI.CUString()
|
||||
}
|
||||
content.MsgType = event.MsgVideo
|
||||
_, _ = intent.UserTyping(portal.MXID, false, 0)
|
||||
|
||||
eventType := event.EventMessage
|
||||
|
||||
_, err = portal.sendMessage(intent, eventType, content, message.CreatedAt.ToTime().Unix())
|
||||
if err != nil {
|
||||
portal.log.Errorfln("Failed to handle message %s: %v", "TODOID", err)
|
||||
return nil, true
|
||||
}
|
||||
|
||||
message.Text = strings.Replace(message.Text, attachment.URL, "", 1)
|
||||
return nil, true
|
||||
return content, true, nil
|
||||
case "file":
|
||||
fileData, fname, fmime := groupmeExt.DownloadFile(portal.Key.JID, attachment.FileID, source.Token)
|
||||
if fmime == "" {
|
||||
@ -1389,7 +1375,7 @@ func (portal *Portal) handleAttachment(intent *appservice.IntentAPI, attachment
|
||||
} else {
|
||||
err = fmt.Errorf("failed to upload media: %w", err)
|
||||
}
|
||||
return err, true
|
||||
return nil, true, err
|
||||
}
|
||||
|
||||
content := &event.MessageEventContent{
|
||||
@ -1416,21 +1402,34 @@ func (portal *Portal) handleAttachment(intent *appservice.IntentAPI, attachment
|
||||
} else {
|
||||
content.MsgType = event.MsgFile
|
||||
}
|
||||
_, _ = intent.UserTyping(portal.MXID, false, 0)
|
||||
|
||||
eventType := event.EventMessage
|
||||
|
||||
_, err = portal.sendMessage(intent, eventType, content, message.CreatedAt.ToTime().Unix())
|
||||
if err != nil {
|
||||
portal.log.Errorfln("Failed to handle message %s: %v", "TODOID", err)
|
||||
return nil, true
|
||||
return content, false, nil
|
||||
case "location":
|
||||
name := attachment.Name
|
||||
lat, _ := strconv.ParseFloat(attachment.Latitude, 64)
|
||||
lng, _ := strconv.ParseFloat(attachment.Longitude, 64)
|
||||
latChar := 'N'
|
||||
if lat < 0 {
|
||||
latChar = 'S'
|
||||
}
|
||||
return nil, false
|
||||
longChar := 'E'
|
||||
if lng < 0 {
|
||||
longChar = 'W'
|
||||
}
|
||||
formattedLoc := fmt.Sprintf("%.4f° %c %.4f° %c", math.Abs(lat), latChar, math.Abs(lng), longChar)
|
||||
|
||||
content := &event.MessageEventContent{
|
||||
MsgType: event.MsgLocation,
|
||||
Body: fmt.Sprintf("Location: %s\n%s", name, formattedLoc), //TODO link and stuff
|
||||
GeoURI: fmt.Sprintf("geo:%.5f,%.5f", lat, lng),
|
||||
}
|
||||
|
||||
return content, false, nil
|
||||
default:
|
||||
portal.log.Warnln("Unable to handle groupme attachment type", attachment.Type)
|
||||
return fmt.Errorf("Unable to handle groupme attachment type %s", attachment.Type), true
|
||||
return nil, true, fmt.Errorf("Unable to handle groupme attachment type %s", attachment.Type)
|
||||
}
|
||||
return nil, true
|
||||
return nil, true, errors.New("Unknown type")
|
||||
}
|
||||
func (portal *Portal) HandleMediaMessage(source *User, msg mediaMessage) {
|
||||
// intent := portal.startHandling(source, msg.info)
|
||||
@ -1583,11 +1582,26 @@ func (portal *Portal) HandleTextMessage(source *User, message *groupme.Message)
|
||||
}
|
||||
|
||||
sendText := true
|
||||
var sentID id.EventID
|
||||
for _, a := range message.Attachments {
|
||||
err, text := portal.handleAttachment(intent, a, source, message)
|
||||
msg, text, err := portal.handleAttachment(intent, a, source, message)
|
||||
|
||||
if err != nil {
|
||||
portal.log.Errorfln("Failed to handle message %s: %v", "TODOID", err)
|
||||
portal.sendMediaBridgeFailure(source, intent, *message, err)
|
||||
continue
|
||||
}
|
||||
if msg == nil {
|
||||
continue
|
||||
}
|
||||
resp, err := portal.sendMessage(intent, event.EventMessage, msg, message.CreatedAt.ToTime().Unix())
|
||||
if err != nil {
|
||||
portal.log.Errorfln("Failed to handle message %s: %v", "TODOID", err)
|
||||
portal.sendMediaBridgeFailure(source, intent, *message, err)
|
||||
continue
|
||||
}
|
||||
sentID = resp.EventID
|
||||
|
||||
sendText = sendText && text
|
||||
}
|
||||
|
||||
@ -1606,12 +1620,13 @@ func (portal *Portal) HandleTextMessage(source *User, message *groupme.Message)
|
||||
portal.log.Errorfln("Failed to handle message %s: %v", message.ID, err)
|
||||
return
|
||||
}
|
||||
sentID = resp.EventID
|
||||
|
||||
portal.finishHandling(source, message, resp.EventID)
|
||||
}
|
||||
portal.finishHandling(source, message, sentID)
|
||||
}
|
||||
|
||||
//func (portal *Portal) HandleLocationMessage(source *User, message whatsapp.LocationMessage) {
|
||||
func (portal *Portal) HandleLocationMessage(source *User, message whatsapp.LocationMessage) {
|
||||
// intent := portal.startHandling(source, message.Info)
|
||||
// if intent == nil {
|
||||
// return
|
||||
@ -1668,7 +1683,7 @@ func (portal *Portal) HandleTextMessage(source *User, message *groupme.Message)
|
||||
// return
|
||||
// }
|
||||
// portal.finishHandling(source, message.Info.Source, resp.EventID)
|
||||
//}
|
||||
}
|
||||
|
||||
//func (portal *Portal) HandleContactMessage(source *User, message whatsapp.ContactMessage) {
|
||||
// intent := portal.startHandling(source, message.Info)
|
||||
|
Loading…
Reference in New Issue
Block a user