From 89f1b826aee039158903878578ac1412d51634f4 Mon Sep 17 00:00:00 2001 From: Sumner Evans <sumner.evans@automattic.com> Date: Thu, 13 Feb 2025 13:37:01 -0700 Subject: [PATCH] connector: implement sending other media Signed-off-by: Sumner Evans <sumner.evans@automattic.com> --- pkg/connector/capabilities.go | 3 +- pkg/connector/matrix.go | 56 ++++------------------------------- pkg/linkedingo/messages.go | 10 ++++++- 3 files changed, 16 insertions(+), 53 deletions(-) diff --git a/pkg/connector/capabilities.go b/pkg/connector/capabilities.go index 73e215e..bb61c65 100644 --- a/pkg/connector/capabilities.go +++ b/pkg/connector/capabilities.go @@ -63,7 +63,8 @@ var fileCaps = event.FileFeatureMap{ event.MsgImage: { MimeTypes: map[string]event.CapabilitySupportLevel{ "image/jpeg": event.CapLevelFullySupported, - "image/png": event.CapLevelPartialSupport, + "image/png": event.CapLevelFullySupported, + "image/webp": event.CapLevelFullySupported, }, Caption: event.CapLevelFullySupported, MaxCaptionLength: MaxCaptionLength, diff --git a/pkg/connector/matrix.go b/pkg/connector/matrix.go index 16c3ca2..b8c9016 100644 --- a/pkg/connector/matrix.go +++ b/pkg/connector/matrix.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "os" - "strings" "maunium.net/go/mautrix/bridgev2" "maunium.net/go/mautrix/bridgev2/database" @@ -22,15 +21,6 @@ func getMediaFilename(content *event.MessageEventContent) (filename string) { } else { filename = content.Body } - if filename == "" { - return "image.jpg" // Assume it's a JPEG image - } - if content.MsgType == event.MsgImage && (!strings.HasSuffix(filename, ".jpg") && !strings.HasSuffix(filename, ".jpeg") && !strings.HasSuffix(filename, ".png")) { - if content.Info != nil && content.Info.MimeType != "" { - return filename + strings.TrimPrefix(content.Info.MimeType, "image/") - } - return filename + ".jpg" // Assume it's a JPEG - } return filename } @@ -49,10 +39,13 @@ func (l *LinkedInClient) HandleMatrixMessage(ctx context.Context, msg *bridgev2. } var renderContent []linkedingo.SendRenderContent - switch msg.Content.MsgType { - case event.MsgImage: + if msg.Content.MsgType.IsMedia() { err := l.main.Bridge.Bot.DownloadMediaToFile(ctx, msg.Content.URL, msg.Content.File, false, func(f *os.File) error { attachmentType := linkedingo.MediaUploadTypePhotoAttachment + if msg.Content.MsgType != event.MsgImage { + attachmentType = linkedingo.MediaUploadTypeFileAttachment + } + filename := getMediaFilename(msg.Content) urn, err := l.client.UploadMedia(ctx, attachmentType, filename, msg.Content.Info.MimeType, msg.Content.Info.Size, f) if err != nil { @@ -73,45 +66,6 @@ func (l *LinkedInClient) HandleMatrixMessage(ctx context.Context, msg *bridgev2. } } - // content := msg.Content - // - // switch content.MsgType { - // case event.MsgText: - // break - // case event.MsgVideo, event.MsgImage: - // if content.Body == content.FileName { - // sendMessagePayload.Message.Body.Text = "" - // } - // - // file := content.GetFile() - // data, err := lc.connector.br.Bot.DownloadMedia(ctx, file.URL, file) - // if err != nil { - // return nil, err - // } - // - // attachmentType := payloadold.MediaUploadFileAttachment - // if content.MsgType == event.MsgImage { - // attachmentType = payloadold.MediaUploadTypePhotoAttachment - // } - // - // mediaMetadata, err := lc.client.UploadMedia(attachmentType, content.FileName, data, typesold.ContentTypeJSONPlaintextUTF8) - // if err != nil { - // return nil, err - // } - // - // lc.client.Logger.Debug().Any("media_metadata", mediaMetadata).Msg("Successfully uploaded media to LinkedIn's servers") - // sendMessagePayload.Message.RenderContentUnions = append(sendMessagePayload.Message.RenderContentUnions, payloadold.RenderContent{ - // File: &payloadold.File{ - // AssetUrn: mediaMetadata.Urn, - // Name: content.FileName, - // MediaType: typesold.ContentType(content.Info.MimeType), - // ByteSize: len(data), - // }, - // }) - // default: - // return nil, fmt.Errorf("%w %s", bridgev2.ErrUnsupportedMessageType, content.MsgType) - // } - resp, err := l.client.SendMessage(ctx, conversationURN, matrixfmt.Parse(ctx, l.matrixParser, msg.Content), renderContent) if err != nil { return nil, err diff --git a/pkg/linkedingo/messages.go b/pkg/linkedingo/messages.go index beca1de..49e64e8 100644 --- a/pkg/linkedingo/messages.go +++ b/pkg/linkedingo/messages.go @@ -8,6 +8,7 @@ import ( "net/url" "github.com/google/uuid" + "go.mau.fi/util/jsontime" "go.mau.fi/util/random" "go.mau.fi/mautrix-linkedin/pkg/linkedingo/types" @@ -45,7 +46,14 @@ type AttributeType struct { } type SendRenderContent struct { - File *SendFile `json:"file,omitempty"` + Audio *SendAudio `json:"audio,omitempty"` + File *SendFile `json:"file,omitempty"` +} + +type SendAudio struct { + AssetURN types.URN `json:"assetUrn,omitempty"` + ByteSize int `json:"byteSize,omitempty"` + Duration jsontime.Milliseconds `json:"duration,omitempty"` } type SendFile struct { -- GitLab