Use beeper gallery types in mautrix-go

This commit is contained in:
Tulir Asokan 2023-09-02 13:29:48 +03:00
parent c9c13db0b3
commit a947eddcee
3 changed files with 16 additions and 27 deletions

2
go.mod
View file

@ -13,7 +13,7 @@ require (
golang.org/x/exp v0.0.0-20230811145659-89c5cff77bcb golang.org/x/exp v0.0.0-20230811145659-89c5cff77bcb
google.golang.org/protobuf v1.31.0 google.golang.org/protobuf v1.31.0
maunium.net/go/maulogger/v2 v2.4.1 maunium.net/go/maulogger/v2 v2.4.1
maunium.net/go/mautrix v0.16.1-0.20230821105106-ac5c2c22102c maunium.net/go/mautrix v0.16.1-0.20230902092631-691b96adc291
) )
require ( require (

4
go.sum
View file

@ -83,5 +83,5 @@ maunium.net/go/mauflag v1.0.0 h1:YiaRc0tEI3toYtJMRIfjP+jklH45uDHtT80nUamyD4M=
maunium.net/go/mauflag v1.0.0/go.mod h1:nLivPOpTpHnpzEh8jEdSL9UqO9+/KBJFmNRlwKfkPeA= maunium.net/go/mauflag v1.0.0/go.mod h1:nLivPOpTpHnpzEh8jEdSL9UqO9+/KBJFmNRlwKfkPeA=
maunium.net/go/maulogger/v2 v2.4.1 h1:N7zSdd0mZkB2m2JtFUsiGTQQAdP0YeFWT7YMc80yAL8= maunium.net/go/maulogger/v2 v2.4.1 h1:N7zSdd0mZkB2m2JtFUsiGTQQAdP0YeFWT7YMc80yAL8=
maunium.net/go/maulogger/v2 v2.4.1/go.mod h1:omPuYwYBILeVQobz8uO3XC8DIRuEb5rXYlQSuqrbCho= maunium.net/go/maulogger/v2 v2.4.1/go.mod h1:omPuYwYBILeVQobz8uO3XC8DIRuEb5rXYlQSuqrbCho=
maunium.net/go/mautrix v0.16.1-0.20230821105106-ac5c2c22102c h1:oRIaFbS4ds9biwJVguT+9Zu7n5zDbKQeuGklXHQxvCU= maunium.net/go/mautrix v0.16.1-0.20230902092631-691b96adc291 h1:ojDbR/zJvXz4pGdgIXhiIsUJo1HV9xVE5jVKZziKpVM=
maunium.net/go/mautrix v0.16.1-0.20230821105106-ac5c2c22102c/go.mod h1:XAjE9pTSGcr6vXaiNgQGiip7tddJ8FQV1a29u2QdBG4= maunium.net/go/mautrix v0.16.1-0.20230902092631-691b96adc291/go.mod h1:XAjE9pTSGcr6vXaiNgQGiip7tddJ8FQV1a29u2QdBG4=

View file

@ -20,7 +20,6 @@ import (
"bytes" "bytes"
"context" "context"
"encoding/base64" "encoding/base64"
"encoding/json"
"errors" "errors"
"fmt" "fmt"
"image" "image"
@ -480,6 +479,8 @@ func (portal *Portal) handleExistingMessageUpdate(ctx context.Context, source *U
} }
log.Debug(). log.Debug().
Str("old_status", dbMsg.Status.Type.String()). Str("old_status", dbMsg.Status.Type.String()).
Bool("has_pending_media", hasPendingMedia).
Bool("updated_media_is_complete", updatedMediaIsComplete).
Msg("Message status changed") Msg("Message status changed")
switch { switch {
case newStatus == gmproto.MessageStatusType_MESSAGE_DELETED: case newStatus == gmproto.MessageStatusType_MESSAGE_DELETED:
@ -1046,13 +1047,12 @@ func (msg *ConvertedMessage) MergeGallery() {
ID: msg.Parts[0].ID, ID: msg.Parts[0].ID,
PendingMedia: pendingMedia, PendingMedia: pendingMedia,
Content: &event.MessageEventContent{ Content: &event.MessageEventContent{
MsgType: "com.beeper.gallery", MsgType: event.MsgBeeperGallery,
Body: "Sent a gallery", Body: "Sent a gallery",
},
Extra: map[string]any{ BeeperGalleryImages: imageParts,
"com.beeper.gallery.images": imageParts, BeeperGalleryCaption: caption,
"com.beeper.gallery.caption": caption, BeeperGalleryCaptionHTML: captionHTML,
"com.beeper.gallery.caption_html": captionHTML,
}, },
}} }}
} }
@ -1734,13 +1734,7 @@ func (portal *Portal) uploadMedia(intent *appservice.IntentAPI, data []byte, con
return nil return nil
} }
type beeperGalleryContent struct { func (portal *Portal) convertMatrixMessage(ctx context.Context, sender *User, content *event.MessageEventContent, txnID string) (*gmproto.SendMessageRequest, error) {
Caption string `json:"com.beeper.gallery.caption,omitempty"`
CaptionHTML string `json:"com.beeper.gallery.caption_html,omitempty"`
Images []*event.MessageEventContent `json:"com.beeper.gallery.images,omitempty"`
}
func (portal *Portal) convertMatrixMessage(ctx context.Context, sender *User, content *event.MessageEventContent, evt *event.Event, txnID string) (*gmproto.SendMessageRequest, error) {
log := zerolog.Ctx(ctx) log := zerolog.Ctx(ctx)
req := &gmproto.SendMessageRequest{ req := &gmproto.SendMessageRequest{
ConversationID: portal.ID, ConversationID: portal.ID,
@ -1786,13 +1780,8 @@ func (portal *Portal) convertMatrixMessage(ctx context.Context, sender *User, co
req.MessagePayload.MessageInfo = []*gmproto.MessageInfo{{ req.MessagePayload.MessageInfo = []*gmproto.MessageInfo{{
Data: &gmproto.MessageInfo_MediaContent{MediaContent: resp}, Data: &gmproto.MessageInfo_MediaContent{MediaContent: resp},
}} }}
case "com.beeper.gallery": case event.MsgBeeperGallery:
var parsed beeperGalleryContent for i, part := range content.BeeperGalleryImages {
err := json.Unmarshal(evt.Content.VeryRaw, &parsed)
if err != nil {
return nil, fmt.Errorf("failed to parse gallery: %w", err)
}
for i, part := range parsed.Images {
convertedPart, err := portal.reuploadMedia(ctx, sender, part) convertedPart, err := portal.reuploadMedia(ctx, sender, part)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to reupload gallery image #%d: %w", i+1, err) return nil, fmt.Errorf("failed to reupload gallery image #%d: %w", i+1, err)
@ -1801,10 +1790,10 @@ func (portal *Portal) convertMatrixMessage(ctx context.Context, sender *User, co
Data: &gmproto.MessageInfo_MediaContent{MediaContent: convertedPart}, Data: &gmproto.MessageInfo_MediaContent{MediaContent: convertedPart},
}) })
} }
if parsed.Caption != "" { if content.BeeperGalleryCaption != "" {
req.MessagePayload.MessageInfo = append(req.MessagePayload.MessageInfo, &gmproto.MessageInfo{ req.MessagePayload.MessageInfo = append(req.MessagePayload.MessageInfo, &gmproto.MessageInfo{
Data: &gmproto.MessageInfo_MessageContent{MessageContent: &gmproto.MessageContent{ Data: &gmproto.MessageInfo_MessageContent{MessageContent: &gmproto.MessageContent{
Content: parsed.Caption, Content: content.BeeperGalleryCaption,
}}, }},
}) })
} }
@ -1872,7 +1861,7 @@ func (portal *Portal) HandleMatrixMessage(sender *User, evt *event.Event, timing
} }
start := time.Now() start := time.Now()
req, err := portal.convertMatrixMessage(ctx, sender, content, evt, txnID) req, err := portal.convertMatrixMessage(ctx, sender, content, txnID)
timings.convert = time.Since(start) timings.convert = time.Since(start)
if err != nil { if err != nil {
go ms.sendMessageMetrics(sender, evt, err, "Error converting", true) go ms.sendMessageMetrics(sender, evt, err, "Error converting", true)